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