Changeset 6054

Show
Ignore:
Timestamp:
05/21/12 18:52:48 (12 months ago)
Author:
khali
Message:

New library for I2C device access under Linux. As a first step, the
library will host the i2c_smbus_*() inline functions which were
previously in the user-space flavor of <linux/i2c-dev.h>.

Location:
i2c-tools/trunk
Files:
4 added
6 modified

Legend:

Unmodified
Added
Removed
  • i2c-tools/trunk

  • i2c-tools/trunk/CHANGES

    r6048 r6054  
    88             Move SMBus helper functions to include/i2c/smbus.h 
    99  i2c-stub-from-dump: Be more tolerant on input dump format 
     10  library: New libi2c library 
    1011 
    11123.1.0 (2011-12-04) 
  • i2c-tools/trunk/Makefile

    r5610 r6054  
    1515man8dir = $(mandir)/man8 
    1616incdir  = $(prefix)/include 
     17libdir  = $(prefix)/lib 
    1718 
    1819INSTALL         := install 
     
    2021INSTALL_DIR     := $(INSTALL) -m 755 -d 
    2122INSTALL_PROGRAM := $(INSTALL) -m 755 
     23LN              := ln -sf 
    2224RM              := rm -f 
    2325 
    2426CC      ?= gcc 
     27AR      ?= ar 
    2528 
    26 CFLAGS  ?= -O2 
     29CFLAGS          ?= -O2 
    2730# When debugging, use the following instead 
    28 #CFLAGS := -O -g 
    29 CFLAGS  += -Wall 
     31#CFLAGS         := -O -g 
     32CFLAGS          += -Wall 
     33SOCFLAGS        := -fpic -D_REENTRANT $(CFLAGS) 
     34 
     35BUILD_STATIC_LIB ?= 1 
    3036 
    3137KERNELVERSION   := $(shell uname -r) 
     
    3743EXTRA   := 
    3844#EXTRA  += py-smbus 
    39 SRCDIRS := include eeprom stub tools $(EXTRA) 
     45SRCDIRS := include lib eeprom stub tools $(EXTRA) 
    4046include $(SRCDIRS:%=%/Module.mk) 
  • i2c-tools/trunk/README

    r5882 r6054  
    22=================== 
    33 
    4 This package contains an heterogeneous set of I2C tools for the Linux kernel. 
    5 These tools were originally part of the lm-sensors project but were finally 
    6 split into their own package for convenience. They compile, run and have been 
    7 tested on GNU/Linux. 
     4This package contains an heterogeneous set of I2C tools for the Linux kernel 
     5as well as an I2C library. The tools were originally part of the lm-sensors 
     6project but were finally split into their own package for convenience. The 
     7library is used by some of the tools, but can also be used by third-party 
     8applications. The tools and library compile, run and have been tested on 
     9GNU/Linux. 
     10 
     11The latest version of the code can be downloaded from: 
     12  http://www.lm-sensors.org/wiki/I2CTools 
    813 
    914 
     
    2631  default. 
    2732 
     33* lib 
     34  The I2C library, used by eepromer, py-smbus and tools. Installed by 
     35  default. 
     36 
    2837* py-smbus 
    2938  Python wrapper for SMBus access over i2c-dev. Not installed by default. 
     
    4150------------ 
    4251 
    43 There's no configure script, so simply run "make" to build the tools, and 
    44 "make install" to install them. You also can use "make uninstall" to remove 
    45 all the files you installed. By default, files are installed in /usr/local 
    46 but you can change this behavior by editing the Makefile file and setting 
    47 prefix to wherever you want. You may change the C compiler and the 
    48 compilation flags as well. 
     52There's no configure script, so simply run "make" to build the library and 
     53tools, and "make install" to install them. You also can use "make uninstall" 
     54to remove all the files you installed. By default, files are installed in 
     55/usr/local but you can change the location by editing the Makefile file and 
     56setting prefix to wherever you want. You may change the C compiler and the 
     57compilation flags as well, and also decide whether to build the static 
     58library or not. 
    4959 
    5060Optionally, you can run "make strip" prior to "make install" if you want 
    5161smaller binaries. However, be aware that this will prevent any further 
    52 attempt to debug the programs. 
     62attempt to debug the library and tools. 
    5363 
    5464If you wish to include sub-directories that are not enabled by default, then 
  • i2c-tools/trunk/include/i2c/smbus.h

    r6053 r6054  
    2424#define LIB_I2C_SMBUS_H 
    2525 
    26 #include <sys/ioctl.h> 
    2726#include <linux/types.h> 
    2827#include <linux/i2c.h> 
    29 #include <linux/i2c-dev.h> 
    3028 
    31 /* Compatibility defines */ 
    32 #ifndef I2C_SMBUS_I2C_BLOCK_BROKEN 
    33 #define I2C_SMBUS_I2C_BLOCK_BROKEN I2C_SMBUS_I2C_BLOCK_DATA 
    34 #endif 
    35 #ifndef I2C_FUNC_SMBUS_PEC 
    36 #define I2C_FUNC_SMBUS_PEC I2C_FUNC_SMBUS_HWPEC_CALC 
    37 #endif 
     29extern __s32 i2c_smbus_access(int file, char read_write, __u8 command, 
     30                              int size, union i2c_smbus_data *data); 
    3831 
    39 static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, 
    40                                      int size, union i2c_smbus_data *data) 
    41 { 
    42         struct i2c_smbus_ioctl_data args; 
    43  
    44         args.read_write = read_write; 
    45         args.command = command; 
    46         args.size = size; 
    47         args.data = data; 
    48         return ioctl(file, I2C_SMBUS, &args); 
    49 } 
    50  
    51  
    52 static inline __s32 i2c_smbus_write_quick(int file, __u8 value) 
    53 { 
    54         return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL); 
    55 } 
    56  
    57 static inline __s32 i2c_smbus_read_byte(int file) 
    58 { 
    59         union i2c_smbus_data data; 
    60         if (i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data)) 
    61                 return -1; 
    62         else 
    63                 return 0x0FF & data.byte; 
    64 } 
    65  
    66 static inline __s32 i2c_smbus_write_byte(int file, __u8 value) 
    67 { 
    68         return i2c_smbus_access(file, I2C_SMBUS_WRITE, value, 
    69                                 I2C_SMBUS_BYTE, NULL); 
    70 } 
    71  
    72 static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) 
    73 { 
    74         union i2c_smbus_data data; 
    75         if (i2c_smbus_access(file, I2C_SMBUS_READ, command, 
    76                              I2C_SMBUS_BYTE_DATA, &data)) 
    77                 return -1; 
    78         else 
    79                 return 0x0FF & data.byte; 
    80 } 
    81  
    82 static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, 
    83                                               __u8 value) 
    84 { 
    85         union i2c_smbus_data data; 
    86         data.byte = value; 
    87         return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    88                                 I2C_SMBUS_BYTE_DATA, &data); 
    89 } 
    90  
    91 static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) 
    92 { 
    93         union i2c_smbus_data data; 
    94         if (i2c_smbus_access(file, I2C_SMBUS_READ, command, 
    95                              I2C_SMBUS_WORD_DATA, &data)) 
    96                 return -1; 
    97         else 
    98                 return 0x0FFFF & data.word; 
    99 } 
    100  
    101 static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, 
    102                                               __u16 value) 
    103 { 
    104         union i2c_smbus_data data; 
    105         data.word = value; 
    106         return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    107                                 I2C_SMBUS_WORD_DATA, &data); 
    108 } 
    109  
    110 static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) 
    111 { 
    112         union i2c_smbus_data data; 
    113         data.word = value; 
    114         if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    115                              I2C_SMBUS_PROC_CALL, &data)) 
    116                 return -1; 
    117         else 
    118                 return 0x0FFFF & data.word; 
    119 } 
    120  
     32extern __s32 i2c_smbus_write_quick(int file, __u8 value); 
     33extern __s32 i2c_smbus_read_byte(int file); 
     34extern __s32 i2c_smbus_write_byte(int file, __u8 value); 
     35extern __s32 i2c_smbus_read_byte_data(int file, __u8 command); 
     36extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); 
     37extern __s32 i2c_smbus_read_word_data(int file, __u8 command); 
     38extern __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); 
     39extern __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); 
    12140 
    12241/* Returns the number of read bytes */ 
    123 static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, 
    124                                               __u8 *values) 
    125 { 
    126         union i2c_smbus_data data; 
    127         int i; 
    128         if (i2c_smbus_access(file, I2C_SMBUS_READ, command, 
    129                              I2C_SMBUS_BLOCK_DATA, &data)) 
    130                 return -1; 
    131         else { 
    132                 for (i = 1; i <= data.block[0]; i++) 
    133                         values[i-1] = data.block[i]; 
    134                 return data.block[0]; 
    135         } 
    136 } 
    137  
    138 static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, 
    139                                                __u8 length, const __u8 *values) 
    140 { 
    141         union i2c_smbus_data data; 
    142         int i; 
    143         if (length > 32) 
    144                 length = 32; 
    145         for (i = 1; i <= length; i++) 
    146                 data.block[i] = values[i-1]; 
    147         data.block[0] = length; 
    148         return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    149                                 I2C_SMBUS_BLOCK_DATA, &data); 
    150 } 
     42extern __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); 
     43extern __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, 
     44                                        const __u8 *values); 
    15145 
    15246/* Returns the number of read bytes */ 
     
    15448   ask for less than 32 bytes, your code will only work with kernels 
    15549   2.6.23 and later. */ 
    156 static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, 
    157                                                   __u8 length, __u8 *values) 
    158 { 
    159         union i2c_smbus_data data; 
    160         int i; 
    161  
    162         if (length > 32) 
    163                 length = 32; 
    164         data.block[0] = length; 
    165         if (i2c_smbus_access(file, I2C_SMBUS_READ, command, 
    166                              length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : 
    167                               I2C_SMBUS_I2C_BLOCK_DATA, &data)) 
    168                 return -1; 
    169         else { 
    170                 for (i = 1; i <= data.block[0]; i++) 
    171                         values[i-1] = data.block[i]; 
    172                 return data.block[0]; 
    173         } 
    174 } 
    175  
    176 static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, 
    177                                                    __u8 length, 
    178                                                    const __u8 *values) 
    179 { 
    180         union i2c_smbus_data data; 
    181         int i; 
    182         if (length > 32) 
    183                 length = 32; 
    184         for (i = 1; i <= length; i++) 
    185                 data.block[i] = values[i-1]; 
    186         data.block[0] = length; 
    187         return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    188                                 I2C_SMBUS_I2C_BLOCK_BROKEN, &data); 
    189 } 
     50extern __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length, 
     51                                           __u8 *values); 
     52extern __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, 
     53                                            const __u8 *values); 
    19054 
    19155/* Returns the number of read bytes */ 
    192 static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, 
    193                                                  __u8 length, __u8 *values) 
    194 { 
    195         union i2c_smbus_data data; 
    196         int i; 
    197         if (length > 32) 
    198                 length = 32; 
    199         for (i = 1; i <= length; i++) 
    200                 data.block[i] = values[i-1]; 
    201         data.block[0] = length; 
    202         if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command, 
    203                              I2C_SMBUS_BLOCK_PROC_CALL, &data)) 
    204                 return -1; 
    205         else { 
    206                 for (i = 1; i <= data.block[0]; i++) 
    207                         values[i-1] = data.block[i]; 
    208                 return data.block[0]; 
    209         } 
    210 } 
     56extern __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, 
     57                                          __u8 *values); 
    21158 
    21259#endif /* LIB_I2C_SMBUS_H */ 
  • i2c-tools/trunk/tools/Module.mk

    r6049 r6054  
    1313                   -Wcast-align -Wwrite-strings -Wnested-externs -Winline \ 
    1414                   -W -Wundef -Wmissing-prototypes -Iinclude 
     15TOOLS_LDFLAGS   := -Llib -li2c 
    1516 
    1617TOOLS_TARGETS   := i2cdetect i2cdump i2cset i2cget 
     
    2122 
    2223$(TOOLS_DIR)/i2cdetect: $(TOOLS_DIR)/i2cdetect.o $(TOOLS_DIR)/i2cbusses.o 
    23         $(CC) $(LDFLAGS) -o $@ $^ 
     24        $(CC) $(LDFLAGS) $(TOOLS_LDFLAGS) -o $@ $^ 
    2425 
    2526$(TOOLS_DIR)/i2cdump: $(TOOLS_DIR)/i2cdump.o $(TOOLS_DIR)/i2cbusses.o $(TOOLS_DIR)/util.o 
    26         $(CC) $(LDFLAGS) -o $@ $^ 
     27        $(CC) $(LDFLAGS) $(TOOLS_LDFLAGS) -o $@ $^ 
    2728 
    2829$(TOOLS_DIR)/i2cset: $(TOOLS_DIR)/i2cset.o $(TOOLS_DIR)/i2cbusses.o $(TOOLS_DIR)/util.o 
    29         $(CC) $(LDFLAGS) -o $@ $^ 
     30        $(CC) $(LDFLAGS) $(TOOLS_LDFLAGS) -o $@ $^ 
    3031 
    3132$(TOOLS_DIR)/i2cget: $(TOOLS_DIR)/i2cget.o $(TOOLS_DIR)/i2cbusses.o $(TOOLS_DIR)/util.o 
    32         $(CC) $(LDFLAGS) -o $@ $^ 
     33        $(CC) $(LDFLAGS) $(TOOLS_LDFLAGS) -o $@ $^ 
    3334 
    3435#