b1fd00d9a5c7f4d476581ae887c71e7b25fd8e78
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / i2c / i2c-core.c
1 /* 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
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
24
25 #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>
32 #include <linux/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <asm/uaccess.h>
37
38 #include "i2c-core.h"
39
40
41 static DEFINE_MUTEX(core_lock);
42 static DEFINE_IDR(i2c_adapter_idr);
43
44 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
45 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
46
47 /* ------------------------------------------------------------------------- */
48
49 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
50 const struct i2c_client *client)
51 {
52 while (id->name[0]) {
53 if (strcmp(client->name, id->name) == 0)
54 return id;
55 id++;
56 }
57 return NULL;
58 }
59
60 static int i2c_device_match(struct device *dev, struct device_driver *drv)
61 {
62 struct i2c_client *client = to_i2c_client(dev);
63 struct i2c_driver *driver = to_i2c_driver(drv);
64
65 /* match on an id table if there is one */
66 if (driver->id_table)
67 return i2c_match_id(driver->id_table, client) != NULL;
68
69 return 0;
70 }
71
72 #ifdef CONFIG_HOTPLUG
73
74 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
75 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
76 {
77 struct i2c_client *client = to_i2c_client(dev);
78
79 if (add_uevent_var(env, "MODALIAS=%s%s",
80 I2C_MODULE_PREFIX, client->name))
81 return -ENOMEM;
82 dev_dbg(dev, "uevent\n");
83 return 0;
84 }
85
86 #else
87 #define i2c_device_uevent NULL
88 #endif /* CONFIG_HOTPLUG */
89
90 static int i2c_device_probe(struct device *dev)
91 {
92 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
94 int status;
95
96 if (!driver->probe || !driver->id_table)
97 return -ENODEV;
98 client->driver = driver;
99 if (!device_can_wakeup(&client->dev))
100 device_init_wakeup(&client->dev,
101 client->flags & I2C_CLIENT_WAKE);
102 dev_dbg(dev, "probe\n");
103
104 status = driver->probe(client, i2c_match_id(driver->id_table, client));
105 if (status)
106 client->driver = NULL;
107 return status;
108 }
109
110 static int i2c_device_remove(struct device *dev)
111 {
112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver;
114 int status;
115
116 if (!dev->driver)
117 return 0;
118
119 driver = to_i2c_driver(dev->driver);
120 if (driver->remove) {
121 dev_dbg(dev, "remove\n");
122 status = driver->remove(client);
123 } else {
124 dev->driver = NULL;
125 status = 0;
126 }
127 if (status == 0)
128 client->driver = NULL;
129 return status;
130 }
131
132 static void i2c_device_shutdown(struct device *dev)
133 {
134 struct i2c_driver *driver;
135
136 if (!dev->driver)
137 return;
138 driver = to_i2c_driver(dev->driver);
139 if (driver->shutdown)
140 driver->shutdown(to_i2c_client(dev));
141 }
142
143 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
144 {
145 struct i2c_driver *driver;
146
147 if (!dev->driver)
148 return 0;
149 driver = to_i2c_driver(dev->driver);
150 if (!driver->suspend)
151 return 0;
152 return driver->suspend(to_i2c_client(dev), mesg);
153 }
154
155 static int i2c_device_resume(struct device *dev)
156 {
157 struct i2c_driver *driver;
158
159 if (!dev->driver)
160 return 0;
161 driver = to_i2c_driver(dev->driver);
162 if (!driver->resume)
163 return 0;
164 return driver->resume(to_i2c_client(dev));
165 }
166
167 static void i2c_client_dev_release(struct device *dev)
168 {
169 kfree(to_i2c_client(dev));
170 }
171
172 static ssize_t
173 show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
174 {
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
177 }
178
179 static ssize_t
180 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
181 {
182 struct i2c_client *client = to_i2c_client(dev);
183 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
184 }
185
186 static struct device_attribute i2c_dev_attrs[] = {
187 __ATTR(name, S_IRUGO, show_client_name, NULL),
188 /* modalias helps coldplug: modprobe $(cat .../modalias) */
189 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
190 { },
191 };
192
193 struct bus_type i2c_bus_type = {
194 .name = "i2c",
195 .dev_attrs = i2c_dev_attrs,
196 .match = i2c_device_match,
197 .uevent = i2c_device_uevent,
198 .probe = i2c_device_probe,
199 .remove = i2c_device_remove,
200 .shutdown = i2c_device_shutdown,
201 .suspend = i2c_device_suspend,
202 .resume = i2c_device_resume,
203 };
204 EXPORT_SYMBOL_GPL(i2c_bus_type);
205
206
207 /**
208 * i2c_verify_client - return parameter as i2c_client, or NULL
209 * @dev: device, probably from some driver model iterator
210 *
211 * When traversing the driver model tree, perhaps using driver model
212 * iterators like @device_for_each_child(), you can't assume very much
213 * about the nodes you find. Use this function to avoid oopses caused
214 * by wrongly treating some non-I2C device as an i2c_client.
215 */
216 struct i2c_client *i2c_verify_client(struct device *dev)
217 {
218 return (dev->bus == &i2c_bus_type)
219 ? to_i2c_client(dev)
220 : NULL;
221 }
222 EXPORT_SYMBOL(i2c_verify_client);
223
224
225 /**
226 * i2c_new_device - instantiate an i2c device
227 * @adap: the adapter managing the device
228 * @info: describes one I2C device; bus_num is ignored
229 * Context: can sleep
230 *
231 * Create an i2c device. Binding is handled through driver model
232 * probe()/remove() methods. A driver may be bound to this device when we
233 * return from this function, or any later moment (e.g. maybe hotplugging will
234 * load the driver module). This call is not appropriate for use by mainboard
235 * initialization logic, which usually runs during an arch_initcall() long
236 * before any i2c_adapter could exist.
237 *
238 * This returns the new i2c client, which may be saved for later use with
239 * i2c_unregister_device(); or NULL to indicate an error.
240 */
241 struct i2c_client *
242 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
243 {
244 struct i2c_client *client;
245 int status;
246
247 client = kzalloc(sizeof *client, GFP_KERNEL);
248 if (!client)
249 return NULL;
250
251 client->adapter = adap;
252
253 client->dev.platform_data = info->platform_data;
254
255 if (info->archdata)
256 client->dev.archdata = *info->archdata;
257
258 client->flags = info->flags;
259 client->addr = info->addr;
260 client->irq = info->irq;
261
262 strlcpy(client->name, info->type, sizeof(client->name));
263
264 /* Check for address business */
265 status = i2c_check_addr(adap, client->addr);
266 if (status)
267 goto out_err;
268
269 client->dev.parent = &client->adapter->dev;
270 client->dev.bus = &i2c_bus_type;
271 client->dev.release = i2c_client_dev_release;
272
273 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
274 client->addr);
275 status = device_register(&client->dev);
276 if (status)
277 goto out_err;
278
279 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
280 client->name, dev_name(&client->dev));
281
282 return client;
283
284 out_err:
285 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
286 "(%d)\n", client->name, client->addr, status);
287 kfree(client);
288 return NULL;
289 }
290 EXPORT_SYMBOL_GPL(i2c_new_device);
291
292
293 /**
294 * i2c_unregister_device - reverse effect of i2c_new_device()
295 * @client: value returned from i2c_new_device()
296 * Context: can sleep
297 */
298 void i2c_unregister_device(struct i2c_client *client)
299 {
300 device_unregister(&client->dev);
301 }
302 EXPORT_SYMBOL_GPL(i2c_unregister_device);
303
304
305 static const struct i2c_device_id dummy_id[] = {
306 { "dummy", 0 },
307 { },
308 };
309
310 static int dummy_probe(struct i2c_client *client,
311 const struct i2c_device_id *id)
312 {
313 return 0;
314 }
315
316 static int dummy_remove(struct i2c_client *client)
317 {
318 return 0;
319 }
320
321 static struct i2c_driver dummy_driver = {
322 .driver.name = "dummy",
323 .probe = dummy_probe,
324 .remove = dummy_remove,
325 .id_table = dummy_id,
326 };
327
328 /**
329 * i2c_new_dummy - return a new i2c device bound to a dummy driver
330 * @adapter: the adapter managing the device
331 * @address: seven bit address to be used
332 * Context: can sleep
333 *
334 * This returns an I2C client bound to the "dummy" driver, intended for use
335 * with devices that consume multiple addresses. Examples of such chips
336 * include various EEPROMS (like 24c04 and 24c08 models).
337 *
338 * These dummy devices have two main uses. First, most I2C and SMBus calls
339 * except i2c_transfer() need a client handle; the dummy will be that handle.
340 * And second, this prevents the specified address from being bound to a
341 * different driver.
342 *
343 * This returns the new i2c client, which should be saved for later use with
344 * i2c_unregister_device(); or NULL to indicate an error.
345 */
346 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
347 {
348 struct i2c_board_info info = {
349 I2C_BOARD_INFO("dummy", address),
350 };
351
352 return i2c_new_device(adapter, &info);
353 }
354 EXPORT_SYMBOL_GPL(i2c_new_dummy);
355
356 /* ------------------------------------------------------------------------- */
357
358 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
359
360 static void i2c_adapter_dev_release(struct device *dev)
361 {
362 struct i2c_adapter *adap = to_i2c_adapter(dev);
363 complete(&adap->dev_released);
364 }
365
366 static ssize_t
367 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
368 {
369 struct i2c_adapter *adap = to_i2c_adapter(dev);
370 return sprintf(buf, "%s\n", adap->name);
371 }
372
373 static struct device_attribute i2c_adapter_attrs[] = {
374 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
375 { },
376 };
377
378 static struct class i2c_adapter_class = {
379 .owner = THIS_MODULE,
380 .name = "i2c-adapter",
381 .dev_attrs = i2c_adapter_attrs,
382 };
383
384 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
385 {
386 struct i2c_devinfo *devinfo;
387
388 mutex_lock(&__i2c_board_lock);
389 list_for_each_entry(devinfo, &__i2c_board_list, list) {
390 if (devinfo->busnum == adapter->nr
391 && !i2c_new_device(adapter,
392 &devinfo->board_info))
393 dev_err(&adapter->dev,
394 "Can't create device at 0x%02x\n",
395 devinfo->board_info.addr);
396 }
397 mutex_unlock(&__i2c_board_lock);
398 }
399
400 static int i2c_do_add_adapter(struct device_driver *d, void *data)
401 {
402 struct i2c_driver *driver = to_i2c_driver(d);
403 struct i2c_adapter *adap = data;
404
405 /* Detect supported devices on that bus, and instantiate them */
406 i2c_detect(adap, driver);
407
408 /* Let legacy drivers scan this bus for matching devices */
409 if (driver->attach_adapter) {
410 /* We ignore the return code; if it fails, too bad */
411 driver->attach_adapter(adap);
412 }
413 return 0;
414 }
415
416 static int i2c_register_adapter(struct i2c_adapter *adap)
417 {
418 int res = 0, dummy;
419
420 /* Can't register until after driver model init */
421 if (unlikely(WARN_ON(!i2c_bus_type.p)))
422 return -EAGAIN;
423
424 mutex_init(&adap->bus_lock);
425
426 mutex_lock(&core_lock);
427
428 /* Set default timeout to 1 second if not already set */
429 if (adap->timeout == 0)
430 adap->timeout = HZ;
431
432 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
433 adap->dev.release = &i2c_adapter_dev_release;
434 adap->dev.class = &i2c_adapter_class;
435 res = device_register(&adap->dev);
436 if (res)
437 goto out_list;
438
439 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
440
441 /* create pre-declared device nodes */
442 if (adap->nr < __i2c_first_dynamic_bus_num)
443 i2c_scan_static_board_info(adap);
444
445 /* Notify drivers */
446 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
447 i2c_do_add_adapter);
448
449 out_unlock:
450 mutex_unlock(&core_lock);
451 return res;
452
453 out_list:
454 idr_remove(&i2c_adapter_idr, adap->nr);
455 goto out_unlock;
456 }
457
458 /**
459 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
460 * @adapter: the adapter to add
461 * Context: can sleep
462 *
463 * This routine is used to declare an I2C adapter when its bus number
464 * doesn't matter. Examples: for I2C adapters dynamically added by
465 * USB links or PCI plugin cards.
466 *
467 * When this returns zero, a new bus number was allocated and stored
468 * in adap->nr, and the specified adapter became available for clients.
469 * Otherwise, a negative errno value is returned.
470 */
471 int i2c_add_adapter(struct i2c_adapter *adapter)
472 {
473 int id, res = 0;
474
475 retry:
476 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
477 return -ENOMEM;
478
479 mutex_lock(&core_lock);
480 /* "above" here means "above or equal to", sigh */
481 res = idr_get_new_above(&i2c_adapter_idr, adapter,
482 __i2c_first_dynamic_bus_num, &id);
483 mutex_unlock(&core_lock);
484
485 if (res < 0) {
486 if (res == -EAGAIN)
487 goto retry;
488 return res;
489 }
490
491 adapter->nr = id;
492 return i2c_register_adapter(adapter);
493 }
494 EXPORT_SYMBOL(i2c_add_adapter);
495
496 /**
497 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
498 * @adap: the adapter to register (with adap->nr initialized)
499 * Context: can sleep
500 *
501 * This routine is used to declare an I2C adapter when its bus number
502 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
503 * or otherwise built in to the system's mainboard, and where i2c_board_info
504 * is used to properly configure I2C devices.
505 *
506 * If no devices have pre-been declared for this bus, then be sure to
507 * register the adapter before any dynamically allocated ones. Otherwise
508 * the required bus ID may not be available.
509 *
510 * When this returns zero, the specified adapter became available for
511 * clients using the bus number provided in adap->nr. Also, the table
512 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
513 * and the appropriate driver model device nodes are created. Otherwise, a
514 * negative errno value is returned.
515 */
516 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
517 {
518 int id;
519 int status;
520
521 if (adap->nr & ~MAX_ID_MASK)
522 return -EINVAL;
523
524 retry:
525 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
526 return -ENOMEM;
527
528 mutex_lock(&core_lock);
529 /* "above" here means "above or equal to", sigh;
530 * we need the "equal to" result to force the result
531 */
532 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
533 if (status == 0 && id != adap->nr) {
534 status = -EBUSY;
535 idr_remove(&i2c_adapter_idr, id);
536 }
537 mutex_unlock(&core_lock);
538 if (status == -EAGAIN)
539 goto retry;
540
541 if (status == 0)
542 status = i2c_register_adapter(adap);
543 return status;
544 }
545 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
546
547 static int i2c_do_del_adapter(struct device_driver *d, void *data)
548 {
549 struct i2c_driver *driver = to_i2c_driver(d);
550 struct i2c_adapter *adapter = data;
551 struct i2c_client *client, *_n;
552 int res;
553
554 /* Remove the devices we created ourselves as the result of hardware
555 * probing (using a driver's detect method) */
556 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
557 if (client->adapter == adapter) {
558 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
559 client->name, client->addr);
560 list_del(&client->detected);
561 i2c_unregister_device(client);
562 }
563 }
564
565 if (!driver->detach_adapter)
566 return 0;
567 res = driver->detach_adapter(adapter);
568 if (res)
569 dev_err(&adapter->dev, "detach_adapter failed (%d) "
570 "for driver [%s]\n", res, driver->driver.name);
571 return res;
572 }
573
574 static int __unregister_client(struct device *dev, void *dummy)
575 {
576 struct i2c_client *client = i2c_verify_client(dev);
577 if (client)
578 i2c_unregister_device(client);
579 return 0;
580 }
581
582 /**
583 * i2c_del_adapter - unregister I2C adapter
584 * @adap: the adapter being unregistered
585 * Context: can sleep
586 *
587 * This unregisters an I2C adapter which was previously registered
588 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
589 */
590 int i2c_del_adapter(struct i2c_adapter *adap)
591 {
592 int res = 0;
593
594 mutex_lock(&core_lock);
595
596 /* First make sure that this adapter was ever added */
597 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
598 pr_debug("i2c-core: attempting to delete unregistered "
599 "adapter [%s]\n", adap->name);
600 res = -EINVAL;
601 goto out_unlock;
602 }
603
604 /* Tell drivers about this removal */
605 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
606 i2c_do_del_adapter);
607 if (res)
608 goto out_unlock;
609
610 /* Detach any active clients. This can't fail, thus we do not
611 checking the returned value. */
612 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
613
614 /* clean up the sysfs representation */
615 init_completion(&adap->dev_released);
616 device_unregister(&adap->dev);
617
618 /* wait for sysfs to drop all references */
619 wait_for_completion(&adap->dev_released);
620
621 /* free bus id */
622 idr_remove(&i2c_adapter_idr, adap->nr);
623
624 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
625
626 /* Clear the device structure in case this adapter is ever going to be
627 added again */
628 memset(&adap->dev, 0, sizeof(adap->dev));
629
630 out_unlock:
631 mutex_unlock(&core_lock);
632 return res;
633 }
634 EXPORT_SYMBOL(i2c_del_adapter);
635
636
637 /* ------------------------------------------------------------------------- */
638
639 static int __attach_adapter(struct device *dev, void *data)
640 {
641 struct i2c_adapter *adapter = to_i2c_adapter(dev);
642 struct i2c_driver *driver = data;
643
644 i2c_detect(adapter, driver);
645
646 /* Legacy drivers scan i2c busses directly */
647 if (driver->attach_adapter)
648 driver->attach_adapter(adapter);
649
650 return 0;
651 }
652
653 /*
654 * An i2c_driver is used with one or more i2c_client (device) nodes to access
655 * i2c slave chips, on a bus instance associated with some i2c_adapter.
656 */
657
658 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
659 {
660 int res;
661
662 /* Can't register until after driver model init */
663 if (unlikely(WARN_ON(!i2c_bus_type.p)))
664 return -EAGAIN;
665
666 /* add the driver to the list of i2c drivers in the driver core */
667 driver->driver.owner = owner;
668 driver->driver.bus = &i2c_bus_type;
669
670 /* When registration returns, the driver core
671 * will have called probe() for all matching-but-unbound devices.
672 */
673 res = driver_register(&driver->driver);
674 if (res)
675 return res;
676
677 mutex_lock(&core_lock);
678
679 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
680
681 INIT_LIST_HEAD(&driver->clients);
682 /* Walk the adapters that are already present */
683 class_for_each_device(&i2c_adapter_class, NULL, driver,
684 __attach_adapter);
685
686 mutex_unlock(&core_lock);
687 return 0;
688 }
689 EXPORT_SYMBOL(i2c_register_driver);
690
691 static int __detach_adapter(struct device *dev, void *data)
692 {
693 struct i2c_adapter *adapter = to_i2c_adapter(dev);
694 struct i2c_driver *driver = data;
695 struct i2c_client *client, *_n;
696
697 /* Remove the devices we created ourselves as the result of hardware
698 * probing (using a driver's detect method) */
699 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
700 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
701 client->name, client->addr);
702 list_del(&client->detected);
703 i2c_unregister_device(client);
704 }
705
706 if (driver->detach_adapter) {
707 if (driver->detach_adapter(adapter))
708 dev_err(&adapter->dev,
709 "detach_adapter failed for driver [%s]\n",
710 driver->driver.name);
711 }
712
713 return 0;
714 }
715
716 /**
717 * i2c_del_driver - unregister I2C driver
718 * @driver: the driver being unregistered
719 * Context: can sleep
720 */
721 void i2c_del_driver(struct i2c_driver *driver)
722 {
723 mutex_lock(&core_lock);
724
725 class_for_each_device(&i2c_adapter_class, NULL, driver,
726 __detach_adapter);
727
728 driver_unregister(&driver->driver);
729 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
730
731 mutex_unlock(&core_lock);
732 }
733 EXPORT_SYMBOL(i2c_del_driver);
734
735 /* ------------------------------------------------------------------------- */
736
737 static int __i2c_check_addr(struct device *dev, void *addrp)
738 {
739 struct i2c_client *client = i2c_verify_client(dev);
740 int addr = *(int *)addrp;
741
742 if (client && client->addr == addr)
743 return -EBUSY;
744 return 0;
745 }
746
747 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
748 {
749 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
750 }
751
752 /**
753 * i2c_use_client - increments the reference count of the i2c client structure
754 * @client: the client being referenced
755 *
756 * Each live reference to a client should be refcounted. The driver model does
757 * that automatically as part of driver binding, so that most drivers don't
758 * need to do this explicitly: they hold a reference until they're unbound
759 * from the device.
760 *
761 * A pointer to the client with the incremented reference counter is returned.
762 */
763 struct i2c_client *i2c_use_client(struct i2c_client *client)
764 {
765 if (client && get_device(&client->dev))
766 return client;
767 return NULL;
768 }
769 EXPORT_SYMBOL(i2c_use_client);
770
771 /**
772 * i2c_release_client - release a use of the i2c client structure
773 * @client: the client being no longer referenced
774 *
775 * Must be called when a user of a client is finished with it.
776 */
777 void i2c_release_client(struct i2c_client *client)
778 {
779 if (client)
780 put_device(&client->dev);
781 }
782 EXPORT_SYMBOL(i2c_release_client);
783
784 struct i2c_cmd_arg {
785 unsigned cmd;
786 void *arg;
787 };
788
789 static int i2c_cmd(struct device *dev, void *_arg)
790 {
791 struct i2c_client *client = i2c_verify_client(dev);
792 struct i2c_cmd_arg *arg = _arg;
793
794 if (client && client->driver && client->driver->command)
795 client->driver->command(client, arg->cmd, arg->arg);
796 return 0;
797 }
798
799 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
800 {
801 struct i2c_cmd_arg cmd_arg;
802
803 cmd_arg.cmd = cmd;
804 cmd_arg.arg = arg;
805 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
806 }
807 EXPORT_SYMBOL(i2c_clients_command);
808
809 static int __init i2c_init(void)
810 {
811 int retval;
812
813 retval = bus_register(&i2c_bus_type);
814 if (retval)
815 return retval;
816 retval = class_register(&i2c_adapter_class);
817 if (retval)
818 goto bus_err;
819 retval = i2c_add_driver(&dummy_driver);
820 if (retval)
821 goto class_err;
822 return 0;
823
824 class_err:
825 class_unregister(&i2c_adapter_class);
826 bus_err:
827 bus_unregister(&i2c_bus_type);
828 return retval;
829 }
830
831 static void __exit i2c_exit(void)
832 {
833 i2c_del_driver(&dummy_driver);
834 class_unregister(&i2c_adapter_class);
835 bus_unregister(&i2c_bus_type);
836 }
837
838 /* We must initialize early, because some subsystems register i2c drivers
839 * in subsys_initcall() code, but are linked (and initialized) before i2c.
840 */
841 postcore_initcall(i2c_init);
842 module_exit(i2c_exit);
843
844 /* ----------------------------------------------------
845 * the functional interface to the i2c busses.
846 * ----------------------------------------------------
847 */
848
849 /**
850 * i2c_transfer - execute a single or combined I2C message
851 * @adap: Handle to I2C bus
852 * @msgs: One or more messages to execute before STOP is issued to
853 * terminate the operation; each message begins with a START.
854 * @num: Number of messages to be executed.
855 *
856 * Returns negative errno, else the number of messages executed.
857 *
858 * Note that there is no requirement that each message be sent to
859 * the same slave address, although that is the most common model.
860 */
861 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
862 {
863 unsigned long orig_jiffies;
864 int ret, try;
865
866 /* REVISIT the fault reporting model here is weak:
867 *
868 * - When we get an error after receiving N bytes from a slave,
869 * there is no way to report "N".
870 *
871 * - When we get a NAK after transmitting N bytes to a slave,
872 * there is no way to report "N" ... or to let the master
873 * continue executing the rest of this combined message, if
874 * that's the appropriate response.
875 *
876 * - When for example "num" is two and we successfully complete
877 * the first message but get an error part way through the
878 * second, it's unclear whether that should be reported as
879 * one (discarding status on the second message) or errno
880 * (discarding status on the first one).
881 */
882
883 if (adap->algo->master_xfer) {
884 #ifdef DEBUG
885 for (ret = 0; ret < num; ret++) {
886 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
887 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
888 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
889 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
890 }
891 #endif
892
893 if (in_atomic() || irqs_disabled()) {
894 ret = mutex_trylock(&adap->bus_lock);
895 if (!ret)
896 /* I2C activity is ongoing. */
897 return -EAGAIN;
898 } else {
899 mutex_lock_nested(&adap->bus_lock, adap->level);
900 }
901
902 /* Retry automatically on arbitration loss */
903 orig_jiffies = jiffies;
904 for (ret = 0, try = 0; try <= adap->retries; try++) {
905 ret = adap->algo->master_xfer(adap, msgs, num);
906 if (ret != -EAGAIN)
907 break;
908 if (time_after(jiffies, orig_jiffies + adap->timeout))
909 break;
910 }
911 mutex_unlock(&adap->bus_lock);
912
913 return ret;
914 } else {
915 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
916 return -EOPNOTSUPP;
917 }
918 }
919 EXPORT_SYMBOL(i2c_transfer);
920
921 /**
922 * i2c_master_send - issue a single I2C message in master transmit mode
923 * @client: Handle to slave device
924 * @buf: Data that will be written to the slave
925 * @count: How many bytes to write
926 *
927 * Returns negative errno, or else the number of bytes written.
928 */
929 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
930 {
931 int ret;
932 struct i2c_adapter *adap=client->adapter;
933 struct i2c_msg msg;
934
935 msg.addr = client->addr;
936 msg.flags = client->flags & I2C_M_TEN;
937 msg.len = count;
938 msg.buf = (char *)buf;
939
940 ret = i2c_transfer(adap, &msg, 1);
941
942 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
943 transmitted, else error code. */
944 return (ret == 1) ? count : ret;
945 }
946 EXPORT_SYMBOL(i2c_master_send);
947
948 /**
949 * i2c_master_recv - issue a single I2C message in master receive mode
950 * @client: Handle to slave device
951 * @buf: Where to store data read from slave
952 * @count: How many bytes to read
953 *
954 * Returns negative errno, or else the number of bytes read.
955 */
956 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
957 {
958 struct i2c_adapter *adap=client->adapter;
959 struct i2c_msg msg;
960 int ret;
961
962 msg.addr = client->addr;
963 msg.flags = client->flags & I2C_M_TEN;
964 msg.flags |= I2C_M_RD;
965 msg.len = count;
966 msg.buf = buf;
967
968 ret = i2c_transfer(adap, &msg, 1);
969
970 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
971 transmitted, else error code. */
972 return (ret == 1) ? count : ret;
973 }
974 EXPORT_SYMBOL(i2c_master_recv);
975
976 /* ----------------------------------------------------
977 * the i2c address scanning function
978 * Will not work for 10-bit addresses!
979 * ----------------------------------------------------
980 */
981
982 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
983 struct i2c_driver *driver)
984 {
985 struct i2c_board_info info;
986 struct i2c_adapter *adapter = temp_client->adapter;
987 int addr = temp_client->addr;
988 int err;
989
990 /* Make sure the address is valid */
991 if (addr < 0x03 || addr > 0x77) {
992 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
993 addr);
994 return -EINVAL;
995 }
996
997 /* Skip if already in use */
998 if (i2c_check_addr(adapter, addr))
999 return 0;
1000
1001 /* Make sure there is something at this address, unless forced */
1002 if (kind < 0) {
1003 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1004 I2C_SMBUS_QUICK, NULL) < 0)
1005 return 0;
1006
1007 /* prevent 24RF08 corruption */
1008 if ((addr & ~0x0f) == 0x50)
1009 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1010 I2C_SMBUS_QUICK, NULL);
1011 }
1012
1013 /* Finally call the custom detection function */
1014 memset(&info, 0, sizeof(struct i2c_board_info));
1015 info.addr = addr;
1016 err = driver->detect(temp_client, kind, &info);
1017 if (err) {
1018 /* -ENODEV is returned if the detection fails. We catch it
1019 here as this isn't an error. */
1020 return err == -ENODEV ? 0 : err;
1021 }
1022
1023 /* Consistency check */
1024 if (info.type[0] == '\0') {
1025 dev_err(&adapter->dev, "%s detection function provided "
1026 "no name for 0x%x\n", driver->driver.name,
1027 addr);
1028 } else {
1029 struct i2c_client *client;
1030
1031 /* Detection succeeded, instantiate the device */
1032 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1033 info.type, info.addr);
1034 client = i2c_new_device(adapter, &info);
1035 if (client)
1036 list_add_tail(&client->detected, &driver->clients);
1037 else
1038 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1039 info.type, info.addr);
1040 }
1041 return 0;
1042 }
1043
1044 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1045 {
1046 const struct i2c_client_address_data *address_data;
1047 struct i2c_client *temp_client;
1048 int i, err = 0;
1049 int adap_id = i2c_adapter_id(adapter);
1050
1051 address_data = driver->address_data;
1052 if (!driver->detect || !address_data)
1053 return 0;
1054
1055 /* Set up a temporary client to help detect callback */
1056 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1057 if (!temp_client)
1058 return -ENOMEM;
1059 temp_client->adapter = adapter;
1060
1061 /* Force entries are done first, and are not affected by ignore
1062 entries */
1063 if (address_data->forces) {
1064 const unsigned short * const *forces = address_data->forces;
1065 int kind;
1066
1067 for (kind = 0; forces[kind]; kind++) {
1068 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1069 i += 2) {
1070 if (forces[kind][i] == adap_id
1071 || forces[kind][i] == ANY_I2C_BUS) {
1072 dev_dbg(&adapter->dev, "found force "
1073 "parameter for adapter %d, "
1074 "addr 0x%02x, kind %d\n",
1075 adap_id, forces[kind][i + 1],
1076 kind);
1077 temp_client->addr = forces[kind][i + 1];
1078 err = i2c_detect_address(temp_client,
1079 kind, driver);
1080 if (err)
1081 goto exit_free;
1082 }
1083 }
1084 }
1085 }
1086
1087 /* Stop here if the classes do not match */
1088 if (!(adapter->class & driver->class))
1089 goto exit_free;
1090
1091 /* Stop here if we can't use SMBUS_QUICK */
1092 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1093 if (address_data->probe[0] == I2C_CLIENT_END
1094 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1095 goto exit_free;
1096
1097 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1098 "can't probe for chips\n");
1099 err = -EOPNOTSUPP;
1100 goto exit_free;
1101 }
1102
1103 /* Probe entries are done second, and are not affected by ignore
1104 entries either */
1105 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1106 if (address_data->probe[i] == adap_id
1107 || address_data->probe[i] == ANY_I2C_BUS) {
1108 dev_dbg(&adapter->dev, "found probe parameter for "
1109 "adapter %d, addr 0x%02x\n", adap_id,
1110 address_data->probe[i + 1]);
1111 temp_client->addr = address_data->probe[i + 1];
1112 err = i2c_detect_address(temp_client, -1, driver);
1113 if (err)
1114 goto exit_free;
1115 }
1116 }
1117
1118 /* Normal entries are done last, unless shadowed by an ignore entry */
1119 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1120 int j, ignore;
1121
1122 ignore = 0;
1123 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1124 j += 2) {
1125 if ((address_data->ignore[j] == adap_id ||
1126 address_data->ignore[j] == ANY_I2C_BUS)
1127 && address_data->ignore[j + 1]
1128 == address_data->normal_i2c[i]) {
1129 dev_dbg(&adapter->dev, "found ignore "
1130 "parameter for adapter %d, "
1131 "addr 0x%02x\n", adap_id,
1132 address_data->ignore[j + 1]);
1133 ignore = 1;
1134 break;
1135 }
1136 }
1137 if (ignore)
1138 continue;
1139
1140 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1141 "addr 0x%02x\n", adap_id,
1142 address_data->normal_i2c[i]);
1143 temp_client->addr = address_data->normal_i2c[i];
1144 err = i2c_detect_address(temp_client, -1, driver);
1145 if (err)
1146 goto exit_free;
1147 }
1148
1149 exit_free:
1150 kfree(temp_client);
1151 return err;
1152 }
1153
1154 struct i2c_client *
1155 i2c_new_probed_device(struct i2c_adapter *adap,
1156 struct i2c_board_info *info,
1157 unsigned short const *addr_list)
1158 {
1159 int i;
1160
1161 /* Stop here if the bus doesn't support probing */
1162 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1163 dev_err(&adap->dev, "Probing not supported\n");
1164 return NULL;
1165 }
1166
1167 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1168 /* Check address validity */
1169 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1170 dev_warn(&adap->dev, "Invalid 7-bit address "
1171 "0x%02x\n", addr_list[i]);
1172 continue;
1173 }
1174
1175 /* Check address availability */
1176 if (i2c_check_addr(adap, addr_list[i])) {
1177 dev_dbg(&adap->dev, "Address 0x%02x already in "
1178 "use, not probing\n", addr_list[i]);
1179 continue;
1180 }
1181
1182 /* Test address responsiveness
1183 The default probe method is a quick write, but it is known
1184 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1185 and could also irreversibly write-protect some EEPROMs, so
1186 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1187 read instead. Also, some bus drivers don't implement
1188 quick write, so we fallback to a byte read it that case
1189 too. */
1190 if ((addr_list[i] & ~0x07) == 0x30
1191 || (addr_list[i] & ~0x0f) == 0x50
1192 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1193 union i2c_smbus_data data;
1194
1195 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1196 I2C_SMBUS_READ, 0,
1197 I2C_SMBUS_BYTE, &data) >= 0)
1198 break;
1199 } else {
1200 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1201 I2C_SMBUS_WRITE, 0,
1202 I2C_SMBUS_QUICK, NULL) >= 0)
1203 break;
1204 }
1205 }
1206
1207 if (addr_list[i] == I2C_CLIENT_END) {
1208 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1209 return NULL;
1210 }
1211
1212 info->addr = addr_list[i];
1213 return i2c_new_device(adap, info);
1214 }
1215 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1216
1217 struct i2c_adapter* i2c_get_adapter(int id)
1218 {
1219 struct i2c_adapter *adapter;
1220
1221 mutex_lock(&core_lock);
1222 adapter = idr_find(&i2c_adapter_idr, id);
1223 if (adapter && !try_module_get(adapter->owner))
1224 adapter = NULL;
1225
1226 mutex_unlock(&core_lock);
1227 return adapter;
1228 }
1229 EXPORT_SYMBOL(i2c_get_adapter);
1230
1231 void i2c_put_adapter(struct i2c_adapter *adap)
1232 {
1233 module_put(adap->owner);
1234 }
1235 EXPORT_SYMBOL(i2c_put_adapter);
1236
1237 /* The SMBus parts */
1238
1239 #define POLY (0x1070U << 3)
1240 static u8 crc8(u16 data)
1241 {
1242 int i;
1243
1244 for(i = 0; i < 8; i++) {
1245 if (data & 0x8000)
1246 data = data ^ POLY;
1247 data = data << 1;
1248 }
1249 return (u8)(data >> 8);
1250 }
1251
1252 /* Incremental CRC8 over count bytes in the array pointed to by p */
1253 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1254 {
1255 int i;
1256
1257 for(i = 0; i < count; i++)
1258 crc = crc8((crc ^ p[i]) << 8);
1259 return crc;
1260 }
1261
1262 /* Assume a 7-bit address, which is reasonable for SMBus */
1263 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1264 {
1265 /* The address will be sent first */
1266 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1267 pec = i2c_smbus_pec(pec, &addr, 1);
1268
1269 /* The data buffer follows */
1270 return i2c_smbus_pec(pec, msg->buf, msg->len);
1271 }
1272
1273 /* Used for write only transactions */
1274 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1275 {
1276 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1277 msg->len++;
1278 }
1279
1280 /* Return <0 on CRC error
1281 If there was a write before this read (most cases) we need to take the
1282 partial CRC from the write part into account.
1283 Note that this function does modify the message (we need to decrease the
1284 message length to hide the CRC byte from the caller). */
1285 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1286 {
1287 u8 rpec = msg->buf[--msg->len];
1288 cpec = i2c_smbus_msg_pec(cpec, msg);
1289
1290 if (rpec != cpec) {
1291 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1292 rpec, cpec);
1293 return -EBADMSG;
1294 }
1295 return 0;
1296 }
1297
1298 /**
1299 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1300 * @client: Handle to slave device
1301 *
1302 * This executes the SMBus "receive byte" protocol, returning negative errno
1303 * else the byte received from the device.
1304 */
1305 s32 i2c_smbus_read_byte(struct i2c_client *client)
1306 {
1307 union i2c_smbus_data data;
1308 int status;
1309
1310 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1311 I2C_SMBUS_READ, 0,
1312 I2C_SMBUS_BYTE, &data);
1313 return (status < 0) ? status : data.byte;
1314 }
1315 EXPORT_SYMBOL(i2c_smbus_read_byte);
1316
1317 /**
1318 * i2c_smbus_write_byte - SMBus "send byte" protocol
1319 * @client: Handle to slave device
1320 * @value: Byte to be sent
1321 *
1322 * This executes the SMBus "send byte" protocol, returning negative errno
1323 * else zero on success.
1324 */
1325 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1326 {
1327 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1328 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1329 }
1330 EXPORT_SYMBOL(i2c_smbus_write_byte);
1331
1332 /**
1333 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1334 * @client: Handle to slave device
1335 * @command: Byte interpreted by slave
1336 *
1337 * This executes the SMBus "read byte" protocol, returning negative errno
1338 * else a data byte received from the device.
1339 */
1340 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1341 {
1342 union i2c_smbus_data data;
1343 int status;
1344
1345 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1346 I2C_SMBUS_READ, command,
1347 I2C_SMBUS_BYTE_DATA, &data);
1348 return (status < 0) ? status : data.byte;
1349 }
1350 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1351
1352 /**
1353 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1354 * @client: Handle to slave device
1355 * @command: Byte interpreted by slave
1356 * @value: Byte being written
1357 *
1358 * This executes the SMBus "write byte" protocol, returning negative errno
1359 * else zero on success.
1360 */
1361 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1362 {
1363 union i2c_smbus_data data;
1364 data.byte = value;
1365 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1366 I2C_SMBUS_WRITE,command,
1367 I2C_SMBUS_BYTE_DATA,&data);
1368 }
1369 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1370
1371 /**
1372 * i2c_smbus_read_word_data - SMBus "read word" protocol
1373 * @client: Handle to slave device
1374 * @command: Byte interpreted by slave
1375 *
1376 * This executes the SMBus "read word" protocol, returning negative errno
1377 * else a 16-bit unsigned "word" received from the device.
1378 */
1379 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1380 {
1381 union i2c_smbus_data data;
1382 int status;
1383
1384 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1385 I2C_SMBUS_READ, command,
1386 I2C_SMBUS_WORD_DATA, &data);
1387 return (status < 0) ? status : data.word;
1388 }
1389 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1390
1391 /**
1392 * i2c_smbus_write_word_data - SMBus "write word" protocol
1393 * @client: Handle to slave device
1394 * @command: Byte interpreted by slave
1395 * @value: 16-bit "word" being written
1396 *
1397 * This executes the SMBus "write word" protocol, returning negative errno
1398 * else zero on success.
1399 */
1400 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1401 {
1402 union i2c_smbus_data data;
1403 data.word = value;
1404 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1405 I2C_SMBUS_WRITE,command,
1406 I2C_SMBUS_WORD_DATA,&data);
1407 }
1408 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1409
1410 /**
1411 * i2c_smbus_process_call - SMBus "process call" protocol
1412 * @client: Handle to slave device
1413 * @command: Byte interpreted by slave
1414 * @value: 16-bit "word" being written
1415 *
1416 * This executes the SMBus "process call" protocol, returning negative errno
1417 * else a 16-bit unsigned "word" received from the device.
1418 */
1419 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1420 {
1421 union i2c_smbus_data data;
1422 int status;
1423 data.word = value;
1424
1425 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1426 I2C_SMBUS_WRITE, command,
1427 I2C_SMBUS_PROC_CALL, &data);
1428 return (status < 0) ? status : data.word;
1429 }
1430 EXPORT_SYMBOL(i2c_smbus_process_call);
1431
1432 /**
1433 * i2c_smbus_read_block_data - SMBus "block read" protocol
1434 * @client: Handle to slave device
1435 * @command: Byte interpreted by slave
1436 * @values: Byte array into which data will be read; big enough to hold
1437 * the data returned by the slave. SMBus allows at most 32 bytes.
1438 *
1439 * This executes the SMBus "block read" protocol, returning negative errno
1440 * else the number of data bytes in the slave's response.
1441 *
1442 * Note that using this function requires that the client's adapter support
1443 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1444 * support this; its emulation through I2C messaging relies on a specific
1445 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1446 */
1447 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1448 u8 *values)
1449 {
1450 union i2c_smbus_data data;
1451 int status;
1452
1453 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1454 I2C_SMBUS_READ, command,
1455 I2C_SMBUS_BLOCK_DATA, &data);
1456 if (status)
1457 return status;
1458
1459 memcpy(values, &data.block[1], data.block[0]);
1460 return data.block[0];
1461 }
1462 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1463
1464 /**
1465 * i2c_smbus_write_block_data - SMBus "block write" protocol
1466 * @client: Handle to slave device
1467 * @command: Byte interpreted by slave
1468 * @length: Size of data block; SMBus allows at most 32 bytes
1469 * @values: Byte array which will be written.
1470 *
1471 * This executes the SMBus "block write" protocol, returning negative errno
1472 * else zero on success.
1473 */
1474 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1475 u8 length, const u8 *values)
1476 {
1477 union i2c_smbus_data data;
1478
1479 if (length > I2C_SMBUS_BLOCK_MAX)
1480 length = I2C_SMBUS_BLOCK_MAX;
1481 data.block[0] = length;
1482 memcpy(&data.block[1], values, length);
1483 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1484 I2C_SMBUS_WRITE,command,
1485 I2C_SMBUS_BLOCK_DATA,&data);
1486 }
1487 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1488
1489 /* Returns the number of read bytes */
1490 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1491 u8 length, u8 *values)
1492 {
1493 union i2c_smbus_data data;
1494 int status;
1495
1496 if (length > I2C_SMBUS_BLOCK_MAX)
1497 length = I2C_SMBUS_BLOCK_MAX;
1498 data.block[0] = length;
1499 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1500 I2C_SMBUS_READ, command,
1501 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1502 if (status < 0)
1503 return status;
1504
1505 memcpy(values, &data.block[1], data.block[0]);
1506 return data.block[0];
1507 }
1508 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1509
1510 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1511 u8 length, const u8 *values)
1512 {
1513 union i2c_smbus_data data;
1514
1515 if (length > I2C_SMBUS_BLOCK_MAX)
1516 length = I2C_SMBUS_BLOCK_MAX;
1517 data.block[0] = length;
1518 memcpy(data.block + 1, values, length);
1519 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1520 I2C_SMBUS_WRITE, command,
1521 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1522 }
1523 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1524
1525 /* Simulate a SMBus command using the i2c protocol
1526 No checking of parameters is done! */
1527 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1528 unsigned short flags,
1529 char read_write, u8 command, int size,
1530 union i2c_smbus_data * data)
1531 {
1532 /* So we need to generate a series of msgs. In the case of writing, we
1533 need to use only one message; when reading, we need two. We initialize
1534 most things with sane defaults, to keep the code below somewhat
1535 simpler. */
1536 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1537 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1538 int num = read_write == I2C_SMBUS_READ?2:1;
1539 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1540 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1541 };
1542 int i;
1543 u8 partial_pec = 0;
1544 int status;
1545
1546 msgbuf0[0] = command;
1547 switch(size) {
1548 case I2C_SMBUS_QUICK:
1549 msg[0].len = 0;
1550 /* Special case: The read/write field is used as data */
1551 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1552 I2C_M_RD : 0);
1553 num = 1;
1554 break;
1555 case I2C_SMBUS_BYTE:
1556 if (read_write == I2C_SMBUS_READ) {
1557 /* Special case: only a read! */
1558 msg[0].flags = I2C_M_RD | flags;
1559 num = 1;
1560 }
1561 break;
1562 case I2C_SMBUS_BYTE_DATA:
1563 if (read_write == I2C_SMBUS_READ)
1564 msg[1].len = 1;
1565 else {
1566 msg[0].len = 2;
1567 msgbuf0[1] = data->byte;
1568 }
1569 break;
1570 case I2C_SMBUS_WORD_DATA:
1571 if (read_write == I2C_SMBUS_READ)
1572 msg[1].len = 2;
1573 else {
1574 msg[0].len=3;
1575 msgbuf0[1] = data->word & 0xff;
1576 msgbuf0[2] = data->word >> 8;
1577 }
1578 break;
1579 case I2C_SMBUS_PROC_CALL:
1580 num = 2; /* Special case */
1581 read_write = I2C_SMBUS_READ;
1582 msg[0].len = 3;
1583 msg[1].len = 2;
1584 msgbuf0[1] = data->word & 0xff;
1585 msgbuf0[2] = data->word >> 8;
1586 break;
1587 case I2C_SMBUS_BLOCK_DATA:
1588 if (read_write == I2C_SMBUS_READ) {
1589 msg[1].flags |= I2C_M_RECV_LEN;
1590 msg[1].len = 1; /* block length will be added by
1591 the underlying bus driver */
1592 } else {
1593 msg[0].len = data->block[0] + 2;
1594 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1595 dev_err(&adapter->dev,
1596 "Invalid block write size %d\n",
1597 data->block[0]);
1598 return -EINVAL;
1599 }
1600 for (i = 1; i < msg[0].len; i++)
1601 msgbuf0[i] = data->block[i-1];
1602 }
1603 break;
1604 case I2C_SMBUS_BLOCK_PROC_CALL:
1605 num = 2; /* Another special case */
1606 read_write = I2C_SMBUS_READ;
1607 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1608 dev_err(&adapter->dev,
1609 "Invalid block write size %d\n",
1610 data->block[0]);
1611 return -EINVAL;
1612 }
1613 msg[0].len = data->block[0] + 2;
1614 for (i = 1; i < msg[0].len; i++)
1615 msgbuf0[i] = data->block[i-1];
1616 msg[1].flags |= I2C_M_RECV_LEN;
1617 msg[1].len = 1; /* block length will be added by
1618 the underlying bus driver */
1619 break;
1620 case I2C_SMBUS_I2C_BLOCK_DATA:
1621 if (read_write == I2C_SMBUS_READ) {
1622 msg[1].len = data->block[0];
1623 } else {
1624 msg[0].len = data->block[0] + 1;
1625 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1626 dev_err(&adapter->dev,
1627 "Invalid block write size %d\n",
1628 data->block[0]);
1629 return -EINVAL;
1630 }
1631 for (i = 1; i <= data->block[0]; i++)
1632 msgbuf0[i] = data->block[i];
1633 }
1634 break;
1635 default:
1636 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1637 return -EOPNOTSUPP;
1638 }
1639
1640 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1641 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1642 if (i) {
1643 /* Compute PEC if first message is a write */
1644 if (!(msg[0].flags & I2C_M_RD)) {
1645 if (num == 1) /* Write only */
1646 i2c_smbus_add_pec(&msg[0]);
1647 else /* Write followed by read */
1648 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1649 }
1650 /* Ask for PEC if last message is a read */
1651 if (msg[num-1].flags & I2C_M_RD)
1652 msg[num-1].len++;
1653 }
1654
1655 status = i2c_transfer(adapter, msg, num);
1656 if (status < 0)
1657 return status;
1658
1659 /* Check PEC if last message is a read */
1660 if (i && (msg[num-1].flags & I2C_M_RD)) {
1661 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1662 if (status < 0)
1663 return status;
1664 }
1665
1666 if (read_write == I2C_SMBUS_READ)
1667 switch(size) {
1668 case I2C_SMBUS_BYTE:
1669 data->byte = msgbuf0[0];
1670 break;
1671 case I2C_SMBUS_BYTE_DATA:
1672 data->byte = msgbuf1[0];
1673 break;
1674 case I2C_SMBUS_WORD_DATA:
1675 case I2C_SMBUS_PROC_CALL:
1676 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1677 break;
1678 case I2C_SMBUS_I2C_BLOCK_DATA:
1679 for (i = 0; i < data->block[0]; i++)
1680 data->block[i+1] = msgbuf1[i];
1681 break;
1682 case I2C_SMBUS_BLOCK_DATA:
1683 case I2C_SMBUS_BLOCK_PROC_CALL:
1684 for (i = 0; i < msgbuf1[0] + 1; i++)
1685 data->block[i] = msgbuf1[i];
1686 break;
1687 }
1688 return 0;
1689 }
1690
1691 /**
1692 * i2c_smbus_xfer - execute SMBus protocol operations
1693 * @adapter: Handle to I2C bus
1694 * @addr: Address of SMBus slave on that bus
1695 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1696 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1697 * @command: Byte interpreted by slave, for protocols which use such bytes
1698 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1699 * @data: Data to be read or written
1700 *
1701 * This executes an SMBus protocol operation, and returns a negative
1702 * errno code else zero on success.
1703 */
1704 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1705 char read_write, u8 command, int protocol,
1706 union i2c_smbus_data *data)
1707 {
1708 unsigned long orig_jiffies;
1709 int try;
1710 s32 res;
1711
1712 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1713
1714 if (adapter->algo->smbus_xfer) {
1715 mutex_lock(&adapter->bus_lock);
1716
1717 /* Retry automatically on arbitration loss */
1718 orig_jiffies = jiffies;
1719 for (res = 0, try = 0; try <= adapter->retries; try++) {
1720 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1721 read_write, command,
1722 protocol, data);
1723 if (res != -EAGAIN)
1724 break;
1725 if (time_after(jiffies,
1726 orig_jiffies + adapter->timeout))
1727 break;
1728 }
1729 mutex_unlock(&adapter->bus_lock);
1730 } else
1731 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1732 command, protocol, data);
1733
1734 return res;
1735 }
1736 EXPORT_SYMBOL(i2c_smbus_xfer);
1737
1738 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1739 MODULE_DESCRIPTION("I2C-Bus main module");
1740 MODULE_LICENSE("GPL");