mpu6515.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /* MPU6515 motion sensor driver
  2. *
  3. * This software is licensed under the terms of the GNU General Public
  4. * License version 2, as published by the Free Software Foundation, and
  5. * may be copied, distributed, and modified under those terms.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/i2c.h>
  15. #include <linux/slab.h>
  16. #include <linux/irq.h>
  17. #include <linux/miscdevice.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/delay.h>
  20. #include <linux/input.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/kobject.h>
  23. #include <linux/platform_device.h>
  24. #include <cust_gyro.h>
  25. #include "mpu6515.h"
  26. #include "mpu6515g.h"
  27. #include "mpu6515a.h"
  28. #include <linux/kernel.h>
  29. /* #include <mach/mt_boot.h> */
  30. #include <mt-plat/mt_boot_common.h>
  31. #ifdef CONFIG_MTK_LEGACY
  32. #include <mach/mt_gpio.h>
  33. #endif
  34. #include <gyroscope.h>
  35. #define INV_GYRO_AUTO_CALI 1
  36. /* #define POWER_NONE_MACRO MT65XX_POWER_NONE */
  37. /*----------------------------------------------------------------------------*/
  38. #define MPU6515_DEFAULT_FS MPU6515_FS_1000
  39. #define MPU6515_DEFAULT_LSB MPU6515_FS_1000_LSB
  40. /* #define USE_EARLY_SUSPEND */
  41. /*---------------------------------------------------------------------------*/
  42. #define DEBUG 1
  43. /*----------------------------------------------------------------------------*/
  44. #define CONFIG_MPU6515_LOWPASS /*apply low pass filter on output */
  45. /*----------------------------------------------------------------------------*/
  46. #define MPU6515_AXIS_X 0
  47. #define MPU6515_AXIS_Y 1
  48. #define MPU6515_AXIS_Z 2
  49. #define MPU6515_AXES_NUM 3
  50. #define MPU6515_DATA_LEN 6
  51. #define MPU6515_DEV_NAME "MPU6515GY" /* name must different with gsensor mpu6515 */
  52. /*----------------------------------------------------------------------------*/
  53. static const struct i2c_device_id mpu6515_i2c_id[] = { {MPU6515_DEV_NAME, 0}, {} };
  54. #ifdef CONFIG_MTK_LEGACY
  55. static struct i2c_board_info i2c_mpu6515 __initdata = {
  56. I2C_BOARD_INFO(MPU6515_DEV_NAME, (MPU6515_I2C_SLAVE_ADDR >> 1)) };
  57. #endif
  58. int packet_thresh = 75; /* 600 ms / 8ms/sample */
  59. /* Maintain cust info here */
  60. struct gyro_hw gyro_cust;
  61. static struct gyro_hw *hw = &gyro_cust;
  62. /* For driver get cust info */
  63. struct gyro_hw *get_cust_gyro(void)
  64. {
  65. return &gyro_cust;
  66. }
  67. struct platform_device *gyroPltFmDev;
  68. /*----------------------------------------------------------------------------*/
  69. static int mpu6515_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  70. static int mpu6515_i2c_remove(struct i2c_client *client);
  71. static int mpu6515_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
  72. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  73. static int mpu6515_suspend(struct i2c_client *client, pm_message_t msg);
  74. static int mpu6515_resume(struct i2c_client *client);
  75. #endif
  76. static int gyroscope_local_init(struct platform_device *pdev);
  77. static int gyroscope_remove(void);
  78. /*----------------------------------------------------------------------------*/
  79. typedef enum {
  80. GYRO_TRC_FILTER = 0x01,
  81. GYRO_TRC_RAWDATA = 0x02,
  82. GYRO_TRC_IOCTL = 0x04,
  83. GYRO_TRC_CALI = 0X08,
  84. GYRO_TRC_INFO = 0X10,
  85. GYRO_TRC_DATA = 0X20,
  86. } GYRO_TRC;
  87. /*----------------------------------------------------------------------------*/
  88. struct scale_factor {
  89. u8 whole;
  90. u8 fraction;
  91. };
  92. /*----------------------------------------------------------------------------*/
  93. struct data_resolution {
  94. struct scale_factor scalefactor;
  95. int sensitivity;
  96. };
  97. /*----------------------------------------------------------------------------*/
  98. #define C_MAX_FIR_LENGTH (32)
  99. /*----------------------------------------------------------------------------*/
  100. struct data_filter {
  101. s16 raw[C_MAX_FIR_LENGTH][MPU6515_AXES_NUM];
  102. int sum[MPU6515_AXES_NUM];
  103. int num;
  104. int idx;
  105. };
  106. /*----------------------------------------------------------------------------*/
  107. struct mpu6515_i2c_data {
  108. struct i2c_client *client;
  109. struct gyro_hw *hw;
  110. struct hwmsen_convert cvt;
  111. /*misc */
  112. struct data_resolution *reso;
  113. atomic_t trace;
  114. atomic_t suspend;
  115. atomic_t selftest;
  116. atomic_t filter;
  117. s16 cali_sw[MPU6515_AXES_NUM + 1];
  118. /*data */
  119. s8 offset[MPU6515_AXES_NUM + 1]; /*+1: for 4-byte alignment */
  120. s16 data[MPU6515_AXES_NUM + 1];
  121. #if defined(CONFIG_MPU6515_LOWPASS)
  122. atomic_t firlen;
  123. atomic_t fir_en;
  124. struct data_filter fir;
  125. #endif
  126. /*early suspend */
  127. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(USE_EARLY_SUSPEND)
  128. struct early_suspend early_drv;
  129. #endif
  130. #if INV_GYRO_AUTO_CALI == 1
  131. s16 inv_cali_raw[MPU6515_AXES_NUM + 1];
  132. s16 temperature;
  133. struct mutex temperature_mutex; /* for temperature protection */
  134. struct mutex raw_data_mutex; /* for inv_cali_raw[] protection */
  135. #endif
  136. };
  137. /*----------------------------------------------------------------------------*/
  138. #ifdef CONFIG_OF
  139. static const struct of_device_id gyro_of_match[] = {
  140. {.compatible = "mediatek,GYRO"},
  141. {},
  142. };
  143. #endif
  144. static struct i2c_driver mpu6515_i2c_driver = {
  145. .driver = {
  146. .name = MPU6515_DEV_NAME,
  147. #ifdef CONFIG_OF
  148. .of_match_table = gyro_of_match,
  149. #endif
  150. },
  151. .probe = mpu6515_i2c_probe,
  152. .remove = mpu6515_i2c_remove,
  153. .detect = mpu6515_i2c_detect,
  154. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  155. .suspend = mpu6515_suspend,
  156. .resume = mpu6515_resume,
  157. #endif
  158. .id_table = mpu6515_i2c_id,
  159. };
  160. /*----------------------------------------------------------------------------*/
  161. static struct i2c_client *mpu6515_i2c_client;
  162. static struct mpu6515_i2c_data *obj_i2c_data;
  163. static bool sensor_power;
  164. static DEFINE_MUTEX(gyroscope_mutex);
  165. static bool enable_status;
  166. static int gyroscope_init_flag = -1; /* 0<==>OK -1 <==> fail */
  167. static struct gyro_init_info gyroscope_init_info = {
  168. .name = "mpu6515",
  169. .init = gyroscope_local_init,
  170. .uninit = gyroscope_remove,
  171. };
  172. /*----------------------------------------------------------------------------*/
  173. /* extern int MPU6515_gse_power(void); */
  174. /* extern int MPU6515_gse_mode(void); */
  175. /* extern int MPU6515_SCP_SetPowerMode(bool enable, int sensorType); */
  176. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  177. /* extern int MPU6515_i2c_master_send(u8 *buf, u8 len); */
  178. /* extern int MPU6515_i2c_master_recv(u8 *buf, u8 len); */
  179. /* extern int MPU6515_hwmsen_read_block(u8 addr, u8 *buf, u8 len); */
  180. /* extern int MPU6515_hwmsen_read_byte(u8 addr, u8 *buf); */
  181. #endif
  182. static unsigned int power_on;
  183. int MPU6515_gyro_power(void)
  184. {
  185. return power_on;
  186. }
  187. EXPORT_SYMBOL(MPU6515_gyro_power);
  188. int MPU6515_gyro_mode(void)
  189. {
  190. return sensor_power;
  191. }
  192. EXPORT_SYMBOL(MPU6515_gyro_mode);
  193. /*--------------------gyroscopy power control function----------------------------------*/
  194. static void MPU6515_power(struct gyro_hw *hw, unsigned int on)
  195. {
  196. #ifndef CONFIG_FPGA_EARLY_PORTING
  197. #if 0
  198. if (hw->power_id != POWER_NONE_MACRO) { /* have externel LDO */
  199. GYRO_LOG("power %s\n", on ? "on" : "off");
  200. if (power_on == on) { /* power status not change */
  201. GYRO_LOG("ignore power control: %d\n", on);
  202. } else if (on) { /* power on */
  203. if (!hwPowerOn(hw->power_id, hw->power_vol, "MPU6515GY"))
  204. GYRO_ERR("power on fails!!\n");
  205. } else { /* power off */
  206. if (MPU6515_gse_power() == false) {
  207. if (!hwPowerDown(hw->power_id, "MPU6515GY"))
  208. GYRO_ERR("power off fail!!\n");
  209. }
  210. }
  211. }
  212. #endif
  213. power_on = on;
  214. #endif /* #ifndef FPGA_EARLY_PORTING */
  215. }
  216. /*----------------------------------------------------------------------------*/
  217. /*----------------------------------------------------------------------------*/
  218. static int MPU6515_write_rel_calibration(struct mpu6515_i2c_data *obj, int dat[MPU6515_AXES_NUM])
  219. {
  220. obj->cali_sw[MPU6515_AXIS_X] =
  221. obj->cvt.sign[MPU6515_AXIS_X] * dat[obj->cvt.map[MPU6515_AXIS_X]];
  222. obj->cali_sw[MPU6515_AXIS_Y] =
  223. obj->cvt.sign[MPU6515_AXIS_Y] * dat[obj->cvt.map[MPU6515_AXIS_Y]];
  224. obj->cali_sw[MPU6515_AXIS_Z] =
  225. obj->cvt.sign[MPU6515_AXIS_Z] * dat[obj->cvt.map[MPU6515_AXIS_Z]];
  226. #if DEBUG
  227. if (atomic_read(&obj->trace) & GYRO_TRC_CALI) {
  228. GYRO_LOG("test (%5d, %5d, %5d) ->(%5d, %5d, %5d)->(%5d, %5d, %5d))\n",
  229. obj->cvt.sign[MPU6515_AXIS_X], obj->cvt.sign[MPU6515_AXIS_Y],
  230. obj->cvt.sign[MPU6515_AXIS_Z], dat[MPU6515_AXIS_X], dat[MPU6515_AXIS_Y],
  231. dat[MPU6515_AXIS_Z], obj->cvt.map[MPU6515_AXIS_X],
  232. obj->cvt.map[MPU6515_AXIS_Y], obj->cvt.map[MPU6515_AXIS_Z]);
  233. GYRO_LOG("write gyro calibration data (%5d, %5d, %5d)\n",
  234. obj->cali_sw[MPU6515_AXIS_X], obj->cali_sw[MPU6515_AXIS_Y],
  235. obj->cali_sw[MPU6515_AXIS_Z]);
  236. }
  237. #endif
  238. return 0;
  239. }
  240. /*----------------------------------------------------------------------------*/
  241. static int MPU6515_ResetCalibration(struct i2c_client *client)
  242. {
  243. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  244. memset(obj->cali_sw, 0x00, sizeof(obj->cali_sw));
  245. return 0;
  246. }
  247. /*----------------------------------------------------------------------------*/
  248. static int MPU6515_ReadCalibration(struct i2c_client *client, int dat[MPU6515_AXES_NUM])
  249. {
  250. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  251. dat[obj->cvt.map[MPU6515_AXIS_X]] =
  252. obj->cvt.sign[MPU6515_AXIS_X] * obj->cali_sw[MPU6515_AXIS_X];
  253. dat[obj->cvt.map[MPU6515_AXIS_Y]] =
  254. obj->cvt.sign[MPU6515_AXIS_Y] * obj->cali_sw[MPU6515_AXIS_Y];
  255. dat[obj->cvt.map[MPU6515_AXIS_Z]] =
  256. obj->cvt.sign[MPU6515_AXIS_Z] * obj->cali_sw[MPU6515_AXIS_Z];
  257. #if DEBUG
  258. if (atomic_read(&obj->trace) & GYRO_TRC_CALI) {
  259. GYRO_LOG("Read gyro calibration data (%5d, %5d, %5d)\n",
  260. dat[MPU6515_AXIS_X], dat[MPU6515_AXIS_Y], dat[MPU6515_AXIS_Z]);
  261. }
  262. #endif
  263. return 0;
  264. }
  265. /*----------------------------------------------------------------------------*/
  266. /*----------------------------------------------------------------------------*/
  267. static int MPU6515_WriteCalibration(struct i2c_client *client, int dat[MPU6515_AXES_NUM])
  268. {
  269. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  270. int err = 0;
  271. int cali[MPU6515_AXES_NUM];
  272. GYRO_FUN();
  273. if (!obj || !dat) {
  274. GYRO_ERR("null ptr!!\n");
  275. return -EINVAL;
  276. } else {
  277. cali[obj->cvt.map[MPU6515_AXIS_X]] =
  278. obj->cvt.sign[MPU6515_AXIS_X] * obj->cali_sw[MPU6515_AXIS_X];
  279. cali[obj->cvt.map[MPU6515_AXIS_Y]] =
  280. obj->cvt.sign[MPU6515_AXIS_Y] * obj->cali_sw[MPU6515_AXIS_Y];
  281. cali[obj->cvt.map[MPU6515_AXIS_Z]] =
  282. obj->cvt.sign[MPU6515_AXIS_Z] * obj->cali_sw[MPU6515_AXIS_Z];
  283. cali[MPU6515_AXIS_X] += dat[MPU6515_AXIS_X];
  284. cali[MPU6515_AXIS_Y] += dat[MPU6515_AXIS_Y];
  285. cali[MPU6515_AXIS_Z] += dat[MPU6515_AXIS_Z];
  286. #if DEBUG
  287. if (atomic_read(&obj->trace) & GYRO_TRC_CALI) {
  288. GYRO_LOG("write gyro calibration data (%5d, %5d, %5d)-->(%5d, %5d, %5d)\n",
  289. dat[MPU6515_AXIS_X], dat[MPU6515_AXIS_Y], dat[MPU6515_AXIS_Z],
  290. cali[MPU6515_AXIS_X], cali[MPU6515_AXIS_Y], cali[MPU6515_AXIS_Z]);
  291. }
  292. #endif
  293. return MPU6515_write_rel_calibration(obj, cali);
  294. }
  295. return err;
  296. }
  297. /*----------------------------------------------------------------------------*/
  298. /*----------------------------------------------------------------------------*/
  299. static int MPU6515_ReadStart(struct i2c_client *client, bool enable)
  300. {
  301. u8 databuf[2] = { 0 };
  302. int res = 0;
  303. GYRO_FUN();
  304. databuf[0] = MPU6515_REG_FIFO_EN;
  305. if (enable) {
  306. /* enable xyz gyro in FIFO */
  307. databuf[1] =
  308. (MPU6515_FIFO_GYROX_EN | MPU6515_FIFO_GYROY_EN | MPU6515_FIFO_GYROZ_EN);
  309. } else {
  310. /* disable xyz gyro in FIFO */
  311. databuf[1] = 0;
  312. }
  313. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  314. res = MPU6515_i2c_master_send(databuf, 0x2);
  315. #else
  316. res = i2c_master_send(client, databuf, 0x2);
  317. #endif
  318. if (res <= 0) {
  319. GYRO_ERR(" enable xyz gyro in FIFO error,enable: 0x%x!\n", databuf[1]);
  320. return MPU6515_ERR_I2C;
  321. }
  322. GYRO_LOG("MPU6515_ReadStart: enable xyz gyro in FIFO: 0x%x\n", databuf[1]);
  323. return MPU6515_SUCCESS;
  324. }
  325. /* ----------------------------------------------------------------------------// */
  326. static int MPU6515_SetPWR_MGMT_2(struct i2c_client *client, bool enable)
  327. {
  328. u8 databuf[2] = { 0 };
  329. int res = 0;
  330. if (enable)
  331. databuf[1] = 0xC0;
  332. else
  333. databuf[1] = 0xC7;
  334. GYRO_LOG("MPU6515_SetPWR_MGMT_2 : en = %d, reg = %x\n", enable, databuf[1]);
  335. databuf[0] = MPU6515_REG_PWR_CTL2;
  336. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  337. res = MPU6515_i2c_master_send(databuf, 0x2);
  338. #else
  339. res = i2c_master_send(client, databuf, 0x2);
  340. #endif
  341. if (res <= 0) {
  342. GYRO_LOG("set power mode failed : %d!\n", res);
  343. return MPU6515_ERR_I2C;
  344. }
  345. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  346. if (MPU6515_hwmsen_read_byte(MPU6515_REG_PWR_CTL2, databuf))
  347. #else
  348. if (hwmsen_read_byte(client, MPU6515_REG_PWR_CTL2, databuf))
  349. #endif
  350. {
  351. GYRO_ERR("read power ctl 2 register err!\n");
  352. } else {
  353. GYRO_LOG("MPU6515_REG_PWR_CTL = %x\n", databuf[0]);
  354. }
  355. return MPU6515_SUCCESS;
  356. }
  357. /* ----------------------------------------------------------------------------// */
  358. static int MPU6515_SetPowerMode(struct i2c_client *client, bool enable)
  359. {
  360. u8 databuf[2] = { 0 };
  361. int res = 0;
  362. if (enable == sensor_power) {
  363. GYRO_LOG("Sensor power status is newest!\n");
  364. return MPU6515_SUCCESS;
  365. }
  366. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  367. if (MPU6515_hwmsen_read_byte(MPU6515_REG_PWR_CTL, databuf))
  368. #else
  369. if (hwmsen_read_byte(client, MPU6515_REG_PWR_CTL, databuf))
  370. #endif
  371. {
  372. GYRO_ERR("read power ctl register err!\n");
  373. return MPU6515_ERR_I2C;
  374. }
  375. if ((databuf[0] & 0x1f) != 0x1)
  376. GYRO_ERR("MPU6515 PWR_MGMT_1 = %x\n", databuf[0]);
  377. databuf[0] &= ~MPU6515_SLEEP;
  378. if (enable == FALSE) {
  379. if (MPU6515_gse_mode() == false)
  380. databuf[0] |= MPU6515_SLEEP;
  381. } else {
  382. /* do nothing */
  383. }
  384. databuf[1] = databuf[0];
  385. databuf[0] = MPU6515_REG_PWR_CTL;
  386. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  387. res = MPU6515_i2c_master_send(databuf, 0x2);
  388. #else
  389. res = i2c_master_send(client, databuf, 0x2);
  390. #endif
  391. if (res <= 0) {
  392. GYRO_LOG("set power mode failed!\n");
  393. return MPU6515_ERR_I2C;
  394. } else {
  395. GYRO_LOG("set power mode ok %d!\n", enable);
  396. }
  397. sensor_power = enable;
  398. return MPU6515_SUCCESS;
  399. }
  400. /*----------------------------------------------------------------------------*/
  401. static int MPU6515_SetDataFormat(struct i2c_client *client, u8 dataformat)
  402. {
  403. u8 databuf[2] = { 0 };
  404. int res = 0;
  405. GYRO_FUN();
  406. databuf[0] = MPU6515_REG_CFG;
  407. databuf[1] = dataformat;
  408. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  409. res = MPU6515_i2c_master_send(databuf, 0x2);
  410. #else
  411. res = i2c_master_send(client, databuf, 0x2);
  412. #endif
  413. if (res <= 0)
  414. return MPU6515_ERR_I2C;
  415. /* read sample rate after written for test */
  416. udelay(500);
  417. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  418. if (MPU6515_hwmsen_read_byte(MPU6515_REG_CFG, databuf))
  419. #else
  420. if (hwmsen_read_byte(client, MPU6515_REG_CFG, databuf))
  421. #endif
  422. {
  423. GYRO_ERR("read data format register err!\n");
  424. return MPU6515_ERR_I2C;
  425. } else {
  426. GYRO_LOG("read data format: 0x%x\n", databuf[0]);
  427. }
  428. return MPU6515_SUCCESS;
  429. }
  430. static int MPU6515_SetFullScale(struct i2c_client *client, u8 dataformat)
  431. {
  432. u8 databuf[2] = { 0 };
  433. int res = 0;
  434. GYRO_FUN();
  435. databuf[0] = MPU6515_REG_GYRO_CFG;
  436. databuf[1] = dataformat;
  437. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  438. res = MPU6515_i2c_master_send(databuf, 0x2);
  439. #else
  440. res = i2c_master_send(client, databuf, 0x2);
  441. #endif
  442. if (res <= 0)
  443. return MPU6515_ERR_I2C;
  444. /* read sample rate after written for test */
  445. udelay(500);
  446. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  447. if (MPU6515_hwmsen_read_byte(MPU6515_REG_GYRO_CFG, databuf))
  448. #else
  449. if (hwmsen_read_byte(client, MPU6515_REG_GYRO_CFG, databuf))
  450. #endif
  451. {
  452. GYRO_ERR("read data format register err!\n");
  453. return MPU6515_ERR_I2C;
  454. } else {
  455. GYRO_LOG("read data format: 0x%x\n", databuf[0]);
  456. }
  457. return MPU6515_SUCCESS;
  458. }
  459. /* set the sample rate */
  460. static int MPU6515_SetSampleRate(struct i2c_client *client, int sample_rate)
  461. {
  462. u8 databuf[2] = { 0 };
  463. int rate_div = 0;
  464. int res = 0;
  465. GYRO_FUN();
  466. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  467. if (MPU6515_hwmsen_read_byte(MPU6515_REG_CFG, databuf))
  468. #else
  469. if (hwmsen_read_byte(client, MPU6515_REG_CFG, databuf))
  470. #endif
  471. {
  472. GYRO_ERR("read gyro data format register err!\n");
  473. return MPU6515_ERR_I2C;
  474. } else {
  475. GYRO_LOG("read gyro data format register: 0x%x\n", databuf[0]);
  476. }
  477. if ((databuf[0] & 0x07) == 0) /* Analog sample rate is 8KHz */
  478. rate_div = 8 * 1024 / sample_rate - 1;
  479. else /* 1kHz */
  480. rate_div = 1024 / sample_rate - 1;
  481. if (rate_div > 255) { /* rate_div: 0 to 255; */
  482. rate_div = 255;
  483. } else if (rate_div < 0) {
  484. rate_div = 0;
  485. }
  486. databuf[0] = MPU6515_REG_SAMRT_DIV;
  487. databuf[1] = rate_div;
  488. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  489. res = MPU6515_i2c_master_send(databuf, 0x2);
  490. #else
  491. res = i2c_master_send(client, databuf, 0x2);
  492. #endif
  493. if (res <= 0) {
  494. GYRO_ERR("write sample rate register err!\n");
  495. return MPU6515_ERR_I2C;
  496. }
  497. /* read sample div after written for test */
  498. udelay(500);
  499. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  500. if (MPU6515_hwmsen_read_byte(MPU6515_REG_SAMRT_DIV, databuf))
  501. #else
  502. if (hwmsen_read_byte(client, MPU6515_REG_SAMRT_DIV, databuf))
  503. #endif
  504. {
  505. GYRO_ERR("read gyro sample rate register err!\n");
  506. return MPU6515_ERR_I2C;
  507. } else {
  508. GYRO_LOG("read gyro sample rate: 0x%x\n", databuf[0]);
  509. }
  510. return MPU6515_SUCCESS;
  511. }
  512. /*----------------------------------------------------------------------------*/
  513. /*----------------------------------------------------------------------------*/
  514. static int MPU6515_FIFOConfig(struct i2c_client *client, u8 clk)
  515. {
  516. u8 databuf[2] = { 0 };
  517. int res = 0;
  518. GYRO_FUN();
  519. /* use gyro X, Y or Z for clocking */
  520. databuf[0] = MPU6515_REG_PWR_CTL;
  521. databuf[1] = clk;
  522. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  523. res = MPU6515_i2c_master_send(databuf, 0x2);
  524. #else
  525. res = i2c_master_send(client, databuf, 0x2);
  526. #endif
  527. if (res <= 0) {
  528. GYRO_ERR("write Power CTRL register err!\n");
  529. return MPU6515_ERR_I2C;
  530. }
  531. GYRO_LOG("MPU6515 use gyro X for clocking OK!\n");
  532. mdelay(50);
  533. /* enable xyz gyro in FIFO */
  534. databuf[0] = MPU6515_REG_FIFO_EN;
  535. databuf[1] = (MPU6515_FIFO_GYROX_EN | MPU6515_FIFO_GYROY_EN | MPU6515_FIFO_GYROZ_EN);
  536. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  537. res = MPU6515_i2c_master_send(databuf, 0x2);
  538. #else
  539. res = i2c_master_send(client, databuf, 0x2);
  540. #endif
  541. if (res <= 0) {
  542. GYRO_ERR("write Power CTRL register err!\n");
  543. return MPU6515_ERR_I2C;
  544. }
  545. GYRO_LOG("MPU6515 enable xyz gyro in FIFO OK!\n");
  546. /* disable AUX_VDDIO */
  547. databuf[0] = MPU6515_REG_AUX_VDD;
  548. databuf[1] = MPU6515_AUX_VDDIO_DIS;
  549. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  550. res = MPU6515_i2c_master_send(databuf, 0x2);
  551. #else
  552. res = i2c_master_send(client, databuf, 0x2);
  553. #endif
  554. if (res <= 0) {
  555. GYRO_ERR("write AUX_VDD register err!\n");
  556. return MPU6515_ERR_I2C;
  557. }
  558. GYRO_LOG("MPU6515 disable AUX_VDDIO OK!\n");
  559. /* enable FIFO and reset FIFO */
  560. databuf[0] = MPU6515_REG_FIFO_CTL;
  561. databuf[1] = (MPU6515_FIFO_EN | MPU6515_FIFO_RST);
  562. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  563. res = MPU6515_i2c_master_send(databuf, 0x2);
  564. #else
  565. res = i2c_master_send(client, databuf, 0x2);
  566. #endif
  567. if (res <= 0) {
  568. GYRO_ERR("write FIFO CTRL register err!\n");
  569. return MPU6515_ERR_I2C;
  570. }
  571. GYRO_LOG("MPU6515_FIFOConfig OK!\n");
  572. return MPU6515_SUCCESS;
  573. }
  574. /*----------------------------------------------------------------------------*/
  575. static int MPU6515_ReadFifoData(struct i2c_client *client, s16 *data, int *datalen)
  576. {
  577. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  578. u8 buf[MPU6515_DATA_LEN] = { 0 };
  579. s16 tmp1[MPU6515_AXES_NUM] = { 0 };
  580. s16 tmp2[MPU6515_AXES_NUM] = { 0 };
  581. int err = 0;
  582. u8 tmp = 0;
  583. int packet_cnt = 0;
  584. int i;
  585. GYRO_FUN();
  586. if (NULL == client)
  587. return -EINVAL;
  588. /* stop putting data in FIFO */
  589. MPU6515_ReadStart(client, FALSE);
  590. /* read data number of bytes in FIFO */
  591. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  592. err = MPU6515_hwmsen_read_byte(MPU6515_REG_FIFO_CNTH, &tmp);
  593. #else
  594. err = hwmsen_read_byte(client, MPU6515_REG_FIFO_CNTH, &tmp);
  595. #endif
  596. if (err) {
  597. GYRO_ERR("read data high number of bytes error: %d\n", err);
  598. return -1;
  599. }
  600. packet_cnt = tmp << 8;
  601. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  602. err = MPU6515_hwmsen_read_byte(MPU6515_REG_FIFO_CNTL, &tmp);
  603. #else
  604. err = hwmsen_read_byte(client, MPU6515_REG_FIFO_CNTL, &tmp);
  605. #endif
  606. if (err) {
  607. GYRO_ERR("read data low number of bytes error: %d\n", err);
  608. return -1;
  609. }
  610. packet_cnt = (packet_cnt + tmp) / MPU6515_DATA_LEN;
  611. GYRO_LOG("MPU6515 Read Data packet number OK: %d\n", packet_cnt);
  612. *datalen = packet_cnt;
  613. /* Within +-5% range: timing_tolerance * packet_thresh=0.05*75 */
  614. if (packet_cnt && (abs(packet_thresh - packet_cnt) < 4)) {
  615. /* read data in FIFO */
  616. for (i = 0; i < packet_cnt; i++) {
  617. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  618. if (MPU6515_hwmsen_read_block(MPU6515_REG_FIFO_DATA, buf, MPU6515_DATA_LEN))
  619. #else
  620. if (hwmsen_read_block(client, MPU6515_REG_FIFO_DATA, buf, MPU6515_DATA_LEN))
  621. #endif
  622. {
  623. GYRO_ERR("MPU6515 read data from FIFO error: %d\n", err);
  624. return -2;
  625. } else {
  626. GYRO_LOG("MPU6515 read Data of diff address from FIFO OK !\n");
  627. }
  628. tmp1[MPU6515_AXIS_X] =
  629. (s16) ((buf[MPU6515_AXIS_X * 2 + 1]) | (buf[MPU6515_AXIS_X * 2] << 8));
  630. tmp1[MPU6515_AXIS_Y] =
  631. (s16) ((buf[MPU6515_AXIS_Y * 2 + 1]) | (buf[MPU6515_AXIS_Y * 2] << 8));
  632. tmp1[MPU6515_AXIS_Z] =
  633. (s16) ((buf[MPU6515_AXIS_Z * 2 + 1]) | (buf[MPU6515_AXIS_Z * 2] << 8));
  634. /* remap coordinate// */
  635. tmp2[obj->cvt.map[MPU6515_AXIS_X]] =
  636. obj->cvt.sign[MPU6515_AXIS_X] * tmp1[MPU6515_AXIS_X];
  637. tmp2[obj->cvt.map[MPU6515_AXIS_Y]] =
  638. obj->cvt.sign[MPU6515_AXIS_Y] * tmp1[MPU6515_AXIS_Y];
  639. tmp2[obj->cvt.map[MPU6515_AXIS_Z]] =
  640. obj->cvt.sign[MPU6515_AXIS_Z] * tmp1[MPU6515_AXIS_Z];
  641. data[3 * i + MPU6515_AXIS_X] = tmp2[MPU6515_AXIS_X];
  642. data[3 * i + MPU6515_AXIS_Y] = tmp2[MPU6515_AXIS_Y];
  643. data[3 * i + MPU6515_AXIS_Z] = tmp2[MPU6515_AXIS_Z];
  644. GYRO_LOG("gyro FIFO packet[%d]:[%04X %04X %04X] => [%5d %5d %5d]\n", i,
  645. data[3 * i + MPU6515_AXIS_X], data[3 * i + MPU6515_AXIS_Y],
  646. data[3 * i + MPU6515_AXIS_Z], data[3 * i + MPU6515_AXIS_X],
  647. data[3 * i + MPU6515_AXIS_Y], data[3 * i + MPU6515_AXIS_Z]);
  648. }
  649. } else {
  650. GYRO_ERR("MPU6515 Incorrect packet count: %d\n", packet_cnt);
  651. return -3;
  652. }
  653. return 0;
  654. }
  655. /*----------------------------------------------------------------------------*/
  656. static int MPU6515_ReadGyroData(struct i2c_client *client, char *buf, int bufsize)
  657. {
  658. char databuf[6];
  659. int data[3];
  660. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  661. #if INV_GYRO_AUTO_CALI == 1
  662. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  663. if (MPU6515_hwmsen_read_block(MPU6515_REG_TEMPH, databuf, 2))
  664. #else
  665. if (hwmsen_read_block(client, MPU6515_REG_TEMPH, databuf, 2))
  666. #endif
  667. {
  668. GYRO_ERR("MPU6515 read temperature data error\n");
  669. return -2;
  670. } else {
  671. mutex_lock(&obj->temperature_mutex);
  672. obj->temperature = ((s16) ((databuf[1]) | (databuf[0] << 8)));
  673. mutex_unlock(&obj->temperature_mutex);
  674. }
  675. #endif
  676. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  677. if (MPU6515_hwmsen_read_block(MPU6515_REG_GYRO_XH, databuf, 6))
  678. #else
  679. if (hwmsen_read_block(client, MPU6515_REG_GYRO_XH, databuf, 6))
  680. #endif
  681. {
  682. GYRO_ERR("MPU6515 read gyroscope data error\n");
  683. return -2;
  684. } else {
  685. obj->data[MPU6515_AXIS_X] =
  686. ((s16)
  687. ((databuf[MPU6515_AXIS_X * 2 + 1]) | (databuf[MPU6515_AXIS_X * 2] << 8)));
  688. obj->data[MPU6515_AXIS_Y] =
  689. ((s16)
  690. ((databuf[MPU6515_AXIS_Y * 2 + 1]) | (databuf[MPU6515_AXIS_Y * 2] << 8)));
  691. obj->data[MPU6515_AXIS_Z] =
  692. ((s16)
  693. ((databuf[MPU6515_AXIS_Z * 2 + 1]) | (databuf[MPU6515_AXIS_Z * 2] << 8)));
  694. #if DEBUG
  695. if (atomic_read(&obj->trace) & GYRO_TRC_RAWDATA) {
  696. GYRO_LOG("read gyro register: %d, %d, %d, %d, %d, %d",
  697. databuf[0], databuf[1], databuf[2], databuf[3], databuf[4],
  698. databuf[5]);
  699. GYRO_LOG("get gyro raw data (0x%08X, 0x%08X, 0x%08X) -> (%5d, %5d, %5d)\n",
  700. obj->data[MPU6515_AXIS_X], obj->data[MPU6515_AXIS_Y],
  701. obj->data[MPU6515_AXIS_Z], obj->data[MPU6515_AXIS_X],
  702. obj->data[MPU6515_AXIS_Y], obj->data[MPU6515_AXIS_Z]);
  703. }
  704. #endif
  705. #if INV_GYRO_AUTO_CALI == 1
  706. mutex_lock(&obj->raw_data_mutex);
  707. /*remap coordinate */
  708. obj->inv_cali_raw[obj->cvt.map[MPU6515_AXIS_X]] =
  709. obj->cvt.sign[MPU6515_AXIS_X] * obj->data[MPU6515_AXIS_X];
  710. obj->inv_cali_raw[obj->cvt.map[MPU6515_AXIS_Y]] =
  711. obj->cvt.sign[MPU6515_AXIS_Y] * obj->data[MPU6515_AXIS_Y];
  712. obj->inv_cali_raw[obj->cvt.map[MPU6515_AXIS_Z]] =
  713. obj->cvt.sign[MPU6515_AXIS_Z] * obj->data[MPU6515_AXIS_Z];
  714. mutex_unlock(&obj->raw_data_mutex);
  715. #endif
  716. obj->data[MPU6515_AXIS_X] =
  717. obj->data[MPU6515_AXIS_X] + obj->cali_sw[MPU6515_AXIS_X];
  718. obj->data[MPU6515_AXIS_Y] =
  719. obj->data[MPU6515_AXIS_Y] + obj->cali_sw[MPU6515_AXIS_Y];
  720. obj->data[MPU6515_AXIS_Z] =
  721. obj->data[MPU6515_AXIS_Z] + obj->cali_sw[MPU6515_AXIS_Z];
  722. /*remap coordinate */
  723. data[obj->cvt.map[MPU6515_AXIS_X]] =
  724. obj->cvt.sign[MPU6515_AXIS_X] * obj->data[MPU6515_AXIS_X];
  725. data[obj->cvt.map[MPU6515_AXIS_Y]] =
  726. obj->cvt.sign[MPU6515_AXIS_Y] * obj->data[MPU6515_AXIS_Y];
  727. data[obj->cvt.map[MPU6515_AXIS_Z]] =
  728. obj->cvt.sign[MPU6515_AXIS_Z] * obj->data[MPU6515_AXIS_Z];
  729. /* Out put the degree/second(o/s) */
  730. data[MPU6515_AXIS_X] =
  731. data[MPU6515_AXIS_X] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  732. data[MPU6515_AXIS_Y] =
  733. data[MPU6515_AXIS_Y] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  734. data[MPU6515_AXIS_Z] =
  735. data[MPU6515_AXIS_Z] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  736. }
  737. sprintf(buf, "%04x %04x %04x", data[MPU6515_AXIS_X], data[MPU6515_AXIS_Y],
  738. data[MPU6515_AXIS_Z]);
  739. #if DEBUG
  740. if (atomic_read(&obj->trace) & GYRO_TRC_DATA)
  741. GYRO_LOG("get gyro data packet:[%d %d %d]\n", data[0], data[1], data[2]);
  742. #endif
  743. return 0;
  744. }
  745. /* for factory mode */
  746. static int MPU6515_PROCESS_SMT_DATA(struct i2c_client *client, short *data)
  747. {
  748. int total_num = 0;
  749. int retval = 0;
  750. long xSum = 0;
  751. long ySum = 0;
  752. long zSum = 0;
  753. long xAvg, yAvg, zAvg;
  754. long xRMS, yRMS, zRMS;
  755. int i = 0;
  756. int bias_thresh = 5242; /* 40 dps * 131.072 LSB/dps */
  757. /* float RMS_thresh = 687.19f; // (.2 dps * 131.072) ^ 2 */
  758. long RMS_thresh = 68719; /* (.2 dps * 131.072) ^ 2 */
  759. total_num = data[0];
  760. retval = data[1];
  761. GYRO_LOG("MPU6515 read gyro data OK, total number: %d\n", total_num);
  762. for (i = 0; i < total_num; i++) {
  763. xSum = xSum + data[MPU6515_AXES_NUM * i + MPU6515_AXIS_X + 2];
  764. ySum = ySum + data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Y + 2];
  765. zSum = zSum + data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Z + 2];
  766. /*
  767. FLPLOGD("read gyro data OK: packet_num:%d, [X:%5d, Y:%5d, Z:%5d]\n", i,
  768. data[MPU6515_AXES_NUM*i + MPU6515_AXIS_X +2], data[MPU6515_AXES_NUM*i + MPU6515_AXIS_Y +2],
  769. data[MPU6515_AXES_NUM*i + MPU6515_AXIS_Z +2]);
  770. FLPLOGD("MPU6515 xSum: %5d, ySum: %5d, zSum: %5d\n", xSum, ySum, zSum);
  771. */
  772. }
  773. GYRO_LOG("MPU6515 xSum: %5ld, ySum: %5ld, zSum: %5ld\n", xSum, ySum, zSum);
  774. if (total_num != 0) {
  775. xAvg = (xSum / total_num);
  776. yAvg = (ySum / total_num);
  777. zAvg = (zSum / total_num);
  778. } else {
  779. xAvg = xSum;
  780. yAvg = ySum;
  781. zAvg = zSum;
  782. }
  783. GYRO_LOG("MPU6515 xAvg: %ld, yAvg: %ld, zAvg: %ld\n", xAvg, yAvg, zAvg);
  784. if (abs(xAvg) > bias_thresh) {
  785. GYRO_LOG("X-Gyro bias exceeded threshold\n");
  786. retval |= 1 << 3;
  787. }
  788. if (abs(yAvg) > bias_thresh) {
  789. GYRO_LOG("Y-Gyro bias exceeded threshold\n");
  790. retval |= 1 << 4;
  791. }
  792. if (abs(zAvg) > bias_thresh) {
  793. GYRO_LOG("Z-Gyro bias exceeded threshold\n");
  794. retval |= 1 << 5;
  795. }
  796. xRMS = 0;
  797. yRMS = 0;
  798. zRMS = 0;
  799. /* Finally, check RMS */
  800. for (i = 0; i < total_num; i++) {
  801. xRMS +=
  802. (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_X + 2] -
  803. xAvg) * (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_X + 2] - xAvg);
  804. yRMS +=
  805. (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Y + 2] -
  806. yAvg) * (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Y + 2] - yAvg);
  807. zRMS +=
  808. (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Z + 2] -
  809. zAvg) * (data[MPU6515_AXES_NUM * i + MPU6515_AXIS_Z + 2] - zAvg);
  810. }
  811. GYRO_LOG("MPU6515 xRMS: %ld, yRMS: %ld, zRMS: %ld\n", xRMS, yRMS, zRMS);
  812. xRMS = 100 * xRMS;
  813. yRMS = 100 * yRMS;
  814. zRMS = 100 * zRMS;
  815. if (FACTORY_BOOT == get_boot_mode())
  816. return retval;
  817. if (xRMS > RMS_thresh * total_num) {
  818. GYRO_LOG("X-Gyro RMS exceeded threshold, RMS_thresh: %ld\n",
  819. RMS_thresh * total_num);
  820. retval |= 1 << 6;
  821. }
  822. if (yRMS > RMS_thresh * total_num) {
  823. GYRO_LOG("Y-Gyro RMS exceeded threshold, RMS_thresh: %ld\n",
  824. RMS_thresh * total_num);
  825. retval |= 1 << 7;
  826. }
  827. if (zRMS > RMS_thresh * total_num) {
  828. GYRO_LOG("Z-Gyro RMS exceeded threshold, RMS_thresh: %ld\n",
  829. RMS_thresh * total_num);
  830. retval |= 1 << 8;
  831. }
  832. if (xRMS == 0 || yRMS == 0 || zRMS == 0)
  833. /* If any of the RMS noise value returns zero, then we might have dead gyro or FIFO/register failure */
  834. retval |= 1 << 9;
  835. GYRO_LOG("retval %d\n", retval);
  836. return retval;
  837. }
  838. /*----------------------------------------------------------------------------*/
  839. static int MPU6515_SMTReadSensorData(struct i2c_client *client, s16 *buf, int bufsize)
  840. {
  841. /* S16 gyro[MPU6515_AXES_NUM*MPU6515_FIFOSIZE]; */
  842. int res = 0;
  843. int i;
  844. int datalen, total_num = 0;
  845. GYRO_FUN();
  846. MPU6515_SetPWR_MGMT_2(client, true);
  847. if (sensor_power == false)
  848. MPU6515_SetPowerMode(client, true);
  849. if (NULL == buf)
  850. return -1;
  851. if (NULL == client) {
  852. *buf = 0;
  853. return -2;
  854. }
  855. for (i = 0; i < MPU6515_AXES_NUM; i++) {
  856. res = MPU6515_FIFOConfig(client, (i + 1));
  857. if (res) {
  858. GYRO_ERR("MPU6515_FIFOConfig error:%d!\n", res);
  859. return -3;
  860. }
  861. /* putting data in FIFO during the delayed 600ms */
  862. mdelay(600);
  863. res = MPU6515_ReadFifoData(client, &(buf[total_num + 2]), &datalen);
  864. if (res) {
  865. if (res == (-3)) {
  866. buf[1] = (1 << i);
  867. } else {
  868. GYRO_ERR("MPU6515_ReadData error:%d!\n", res);
  869. return -3;
  870. }
  871. } else {
  872. buf[0] = datalen;
  873. total_num += datalen * MPU6515_AXES_NUM;
  874. }
  875. }
  876. GYRO_LOG("gyroscope read data OK, total packet: %d", buf[0]);
  877. return 0;
  878. }
  879. /*----------------------------------------------------------------------------*/
  880. static int MPU6515_ReadChipInfo(struct i2c_client *client, char *buf, int bufsize)
  881. {
  882. u8 databuf[10];
  883. memset(databuf, 0, sizeof(u8) * 10);
  884. if ((NULL == buf) || (bufsize <= 30))
  885. return -1;
  886. if (NULL == client) {
  887. *buf = 0;
  888. return -2;
  889. }
  890. sprintf(buf, "MPU6515 Chip");
  891. return 0;
  892. }
  893. #if INV_GYRO_AUTO_CALI == 1
  894. /*----------------------------------------------------------------------------*/
  895. static int MPU6515_ReadGyroDataRaw(struct i2c_client *client, char *buf, int bufsize)
  896. {
  897. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  898. mutex_lock(&obj->raw_data_mutex);
  899. /* return gyro raw LSB in device orientation */
  900. sprintf(buf, "%x %x %x", obj->inv_cali_raw[MPU6515_AXIS_X],
  901. obj->inv_cali_raw[MPU6515_AXIS_Y], obj->inv_cali_raw[MPU6515_AXIS_Z]);
  902. #if DEBUG
  903. if (atomic_read(&obj->trace) & GYRO_TRC_DATA) {
  904. GYRO_LOG("get gyro raw data packet:[%d %d %d]\n", obj->inv_cali_raw[0],
  905. obj->inv_cali_raw[1], obj->inv_cali_raw[2]);
  906. }
  907. #endif
  908. mutex_unlock(&obj->raw_data_mutex);
  909. return 0;
  910. }
  911. /*----------------------------------------------------------------------------*/
  912. static int MPU6515_ReadTemperature(struct i2c_client *client, char *buf, int bufsize)
  913. {
  914. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  915. mutex_lock(&obj->temperature_mutex);
  916. sprintf(buf, "%x", obj->temperature);
  917. #if DEBUG
  918. if (atomic_read(&obj->trace) & GYRO_TRC_DATA)
  919. GYRO_LOG("get gyro temperature:[%d]\n", obj->temperature);
  920. #endif
  921. mutex_unlock(&obj->temperature_mutex);
  922. return 0;
  923. }
  924. /*----------------------------------------------------------------------------*/
  925. static int MPU6515_ReadPowerStatus(struct i2c_client *client, char *buf, int bufsize)
  926. {
  927. #if DEBUG
  928. GYRO_LOG("get gyro PowerStatus:[%d]\n", sensor_power);
  929. #endif
  930. sprintf(buf, "%x", sensor_power);
  931. return 0;
  932. }
  933. #endif
  934. /*----------------------------------------------------------------------------*/
  935. static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
  936. {
  937. struct i2c_client *client = mpu6515_i2c_client;
  938. char strbuf[MPU6515_BUFSIZE];
  939. if (NULL == client) {
  940. GYRO_ERR("i2c client is null!!\n");
  941. return 0;
  942. }
  943. MPU6515_ReadChipInfo(client, strbuf, MPU6515_BUFSIZE);
  944. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  945. }
  946. /*----------------------------------------------------------------------------*/
  947. static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
  948. {
  949. struct i2c_client *client = mpu6515_i2c_client;
  950. char strbuf[MPU6515_BUFSIZE];
  951. if (NULL == client) {
  952. GYRO_ERR("i2c client is null!!\n");
  953. return 0;
  954. }
  955. MPU6515_ReadGyroData(client, strbuf, MPU6515_BUFSIZE);
  956. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  957. }
  958. /*----------------------------------------------------------------------------*/
  959. static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
  960. {
  961. ssize_t res;
  962. struct mpu6515_i2c_data *obj = obj_i2c_data;
  963. if (obj == NULL) {
  964. GYRO_ERR("i2c_data obj is null!!\n");
  965. return 0;
  966. }
  967. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
  968. return res;
  969. }
  970. /*----------------------------------------------------------------------------*/
  971. static ssize_t store_trace_value(struct device_driver *ddri, const char *buf, size_t count)
  972. {
  973. struct mpu6515_i2c_data *obj = obj_i2c_data;
  974. int trace;
  975. if (obj == NULL) {
  976. GYRO_ERR("i2c_data obj is null!!\n");
  977. return 0;
  978. }
  979. if (1 == sscanf(buf, "0x%x", &trace))
  980. atomic_set(&obj->trace, trace);
  981. else
  982. GYRO_ERR("invalid content: '%s', length = %d\n", buf, (int)count);
  983. return count;
  984. }
  985. /*----------------------------------------------------------------------------*/
  986. static ssize_t show_status_value(struct device_driver *ddri, char *buf)
  987. {
  988. ssize_t len = 0;
  989. struct mpu6515_i2c_data *obj = obj_i2c_data;
  990. if (obj == NULL) {
  991. GYRO_ERR("i2c_data obj is null!!\n");
  992. return 0;
  993. }
  994. if (obj->hw) {
  995. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: %d %d (%d %d)\n",
  996. obj->hw->i2c_num, obj->hw->direction, obj->hw->power_id,
  997. obj->hw->power_vol);
  998. } else {
  999. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: NULL\n");
  1000. }
  1001. return len;
  1002. }
  1003. /*----------------------------------------------------------------------------*/
  1004. static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
  1005. static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
  1006. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, show_trace_value, store_trace_value);
  1007. static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
  1008. /*----------------------------------------------------------------------------*/
  1009. static struct driver_attribute *MPU6515_attr_list[] = {
  1010. &driver_attr_chipinfo, /*chip information */
  1011. &driver_attr_sensordata, /*dump sensor data */
  1012. &driver_attr_trace, /*trace log */
  1013. &driver_attr_status,
  1014. };
  1015. /*----------------------------------------------------------------------------*/
  1016. static int mpu6515_create_attr(struct device_driver *driver)
  1017. {
  1018. int idx, err = 0;
  1019. int num = (int)(sizeof(MPU6515_attr_list) / sizeof(MPU6515_attr_list[0]));
  1020. if (driver == NULL)
  1021. return -EINVAL;
  1022. for (idx = 0; idx < num; idx++) {
  1023. err = driver_create_file(driver, MPU6515_attr_list[idx]);
  1024. if (0 != err) {
  1025. GYRO_ERR("driver_create_file (%s) = %d\n",
  1026. MPU6515_attr_list[idx]->attr.name, err);
  1027. break;
  1028. }
  1029. }
  1030. return err;
  1031. }
  1032. /*----------------------------------------------------------------------------*/
  1033. static int mpu6515_delete_attr(struct device_driver *driver)
  1034. {
  1035. int idx, err = 0;
  1036. int num = (int)(sizeof(MPU6515_attr_list) / sizeof(MPU6515_attr_list[0]));
  1037. if (driver == NULL)
  1038. return -EINVAL;
  1039. for (idx = 0; idx < num; idx++)
  1040. driver_remove_file(driver, MPU6515_attr_list[idx]);
  1041. return err;
  1042. }
  1043. /*----------------------------------------------------------------------------*/
  1044. static int mpu6515_gpio_config(void)
  1045. {
  1046. #ifndef CONFIG_FPGA_EARLY_PORTING
  1047. /* because we donot use EINT ,to support low power */
  1048. /* config to GPIO input mode + PD */
  1049. /* set GPIO_MSE_EINT_PIN */
  1050. int ret;
  1051. struct pinctrl *pinctrl;
  1052. struct pinctrl_state *pins_default;
  1053. struct pinctrl_state *pins_cfg;
  1054. #ifdef CONFIG_MTK_LEGACY
  1055. mt_set_gpio_mode(GPIO_GYRO_EINT_PIN, GPIO_GYRO_EINT_PIN_M_GPIO);
  1056. mt_set_gpio_dir(GPIO_GYRO_EINT_PIN, GPIO_DIR_IN);
  1057. mt_set_gpio_pull_enable(GPIO_GYRO_EINT_PIN, GPIO_PULL_ENABLE);
  1058. mt_set_gpio_pull_select(GPIO_GYRO_EINT_PIN, GPIO_PULL_DOWN);
  1059. #else
  1060. pinctrl = devm_pinctrl_get(&gyroPltFmDev->dev);
  1061. if (IS_ERR(pinctrl)) {
  1062. ret = PTR_ERR(pinctrl);
  1063. GYRO_ERR("Cannot find gyro pinctrl!\n");
  1064. }
  1065. pins_default = pinctrl_lookup_state(pinctrl, "pin_default");
  1066. if (IS_ERR(pins_default)) {
  1067. ret = PTR_ERR(pins_default);
  1068. GYRO_ERR("Cannot find gyro pinctrl default!\n");
  1069. }
  1070. pins_cfg = pinctrl_lookup_state(pinctrl, "pin_cfg");
  1071. if (IS_ERR(pins_cfg)) {
  1072. ret = PTR_ERR(pins_cfg);
  1073. GYRO_ERR("Cannot find gyro pinctrl pin_cfg!\n");
  1074. }
  1075. pinctrl_select_state(pinctrl, pins_cfg);
  1076. #endif
  1077. #endif /* #ifndef FPGA_EARLY_PORTING */
  1078. return 0;
  1079. }
  1080. static int mpu6515_init_client(struct i2c_client *client, bool enable)
  1081. {
  1082. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  1083. int res = 0;
  1084. GYRO_FUN();
  1085. mpu6515_gpio_config();
  1086. res = MPU6515_SetPowerMode(client, true);
  1087. if (res != MPU6515_SUCCESS)
  1088. return res;
  1089. /* The range should at least be 17.45 rad/s (ie: ~1000 deg/s). */
  1090. res = MPU6515_SetDataFormat(client, (MPU6515_SYNC_GYROX << MPU6515_EXT_SYNC) |
  1091. MPU6515_RATE_1K_LPFB_188HZ);
  1092. res = MPU6515_SetFullScale(client, (MPU6515_DEFAULT_FS << MPU6515_FS_RANGE));
  1093. if (res != MPU6515_SUCCESS)
  1094. return res;
  1095. /* Set 125HZ sample rate */
  1096. res = MPU6515_SetSampleRate(client, 200);
  1097. if (res != MPU6515_SUCCESS)
  1098. return res;
  1099. res = MPU6515_SetPWR_MGMT_2(client, enable_status);
  1100. if (res != MPU6515_SUCCESS)
  1101. return res;
  1102. res = MPU6515_SetPowerMode(client, enable_status);
  1103. if (res != MPU6515_SUCCESS)
  1104. return res;
  1105. GYRO_LOG("mpu6515_init_client OK!\n");
  1106. #ifdef CONFIG_MPU6515_LOWPASS
  1107. memset(&obj->fir, 0x00, sizeof(obj->fir));
  1108. #endif
  1109. return MPU6515_SUCCESS;
  1110. }
  1111. /******************************************************************************
  1112. * Function Configuration
  1113. ******************************************************************************/
  1114. static int mpu6515_open(struct inode *inode, struct file *file)
  1115. {
  1116. file->private_data = mpu6515_i2c_client;
  1117. if (file->private_data == NULL) {
  1118. GYRO_ERR("null pointer!!\n");
  1119. return -EINVAL;
  1120. }
  1121. return nonseekable_open(inode, file);
  1122. }
  1123. /*----------------------------------------------------------------------------*/
  1124. static int mpu6515_release(struct inode *inode, struct file *file)
  1125. {
  1126. file->private_data = NULL;
  1127. return 0;
  1128. }
  1129. /*----------------------------------------------------------------------------*/
  1130. static long mpu6515_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1131. {
  1132. struct i2c_client *client = (struct i2c_client *)file->private_data;
  1133. char strbuf[MPU6515_BUFSIZE] = { 0 };
  1134. s16 *SMTdata;
  1135. void __user *data;
  1136. long err = 0;
  1137. int copy_cnt = 0;
  1138. struct SENSOR_DATA sensor_data;
  1139. int cali[3];
  1140. int smtRes = 0;
  1141. if (_IOC_DIR(cmd) & _IOC_READ)
  1142. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  1143. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  1144. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  1145. if (err) {
  1146. GYRO_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  1147. return -EFAULT;
  1148. }
  1149. switch (cmd) {
  1150. case GYROSCOPE_IOCTL_INIT:
  1151. mpu6515_init_client(client, false);
  1152. break;
  1153. case GYROSCOPE_IOCTL_SMT_DATA:
  1154. data = (void __user *)arg;
  1155. if (data == NULL) {
  1156. err = -EINVAL;
  1157. break;
  1158. }
  1159. SMTdata = kzalloc(sizeof(*SMTdata) * 800, GFP_KERNEL);
  1160. if (SMTdata == NULL) {
  1161. err = -ENOMEM;
  1162. break;
  1163. }
  1164. memset(SMTdata, 0, sizeof(*SMTdata) * 800);
  1165. MPU6515_SMTReadSensorData(client, SMTdata, 800);
  1166. GYRO_LOG("gyroscope read data from kernel OK: SMTdata[0]:%d, copied packet:%d!\n",
  1167. SMTdata[0], (int)((SMTdata[0] * MPU6515_AXES_NUM + 2) * sizeof(s16) + 1));
  1168. smtRes = MPU6515_PROCESS_SMT_DATA(client, SMTdata);
  1169. GYRO_LOG("ioctl smtRes: %d!\n", smtRes);
  1170. copy_cnt = copy_to_user(data, &smtRes, sizeof(smtRes));
  1171. kfree(SMTdata);
  1172. if (copy_cnt) {
  1173. err = -EFAULT;
  1174. GYRO_ERR("copy gyro data to user failed!\n");
  1175. }
  1176. GYRO_LOG("copy gyro data to user OK: %d!\n", copy_cnt);
  1177. break;
  1178. case GYROSCOPE_IOCTL_READ_SENSORDATA:
  1179. data = (void __user *)arg;
  1180. if (data == NULL) {
  1181. err = -EINVAL;
  1182. break;
  1183. }
  1184. /* prevent meta calibration timeout */
  1185. MPU6515_SetPWR_MGMT_2(client, true);
  1186. if (false == sensor_power) {
  1187. MPU6515_SetPowerMode(client, true);
  1188. msleep(50);
  1189. }
  1190. MPU6515_ReadGyroData(client, strbuf, MPU6515_BUFSIZE);
  1191. if (copy_to_user(data, strbuf, sizeof(strbuf))) {
  1192. err = -EFAULT;
  1193. break;
  1194. }
  1195. break;
  1196. case GYROSCOPE_IOCTL_SET_CALI:
  1197. data = (void __user *)arg;
  1198. if (data == NULL) {
  1199. err = -EINVAL;
  1200. break;
  1201. }
  1202. if (copy_from_user(&sensor_data, data, sizeof(sensor_data))) {
  1203. err = -EFAULT;
  1204. break;
  1205. }
  1206. else {
  1207. cali[MPU6515_AXIS_X] =
  1208. sensor_data.x * MPU6515_DEFAULT_LSB / MPU6515_FS_MAX_LSB;
  1209. cali[MPU6515_AXIS_Y] =
  1210. sensor_data.y * MPU6515_DEFAULT_LSB / MPU6515_FS_MAX_LSB;
  1211. cali[MPU6515_AXIS_Z] =
  1212. sensor_data.z * MPU6515_DEFAULT_LSB / MPU6515_FS_MAX_LSB;
  1213. GYRO_LOG("gyro set cali:[%5d %5d %5d]\n", cali[MPU6515_AXIS_X],
  1214. cali[MPU6515_AXIS_Y], cali[MPU6515_AXIS_Z]);
  1215. err = MPU6515_WriteCalibration(client, cali);
  1216. }
  1217. break;
  1218. case GYROSCOPE_IOCTL_CLR_CALI:
  1219. err = MPU6515_ResetCalibration(client);
  1220. break;
  1221. case GYROSCOPE_IOCTL_GET_CALI:
  1222. data = (void __user *)arg;
  1223. if (data == NULL) {
  1224. err = -EINVAL;
  1225. break;
  1226. }
  1227. err = MPU6515_ReadCalibration(client, cali);
  1228. if (err)
  1229. break;
  1230. sensor_data.x = cali[MPU6515_AXIS_X] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  1231. sensor_data.y = cali[MPU6515_AXIS_Y] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  1232. sensor_data.z = cali[MPU6515_AXIS_Z] * MPU6515_FS_MAX_LSB / MPU6515_DEFAULT_LSB;
  1233. if (copy_to_user(data, &sensor_data, sizeof(sensor_data))) {
  1234. err = -EFAULT;
  1235. break;
  1236. }
  1237. break;
  1238. #if INV_GYRO_AUTO_CALI == 1
  1239. case GYROSCOPE_IOCTL_READ_SENSORDATA_RAW:
  1240. data = (void __user *)arg;
  1241. if (data == NULL) {
  1242. err = -EINVAL;
  1243. break;
  1244. }
  1245. MPU6515_ReadGyroDataRaw(client, strbuf, MPU6515_BUFSIZE);
  1246. if (copy_to_user(data, strbuf, sizeof(strbuf))) {
  1247. err = -EFAULT;
  1248. break;
  1249. }
  1250. break;
  1251. case GYROSCOPE_IOCTL_READ_TEMPERATURE:
  1252. data = (void __user *)arg;
  1253. if (data == NULL) {
  1254. err = -EINVAL;
  1255. break;
  1256. }
  1257. MPU6515_ReadTemperature(client, strbuf, MPU6515_BUFSIZE);
  1258. if (copy_to_user(data, strbuf, sizeof(strbuf))) {
  1259. err = -EFAULT;
  1260. break;
  1261. }
  1262. break;
  1263. case GYROSCOPE_IOCTL_GET_POWER_STATUS:
  1264. data = (void __user *)arg;
  1265. if (data == NULL) {
  1266. err = -EINVAL;
  1267. break;
  1268. }
  1269. MPU6515_ReadPowerStatus(client, strbuf, MPU6515_BUFSIZE);
  1270. if (copy_to_user(data, strbuf, sizeof(strbuf))) {
  1271. err = -EFAULT;
  1272. break;
  1273. }
  1274. break;
  1275. #endif
  1276. default:
  1277. GYRO_ERR("unknown IOCTL: 0x%08x\n", cmd);
  1278. err = -ENOIOCTLCMD;
  1279. break;
  1280. }
  1281. return err;
  1282. }
  1283. #if IS_ENABLED(CONFIG_COMPAT)
  1284. static long compat_mpu6515_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1285. {
  1286. GYRO_FUN();
  1287. if (!filp->f_op || !filp->f_op->unlocked_ioctl) {
  1288. GYRO_ERR("compat_ion_ioctl file has no f_op or no f_op->unlocked_ioctl.\n");
  1289. return -ENOTTY;
  1290. }
  1291. switch (cmd) {
  1292. case COMPAT_GYROSCOPE_IOCTL_INIT:
  1293. case COMPAT_GYROSCOPE_IOCTL_SMT_DATA:
  1294. case COMPAT_GYROSCOPE_IOCTL_READ_SENSORDATA_RAW:
  1295. case COMPAT_GYROSCOPE_IOCTL_READ_TEMPERATURE:
  1296. case COMPAT_GYROSCOPE_IOCTL_GET_POWER_STATUS:
  1297. case COMPAT_GYROSCOPE_IOCTL_READ_SENSORDATA:
  1298. /* NVRAM will use below ioctl */
  1299. case COMPAT_GYROSCOPE_IOCTL_SET_CALI:
  1300. case COMPAT_GYROSCOPE_IOCTL_CLR_CALI:
  1301. case COMPAT_GYROSCOPE_IOCTL_GET_CALI:{
  1302. GYRO_LOG("compat_ion_ioctl : GYROSCOPE_IOCTL_XXX command is 0x%x\n", cmd);
  1303. return filp->f_op->unlocked_ioctl(filp, cmd,
  1304. (unsigned long)compat_ptr(arg));
  1305. }
  1306. default:{
  1307. GYRO_ERR("compat_ion_ioctl : No such command!! 0x%x\n", cmd);
  1308. return -ENOIOCTLCMD;
  1309. }
  1310. }
  1311. }
  1312. #endif
  1313. /*----------------------------------------------------------------------------*/
  1314. static const struct file_operations mpu6515_fops = {
  1315. .open = mpu6515_open,
  1316. .release = mpu6515_release,
  1317. .unlocked_ioctl = mpu6515_unlocked_ioctl,
  1318. #if IS_ENABLED(CONFIG_COMPAT)
  1319. .compat_ioctl = compat_mpu6515_unlocked_ioctl,
  1320. #endif
  1321. };
  1322. /*----------------------------------------------------------------------------*/
  1323. static struct miscdevice mpu6515_device = {
  1324. .minor = MISC_DYNAMIC_MINOR,
  1325. .name = "gyroscope",
  1326. .fops = &mpu6515_fops,
  1327. };
  1328. /*----------------------------------------------------------------------------*/
  1329. #if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
  1330. /*----------------------------------------------------------------------------*/
  1331. static int mpu6515_suspend(struct i2c_client *client, pm_message_t msg)
  1332. {
  1333. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  1334. int err = 0;
  1335. GYRO_FUN();
  1336. if (msg.event == PM_EVENT_SUSPEND) {
  1337. if (obj == NULL) {
  1338. GYRO_ERR("null pointer!!\n");
  1339. return -EINVAL;
  1340. }
  1341. atomic_set(&obj->suspend, 1);
  1342. MPU6515_SetPWR_MGMT_2(client, false);
  1343. #ifndef CUSTOM_KERNEL_SENSORHUB
  1344. err = MPU6515_SetPowerMode(client, false);
  1345. #else
  1346. err = MPU6515_SCP_SetPowerMode(false, ID_GYROSCOPE);
  1347. #endif
  1348. if (err <= 0)
  1349. return err;
  1350. }
  1351. return err;
  1352. }
  1353. /*----------------------------------------------------------------------------*/
  1354. static int mpu6515_resume(struct i2c_client *client)
  1355. {
  1356. struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
  1357. int err;
  1358. GYRO_FUN();
  1359. if (obj == NULL) {
  1360. GYRO_ERR("null pointer!!\n");
  1361. return -EINVAL;
  1362. }
  1363. MPU6515_power(obj->hw, 1);
  1364. MPU6515_SetPWR_MGMT_2(client, enable_status);
  1365. #ifndef CUSTOM_KERNEL_SENSORHUB
  1366. err = mpu6515_init_client(client, false);
  1367. #else
  1368. err = MPU6515_SCP_SetPowerMode(enable_status, ID_GYROSCOPE);
  1369. #endif
  1370. if (err) {
  1371. GYRO_ERR("initialize client fail!!\n");
  1372. return err;
  1373. }
  1374. atomic_set(&obj->suspend, 0);
  1375. return 0;
  1376. }
  1377. /*----------------------------------------------------------------------------*/
  1378. #else /*CONFIG_HAS_EARLY_SUSPEND is defined */
  1379. /*----------------------------------------------------------------------------*/
  1380. static void mpu6515_early_suspend(struct early_suspend *h)
  1381. {
  1382. struct mpu6515_i2c_data *obj = container_of(h, struct mpu6515_i2c_data, early_drv);
  1383. int err;
  1384. /* u8 databuf[2]; */
  1385. GYRO_FUN();
  1386. if (obj == NULL) {
  1387. GYRO_ERR("null pointer!!\n");
  1388. return;
  1389. }
  1390. atomic_set(&obj->suspend, 1);
  1391. MPU6515_SetPWR_MGMT_2(obj->client, false);
  1392. #ifndef CUSTOM_KERNEL_SENSORHUB
  1393. err = MPU6515_SetPowerMode(obj->client, false);
  1394. #else
  1395. err = MPU6515_SCP_SetPowerMode(false, ID_GYROSCOPE);
  1396. #endif
  1397. if (err) {
  1398. GYRO_ERR("write power control fail!!\n");
  1399. return;
  1400. }
  1401. sensor_power = false;
  1402. MPU6515_power(obj->hw, 0);
  1403. }
  1404. /*----------------------------------------------------------------------------*/
  1405. static void mpu6515_late_resume(struct early_suspend *h)
  1406. {
  1407. struct mpu6515_i2c_data *obj = container_of(h, struct mpu6515_i2c_data, early_drv);
  1408. int err;
  1409. GYRO_FUN();
  1410. if (obj == NULL) {
  1411. GYRO_ERR("null pointer!!\n");
  1412. return;
  1413. }
  1414. MPU6515_power(obj->hw, 1);
  1415. MPU6515_SetPWR_MGMT_2(obj->client, enable_status);
  1416. #ifndef CUSTOM_KERNEL_SENSORHUB
  1417. err = mpu6515_init_client(obj->client, false);
  1418. #else
  1419. err = MPU6515_SCP_SetPowerMode(enable_status, ID_GYROSCOPE);
  1420. #endif
  1421. if (err) {
  1422. GYRO_ERR("initialize client fail! err code %d!\n", err);
  1423. return;
  1424. }
  1425. atomic_set(&obj->suspend, 0);
  1426. }
  1427. /*----------------------------------------------------------------------------*/
  1428. #endif /*CONFIG_HAS_EARLYSUSPEND */
  1429. /*----------------------------------------------------------------------------*/
  1430. /* if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL */
  1431. static int gyroscope_open_report_data(int open)
  1432. {
  1433. /* should queuq work to report event if is_report_input_direct=true */
  1434. return 0;
  1435. }
  1436. /*----------------------------------------------------------------------------*/
  1437. /* if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL */
  1438. static int gyroscope_enable_nodata(int en)
  1439. {
  1440. int err = 0;
  1441. #ifdef GSENSOR_UT
  1442. GYRO_FUN();
  1443. #endif
  1444. mutex_lock(&gyroscope_mutex);
  1445. if (((en == 0) && (sensor_power == false)) || ((en == 1) && (sensor_power == true))) {
  1446. enable_status = sensor_power;
  1447. GYRO_LOG("Gsensor device have updated!\n");
  1448. } else {
  1449. enable_status = !sensor_power;
  1450. if (atomic_read(&obj_i2c_data->suspend) == 0) {
  1451. #ifndef CUSTOM_KERNEL_SENSORHUB
  1452. err = MPU6515_SetPowerMode(obj_i2c_data->client, enable_status);
  1453. #else
  1454. err = MPU6515_SCP_SetPowerMode(enable_status, ID_GYROSCOPE);
  1455. if (0 == err)
  1456. sensor_power = enable_status;
  1457. #endif
  1458. MPU6515_SetPWR_MGMT_2(obj_i2c_data->client, enable_status);
  1459. GYRO_LOG
  1460. ("Gsensor not in suspend gsensor_SetPowerMode!, enable_status = %d\n",
  1461. enable_status);
  1462. } else {
  1463. GYRO_LOG
  1464. ("Gsensor in suspend and can not enable or disable!enable_status = %d\n",
  1465. enable_status);
  1466. }
  1467. }
  1468. mutex_unlock(&gyroscope_mutex);
  1469. if (err != MPU6515_SUCCESS) {
  1470. GYRO_ERR("gsensor_enable_nodata fail!\n");
  1471. return -1;
  1472. }
  1473. GYRO_LOG("gsensor_enable_nodata OK!\n");
  1474. return 0;
  1475. }
  1476. /*----------------------------------------------------------------------------*/
  1477. static int gyroscope_set_delay(u64 ns)
  1478. {
  1479. return 0;
  1480. }
  1481. /*----------------------------------------------------------------------------*/
  1482. static int gyroscope_get_data(int *x, int *y, int *z, int *status)
  1483. {
  1484. char buff[MPU6515_BUFSIZE];
  1485. #ifdef GSENSOR_UT
  1486. GSE_FUN();
  1487. #endif
  1488. mutex_lock(&gyroscope_mutex);
  1489. MPU6515_ReadGyroData(obj_i2c_data->client, buff, MPU6515_BUFSIZE);
  1490. mutex_unlock(&gyroscope_mutex);
  1491. if (sscanf(buff, "%x %x %x", x, y, z) != 3)
  1492. GYRO_ERR("sscanf parsing fail\n");
  1493. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1494. return 0;
  1495. }
  1496. /*----------------------------------------------------------------------------*/
  1497. static int mpu6515_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  1498. {
  1499. strcpy(info->type, MPU6515_DEV_NAME);
  1500. return 0;
  1501. }
  1502. /*----------------------------------------------------------------------------*/
  1503. static int mpu6515_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1504. {
  1505. struct i2c_client *new_client;
  1506. struct mpu6515_i2c_data *obj;
  1507. struct gyro_control_path ctl = { 0 };
  1508. struct gyro_data_path data = { 0 };
  1509. int err = 0;
  1510. GYRO_FUN();
  1511. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  1512. if (!(obj)) {
  1513. err = -ENOMEM;
  1514. goto exit;
  1515. }
  1516. memset(obj, 0, sizeof(struct mpu6515_i2c_data));
  1517. obj->hw = get_cust_gyro();
  1518. err = hwmsen_get_convert(obj->hw->direction, &obj->cvt);
  1519. if (err) {
  1520. GYRO_ERR("invalid direction: %d\n", obj->hw->direction);
  1521. goto exit;
  1522. }
  1523. GYRO_LOG("gyro_default_i2c_addr: %x\n", client->addr);
  1524. #ifdef MPU6515_ACCESS_BY_GSE_I2C
  1525. obj->hw->addr = MPU6515_I2C_SLAVE_ADDR; /* mtk i2c not allow to probe two same address */
  1526. #endif
  1527. GYRO_LOG("gyro_custom_i2c_addr: %x\n", obj->hw->addr);
  1528. if (0 != obj->hw->addr) {
  1529. client->addr = obj->hw->addr >> 1;
  1530. GYRO_LOG("gyro_use_i2c_addr: %x\n", client->addr);
  1531. }
  1532. obj_i2c_data = obj;
  1533. obj->client = client;
  1534. new_client = obj->client;
  1535. i2c_set_clientdata(new_client, obj);
  1536. atomic_set(&obj->trace, 0);
  1537. atomic_set(&obj->suspend, 0);
  1538. mpu6515_i2c_client = new_client;
  1539. err = mpu6515_init_client(new_client, false);
  1540. if (err)
  1541. goto exit_init_failed;
  1542. err = misc_register(&mpu6515_device);
  1543. if (err) {
  1544. GYRO_ERR("mpu6515_device misc register failed!\n");
  1545. goto exit_misc_device_register_failed;
  1546. }
  1547. err = mpu6515_create_attr(&gyroscope_init_info.platform_diver_addr->driver);
  1548. if (err) {
  1549. GYRO_ERR("mpu6515 create attribute err = %d\n", err);
  1550. goto exit_create_attr_failed;
  1551. }
  1552. ctl.open_report_data = gyroscope_open_report_data;
  1553. ctl.enable_nodata = gyroscope_enable_nodata;
  1554. ctl.set_delay = gyroscope_set_delay;
  1555. ctl.is_report_input_direct = false;
  1556. err = gyro_register_control_path(&ctl);
  1557. if (err) {
  1558. GYRO_ERR("register acc control path err\n");
  1559. goto exit_kfree;
  1560. }
  1561. data.get_data = gyroscope_get_data;
  1562. data.vender_div = DEGREE_TO_RAD;
  1563. err = gyro_register_data_path(&data);
  1564. if (err) {
  1565. GYRO_ERR("register acc data path err\n");
  1566. goto exit_kfree;
  1567. }
  1568. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND) && defined(USE_EARLY_SUSPEND)
  1569. obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 2,
  1570. obj->early_drv.suspend = mpu6515_early_suspend,
  1571. obj->early_drv.resume = mpu6515_late_resume, register_early_suspend(&obj->early_drv);
  1572. #endif
  1573. #if INV_GYRO_AUTO_CALI == 1
  1574. mutex_init(&obj->temperature_mutex);
  1575. mutex_init(&obj->raw_data_mutex);
  1576. #endif
  1577. gyroscope_init_flag = 0;
  1578. GYRO_LOG("%s: OK\n", __func__);
  1579. return 0;
  1580. exit_create_attr_failed:
  1581. misc_deregister(&mpu6515_device);
  1582. exit_misc_device_register_failed:
  1583. exit_init_failed:
  1584. /* i2c_detach_client(new_client); */
  1585. exit_kfree:
  1586. kfree(obj);
  1587. exit:
  1588. GYRO_ERR("%s: err = %d\n", __func__, err);
  1589. gyroscope_init_flag = -1;
  1590. return err;
  1591. }
  1592. /*----------------------------------------------------------------------------*/
  1593. static int mpu6515_i2c_remove(struct i2c_client *client)
  1594. {
  1595. int err = 0;
  1596. err = mpu6515_delete_attr(&gyroscope_init_info.platform_diver_addr->driver);
  1597. if (err)
  1598. GYRO_ERR("mpu6515_delete_attr fail: %d\n", err);
  1599. err = misc_deregister(&mpu6515_device);
  1600. if (err)
  1601. GYRO_ERR("misc_deregister fail: %d\n", err);
  1602. err = hwmsen_detach(ID_ACCELEROMETER);
  1603. if (err)
  1604. GYRO_ERR("hwmsen_detach fail: %d\n", err);
  1605. mpu6515_i2c_client = NULL;
  1606. i2c_unregister_device(client);
  1607. kfree(i2c_get_clientdata(client));
  1608. return 0;
  1609. }
  1610. /*----------------------------------------------------------------------------*/
  1611. static int gyroscope_local_init(struct platform_device *pdev)
  1612. {
  1613. struct gyro_hw *hw = get_cust_gyro();
  1614. gyroPltFmDev = pdev;
  1615. GYRO_FUN();
  1616. MPU6515_power(hw, 1);
  1617. if (i2c_add_driver(&mpu6515_i2c_driver)) {
  1618. GYRO_ERR("add driver error\n");
  1619. return -1;
  1620. }
  1621. if (-1 == gyroscope_init_flag)
  1622. return -1;
  1623. return 0;
  1624. }
  1625. /*----------------------------------------------------------------------------*/
  1626. static int gyroscope_remove(void)
  1627. {
  1628. struct gyro_hw *hw = get_cust_gyro();
  1629. GYRO_FUN();
  1630. MPU6515_power(hw, 0);
  1631. i2c_del_driver(&mpu6515_i2c_driver);
  1632. return 0;
  1633. }
  1634. /*----------------------------------------------------------------------------*/
  1635. static int __init mpu6515_init(void)
  1636. {
  1637. const char *name = "mediatek,mpu6515g";
  1638. hw = get_gyro_dts_func(name, hw);
  1639. if (!hw)
  1640. GYRO_ERR("get cust_gyro dts info fail\n");
  1641. GYRO_LOG("%s: i2c_number=%d\n", __func__, hw->i2c_num);
  1642. #ifdef CONFIG_MTK_LEGACY
  1643. i2c_register_board_info(hw->i2c_num, &i2c_mpu6515, 1);
  1644. #endif
  1645. gyro_driver_add(&gyroscope_init_info);
  1646. return 0;
  1647. }
  1648. /*----------------------------------------------------------------------------*/
  1649. static void __exit mpu6515_exit(void)
  1650. {
  1651. GYRO_FUN();
  1652. }
  1653. /*----------------------------------------------------------------------------*/
  1654. module_init(mpu6515_init);
  1655. module_exit(mpu6515_exit);
  1656. /*----------------------------------------------------------------------------*/
  1657. MODULE_LICENSE("GPL");
  1658. MODULE_DESCRIPTION("MPU6515 gyroscope driver");
  1659. MODULE_AUTHOR("Yucong.Xiong@mediatek.com");