mpu6500_selftest.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. $
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/input.h>
  20. #include <linux/input-polldev.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/irq.h>
  28. #include <linux/gpio.h>
  29. #ifdef CONFIG_HAS_EARLYSUSPEND
  30. #include <linux/earlysuspend.h>
  31. #endif
  32. #include "./mpu6500_input.h"
  33. #include "./mpu6500_selftest.h"
  34. #define VERBOSE_OUT 1
  35. #define X (0)
  36. #define Y (1)
  37. #define Z (2)
  38. /*--- Test parameters defaults. See set_test_parameters for more details ---*/
  39. #define DEF_MPU_ADDR (0x68) /* I2C address of the mpu */
  40. #define DEF_GYRO_FULLSCALE (2000) /* gyro full scale dps */
  41. #define DEF_GYRO_SENS (32768 / DEF_GYRO_FULLSCALE)
  42. /* gyro sensitivity LSB/dps */
  43. #define DEF_PACKET_THRESH (75) /* 75 ms / (1ms / sample) OR
  44. 600 ms / (8ms / sample) */
  45. #define DEF_TOTAL_TIMING_TOL (3) /* 3% = 2 pkts + 1% proc tol. */
  46. #define DEF_BIAS_THRESH_SELF (20) /* dps */
  47. #define DEF_BIAS_THRESH_CAL (20)
  48. #define DEF_BIAS_LSB_THRESH_SELF (DEF_BIAS_THRESH_SELF*DEF_GYRO_SENS)
  49. #define DEF_BIAS_LSB_THRESH_CAL (DEF_BIAS_THRESH_CAL*DEF_GYRO_SENS)
  50. /* 0.4 dps-rms in LSB-rms */
  51. #define DEF_RMS_THRESH_SELF (5) /* dps : spec is 0.4dps_rms */
  52. #define DEF_RMS_LSB_THRESH_SELF (DEF_RMS_THRESH_SELF*DEF_GYRO_SENS)
  53. #define DEF_TESTS_PER_AXIS (1) /* num of periods used to test
  54. each axis */
  55. #define DEF_N_ACCEL_SAMPLES (20) /* num of accel samples to
  56. average from, if applic. */
  57. #define ML_INIT_CAL_LEN (36) /* length in bytes of
  58. calibration data file */
  59. #define DEF_PERIOD_SELF (75) /* ms of time, self test */
  60. #define DEF_PERIOD_CAL (600) /* ms of time, full cal */
  61. #define DEF_SCALE_FOR_FLOAT (1000)
  62. #define DEF_RMS_SCALE_FOR_RMS (10000)
  63. #define DEF_SQRT_SCALE_FOR_RMS (100)
  64. #define FIFO_PACKET_SIZE 6
  65. #define GYRO_PACKET_THRESH 50
  66. #define GYRO_MAX_PACKET_THRESH 75
  67. #define BIT_GYRO_FIFO_EN (BIT_XG_FIFO_EN|BIT_YG_FIFO_EN|BIT_ZG_FIFO_EN)
  68. #define BIT_FIFO_DIS 0x0
  69. #define BIT_XG_FIFO_EN 0x40
  70. #define BIT_YG_FIFO_EN 0x20
  71. #define BIT_ZG_FIFO_EN 0x10
  72. #define GYRO_WAIT_TIME GYRO_PACKET_THRESH
  73. #define GYRO_THRESH 3
  74. #define THREE_AXIS 3
  75. /* HW self test */
  76. #define BYTES_PER_SENSOR 6
  77. #define DEF_ST_STABLE_TIME 200
  78. #define THREE_AXIS 3
  79. #define DEF_GYRO_WAIT_TIME 51
  80. #define DEF_ST_PRECISION 1000
  81. #define BIT_ACCEL_OUT 0x08
  82. #define BITS_GYRO_OUT 0x70
  83. #define FIFO_COUNT_BYTE 2
  84. #define BITS_SELF_TEST_EN 0xE0
  85. #define INIT_ST_SAMPLES 50
  86. #define INIT_ST_THRESHOLD 14
  87. #define DEF_ST_TRY_TIMES 2
  88. #define REG_ST_GCT_X 0xD
  89. #define DEF_GYRO_CT_SHIFT_DELTA 500
  90. /* gyroscope Coriolis self test min and max bias shift (dps) */
  91. #define DEF_GYRO_CT_SHIFT_MIN 10
  92. #define DEF_GYRO_CT_SHIFT_MAX 105
  93. #define DEF_ST_OTP0_THRESH 60
  94. #define DEF_ST_ABS_THRESH 20
  95. #define DEF_ST_TOR 2
  96. #define DEF_GYRO_SCALE 131
  97. #define DEF_GYRO_SELFTEST_DPS 250
  98. #define DEF_SELFTEST_GYRO_SENS (32768 / 250)
  99. static const int gyro_6500_st_tb[256] = {
  100. 2621, 2648, 2674, 2701, 2728, 2755, 2783, 2811,
  101. 2839, 2867, 2896, 2925, 2954, 2983, 3013, 3043,
  102. 3074, 3105, 3136, 3167, 3199, 3231, 3263, 3296,
  103. 3329, 3362, 3395, 3429, 3464, 3498, 3533, 3569,
  104. 3604, 3640, 3677, 3714, 3751, 3788, 3826, 3864,
  105. 3903, 3942, 3981, 4021, 4061, 4102, 4143, 4185,
  106. 4226, 4269, 4311, 4354, 4398, 4442, 4486, 4531,
  107. 4577, 4622, 4669, 4715, 4762, 4810, 4858, 4907,
  108. 4956, 5005, 5055, 5106, 5157, 5209, 5261, 5313,
  109. 5366, 5420, 5474, 5529, 5584, 5640, 5696, 5753,
  110. 5811, 5869, 5928, 5987, 6047, 6107, 6168, 6230,
  111. 6292, 6355, 6419, 6483, 6548, 6613, 6680, 6746,
  112. 6814, 6882, 6951, 7020, 7091, 7161, 7233, 7305,
  113. 7378, 7452, 7527, 7602, 7678, 7755, 7832, 7911,
  114. 7990, 8070, 8150, 8232, 8314, 8397, 8481, 8566,
  115. 8652, 8738, 8826, 8914, 9003, 9093, 9184, 9276,
  116. 9369, 9462, 9557, 9653, 9749, 9847, 9945, 10044,
  117. 10145, 10246, 10349, 10452, 10557, 10662, 10769, 10877,
  118. 10985, 11095, 11206, 11318, 11432, 11546, 11661, 11778,
  119. 11896, 12015, 12135, 12256, 12379, 12502, 12627, 12754,
  120. 12881, 13010, 13140, 13272, 13404, 13538, 13674, 13810,
  121. 13949, 14088, 14229, 14371, 14515, 14660, 14807, 14955,
  122. 15104, 15255, 15408, 15562, 15718, 15875, 16034, 16194,
  123. 16356, 16519, 16685, 16851, 17020, 17190, 17362, 17536,
  124. 17711, 17888, 18067, 18248, 18430, 18614, 18801, 18989,
  125. 19179, 19370, 19564, 19760, 19957, 20157, 20358, 20562,
  126. 20768, 20975, 21185, 21397, 21611, 21827, 22045, 22266,
  127. 22488, 22713, 22940, 23170, 23401, 23635, 23872, 24111,
  128. 24352, 24595, 24841, 25089, 25340, 25594, 25850, 26108,
  129. 26369, 26633, 26899, 27168, 27440, 27714, 27992, 28271,
  130. 28554, 28840, 29128, 29419, 29714, 30011, 30311, 30614,
  131. 30920, 31229, 31542, 31857, 32176, 32497, 32822, 33151,
  132. };
  133. /*
  134. Types
  135. */
  136. struct mpu6500_selftest_info {
  137. int gyro_sens;
  138. int gyro_fs;
  139. int packet_thresh;
  140. int total_timing_tol;
  141. int bias_thresh;
  142. int rms_thresh;
  143. unsigned int tests_per_axis;
  144. unsigned short accel_samples;
  145. };
  146. struct mpu6500_selftest {
  147. unsigned char pwm_mgmt[2];
  148. unsigned char smplrt_div;
  149. unsigned char user_ctrl;
  150. unsigned char config;
  151. unsigned char gyro_config;
  152. unsigned char int_enable;
  153. };
  154. /*
  155. Global variables
  156. */
  157. static struct mpu6500_selftest mpu6500_selftest;
  158. short mpu6500_big8_to_int16(const unsigned char *big8)
  159. {
  160. short x;
  161. x = ((short)big8[0] << 8) | ((short)big8[1]);
  162. return x;
  163. }
  164. static int mpu6500_backup_register(struct i2c_client *client)
  165. {
  166. int result = 0;
  167. result =
  168. mpu6500_i2c_read_reg(client, MPUREG_PWR_MGMT_1,
  169. 2, mpu6500_selftest.pwm_mgmt);
  170. if (result)
  171. return result;
  172. result =
  173. mpu6500_i2c_read_reg(client, MPUREG_CONFIG,
  174. 1, &mpu6500_selftest.config);
  175. if (result)
  176. return result;
  177. result =
  178. mpu6500_i2c_read_reg(client, MPUREG_GYRO_CONFIG,
  179. 1, &mpu6500_selftest.gyro_config);
  180. if (result)
  181. return result;
  182. result =
  183. mpu6500_i2c_read_reg(client, MPUREG_USER_CTRL,
  184. 1, &mpu6500_selftest.user_ctrl);
  185. if (result)
  186. return result;
  187. result =
  188. mpu6500_i2c_read_reg(client, MPUREG_INT_ENABLE,
  189. 1, &mpu6500_selftest.int_enable);
  190. if (result)
  191. return result;
  192. result =
  193. mpu6500_i2c_read_reg(client, MPUREG_SMPLRT_DIV,
  194. 1, &mpu6500_selftest.smplrt_div);
  195. if (result)
  196. return result;
  197. return result;
  198. }
  199. static int mpu6500_recover_register(struct i2c_client *client)
  200. {
  201. int result = 0;
  202. result =
  203. mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_1,
  204. mpu6500_selftest.pwm_mgmt[0]);
  205. if (result)
  206. return result;
  207. result =
  208. mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_2,
  209. mpu6500_selftest.pwm_mgmt[1]);
  210. if (result)
  211. return result;
  212. result =
  213. mpu6500_i2c_write_single_reg(client, MPUREG_CONFIG,
  214. mpu6500_selftest.config);
  215. if (result)
  216. return result;
  217. result =
  218. mpu6500_i2c_write_single_reg(client, MPUREG_GYRO_CONFIG,
  219. mpu6500_selftest.gyro_config);
  220. if (result)
  221. return result;
  222. result =
  223. mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL,
  224. mpu6500_selftest.user_ctrl);
  225. if (result)
  226. return result;
  227. result =
  228. mpu6500_i2c_write_single_reg(client, MPUREG_SMPLRT_DIV,
  229. mpu6500_selftest.smplrt_div);
  230. if (result)
  231. return result;
  232. result =
  233. mpu6500_i2c_write_single_reg(client, MPUREG_INT_ENABLE,
  234. mpu6500_selftest.int_enable);
  235. if (result)
  236. return result;
  237. return result;
  238. }
  239. u32 mpu6500_selftest_sqrt(u32 sqsum)
  240. {
  241. u32 sq_rt;
  242. int g0, g1, g2, g3, g4;
  243. int seed;
  244. int next;
  245. int step;
  246. g4 = sqsum / 100000000;
  247. g3 = (sqsum - g4 * 100000000) / 1000000;
  248. g2 = (sqsum - g4 * 100000000 - g3 * 1000000) / 10000;
  249. g1 = (sqsum - g4 * 100000000 - g3 * 1000000 - g2 * 10000) / 100;
  250. g0 = (sqsum - g4 * 100000000 - g3 * 1000000 - g2 * 10000 - g1 * 100);
  251. next = g4;
  252. step = 0;
  253. seed = 0;
  254. while (((seed + 1) * (step + 1)) <= next) {
  255. step++;
  256. seed++;
  257. }
  258. sq_rt = seed * 10000;
  259. next = (next - (seed * step)) * 100 + g3;
  260. step = 0;
  261. seed = 2 * seed * 10;
  262. while (((seed + 1) * (step + 1)) <= next) {
  263. step++;
  264. seed++;
  265. }
  266. sq_rt = sq_rt + step * 1000;
  267. next = (next - seed * step) * 100 + g2;
  268. seed = (seed + step) * 10;
  269. step = 0;
  270. while (((seed + 1) * (step + 1)) <= next) {
  271. step++;
  272. seed++;
  273. }
  274. sq_rt = sq_rt + step * 100;
  275. next = (next - seed * step) * 100 + g1;
  276. seed = (seed + step) * 10;
  277. step = 0;
  278. while (((seed + 1) * (step + 1)) <= next) {
  279. step++;
  280. seed++;
  281. }
  282. sq_rt = sq_rt + step * 10;
  283. next = (next - seed * step) * 100 + g0;
  284. seed = (seed + step) * 10;
  285. step = 0;
  286. while (((seed + 1) * (step + 1)) <= next) {
  287. step++;
  288. seed++;
  289. }
  290. sq_rt = sq_rt + step;
  291. return sq_rt;
  292. }
  293. short mpu_big8_to_int16(unsigned char *big8)
  294. {
  295. short x;
  296. x = ((short)big8[0] << 8) | ((short)big8[1]);
  297. return x;
  298. }
  299. int mpu6500_selftest_run(struct i2c_client *client,
  300. int packet_cnt[3],
  301. int gyro_bias[3],
  302. int gyro_rms[3],
  303. int gyro_lsb_bias[3])
  304. {
  305. int ret_val = 0;
  306. int result;
  307. int packet_count;
  308. long avg[3]={0};
  309. long rms[3]={0};
  310. int i, j;
  311. unsigned char regs[7] = {0};
  312. unsigned char data[FIFO_PACKET_SIZE * 2]={0}, read_data[2];
  313. int gyro_data[3][GYRO_MAX_PACKET_THRESH]={{0},};
  314. short fifo_cnt;
  315. int gyro_avg_tmp[3]={0};
  316. struct mpu6500_selftest_info test_setup = {
  317. DEF_GYRO_SENS, DEF_GYRO_FULLSCALE, DEF_PACKET_THRESH,
  318. DEF_TOTAL_TIMING_TOL, (int)DEF_BIAS_THRESH_SELF,
  319. DEF_RMS_LSB_THRESH_SELF * DEF_RMS_LSB_THRESH_SELF,
  320. /* now obsolete - has no effect */
  321. DEF_TESTS_PER_AXIS, DEF_N_ACCEL_SAMPLES
  322. };
  323. char a_name[3][2] = { "X", "Y", "Z" };
  324. /*backup registers */
  325. result = mpu6500_backup_register(client);
  326. if (result) {
  327. printk(KERN_ERR "register backup error=%d", result);
  328. return result;
  329. }
  330. if (mpu6500_selftest.pwm_mgmt[0] & 0x40) {
  331. result = mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_1, 0x00);
  332. if (result) {
  333. printk(KERN_INFO "init PWR_MGMT error=%d", result);
  334. return result;
  335. }
  336. }
  337. regs[0] = mpu6500_selftest.pwm_mgmt[1] & ~(BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
  338. result = mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_2, regs[0]);
  339. result = mpu6500_i2c_write_single_reg(client, MPUREG_INT_ENABLE, 0);
  340. if (result)
  341. return result;
  342. /* disable the sensor output to FIFO */
  343. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, 0);
  344. if (result)
  345. return result;
  346. /* make sure the DMP is disabled first */
  347. result = mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL, 0x00);
  348. if (result) {
  349. printk(KERN_INFO "DMP disable error=%d", result);
  350. return result;
  351. }
  352. /* clear FIFO */
  353. result = mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL, BIT_FIFO_RST);
  354. if (result)
  355. return result;
  356. /* sample rate *//* = 1ms */
  357. result = mpu6500_i2c_write_single_reg(client, MPUREG_SMPLRT_DIV, 0x00);
  358. if (result)
  359. return result;
  360. test_setup.bias_thresh = DEF_BIAS_LSB_THRESH_SELF;
  361. regs[0] = 0x03; /* filter = 42Hz, analog_sample rate = 1 KHz */
  362. switch (test_setup.gyro_fs) {
  363. case 2000:
  364. regs[0] |= 0x18;
  365. break;
  366. case 1000:
  367. regs[0] |= 0x10;
  368. break;
  369. case 500:
  370. regs[0] |= 0x08;
  371. break;
  372. case 250:
  373. default:
  374. regs[0] |= 0x00;
  375. break;
  376. }
  377. result = mpu6500_i2c_write_single_reg(client, MPUREG_CONFIG, regs[0]);
  378. if (result)
  379. return result;
  380. switch (test_setup.gyro_fs) {
  381. case 2000:
  382. regs[0] = 0x03;
  383. break;
  384. case 1000:
  385. regs[0] = 0x02;
  386. break;
  387. case 500:
  388. regs[0] = 0x01;
  389. break;
  390. case 250:
  391. default:
  392. regs[0] = 0x00;
  393. break;
  394. }
  395. result = mpu6500_i2c_write_single_reg(client, MPUREG_GYRO_CONFIG, regs[0] << 3);
  396. if (result)
  397. return result;
  398. // Wait time
  399. // msleep(GYRO_WAIT_TIME);
  400. mdelay(200);
  401. // Enable FIFO
  402. result = mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL, BIT_FIFO_EN);
  403. if (result)
  404. return result;
  405. // Enable gyro output to FIFO
  406. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, BIT_GYRO_FIFO_EN);
  407. if (result)
  408. return result;
  409. // Wait time
  410. mdelay(GYRO_WAIT_TIME);
  411. // Stop gyro FIFO
  412. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, BIT_FIFO_DIS);
  413. if (result)
  414. return result;
  415. // Read FIFO count
  416. result = mpu6500_i2c_read_reg(client, MPUREG_FIFO_COUNTH, 2, read_data);
  417. if (result)
  418. return result;
  419. fifo_cnt = be16_to_cpup((__be16 *)(&read_data[0]));
  420. packet_count = fifo_cnt != 0 ? (fifo_cnt/FIFO_PACKET_SIZE) : 1;
  421. if(packet_count > GYRO_PACKET_THRESH)
  422. packet_count = GYRO_PACKET_THRESH;
  423. // Check packet count
  424. if((abs(packet_count - GYRO_PACKET_THRESH) > GYRO_THRESH) && (packet_count < GYRO_PACKET_THRESH)) {
  425. printk("\r\n Gyro Packet counter Error: %d \r\n",packet_count);
  426. return (ret_val |= 1);
  427. }
  428. printk("\r\n Gyro Packet counter : %d \r\n",packet_count);
  429. for (i = 0; i < packet_count; i++) {
  430. /* getting FIFO data */
  431. result = mpu6500_i2c_read_reg(client, MPUREG_FIFO_R_W, FIFO_PACKET_SIZE, data);
  432. if (result)
  433. return result;
  434. for (j = 0; j < THREE_AXIS; j++) {
  435. gyro_data[j][i] = (int)mpu_big8_to_int16((&data[2*j]));
  436. gyro_avg_tmp[j] += gyro_data[j][i];
  437. avg[j] = (long)gyro_avg_tmp[j];
  438. avg[j] /= packet_count;
  439. }
  440. }
  441. printk(KERN_INFO "bias : %+8ld %+8ld %+8ld (LSB)\n", avg[X], avg[Y], avg[Z]);
  442. gyro_bias[X] = (int)((avg[X] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS);
  443. gyro_bias[Y] = (int)((avg[Y] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS);
  444. gyro_bias[Z] = (int)((avg[Z] * DEF_SCALE_FOR_FLOAT) / DEF_GYRO_SENS);
  445. gyro_lsb_bias[X] = (int)avg[X];
  446. gyro_lsb_bias[Y] = (int)avg[Y];
  447. gyro_lsb_bias[Z] = (int)avg[Z];
  448. if (VERBOSE_OUT) {
  449. printk(KERN_INFO
  450. "abs bias : %+8d.%03d %+8d.%03d %+8d.%03d (dps)\n",
  451. (int)abs(gyro_bias[X]) / DEF_SCALE_FOR_FLOAT,
  452. (int)abs(gyro_bias[X]) % DEF_SCALE_FOR_FLOAT,
  453. (int)abs(gyro_bias[Y]) / DEF_SCALE_FOR_FLOAT,
  454. (int)abs(gyro_bias[Y]) % DEF_SCALE_FOR_FLOAT,
  455. (int)abs(gyro_bias[Z]) / DEF_SCALE_FOR_FLOAT,
  456. (int)abs(gyro_bias[Z]) % DEF_SCALE_FOR_FLOAT);
  457. }
  458. for (j = 0; j < 3; j++) {
  459. if (abs(avg[j]) > test_setup.bias_thresh) {
  460. printk(KERN_INFO
  461. "%s-Gyro bias (%ld) exceeded threshold "
  462. "(threshold = %d LSB)\n", a_name[j], avg[j],
  463. test_setup.bias_thresh);
  464. ret_val |= 1 << (3 + j);
  465. }
  466. }
  467. /* 3rd, check RMS for dead gyros
  468. If any of the RMS noise value returns zero,
  469. then we might have dead gyro or FIFO/register failure,
  470. the part is sleeping, or the part is not responsive */
  471. for (i = 0, rms[X] = 0, rms[Y] = 0, rms[Z] = 0; i < packet_count; i++) {
  472. rms[X] += (long)(gyro_data[0][i] - avg[X]) * (gyro_data[0][i] - avg[X]);
  473. rms[Y] += (long)(gyro_data[1][i] - avg[Y]) * (gyro_data[1][i] - avg[Y]);
  474. rms[Z] += (long)(gyro_data[2][i] - avg[Z]) * (gyro_data[2][i] - avg[Z]);
  475. }
  476. if (rms[X] == 0 || rms[Y] == 0 || rms[Z] == 0)
  477. ret_val |= 1 << 6;
  478. if (VERBOSE_OUT) {
  479. printk(KERN_INFO "RMS ^ 2 : %+8ld %+8ld %+8ld\n",
  480. (long)rms[X] / packet_count,
  481. (long)rms[Y] / packet_count, (long)rms[Z] / packet_count);
  482. }
  483. {
  484. int dps_rms[3] = { 0 };
  485. u32 tmp;
  486. int i = 0;
  487. for (j = 0; j < 3; j++) {
  488. if (rms[j] / packet_count > test_setup.rms_thresh) {
  489. printk(KERN_INFO
  490. "%s-Gyro rms (%ld) exceeded threshold "
  491. "(threshold = %d LSB)\n", a_name[j],
  492. rms[j] / packet_count,
  493. test_setup.rms_thresh);
  494. ret_val |= 1 << (7 + j);
  495. }
  496. }
  497. for (i = 0; i < 3; i++) {
  498. if (rms[i] > 10000) {
  499. tmp = ((u32) (rms[i] / packet_count)) * DEF_RMS_SCALE_FOR_RMS;
  500. } else {
  501. tmp = ((u32) (rms[i] * DEF_RMS_SCALE_FOR_RMS)) / packet_count;
  502. }
  503. if (rms[i] < 0)
  504. tmp = 1 << 31;
  505. dps_rms[i] = mpu6500_selftest_sqrt(tmp) / DEF_GYRO_SENS;
  506. gyro_rms[i] = dps_rms[i] * DEF_SCALE_FOR_FLOAT / DEF_SQRT_SCALE_FOR_RMS;
  507. }
  508. printk(KERN_INFO
  509. "RMS : %+8d.%03d %+8d.%03d %+8d.%03d (dps)\n",
  510. (int)abs(gyro_rms[X]) / DEF_SCALE_FOR_FLOAT,
  511. (int)abs(gyro_rms[X]) % DEF_SCALE_FOR_FLOAT,
  512. (int)abs(gyro_rms[Y]) / DEF_SCALE_FOR_FLOAT,
  513. (int)abs(gyro_rms[Y]) % DEF_SCALE_FOR_FLOAT,
  514. (int)abs(gyro_rms[Z]) / DEF_SCALE_FOR_FLOAT,
  515. (int)abs(gyro_rms[Z]) % DEF_SCALE_FOR_FLOAT);
  516. }
  517. /*recover registers */
  518. result = mpu6500_recover_register(client);
  519. if (result) {
  520. printk(KERN_ERR "register recovering error=%d", result);
  521. return result;
  522. }
  523. return ret_val;
  524. }
  525. static int mpu6500_do_powerup(struct i2c_client *client)
  526. {
  527. int result = 0;
  528. char reg;
  529. mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_1, 0x1);
  530. mdelay(20);
  531. mpu6500_i2c_read_reg(client, MPUREG_PWR_MGMT_2, 1, &reg);
  532. reg &= ~(BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
  533. mpu6500_i2c_write_single_reg(client, MPUREG_PWR_MGMT_2, reg);
  534. return result;
  535. }
  536. static int mpu6500_do_test(struct i2c_client *client, int self_test_flag, int *gyro_result)
  537. {
  538. int result, i, j, packet_size;
  539. u8 data[BYTES_PER_SENSOR * 2], d;
  540. int fifo_count, packet_count, ind, s;
  541. packet_size = BYTES_PER_SENSOR;
  542. result = mpu6500_i2c_write_single_reg(client, MPUREG_INT_ENABLE, 0);
  543. if (result)
  544. return result;
  545. /* disable the sensor output to FIFO */
  546. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, 0);
  547. if (result)
  548. return result;
  549. /* disable fifo reading */
  550. result = mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL, 0);
  551. if (result)
  552. return result;
  553. /* clear FIFO */
  554. result = mpu6500_i2c_write_single_reg(client, MPUREG_USER_CTRL, BIT_FIFO_RST);
  555. if (result)
  556. return result;
  557. /* setup parameters */
  558. result = mpu6500_i2c_write_single_reg(client, MPUREG_CONFIG, MPU_FILTER_184HZ);
  559. if (result)
  560. return result;
  561. result = mpu6500_i2c_write_single_reg(client, MPUREG_SMPLRT_DIV, 0x0);
  562. if (result)
  563. return result;
  564. result = mpu6500_i2c_write_single_reg(client, MPUREG_GYRO_CONFIG, self_test_flag | (MPU_FS_250DPS << 3));
  565. if (result)
  566. return result;
  567. /* wait for the output to get stable */
  568. mdelay(DEF_ST_STABLE_TIME);
  569. /* enable FIFO reading */
  570. result = mpu6500_i2c_write_single_reg(client,
  571. MPUREG_USER_CTRL, BIT_FIFO_EN);
  572. if (result)
  573. return result;
  574. /* enable sensor output to FIFO */
  575. d = BITS_GYRO_OUT;
  576. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, d);
  577. if (result)
  578. return result;
  579. for (i = 0; i < THREE_AXIS; i++)
  580. gyro_result[i] = 0;
  581. s = 0;
  582. while (s < INIT_ST_SAMPLES) {
  583. mdelay(DEF_GYRO_WAIT_TIME);
  584. /* stop sending data to FIFO */
  585. result = mpu6500_i2c_write_single_reg(client, MPUREG_FIFO_EN, 0);
  586. if (result)
  587. return result;
  588. result = mpu6500_i2c_read_reg(client,
  589. MPUREG_FIFO_COUNTH, FIFO_COUNT_BYTE, data);
  590. if (result)
  591. return result;
  592. fifo_count = be16_to_cpup((__be16 *)(&data[0]));
  593. packet_count = fifo_count / packet_size;
  594. result = mpu6500_i2c_read_reg(client, MPUREG_FIFO_R_W,
  595. packet_size, data);
  596. if (result)
  597. return result;
  598. i = 0;
  599. while ((i < packet_count) && (s < INIT_ST_SAMPLES)) {
  600. result = mpu6500_i2c_read_reg(client, MPUREG_FIFO_R_W,
  601. packet_size, data);
  602. if (result)
  603. return result;
  604. ind = 0;
  605. for (j = 0; j < THREE_AXIS; j++)
  606. gyro_result[j] +=
  607. (short)be16_to_cpup( (__be16 *)(&data[ind + 2 * j]));
  608. s++;
  609. i++;
  610. }
  611. }
  612. for (j = 0; j < THREE_AXIS; j++) {
  613. gyro_result[j] = gyro_result[j]/s;
  614. gyro_result[j] *= DEF_ST_PRECISION;
  615. }
  616. return 0;
  617. }
  618. static int mpu6500_gyro_self_test(struct i2c_client *client,
  619. int *reg_avg, int *st_avg, int *ratio)
  620. {
  621. int result;
  622. int ret_val;
  623. int ct_shift_prod[3], st_shift_cust[3], st_shift_ratio[3], i;
  624. u8 regs[3];
  625. ret_val = 0;
  626. result = mpu6500_i2c_read_reg(client, MPUREG_SELF_TEST_X_GYRO, 3, regs);
  627. if (result)
  628. return result;
  629. for (i = 0; i < 3; i++) {
  630. if (regs[i] != 0)
  631. ct_shift_prod[i] = gyro_6500_st_tb[regs[i] - 1];
  632. else
  633. ct_shift_prod[i] = 0;
  634. }
  635. pr_info("reg_bias : %d, %d, %d \n", reg_avg[0], reg_avg[1], reg_avg[2]);
  636. pr_info("st_avg : %d, %d, %d \n", st_avg[0], st_avg[1], st_avg[2]);
  637. for (i = 0; i < 3; i++) {
  638. st_shift_cust[i] = abs(reg_avg[i] - st_avg[i]);
  639. if (ct_shift_prod[i]) {
  640. st_shift_ratio[i] =
  641. abs((st_shift_cust[i] / ct_shift_prod[i])
  642. - DEF_ST_PRECISION);
  643. ratio[i] = st_shift_ratio[i];
  644. if (st_shift_ratio[i] > DEF_GYRO_CT_SHIFT_DELTA)
  645. ret_val |= 1 << i;
  646. } else {
  647. if (st_shift_cust[i] <
  648. DEF_ST_PRECISION * DEF_GYRO_CT_SHIFT_MIN *
  649. DEF_SELFTEST_GYRO_SENS)
  650. ret_val |= 1 << i;
  651. if (st_shift_cust[i] >
  652. DEF_ST_PRECISION * DEF_GYRO_CT_SHIFT_MAX *
  653. DEF_SELFTEST_GYRO_SENS)
  654. ret_val |= 1 << i;
  655. }
  656. }
  657. pr_info("ct_shift_prod : %d %d %d\n", ct_shift_prod[0],
  658. ct_shift_prod[1], ct_shift_prod[2]);
  659. pr_info("st_shift_cust : %d %d %d\n", st_shift_cust[0],
  660. st_shift_cust[1], st_shift_cust[2]);
  661. pr_info("st_shift_ratio : %d %d %d\n", st_shift_ratio[0],
  662. st_shift_ratio[1], st_shift_ratio[2]);
  663. pr_err("%s, ret_val = %d\n", __func__, ret_val);
  664. return ret_val;
  665. }
  666. int mpu6500_gyro_hw_self_check(struct i2c_client *client, int ratio[3])
  667. {
  668. int result;
  669. int gyro_bias_st[THREE_AXIS], gyro_bias_regular[THREE_AXIS];
  670. int test_times;
  671. int gyro_result;
  672. /*backup registers */
  673. result = mpu6500_backup_register(client);
  674. if (result) {
  675. pr_err("register backup error=%d", result);
  676. goto err_state;
  677. }
  678. /*power-up*/
  679. mpu6500_do_powerup(client);
  680. /*get regular bias*/
  681. test_times = DEF_ST_TRY_TIMES;
  682. while (test_times > 0) {
  683. result = mpu6500_do_test(client, 0, gyro_bias_regular);
  684. if (result == -EAGAIN)
  685. test_times--;
  686. else
  687. test_times = 0;
  688. }
  689. if (result)
  690. goto test_fail;
  691. /*get st bias*/
  692. test_times = DEF_ST_TRY_TIMES;
  693. while (test_times > 0) {
  694. result = mpu6500_do_test(client,
  695. BITS_SELF_TEST_EN, gyro_bias_st);
  696. if (result == -EAGAIN)
  697. test_times--;
  698. else
  699. test_times = 0;
  700. }
  701. if (result)
  702. goto test_fail;
  703. gyro_result = mpu6500_gyro_self_test(client, gyro_bias_regular,
  704. gyro_bias_st, ratio);
  705. result = gyro_result;
  706. err_state:
  707. test_fail:
  708. /*recover registers */
  709. if (mpu6500_recover_register(client)) {
  710. pr_err("register recovering error\n");
  711. goto err_state;
  712. }
  713. pr_err("%s, gyro hw result = %d\n", __func__, result);
  714. return result;
  715. }