fc8150_i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*****************************************************************************
  2. Copyright(c) 2012 FCI Inc. All Rights Reserved
  3. File name : fc8150_i2c.c
  4. Description : fc8150 host interface
  5. *******************************************************************************/
  6. #include <linux/delay.h>
  7. #include <linux/mutex.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include<linux/slab.h>
  11. #include "fci_types.h"
  12. #include "fc8150_regs.h"
  13. #include "fci_oal.h"
  14. #include "fci_hal.h"
  15. #define HPIC_READ 0x01 /* read command */
  16. #define HPIC_WRITE 0x02 /* write command */
  17. #define HPIC_AINC 0x04 /* address increment */
  18. #define HPIC_BMODE 0x00 /* byte mode */
  19. #define HPIC_WMODE 0x10 /* word mode */
  20. #define HPIC_LMODE 0x20 /* long mode */
  21. #define HPIC_ENDIAN 0x00 /* little endian */
  22. #define HPIC_CLEAR 0x80 /* currently not used */
  23. #define CHIP_ADDR 0x58
  24. static DEFINE_MUTEX(fci_i2c_lock);
  25. #define I2C_M_FCIRD 1
  26. #define I2C_M_FCIWR 0
  27. #define I2C_MAX_SEND_LENGTH 300
  28. struct i2c_ts_driver{
  29. struct i2c_client *client;
  30. struct input_dev *input_dev;
  31. struct work_struct work;
  32. };
  33. struct i2c_client *fc8150_i2c;
  34. struct i2c_driver fc8150_i2c_driver;
  35. int fc8150_tsif_setting(HANDLE hDevice)//shubham
  36. {
  37. PRINTF(NULL, "fc8150_tsif_setting called \n");
  38. bbm_write(hDevice, BBM_TS_CLK_DIV, 0x04); //04 as original
  39. bbm_write(hDevice, BBM_TS_PAUSE, 0x80);
  40. #ifdef MSMCHIP
  41. PRINTF(NULL, "TSIF enable...MSM mode\n");
  42. bbm_write(hDevice, BBM_TS_CTRL, 0x06); //02 as original
  43. bbm_write(hDevice, BBM_TS_SEL, 0x84);
  44. #else
  45. PRINTF(NULL, "TSIF enable...normal mode\n");
  46. bbm_write(hDevice, BBM_TS_CTRL, 0x00);
  47. bbm_write(hDevice, BBM_TS_SEL, 0x83);
  48. #endif
  49. PRINTF(NULL, "fc8150_tsif_setting completed \n");
  50. return BBM_OK;
  51. }
  52. static int fc8150_i2c_probe(struct i2c_client *i2c_client,
  53. const struct i2c_device_id *id)
  54. {
  55. PRINTF(NULL, "fc8150_i2c_probe called \n");
  56. fc8150_i2c = i2c_client;
  57. i2c_set_clientdata(i2c_client, NULL);
  58. PRINTF(NULL, "fc8150_i2c_probe OK \n");
  59. return 0;
  60. }
  61. static int fc8150_remove(struct i2c_client *client)
  62. {
  63. return 0;
  64. }
  65. static const struct i2c_device_id fc8150_id[] = {
  66. { "fc8150_i2c", 0 },
  67. { },
  68. };
  69. static struct of_device_id fc8150_match_table[] = {
  70. { .compatible = "isdb,isdb_fc8300",},
  71. { },
  72. };
  73. struct i2c_driver fc8150_i2c_driver = {
  74. .driver = {
  75. .name = "fc8150_i2c",
  76. .owner = THIS_MODULE,
  77. .of_match_table = fc8150_match_table,
  78. },
  79. .probe = fc8150_i2c_probe,
  80. .remove = fc8150_remove,
  81. .id_table = fc8150_id,
  82. };
  83. static int i2c_bulkread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  84. {
  85. int res;
  86. struct i2c_msg rmsg[2];
  87. unsigned char i2c_data[1];
  88. if (fc8150_i2c == NULL) {
  89. PRINTF(0, "[ERROR] FC8150_I2C Handle Fail...........\n");
  90. return BBM_NOK;
  91. }
  92. rmsg[0].addr = chip;
  93. rmsg[0].flags = I2C_M_FCIWR;
  94. rmsg[0].len = 1;
  95. rmsg[0].buf = i2c_data;
  96. i2c_data[0] = addr & 0xff;
  97. rmsg[1].addr = chip;
  98. rmsg[1].flags = I2C_M_FCIRD;
  99. rmsg[1].len = length;
  100. rmsg[1].buf = data;
  101. res = i2c_transfer(fc8150_i2c->adapter, &rmsg[0], 2);
  102. return res;
  103. }
  104. static int i2c_bulkwrite(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  105. {
  106. int res;
  107. struct i2c_msg wmsg;
  108. unsigned char i2c_data[I2C_MAX_SEND_LENGTH];
  109. if (fc8150_i2c == NULL) {
  110. PRINTF(0, "[ERROR] FC8150_I2C Handle Fail...........\n");
  111. return BBM_NOK;
  112. }
  113. if (length+1 > I2C_MAX_SEND_LENGTH) {
  114. PRINTF(0, ".......error");
  115. return -ENODEV;
  116. }
  117. wmsg.addr = chip;
  118. wmsg.flags = I2C_M_FCIWR;
  119. wmsg.len = length + 1;
  120. wmsg.buf = i2c_data;
  121. i2c_data[0] = addr & 0xff;
  122. memcpy(&i2c_data[1], data, length);
  123. res = i2c_transfer(fc8150_i2c->adapter, &wmsg, 1);
  124. return res;
  125. }
  126. static int i2c_dataread(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u32 length)
  127. {
  128. return i2c_bulkread(hDevice, chip, addr, data, length);
  129. }
  130. int fc8150_bypass_read(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  131. {
  132. int res;
  133. u8 bypass_addr = 0x03;
  134. u8 bypass_data = 1;
  135. u8 bypass_len = 1;
  136. mutex_lock(&fci_i2c_lock);
  137. res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr
  138. , &bypass_data, bypass_len);
  139. res |= i2c_bulkread(hDevice, chip, addr, data, length);
  140. mutex_unlock(&fci_i2c_lock);
  141. return res;
  142. }
  143. int fc8150_bypass_write(HANDLE hDevice, u8 chip, u8 addr, u8 *data, u16 length)
  144. {
  145. int res;
  146. u8 bypass_addr = 0x03;
  147. u8 bypass_data = 1;
  148. u8 bypass_len = 1;
  149. mutex_lock(&fci_i2c_lock);
  150. res = i2c_bulkwrite(hDevice, CHIP_ADDR, bypass_addr
  151. , &bypass_data, bypass_len);
  152. res |= i2c_bulkwrite(hDevice, chip, addr, data, length);
  153. mutex_unlock(&fci_i2c_lock);
  154. return res;
  155. }
  156. int fc8150_i2c_init(HANDLE hDevice, u16 param1, u16 param2)
  157. {
  158. int res;
  159. PRINTF(NULL, "fc8150_i2c_init \n");
  160. fc8150_i2c = kzalloc(sizeof(struct i2c_ts_driver), GFP_KERNEL); //shubham
  161. if (fc8150_i2c == NULL) //shubham
  162. return -ENOMEM;
  163. res = i2c_add_driver(&fc8150_i2c_driver);
  164. fc8150_tsif_setting(hDevice);
  165. PRINTF(NULL, "fc8150_i2c_init exit\n");
  166. return res;
  167. }
  168. int fc8150_i2c_byteread(HANDLE hDevice, u16 addr, u8 *data)
  169. {
  170. int res;
  171. u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  172. mutex_lock(&fci_i2c_lock);
  173. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  174. , (u8 *)&addr, 2);
  175. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  176. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, 1);
  177. mutex_unlock(&fci_i2c_lock);
  178. return res;
  179. }
  180. int fc8150_i2c_wordread(HANDLE hDevice, u16 addr, u16 *data)
  181. {
  182. int res;
  183. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  184. mutex_lock(&fci_i2c_lock);
  185. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  186. , (u8 *)&addr, 2);
  187. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  188. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 2);
  189. mutex_unlock(&fci_i2c_lock);
  190. return res;
  191. }
  192. int fc8150_i2c_longread(HANDLE hDevice, u16 addr, u32 *data)
  193. {
  194. int res;
  195. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  196. mutex_lock(&fci_i2c_lock);
  197. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  198. , (u8 *)&addr, 2);
  199. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  200. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)data, 4);
  201. mutex_unlock(&fci_i2c_lock);
  202. return res;
  203. }
  204. int fc8150_i2c_bulkread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  205. {
  206. int res;
  207. u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  208. mutex_lock(&fci_i2c_lock);
  209. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  210. , (u8 *)&addr, 2);
  211. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  212. res |= i2c_bulkread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  213. mutex_unlock(&fci_i2c_lock);
  214. return res;
  215. }
  216. int fc8150_i2c_bytewrite(HANDLE hDevice, u16 addr, u8 data)
  217. {
  218. int res;
  219. u8 command = HPIC_WRITE | HPIC_BMODE | HPIC_ENDIAN;
  220. mutex_lock(&fci_i2c_lock);
  221. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  222. , (u8 *)&addr, 2);
  223. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  224. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 1);
  225. mutex_unlock(&fci_i2c_lock);
  226. return res;
  227. }
  228. int fc8150_i2c_wordwrite(HANDLE hDevice, u16 addr, u16 data)
  229. {
  230. int res;
  231. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  232. mutex_lock(&fci_i2c_lock);
  233. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  234. , (u8 *)&addr, 2);
  235. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  236. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 2);
  237. mutex_unlock(&fci_i2c_lock);
  238. return res;
  239. }
  240. int fc8150_i2c_longwrite(HANDLE hDevice, u16 addr, u32 data)
  241. {
  242. int res;
  243. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  244. mutex_lock(&fci_i2c_lock);
  245. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  246. , (u8 *)&addr, 2);
  247. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  248. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, (u8 *)&data, 4);
  249. mutex_unlock(&fci_i2c_lock);
  250. return res;
  251. }
  252. int fc8150_i2c_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
  253. {
  254. int res;
  255. u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;
  256. mutex_lock(&fci_i2c_lock);
  257. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  258. , (u8 *)&addr, 2);
  259. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  260. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  261. mutex_unlock(&fci_i2c_lock);
  262. return res;
  263. }
  264. int fc8150_i2c_dataread(HANDLE hDevice, u16 addr, u8 *data, u32 length)
  265. {
  266. int res;
  267. u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;
  268. mutex_lock(&fci_i2c_lock);
  269. res = i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_ADDRESS_REG
  270. , (u8 *)&addr, 2);
  271. res |= i2c_bulkwrite(hDevice, CHIP_ADDR, BBM_COMMAND_REG, &command, 1);
  272. res |= i2c_dataread(hDevice, CHIP_ADDR, BBM_DATA_REG, data, length);
  273. mutex_unlock(&fci_i2c_lock);
  274. return res;
  275. }
  276. int fc8150_i2c_deinit(HANDLE hDevice)
  277. {
  278. bbm_write(hDevice, BBM_TS_SEL, 0x00);
  279. PRINTF(NULL, "fc8150_i2c_deinit \n");
  280. i2c_del_driver(&fc8150_i2c_driver);
  281. return BBM_OK;
  282. }