gyro_mpu6500.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 <linux/kernel.h>
  16. #include "../ssp.h"
  17. /*************************************************************************/
  18. /* factory Sysfs */
  19. /*************************************************************************/
  20. #define VENDOR "INVENSENSE"
  21. #define CHIP_ID "MPU6500"
  22. #define VENDOR_K330 "STM"
  23. #define CHIP_ID_K330 "K330"
  24. #define CALIBRATION_FILE_PATH "/efs/gyro_cal_data"
  25. #define VERBOSE_OUT 1
  26. #define CALIBRATION_DATA_AMOUNT 20
  27. #define DEF_GYRO_FULLSCALE 2000
  28. #define DEF_GYRO_SENS (32768 / DEF_GYRO_FULLSCALE)
  29. #define DEF_BIAS_LSB_THRESH_SELF (20 * DEF_GYRO_SENS)
  30. #define DEF_BIAS_LSB_THRESH_SELF_6500 (30 * DEF_GYRO_SENS)
  31. #define DEF_RMS_LSB_TH_SELF (5 * DEF_GYRO_SENS)
  32. #define DEF_RMS_THRESH ((DEF_RMS_LSB_TH_SELF) * (DEF_RMS_LSB_TH_SELF))
  33. #define DEF_SCALE_FOR_FLOAT (1000)
  34. #define DEF_RMS_SCALE_FOR_RMS (10000)
  35. #define DEF_SQRT_SCALE_FOR_RMS (100)
  36. static ssize_t gyro_vendor_show(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct ssp_data *data = dev_get_drvdata(dev);
  40. if (data->sns_combination == STM_K330_AG)
  41. return sprintf(buf, "%s\n", VENDOR_K330);
  42. else
  43. return sprintf(buf, "%s\n", VENDOR);
  44. }
  45. static ssize_t gyro_name_show(struct device *dev,
  46. struct device_attribute *attr, char *buf)
  47. {
  48. struct ssp_data *data = dev_get_drvdata(dev);
  49. if (data->sns_combination == STM_K330_AG)
  50. return sprintf(buf, "%s\n", CHIP_ID_K330);
  51. else
  52. return sprintf(buf, "%s\n", CHIP_ID);
  53. }
  54. int gyro_open_calibration(struct ssp_data *data)
  55. {
  56. int iRet = 0;
  57. mm_segment_t old_fs;
  58. struct file *cal_filp = NULL;
  59. old_fs = get_fs();
  60. set_fs(KERNEL_DS);
  61. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY, 0666);
  62. if (IS_ERR(cal_filp)) {
  63. set_fs(old_fs);
  64. iRet = PTR_ERR(cal_filp);
  65. data->gyrocal.x = 0;
  66. data->gyrocal.y = 0;
  67. data->gyrocal.z = 0;
  68. return iRet;
  69. }
  70. iRet = cal_filp->f_op->read(cal_filp, (char *)&data->gyrocal,
  71. 3 * sizeof(int), &cal_filp->f_pos);
  72. if (iRet != 3 * sizeof(int))
  73. iRet = -EIO;
  74. filp_close(cal_filp, current->files);
  75. set_fs(old_fs);
  76. ssp_dbg("[SSP]: open gyro calibration %d, %d, %d\n",
  77. data->gyrocal.x, data->gyrocal.y, data->gyrocal.z);
  78. return iRet;
  79. }
  80. static int save_gyro_caldata(struct ssp_data *data, s16 *iCalData)
  81. {
  82. int iRet = 0;
  83. struct file *cal_filp = NULL;
  84. mm_segment_t old_fs;
  85. data->gyrocal.x = iCalData[0] << 2;
  86. data->gyrocal.y = iCalData[1] << 2;
  87. data->gyrocal.z = iCalData[2] << 2;
  88. ssp_dbg("[SSP]: do gyro calibrate %d, %d, %d\n",
  89. data->gyrocal.x, data->gyrocal.y, data->gyrocal.z);
  90. old_fs = get_fs();
  91. set_fs(KERNEL_DS);
  92. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  93. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  94. if (IS_ERR(cal_filp)) {
  95. pr_err("[SSP]: %s - Can't open calibration file\n", __func__);
  96. set_fs(old_fs);
  97. iRet = PTR_ERR(cal_filp);
  98. return -EIO;
  99. }
  100. iRet = cal_filp->f_op->write(cal_filp, (char *)&data->gyrocal,
  101. 3 * sizeof(int), &cal_filp->f_pos);
  102. if (iRet != 3 * sizeof(int)) {
  103. pr_err("[SSP]: %s - Can't write gyro cal to file\n", __func__);
  104. iRet = -EIO;
  105. }
  106. filp_close(cal_filp, current->files);
  107. set_fs(old_fs);
  108. return iRet;
  109. }
  110. int set_gyro_cal(struct ssp_data *data)
  111. {
  112. int iRet = 0;
  113. struct ssp_msg *msg;
  114. s16 gyro_cal[3];
  115. if (!(data->uSensorState & (1 << GYROSCOPE_SENSOR))) {
  116. pr_info("[SSP]: %s - Skip this function!!!"\
  117. ", gyro sensor is not connected(0x%x)\n",
  118. __func__, data->uSensorState);
  119. return iRet;
  120. }
  121. gyro_cal[0] = data->gyrocal.x;
  122. gyro_cal[1] = data->gyrocal.y;
  123. gyro_cal[2] = data->gyrocal.z;
  124. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  125. msg->cmd = MSG2SSP_AP_MCU_SET_GYRO_CAL;
  126. msg->length = 6;
  127. msg->options = AP2HUB_WRITE;
  128. msg->buffer = (char*) kzalloc(6, GFP_KERNEL);
  129. msg->free_buffer = 1;
  130. memcpy(msg->buffer, gyro_cal, 6);
  131. iRet = ssp_spi_async(data, msg);
  132. if (iRet != SUCCESS) {
  133. pr_err("[SSP]: %s - i2c fail %d\n", __func__, iRet);
  134. iRet = ERROR;
  135. }
  136. pr_info("[SSP] Set gyro cal data %d, %d, %d\n", gyro_cal[0], gyro_cal[1], gyro_cal[2]);
  137. return iRet;
  138. }
  139. static ssize_t gyro_power_off(struct device *dev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. ssp_dbg("[SSP]: %s\n", __func__);
  143. return sprintf(buf, "%d\n", 1);
  144. }
  145. static ssize_t gyro_power_on(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. ssp_dbg("[SSP]: %s\n", __func__);
  149. return sprintf(buf, "%d\n", 1);
  150. }
  151. short mpu6500_gyro_get_temp(struct ssp_data *data)
  152. {
  153. char chTempBuf[2] = { 0};
  154. unsigned char reg[2];
  155. short temperature = 0;
  156. int iRet = 0;
  157. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  158. msg->cmd = GYROSCOPE_TEMP_FACTORY;
  159. msg->length = 2;
  160. msg->options = AP2HUB_READ;
  161. msg->buffer = chTempBuf;
  162. msg->free_buffer = 0;
  163. iRet = ssp_spi_sync(data, msg, 3000);
  164. if (iRet != SUCCESS) {
  165. pr_err("[SSP]: %s - Gyro Temp Timeout!!\n", __func__);
  166. goto exit;
  167. }
  168. reg[0] = chTempBuf[1];
  169. reg[1] = chTempBuf[0];
  170. temperature = (short) (((reg[0]) << 8) | reg[1]);
  171. ssp_dbg("[SSP]: %s - %d\n", __func__, temperature);
  172. exit:
  173. return temperature;
  174. }
  175. char k330_gyro_get_temp(struct ssp_data *data)
  176. {
  177. char chTemp = 0;
  178. int iRet = 0;
  179. struct ssp_msg *msg;
  180. if (!(data->uSensorState & (1 << GYROSCOPE_SENSOR)))
  181. goto exit;
  182. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  183. msg->cmd = GYROSCOPE_TEMP_FACTORY;
  184. msg->length = 1;
  185. msg->options = AP2HUB_READ;
  186. msg->buffer = &chTemp;
  187. msg->free_buffer = 0;
  188. iRet = ssp_spi_sync(data, msg, 3000);
  189. if (iRet != SUCCESS) {
  190. pr_err("[SSP]: %s - Gyro Temp Timeout!!\n", __func__);
  191. goto exit;
  192. }
  193. ssp_dbg("[SSP]: %s - %d\n", __func__, chTemp);
  194. exit:
  195. return chTemp;
  196. }
  197. static ssize_t gyro_get_temp(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. short temperature = 0;
  201. struct ssp_data *data = dev_get_drvdata(dev);
  202. if (data->sns_combination == STM_K330_AG)
  203. temperature = (short)k330_gyro_get_temp(data);
  204. else
  205. temperature = mpu6500_gyro_get_temp(data);
  206. return sprintf(buf, "%d\n", temperature);
  207. }
  208. u32 mpu6050_selftest_sqrt(u32 sqsum)
  209. {
  210. u32 sq_rt;
  211. u32 g0, g1, g2, g3, g4;
  212. u32 seed;
  213. u32 next;
  214. u32 step;
  215. g4 = sqsum / 100000000;
  216. g3 = (sqsum - g4 * 100000000) / 1000000;
  217. g2 = (sqsum - g4 * 100000000 - g3 * 1000000) / 10000;
  218. g1 = (sqsum - g4 * 100000000 - g3 * 1000000 - g2 * 10000) / 100;
  219. g0 = (sqsum - g4 * 100000000 - g3 * 1000000 - g2 * 10000 - g1 * 100);
  220. next = g4;
  221. step = 0;
  222. seed = 0;
  223. while (((seed + 1) * (step + 1)) <= next) {
  224. step++;
  225. seed++;
  226. }
  227. sq_rt = seed * 10000;
  228. next = (next - (seed * step)) * 100 + g3;
  229. step = 0;
  230. seed = 2 * seed * 10;
  231. while (((seed + 1) * (step + 1)) <= next) {
  232. step++;
  233. seed++;
  234. }
  235. sq_rt = sq_rt + step * 1000;
  236. next = (next - seed * step) * 100 + g2;
  237. seed = (seed + step) * 10;
  238. step = 0;
  239. while (((seed + 1) * (step + 1)) <= next) {
  240. step++;
  241. seed++;
  242. }
  243. sq_rt = sq_rt + step * 100;
  244. next = (next - seed * step) * 100 + g1;
  245. seed = (seed + step) * 10;
  246. step = 0;
  247. while (((seed + 1) * (step + 1)) <= next) {
  248. step++;
  249. seed++;
  250. }
  251. sq_rt = sq_rt + step * 10;
  252. next = (next - seed * step) * 100 + g0;
  253. seed = (seed + step) * 10;
  254. step = 0;
  255. while (((seed + 1) * (step + 1)) <= next) {
  256. step++;
  257. seed++;
  258. }
  259. sq_rt = sq_rt + step;
  260. return sq_rt;
  261. }
  262. ssize_t k330_gyro_selftest(char *buf, struct ssp_data *data)
  263. {
  264. char chTempBuf[36] = { 0,};
  265. u8 uFifoPass = 2;
  266. u8 uBypassPass = 2;
  267. u8 uCalPass = 0;
  268. u8 dummy[2] = {0,};
  269. s16 iNOST[3] = {0,}, iST[3] = {0,}, iCalData[3] = {0,};
  270. s16 iZeroRateData[3] = {0,}, fifo_data[4] = {0,};
  271. int iRet = 0;
  272. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  273. msg->cmd = GYROSCOPE_FACTORY;
  274. msg->length = 36;
  275. msg->options = AP2HUB_READ;
  276. msg->buffer = chTempBuf;
  277. msg->free_buffer = 0;
  278. iRet = ssp_spi_sync(data, msg, 5000);
  279. if (iRet != SUCCESS) {
  280. pr_err("[SSP]: %s - Gyro Selftest Timeout!!\n", __func__);
  281. goto exit;
  282. }
  283. data->uTimeOutCnt = 0;
  284. iNOST[0] = (s16)((chTempBuf[0] << 8) + chTempBuf[1]);
  285. iNOST[1] = (s16)((chTempBuf[2] << 8) + chTempBuf[3]);
  286. iNOST[2] = (s16)((chTempBuf[4] << 8) + chTempBuf[5]);
  287. iST[0] = (s16)((chTempBuf[6] << 8) + chTempBuf[7]);
  288. iST[1] = (s16)((chTempBuf[8] << 8) + chTempBuf[9]);
  289. iST[2] = (s16)((chTempBuf[10] << 8) + chTempBuf[11]);
  290. iCalData[0] = (s16)((chTempBuf[12] << 8) + chTempBuf[13]);
  291. iCalData[1] =( s16)((chTempBuf[14] << 8) + chTempBuf[15]);
  292. iCalData[2] = (s16)((chTempBuf[16] << 8) + chTempBuf[17]);
  293. iZeroRateData[0] = (s16)((chTempBuf[18] << 8) + chTempBuf[19]);
  294. iZeroRateData[1] = (s16)((chTempBuf[20] << 8) + chTempBuf[21]);
  295. iZeroRateData[2] = (s16)((chTempBuf[22] << 8) + chTempBuf[23]);
  296. fifo_data[0] = chTempBuf[24];
  297. fifo_data[1] = (s16)((chTempBuf[25] << 8) + chTempBuf[26]);
  298. fifo_data[2] = (s16)((chTempBuf[27] << 8) + chTempBuf[28]);
  299. fifo_data[3] = (s16)((chTempBuf[29] << 8) + chTempBuf[30]);
  300. uCalPass = chTempBuf[31];
  301. uFifoPass = chTempBuf[32];
  302. uBypassPass = chTempBuf[33];
  303. dummy[0] = chTempBuf[34];
  304. dummy[1] = chTempBuf[35];
  305. pr_info("[SSP] %s dummy = 0x%X, 0x%X\n", __func__, dummy[0], dummy[1]);
  306. if (uFifoPass && uBypassPass && uCalPass)
  307. save_gyro_caldata(data, iCalData);
  308. ssp_dbg("[SSP]: %s - %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  309. __func__, iNOST[0], iNOST[1], iNOST[2], iST[0], iST[1], iST[2],
  310. iZeroRateData[0], iZeroRateData[1], iZeroRateData[2],
  311. fifo_data[0], fifo_data[1], fifo_data[2], fifo_data[3],
  312. uFifoPass & uBypassPass & uCalPass, uFifoPass, uCalPass);
  313. exit:
  314. return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  315. iNOST[0], iNOST[1], iNOST[2], iST[0], iST[1], iST[2],
  316. iZeroRateData[0], iZeroRateData[1], iZeroRateData[2],
  317. fifo_data[0], fifo_data[1], fifo_data[2], fifo_data[3],
  318. uFifoPass & uBypassPass & uCalPass, uFifoPass, uCalPass);
  319. }
  320. ssize_t mpu6500_gyro_selftest(char *buf, struct ssp_data *data)
  321. {
  322. char chTempBuf[36] = { 0,};
  323. u8 initialized = 0;
  324. s8 hw_result = 0;
  325. int i = 0, j = 0, total_count = 0, ret_val = 0;
  326. long avg[3] = {0,}, rms[3] = {0,};
  327. int gyro_bias[3] = {0,}, gyro_rms[3] = {0,};
  328. s16 shift_ratio[3] = {0,};
  329. s16 iCalData[3] = {0,};
  330. char a_name[3][2] = { "X", "Y", "Z" };
  331. int iRet = 0;
  332. int dps_rms[3] = { 0, };
  333. u32 temp = 0;
  334. int bias_thresh = DEF_BIAS_LSB_THRESH_SELF_6500;
  335. struct ssp_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  336. msg->cmd = GYROSCOPE_FACTORY;
  337. msg->length = 36;
  338. msg->options = AP2HUB_READ;
  339. msg->buffer = chTempBuf;
  340. msg->free_buffer = 0;
  341. iRet = ssp_spi_sync(data, msg, 7000);
  342. if (iRet != SUCCESS) {
  343. pr_err("[SSP]: %s - Gyro Selftest Timeout!!\n", __func__);
  344. ret_val = 1;
  345. goto exit;
  346. }
  347. data->uTimeOutCnt = 0;
  348. pr_err("[SSP]%d %d %d %d %d %d %d %d %d %d %d %d", chTempBuf[0], chTempBuf[1],
  349. chTempBuf[2], chTempBuf[3], chTempBuf[4], chTempBuf[5], chTempBuf[6],
  350. chTempBuf[7], chTempBuf[8], chTempBuf[9], chTempBuf[10], chTempBuf[11]);
  351. initialized = chTempBuf[0];
  352. shift_ratio[0] = (s16)((chTempBuf[2] << 8) +
  353. chTempBuf[1]);
  354. shift_ratio[1] = (s16)((chTempBuf[4] << 8) +
  355. chTempBuf[3]);
  356. shift_ratio[2] = (s16)((chTempBuf[6] << 8) +
  357. chTempBuf[5]);
  358. hw_result = (s8)chTempBuf[7];
  359. total_count = (int)((chTempBuf[11] << 24) +
  360. (chTempBuf[10] << 16) +
  361. (chTempBuf[9] << 8) +
  362. chTempBuf[8]);
  363. avg[0] = (long)((chTempBuf[15] << 24) +
  364. (chTempBuf[14] << 16) +
  365. (chTempBuf[13] << 8) +
  366. chTempBuf[12]);
  367. avg[1] = (long)((chTempBuf[19] << 24) +
  368. (chTempBuf[18] << 16) +
  369. (chTempBuf[17] << 8) +
  370. chTempBuf[16]);
  371. avg[2] = (long)((chTempBuf[23] << 24) +
  372. (chTempBuf[22] << 16) +
  373. (chTempBuf[21] << 8) +
  374. chTempBuf[20]);
  375. rms[0] = (long)((chTempBuf[27] << 24) +
  376. (chTempBuf[26] << 16) +
  377. (chTempBuf[25] << 8) +
  378. chTempBuf[24]);
  379. rms[1] = (long)((chTempBuf[31] << 24) +
  380. (chTempBuf[30] << 16) +
  381. (chTempBuf[29] << 8) +
  382. chTempBuf[28]);
  383. rms[2] = (long)((chTempBuf[35] << 24) +
  384. (chTempBuf[34] << 16) +
  385. (chTempBuf[33] << 8) +
  386. chTempBuf[32]);
  387. pr_info("[SSP] init: %d, total cnt: %d\n", initialized, total_count);
  388. pr_info("[SSP] hw_result: %d, %d, %d, %d\n", hw_result,
  389. shift_ratio[0], shift_ratio[1], shift_ratio[2]);
  390. pr_info("[SSP] avg %+8ld %+8ld %+8ld (LSB)\n", avg[0], avg[1], avg[2]);
  391. pr_info("[SSP] rms %+8ld %+8ld %+8ld (LSB)\n", rms[0], rms[1], rms[2]);
  392. if (hw_result < 0) {
  393. pr_err("[SSP] %s - hw selftest fail(%d), sw selftest skip\n",
  394. __func__, hw_result);
  395. return sprintf(buf, "-1,0,0,0,0,0,0,%d.%d,%d.%d,%d.%d,0,0,0\n",
  396. shift_ratio[0] / 10, shift_ratio[0] % 10,
  397. shift_ratio[1] / 10, shift_ratio[1] % 10,
  398. shift_ratio[2] / 10, shift_ratio[2] % 10);
  399. }
  400. gyro_bias[0] = (avg[0] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS;
  401. gyro_bias[1] = (avg[1] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS;
  402. gyro_bias[2] = (avg[2] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS;
  403. iCalData[0] = (s16)avg[0];
  404. iCalData[1] = (s16)avg[1];
  405. iCalData[2] = (s16)avg[2];
  406. if (VERBOSE_OUT) {
  407. pr_info("[SSP] abs bias : %+8d.%03d %+8d.%03d %+8d.%03d (dps)\n",
  408. (int)abs(gyro_bias[0]) / DEF_SCALE_FOR_FLOAT,
  409. (int)abs(gyro_bias[0]) % DEF_SCALE_FOR_FLOAT,
  410. (int)abs(gyro_bias[1]) / DEF_SCALE_FOR_FLOAT,
  411. (int)abs(gyro_bias[1]) % DEF_SCALE_FOR_FLOAT,
  412. (int)abs(gyro_bias[2]) / DEF_SCALE_FOR_FLOAT,
  413. (int)abs(gyro_bias[2]) % DEF_SCALE_FOR_FLOAT);
  414. }
  415. for (j = 0; j < 3; j++) {
  416. if (unlikely(abs(avg[j]) > bias_thresh)) {
  417. pr_err("[SSP] %s-Gyro bias (%ld) exceeded threshold "
  418. "(threshold = %d LSB)\n", a_name[j],
  419. avg[j], bias_thresh);
  420. ret_val |= 1 << (3 + j);
  421. }
  422. }
  423. /* 3rd, check RMS for dead gyros
  424. If any of the RMS noise value returns zero,
  425. then we might have dead gyro or FIFO/register failure,
  426. the part is sleeping, or the part is not responsive */
  427. if (rms[0] == 0 || rms[1] == 0 || rms[2] == 0)
  428. ret_val |= 1 << 6;
  429. if (VERBOSE_OUT) {
  430. pr_info("[SSP] RMS ^ 2 : %+8ld %+8ld %+8ld\n",
  431. (long)rms[0] / total_count,
  432. (long)rms[1] / total_count, (long)rms[2] / total_count);
  433. }
  434. for (j = 0; j < 3; j++) {
  435. if (unlikely(rms[j] / total_count > DEF_RMS_THRESH)) {
  436. pr_err("[SSP] %s-Gyro rms (%ld) exceeded threshold "
  437. "(threshold = %d LSB)\n", a_name[j],
  438. rms[j] / total_count, DEF_RMS_THRESH);
  439. ret_val |= 1 << (7 + j);
  440. }
  441. }
  442. for (i = 0; i < 3; i++) {
  443. if (rms[i] > 10000) {
  444. temp =
  445. ((u32) (rms[i] / total_count)) *
  446. DEF_RMS_SCALE_FOR_RMS;
  447. } else {
  448. temp =
  449. ((u32) (rms[i] * DEF_RMS_SCALE_FOR_RMS)) /
  450. total_count;
  451. }
  452. if (rms[i] < 0)
  453. temp = 1 << 31;
  454. dps_rms[i] = mpu6050_selftest_sqrt(temp) / DEF_GYRO_SENS;
  455. gyro_rms[i] =
  456. dps_rms[i] * DEF_SCALE_FOR_FLOAT / DEF_SQRT_SCALE_FOR_RMS;
  457. }
  458. pr_info("[SSP] RMS : %+8d.%03d %+8d.%03d %+8d.%03d (dps)\n",
  459. (int)abs(gyro_rms[0]) / DEF_SCALE_FOR_FLOAT,
  460. (int)abs(gyro_rms[0]) % DEF_SCALE_FOR_FLOAT,
  461. (int)abs(gyro_rms[1]) / DEF_SCALE_FOR_FLOAT,
  462. (int)abs(gyro_rms[1]) % DEF_SCALE_FOR_FLOAT,
  463. (int)abs(gyro_rms[2]) / DEF_SCALE_FOR_FLOAT,
  464. (int)abs(gyro_rms[2]) % DEF_SCALE_FOR_FLOAT);
  465. if (likely(!ret_val)) {
  466. save_gyro_caldata(data, iCalData);
  467. } else {
  468. pr_err("[SSP] ret_val != 0, gyrocal is 0 at all axis\n");
  469. data->gyrocal.x = 0;
  470. data->gyrocal.y = 0;
  471. data->gyrocal.z = 0;
  472. }
  473. exit:
  474. ssp_dbg("[SSP]: %s - %d,"
  475. "%d.%03d,%d.%03d,%d.%03d,"
  476. "%d.%03d,%d.%03d,%d.%03d,"
  477. "%d.%d,%d.%d,%d.%d,"
  478. "%d,%d,%d\n",
  479. __func__, ret_val,
  480. (int)abs(gyro_bias[0]/1000),
  481. (int)abs(gyro_bias[0])%1000,
  482. (int)abs(gyro_bias[1]/1000),
  483. (int)abs(gyro_bias[1])%1000,
  484. (int)abs(gyro_bias[2]/1000),
  485. (int)abs(gyro_bias[2])%1000,
  486. gyro_rms[0]/1000,
  487. (int)abs(gyro_rms[0])%1000,
  488. gyro_rms[1]/1000,
  489. (int)abs(gyro_rms[1])%1000,
  490. gyro_rms[2]/1000,
  491. (int)abs(gyro_rms[2])%1000,
  492. shift_ratio[0] / 10, shift_ratio[0] % 10,
  493. shift_ratio[1] / 10, shift_ratio[1] % 10,
  494. shift_ratio[2] / 10, shift_ratio[2] % 10,
  495. (int)(total_count/3),
  496. (int)(total_count/3),
  497. (int)(total_count/3));
  498. return sprintf(buf, "%d,"
  499. "%d.%03d,%d.%03d,%d.%03d,"
  500. "%d.%03d,%d.%03d,%d.%03d,"
  501. "%d.%d,%d.%d,%d.%d,"
  502. "%d,%d,%d\n",
  503. ret_val,
  504. (int)abs(gyro_bias[0]/1000),
  505. (int)abs(gyro_bias[0])%1000,
  506. (int)abs(gyro_bias[1]/1000),
  507. (int)abs(gyro_bias[1])%1000,
  508. (int)abs(gyro_bias[2]/1000),
  509. (int)abs(gyro_bias[2])%1000,
  510. gyro_rms[0]/1000,
  511. (int)abs(gyro_rms[0])%1000,
  512. gyro_rms[1]/1000,
  513. (int)abs(gyro_rms[1])%1000,
  514. gyro_rms[2]/1000,
  515. (int)abs(gyro_rms[2])%1000,
  516. shift_ratio[0] / 10, shift_ratio[0] % 10,
  517. shift_ratio[1] / 10, shift_ratio[1] % 10,
  518. shift_ratio[2] / 10, shift_ratio[2] % 10,
  519. (int)(total_count/3),
  520. (int)(total_count/3),
  521. (int)(total_count/3));
  522. }
  523. static ssize_t gyro_selftest_show(struct device *dev,
  524. struct device_attribute *attr, char *buf)
  525. {
  526. struct ssp_data *data = dev_get_drvdata(dev);
  527. if (data->sns_combination == STM_K330_AG)
  528. return k330_gyro_selftest(buf, data);
  529. else
  530. return mpu6500_gyro_selftest(buf, data);
  531. }
  532. static ssize_t gyro_selftest_dps_store(struct device *dev,
  533. struct device_attribute *attr, const char *buf, size_t count)
  534. {
  535. int iNewDps = 0;
  536. int iRet = 0;
  537. char chTempBuf = 0;
  538. struct ssp_data *data = dev_get_drvdata(dev);
  539. struct ssp_msg *msg;
  540. if (!(data->uSensorState & (1 << GYROSCOPE_SENSOR)))
  541. goto exit;
  542. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  543. msg->cmd = GYROSCOPE_DPS_FACTORY;
  544. msg->length = 1;
  545. msg->options = AP2HUB_READ;
  546. msg->buffer = &chTempBuf;
  547. msg->free_buffer = 0;
  548. sscanf(buf, "%d", &iNewDps);
  549. if (iNewDps == GYROSCOPE_DPS250)
  550. msg->options |= 0 << SSP_GYRO_DPS;
  551. else if (iNewDps == GYROSCOPE_DPS500)
  552. msg->options |= 1 << SSP_GYRO_DPS;
  553. else if (iNewDps == GYROSCOPE_DPS2000)
  554. msg->options |= 2 << SSP_GYRO_DPS;
  555. else {
  556. msg->options |= 1 << SSP_GYRO_DPS;
  557. iNewDps = GYROSCOPE_DPS500;
  558. }
  559. iRet = ssp_spi_sync(data, msg, 3000);
  560. if (iRet != SUCCESS) {
  561. pr_err("[SSP]: %s - Gyro Selftest DPS Timeout!!\n", __func__);
  562. goto exit;
  563. }
  564. if (chTempBuf != SUCCESS) {
  565. pr_err("[SSP]: %s - Gyro Selftest DPS Error!!\n", __func__);
  566. goto exit;
  567. }
  568. data->uGyroDps = (unsigned int)iNewDps;
  569. pr_err("[SSP]: %s - %u dps stored\n", __func__, data->uGyroDps);
  570. exit:
  571. return count;
  572. }
  573. static ssize_t gyro_selftest_dps_show(struct device *dev,
  574. struct device_attribute *attr, char *buf)
  575. {
  576. struct ssp_data *data = dev_get_drvdata(dev);
  577. return sprintf(buf, "%u\n", data->uGyroDps);
  578. }
  579. static DEVICE_ATTR(name, S_IRUGO, gyro_name_show, NULL);
  580. static DEVICE_ATTR(vendor, S_IRUGO, gyro_vendor_show, NULL);
  581. static DEVICE_ATTR(power_off, S_IRUGO, gyro_power_off, NULL);
  582. static DEVICE_ATTR(power_on, S_IRUGO, gyro_power_on, NULL);
  583. static DEVICE_ATTR(temperature, S_IRUGO, gyro_get_temp, NULL);
  584. static DEVICE_ATTR(selftest, S_IRUSR|S_IRGRP, gyro_selftest_show, NULL);
  585. static DEVICE_ATTR(selftest_dps, S_IRUGO | S_IWUSR | S_IWGRP,
  586. gyro_selftest_dps_show, gyro_selftest_dps_store);
  587. static struct device_attribute *gyro_attrs[] = {
  588. &dev_attr_name,
  589. &dev_attr_vendor,
  590. &dev_attr_selftest,
  591. &dev_attr_power_on,
  592. &dev_attr_power_off,
  593. &dev_attr_temperature,
  594. &dev_attr_selftest_dps,
  595. NULL,
  596. };
  597. void initialize_gyro_factorytest(struct ssp_data *data)
  598. {
  599. sensors_register(data->gyro_device, data, gyro_attrs, "gyro_sensor");
  600. }
  601. void remove_gyro_factorytest(struct ssp_data *data)
  602. {
  603. sensors_unregister(data->gyro_device, gyro_attrs);
  604. }