A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
11KB

  1. /*
  2. i2c-dev.h - i2c-bus driver, char device interface
  3. Copyright (C) 1995-97 Simon G. Vogl
  4. Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
  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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* $Id: i2c-dev.h 4948 2007-10-14 11:21:00Z khali $ */
  18. #ifndef LIB_I2CDEV_H
  19. #define LIB_I2CDEV_H
  20. #include <linux/types.h>
  21. #include <sys/ioctl.h>
  22. /* -- i2c.h -- */
  23. /*
  24. * I2C Message - used for pure i2c transaction, also from /dev interface
  25. */
  26. struct i2c_msg {
  27. __u16 addr; /* slave address */
  28. unsigned short flags;
  29. #define I2C_M_TEN 0x10 /* we have a ten bit chip address */
  30. #define I2C_M_RD 0x01
  31. #define I2C_M_NOSTART 0x4000
  32. #define I2C_M_REV_DIR_ADDR 0x2000
  33. #define I2C_M_IGNORE_NAK 0x1000
  34. #define I2C_M_NO_RD_ACK 0x0800
  35. short len; /* msg length */
  36. char *buf; /* pointer to msg data */
  37. };
  38. /* To determine what functionality is present */
  39. #define I2C_FUNC_I2C 0x00000001
  40. #define I2C_FUNC_10BIT_ADDR 0x00000002
  41. #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
  42. #define I2C_FUNC_SMBUS_PEC 0x00000008
  43. #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
  44. #define I2C_FUNC_SMBUS_QUICK 0x00010000
  45. #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
  46. #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
  47. #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
  48. #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
  49. #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
  50. #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
  51. #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
  52. #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
  53. #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
  54. #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
  55. #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
  56. #define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 /* I2C-like block xfer */
  57. #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 /* w/ 2-byte reg. addr. */
  58. #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
  59. I2C_FUNC_SMBUS_WRITE_BYTE)
  60. #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
  61. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
  62. #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
  63. I2C_FUNC_SMBUS_WRITE_WORD_DATA)
  64. #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
  65. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
  66. #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
  67. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
  68. #define I2C_FUNC_SMBUS_I2C_BLOCK_2 (I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 | \
  69. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2)
  70. /* Old name, for compatibility */
  71. #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC
  72. /*
  73. * Data for SMBus Messages
  74. */
  75. #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
  76. #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
  77. union i2c_smbus_data {
  78. __u8 byte;
  79. __u16 word;
  80. __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
  81. /* and one more for PEC */
  82. };
  83. /* smbus_access read or write markers */
  84. #define I2C_SMBUS_READ 1
  85. #define I2C_SMBUS_WRITE 0
  86. /* SMBus transaction types (size parameter in the above functions)
  87. Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
  88. #define I2C_SMBUS_QUICK 0
  89. #define I2C_SMBUS_BYTE 1
  90. #define I2C_SMBUS_BYTE_DATA 2
  91. #define I2C_SMBUS_WORD_DATA 3
  92. #define I2C_SMBUS_PROC_CALL 4
  93. #define I2C_SMBUS_BLOCK_DATA 5
  94. #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
  95. #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
  96. #define I2C_SMBUS_I2C_BLOCK_DATA 8
  97. /* ----- commands for the ioctl like i2c_command call:
  98. * note that additional calls are defined in the algorithm and hw
  99. * dependent layers - these can be listed here, or see the
  100. * corresponding header files.
  101. */
  102. /* -> bit-adapter specific ioctls */
  103. #define I2C_RETRIES 0x0701 /* number of times a device address */
  104. /* should be polled when not */
  105. /* acknowledging */
  106. #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */
  107. /* this is for i2c-dev.c */
  108. #define I2C_SLAVE 0x0703 /* Change slave address */
  109. /* Attn.: Slave address is 7 or 10 bits */
  110. #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
  111. /* Attn.: Slave address is 7 or 10 bits */
  112. /* This changes the address, even if it */
  113. /* is already taken! */
  114. #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
  115. #define I2C_FUNCS 0x0705 /* Get the adapter functionality */
  116. #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
  117. #define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
  118. #define I2C_SMBUS 0x0720 /* SMBus-level access */
  119. /* -- i2c.h -- */
  120. /* Note: 10-bit addresses are NOT supported! */
  121. /* This is the structure as used in the I2C_SMBUS ioctl call */
  122. struct i2c_smbus_ioctl_data {
  123. char read_write;
  124. __u8 command;
  125. int size;
  126. union i2c_smbus_data *data;
  127. };
  128. /* This is the structure as used in the I2C_RDWR ioctl call */
  129. struct i2c_rdwr_ioctl_data {
  130. struct i2c_msg *msgs; /* pointers to i2c_msgs */
  131. int nmsgs; /* number of i2c_msgs */
  132. };
  133. static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
  134. int size, union i2c_smbus_data *data)
  135. {
  136. struct i2c_smbus_ioctl_data args;
  137. args.read_write = read_write;
  138. args.command = command;
  139. args.size = size;
  140. args.data = data;
  141. return ioctl(file,I2C_SMBUS,&args);
  142. }
  143. static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
  144. {
  145. return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
  146. }
  147. static inline __s32 i2c_smbus_read_byte(int file)
  148. {
  149. union i2c_smbus_data data;
  150. if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
  151. return -1;
  152. else
  153. return 0x0FF & data.byte;
  154. }
  155. static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
  156. {
  157. return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
  158. I2C_SMBUS_BYTE,NULL);
  159. }
  160. static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
  161. {
  162. union i2c_smbus_data data;
  163. if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
  164. I2C_SMBUS_BYTE_DATA,&data))
  165. return -1;
  166. else
  167. return 0x0FF & data.byte;
  168. }
  169. static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
  170. __u8 value)
  171. {
  172. union i2c_smbus_data data;
  173. data.byte = value;
  174. return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  175. I2C_SMBUS_BYTE_DATA, &data);
  176. }
  177. static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
  178. {
  179. union i2c_smbus_data data;
  180. if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
  181. I2C_SMBUS_WORD_DATA,&data))
  182. return -1;
  183. else
  184. return 0x0FFFF & data.word;
  185. }
  186. static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
  187. __u16 value)
  188. {
  189. union i2c_smbus_data data;
  190. data.word = value;
  191. return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  192. I2C_SMBUS_WORD_DATA, &data);
  193. }
  194. static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
  195. {
  196. union i2c_smbus_data data;
  197. data.word = value;
  198. if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  199. I2C_SMBUS_PROC_CALL,&data))
  200. return -1;
  201. else
  202. return 0x0FFFF & data.word;
  203. }
  204. /* Returns the number of read bytes */
  205. static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
  206. __u8 *values)
  207. {
  208. union i2c_smbus_data data;
  209. int i;
  210. if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
  211. I2C_SMBUS_BLOCK_DATA,&data))
  212. return -1;
  213. else {
  214. for (i = 1; i <= data.block[0]; i++)
  215. values[i-1] = data.block[i];
  216. return data.block[0];
  217. }
  218. }
  219. static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
  220. __u8 length, __u8 *values)
  221. {
  222. union i2c_smbus_data data;
  223. int i;
  224. if (length > 32)
  225. length = 32;
  226. for (i = 1; i <= length; i++)
  227. data.block[i] = values[i-1];
  228. data.block[0] = length;
  229. return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  230. I2C_SMBUS_BLOCK_DATA, &data);
  231. }
  232. /* Returns the number of read bytes */
  233. /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
  234. ask for less than 32 bytes, your code will only work with kernels
  235. 2.6.23 and later. */
  236. static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
  237. __u8 length, __u8 *values)
  238. {
  239. union i2c_smbus_data data;
  240. int i;
  241. if (length > 32)
  242. length = 32;
  243. data.block[0] = length;
  244. if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
  245. length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
  246. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  247. return -1;
  248. else {
  249. for (i = 1; i <= data.block[0]; i++)
  250. values[i-1] = data.block[i];
  251. return data.block[0];
  252. }
  253. }
  254. static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
  255. __u8 length, __u8 *values)
  256. {
  257. union i2c_smbus_data data;
  258. int i;
  259. if (length > 32)
  260. length = 32;
  261. for (i = 1; i <= length; i++)
  262. data.block[i] = values[i-1];
  263. data.block[0] = length;
  264. return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  265. I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
  266. }
  267. /* Returns the number of read bytes */
  268. static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
  269. __u8 length, __u8 *values)
  270. {
  271. union i2c_smbus_data data;
  272. int i;
  273. if (length > 32)
  274. length = 32;
  275. for (i = 1; i <= length; i++)
  276. data.block[i] = values[i-1];
  277. data.block[0] = length;
  278. if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
  279. I2C_SMBUS_BLOCK_PROC_CALL,&data))
  280. return -1;
  281. else {
  282. for (i = 1; i <= data.block[0]; i++)
  283. values[i-1] = data.block[i];
  284. return data.block[0];
  285. }
  286. }
  287. #endif /* LIB_I2CDEV_H */