fc8080_i2c.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : fc8080_i2c.c
  4. Description : i2c interface source file
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. History :
  17. ----------------------------------------------------------------------
  18. *******************************************************************************/
  19. #include <linux/module.h>
  20. #include <linux/input.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/i2c.h>
  24. #include <linux/delay.h>
  25. #include <linux/mutex.h>
  26. #include "fci_types.h"
  27. #include "fc8080_regs.h"
  28. #include "fci_oal.h"
  29. #include "tdmb.h"
  30. #define CHIP_ADDR 0x58
  31. #define I2C_M_FCIRD 1
  32. #define I2C_M_FCIWR 0
  33. #define I2C_MAX_SEND_LENGTH 500
  34. struct i2c_ts_driver {
  35. struct i2c_client *client;
  36. struct input_dev *input_dev;
  37. struct work_struct work;
  38. };
  39. struct i2c_client *fc8080_i2c;
  40. static DEFINE_MUTEX(lock);
  41. static s32 i2c_bulkread(HANDLE handle, u8 chip, u16 addr, u8 *data, u16 length)
  42. {
  43. int res;
  44. struct i2c_msg rmsg[2];
  45. unsigned char i2c_data[2];
  46. rmsg[0].addr = CHIP_ADDR;
  47. rmsg[0].flags = I2C_M_FCIWR;
  48. rmsg[0].len = 2;
  49. rmsg[0].buf = i2c_data;
  50. i2c_data[0] = (addr >> 8) & 0xff;
  51. i2c_data[1] = (addr) & 0xff;
  52. rmsg[1].addr = CHIP_ADDR;
  53. rmsg[1].flags = I2C_M_FCIRD;
  54. rmsg[1].len = length;
  55. rmsg[1].buf = data;
  56. res = i2c_transfer(fc8080_i2c->adapter, &rmsg[0], 2);
  57. return res;
  58. }
  59. static s32 i2c_bulkwrite(HANDLE handle, u8 chip, u16 addr, u8 *data, u16 length)
  60. {
  61. int res;
  62. struct i2c_msg wmsg;
  63. unsigned char i2c_data[I2C_MAX_SEND_LENGTH];
  64. if ((length + 2) > I2C_MAX_SEND_LENGTH)
  65. return -ENODEV;
  66. wmsg.addr = CHIP_ADDR;
  67. wmsg.flags = I2C_M_FCIWR;
  68. wmsg.len = length + 2;
  69. wmsg.buf = i2c_data;
  70. i2c_data[0] = (addr >> 8) & 0xff;
  71. i2c_data[1] = (addr) & 0xff;
  72. memcpy(&i2c_data[2], data, length);
  73. res = i2c_transfer(fc8080_i2c->adapter, &wmsg, 1);
  74. return res;
  75. }
  76. static s32 i2c_dataread(HANDLE handle, u8 chip, u16 addr, u8 *data, u16 length)
  77. {
  78. return i2c_bulkread(handle, chip, addr, data, length);
  79. }
  80. s32 fc8080_i2c_init(HANDLE handle, u16 param1, u16 param2)
  81. {
  82. static u32 fc8080_i2c_temp;
  83. fc8080_i2c_temp = param2;
  84. fc8080_i2c_temp <<= 16;
  85. fc8080_i2c_temp |= param1;
  86. fc8080_i2c = (struct i2c_client *)fc8080_i2c_temp;
  87. DPRINTK("%s : 0x%p\n", __func__, fc8080_i2c);
  88. return BBM_OK;
  89. }
  90. s32 fc8080_i2c_byteread(HANDLE handle, u16 addr, u8 *data)
  91. {
  92. s32 res;
  93. mutex_lock(&lock);
  94. res = i2c_bulkread(handle, (u8) CHIP_ADDR, addr, data, 1);
  95. mutex_unlock(&lock);
  96. return res;
  97. }
  98. s32 fc8080_i2c_wordread(HANDLE handle, u16 addr, u16 *data)
  99. {
  100. s32 res;
  101. mutex_lock(&lock);
  102. res = i2c_bulkread(handle, (u8) CHIP_ADDR, addr, (u8 *) data, 2);
  103. mutex_unlock(&lock);
  104. return res;
  105. }
  106. s32 fc8080_i2c_longread(HANDLE handle, u16 addr, u32 *data)
  107. {
  108. s32 res;
  109. mutex_lock(&lock);
  110. res = i2c_bulkread(handle, (u8) CHIP_ADDR, addr, (u8 *) data, 4);
  111. mutex_unlock(&lock);
  112. return res;
  113. }
  114. s32 fc8080_i2c_bulkread(HANDLE handle, u16 addr, u8 *data, u16 length)
  115. {
  116. s32 res;
  117. mutex_lock(&lock);
  118. res = i2c_bulkread(handle, (u8) CHIP_ADDR, addr, data, length);
  119. mutex_unlock(&lock);
  120. return res;
  121. }
  122. s32 fc8080_i2c_bytewrite(HANDLE handle, u16 addr, u8 data)
  123. {
  124. s32 res;
  125. mutex_lock(&lock);
  126. res = i2c_bulkwrite(handle, (u8) CHIP_ADDR, addr, (u8 *) &data, 1);
  127. mutex_unlock(&lock);
  128. return res;
  129. }
  130. s32 fc8080_i2c_wordwrite(HANDLE handle, u16 addr, u16 data)
  131. {
  132. s32 res;
  133. mutex_lock(&lock);
  134. res = i2c_bulkwrite(handle, (u8) CHIP_ADDR, addr, (u8 *) &data, 2);
  135. mutex_unlock(&lock);
  136. return res;
  137. }
  138. s32 fc8080_i2c_longwrite(HANDLE handle, u16 addr, u32 data)
  139. {
  140. s32 res;
  141. mutex_lock(&lock);
  142. res = i2c_bulkwrite(handle, (u8) CHIP_ADDR, addr, (u8 *) &data, 4);
  143. mutex_unlock(&lock);
  144. return res;
  145. }
  146. s32 fc8080_i2c_bulkwrite(HANDLE handle, u16 addr, u8 *data, u16 length)
  147. {
  148. s32 res;
  149. mutex_lock(&lock);
  150. res = i2c_bulkwrite(handle, (u8) CHIP_ADDR, addr, data, length);
  151. mutex_unlock(&lock);
  152. return res;
  153. }
  154. s32 fc8080_i2c_dataread(HANDLE handle, u16 addr, u8 *data, u32 length)
  155. {
  156. s32 res;
  157. mutex_lock(&lock);
  158. res = i2c_dataread(handle, (u8) CHIP_ADDR, addr, data, length);
  159. mutex_unlock(&lock);
  160. return res;
  161. }
  162. s32 fc8080_i2c_deinit(HANDLE handle)
  163. {
  164. return BBM_OK;
  165. }