fc8300_i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : fc8300_i2c.c
  4. Description : source of I2C interface
  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/i2c.h>
  20. #include <linux/module.h>
  21. #include<linux/slab.h>
  22. #include <mach/sec_debug.h>
  23. #include "fci_types.h"
  24. #include "fc8300_regs.h"
  25. #include "fci_oal.h"
  26. #include "fc8300_spi.h"
  27. #define I2C_M_FCIRD 1
  28. #define I2C_M_FCIWR 0
  29. #define I2C_MAX_SEND_LENGTH 256
  30. struct i2c_ts_driver{
  31. struct i2c_client *client;
  32. struct input_dev *input_dev;
  33. struct work_struct work;
  34. };
  35. struct i2c_client *fc8300_i2c;
  36. struct i2c_driver fc8300_i2c_driver;
  37. static DEFINE_MUTEX(fci_i2c_lock);
  38. static int fc8300_i2c_probe(struct i2c_client *i2c_client,
  39. const struct i2c_device_id *id)
  40. {
  41. pr_info("%s \n",__func__);
  42. fc8300_i2c = i2c_client;
  43. i2c_set_clientdata(i2c_client, NULL);
  44. return 0;
  45. }
  46. static int fc8300_remove(struct i2c_client *client)
  47. {
  48. pr_info("%s \n",__func__);
  49. return 0;
  50. }
  51. static const struct i2c_device_id fc8300_id[] = {
  52. { "fc8300_i2c", 0 },
  53. { },
  54. };
  55. static struct of_device_id fc8300_match_table[] = {
  56. { .compatible = "isdb,isdb_fc8300",},
  57. { },
  58. };
  59. struct i2c_driver fc8300_i2c_driver = {
  60. .driver = {
  61. .name = "fc8300_i2c",
  62. .owner = THIS_MODULE,
  63. .of_match_table = fc8300_match_table,
  64. },
  65. .probe = fc8300_i2c_probe,
  66. .remove = fc8300_remove,
  67. .id_table = fc8300_id,
  68. };
  69. static s32 i2c_bulkread(HANDLE handle, u8 chip, u16 addr, u8 *data, u16 length)
  70. {
  71. int res;
  72. struct i2c_msg rmsg[2];
  73. unsigned char i2c_data[2];
  74. rmsg[0].addr = chip;
  75. rmsg[0].flags = I2C_M_FCIWR;
  76. rmsg[0].len = 2;
  77. rmsg[0].buf = i2c_data;
  78. i2c_data[0] = (addr >> 8) & 0xff;
  79. i2c_data[1] = addr & 0xff;
  80. rmsg[1].addr = chip;
  81. rmsg[1].flags = I2C_M_FCIRD;
  82. rmsg[1].len = length;
  83. rmsg[1].buf = data;
  84. res = i2c_transfer(fc8300_i2c->adapter, &rmsg[0], 2);
  85. return 0;
  86. }
  87. static s32 i2c_bulkwrite(HANDLE handle, u8 chip, u16 addr, u8 *data, u16 length)
  88. {
  89. int res;
  90. struct i2c_msg wmsg;
  91. unsigned char i2c_data[I2C_MAX_SEND_LENGTH + 2];
  92. //prevent issue CID 23929
  93. if ((length) > I2C_MAX_SEND_LENGTH)
  94. return -ENODEV;
  95. wmsg.addr = chip;
  96. wmsg.flags = I2C_M_FCIWR;
  97. wmsg.len = length + 2;
  98. wmsg.buf = i2c_data;
  99. i2c_data[0] = (addr >> 8) & 0xff;
  100. i2c_data[1] = addr & 0xff;
  101. memcpy(&i2c_data[2], data, length);
  102. res = i2c_transfer(fc8300_i2c->adapter, &wmsg, 1);
  103. return 0;
  104. }
  105. s32 fc8300_i2c_init(HANDLE handle, u16 param1, u16 param2)
  106. {
  107. s32 res;
  108. //OAL_CREATE_SEMAPHORE();
  109. pr_info("%s \n",__func__);
  110. //prevent issue CID 23943
  111. fc8300_i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  112. if (fc8300_i2c == NULL)
  113. return -ENOMEM;
  114. res = i2c_add_driver(&fc8300_i2c_driver);
  115. #ifdef BBM_I2C_SPI
  116. fc8300_spi_init(handle, 0, 0);
  117. #else
  118. /* ts_initialize(); */
  119. #endif
  120. return res;
  121. }
  122. s32 fc8300_i2c_byteread(HANDLE handle, DEVICEID devid, u16 addr, u8 *data)
  123. {
  124. s32 res;
  125. mutex_lock(&fci_i2c_lock);
  126. res = i2c_bulkread(handle, (u8) (devid >> 8) & 0xff, addr, data, 1);
  127. mutex_unlock(&fci_i2c_lock);
  128. return res;
  129. }
  130. s32 fc8300_i2c_wordread(HANDLE handle, DEVICEID devid, u16 addr, u16 *data)
  131. {
  132. s32 res;
  133. mutex_lock(&fci_i2c_lock);
  134. res = i2c_bulkread(handle, (u8) (devid >> 8) & 0xff,
  135. addr, (u8 *) data, 2);
  136. mutex_unlock(&fci_i2c_lock);
  137. return res;
  138. }
  139. s32 fc8300_i2c_longread(HANDLE handle, DEVICEID devid, u16 addr, u32 *data)
  140. {
  141. s32 res;
  142. mutex_lock(&fci_i2c_lock);
  143. res = i2c_bulkread(handle, (u8) (devid >> 8) & 0xff,
  144. addr, (u8 *) data, 4);
  145. mutex_unlock(&fci_i2c_lock);
  146. return res;
  147. }
  148. s32 fc8300_i2c_bulkread(HANDLE handle, DEVICEID devid,
  149. u16 addr, u8 *data, u16 length)
  150. {
  151. s32 res;
  152. mutex_lock(&fci_i2c_lock);
  153. res = i2c_bulkread(handle, (u8) (devid >> 8) & 0xff,
  154. addr, data, length);
  155. mutex_unlock(&fci_i2c_lock);
  156. return res;
  157. }
  158. s32 fc8300_i2c_bytewrite(HANDLE handle, DEVICEID devid, u16 addr, u8 data)
  159. {
  160. s32 res;
  161. mutex_lock(&fci_i2c_lock);
  162. res = i2c_bulkwrite(handle, (u8) (devid >> 8) & 0xff,
  163. addr, (u8 *)&data, 1);
  164. mutex_unlock(&fci_i2c_lock);
  165. return res;
  166. }
  167. s32 fc8300_i2c_wordwrite(HANDLE handle, DEVICEID devid, u16 addr, u16 data)
  168. {
  169. s32 res;
  170. mutex_lock(&fci_i2c_lock);
  171. res = i2c_bulkwrite(handle, (u8) (devid >> 8) & 0xff,
  172. addr, (u8 *)&data, 2);
  173. mutex_unlock(&fci_i2c_lock);
  174. return res;
  175. }
  176. s32 fc8300_i2c_longwrite(HANDLE handle, DEVICEID devid, u16 addr, u32 data)
  177. {
  178. s32 res;
  179. mutex_lock(&fci_i2c_lock);
  180. res = i2c_bulkwrite(handle, (u8) (devid >> 8) & 0xff,
  181. addr, (u8 *)&data, 4);
  182. mutex_unlock(&fci_i2c_lock);
  183. return res;
  184. }
  185. s32 fc8300_i2c_bulkwrite(HANDLE handle, DEVICEID devid,
  186. u16 addr, u8 *data, u16 length)
  187. {
  188. s32 res;
  189. mutex_lock(&fci_i2c_lock);
  190. res = i2c_bulkwrite(handle, (u8) (devid >> 8) & 0xff,
  191. addr, data, length);
  192. mutex_unlock(&fci_i2c_lock);
  193. return res;
  194. }
  195. s32 fc8300_i2c_dataread(HANDLE handle, DEVICEID devid,
  196. u16 addr, u8 *data, u32 length)
  197. {
  198. s32 res;
  199. ISDB_PR_DBG("%s \n",__func__);
  200. #ifdef BBM_I2C_SPI
  201. res = fc8300_spi_dataread(handle, devid,
  202. addr, data, length);
  203. #else
  204. mutex_lock(&fci_i2c_lock);
  205. res = i2c_bulkread(handle, (u8) (devid >> 8) & 0xff,
  206. addr, data, length);
  207. mutex_unlock(&fci_i2c_lock);
  208. #endif
  209. return res;
  210. }
  211. s32 fc8300_i2c_deinit(HANDLE handle)
  212. {
  213. i2c_del_driver(&fc8300_i2c_driver);
  214. #ifdef BBM_I2C_SPI
  215. fc8300_spi_deinit(handle);
  216. #else
  217. /* ts_receiver_disable(); */
  218. #endif
  219. //OAL_DELETE_SEMAPHORE();
  220. return BBM_OK;
  221. }