1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
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.
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.
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 /* ------------------------------------------------------------------------- */
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> */
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/init.h>
30 #include <linux/idr.h>
31 #include <linux/seq_file.h>
32 #include <asm/uaccess.h>
35 static LIST_HEAD(adapters
);
36 static LIST_HEAD(drivers
);
37 static DECLARE_MUTEX(core_lists
);
38 static DEFINE_IDR(i2c_adapter_idr
);
40 /* match always succeeds, as we want the probe() to tell if we really accept this match */
41 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
46 static int i2c_bus_suspend(struct device
* dev
, pm_message_t state
)
50 if (dev
->driver
&& dev
->driver
->suspend
)
51 rc
= dev
->driver
->suspend(dev
,state
,0);
55 static int i2c_bus_resume(struct device
* dev
)
59 if (dev
->driver
&& dev
->driver
->resume
)
60 rc
= dev
->driver
->resume(dev
,0);
64 struct bus_type i2c_bus_type
= {
66 .match
= i2c_device_match
,
67 .suspend
= i2c_bus_suspend
,
68 .resume
= i2c_bus_resume
,
71 static int i2c_device_probe(struct device
*dev
)
76 static int i2c_device_remove(struct device
*dev
)
81 void i2c_adapter_dev_release(struct device
*dev
)
83 struct i2c_adapter
*adap
= dev_to_i2c_adapter(dev
);
84 complete(&adap
->dev_released
);
87 struct device_driver i2c_adapter_driver
= {
88 .name
= "i2c_adapter",
90 .probe
= i2c_device_probe
,
91 .remove
= i2c_device_remove
,
94 static void i2c_adapter_class_dev_release(struct class_device
*dev
)
96 struct i2c_adapter
*adap
= class_dev_to_i2c_adapter(dev
);
97 complete(&adap
->class_dev_released
);
100 struct class i2c_adapter_class
= {
101 .name
= "i2c-adapter",
102 .release
= &i2c_adapter_class_dev_release
,
105 static ssize_t
show_adapter_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
107 struct i2c_adapter
*adap
= dev_to_i2c_adapter(dev
);
108 return sprintf(buf
, "%s\n", adap
->name
);
110 static DEVICE_ATTR(name
, S_IRUGO
, show_adapter_name
, NULL
);
113 static void i2c_client_release(struct device
*dev
)
115 struct i2c_client
*client
= to_i2c_client(dev
);
116 complete(&client
->released
);
119 static ssize_t
show_client_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
121 struct i2c_client
*client
= to_i2c_client(dev
);
122 return sprintf(buf
, "%s\n", client
->name
);
126 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
127 * different type of a device. So beware if the DEVICE_ATTR() macro ever
128 * changes, this definition will also have to change.
130 static struct device_attribute dev_attr_client_name
= {
131 .attr
= {.name
= "name", .mode
= S_IRUGO
, .owner
= THIS_MODULE
},
132 .show
= &show_client_name
,
136 /* ---------------------------------------------------
137 * registering functions
138 * ---------------------------------------------------
142 * i2c_add_adapter is called from within the algorithm layer,
143 * when a new hw adapter registers. A new device is register to be
144 * available for clients.
146 int i2c_add_adapter(struct i2c_adapter
*adap
)
149 struct list_head
*item
;
150 struct i2c_driver
*driver
;
154 if (idr_pre_get(&i2c_adapter_idr
, GFP_KERNEL
) == 0) {
159 res
= idr_get_new(&i2c_adapter_idr
, adap
, &id
);
166 adap
->nr
= id
& MAX_ID_MASK
;
167 init_MUTEX(&adap
->bus_lock
);
168 init_MUTEX(&adap
->clist_lock
);
169 list_add_tail(&adap
->list
,&adapters
);
170 INIT_LIST_HEAD(&adap
->clients
);
172 /* Add the adapter to the driver core.
173 * If the parent pointer is not set up,
174 * we add this adapter to the host bus.
176 if (adap
->dev
.parent
== NULL
)
177 adap
->dev
.parent
= &platform_bus
;
178 sprintf(adap
->dev
.bus_id
, "i2c-%d", adap
->nr
);
179 adap
->dev
.driver
= &i2c_adapter_driver
;
180 adap
->dev
.release
= &i2c_adapter_dev_release
;
181 device_register(&adap
->dev
);
182 device_create_file(&adap
->dev
, &dev_attr_name
);
184 /* Add this adapter to the i2c_adapter class */
185 memset(&adap
->class_dev
, 0x00, sizeof(struct class_device
));
186 adap
->class_dev
.dev
= &adap
->dev
;
187 adap
->class_dev
.class = &i2c_adapter_class
;
188 strlcpy(adap
->class_dev
.class_id
, adap
->dev
.bus_id
, BUS_ID_SIZE
);
189 class_device_register(&adap
->class_dev
);
191 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
193 /* inform drivers of new adapters */
194 list_for_each(item
,&drivers
) {
195 driver
= list_entry(item
, struct i2c_driver
, list
);
196 if (driver
->flags
& I2C_DF_NOTIFY
)
197 /* We ignore the return code; if it fails, too bad */
198 driver
->attach_adapter(adap
);
207 int i2c_del_adapter(struct i2c_adapter
*adap
)
209 struct list_head
*item
, *_n
;
210 struct i2c_adapter
*adap_from_list
;
211 struct i2c_driver
*driver
;
212 struct i2c_client
*client
;
217 /* First make sure that this adapter was ever added */
218 list_for_each_entry(adap_from_list
, &adapters
, list
) {
219 if (adap_from_list
== adap
)
222 if (adap_from_list
!= adap
) {
223 pr_debug("i2c-core: attempting to delete unregistered "
224 "adapter [%s]\n", adap
->name
);
229 list_for_each(item
,&drivers
) {
230 driver
= list_entry(item
, struct i2c_driver
, list
);
231 if (driver
->detach_adapter
)
232 if ((res
= driver
->detach_adapter(adap
))) {
233 dev_err(&adap
->dev
, "detach_adapter failed "
234 "for driver [%s]\n", driver
->name
);
239 /* detach any active clients. This must be done first, because
240 * it can fail; in which case we give up. */
241 list_for_each_safe(item
, _n
, &adap
->clients
) {
242 client
= list_entry(item
, struct i2c_client
, list
);
244 /* detaching devices is unconditional of the set notify
245 * flag, as _all_ clients that reside on the adapter
246 * must be deleted, as this would cause invalid states.
248 if ((res
=client
->driver
->detach_client(client
))) {
249 dev_err(&adap
->dev
, "detach_client failed for client "
250 "[%s] at address 0x%02x\n", client
->name
,
256 /* clean up the sysfs representation */
257 init_completion(&adap
->dev_released
);
258 init_completion(&adap
->class_dev_released
);
259 class_device_unregister(&adap
->class_dev
);
260 device_remove_file(&adap
->dev
, &dev_attr_name
);
261 device_unregister(&adap
->dev
);
262 list_del(&adap
->list
);
264 /* wait for sysfs to drop all references */
265 wait_for_completion(&adap
->dev_released
);
266 wait_for_completion(&adap
->class_dev_released
);
268 /* free dynamically allocated bus id */
269 idr_remove(&i2c_adapter_idr
, adap
->nr
);
271 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
280 * What follows is the "upwards" interface: commands for talking to clients,
281 * which implement the functions to access the physical information of the
285 int i2c_add_driver(struct i2c_driver
*driver
)
287 struct list_head
*item
;
288 struct i2c_adapter
*adapter
;
293 /* add the driver to the list of i2c drivers in the driver core */
294 driver
->driver
.name
= driver
->name
;
295 driver
->driver
.bus
= &i2c_bus_type
;
296 driver
->driver
.probe
= i2c_device_probe
;
297 driver
->driver
.remove
= i2c_device_remove
;
299 res
= driver_register(&driver
->driver
);
303 list_add_tail(&driver
->list
,&drivers
);
304 pr_debug("i2c-core: driver [%s] registered\n", driver
->name
);
306 /* now look for instances of driver on our adapters */
307 if (driver
->flags
& I2C_DF_NOTIFY
) {
308 list_for_each(item
,&adapters
) {
309 adapter
= list_entry(item
, struct i2c_adapter
, list
);
310 driver
->attach_adapter(adapter
);
319 int i2c_del_driver(struct i2c_driver
*driver
)
321 struct list_head
*item1
, *item2
, *_n
;
322 struct i2c_client
*client
;
323 struct i2c_adapter
*adap
;
329 /* Have a look at each adapter, if clients of this driver are still
330 * attached. If so, detach them to be able to kill the driver
333 * Removing clients does not depend on the notify flag, else
334 * invalid operation might (will!) result, when using stale client
337 list_for_each(item1
,&adapters
) {
338 adap
= list_entry(item1
, struct i2c_adapter
, list
);
339 if (driver
->detach_adapter
) {
340 if ((res
= driver
->detach_adapter(adap
))) {
341 dev_err(&adap
->dev
, "detach_adapter failed "
342 "for driver [%s]\n", driver
->name
);
346 list_for_each_safe(item2
, _n
, &adap
->clients
) {
347 client
= list_entry(item2
, struct i2c_client
, list
);
348 if (client
->driver
!= driver
)
350 dev_dbg(&adap
->dev
, "detaching client [%s] "
351 "at 0x%02x\n", client
->name
,
353 if ((res
= driver
->detach_client(client
))) {
354 dev_err(&adap
->dev
, "detach_client "
355 "failed for client [%s] at "
356 "0x%02x\n", client
->name
,
364 driver_unregister(&driver
->driver
);
365 list_del(&driver
->list
);
366 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->name
);
373 static int __i2c_check_addr(struct i2c_adapter
*adapter
, unsigned int addr
)
375 struct list_head
*item
;
376 struct i2c_client
*client
;
378 list_for_each(item
,&adapter
->clients
) {
379 client
= list_entry(item
, struct i2c_client
, list
);
380 if (client
->addr
== addr
)
386 int i2c_check_addr(struct i2c_adapter
*adapter
, int addr
)
390 down(&adapter
->clist_lock
);
391 rval
= __i2c_check_addr(adapter
, addr
);
392 up(&adapter
->clist_lock
);
397 int i2c_attach_client(struct i2c_client
*client
)
399 struct i2c_adapter
*adapter
= client
->adapter
;
401 down(&adapter
->clist_lock
);
402 if (__i2c_check_addr(client
->adapter
, client
->addr
)) {
403 up(&adapter
->clist_lock
);
406 list_add_tail(&client
->list
,&adapter
->clients
);
407 up(&adapter
->clist_lock
);
409 if (adapter
->client_register
) {
410 if (adapter
->client_register(client
)) {
411 dev_dbg(&adapter
->dev
, "client_register "
412 "failed for client [%s] at 0x%02x\n",
413 client
->name
, client
->addr
);
417 if (client
->flags
& I2C_CLIENT_ALLOW_USE
)
418 client
->usage_count
= 0;
420 client
->dev
.parent
= &client
->adapter
->dev
;
421 client
->dev
.driver
= &client
->driver
->driver
;
422 client
->dev
.bus
= &i2c_bus_type
;
423 client
->dev
.release
= &i2c_client_release
;
425 snprintf(&client
->dev
.bus_id
[0], sizeof(client
->dev
.bus_id
),
426 "%d-%04x", i2c_adapter_id(adapter
), client
->addr
);
427 dev_dbg(&adapter
->dev
, "client [%s] registered with bus id %s\n",
428 client
->name
, client
->dev
.bus_id
);
429 device_register(&client
->dev
);
430 device_create_file(&client
->dev
, &dev_attr_client_name
);
436 int i2c_detach_client(struct i2c_client
*client
)
438 struct i2c_adapter
*adapter
= client
->adapter
;
441 if ((client
->flags
& I2C_CLIENT_ALLOW_USE
)
442 && (client
->usage_count
> 0)) {
443 dev_warn(&client
->dev
, "Client [%s] still busy, "
444 "can't detach\n", client
->name
);
448 if (adapter
->client_unregister
) {
449 res
= adapter
->client_unregister(client
);
451 dev_err(&client
->dev
,
452 "client_unregister [%s] failed, "
453 "client not detached\n", client
->name
);
458 down(&adapter
->clist_lock
);
459 list_del(&client
->list
);
460 init_completion(&client
->released
);
461 device_remove_file(&client
->dev
, &dev_attr_client_name
);
462 device_unregister(&client
->dev
);
463 up(&adapter
->clist_lock
);
464 wait_for_completion(&client
->released
);
470 static int i2c_inc_use_client(struct i2c_client
*client
)
473 if (!try_module_get(client
->driver
->owner
))
475 if (!try_module_get(client
->adapter
->owner
)) {
476 module_put(client
->driver
->owner
);
483 static void i2c_dec_use_client(struct i2c_client
*client
)
485 module_put(client
->driver
->owner
);
486 module_put(client
->adapter
->owner
);
489 int i2c_use_client(struct i2c_client
*client
)
493 ret
= i2c_inc_use_client(client
);
497 if (client
->flags
& I2C_CLIENT_ALLOW_USE
) {
498 if (client
->flags
& I2C_CLIENT_ALLOW_MULTIPLE_USE
)
499 client
->usage_count
++;
500 else if (client
->usage_count
> 0)
503 client
->usage_count
++;
508 i2c_dec_use_client(client
);
512 int i2c_release_client(struct i2c_client
*client
)
514 if(client
->flags
& I2C_CLIENT_ALLOW_USE
) {
515 if(client
->usage_count
>0)
516 client
->usage_count
--;
518 pr_debug("i2c-core: %s used one too many times\n",
524 i2c_dec_use_client(client
);
529 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
531 struct list_head
*item
;
532 struct i2c_client
*client
;
534 down(&adap
->clist_lock
);
535 list_for_each(item
,&adap
->clients
) {
536 client
= list_entry(item
, struct i2c_client
, list
);
537 if (!try_module_get(client
->driver
->owner
))
539 if (NULL
!= client
->driver
->command
) {
540 up(&adap
->clist_lock
);
541 client
->driver
->command(client
,cmd
,arg
);
542 down(&adap
->clist_lock
);
544 module_put(client
->driver
->owner
);
546 up(&adap
->clist_lock
);
549 static int __init
i2c_init(void)
553 retval
= bus_register(&i2c_bus_type
);
556 retval
= driver_register(&i2c_adapter_driver
);
559 return class_register(&i2c_adapter_class
);
562 static void __exit
i2c_exit(void)
564 class_unregister(&i2c_adapter_class
);
565 driver_unregister(&i2c_adapter_driver
);
566 bus_unregister(&i2c_bus_type
);
569 subsys_initcall(i2c_init
);
570 module_exit(i2c_exit
);
572 /* ----------------------------------------------------
573 * the functional interface to the i2c busses.
574 * ----------------------------------------------------
577 int i2c_transfer(struct i2c_adapter
* adap
, struct i2c_msg
*msgs
, int num
)
581 if (adap
->algo
->master_xfer
) {
583 for (ret
= 0; ret
< num
; ret
++) {
584 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
585 "len=%d\n", ret
, msgs
[ret
].flags
& I2C_M_RD
?
586 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
);
590 down(&adap
->bus_lock
);
591 ret
= adap
->algo
->master_xfer(adap
,msgs
,num
);
596 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
601 int i2c_master_send(struct i2c_client
*client
,const char *buf
,int count
)
604 struct i2c_adapter
*adap
=client
->adapter
;
607 msg
.addr
= client
->addr
;
608 msg
.flags
= client
->flags
& I2C_M_TEN
;
610 msg
.buf
= (char *)buf
;
612 ret
= i2c_transfer(adap
, &msg
, 1);
614 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
615 transmitted, else error code. */
616 return (ret
== 1) ? count
: ret
;
619 int i2c_master_recv(struct i2c_client
*client
, char *buf
,int count
)
621 struct i2c_adapter
*adap
=client
->adapter
;
625 msg
.addr
= client
->addr
;
626 msg
.flags
= client
->flags
& I2C_M_TEN
;
627 msg
.flags
|= I2C_M_RD
;
631 ret
= i2c_transfer(adap
, &msg
, 1);
633 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
634 transmitted, else error code. */
635 return (ret
== 1) ? count
: ret
;
639 int i2c_control(struct i2c_client
*client
,
640 unsigned int cmd
, unsigned long arg
)
643 struct i2c_adapter
*adap
= client
->adapter
;
645 dev_dbg(&client
->adapter
->dev
, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd
, arg
);
654 if (adap
->algo
->algo_control
!=NULL
)
655 ret
= adap
->algo
->algo_control(adap
,cmd
,arg
);
660 /* ----------------------------------------------------
661 * the i2c address scanning function
662 * Will not work for 10-bit addresses!
663 * ----------------------------------------------------
665 static int i2c_probe_address(struct i2c_adapter
*adapter
, int addr
, int kind
,
666 int (*found_proc
) (struct i2c_adapter
*, int, int))
670 /* Make sure the address is valid */
671 if (addr
< 0x03 || addr
> 0x77) {
672 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
677 /* Skip if already in use */
678 if (i2c_check_addr(adapter
, addr
))
681 /* Make sure there is something at this address, unless forced */
683 if (i2c_smbus_xfer(adapter
, addr
, 0, 0, 0,
684 I2C_SMBUS_QUICK
, NULL
) < 0)
687 /* prevent 24RF08 corruption */
688 if ((addr
& ~0x0f) == 0x50)
689 i2c_smbus_xfer(adapter
, addr
, 0, 0, 0,
690 I2C_SMBUS_QUICK
, NULL
);
693 /* Finally call the custom detection function */
694 err
= found_proc(adapter
, addr
, kind
);
696 /* -ENODEV can be returned if there is a chip at the given address
697 but it isn't supported by this chip driver. We catch it here as
698 this isn't an error. */
699 return (err
== -ENODEV
) ? 0 : err
;
702 int i2c_probe(struct i2c_adapter
*adapter
,
703 struct i2c_client_address_data
*address_data
,
704 int (*found_proc
) (struct i2c_adapter
*, int, int))
707 int adap_id
= i2c_adapter_id(adapter
);
709 /* Forget it if we can't probe using SMBUS_QUICK */
710 if (! i2c_check_functionality(adapter
,I2C_FUNC_SMBUS_QUICK
))
713 /* Force entries are done first, and are not affected by ignore
715 if (address_data
->forces
) {
716 unsigned short **forces
= address_data
->forces
;
719 for (kind
= 0; forces
[kind
]; kind
++) {
720 for (i
= 0; forces
[kind
][i
] != I2C_CLIENT_END
;
722 if (forces
[kind
][i
] == adap_id
723 || forces
[kind
][i
] == ANY_I2C_BUS
) {
724 dev_dbg(&adapter
->dev
, "found force "
725 "parameter for adapter %d, "
726 "addr 0x%02x, kind %d\n",
727 adap_id
, forces
[kind
][i
+ 1],
729 err
= i2c_probe_address(adapter
,
739 /* Probe entries are done second, and are not affected by ignore
741 for (i
= 0; address_data
->probe
[i
] != I2C_CLIENT_END
; i
+= 2) {
742 if (address_data
->probe
[i
] == adap_id
743 || address_data
->probe
[i
] == ANY_I2C_BUS
) {
744 dev_dbg(&adapter
->dev
, "found probe parameter for "
745 "adapter %d, addr 0x%02x\n", adap_id
,
746 address_data
->probe
[i
+ 1]);
747 err
= i2c_probe_address(adapter
,
748 address_data
->probe
[i
+ 1],
755 /* Normal entries are done last, unless shadowed by an ignore entry */
756 for (i
= 0; address_data
->normal_i2c
[i
] != I2C_CLIENT_END
; i
+= 1) {
760 for (j
= 0; address_data
->ignore
[j
] != I2C_CLIENT_END
;
762 if ((address_data
->ignore
[j
] == adap_id
||
763 address_data
->ignore
[j
] == ANY_I2C_BUS
)
764 && address_data
->ignore
[j
+ 1]
765 == address_data
->normal_i2c
[i
]) {
766 dev_dbg(&adapter
->dev
, "found ignore "
767 "parameter for adapter %d, "
768 "addr 0x%02x\n", adap_id
,
769 address_data
->ignore
[j
+ 1]);
777 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
778 "addr 0x%02x\n", adap_id
,
779 address_data
->normal_i2c
[i
]);
780 err
= i2c_probe_address(adapter
, address_data
->normal_i2c
[i
],
789 struct i2c_adapter
* i2c_get_adapter(int id
)
791 struct i2c_adapter
*adapter
;
794 adapter
= (struct i2c_adapter
*)idr_find(&i2c_adapter_idr
, id
);
795 if (adapter
&& !try_module_get(adapter
->owner
))
802 void i2c_put_adapter(struct i2c_adapter
*adap
)
804 module_put(adap
->owner
);
807 /* The SMBus parts */
809 #define POLY (0x1070U << 3)
815 for(i
= 0; i
< 8; i
++) {
820 return (u8
)(data
>> 8);
823 /* CRC over count bytes in the first array plus the bytes in the rest
824 array if it is non-null. rest[0] is the (length of rest) - 1
826 static u8
i2c_smbus_partial_pec(u8 crc
, int count
, u8
*first
, u8
*rest
)
830 for(i
= 0; i
< count
; i
++)
831 crc
= crc8((crc
^ first
[i
]) << 8);
833 for(i
= 0; i
<= rest
[0]; i
++)
834 crc
= crc8((crc
^ rest
[i
]) << 8);
838 static u8
i2c_smbus_pec(int count
, u8
*first
, u8
*rest
)
840 return i2c_smbus_partial_pec(0, count
, first
, rest
);
843 /* Returns new "size" (transaction type)
844 Note that we convert byte to byte_data and byte_data to word_data
845 rather than invent new xxx_PEC transactions. */
846 static int i2c_smbus_add_pec(u16 addr
, u8 command
, int size
,
847 union i2c_smbus_data
*data
)
855 data
->byte
= i2c_smbus_pec(2, buf
, NULL
);
856 size
= I2C_SMBUS_BYTE_DATA
;
858 case I2C_SMBUS_BYTE_DATA
:
860 data
->word
= buf
[2] ||
861 (i2c_smbus_pec(3, buf
, NULL
) << 8);
862 size
= I2C_SMBUS_WORD_DATA
;
864 case I2C_SMBUS_WORD_DATA
:
867 case I2C_SMBUS_BLOCK_DATA
:
868 data
->block
[data
->block
[0] + 1] =
869 i2c_smbus_pec(2, buf
, data
->block
);
870 size
= I2C_SMBUS_BLOCK_DATA_PEC
;
876 static int i2c_smbus_check_pec(u16 addr
, u8 command
, int size
, u8 partial
,
877 union i2c_smbus_data
*data
)
879 u8 buf
[3], rpec
, cpec
;
883 case I2C_SMBUS_BYTE_DATA
:
884 buf
[0] = (addr
<< 1) | 1;
885 cpec
= i2c_smbus_pec(2, buf
, NULL
);
888 case I2C_SMBUS_WORD_DATA
:
889 buf
[0] = (addr
<< 1) | 1;
890 buf
[2] = data
->word
& 0xff;
891 cpec
= i2c_smbus_pec(3, buf
, NULL
);
892 rpec
= data
->word
>> 8;
894 case I2C_SMBUS_WORD_DATA_PEC
:
898 case I2C_SMBUS_PROC_CALL_PEC
:
902 case I2C_SMBUS_BLOCK_DATA_PEC
:
903 buf
[0] = (addr
<< 1);
904 buf
[2] = (addr
<< 1) | 1;
905 cpec
= i2c_smbus_pec(3, buf
, data
->block
);
906 rpec
= data
->block
[data
->block
[0] + 1];
908 case I2C_SMBUS_BLOCK_PROC_CALL_PEC
:
909 buf
[0] = (addr
<< 1) | 1;
910 rpec
= i2c_smbus_partial_pec(partial
, 1,
912 cpec
= data
->block
[data
->block
[0] + 1];
919 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
926 s32
i2c_smbus_write_quick(struct i2c_client
*client
, u8 value
)
928 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
929 value
,0,I2C_SMBUS_QUICK
,NULL
);
932 s32
i2c_smbus_read_byte(struct i2c_client
*client
)
934 union i2c_smbus_data data
;
935 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
936 I2C_SMBUS_READ
,0,I2C_SMBUS_BYTE
, &data
))
939 return 0x0FF & data
.byte
;
942 s32
i2c_smbus_write_byte(struct i2c_client
*client
, u8 value
)
944 union i2c_smbus_data data
; /* only for PEC */
945 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
946 I2C_SMBUS_WRITE
,value
, I2C_SMBUS_BYTE
,&data
);
949 s32
i2c_smbus_read_byte_data(struct i2c_client
*client
, u8 command
)
951 union i2c_smbus_data data
;
952 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
953 I2C_SMBUS_READ
,command
, I2C_SMBUS_BYTE_DATA
,&data
))
956 return 0x0FF & data
.byte
;
959 s32
i2c_smbus_write_byte_data(struct i2c_client
*client
, u8 command
, u8 value
)
961 union i2c_smbus_data data
;
963 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
964 I2C_SMBUS_WRITE
,command
,
965 I2C_SMBUS_BYTE_DATA
,&data
);
968 s32
i2c_smbus_read_word_data(struct i2c_client
*client
, u8 command
)
970 union i2c_smbus_data data
;
971 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
972 I2C_SMBUS_READ
,command
, I2C_SMBUS_WORD_DATA
, &data
))
975 return 0x0FFFF & data
.word
;
978 s32
i2c_smbus_write_word_data(struct i2c_client
*client
, u8 command
, u16 value
)
980 union i2c_smbus_data data
;
982 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
983 I2C_SMBUS_WRITE
,command
,
984 I2C_SMBUS_WORD_DATA
,&data
);
987 s32
i2c_smbus_write_block_data(struct i2c_client
*client
, u8 command
,
988 u8 length
, u8
*values
)
990 union i2c_smbus_data data
;
992 if (length
> I2C_SMBUS_BLOCK_MAX
)
993 length
= I2C_SMBUS_BLOCK_MAX
;
994 for (i
= 1; i
<= length
; i
++)
995 data
.block
[i
] = values
[i
-1];
996 data
.block
[0] = length
;
997 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
998 I2C_SMBUS_WRITE
,command
,
999 I2C_SMBUS_BLOCK_DATA
,&data
);
1002 /* Returns the number of read bytes */
1003 s32
i2c_smbus_read_i2c_block_data(struct i2c_client
*client
, u8 command
, u8
*values
)
1005 union i2c_smbus_data data
;
1007 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1008 I2C_SMBUS_READ
,command
,
1009 I2C_SMBUS_I2C_BLOCK_DATA
,&data
))
1012 for (i
= 1; i
<= data
.block
[0]; i
++)
1013 values
[i
-1] = data
.block
[i
];
1014 return data
.block
[0];
1018 /* Simulate a SMBus command using the i2c protocol
1019 No checking of parameters is done! */
1020 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
* adapter
, u16 addr
,
1021 unsigned short flags
,
1022 char read_write
, u8 command
, int size
,
1023 union i2c_smbus_data
* data
)
1025 /* So we need to generate a series of msgs. In the case of writing, we
1026 need to use only one message; when reading, we need two. We initialize
1027 most things with sane defaults, to keep the code below somewhat
1029 unsigned char msgbuf0
[34];
1030 unsigned char msgbuf1
[34];
1031 int num
= read_write
== I2C_SMBUS_READ
?2:1;
1032 struct i2c_msg msg
[2] = { { addr
, flags
, 1, msgbuf0
},
1033 { addr
, flags
| I2C_M_RD
, 0, msgbuf1
}
1037 msgbuf0
[0] = command
;
1039 case I2C_SMBUS_QUICK
:
1041 /* Special case: The read/write field is used as data */
1042 msg
[0].flags
= flags
| (read_write
==I2C_SMBUS_READ
)?I2C_M_RD
:0;
1045 case I2C_SMBUS_BYTE
:
1046 if (read_write
== I2C_SMBUS_READ
) {
1047 /* Special case: only a read! */
1048 msg
[0].flags
= I2C_M_RD
| flags
;
1052 case I2C_SMBUS_BYTE_DATA
:
1053 if (read_write
== I2C_SMBUS_READ
)
1057 msgbuf0
[1] = data
->byte
;
1060 case I2C_SMBUS_WORD_DATA
:
1061 if (read_write
== I2C_SMBUS_READ
)
1065 msgbuf0
[1] = data
->word
& 0xff;
1066 msgbuf0
[2] = (data
->word
>> 8) & 0xff;
1069 case I2C_SMBUS_PROC_CALL
:
1070 num
= 2; /* Special case */
1071 read_write
= I2C_SMBUS_READ
;
1074 msgbuf0
[1] = data
->word
& 0xff;
1075 msgbuf0
[2] = (data
->word
>> 8) & 0xff;
1077 case I2C_SMBUS_BLOCK_DATA
:
1078 case I2C_SMBUS_BLOCK_DATA_PEC
:
1079 if (read_write
== I2C_SMBUS_READ
) {
1080 dev_err(&adapter
->dev
, "Block read not supported "
1081 "under I2C emulation!\n");
1084 msg
[0].len
= data
->block
[0] + 2;
1085 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
1086 dev_err(&adapter
->dev
, "smbus_access called with "
1087 "invalid block write size (%d)\n",
1091 if(size
== I2C_SMBUS_BLOCK_DATA_PEC
)
1093 for (i
= 1; i
<= msg
[0].len
; i
++)
1094 msgbuf0
[i
] = data
->block
[i
-1];
1097 case I2C_SMBUS_BLOCK_PROC_CALL
:
1098 case I2C_SMBUS_BLOCK_PROC_CALL_PEC
:
1099 dev_dbg(&adapter
->dev
, "Block process call not supported "
1100 "under I2C emulation!\n");
1102 case I2C_SMBUS_I2C_BLOCK_DATA
:
1103 if (read_write
== I2C_SMBUS_READ
) {
1104 msg
[1].len
= I2C_SMBUS_I2C_BLOCK_MAX
;
1106 msg
[0].len
= data
->block
[0] + 1;
1107 if (msg
[0].len
> I2C_SMBUS_I2C_BLOCK_MAX
+ 1) {
1108 dev_err(&adapter
->dev
, "i2c_smbus_xfer_emulated called with "
1109 "invalid block write size (%d)\n",
1113 for (i
= 1; i
<= data
->block
[0]; i
++)
1114 msgbuf0
[i
] = data
->block
[i
];
1118 dev_err(&adapter
->dev
, "smbus_access called with invalid size (%d)\n",
1123 if (i2c_transfer(adapter
, msg
, num
) < 0)
1126 if (read_write
== I2C_SMBUS_READ
)
1128 case I2C_SMBUS_BYTE
:
1129 data
->byte
= msgbuf0
[0];
1131 case I2C_SMBUS_BYTE_DATA
:
1132 data
->byte
= msgbuf1
[0];
1134 case I2C_SMBUS_WORD_DATA
:
1135 case I2C_SMBUS_PROC_CALL
:
1136 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
1138 case I2C_SMBUS_I2C_BLOCK_DATA
:
1139 /* fixed at 32 for now */
1140 data
->block
[0] = I2C_SMBUS_I2C_BLOCK_MAX
;
1141 for (i
= 0; i
< I2C_SMBUS_I2C_BLOCK_MAX
; i
++)
1142 data
->block
[i
+1] = msgbuf1
[i
];
1149 s32
i2c_smbus_xfer(struct i2c_adapter
* adapter
, u16 addr
, unsigned short flags
,
1150 char read_write
, u8 command
, int size
,
1151 union i2c_smbus_data
* data
)
1157 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
;
1158 if((flags
& I2C_CLIENT_PEC
) &&
1159 !(i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_HWPEC_CALC
))) {
1161 if(read_write
== I2C_SMBUS_READ
&&
1162 size
== I2C_SMBUS_BLOCK_DATA
)
1163 size
= I2C_SMBUS_BLOCK_DATA_PEC
;
1164 else if(size
== I2C_SMBUS_PROC_CALL
)
1165 size
= I2C_SMBUS_PROC_CALL_PEC
;
1166 else if(size
== I2C_SMBUS_BLOCK_PROC_CALL
) {
1167 i2c_smbus_add_pec(addr
, command
,
1168 I2C_SMBUS_BLOCK_DATA
, data
);
1169 partial
= data
->block
[data
->block
[0] + 1];
1170 size
= I2C_SMBUS_BLOCK_PROC_CALL_PEC
;
1171 } else if(read_write
== I2C_SMBUS_WRITE
&&
1172 size
!= I2C_SMBUS_QUICK
&&
1173 size
!= I2C_SMBUS_I2C_BLOCK_DATA
)
1174 size
= i2c_smbus_add_pec(addr
, command
, size
, data
);
1177 if (adapter
->algo
->smbus_xfer
) {
1178 down(&adapter
->bus_lock
);
1179 res
= adapter
->algo
->smbus_xfer(adapter
,addr
,flags
,read_write
,
1181 up(&adapter
->bus_lock
);
1183 res
= i2c_smbus_xfer_emulated(adapter
,addr
,flags
,read_write
,
1186 if(res
>= 0 && swpec
&&
1187 size
!= I2C_SMBUS_QUICK
&& size
!= I2C_SMBUS_I2C_BLOCK_DATA
&&
1188 (read_write
== I2C_SMBUS_READ
|| size
== I2C_SMBUS_PROC_CALL_PEC
||
1189 size
== I2C_SMBUS_BLOCK_PROC_CALL_PEC
)) {
1190 if(i2c_smbus_check_pec(addr
, command
, size
, partial
, data
))
1197 /* Next four are needed by i2c-isa */
1198 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release
);
1199 EXPORT_SYMBOL_GPL(i2c_adapter_driver
);
1200 EXPORT_SYMBOL_GPL(i2c_adapter_class
);
1201 EXPORT_SYMBOL_GPL(i2c_bus_type
);
1203 EXPORT_SYMBOL(i2c_add_adapter
);
1204 EXPORT_SYMBOL(i2c_del_adapter
);
1205 EXPORT_SYMBOL(i2c_add_driver
);
1206 EXPORT_SYMBOL(i2c_del_driver
);
1207 EXPORT_SYMBOL(i2c_attach_client
);
1208 EXPORT_SYMBOL(i2c_detach_client
);
1209 EXPORT_SYMBOL(i2c_use_client
);
1210 EXPORT_SYMBOL(i2c_release_client
);
1211 EXPORT_SYMBOL(i2c_clients_command
);
1212 EXPORT_SYMBOL(i2c_check_addr
);
1214 EXPORT_SYMBOL(i2c_master_send
);
1215 EXPORT_SYMBOL(i2c_master_recv
);
1216 EXPORT_SYMBOL(i2c_control
);
1217 EXPORT_SYMBOL(i2c_transfer
);
1218 EXPORT_SYMBOL(i2c_get_adapter
);
1219 EXPORT_SYMBOL(i2c_put_adapter
);
1220 EXPORT_SYMBOL(i2c_probe
);
1222 EXPORT_SYMBOL(i2c_smbus_xfer
);
1223 EXPORT_SYMBOL(i2c_smbus_write_quick
);
1224 EXPORT_SYMBOL(i2c_smbus_read_byte
);
1225 EXPORT_SYMBOL(i2c_smbus_write_byte
);
1226 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
1227 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
1228 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
1229 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
1230 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
1231 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
1233 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1234 MODULE_DESCRIPTION("I2C-Bus main module");
1235 MODULE_LICENSE("GPL");