ssp_i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  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. *
  14. */
  15. #include "ssp.h"
  16. #define LIMIT_DELAY_CNT 200
  17. #define RECEIVEBUFFERSIZE 12
  18. #define DEBUG_SHOW_DATA 0
  19. static void clean_msg(struct ssp_msg *msg) {
  20. if (msg->free_buffer)
  21. kfree(msg->buffer);
  22. kfree(msg);
  23. }
  24. static int do_transfer(struct ssp_data *data, struct ssp_msg *msg,
  25. struct completion *done, int timeout) {
  26. int status = 0;
  27. int iDelaycnt = 0;
  28. bool msg_dead = false, ssp_down = false;
  29. bool use_no_irq = msg->length == 0;
  30. msg->dead_hook = &msg_dead;
  31. msg->dead = false;
  32. msg->done = done;
  33. mutex_lock(&data->comm_mutex);
  34. gpio_set_value_cansleep(data->ap_int, 0);
  35. while (gpio_get_value_cansleep(data->mcu_int2)) {
  36. mdelay(3);
  37. if ((ssp_down = data->bSspShutdown) || iDelaycnt++ > 500) {
  38. pr_err("[SSP]: %s exit1 - Time out!!\n", __func__);
  39. gpio_set_value_cansleep(data->ap_int, 1);
  40. status = -1;
  41. goto exit;
  42. }
  43. }
  44. status = spi_write(data->spi, msg, 9) >= 0;
  45. if (status == 0) {
  46. pr_err("[SSP]: %s spi_write fail!!\n", __func__);
  47. gpio_set_value_cansleep(data->ap_int, 1);
  48. status = -1;
  49. goto exit;
  50. }
  51. if (!use_no_irq) {
  52. mutex_lock(&data->pending_mutex);
  53. list_add_tail(&msg->list, &data->pending_list);
  54. mutex_unlock(&data->pending_mutex);
  55. }
  56. iDelaycnt = 0;
  57. gpio_set_value_cansleep(data->ap_int, 1);
  58. while (!gpio_get_value_cansleep(data->mcu_int2)) {
  59. mdelay(3);
  60. if ((ssp_down = data->bSspShutdown) || iDelaycnt++ > 500) {
  61. pr_err("[SSP]: %s exit2 - Time out!!\n", __func__);
  62. status = -2;
  63. goto exit;
  64. }
  65. }
  66. exit:
  67. mutex_unlock(&data->comm_mutex);
  68. if (ssp_down)
  69. pr_err("[SSP] : %s, ssp down", __func__);
  70. if (status == -1) {
  71. data->uTimeOutCnt += ssp_down ? 0 : 1;
  72. clean_msg(msg);
  73. return status;
  74. }
  75. if (status == 1 && done != NULL)
  76. if (wait_for_completion_timeout(done, msecs_to_jiffies(timeout)) == 0)
  77. status = -2;
  78. mutex_lock(&data->pending_mutex);
  79. if (!msg_dead) {
  80. msg->done = NULL;
  81. msg->dead_hook = NULL;
  82. if (status != 1)
  83. msg->dead = true;
  84. if (status == -2)
  85. data->uTimeOutCnt += ssp_down ? 0 : 1;
  86. }
  87. mutex_unlock(&data->pending_mutex);
  88. if (use_no_irq)
  89. clean_msg(msg);
  90. return status;
  91. }
  92. int ssp_spi_async(struct ssp_data *data, struct ssp_msg *msg) {
  93. int status = 0;
  94. status = do_transfer(data, msg, NULL, 0);
  95. return status;
  96. }
  97. int ssp_spi_sync(struct ssp_data *data, struct ssp_msg *msg, int timeout) {
  98. DECLARE_COMPLETION_ONSTACK(done);
  99. int status = 0;
  100. if (msg->length == 0) {
  101. pr_err("[SSP]: %s length must not be 0\n", __func__);
  102. clean_msg(msg);
  103. return status;
  104. }
  105. status = do_transfer(data, msg, &done, timeout);
  106. return status;
  107. }
  108. int select_irq_msg(struct ssp_data *data) {
  109. struct ssp_msg *msg, *n;
  110. bool found = false;
  111. u16 chLength = 0, msg_options = 0;
  112. u8 msg_type = 0;
  113. int iRet = 0;
  114. char* buffer;
  115. char chTempBuf[4] = { -1 };
  116. iRet = spi_read(data->spi, chTempBuf, sizeof(chTempBuf));
  117. if (iRet < 0) {
  118. pr_err("[SSP]: %s spi_read fail!!\n", __func__);
  119. return ERROR;
  120. }
  121. memcpy(&msg_options, &chTempBuf[0], 2);
  122. msg_type = msg_options & SSP_SPI_MASK;
  123. memcpy(&chLength, &chTempBuf[2], 2);
  124. switch (msg_type) {
  125. case AP2HUB_READ:
  126. case AP2HUB_WRITE:
  127. mutex_lock(&data->pending_mutex);
  128. if (!list_empty(&data->pending_list)) {
  129. list_for_each_entry_safe(msg, n, &data->pending_list, list)
  130. {
  131. if (msg->options == msg_options) {
  132. list_del(&msg->list);
  133. found = true;
  134. break;
  135. }
  136. }
  137. if (!found) {
  138. pr_err("[SSP]: %s %d - Not match error\n", __func__, msg_options);
  139. goto exit;
  140. }
  141. if (msg->dead && !msg->free_buffer) {
  142. msg->buffer = (char*) kzalloc(msg->length, GFP_KERNEL);
  143. msg->free_buffer = 1;
  144. } // For dead msg, make a temporary buffer to read.
  145. if (msg_type == AP2HUB_READ)
  146. iRet = spi_read(data->spi, msg->buffer, msg->length);
  147. if (msg_type == AP2HUB_WRITE) {
  148. iRet = spi_write(data->spi, msg->buffer, msg->length);
  149. if (msg_options & AP2HUB_RETURN) {
  150. msg->options = AP2HUB_READ | AP2HUB_RETURN;
  151. msg->length = 1;
  152. list_add_tail(&msg->list, &data->pending_list);
  153. goto exit;
  154. }
  155. }
  156. if (msg->done != NULL && !completion_done(msg->done))
  157. complete(msg->done);
  158. if (msg->dead_hook != NULL)
  159. *(msg->dead_hook) = true;
  160. clean_msg(msg);
  161. } else
  162. pr_err("[SSP]List empty error(%d)\n", msg_type);
  163. exit:
  164. mutex_unlock(&data->pending_mutex);
  165. break;
  166. case HUB2AP_WRITE:
  167. buffer = (char*) kzalloc(chLength, GFP_KERNEL);
  168. if (buffer == NULL) {
  169. pr_err("[SSP] %s, failed to alloc memory for buffer\n", __func__);
  170. iRet = -ENOMEM;
  171. break;
  172. }
  173. iRet = spi_read(data->spi, buffer, chLength);
  174. parse_dataframe(data, buffer, chLength);
  175. kfree(buffer);
  176. break;
  177. default:
  178. pr_err("[SSP]No type error(%d)\n", msg_type);
  179. break;
  180. }
  181. if (iRet < 0) {
  182. pr_err("[SSP]: %s - MSG2SSP_SSD error %d\n", __func__, iRet);
  183. return ERROR;
  184. }
  185. return SUCCESS;
  186. }
  187. void clean_pending_list(struct ssp_data *data) {
  188. struct ssp_msg *msg, *n;
  189. mutex_lock(&data->pending_mutex);
  190. list_for_each_entry_safe(msg, n, &data->pending_list, list)
  191. {
  192. list_del(&msg->list);
  193. if (msg->done != NULL && !completion_done(msg->done))
  194. complete(msg->done);
  195. if (msg->dead_hook != NULL)
  196. *(msg->dead_hook) = true;
  197. clean_msg(msg);
  198. }
  199. mutex_unlock(&data->pending_mutex);
  200. }
  201. int ssp_send_cmd(struct ssp_data *data, char command, int arg)
  202. {
  203. int iRet = 0;
  204. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  205. msg->cmd = command;
  206. msg->length = 0;
  207. msg->options = AP2HUB_WRITE;
  208. msg->data = arg;
  209. msg->free_buffer = 0;
  210. iRet = ssp_spi_async(data, msg);
  211. if (iRet != SUCCESS) {
  212. pr_err("[SSP]: %s - command 0x%x failed %d\n",
  213. __func__, command, iRet);
  214. return ERROR;
  215. }
  216. data->uInstFailCnt = 0;
  217. ssp_dbg("[SSP]: %s - command 0x%x %d\n", __func__, command, arg);
  218. return SUCCESS;
  219. }
  220. int send_instruction(struct ssp_data *data, u8 uInst,
  221. u8 uSensorType, u8 *uSendBuf, u8 uLength)
  222. {
  223. char command;
  224. int iRet = 0;
  225. struct ssp_msg *msg;
  226. if (data->fw_dl_state == FW_DL_STATE_DOWNLOADING) {
  227. pr_err("[SSP] %s - Skip Inst! DL state = %d\n",
  228. __func__, data->fw_dl_state);
  229. return SUCCESS;
  230. } else if ((!(data->uSensorState & (1 << uSensorType)))
  231. && (uInst <= CHANGE_DELAY)) {
  232. pr_err("[SSP]: %s - Bypass Inst Skip! - %u\n",
  233. __func__, uSensorType);
  234. return FAIL;
  235. }
  236. switch (uInst) {
  237. case REMOVE_SENSOR:
  238. command = MSG2SSP_INST_BYPASS_SENSOR_REMOVE;
  239. break;
  240. case ADD_SENSOR:
  241. command = MSG2SSP_INST_BYPASS_SENSOR_ADD;
  242. break;
  243. case CHANGE_DELAY:
  244. command = MSG2SSP_INST_CHANGE_DELAY;
  245. break;
  246. case GO_SLEEP:
  247. command = MSG2SSP_AP_STATUS_SLEEP;
  248. data->uLastAPState = MSG2SSP_AP_STATUS_SLEEP;
  249. break;
  250. case REMOVE_LIBRARY:
  251. command = MSG2SSP_INST_LIBRARY_REMOVE;
  252. break;
  253. case ADD_LIBRARY:
  254. command = MSG2SSP_INST_LIBRARY_ADD;
  255. break;
  256. default:
  257. command = uInst;
  258. break;
  259. }
  260. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  261. if (msg == NULL) {
  262. pr_err("[SSP] %s, failed to alloc memory for ssp_msg\n", __func__);
  263. iRet = -ENOMEM;
  264. return iRet;
  265. }
  266. msg->cmd = command;
  267. msg->length = uLength + 1;
  268. msg->options = AP2HUB_WRITE;
  269. msg->buffer = (char*) kzalloc(uLength + 1, GFP_KERNEL);
  270. msg->free_buffer = 1;
  271. msg->buffer[0] = uSensorType;
  272. memcpy(&msg->buffer[1], uSendBuf, uLength);
  273. ssp_dbg("[SSP]: %s - Inst = 0x%x, Sensor Type = 0x%x, data = %u\n",
  274. __func__, command, uSensorType, msg->buffer[1]);
  275. iRet = ssp_spi_async(data, msg);
  276. if (iRet != SUCCESS) {
  277. pr_err("[SSP]: %s - Instruction CMD Fail %d\n", __func__, iRet);
  278. return ERROR;
  279. }
  280. data->uInstFailCnt = 0;
  281. return iRet;
  282. }
  283. int send_instruction_sync(struct ssp_data *data, u8 uInst,
  284. u8 uSensorType, u8 *uSendBuf, u8 uLength)
  285. {
  286. char command;
  287. int iRet = 0;
  288. char buffer[10] = { 0, };
  289. struct ssp_msg *msg;
  290. if (data->fw_dl_state == FW_DL_STATE_DOWNLOADING) {
  291. pr_err("[SSP] %s - Skip Inst! DL state = %d\n",
  292. __func__, data->fw_dl_state);
  293. return SUCCESS;
  294. } else if ((!(data->uSensorState & (1 << uSensorType)))
  295. && (uInst <= CHANGE_DELAY)) {
  296. pr_err("[SSP]: %s - Bypass Inst Skip! - %u\n",
  297. __func__, uSensorType);
  298. return FAIL;
  299. }
  300. switch (uInst) {
  301. case REMOVE_SENSOR:
  302. command = MSG2SSP_INST_BYPASS_SENSOR_REMOVE;
  303. break;
  304. case ADD_SENSOR:
  305. command = MSG2SSP_INST_BYPASS_SENSOR_ADD;
  306. break;
  307. case CHANGE_DELAY:
  308. command = MSG2SSP_INST_CHANGE_DELAY;
  309. break;
  310. case GO_SLEEP:
  311. command = MSG2SSP_AP_STATUS_SLEEP;
  312. data->uLastAPState = MSG2SSP_AP_STATUS_SLEEP;
  313. break;
  314. case REMOVE_LIBRARY:
  315. command = MSG2SSP_INST_LIBRARY_REMOVE;
  316. break;
  317. case ADD_LIBRARY:
  318. command = MSG2SSP_INST_LIBRARY_ADD;
  319. break;
  320. default:
  321. command = uInst;
  322. break;
  323. }
  324. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  325. msg->cmd = command;
  326. msg->length = uLength + 1;
  327. msg->options = AP2HUB_WRITE | AP2HUB_RETURN;
  328. msg->buffer = buffer;
  329. msg->free_buffer = 0;
  330. msg->buffer[0] = uSensorType;
  331. memcpy(&msg->buffer[1], uSendBuf, uLength);
  332. ssp_dbg("[SSP]: %s - Inst = 0x%x, Sensor Type = %u, data = %u\n",
  333. __func__, command, uSensorType, msg->buffer[0]);
  334. iRet = ssp_spi_sync(data, msg, 1000);
  335. if (iRet != SUCCESS) {
  336. pr_err("[SSP]: %s - Instruction CMD Fail %d\n", __func__, iRet);
  337. return ERROR;
  338. }
  339. data->uInstFailCnt = 0;
  340. return buffer[0];
  341. }
  342. int flush(struct ssp_data *data, u8 uSensorType) {
  343. int iRet = 0;
  344. char buffer = 0;
  345. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  346. msg->cmd = MSG2SSP_AP_MCU_BATCH_FLUSH;
  347. msg->length = 1;
  348. msg->options = AP2HUB_READ;
  349. msg->data = uSensorType;
  350. msg->buffer = &buffer;
  351. msg->free_buffer = 0;
  352. ssp_dbg("[SSP]: %s Sensor Type = 0x%x, data = %u\n", __func__,
  353. uSensorType, buffer);
  354. iRet = ssp_spi_sync(data, msg, 1000);
  355. if (iRet != SUCCESS) {
  356. pr_err("[SSP]: %s - fail %d\n", __func__, iRet);
  357. return ERROR;
  358. }
  359. return buffer ? 0 : -1;
  360. }
  361. int get_batch_count(struct ssp_data *data, u8 uSensorType) {
  362. int iRet = 0;
  363. s32 result = 0;
  364. char buffer[4] = { 0, };
  365. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  366. msg->cmd = MSG2SSP_AP_MCU_BATCH_COUNT;
  367. msg->length = 4;
  368. msg->options = AP2HUB_READ;
  369. msg->data = uSensorType;
  370. msg->buffer = buffer;
  371. msg->free_buffer = 0;
  372. iRet = ssp_spi_sync(data, msg, 1000);
  373. if (iRet != SUCCESS) {
  374. pr_err("[SSP]: %s - fail %d\n", __func__, iRet);
  375. return ERROR;
  376. }
  377. memcpy(&result, buffer, 4);
  378. ssp_dbg("[SSP]: %s Sensor Type = 0x%x, data = %u\n", __func__, uSensorType,
  379. result);
  380. return result;
  381. }
  382. int get_chipid(struct ssp_data *data)
  383. {
  384. int iRet, iReties = 0;
  385. char buffer = 0;
  386. struct ssp_msg *msg;
  387. retries:
  388. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  389. if (msg == NULL) {
  390. pr_err("[SSP] %s, failed to alloc memory for ssp_msg\n", __func__);
  391. iRet = -ENOMEM;
  392. return iRet;
  393. }
  394. msg->cmd = MSG2SSP_AP_WHOAMI;
  395. msg->length = 1;
  396. msg->options = AP2HUB_READ;
  397. msg->buffer = &buffer;
  398. msg->free_buffer = 0;
  399. iRet = ssp_spi_sync(data, msg, 1000);
  400. if (buffer != DEVICE_ID && iReties++ < 2) {
  401. mdelay(5);
  402. pr_err("[SSP] %s - get chip ID retry\n", __func__);
  403. goto retries;
  404. }
  405. if (iRet == SUCCESS)
  406. return buffer;
  407. pr_err("[SSP] %s - get chip ID failed %d\n", __func__, iRet);
  408. return ERROR;
  409. }
  410. int set_sensor_position(struct ssp_data *data)
  411. {
  412. int iRet = 0;
  413. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  414. msg->cmd = MSG2SSP_AP_SENSOR_FORMATION;
  415. msg->length = 3;
  416. msg->options = AP2HUB_WRITE;
  417. msg->buffer = (char*) kzalloc(3, GFP_KERNEL);
  418. msg->free_buffer = 1;
  419. msg->buffer[0] = data->accel_position;
  420. msg->buffer[1] = data->accel_position;
  421. msg->buffer[2] = data->mag_position;
  422. pr_info("[SSP] Sensor Posision A : %u, G : %u, M: %u\n",
  423. data->accel_position, data->accel_position, data->mag_position);
  424. iRet = ssp_spi_async(data, msg);
  425. if (iRet != SUCCESS) {
  426. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  427. iRet = ERROR;
  428. }
  429. return iRet;
  430. }
  431. #if defined(CONFIG_SENSORS_SSP_BOUNCE_FIRMWARE)
  432. int set_sensor_tilt(struct ssp_data *data)
  433. {
  434. int iRet = 0;
  435. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  436. msg->cmd = MSG2SSP_AP_SENSOR_TILT;
  437. msg->length = 1;
  438. msg->options = AP2HUB_WRITE;
  439. msg->buffer = (char*) kzalloc(1, GFP_KERNEL);
  440. msg->free_buffer = 1;
  441. msg->buffer[0] = data->rot_direction;
  442. iRet = ssp_spi_async(data, msg);
  443. pr_info("[SSP] Sensor Position tilt : %u\n",
  444. data->rot_direction);
  445. if (iRet != SUCCESS) {
  446. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  447. iRet = ERROR;
  448. }
  449. return iRet;
  450. }
  451. #endif
  452. void set_proximity_threshold(struct ssp_data *data,
  453. unsigned int uData1, unsigned int uData2)
  454. {
  455. int iRet = 0;
  456. struct ssp_msg *msg;
  457. if (!(data->uSensorState & (1<<PROXIMITY_SENSOR))) {
  458. pr_info("[SSP]: %s - Skip this function!!!"\
  459. ", proximity sensor is not connected(0x%x)\n",
  460. __func__, data->uSensorState);
  461. return;
  462. }
  463. msg= kzalloc(sizeof(*msg), GFP_KERNEL);
  464. msg->cmd = MSG2SSP_AP_SENSOR_PROXTHRESHOLD;
  465. #if defined (CONFIG_SENSORS_SSP_MAX88920)
  466. msg->length = 2;
  467. msg->options = AP2HUB_WRITE;
  468. msg->buffer = (char*) kzalloc(2, GFP_KERNEL);
  469. msg->free_buffer = 1;
  470. pr_err("[SSP]: %s - SENSOR_PROXTHRESHOL",__func__);
  471. msg->buffer[0] = (unsigned char) uData1;
  472. msg->buffer[1] = (unsigned char) uData2;
  473. #else
  474. msg->length = 4;
  475. msg->options = AP2HUB_WRITE;
  476. msg->buffer = (char*) kzalloc(4, GFP_KERNEL);
  477. msg->free_buffer = 1;
  478. pr_err("[SSP]: %s - SENSOR_PROXTHRESHOL",__func__);
  479. msg->buffer[0] = ((char) (uData1 >> 8) & 0x07);
  480. msg->buffer[1] = (char) uData1;
  481. msg->buffer[2] = ((char) (uData2 >> 8) & 0x07);
  482. msg->buffer[3] = (char) uData2;
  483. #endif
  484. iRet = ssp_spi_async(data, msg);
  485. if (iRet != SUCCESS) {
  486. pr_err("[SSP]: %s - SENSOR_PROXTHRESHOLD CMD fail %d\n",
  487. __func__, iRet);
  488. return;
  489. }
  490. data->uInstFailCnt = 0;
  491. pr_info("[SSP]: Proximity Threshold - %u, %u\n", uData1, uData2);
  492. }
  493. void set_proximity_barcode_enable(struct ssp_data *data, bool bEnable)
  494. {
  495. int iRet = 0;
  496. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  497. msg->cmd = MSG2SSP_AP_SENSOR_BARCODE_EMUL;
  498. msg->length = 1;
  499. msg->options = AP2HUB_WRITE;
  500. msg->buffer = (char*) kzalloc(1, GFP_KERNEL);
  501. msg->free_buffer = 1;
  502. data->bBarcodeEnabled = bEnable;
  503. msg->buffer[0] = bEnable;
  504. iRet = ssp_spi_async(data, msg);
  505. if (iRet != SUCCESS) {
  506. pr_err("[SSP]: %s - SENSOR_BARCODE_EMUL CMD fail %d\n",
  507. __func__, iRet);
  508. return;
  509. }
  510. data->uInstFailCnt = 0;
  511. pr_info("[SSP] Proximity Barcode En : %u\n", bEnable);
  512. }
  513. void set_gesture_current(struct ssp_data *data, unsigned char uData1)
  514. {
  515. int iRet = 0;
  516. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  517. msg->cmd = MSG2SSP_AP_SENSOR_GESTURE_CURRENT;
  518. msg->length = 1;
  519. msg->options = AP2HUB_WRITE;
  520. msg->buffer = (char*) kzalloc(1, GFP_KERNEL);
  521. msg->free_buffer = 1;
  522. msg->buffer[0] = uData1;
  523. iRet = ssp_spi_async(data, msg);
  524. if (iRet != SUCCESS) {
  525. pr_err("[SSP]: %s - SENSOR_GESTURE_CURRENT CMD fail %d\n", __func__,
  526. iRet);
  527. return;
  528. }
  529. data->uInstFailCnt = 0;
  530. pr_info("[SSP]: Gesture Current Setting - %u\n", uData1);
  531. }
  532. unsigned int get_sensor_scanning_info(struct ssp_data *data)
  533. {
  534. int iRet, iReties = 0;
  535. unsigned int iSensorState = 0;
  536. char buffer[4] = { 0, };
  537. struct ssp_msg *msg;
  538. retries:
  539. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  540. if (msg == NULL) {
  541. pr_err("[SSP] %s, failed to alloc memory for ssp_msg\n", __func__);
  542. iRet = -ENOMEM;
  543. return iRet;
  544. }
  545. msg->cmd = MSG2SSP_AP_SENSOR_SCANNING;
  546. msg->length = 4;
  547. msg->options = AP2HUB_READ;
  548. msg->buffer = buffer;
  549. msg->free_buffer = 0;
  550. iRet = ssp_spi_sync(data, msg, 1000);
  551. if (iRet != SUCCESS) {
  552. pr_err("[SSP] %s -fail to get_sensor_scanning_info %d\n",
  553. __func__, iRet);
  554. if(iReties++ < 2)
  555. {
  556. pr_err("[SSP] %s get_sensor_scanning_info fail retry\n",
  557. __func__);
  558. mdelay(5);
  559. goto retries;
  560. }
  561. return 0;
  562. }
  563. else
  564. {
  565. iSensorState = (unsigned int)(buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3] );
  566. // exception for abnormail sensorstatus
  567. if(iReties++ < 2 && (iSensorState < 0x10000 || (iSensorState>>5 & 0x01) != 0x01 || (iSensorState & 0x03) != 0x03))
  568. {
  569. pr_err("[SSP] %s get_sensor_scanning_info val retry %d\n",
  570. __func__, iSensorState);
  571. mdelay(5);
  572. goto retries;
  573. }
  574. }
  575. pr_err("[SSP]: %s - %d %d %d %d\n", __func__, buffer[0] ,buffer[1],buffer[2],buffer[3]);
  576. return iSensorState;
  577. }
  578. unsigned int get_firmware_rev(struct ssp_data *data)
  579. {
  580. unsigned int uRev = 99999;
  581. int iRet;
  582. char buffer[3] = { 0, };
  583. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  584. msg->cmd = MSG2SSP_AP_FIRMWARE_REV;
  585. msg->length = 3;
  586. msg->options = AP2HUB_READ;
  587. msg->buffer = buffer;
  588. msg->free_buffer = 0;
  589. iRet = ssp_spi_sync(data, msg, 1000);
  590. if (iRet != SUCCESS)
  591. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  592. else
  593. uRev = ((unsigned int)buffer[0] << 16)
  594. | ((unsigned int)buffer[1] << 8) | buffer[2];
  595. return uRev;
  596. }
  597. int get_fuserom_data(struct ssp_data *data)
  598. {
  599. int iRet = 0;
  600. char buffer[3] = { 0, };
  601. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  602. msg->cmd = MSG2SSP_AP_FUSEROM;
  603. msg->length = 3;
  604. msg->options = AP2HUB_READ;
  605. msg->buffer = buffer;
  606. msg->free_buffer = 0;
  607. iRet = ssp_spi_sync(data, msg, 1000);
  608. if (iRet) {
  609. data->uFuseRomData[0] = buffer[0];
  610. data->uFuseRomData[1] = buffer[1];
  611. data->uFuseRomData[2] = buffer[2];
  612. } else {
  613. data->uFuseRomData[0] = 0;
  614. data->uFuseRomData[1] = 0;
  615. data->uFuseRomData[2] = 0;
  616. return FAIL;
  617. }
  618. pr_info("[SSP] FUSE ROM Data %d , %d, %d\n", data->uFuseRomData[0],
  619. data->uFuseRomData[1], data->uFuseRomData[2]);
  620. return SUCCESS;
  621. }
  622. int set_big_data_start(struct ssp_data *data, u8 type, u32 length) {
  623. int iRet = 0;
  624. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  625. msg->cmd = MSG2SSP_AP_START_BIG_DATA;
  626. msg->length = 5;
  627. msg->options = AP2HUB_WRITE;
  628. msg->buffer = (char*) kzalloc(5, GFP_KERNEL);
  629. msg->free_buffer = 1;
  630. msg->buffer[0] = type;
  631. memcpy(&msg->buffer[1], &length, 4);
  632. iRet = ssp_spi_async(data, msg);
  633. if (iRet != SUCCESS) {
  634. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  635. iRet = ERROR;
  636. }
  637. return iRet;
  638. }
  639. int sanity_check(struct ssp_data *data) {
  640. int iRet;
  641. char buffer = 0;
  642. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  643. msg->cmd = MSG2SSP_AP_MCU_SANITY_CHECK;
  644. msg->length = 1;
  645. msg->options = AP2HUB_READ;
  646. msg->buffer = &buffer;
  647. msg->free_buffer = 0;
  648. iRet = ssp_spi_sync(data, msg, 1000);
  649. if (iRet != SUCCESS) {
  650. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  651. iRet = ERROR;
  652. }
  653. if (iRet == SUCCESS && buffer != MCU_IS_SANE) {
  654. if (initialize_mcu(data) > 0) {
  655. sync_sensor_state(data);
  656. ssp_sensorhub_report_notice(data, MSG2SSP_AP_STATUS_RESET);
  657. pr_err("[SSP]: %s %d\n", __func__, iRet);
  658. if( data->uLastAPState!=0 )
  659. ssp_send_cmd(data, data->uLastAPState, 0);
  660. if( data->uLastResumeState != 0)
  661. ssp_send_cmd(data, data->uLastResumeState, 0);
  662. }
  663. return 1;
  664. } else
  665. return 0;
  666. }
  667. int set_time(struct ssp_data *data) {
  668. int iRet;
  669. struct ssp_msg *msg;
  670. struct timespec ts;
  671. struct rtc_time tm;
  672. getnstimeofday(&ts);
  673. rtc_time_to_tm(ts.tv_sec, &tm);
  674. pr_info("[SSP]: %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n", __func__,
  675. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
  676. tm.tm_sec, ts.tv_nsec);
  677. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  678. msg->cmd = MSG2SSP_AP_MCU_SET_TIME;
  679. msg->length = 12;
  680. msg->options = AP2HUB_WRITE;
  681. msg->buffer = (char*) kzalloc(12, GFP_KERNEL);
  682. msg->free_buffer = 1;
  683. msg->buffer[0] = tm.tm_hour;
  684. msg->buffer[1] = tm.tm_min;
  685. msg->buffer[2] = tm.tm_sec;
  686. msg->buffer[3] = tm.tm_hour > 11 ? 64 : 0;
  687. msg->buffer[4] = tm.tm_wday;
  688. msg->buffer[5] = tm.tm_mon + 1;
  689. msg->buffer[6] = tm.tm_mday;
  690. msg->buffer[7] = tm.tm_year % 100;
  691. memcpy(&msg->buffer[8], &ts.tv_nsec, 4);
  692. iRet = ssp_spi_async(data, msg);
  693. if (iRet != SUCCESS) {
  694. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  695. iRet = ERROR;
  696. }
  697. return iRet;
  698. }
  699. int get_time(struct ssp_data *data) {
  700. int iRet;
  701. char buffer[12] = { 0, };
  702. struct ssp_msg *msg;
  703. struct timespec ts;
  704. struct rtc_time tm;
  705. getnstimeofday(&ts);
  706. rtc_time_to_tm(ts.tv_sec, &tm);
  707. pr_info("[SSP]: %s ap %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n", __func__,
  708. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
  709. tm.tm_sec, ts.tv_nsec);
  710. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  711. msg->cmd = MSG2SSP_AP_MCU_GET_TIME;
  712. msg->length = 12;
  713. msg->options = AP2HUB_READ;
  714. msg->buffer = buffer;
  715. msg->free_buffer = 0;
  716. iRet = ssp_spi_sync(data, msg, 1000);
  717. if (iRet != SUCCESS) {
  718. pr_err("[SSP]: %s - i2c failed %d\n", __func__, iRet);
  719. return 0;
  720. }
  721. tm.tm_hour = buffer[0];
  722. tm.tm_min = buffer[1];
  723. tm.tm_sec = buffer[2];
  724. tm.tm_mon = msg->buffer[5] - 1;
  725. tm.tm_mday = buffer[6];
  726. tm.tm_year = buffer[7] + 100;
  727. rtc_tm_to_time(&tm, &ts.tv_sec);
  728. memcpy(&ts.tv_nsec, &msg->buffer[8], 4);
  729. rtc_time_to_tm(ts.tv_sec, &tm);
  730. pr_info("[SSP]: %s mcu %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n", __func__,
  731. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
  732. tm.tm_sec, ts.tv_nsec);
  733. return iRet;
  734. }