icm20645.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /* ICM20645 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 <cust_gyro.h>
  14. #include "icm20645.h"
  15. #include <gyroscope.h>
  16. /*----------------------------------------------------------------------------*/
  17. #define ICM20645_DEFAULT_FS ICM20645_FS_1000
  18. #define ICM20645_DEFAULT_LSB ICM20645_FS_1000_LSB
  19. /*---------------------------------------------------------------------------*/
  20. #define DEBUG 0
  21. /*----------------------------------------------------------------------------*/
  22. #define CONFIG_ICM20645_LOWPASS /*apply low pass filter on output */
  23. /*----------------------------------------------------------------------------*/
  24. #define ICM20645_AXIS_X 0
  25. #define ICM20645_AXIS_Y 1
  26. #define ICM20645_AXIS_Z 2
  27. #define ICM20645_AXES_NUM 3
  28. #define ICM20645_DATA_LEN 6
  29. #define ICM20645_DEV_NAME "ICM20645GY" /* name must different with gsensor icm20645 */
  30. /*----------------------------------------------------------------------------*/
  31. static const struct i2c_device_id icm20645_i2c_id[] = { {ICM20645_DEV_NAME, 0}, {} };
  32. struct gyro_hw gyro_cust;
  33. static struct gyro_hw *hw = &gyro_cust;
  34. struct platform_device *gyroPltFmDev;
  35. /* For driver get cust info */
  36. struct gyro_hw *get_cust_gyro(void)
  37. {
  38. return &gyro_cust;
  39. }
  40. /*----------------------------------------------------------------------------*/
  41. static int icm20645_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  42. static int icm20645_i2c_remove(struct i2c_client *client);
  43. static int icm20645_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
  44. #if !defined(CONFIG_HAS_EARLYSUSPEND)
  45. static int icm20645_suspend(struct i2c_client *client, pm_message_t msg);
  46. static int icm20645_resume(struct i2c_client *client);
  47. #endif
  48. static int icm20645_local_init(struct platform_device *pdev);
  49. static int icm20645_remove(void);
  50. static int icm20645_init_flag = -1;
  51. static struct gyro_init_info icm20645_init_info = {
  52. .name = "icm20645GY",
  53. .init = icm20645_local_init,
  54. .uninit = icm20645_remove,
  55. };
  56. /*----------------------------------------------------------------------------*/
  57. enum {
  58. GYRO_TRC_FILTER = 0x01,
  59. GYRO_TRC_RAWDATA = 0x02,
  60. GYRO_TRC_IOCTL = 0x04,
  61. GYRO_TRC_CALI = 0X08,
  62. GYRO_TRC_INFO = 0X10,
  63. GYRO_TRC_DATA = 0X20,
  64. } GYRO_TRC;
  65. /*----------------------------------------------------------------------------*/
  66. struct scale_factor {
  67. u8 whole;
  68. u8 fraction;
  69. };
  70. /*----------------------------------------------------------------------------*/
  71. struct data_resolution {
  72. struct scale_factor scalefactor;
  73. int sensitivity;
  74. };
  75. /*----------------------------------------------------------------------------*/
  76. #define C_MAX_FIR_LENGTH (32)
  77. /*----------------------------------------------------------------------------*/
  78. struct data_filter {
  79. s16 raw[C_MAX_FIR_LENGTH][ICM20645_AXES_NUM];
  80. int sum[ICM20645_AXES_NUM];
  81. int num;
  82. int idx;
  83. };
  84. /*----------------------------------------------------------------------------*/
  85. struct icm20645_i2c_data {
  86. struct i2c_client *client;
  87. struct gyro_hw *hw;
  88. struct hwmsen_convert cvt;
  89. /*misc */
  90. struct data_resolution *reso;
  91. atomic_t trace;
  92. atomic_t suspend;
  93. atomic_t selftest;
  94. atomic_t filter;
  95. s16 cali_sw[ICM20645_AXES_NUM + 1];
  96. /*data */
  97. s8 offset[ICM20645_AXES_NUM + 1]; /*+1: for 4-byte alignment */
  98. s16 data[ICM20645_AXES_NUM + 1];
  99. #if defined(CONFIG_ICM20645_LOWPASS)
  100. atomic_t firlen;
  101. atomic_t fir_en;
  102. struct data_filter fir;
  103. #endif
  104. };
  105. /*----------------------------------------------------------------------------*/
  106. #ifdef CONFIG_OF
  107. static const struct of_device_id gyro_of_match[] = {
  108. {.compatible = "mediatek,gyro"},
  109. {},
  110. };
  111. #endif
  112. static struct i2c_driver icm20645_i2c_driver = {
  113. .driver = {
  114. .name = ICM20645_DEV_NAME,
  115. #ifdef CONFIG_OF
  116. .of_match_table = gyro_of_match,
  117. #endif
  118. },
  119. .probe = icm20645_i2c_probe,
  120. .remove = icm20645_i2c_remove,
  121. .detect = icm20645_i2c_detect,
  122. #if !defined(CONFIG_HAS_EARLYSUSPEND)
  123. .suspend = icm20645_suspend,
  124. .resume = icm20645_resume,
  125. #endif
  126. .id_table = icm20645_i2c_id,
  127. };
  128. /*----------------------------------------------------------------------------*/
  129. static struct i2c_client *icm20645_i2c_client;
  130. static struct icm20645_i2c_data *obj_i2c_data;
  131. static bool sensor_power;
  132. static unsigned int power_on;
  133. int ICM20645_gyro_power(void)
  134. {
  135. return power_on;
  136. }
  137. EXPORT_SYMBOL(ICM20645_gyro_power);
  138. int ICM20645_gyro_mode(void)
  139. {
  140. return sensor_power;
  141. }
  142. EXPORT_SYMBOL(ICM20645_gyro_mode);
  143. static int icm20645_set_bank(struct i2c_client *client, u8 bank)
  144. {
  145. int res;
  146. u8 databuf[2];
  147. databuf[0] = bank;
  148. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  149. res = ICM20645_hwmsen_write_block(REG_BANK_SEL, databuf, 0x01);
  150. if (res < 0) {
  151. GYRO_LOG("icm20645_set_bank fail at %x\n", bank);
  152. return ICM20645_ERR_I2C;
  153. }
  154. #else
  155. if (hwmsen_write_byte(client, REG_BANK_SEL, databuf)) {
  156. GYRO_LOG("icm20645_set_bank fail at %x\n", bank);
  157. return ICM20645_ERR_I2C;
  158. }
  159. #endif
  160. return ICM20645_SUCCESS;
  161. }
  162. static int icm20645_lp_mode(struct i2c_client *client, bool on)
  163. {
  164. int res;
  165. u8 databuf[2];
  166. memset(databuf, 0, sizeof(databuf));
  167. icm20645_set_bank(client, BANK_SEL_0);
  168. /* gyroscope lp config */
  169. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  170. res = ICM20645_hwmsen_read_block(ICM20645_REG_LP_CONFIG, databuf, 0x01);
  171. if (res < 0) {
  172. GYRO_LOG("icm20645_gyro_lp_mode fail at %x\n", on);
  173. return ICM20645_ERR_I2C;
  174. }
  175. #else
  176. if (hwmsen_read_byte(client, ICM20645_REG_LP_CONFIG, databuf)) {
  177. GYRO_LOG("icm20645_gyro_lp_mode fail at %x\n", on);
  178. return ICM20645_ERR_I2C;
  179. }
  180. #endif
  181. if (on == true) {
  182. databuf[0] |= BIT_GYRO_LP_EN;
  183. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  184. res = ICM20645_hwmsen_write_block(ICM20645_REG_LP_CONFIG, databuf, 0x01);
  185. if (res < 0) {
  186. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  187. return ICM20645_ERR_I2C;
  188. }
  189. #else
  190. if (hwmsen_write_byte(client, ICM20645_REG_LP_CONFIG, databuf)) {
  191. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  192. return ICM20645_ERR_I2C;
  193. }
  194. #endif
  195. } else {
  196. databuf[0] &= ~BIT_GYRO_LP_EN;
  197. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  198. res = ICM20645_hwmsen_write_block(ICM20645_REG_LP_CONFIG, databuf, 0x01);
  199. if (res < 0) {
  200. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  201. return ICM20645_ERR_I2C;
  202. }
  203. #else
  204. if (hwmsen_write_byte(client, ICM20645_REG_LP_CONFIG, databuf)) {
  205. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  206. return ICM20645_ERR_I2C;
  207. }
  208. #endif
  209. }
  210. /* all_chip_lp_config */
  211. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  212. res = ICM20645_hwmsen_read_block(ICM20645_REG_PWR_CTL, databuf, 0x01);
  213. if (res < 0) {
  214. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  215. return ICM20645_ERR_I2C;
  216. }
  217. #else
  218. if (hwmsen_read_byte(client, ICM20645_REG_PWR_CTL, databuf)) {
  219. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  220. return ICM20645_ERR_I2C;
  221. }
  222. #endif
  223. if (on == true) {
  224. databuf[0] |= BIT_LP_EN;
  225. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  226. res = ICM20645_hwmsen_write_block(ICM20645_REG_PWR_CTL, databuf, 0x01);
  227. if (res < 0) {
  228. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  229. return ICM20645_ERR_I2C;
  230. }
  231. #else
  232. if (hwmsen_write_byte(client, ICM20645_REG_PWR_CTL, databuf)) {
  233. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  234. return ICM20645_ERR_I2C;
  235. }
  236. #endif
  237. } else {
  238. databuf[0] &= ~BIT_LP_EN;
  239. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  240. res = ICM20645_hwmsen_write_block(ICM20645_REG_PWR_CTL, databuf, 0x01);
  241. if (res < 0) {
  242. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  243. return ICM20645_ERR_I2C;
  244. }
  245. #else
  246. if (hwmsen_write_byte(client, ICM20645_REG_PWR_CTL, databuf)) {
  247. GYRO_LOG("icm20645_lp_mode fail at %x\n", on);
  248. return ICM20645_ERR_I2C;
  249. }
  250. #endif
  251. }
  252. return ICM20645_SUCCESS;
  253. }
  254. static int icm20645_turn_on(struct i2c_client *client, u8 status, bool on)
  255. {
  256. int res;
  257. u8 databuf[2];
  258. memset(databuf, 0, sizeof(databuf));
  259. icm20645_set_bank(client, BANK_SEL_0);
  260. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  261. res = ICM20645_hwmsen_read_block(ICM20645_REG_POWER_CTL2, databuf, 0x01);
  262. if (res < 0) {
  263. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  264. return ICM20645_ERR_I2C;
  265. }
  266. #else
  267. if (hwmsen_read_byte(client, ICM20645_REG_POWER_CTL2, databuf)) {
  268. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  269. return ICM20645_ERR_I2C;
  270. }
  271. #endif
  272. if (on == true) {
  273. databuf[0] &= ~status;
  274. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  275. res = ICM20645_hwmsen_write_block(ICM20645_REG_POWER_CTL2, databuf, 0x01);
  276. if (res < 0) {
  277. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  278. return ICM20645_ERR_I2C;
  279. }
  280. #else
  281. if (hwmsen_write_byte(client, ICM20645_REG_POWER_CTL2, databuf)) {
  282. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  283. return ICM20645_ERR_I2C;
  284. }
  285. #endif
  286. } else {
  287. databuf[0] |= status;
  288. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  289. res = ICM20645_hwmsen_write_block(ICM20645_REG_POWER_CTL2, databuf, 0x01);
  290. if (res < 0) {
  291. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  292. return ICM20645_ERR_I2C;
  293. }
  294. #else
  295. if (hwmsen_write_byte(client, ICM20645_REG_POWER_CTL2, databuf)) {
  296. GYRO_LOG("icm20645_turn_on fail at %x\n", on);
  297. return ICM20645_ERR_I2C;
  298. }
  299. #endif
  300. }
  301. return ICM20645_SUCCESS;
  302. }
  303. /*--------------------gyroscopy power control function----------------------------------*/
  304. static void ICM20645_power(struct gyro_hw *hw, unsigned int on)
  305. {
  306. }
  307. /*----------------------------------------------------------------------------*/
  308. static int ICM20645_write_rel_calibration(struct icm20645_i2c_data *obj, int dat[ICM20645_AXES_NUM])
  309. {
  310. obj->cali_sw[ICM20645_AXIS_X] = obj->cvt.sign[ICM20645_AXIS_X] * dat[obj->cvt.map[ICM20645_AXIS_X]];
  311. obj->cali_sw[ICM20645_AXIS_Y] = obj->cvt.sign[ICM20645_AXIS_Y] * dat[obj->cvt.map[ICM20645_AXIS_Y]];
  312. obj->cali_sw[ICM20645_AXIS_Z] = obj->cvt.sign[ICM20645_AXIS_Z] * dat[obj->cvt.map[ICM20645_AXIS_Z]];
  313. #if DEBUG
  314. if (atomic_read(&obj->trace) & GYRO_TRC_CALI) {
  315. GYRO_LOG("test (%5d, %5d, %5d) ->(%5d, %5d, %5d)->(%5d, %5d, %5d))\n",
  316. obj->cvt.sign[ICM20645_AXIS_X], obj->cvt.sign[ICM20645_AXIS_Y], obj->cvt.sign[ICM20645_AXIS_Z],
  317. dat[ICM20645_AXIS_X], dat[ICM20645_AXIS_Y], dat[ICM20645_AXIS_Z],
  318. obj->cvt.map[ICM20645_AXIS_X], obj->cvt.map[ICM20645_AXIS_Y], obj->cvt.map[ICM20645_AXIS_Z]);
  319. GYRO_LOG("write gyro calibration data (%5d, %5d, %5d)\n",
  320. obj->cali_sw[ICM20645_AXIS_X], obj->cali_sw[ICM20645_AXIS_Y], obj->cali_sw[ICM20645_AXIS_Z]);
  321. }
  322. #endif
  323. return 0;
  324. }
  325. /*----------------------------------------------------------------------------*/
  326. static int ICM20645_ResetCalibration(struct i2c_client *client)
  327. {
  328. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  329. memset(obj->cali_sw, 0x00, sizeof(obj->cali_sw));
  330. return 0;
  331. }
  332. /*----------------------------------------------------------------------------*/
  333. static int ICM20645_ReadCalibration(struct i2c_client *client, int dat[ICM20645_AXES_NUM])
  334. {
  335. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  336. dat[obj->cvt.map[ICM20645_AXIS_X]] = obj->cvt.sign[ICM20645_AXIS_X] * obj->cali_sw[ICM20645_AXIS_X];
  337. dat[obj->cvt.map[ICM20645_AXIS_Y]] = obj->cvt.sign[ICM20645_AXIS_Y] * obj->cali_sw[ICM20645_AXIS_Y];
  338. dat[obj->cvt.map[ICM20645_AXIS_Z]] = obj->cvt.sign[ICM20645_AXIS_Z] * obj->cali_sw[ICM20645_AXIS_Z];
  339. #if DEBUG
  340. if (atomic_read(&obj->trace) & GYRO_TRC_CALI)
  341. GYRO_LOG("Read gyro calibration data (%5d, %5d, %5d)\n",
  342. dat[ICM20645_AXIS_X], dat[ICM20645_AXIS_Y], dat[ICM20645_AXIS_Z]);
  343. #endif
  344. return 0;
  345. }
  346. /*----------------------------------------------------------------------------*/
  347. /*----------------------------------------------------------------------------*/
  348. static int ICM20645_WriteCalibration(struct i2c_client *client, int dat[ICM20645_AXES_NUM])
  349. {
  350. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  351. int err = 0;
  352. int cali[ICM20645_AXES_NUM];
  353. if (!obj || !dat) {
  354. GYRO_ERR("null ptr!!\n");
  355. return -EINVAL;
  356. }
  357. cali[obj->cvt.map[ICM20645_AXIS_X]] = obj->cvt.sign[ICM20645_AXIS_X] * obj->cali_sw[ICM20645_AXIS_X];
  358. cali[obj->cvt.map[ICM20645_AXIS_Y]] = obj->cvt.sign[ICM20645_AXIS_Y] * obj->cali_sw[ICM20645_AXIS_Y];
  359. cali[obj->cvt.map[ICM20645_AXIS_Z]] = obj->cvt.sign[ICM20645_AXIS_Z] * obj->cali_sw[ICM20645_AXIS_Z];
  360. cali[ICM20645_AXIS_X] += dat[ICM20645_AXIS_X];
  361. cali[ICM20645_AXIS_Y] += dat[ICM20645_AXIS_Y];
  362. cali[ICM20645_AXIS_Z] += dat[ICM20645_AXIS_Z];
  363. #if DEBUG
  364. if (atomic_read(&obj->trace) & GYRO_TRC_CALI)
  365. GYRO_LOG("write gyro calibration data (%5d, %5d, %5d)-->(%5d, %5d, %5d)\n",
  366. dat[ICM20645_AXIS_X], dat[ICM20645_AXIS_Y], dat[ICM20645_AXIS_Z],
  367. cali[ICM20645_AXIS_X], cali[ICM20645_AXIS_Y], cali[ICM20645_AXIS_Z]);
  368. #endif
  369. return ICM20645_write_rel_calibration(obj, cali);
  370. return err;
  371. }
  372. static int ICM20645_SetPowerMode(struct i2c_client *client, bool enable)
  373. {
  374. u8 databuf[2] = { 0 };
  375. int res = 0;
  376. icm20645_set_bank(client, BANK_SEL_0);
  377. if (enable == sensor_power) {
  378. GYRO_LOG("Sensor power status is newest!\n");
  379. return ICM20645_SUCCESS;
  380. }
  381. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  382. res = ICM20645_hwmsen_read_block(ICM20645_REG_PWR_CTL, databuf, 0x01);
  383. if (res < 0) {
  384. GYRO_ERR("read power ctl register err!\n");
  385. return ICM20645_ERR_I2C;
  386. }
  387. #else
  388. if (hwmsen_read_byte(client, ICM20645_REG_PWR_CTL, databuf)) {
  389. GYRO_ERR("read power ctl register err!\n");
  390. return ICM20645_ERR_I2C;
  391. }
  392. #endif
  393. databuf[0] &= ~ICM20645_SLEEP;
  394. if (enable == FALSE) {
  395. if (ICM20645_gse_mode() == false)
  396. databuf[0] |= ICM20645_SLEEP;
  397. }
  398. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  399. res = ICM20645_hwmsen_write_block(ICM20645_REG_PWR_CTL, databuf, 0x1);
  400. #else
  401. databuf[1] = databuf[0];
  402. databuf[0] = ICM20645_REG_PWR_CTL;
  403. res = i2c_master_send(client, databuf, 0x2);
  404. #endif
  405. if (res < 0) {
  406. GYRO_LOG("set power mode failed!\n");
  407. return ICM20645_ERR_I2C;
  408. }
  409. GYRO_LOG("set power mode ok %d!\n", enable);
  410. sensor_power = enable;
  411. return ICM20645_SUCCESS;
  412. }
  413. /*----------------------------------------------------------------------------*/
  414. static int ICM20645_SetDataFormat(struct i2c_client *client, u8 dataformat)
  415. {
  416. u8 databuf[2] = { 0 };
  417. int res = 0;
  418. icm20645_set_bank(client, BANK_SEL_2);
  419. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  420. databuf[0] = dataformat;
  421. res = ICM20645_hwmsen_write_block(ICM20645_REG_CFG, databuf, 0x1);
  422. #else
  423. databuf[0] = ICM20645_REG_CFG;
  424. databuf[1] = dataformat;
  425. res = i2c_master_send(client, databuf, 0x2);
  426. #endif
  427. if (res < 0) {
  428. GYRO_ERR("ICM20645_SetDataFormat ERR : 0x%x\n", databuf[0]);
  429. return ICM20645_ERR_I2C;
  430. }
  431. udelay(500);
  432. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  433. res = ICM20645_hwmsen_read_block(ICM20645_REG_CFG, databuf, 0x01);
  434. if (res < 0) {
  435. GYRO_ERR("read data format register err!\n");
  436. return ICM20645_ERR_I2C;
  437. }
  438. GYRO_LOG("read data format: 0x%x\n", databuf[0]);
  439. #else
  440. if (hwmsen_read_byte(client, ICM20645_REG_CFG, databuf)) {
  441. GYRO_ERR("read data format register err!\n");
  442. return ICM20645_ERR_I2C;
  443. }
  444. GYRO_LOG("read data format: 0x%x\n", databuf[0]);
  445. #endif
  446. icm20645_set_bank(client, BANK_SEL_0);
  447. return ICM20645_SUCCESS;
  448. }
  449. static int ICM20645_Setfilter(struct i2c_client *client, int filter_sample)
  450. {
  451. u8 databuf[2] = { 0 };
  452. int res = 0;
  453. icm20645_set_bank(client, BANK_SEL_2);
  454. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  455. databuf[0] = filter_sample;
  456. res = ICM20645_hwmsen_write_block(ICM20645_GYRO_CFG2, databuf, 0x1);
  457. #else
  458. databuf[0] = ICM20645_GYRO_CFG2;
  459. databuf[1] = filter_sample;
  460. res = i2c_master_send(client, databuf, 0x2);
  461. #endif
  462. if (res <= 0) {
  463. GYRO_ERR("write sample rate register err!\n");
  464. return ICM20645_ERR_I2C;
  465. }
  466. icm20645_set_bank(client, BANK_SEL_0);
  467. return ICM20645_SUCCESS;
  468. }
  469. static int ICM20645_SetSampleRate(struct i2c_client *client, int sample_rate)
  470. {
  471. u8 databuf[2] = { 0 };
  472. int rate_div = 0;
  473. int res = 0;
  474. rate_div = 1125 / sample_rate - 1;
  475. icm20645_set_bank(client, BANK_SEL_2);
  476. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  477. databuf[0] = rate_div;
  478. res = ICM20645_hwmsen_write_block(ICM20645_REG_SAMRT_DIV, databuf, 0x1);
  479. #else
  480. databuf[0] = ICM20645_REG_SAMRT_DIV;
  481. databuf[1] = rate_div;
  482. res = i2c_master_send(client, databuf, 0x2);
  483. #endif
  484. if (res <= 0) {
  485. GYRO_ERR("write sample rate register err!\n");
  486. return ICM20645_ERR_I2C;
  487. }
  488. icm20645_set_bank(client, BANK_SEL_0);
  489. return ICM20645_SUCCESS;
  490. }
  491. /*----------------------------------------------------------------------------*/
  492. static int ICM20645_ReadGyroData(struct i2c_client *client, char *buf, int bufsize)
  493. {
  494. char databuf[6];
  495. int data[3];
  496. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  497. if (sensor_power == false) {
  498. ICM20645_SetPowerMode(client, true);
  499. msleep(50);
  500. }
  501. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  502. if (ICM20645_hwmsen_read_block(ICM20645_REG_GYRO_XH, databuf, 6)) {
  503. GYRO_ERR("ICM20645 read gyroscope data error\n");
  504. return -2;
  505. }
  506. #endif
  507. obj->data[ICM20645_AXIS_X] =
  508. ((s16) ((databuf[ICM20645_AXIS_X * 2 + 1]) | (databuf[ICM20645_AXIS_X * 2] << 8)));
  509. obj->data[ICM20645_AXIS_Y] =
  510. ((s16) ((databuf[ICM20645_AXIS_Y * 2 + 1]) | (databuf[ICM20645_AXIS_Y * 2] << 8)));
  511. obj->data[ICM20645_AXIS_Z] =
  512. ((s16) ((databuf[ICM20645_AXIS_Z * 2 + 1]) | (databuf[ICM20645_AXIS_Z * 2] << 8)));
  513. #if DEBUG
  514. if (atomic_read(&obj->trace) & GYRO_TRC_RAWDATA) {
  515. GYRO_LOG("read gyro register: %d, %d, %d, %d, %d, %d",
  516. databuf[0], databuf[1], databuf[2], databuf[3], databuf[4], databuf[5]);
  517. GYRO_LOG("get gyro raw data (0x%08X, 0x%08X, 0x%08X) -> (%5d, %5d, %5d)\n",
  518. obj->data[ICM20645_AXIS_X], obj->data[ICM20645_AXIS_Y], obj->data[ICM20645_AXIS_Z],
  519. obj->data[ICM20645_AXIS_X], obj->data[ICM20645_AXIS_Y], obj->data[ICM20645_AXIS_Z]);
  520. }
  521. #endif
  522. obj->data[ICM20645_AXIS_X] = obj->data[ICM20645_AXIS_X] + obj->cali_sw[ICM20645_AXIS_X];
  523. obj->data[ICM20645_AXIS_Y] = obj->data[ICM20645_AXIS_Y] + obj->cali_sw[ICM20645_AXIS_Y];
  524. obj->data[ICM20645_AXIS_Z] = obj->data[ICM20645_AXIS_Z] + obj->cali_sw[ICM20645_AXIS_Z];
  525. /*remap coordinate */
  526. data[obj->cvt.map[ICM20645_AXIS_X]] = obj->cvt.sign[ICM20645_AXIS_X] * obj->data[ICM20645_AXIS_X];
  527. data[obj->cvt.map[ICM20645_AXIS_Y]] = obj->cvt.sign[ICM20645_AXIS_Y] * obj->data[ICM20645_AXIS_Y];
  528. data[obj->cvt.map[ICM20645_AXIS_Z]] = obj->cvt.sign[ICM20645_AXIS_Z] * obj->data[ICM20645_AXIS_Z];
  529. data[ICM20645_AXIS_X] = data[ICM20645_AXIS_X] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  530. data[ICM20645_AXIS_Y] = data[ICM20645_AXIS_Y] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  531. data[ICM20645_AXIS_Z] = data[ICM20645_AXIS_Z] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  532. sprintf(buf, "%04x %04x %04x", data[ICM20645_AXIS_X], data[ICM20645_AXIS_Y], data[ICM20645_AXIS_Z]);
  533. #if DEBUG
  534. if (atomic_read(&obj->trace) & GYRO_TRC_DATA)
  535. GYRO_LOG("get gyro data packet:[%d %d %d]\n", data[0], data[1], data[2]);
  536. #endif
  537. return 0;
  538. }
  539. /*----------------------------------------------------------------------------*/
  540. static int ICM20645_ReadChipInfo(struct i2c_client *client, char *buf, int bufsize)
  541. {
  542. u8 databuf[10];
  543. memset(databuf, 0, sizeof(u8) * 10);
  544. if ((NULL == buf) || (bufsize <= 30))
  545. return -1;
  546. if (NULL == client) {
  547. *buf = 0;
  548. return -2;
  549. }
  550. sprintf(buf, "ICM20645 Chip");
  551. return 0;
  552. }
  553. /*----------------------------------------------------------------------------*/
  554. static int ICM20645_ReadAllReg(struct i2c_client *client, char *buf, int bufsize)
  555. {
  556. u8 total_len = 8;
  557. u8 addr = 0;
  558. u8 buff[total_len + 1];
  559. int err = 0;
  560. int i;
  561. if (sensor_power == FALSE) {
  562. err = ICM20645_SetPowerMode(client, true);
  563. if (err)
  564. GYRO_ERR("Power on mpu6050 error %d!\n", err);
  565. msleep(50);
  566. }
  567. icm20645_set_bank(client, BANK_SEL_0);
  568. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  569. if (ICM20645_hwmsen_read_block(addr, buff, total_len)) {
  570. GYRO_ERR("ICM20645_ReadAllReg err!\n");
  571. return ICM20645_ERR_I2C;
  572. }
  573. #endif
  574. for (i = 0; i <= total_len; i++)
  575. GYRO_ERR("ICM20645 bank0 reg=0x%x, data=0x%x\n", (addr + i), buff[i]);
  576. icm20645_set_bank(client, BANK_SEL_2);
  577. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  578. if (ICM20645_hwmsen_read_block(addr, buff, total_len)) {
  579. GYRO_ERR("ICM20645_ReadAllReg err!\n");
  580. return ICM20645_ERR_I2C;
  581. }
  582. #endif
  583. for (i = 0; i <= total_len; i++)
  584. GYRO_ERR("ICM20645 bank2 reg=0x%x, data=0x%x\n", (addr + i), buff[i]);
  585. icm20645_set_bank(client, BANK_SEL_0);
  586. return 0;
  587. }
  588. /*----------------------------------------------------------------------------*/
  589. static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
  590. {
  591. struct i2c_client *client = icm20645_i2c_client;
  592. char strbuf[ICM20645_BUFSIZE];
  593. if (NULL == client) {
  594. GYRO_ERR("i2c client is null!!\n");
  595. return 0;
  596. }
  597. ICM20645_ReadAllReg(client, strbuf, ICM20645_BUFSIZE);
  598. ICM20645_ReadChipInfo(client, strbuf, ICM20645_BUFSIZE);
  599. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  600. }
  601. /*----------------------------------------------------------------------------*/
  602. static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
  603. {
  604. struct i2c_client *client = icm20645_i2c_client;
  605. char strbuf[ICM20645_BUFSIZE];
  606. if (NULL == client) {
  607. GYRO_ERR("i2c client is null!!\n");
  608. return 0;
  609. }
  610. ICM20645_ReadGyroData(client, strbuf, ICM20645_BUFSIZE);
  611. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  612. }
  613. /*----------------------------------------------------------------------------*/
  614. static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
  615. {
  616. ssize_t res;
  617. struct icm20645_i2c_data *obj = obj_i2c_data;
  618. if (obj == NULL) {
  619. GYRO_ERR("i2c_data obj is null!!\n");
  620. return 0;
  621. }
  622. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
  623. return res;
  624. }
  625. /*----------------------------------------------------------------------------*/
  626. static ssize_t store_trace_value(struct device_driver *ddri, const char *buf, size_t count)
  627. {
  628. struct icm20645_i2c_data *obj = obj_i2c_data;
  629. int trace;
  630. if (obj == NULL) {
  631. GYRO_ERR("i2c_data obj is null!!\n");
  632. return 0;
  633. }
  634. if (1 == sscanf(buf, "0x%x", &trace))
  635. atomic_set(&obj->trace, trace);
  636. else
  637. GYRO_ERR("invalid content: '%s', length = %zu\n", buf, count);
  638. return count;
  639. }
  640. /*----------------------------------------------------------------------------*/
  641. static ssize_t show_status_value(struct device_driver *ddri, char *buf)
  642. {
  643. ssize_t len = 0;
  644. struct icm20645_i2c_data *obj = obj_i2c_data;
  645. if (obj == NULL) {
  646. GYRO_ERR("i2c_data obj is null!!\n");
  647. return 0;
  648. }
  649. if (obj->hw)
  650. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: %d %d (%d %d)\n",
  651. obj->hw->i2c_num, obj->hw->direction, obj->hw->power_id, obj->hw->power_vol);
  652. else
  653. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: NULL\n");
  654. return len;
  655. }
  656. static ssize_t show_chip_orientation(struct device_driver *ddri, char *buf)
  657. {
  658. ssize_t _tLength = 0;
  659. struct gyro_hw *_ptAccelHw = hw;
  660. GYRO_LOG("[%s] default direction: %d\n", __func__, _ptAccelHw->direction);
  661. _tLength = snprintf(buf, PAGE_SIZE, "default direction = %d\n", _ptAccelHw->direction);
  662. return _tLength;
  663. }
  664. static ssize_t store_chip_orientation(struct device_driver *ddri, const char *buf, size_t tCount)
  665. {
  666. int _nDirection = 0, ret = 0;
  667. struct icm20645_i2c_data *_pt_i2c_obj = obj_i2c_data;
  668. if (NULL == _pt_i2c_obj)
  669. return 0;
  670. ret = kstrtoint(buf, 10, &_nDirection);
  671. if (ret == 0) {
  672. if (hwmsen_get_convert(_nDirection, &_pt_i2c_obj->cvt))
  673. GYRO_ERR("ERR: fail to set direction\n");
  674. }
  675. GYRO_LOG("[%s] set direction: %d\n", __func__, _nDirection);
  676. return tCount;
  677. }
  678. /*----------------------------------------------------------------------------*/
  679. static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
  680. static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
  681. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, show_trace_value, store_trace_value);
  682. static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
  683. static DRIVER_ATTR(orientation, S_IWUSR | S_IRUGO, show_chip_orientation, store_chip_orientation);
  684. /*----------------------------------------------------------------------------*/
  685. static struct driver_attribute *ICM20645_attr_list[] = {
  686. &driver_attr_chipinfo, /*chip information */
  687. &driver_attr_sensordata, /*dump sensor data */
  688. &driver_attr_trace, /*trace log */
  689. &driver_attr_status,
  690. &driver_attr_orientation,
  691. };
  692. /*----------------------------------------------------------------------------*/
  693. static int icm20645_create_attr(struct device_driver *driver)
  694. {
  695. int idx, err = 0;
  696. int num = (int)(sizeof(ICM20645_attr_list) / sizeof(ICM20645_attr_list[0]));
  697. if (driver == NULL)
  698. return -EINVAL;
  699. for (idx = 0; idx < num; idx++) {
  700. err = driver_create_file(driver, ICM20645_attr_list[idx]);
  701. if (0 != err) {
  702. GYRO_ERR("driver_create_file (%s) = %d\n", ICM20645_attr_list[idx]->attr.name, err);
  703. break;
  704. }
  705. }
  706. return err;
  707. }
  708. /*----------------------------------------------------------------------------*/
  709. static int icm20645_delete_attr(struct device_driver *driver)
  710. {
  711. int idx, err = 0;
  712. int num = (int)(sizeof(ICM20645_attr_list) / sizeof(ICM20645_attr_list[0]));
  713. if (driver == NULL)
  714. return -EINVAL;
  715. for (idx = 0; idx < num; idx++)
  716. driver_remove_file(driver, ICM20645_attr_list[idx]);
  717. return err;
  718. }
  719. /*----------------------------------------------------------------------------*/
  720. static int icm20645_gpio_config(void)
  721. {
  722. int ret;
  723. struct pinctrl *pinctrl;
  724. struct pinctrl_state *pins_default;
  725. struct pinctrl_state *pins_cfg;
  726. pinctrl = devm_pinctrl_get(&gyroPltFmDev->dev);
  727. if (IS_ERR(pinctrl)) {
  728. ret = PTR_ERR(pinctrl);
  729. GYRO_ERR("Cannot find gyro pinctrl!\n");
  730. return ret;
  731. }
  732. pins_default = pinctrl_lookup_state(pinctrl, "pin_default");
  733. if (IS_ERR(pins_default)) {
  734. ret = PTR_ERR(pins_default);
  735. GYRO_ERR("Cannot find gyro pinctrl default!\n");
  736. }
  737. pins_cfg = pinctrl_lookup_state(pinctrl, "pin_cfg");
  738. if (IS_ERR(pins_cfg)) {
  739. ret = PTR_ERR(pins_cfg);
  740. GYRO_ERR("Cannot find gyro pinctrl pin_cfg!\n");
  741. return ret;
  742. }
  743. pinctrl_select_state(pinctrl, pins_cfg);
  744. return 0;
  745. }
  746. static int icm20645_init_client(struct i2c_client *client, bool enable)
  747. {
  748. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  749. int res = 0;
  750. res = icm20645_gpio_config();
  751. if (res != ICM20645_SUCCESS) {
  752. GYRO_ERR("icm20645_gpio_config ERR!\n");
  753. return res;
  754. }
  755. res = ICM20645_SetPowerMode(client, true);
  756. if (res != ICM20645_SUCCESS) {
  757. GYRO_ERR("ICM20645_SetPowerMode ERR!\n");
  758. return res;
  759. }
  760. res = ICM20645_SetDataFormat(client, (GYRO_DLPFCFG | GYRO_FS_SEL | GYRO_FCHOICE));
  761. if (res != ICM20645_SUCCESS) {
  762. GYRO_ERR("ICM20645_SetDataFormat ERR!\n");
  763. return res;
  764. }
  765. res = ICM20645_SetSampleRate(client, 125);
  766. if (res != ICM20645_SUCCESS) {
  767. GYRO_ERR("ICM20645_SetSampleRate ERR!\n");
  768. return res;
  769. }
  770. res = ICM20645_Setfilter(client, GYRO_AVGCFG_8X);
  771. if (res != ICM20645_SUCCESS) {
  772. GYRO_ERR("ICM20645_Setfilter ERR!\n");
  773. return res;
  774. }
  775. #ifdef CONFIG_ICM20645_LOWPASS
  776. memset(&obj->fir, 0x00, sizeof(obj->fir));
  777. #endif
  778. icm20645_turn_on(client, BIT_PWR_GYRO_STBY, true);
  779. if (res != ICM20645_SUCCESS) {
  780. GYRO_ERR("icm20645_turn_on ERR!\n");
  781. return res;
  782. }
  783. icm20645_lp_mode(client, true);
  784. if (res != ICM20645_SUCCESS) {
  785. GYRO_ERR("icm20645_lp_mode ERR!\n");
  786. return res;
  787. }
  788. res = ICM20645_SetPowerMode(client, enable);
  789. if (res != ICM20645_SUCCESS) {
  790. GYRO_ERR("ICM20645_SetPowerMode ERR!\n");
  791. return res;
  792. }
  793. icm20645_set_bank(client, BANK_SEL_0);
  794. GYRO_LOG("icm20645_init_client OK!\n");
  795. return ICM20645_SUCCESS;
  796. }
  797. static int icm20645_open(struct inode *inode, struct file *file)
  798. {
  799. file->private_data = icm20645_i2c_client;
  800. if (file->private_data == NULL) {
  801. GYRO_ERR("null pointer!!\n");
  802. return -EINVAL;
  803. }
  804. return nonseekable_open(inode, file);
  805. }
  806. /*----------------------------------------------------------------------------*/
  807. static int icm20645_release(struct inode *inode, struct file *file)
  808. {
  809. file->private_data = NULL;
  810. return 0;
  811. }
  812. /*----------------------------------------------------------------------------*/
  813. static long icm20645_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  814. {
  815. struct i2c_client *client = (struct i2c_client *)file->private_data;
  816. char strbuf[ICM20645_BUFSIZE] = { 0 };
  817. void __user *data;
  818. long err = 0;
  819. struct SENSOR_DATA sensor_data;
  820. int cali[3];
  821. int smtRes = 0;
  822. int copy_cnt = 0;
  823. if (_IOC_DIR(cmd) & _IOC_READ)
  824. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  825. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  826. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  827. if (err) {
  828. GYRO_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  829. return -EFAULT;
  830. }
  831. switch (cmd) {
  832. case GYROSCOPE_IOCTL_INIT:
  833. icm20645_init_client(client, false);
  834. break;
  835. case GYROSCOPE_IOCTL_SMT_DATA:
  836. data = (void __user *)arg;
  837. if (data == NULL) {
  838. err = -EINVAL;
  839. break;
  840. }
  841. copy_cnt = copy_to_user(data, &smtRes, sizeof(smtRes));
  842. if (copy_cnt) {
  843. err = -EFAULT;
  844. GYRO_ERR("copy gyro data to user failed!\n");
  845. }
  846. err = 0;
  847. break;
  848. case GYROSCOPE_IOCTL_READ_SENSORDATA:
  849. data = (void __user *)arg;
  850. if (data == NULL) {
  851. err = -EINVAL;
  852. break;
  853. }
  854. ICM20645_ReadGyroData(client, strbuf, ICM20645_BUFSIZE);
  855. if (copy_to_user(data, strbuf, sizeof(strbuf))) {
  856. err = -EFAULT;
  857. break;
  858. }
  859. break;
  860. case GYROSCOPE_IOCTL_SET_CALI:
  861. data = (void __user *)arg;
  862. if (data == NULL) {
  863. err = -EINVAL;
  864. break;
  865. }
  866. if (copy_from_user(&sensor_data, data, sizeof(sensor_data))) {
  867. err = -EFAULT;
  868. break;
  869. }
  870. else {
  871. cali[ICM20645_AXIS_X] = sensor_data.x * ICM20645_DEFAULT_LSB / ICM20645_FS_MAX_LSB;
  872. cali[ICM20645_AXIS_Y] = sensor_data.y * ICM20645_DEFAULT_LSB / ICM20645_FS_MAX_LSB;
  873. cali[ICM20645_AXIS_Z] = sensor_data.z * ICM20645_DEFAULT_LSB / ICM20645_FS_MAX_LSB;
  874. GYRO_LOG("gyro set cali:[%5d %5d %5d]\n",
  875. cali[ICM20645_AXIS_X], cali[ICM20645_AXIS_Y], cali[ICM20645_AXIS_Z]);
  876. err = ICM20645_WriteCalibration(client, cali);
  877. }
  878. break;
  879. case GYROSCOPE_IOCTL_CLR_CALI:
  880. err = ICM20645_ResetCalibration(client);
  881. break;
  882. case GYROSCOPE_IOCTL_GET_CALI:
  883. data = (void __user *)arg;
  884. if (data == NULL) {
  885. err = -EINVAL;
  886. break;
  887. }
  888. err = ICM20645_ReadCalibration(client, cali);
  889. if (err)
  890. break;
  891. sensor_data.x = cali[ICM20645_AXIS_X] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  892. sensor_data.y = cali[ICM20645_AXIS_Y] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  893. sensor_data.z = cali[ICM20645_AXIS_Z] * ICM20645_FS_MAX_LSB / ICM20645_DEFAULT_LSB;
  894. if (copy_to_user(data, &sensor_data, sizeof(sensor_data))) {
  895. err = -EFAULT;
  896. break;
  897. }
  898. break;
  899. default:
  900. GYRO_ERR("unknown IOCTL: 0x%08x\n", cmd);
  901. err = -ENOIOCTLCMD;
  902. break;
  903. }
  904. return err;
  905. }
  906. #ifdef CONFIG_COMPAT
  907. static long icm20645_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  908. {
  909. long ret;
  910. void __user *arg32 = compat_ptr(arg);
  911. if (!file->f_op || !file->f_op->unlocked_ioctl)
  912. return -ENOTTY;
  913. switch (cmd) {
  914. case COMPAT_GYROSCOPE_IOCTL_INIT:
  915. if (arg32 == NULL) {
  916. GYRO_ERR("invalid argument.");
  917. return -EINVAL;
  918. }
  919. ret = file->f_op->unlocked_ioctl(file, GYROSCOPE_IOCTL_INIT, (unsigned long)arg32);
  920. if (ret) {
  921. GYRO_ERR("GYROSCOPE_IOCTL_INIT unlocked_ioctl failed.\n");
  922. return ret;
  923. }
  924. break;
  925. case COMPAT_GYROSCOPE_IOCTL_SET_CALI:
  926. if (arg32 == NULL) {
  927. GYRO_ERR("invalid argument.");
  928. return -EINVAL;
  929. }
  930. ret = file->f_op->unlocked_ioctl(file, GYROSCOPE_IOCTL_SET_CALI, (unsigned long)arg32);
  931. if (ret) {
  932. GYRO_ERR("GYROSCOPE_IOCTL_SET_CALI unlocked_ioctl failed.\n");
  933. return ret;
  934. }
  935. break;
  936. case COMPAT_GYROSCOPE_IOCTL_CLR_CALI:
  937. if (arg32 == NULL) {
  938. GYRO_ERR("invalid argument.");
  939. return -EINVAL;
  940. }
  941. ret = file->f_op->unlocked_ioctl(file, GYROSCOPE_IOCTL_CLR_CALI, (unsigned long)arg32);
  942. if (ret) {
  943. GYRO_ERR("GYROSCOPE_IOCTL_CLR_CALI unlocked_ioctl failed.\n");
  944. return ret;
  945. }
  946. break;
  947. case COMPAT_GYROSCOPE_IOCTL_GET_CALI:
  948. if (arg32 == NULL) {
  949. GYRO_ERR("invalid argument.");
  950. return -EINVAL;
  951. }
  952. ret = file->f_op->unlocked_ioctl(file, GYROSCOPE_IOCTL_GET_CALI, (unsigned long)arg32);
  953. if (ret) {
  954. GYRO_ERR("GYROSCOPE_IOCTL_GET_CALI unlocked_ioctl failed.\n");
  955. return ret;
  956. }
  957. break;
  958. case COMPAT_GYROSCOPE_IOCTL_READ_SENSORDATA:
  959. if (arg32 == NULL) {
  960. GYRO_ERR("invalid argument.");
  961. return -EINVAL;
  962. }
  963. ret = file->f_op->unlocked_ioctl(file, GYROSCOPE_IOCTL_READ_SENSORDATA, (unsigned long)arg32);
  964. if (ret) {
  965. GYRO_ERR("GYROSCOPE_IOCTL_READ_SENSORDATA unlocked_ioctl failed.\n");
  966. return ret;
  967. }
  968. break;
  969. default:
  970. GYRO_ERR("%s not supported = 0x%04x\n", __func__, cmd);
  971. ret = -ENOIOCTLCMD;
  972. break;
  973. }
  974. return ret;
  975. }
  976. #endif
  977. /*----------------------------------------------------------------------------*/
  978. static const struct file_operations icm20645_fops = {
  979. .open = icm20645_open,
  980. .release = icm20645_release,
  981. .unlocked_ioctl = icm20645_unlocked_ioctl,
  982. #ifdef CONFIG_COMPAT
  983. .compat_ioctl = icm20645_compat_ioctl,
  984. #endif
  985. };
  986. /*----------------------------------------------------------------------------*/
  987. static struct miscdevice icm20645_device = {
  988. .minor = MISC_DYNAMIC_MINOR,
  989. .name = "gyroscope",
  990. .fops = &icm20645_fops,
  991. };
  992. static int icm20645_suspend(struct i2c_client *client, pm_message_t msg)
  993. {
  994. int err = 0;
  995. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  996. if (msg.event == PM_EVENT_SUSPEND) {
  997. if (obj == NULL) {
  998. GYRO_ERR("null pointer!!\n");
  999. return -EINVAL;
  1000. }
  1001. atomic_set(&obj->suspend, 1);
  1002. err = ICM20645_SetPowerMode(client, false);
  1003. if (err <= 0)
  1004. return err;
  1005. }
  1006. return err;
  1007. }
  1008. static int icm20645_resume(struct i2c_client *client)
  1009. {
  1010. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  1011. int err;
  1012. if (obj == NULL) {
  1013. GYRO_ERR("null pointer!!\n");
  1014. return -EINVAL;
  1015. }
  1016. ICM20645_power(obj->hw, 1);
  1017. err = icm20645_init_client(client, false);
  1018. if (err) {
  1019. GYRO_ERR("initialize client fail!!\n");
  1020. return err;
  1021. }
  1022. atomic_set(&obj->suspend, 0);
  1023. return 0;
  1024. }
  1025. static int icm20645_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  1026. {
  1027. strcpy(info->type, ICM20645_DEV_NAME);
  1028. return 0;
  1029. }
  1030. static int icm20645_open_report_data(int open)
  1031. {
  1032. return 0;
  1033. }
  1034. static int icm20645_enable_nodata(int en)
  1035. {
  1036. int res = 0;
  1037. int retry = 0;
  1038. bool power = false;
  1039. if (1 == en)
  1040. power = true;
  1041. if (0 == en)
  1042. power = false;
  1043. for (retry = 0; retry < 3; retry++) {
  1044. res = ICM20645_SetPowerMode(obj_i2c_data->client, power);
  1045. if (res == 0) {
  1046. GYRO_LOG("ICM20645_SetPowerMode done\n");
  1047. break;
  1048. }
  1049. GYRO_LOG("ICM20645_SetPowerMode fail\n");
  1050. }
  1051. if (res != ICM20645_SUCCESS) {
  1052. GYRO_LOG("ICM20645_SetPowerMode fail!\n");
  1053. return -1;
  1054. }
  1055. GYRO_LOG("icm20645_enable_nodata OK!\n");
  1056. return 0;
  1057. }
  1058. static int icm20645_set_delay(u64 ns)
  1059. {
  1060. return 0;
  1061. }
  1062. static int icm20645_get_data(int *x, int *y, int *z, int *status)
  1063. {
  1064. char buff[ICM20645_BUFSIZE];
  1065. ICM20645_ReadGyroData(obj_i2c_data->client, buff, ICM20645_BUFSIZE);
  1066. if (3 == sscanf(buff, "%x %x %x", x, y, z))
  1067. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1068. return 0;
  1069. }
  1070. /*----------------------------------------------------------------------------*/
  1071. static int icm20645_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1072. {
  1073. struct i2c_client *new_client;
  1074. struct icm20645_i2c_data *obj;
  1075. int err = 0;
  1076. struct gyro_control_path ctl = { 0 };
  1077. struct gyro_data_path data = { 0 };
  1078. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  1079. if (!obj) {
  1080. err = -ENOMEM;
  1081. goto exit;
  1082. }
  1083. memset(obj, 0, sizeof(struct icm20645_i2c_data));
  1084. obj->hw = hw;
  1085. err = hwmsen_get_convert(obj->hw->direction, &obj->cvt);
  1086. if (err) {
  1087. GYRO_ERR("invalid direction: %d\n", obj->hw->direction);
  1088. goto exit;
  1089. }
  1090. GYRO_LOG("gyro_default_i2c_addr: %x\n", client->addr);
  1091. #ifdef ICM20645_ACCESS_BY_GSE_I2C
  1092. obj->hw->addr = ICM20645_I2C_SLAVE_ADDR; /* mtk i2c not allow to probe two same address */
  1093. #endif
  1094. GYRO_LOG("gyro_custom_i2c_addr: %x\n", obj->hw->addr);
  1095. if (0 != obj->hw->addr) {
  1096. client->addr = obj->hw->addr >> 1;
  1097. GYRO_LOG("gyro_use_i2c_addr: %x\n", client->addr);
  1098. }
  1099. obj_i2c_data = obj;
  1100. obj->client = client;
  1101. new_client = obj->client;
  1102. i2c_set_clientdata(new_client, obj);
  1103. atomic_set(&obj->trace, 0);
  1104. atomic_set(&obj->suspend, 0);
  1105. icm20645_i2c_client = new_client;
  1106. err = icm20645_init_client(new_client, false);
  1107. if (err)
  1108. goto exit_init_failed;
  1109. err = misc_register(&icm20645_device);
  1110. if (err) {
  1111. GYRO_ERR("icm20645_device misc register failed!\n");
  1112. goto exit_misc_device_register_failed;
  1113. }
  1114. ctl.is_use_common_factory = false;
  1115. err = icm20645_create_attr(&(icm20645_init_info.platform_diver_addr->driver));
  1116. if (err) {
  1117. GYRO_ERR("icm20645 create attribute err = %d\n", err);
  1118. goto exit_create_attr_failed;
  1119. }
  1120. ctl.open_report_data = icm20645_open_report_data;
  1121. ctl.enable_nodata = icm20645_enable_nodata;
  1122. ctl.set_delay = icm20645_set_delay;
  1123. ctl.is_report_input_direct = false;
  1124. ctl.is_support_batch = obj->hw->is_batch_supported;
  1125. err = gyro_register_control_path(&ctl);
  1126. if (err) {
  1127. GYRO_ERR("register gyro control path err\n");
  1128. goto exit_kfree;
  1129. }
  1130. data.get_data = icm20645_get_data;
  1131. data.vender_div = DEGREE_TO_RAD;
  1132. err = gyro_register_data_path(&data);
  1133. if (err) {
  1134. GYRO_ERR("gyro_register_data_path fail = %d\n", err);
  1135. goto exit_kfree;
  1136. }
  1137. #ifdef CONFIG_HAS_EARLYSUSPEND
  1138. obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 2,
  1139. obj->early_drv.suspend = icm20645_early_suspend,
  1140. obj->early_drv.resume = icm20645_late_resume, register_early_suspend(&obj->early_drv);
  1141. #endif
  1142. icm20645_init_flag = 0;
  1143. GYRO_LOG("%s: OK\n", __func__);
  1144. return 0;
  1145. exit_create_attr_failed:
  1146. misc_deregister(&icm20645_device);
  1147. exit_misc_device_register_failed:
  1148. exit_init_failed:
  1149. /*i2c_detach_client(new_client);*/
  1150. exit_kfree:
  1151. kfree(obj);
  1152. exit:
  1153. icm20645_init_flag = -1;
  1154. GYRO_ERR("%s: err = %d\n", __func__, err);
  1155. return err;
  1156. }
  1157. /*----------------------------------------------------------------------------*/
  1158. static int icm20645_i2c_remove(struct i2c_client *client)
  1159. {
  1160. int err = 0;
  1161. err = icm20645_delete_attr(&(icm20645_init_info.platform_diver_addr->driver));
  1162. if (err)
  1163. GYRO_ERR("icm20645_delete_attr fail: %d\n", err);
  1164. err = misc_deregister(&icm20645_device);
  1165. if (err)
  1166. GYRO_ERR("misc_deregister fail: %d\n", err);
  1167. icm20645_i2c_client = NULL;
  1168. i2c_unregister_device(client);
  1169. kfree(i2c_get_clientdata(client));
  1170. return 0;
  1171. }
  1172. /*----------------------------------------------------------------------------*/
  1173. static int icm20645_remove(void)
  1174. {
  1175. ICM20645_power(hw, 0);
  1176. i2c_del_driver(&icm20645_i2c_driver);
  1177. return 0;
  1178. }
  1179. /*----------------------------------------------------------------------------*/
  1180. static int icm20645_local_init(struct platform_device *pdev)
  1181. {
  1182. gyroPltFmDev = pdev;
  1183. ICM20645_power(hw, 1);
  1184. if (i2c_add_driver(&icm20645_i2c_driver)) {
  1185. GYRO_ERR("add driver error\n");
  1186. return -1;
  1187. }
  1188. if (-1 == icm20645_init_flag)
  1189. return -1;
  1190. return 0;
  1191. }
  1192. /*----------------------------------------------------------------------------*/
  1193. static int __init icm20645_init(void)
  1194. {
  1195. const char *name = "mediatek,icm20645gy";
  1196. hw = get_gyro_dts_func(name, hw);
  1197. if (!hw)
  1198. GYRO_ERR("get dts info fail\n");
  1199. gyro_driver_add(&icm20645_init_info);
  1200. return 0;
  1201. }
  1202. /*----------------------------------------------------------------------------*/
  1203. static void __exit icm20645_exit(void)
  1204. {
  1205. }
  1206. /*----------------------------------------------------------------------------*/
  1207. module_init(icm20645_init);
  1208. module_exit(icm20645_exit);
  1209. /*----------------------------------------------------------------------------*/
  1210. MODULE_LICENSE("GPL");
  1211. MODULE_DESCRIPTION("ICM20645 gyroscope driver");
  1212. MODULE_AUTHOR("Yucong.Xiong@mediatek.com");