icm20645.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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_acc.h>
  14. #include "icm20645.h"
  15. #include <accel.h>
  16. #include <hwmsensor.h>
  17. static DEFINE_MUTEX(icm20645_i2c_mutex);
  18. /* Maintain cust info here */
  19. struct acc_hw accel_cust;
  20. static struct acc_hw *hw = &accel_cust;
  21. /* For driver get cust info */
  22. struct acc_hw *get_cust_acc(void)
  23. {
  24. return &accel_cust;
  25. }
  26. /*----------------------------------------------------------------------------*/
  27. #define DEBUG 1
  28. /*----------------------------------------------------------------------------*/
  29. #define CONFIG_ICM20645_LOWPASS /*apply low pass filter on output */
  30. #define SW_CALIBRATION
  31. /*----------------------------------------------------------------------------*/
  32. #define ICM20645_AXIS_X 0
  33. #define ICM20645_AXIS_Y 1
  34. #define ICM20645_AXIS_Z 2
  35. #define ICM20645_AXES_NUM 3
  36. #define ICM20645_DATA_LEN 6
  37. #define ICM20645_DEV_NAME "ICM20645G" /* name must different with gyro icm20645 */
  38. /*----------------------------------------------------------------------------*/
  39. static const struct i2c_device_id icm20645_i2c_id[] = { {ICM20645_DEV_NAME, 0}, {} };
  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. #ifndef USE_EARLY_SUSPEND
  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(void);
  49. static int icm20645_remove(void);
  50. static int icm20645_init_flag = -1;
  51. static struct acc_init_info icm20645_init_info = {
  52. .name = "icm20645g",
  53. .init = icm20645_local_init,
  54. .uninit = icm20645_remove,
  55. };
  56. /*----------------------------------------------------------------------------*/
  57. typedef enum {
  58. ICM20645_TRC_FILTER = 0x01,
  59. ICM20645_TRC_RAWDATA = 0x02,
  60. ICM20645_TRC_IOCTL = 0x04,
  61. ICM20645_TRC_CALI = 0X08,
  62. ICM20645_TRC_INFO = 0X10,
  63. } ICM20645_TRC;
  64. /*----------------------------------------------------------------------------*/
  65. struct scale_factor {
  66. u8 whole;
  67. u8 fraction;
  68. };
  69. /*----------------------------------------------------------------------------*/
  70. struct data_resolution {
  71. struct scale_factor scalefactor;
  72. int sensitivity;
  73. };
  74. /*----------------------------------------------------------------------------*/
  75. #define C_MAX_FIR_LENGTH (32)
  76. /*----------------------------------------------------------------------------*/
  77. struct data_filter {
  78. s16 raw[C_MAX_FIR_LENGTH][ICM20645_AXES_NUM];
  79. int sum[ICM20645_AXES_NUM];
  80. int num;
  81. int idx;
  82. };
  83. /*----------------------------------------------------------------------------*/
  84. struct icm20645_i2c_data {
  85. struct i2c_client *client;
  86. struct acc_hw *hw;
  87. struct hwmsen_convert cvt;
  88. /*misc */
  89. struct data_resolution *reso;
  90. atomic_t trace;
  91. atomic_t suspend;
  92. atomic_t selftest;
  93. atomic_t filter;
  94. s16 cali_sw[ICM20645_AXES_NUM + 1];
  95. /*data */
  96. s8 offset[ICM20645_AXES_NUM + 1]; /*+1: for 4-byte alignment */
  97. s16 data[ICM20645_AXES_NUM + 1];
  98. #if defined(CONFIG_ICM20645_LOWPASS)
  99. atomic_t firlen;
  100. atomic_t fir_en;
  101. struct data_filter fir;
  102. #endif
  103. /*early suspend */
  104. #if defined(USE_EARLY_SUSPEND)
  105. struct early_suspend early_drv;
  106. #endif
  107. u8 bandwidth;
  108. };
  109. /*----------------------------------------------------------------------------*/
  110. #ifdef CONFIG_OF
  111. static const struct of_device_id accel_of_match[] = {
  112. {.compatible = "mediatek,gsensor"},
  113. {},
  114. };
  115. #endif
  116. static struct i2c_driver icm20645_i2c_driver = {
  117. .probe = icm20645_i2c_probe,
  118. .remove = icm20645_i2c_remove,
  119. .detect = icm20645_i2c_detect,
  120. #if !defined(USE_EARLY_SUSPEND)
  121. .suspend = icm20645_suspend,
  122. .resume = icm20645_resume,
  123. #endif
  124. .id_table = icm20645_i2c_id,
  125. .driver = {
  126. .name = ICM20645_DEV_NAME,
  127. #ifdef CONFIG_OF
  128. .of_match_table = accel_of_match,
  129. #endif
  130. },
  131. };
  132. /*----------------------------------------------------------------------------*/
  133. static struct i2c_client *icm20645_i2c_client;
  134. static struct icm20645_i2c_data *obj_i2c_data;
  135. static bool sensor_power;
  136. static struct GSENSOR_VECTOR3D gsensor_gain;
  137. static char selftestRes[8] = { 0 };
  138. /*----------------------------------------------------------------------------*/
  139. #define GSE_TAG "[Gsensor] "
  140. #define GSE_FUN(f) pr_debug(GSE_TAG"%s\n", __func__)
  141. #define GSE_ERR(fmt, args...) pr_err(GSE_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
  142. #define GSE_LOG(fmt, args...) pr_debug(GSE_TAG fmt, ##args)
  143. /*----------------------------------------------------------------------------*/
  144. static struct data_resolution icm20645_data_resolution[] = {
  145. /*8 combination by {FULL_RES,RANGE} */
  146. {{0, 6}, 16384}, /*+/-2g in 16-bit resolution: 0.06 mg/LSB */
  147. {{0, 12}, 8192}, /*+/-4g in 16-bit resolution: 0.12 mg/LSB */
  148. {{0, 24}, 4096}, /*+/-8g in 16-bit resolution: 0.24 mg/LSB */
  149. {{0, 5}, 2048}, /*+/-16g in 16-bit resolution: 0.49 mg/LSB */
  150. };
  151. /*----------------------------------------------------------------------------*/
  152. static struct data_resolution icm20645_offset_resolution = { {0, 5}, 2048 };
  153. static unsigned int power_on;
  154. int ICM20645_gse_power(void)
  155. {
  156. return power_on;
  157. }
  158. EXPORT_SYMBOL(ICM20645_gse_power);
  159. int ICM20645_gse_mode(void)
  160. {
  161. return sensor_power;
  162. }
  163. EXPORT_SYMBOL(ICM20645_gse_mode);
  164. /*----------------------------------------------------------------------------*/
  165. static int mpu_i2c_read_block(struct i2c_client *client, u8 addr, u8 *data, u8 len)
  166. {
  167. int err = 0;
  168. u8 beg = addr;
  169. struct i2c_msg msgs[2] = { {0}, {0} };
  170. mutex_lock(&icm20645_i2c_mutex);
  171. msgs[0].addr = client->addr;
  172. msgs[0].flags = 0;
  173. msgs[0].len = 1;
  174. msgs[0].buf = &beg;
  175. msgs[1].addr = client->addr;
  176. msgs[1].flags = I2C_M_RD;
  177. msgs[1].len = len;
  178. msgs[1].buf = data;
  179. if (!client) {
  180. mutex_unlock(&icm20645_i2c_mutex);
  181. return -EINVAL;
  182. } else if (len > C_I2C_FIFO_SIZE) {
  183. mutex_unlock(&icm20645_i2c_mutex);
  184. GSE_ERR("length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
  185. return -EINVAL;
  186. }
  187. err = i2c_transfer(client->adapter, msgs, sizeof(msgs) / sizeof(msgs[0]));
  188. if (err != 2) {
  189. GSE_ERR("i2c_transfer error: (%d %p %d) %d\n", addr, data, len, err);
  190. err = -EIO;
  191. } else
  192. err = 0;
  193. mutex_unlock(&icm20645_i2c_mutex);
  194. return err;
  195. }
  196. static int mpu_i2c_write_block(struct i2c_client *client, u8 addr, u8 *data, u8 len)
  197. { /*because address also occupies one byte, the maximum length for write is 7 bytes */
  198. int err = 0, idx = 0, num = 0;
  199. char buf[C_I2C_FIFO_SIZE];
  200. err = 0;
  201. mutex_lock(&icm20645_i2c_mutex);
  202. if (!client) {
  203. mutex_unlock(&icm20645_i2c_mutex);
  204. return -EINVAL;
  205. } else if (len >= C_I2C_FIFO_SIZE) {
  206. mutex_unlock(&icm20645_i2c_mutex);
  207. GSE_ERR(" length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
  208. return -EINVAL;
  209. }
  210. num = 0;
  211. buf[num++] = addr;
  212. for (idx = 0; idx < len; idx++)
  213. buf[num++] = data[idx];
  214. err = i2c_master_send(client, buf, num);
  215. if (err < 0) {
  216. mutex_unlock(&icm20645_i2c_mutex);
  217. GSE_ERR("send command error!!\n");
  218. return -EFAULT;
  219. }
  220. mutex_unlock(&icm20645_i2c_mutex);
  221. return err;
  222. }
  223. int ICM20645_hwmsen_read_block(u8 addr, u8 *buf, u8 len)
  224. {
  225. if (NULL == icm20645_i2c_client) {
  226. GSE_ERR("ICM20645_hwmsen_read_block null ptr!!\n");
  227. return ICM20645_ERR_I2C;
  228. }
  229. return mpu_i2c_read_block(icm20645_i2c_client, addr, buf, len);
  230. }
  231. EXPORT_SYMBOL(ICM20645_hwmsen_read_block);
  232. int ICM20645_hwmsen_write_block(u8 addr, u8 *buf, u8 len)
  233. {
  234. if (NULL == icm20645_i2c_client) {
  235. GSE_ERR("ICM20645_hwmsen_read_block null ptr!!\n");
  236. return ICM20645_ERR_I2C;
  237. }
  238. return mpu_i2c_write_block(icm20645_i2c_client, addr, buf, len);
  239. }
  240. EXPORT_SYMBOL(ICM20645_hwmsen_write_block);
  241. static int icm20645_set_bank(struct i2c_client *client, u8 bank)
  242. {
  243. int res = 0;
  244. u8 databuf[2];
  245. databuf[0] = bank;
  246. res = mpu_i2c_write_block(client, REG_BANK_SEL, databuf, 0x1);
  247. if (res < 0) {
  248. GSE_LOG("icm20645_set_bank fail at %x", bank);
  249. return ICM20645_ERR_I2C;
  250. }
  251. return ICM20645_SUCCESS;
  252. }
  253. static int icm20645_turn_on(struct i2c_client *client, u8 status, bool on)
  254. {
  255. int res = 0;
  256. u8 databuf[2];
  257. memset(databuf, 0, sizeof(databuf));
  258. GSE_FUN(f);
  259. icm20645_set_bank(client, BANK_SEL_0);
  260. res = mpu_i2c_read_block(client, ICM20645_REG_POWER_CTL2, databuf, 0x1);
  261. if (res < 0) {
  262. GSE_LOG("icm20645_turn_on fail at %x", on);
  263. return ICM20645_ERR_I2C;
  264. }
  265. if (on == true) {
  266. databuf[0] &= ~status;
  267. res = mpu_i2c_write_block(client, ICM20645_REG_POWER_CTL2, databuf, 0x1);
  268. if (res < 0) {
  269. GSE_LOG("icm20645_turn_on fail at %x", on);
  270. return ICM20645_ERR_I2C;
  271. }
  272. } else {
  273. databuf[0] |= status;
  274. res = mpu_i2c_write_block(client, ICM20645_REG_POWER_CTL2, databuf, 0x1);
  275. if (res < 0) {
  276. GSE_LOG("icm20645_turn_on fail at %x", on);
  277. return ICM20645_ERR_I2C;
  278. }
  279. }
  280. return ICM20645_SUCCESS;
  281. }
  282. static int icm20645_lp_mode(struct i2c_client *client, bool on)
  283. {
  284. int res = 0;
  285. u8 databuf[2];
  286. memset(databuf, 0, sizeof(databuf));
  287. icm20645_set_bank(client, BANK_SEL_0);
  288. GSE_FUN(f);
  289. /* acc lp config */
  290. res = mpu_i2c_read_block(client, ICM20645_REG_LP_CONFIG, databuf, 0x1);
  291. if (res < 0) {
  292. GSE_ERR("icm20645_lp_mode fail at %x", on);
  293. return ICM20645_ERR_I2C;
  294. }
  295. if (on == true) {
  296. databuf[0] |= BIT_ACC_LP_EN;
  297. databuf[0] &= ~BIT_ACC_I2C_MST;
  298. res = mpu_i2c_write_block(client, ICM20645_REG_LP_CONFIG, databuf, 0x1);
  299. if (res < 0) {
  300. GSE_ERR("icm20645_lp_mode fail at %x", on);
  301. return ICM20645_ERR_I2C;
  302. }
  303. } else {
  304. databuf[0] &= ~BIT_ACC_LP_EN;
  305. databuf[0] &= ~BIT_ACC_I2C_MST;
  306. res = mpu_i2c_write_block(client, ICM20645_REG_LP_CONFIG, databuf, 0x1);
  307. if (res < 0) {
  308. GSE_ERR("icm20645_lp_mode fail at %x", on);
  309. return ICM20645_ERR_I2C;
  310. }
  311. }
  312. return ICM20645_SUCCESS;
  313. }
  314. static int ICM20645_Setfilter(struct i2c_client *client, int filter_sample)
  315. {
  316. u8 databuf[2] = { 0 };
  317. int res = 0;
  318. GSE_FUN(f);
  319. icm20645_set_bank(client, BANK_SEL_2);
  320. res = mpu_i2c_read_block(client, ICM20645_ACC_CONFIG_2, databuf, 0x1);
  321. if (res < 0) {
  322. GSE_ERR("ICM20645_ACC_CONFIG_2 fail\n");
  323. return ICM20645_ERR_I2C;
  324. }
  325. databuf[0] = filter_sample;
  326. res = mpu_i2c_write_block(client, ICM20645_ACC_CONFIG_2, databuf, 0x1);
  327. if (res <= 0) {
  328. GSE_ERR("ICM20645_ACC_CONFIG_2 err!\n");
  329. return ICM20645_ERR_I2C;
  330. }
  331. icm20645_set_bank(client, BANK_SEL_0);
  332. return ICM20645_SUCCESS;
  333. }
  334. static int ICM20645_SetSampleRate(struct i2c_client *client, int sample_rate)
  335. {
  336. u8 databuf[2] = { 0 };
  337. u16 rate_div = 0;
  338. int res = 0;
  339. rate_div = 1125 / sample_rate - 1;
  340. GSE_FUN(f);
  341. icm20645_set_bank(client, BANK_SEL_2);
  342. databuf[0] = rate_div % 256;
  343. res = mpu_i2c_write_block(client, ICM20645_REG_SAMRT_DIV2, databuf, 0x1);
  344. if (res <= 0) {
  345. GSE_ERR("write sample rate register err!\n");
  346. return ICM20645_ERR_I2C;
  347. }
  348. databuf[0] = rate_div / 256;
  349. res = mpu_i2c_write_block(client, ICM20645_REG_SAMRT_DIV1, databuf, 0x1);
  350. if (res <= 0) {
  351. GSE_ERR("write sample rate register err!\n");
  352. return ICM20645_ERR_I2C;
  353. }
  354. icm20645_set_bank(client, BANK_SEL_0);
  355. return ICM20645_SUCCESS;
  356. }
  357. /*--------------------icm20645 power control function----------------------------------*/
  358. static void ICM20645_power(struct acc_hw *hw, unsigned int on)
  359. {
  360. }
  361. /*----------------------------------------------------------------------------*/
  362. static int ICM20645_SetPowerMode(struct i2c_client *client, bool enable)
  363. {
  364. u8 databuf[2];
  365. int res = 0;
  366. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  367. GSE_FUN(f);
  368. icm20645_set_bank(client, BANK_SEL_0);
  369. if (enable == sensor_power) {
  370. GSE_LOG("Sensor power status is newest!\n");
  371. return ICM20645_SUCCESS;
  372. }
  373. res = mpu_i2c_read_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  374. if (res < 0)
  375. return ICM20645_ERR_I2C;
  376. databuf[0] &= ~ICM20645_SLEEP;
  377. if (enable == false) {
  378. if (ICM20645_gyro_mode() == false)
  379. databuf[0] |= ICM20645_SLEEP;
  380. }
  381. res = mpu_i2c_write_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  382. if (res < 0) {
  383. GSE_LOG("set power mode failed!\n");
  384. return ICM20645_ERR_I2C;
  385. } else if (atomic_read(&obj->trace) & ICM20645_TRC_INFO)
  386. GSE_LOG("set power mode ok %d!\n", databuf[0]);
  387. sensor_power = enable;
  388. return ICM20645_SUCCESS;
  389. }
  390. /*----------------------------------------------------------------------------*/
  391. static int ICM20645_SetDataResolution(struct icm20645_i2c_data *obj)
  392. {
  393. int err = 0;
  394. u8 dat = 0, reso = 0;
  395. GSE_FUN(f);
  396. err = mpu_i2c_read_block(obj->client, ICM20645_ACC_CONFIG, &dat, 1);
  397. if (err) {
  398. GSE_ERR("write data format fail!!\n");
  399. return err;
  400. }
  401. /*the data_reso is combined by 3 bits: {FULL_RES, DATA_RANGE} */
  402. reso = 0x00;
  403. reso = (dat & ICM20645_RANGE_16G) >> 1;
  404. if (reso < sizeof(icm20645_data_resolution) / sizeof(icm20645_data_resolution[0])) {
  405. obj->reso = &icm20645_data_resolution[reso];
  406. return 0;
  407. } else
  408. return -EINVAL;
  409. }
  410. /*----------------------------------------------------------------------------*/
  411. static int ICM20645_ReadData(struct i2c_client *client, s16 data[ICM20645_AXES_NUM])
  412. {
  413. struct icm20645_i2c_data *priv = i2c_get_clientdata(client);
  414. u8 buf[ICM20645_DATA_LEN] = { 0 };
  415. int err = 0;
  416. if (NULL == client)
  417. return -EINVAL;
  418. /* write then burst read */
  419. mpu_i2c_read_block(client, ICM20645_REG_DATAX0, buf, ICM20645_DATA_LEN);
  420. data[ICM20645_AXIS_X] = (s16) ((buf[ICM20645_AXIS_X * 2] << 8) | (buf[ICM20645_AXIS_X * 2 + 1]));
  421. data[ICM20645_AXIS_Y] = (s16) ((buf[ICM20645_AXIS_Y * 2] << 8) | (buf[ICM20645_AXIS_Y * 2 + 1]));
  422. data[ICM20645_AXIS_Z] = (s16) ((buf[ICM20645_AXIS_Z * 2] << 8) | (buf[ICM20645_AXIS_Z * 2 + 1]));
  423. if (atomic_read(&priv->trace) & ICM20645_TRC_RAWDATA) {
  424. GSE_LOG("[%08X %08X %08X] => [%5d %5d %5d]\n", data[ICM20645_AXIS_X], data[ICM20645_AXIS_Y],
  425. data[ICM20645_AXIS_Z], data[ICM20645_AXIS_X], data[ICM20645_AXIS_Y], data[ICM20645_AXIS_Z]);
  426. }
  427. #ifdef CONFIG_ICM20645_LOWPASS
  428. if (atomic_read(&priv->filter)) {
  429. if (atomic_read(&priv->fir_en) && !atomic_read(&priv->suspend)) {
  430. int idx, firlen = atomic_read(&priv->firlen);
  431. if (priv->fir.num < firlen) {
  432. priv->fir.raw[priv->fir.num][ICM20645_AXIS_X] = data[ICM20645_AXIS_X];
  433. priv->fir.raw[priv->fir.num][ICM20645_AXIS_Y] = data[ICM20645_AXIS_Y];
  434. priv->fir.raw[priv->fir.num][ICM20645_AXIS_Z] = data[ICM20645_AXIS_Z];
  435. priv->fir.sum[ICM20645_AXIS_X] += data[ICM20645_AXIS_X];
  436. priv->fir.sum[ICM20645_AXIS_Y] += data[ICM20645_AXIS_Y];
  437. priv->fir.sum[ICM20645_AXIS_Z] += data[ICM20645_AXIS_Z];
  438. if (atomic_read(&priv->trace) & ICM20645_TRC_FILTER) {
  439. GSE_LOG("add [%2d] [%5d %5d %5d] => [%5d %5d %5d]\n", priv->fir.num,
  440. priv->fir.raw[priv->fir.num][ICM20645_AXIS_X],
  441. priv->fir.raw[priv->fir.num][ICM20645_AXIS_Y],
  442. priv->fir.raw[priv->fir.num][ICM20645_AXIS_Z],
  443. priv->fir.sum[ICM20645_AXIS_X], priv->fir.sum[ICM20645_AXIS_Y],
  444. priv->fir.sum[ICM20645_AXIS_Z]);
  445. }
  446. priv->fir.num++;
  447. priv->fir.idx++;
  448. } else {
  449. idx = priv->fir.idx % firlen;
  450. priv->fir.sum[ICM20645_AXIS_X] -= priv->fir.raw[idx][ICM20645_AXIS_X];
  451. priv->fir.sum[ICM20645_AXIS_Y] -= priv->fir.raw[idx][ICM20645_AXIS_Y];
  452. priv->fir.sum[ICM20645_AXIS_Z] -= priv->fir.raw[idx][ICM20645_AXIS_Z];
  453. priv->fir.raw[idx][ICM20645_AXIS_X] = data[ICM20645_AXIS_X];
  454. priv->fir.raw[idx][ICM20645_AXIS_Y] = data[ICM20645_AXIS_Y];
  455. priv->fir.raw[idx][ICM20645_AXIS_Z] = data[ICM20645_AXIS_Z];
  456. priv->fir.sum[ICM20645_AXIS_X] += data[ICM20645_AXIS_X];
  457. priv->fir.sum[ICM20645_AXIS_Y] += data[ICM20645_AXIS_Y];
  458. priv->fir.sum[ICM20645_AXIS_Z] += data[ICM20645_AXIS_Z];
  459. priv->fir.idx++;
  460. data[ICM20645_AXIS_X] = priv->fir.sum[ICM20645_AXIS_X] / firlen;
  461. data[ICM20645_AXIS_Y] = priv->fir.sum[ICM20645_AXIS_Y] / firlen;
  462. data[ICM20645_AXIS_Z] = priv->fir.sum[ICM20645_AXIS_Z] / firlen;
  463. if (atomic_read(&priv->trace) & ICM20645_TRC_FILTER) {
  464. GSE_LOG("add [%2d] [%5d %5d %5d] => [%5d %5d %5d] : [%5d %5d %5d]\n", idx,
  465. priv->fir.raw[idx][ICM20645_AXIS_X],
  466. priv->fir.raw[idx][ICM20645_AXIS_Y],
  467. priv->fir.raw[idx][ICM20645_AXIS_Z], priv->fir.sum[ICM20645_AXIS_X],
  468. priv->fir.sum[ICM20645_AXIS_Y], priv->fir.sum[ICM20645_AXIS_Z],
  469. data[ICM20645_AXIS_X], data[ICM20645_AXIS_Y], data[ICM20645_AXIS_Z]);
  470. }
  471. }
  472. }
  473. }
  474. #endif
  475. return err;
  476. }
  477. /*----------------------------------------------------------------------------*/
  478. static int ICM20645_ReadOffset(struct i2c_client *client, s8 ofs[ICM20645_AXES_NUM])
  479. {
  480. int err = 0;
  481. #ifdef SW_CALIBRATION
  482. ofs[0] = ofs[1] = ofs[2] = 0x0;
  483. #endif
  484. return err;
  485. }
  486. /*----------------------------------------------------------------------------*/
  487. static int ICM20645_ResetCalibration(struct i2c_client *client)
  488. {
  489. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  490. #ifndef SW_CALIBRATION
  491. s8 ofs[ICM20645_AXES_NUM] = { 0x00, 0x00, 0x00 };
  492. #endif
  493. int err = 0;
  494. memset(obj->cali_sw, 0x00, sizeof(obj->cali_sw));
  495. memset(obj->offset, 0x00, sizeof(obj->offset));
  496. return err;
  497. }
  498. /*----------------------------------------------------------------------------*/
  499. static int ICM20645_ReadCalibration(struct i2c_client *client, int dat[ICM20645_AXES_NUM])
  500. {
  501. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  502. #ifdef SW_CALIBRATION
  503. int mul = 0;
  504. #endif
  505. dat[obj->cvt.map[ICM20645_AXIS_X]] =
  506. obj->cvt.sign[ICM20645_AXIS_X] * (obj->offset[ICM20645_AXIS_X] * mul + obj->cali_sw[ICM20645_AXIS_X]);
  507. dat[obj->cvt.map[ICM20645_AXIS_Y]] =
  508. obj->cvt.sign[ICM20645_AXIS_Y] * (obj->offset[ICM20645_AXIS_Y] * mul + obj->cali_sw[ICM20645_AXIS_Y]);
  509. dat[obj->cvt.map[ICM20645_AXIS_Z]] =
  510. obj->cvt.sign[ICM20645_AXIS_Z] * (obj->offset[ICM20645_AXIS_Z] * mul + obj->cali_sw[ICM20645_AXIS_Z]);
  511. return 0;
  512. }
  513. /*----------------------------------------------------------------------------*/
  514. static int ICM20645_ReadCalibrationEx(struct i2c_client *client, int act[ICM20645_AXES_NUM], int raw[ICM20645_AXES_NUM])
  515. {
  516. /*raw: the raw calibration data; act: the actual calibration data */
  517. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  518. #ifdef SW_CALIBRATION
  519. int mul = 0;
  520. #endif
  521. raw[ICM20645_AXIS_X] = obj->offset[ICM20645_AXIS_X] * mul + obj->cali_sw[ICM20645_AXIS_X];
  522. raw[ICM20645_AXIS_Y] = obj->offset[ICM20645_AXIS_Y] * mul + obj->cali_sw[ICM20645_AXIS_Y];
  523. raw[ICM20645_AXIS_Z] = obj->offset[ICM20645_AXIS_Z] * mul + obj->cali_sw[ICM20645_AXIS_Z];
  524. act[obj->cvt.map[ICM20645_AXIS_X]] = obj->cvt.sign[ICM20645_AXIS_X] * raw[ICM20645_AXIS_X];
  525. act[obj->cvt.map[ICM20645_AXIS_Y]] = obj->cvt.sign[ICM20645_AXIS_Y] * raw[ICM20645_AXIS_Y];
  526. act[obj->cvt.map[ICM20645_AXIS_Z]] = obj->cvt.sign[ICM20645_AXIS_Z] * raw[ICM20645_AXIS_Z];
  527. return 0;
  528. }
  529. /*----------------------------------------------------------------------------*/
  530. static int ICM20645_WriteCalibration(struct i2c_client *client, int dat[ICM20645_AXES_NUM])
  531. {
  532. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  533. int err = 0;
  534. int cali[ICM20645_AXES_NUM], raw[ICM20645_AXES_NUM];
  535. #ifndef SW_CALIBRATION
  536. int lsb = icm20645_offset_resolution.sensitivity;
  537. int divisor = obj->reso->sensitivity / lsb;
  538. #endif
  539. err = ICM20645_ReadCalibrationEx(client, cali, raw);
  540. if (err) {
  541. GSE_ERR("read offset fail, %d\n", err);
  542. return err;
  543. }
  544. GSE_LOG("OLDOFF: (%+3d %+3d %+3d): (%+3d %+3d %+3d) / (%+3d %+3d %+3d)\n",
  545. raw[ICM20645_AXIS_X], raw[ICM20645_AXIS_Y], raw[ICM20645_AXIS_Z],
  546. obj->offset[ICM20645_AXIS_X], obj->offset[ICM20645_AXIS_Y], obj->offset[ICM20645_AXIS_Z],
  547. obj->cali_sw[ICM20645_AXIS_X], obj->cali_sw[ICM20645_AXIS_Y], obj->cali_sw[ICM20645_AXIS_Z]);
  548. /*calculate the real offset expected by caller */
  549. cali[ICM20645_AXIS_X] += dat[ICM20645_AXIS_X];
  550. cali[ICM20645_AXIS_Y] += dat[ICM20645_AXIS_Y];
  551. cali[ICM20645_AXIS_Z] += dat[ICM20645_AXIS_Z];
  552. GSE_LOG("UPDATE: (%+3d %+3d %+3d)\n", dat[ICM20645_AXIS_X], dat[ICM20645_AXIS_Y], dat[ICM20645_AXIS_Z]);
  553. #ifdef SW_CALIBRATION
  554. obj->cali_sw[ICM20645_AXIS_X] = obj->cvt.sign[ICM20645_AXIS_X] * (cali[obj->cvt.map[ICM20645_AXIS_X]]);
  555. obj->cali_sw[ICM20645_AXIS_Y] = obj->cvt.sign[ICM20645_AXIS_Y] * (cali[obj->cvt.map[ICM20645_AXIS_Y]]);
  556. obj->cali_sw[ICM20645_AXIS_Z] = obj->cvt.sign[ICM20645_AXIS_Z] * (cali[obj->cvt.map[ICM20645_AXIS_Z]]);
  557. #else
  558. obj->offset[ICM20645_AXIS_X] =
  559. (s8) (obj->cvt.sign[ICM20645_AXIS_X] * (cali[obj->cvt.map[ICM20645_AXIS_X]]) / (divisor));
  560. obj->offset[ICM20645_AXIS_Y] =
  561. (s8) (obj->cvt.sign[ICM20645_AXIS_Y] * (cali[obj->cvt.map[ICM20645_AXIS_Y]]) / (divisor));
  562. obj->offset[ICM20645_AXIS_Z] =
  563. (s8) (obj->cvt.sign[ICM20645_AXIS_Z] * (cali[obj->cvt.map[ICM20645_AXIS_Z]]) / (divisor));
  564. /*convert software calibration using standard calibration */
  565. obj->cali_sw[ICM20645_AXIS_X] =
  566. obj->cvt.sign[ICM20645_AXIS_X] * (cali[obj->cvt.map[ICM20645_AXIS_X]]) % (divisor);
  567. obj->cali_sw[ICM20645_AXIS_Y] =
  568. obj->cvt.sign[ICM20645_AXIS_Y] * (cali[obj->cvt.map[ICM20645_AXIS_Y]]) % (divisor);
  569. obj->cali_sw[ICM20645_AXIS_Z] =
  570. obj->cvt.sign[ICM20645_AXIS_Z] * (cali[obj->cvt.map[ICM20645_AXIS_Z]]) % (divisor);
  571. GSE_LOG("NEWOFF: (%+3d %+3d %+3d): (%+3d %+3d %+3d) / (%+3d %+3d %+3d)\n",
  572. obj->offset[ICM20645_AXIS_X] * divisor + obj->cali_sw[ICM20645_AXIS_X],
  573. obj->offset[ICM20645_AXIS_Y] * divisor + obj->cali_sw[ICM20645_AXIS_Y],
  574. obj->offset[ICM20645_AXIS_Z] * divisor + obj->cali_sw[ICM20645_AXIS_Z],
  575. obj->offset[ICM20645_AXIS_X], obj->offset[ICM20645_AXIS_Y], obj->offset[ICM20645_AXIS_Z],
  576. obj->cali_sw[ICM20645_AXIS_X], obj->cali_sw[ICM20645_AXIS_Y], obj->cali_sw[ICM20645_AXIS_Z]);
  577. err = hwmsen_write_block(obj->client, ICM20645_REG_OFSX, obj->offset, ICM20645_AXES_NUM);
  578. if (err) {
  579. GSE_ERR("write offset fail: %d\n", err);
  580. return err;
  581. }
  582. #endif
  583. return err;
  584. }
  585. /*----------------------------------------------------------------------------*/
  586. static int ICM20645_CheckDeviceID(struct i2c_client *client)
  587. {
  588. u8 databuf[10];
  589. int res = 0;
  590. memset(databuf, 0, sizeof(u8) * 10);
  591. icm20645_set_bank(client, BANK_SEL_0);
  592. res = mpu_i2c_read_block(client, ICM20645_REG_DEVID, databuf, 0x1);
  593. if (res < 0)
  594. goto exit_ICM20645_CheckDeviceID;
  595. GSE_LOG("ICM20645_CheckDeviceID 0x%x\n", databuf[0]);
  596. exit_ICM20645_CheckDeviceID:
  597. if (res < 0)
  598. return ICM20645_ERR_I2C;
  599. return ICM20645_SUCCESS;
  600. }
  601. /*----------------------------------------------------------------------------*/
  602. static int ICM20645_SetDataFormat(struct i2c_client *client, u8 dataformat)
  603. {
  604. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  605. u8 databuf[2];
  606. int res = 0;
  607. GSE_FUN(f);
  608. memset(databuf, 0, sizeof(u8) * 2);
  609. icm20645_set_bank(client, BANK_SEL_2);
  610. /* write */
  611. databuf[0] = (0 | dataformat);
  612. res = mpu_i2c_write_block(client, ICM20645_ACC_CONFIG, databuf, 0x1);
  613. if (res < 0)
  614. return ICM20645_ERR_I2C;
  615. return ICM20645_SetDataResolution(obj);
  616. }
  617. /*----------------------------------------------------------------------------*/
  618. static int ICM20645_Dev_Reset(struct i2c_client *client)
  619. {
  620. u8 databuf[10];
  621. int res = 0;
  622. GSE_FUN(f);
  623. memset(databuf, 0, sizeof(u8) * 10);
  624. icm20645_set_bank(client, BANK_SEL_0);
  625. /* read */
  626. res = mpu_i2c_read_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  627. if (res < 0)
  628. return ICM20645_ERR_I2C;
  629. /* write */
  630. databuf[0] = databuf[0] | ICM20645_DEV_RESET;
  631. res = mpu_i2c_write_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  632. if (res < 0) {
  633. GSE_LOG("ICM20645_Dev_Reset fail\n");
  634. return ICM20645_ERR_I2C;
  635. }
  636. do {
  637. res = mpu_i2c_read_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  638. if (res < 0)
  639. return ICM20645_ERR_I2C;
  640. GSE_LOG("[Gsensor] check reset bit");
  641. } while ((databuf[0] & ICM20645_DEV_RESET) != 0);
  642. msleep(50);
  643. return ICM20645_SUCCESS;
  644. }
  645. /*----------------------------------------------------------------------------*/
  646. static int ICM20645_SetIntEnable(struct i2c_client *client, u8 intenable)
  647. {
  648. u8 databuf[2];
  649. int res = 0;
  650. GSE_FUN(f);
  651. memset(databuf, 0, sizeof(u8) * 2);
  652. databuf[0] = intenable;
  653. res = mpu_i2c_write_block(client, ICM20645_REG_INT_ENABLE, databuf, 0x1);
  654. if (res < 0)
  655. return ICM20645_ERR_I2C;
  656. return ICM20645_SUCCESS;
  657. }
  658. /*----------------------------------------------------------------------------*/
  659. static int icm20645_gpio_config(void)
  660. {
  661. /*
  662. mt_set_gpio_mode(GPIO_GSE_1_EINT_PIN, GPIO_GSE_1_EINT_PIN_M_GPIO);
  663. mt_set_gpio_dir(GPIO_GSE_1_EINT_PIN, GPIO_DIR_IN);
  664. mt_set_gpio_pull_enable(GPIO_GSE_1_EINT_PIN, GPIO_PULL_ENABLE);
  665. mt_set_gpio_pull_select(GPIO_GSE_1_EINT_PIN, GPIO_PULL_DOWN);
  666. */
  667. /*
  668. mt_set_gpio_mode(GPIO_GSE_2_EINT_PIN, GPIO_GSE_2_EINT_PIN_M_GPIO);
  669. mt_set_gpio_dir(GPIO_GSE_2_EINT_PIN, GPIO_DIR_IN);
  670. mt_set_gpio_pull_enable(GPIO_GSE_2_EINT_PIN, GPIO_PULL_ENABLE);
  671. mt_set_gpio_pull_select(GPIO_GSE_2_EINT_PIN, GPIO_PULL_DOWN);
  672. */
  673. return 0;
  674. }
  675. static int ICM20645_selclk(struct i2c_client *client)
  676. {
  677. int res = 0;
  678. u8 databuf[2];
  679. res = mpu_i2c_read_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  680. if (res < 0) {
  681. GSE_ERR("ICM20645_REG_POWER_CTL read fail\n");
  682. return ICM20645_ERR_I2C;
  683. }
  684. databuf[0] |= (BIT_CLK_PLL | BIT_TEMP_DIS);
  685. res = mpu_i2c_write_block(client, ICM20645_REG_POWER_CTL, databuf, 0x1);
  686. if (res < 0) {
  687. GSE_ERR("ICM20645_REG_POWER_CTL write fail\n");
  688. return ICM20645_ERR_I2C;
  689. }
  690. return ICM20645_SUCCESS;
  691. }
  692. static int icm20645_init_client(struct i2c_client *client, int reset_cali)
  693. {
  694. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  695. int res = 0;
  696. icm20645_gpio_config();
  697. res = ICM20645_selclk(client);
  698. if (res != ICM20645_SUCCESS) {
  699. GSE_ERR("ICM20645_selclk error\n");
  700. return res;
  701. }
  702. res = ICM20645_CheckDeviceID(client);
  703. if (res != ICM20645_SUCCESS) {
  704. GSE_ERR("Check ID error\n");
  705. return res;
  706. }
  707. res = ICM20645_SetPowerMode(client, true);
  708. if (res != ICM20645_SUCCESS) {
  709. GSE_ERR("set power error\n");
  710. return res;
  711. }
  712. res = ICM20645_SetDataFormat(client, (ACCEL_DLPFCFG | ICM20645_RANGE_16G | ACCEL_FCHOICE));
  713. if (res != ICM20645_SUCCESS) {
  714. GSE_ERR("set data format error\n");
  715. return res;
  716. }
  717. res = ICM20645_SetSampleRate(client, 125);
  718. if (res != ICM20645_SUCCESS) {
  719. GSE_ERR("ICM20645_SetSampleRate error\n");
  720. return res;
  721. }
  722. res = ICM20645_Setfilter(client, ACCEL_AVGCFG_8X);
  723. if (res != ICM20645_SUCCESS) {
  724. GSE_ERR("ICM20645_Setfilter error\n");
  725. return res;
  726. }
  727. gsensor_gain.x = gsensor_gain.y = gsensor_gain.z = obj->reso->sensitivity;
  728. res = ICM20645_SetIntEnable(client, 0x00);
  729. if (res != ICM20645_SUCCESS) {
  730. GSE_ERR("icm20645_SetIntEnable error\n");
  731. return res;
  732. }
  733. if (0 != reset_cali) {
  734. /*reset calibration only in power on */
  735. res = ICM20645_ResetCalibration(client);
  736. if (res != ICM20645_SUCCESS)
  737. return res;
  738. }
  739. #ifdef CONFIG_ICM20645_LOWPASS
  740. memset(&obj->fir, 0x00, sizeof(obj->fir));
  741. #endif
  742. res = icm20645_turn_on(client, BIT_PWR_ACCEL_STBY, true);
  743. if (res != ICM20645_SUCCESS) {
  744. GSE_ERR("icm20645_turn_on error\n");
  745. return res;
  746. }
  747. res = icm20645_lp_mode(client, true);
  748. if (res != ICM20645_SUCCESS) {
  749. GSE_ERR("icm20645_lp_mode error\n");
  750. return res;
  751. }
  752. res = ICM20645_SetPowerMode(client, false);
  753. if (res != ICM20645_SUCCESS) {
  754. GSE_ERR("set power error\n");
  755. return res;
  756. }
  757. icm20645_set_bank(client, BANK_SEL_0);
  758. return ICM20645_SUCCESS;
  759. }
  760. /*----------------------------------------------------------------------------*/
  761. static int ICM20645_ReadAllReg(struct i2c_client *client, char *buf, int bufsize)
  762. {
  763. u8 total_len = 8;
  764. u8 addr = 0;
  765. u8 buff[total_len + 1];
  766. int err = 0;
  767. int i, res;
  768. if (sensor_power == false) {
  769. err = ICM20645_SetPowerMode(client, true);
  770. if (err)
  771. GSE_ERR("Power on mpu6050 error %d!\n", err);
  772. msleep(50);
  773. }
  774. icm20645_set_bank(client, BANK_SEL_0);
  775. res = mpu_i2c_read_block(client, addr, buff, total_len);
  776. if (res < 0)
  777. return ICM20645_ERR_I2C;
  778. for (i = 0; i <= total_len; i++)
  779. GSE_ERR("ICM20645 bank0 reg=0x%x, data=0x%x\n", (addr + i), buff[i]);
  780. icm20645_set_bank(client, BANK_SEL_2);
  781. addr = 0X10;
  782. res = mpu_i2c_read_block(client, addr, buff, total_len);
  783. if (res < 0)
  784. return ICM20645_ERR_I2C;
  785. for (i = 0; i <= total_len; i++)
  786. GSE_ERR("ICM20645 bank2 reg=0x%x, data=0x%x\n", (addr + i), buff[i]);
  787. icm20645_set_bank(client, BANK_SEL_0);
  788. return 0;
  789. }
  790. /*----------------------------------------------------------------------------*/
  791. static int ICM20645_ReadChipInfo(struct i2c_client *client, char *buf, int bufsize)
  792. {
  793. u8 databuf[10];
  794. memset(databuf, 0, sizeof(u8) * 10);
  795. if ((NULL == buf) || (bufsize <= 30))
  796. return -1;
  797. if (NULL == client) {
  798. *buf = 0;
  799. return -2;
  800. }
  801. sprintf(buf, "ICM20645 Chip");
  802. return 0;
  803. }
  804. /*----------------------------------------------------------------------------*/
  805. static int ICM20645_ReadSensorData(struct i2c_client *client, char *buf, int bufsize)
  806. {
  807. struct icm20645_i2c_data *obj = obj_i2c_data;
  808. int acc[ICM20645_AXES_NUM];
  809. int res = 0;
  810. client = obj->client;
  811. if (atomic_read(&obj->suspend))
  812. return -3;
  813. if (NULL == buf)
  814. return -1;
  815. if (NULL == client) {
  816. *buf = 0;
  817. return -2;
  818. }
  819. if (sensor_power == false) {
  820. res = ICM20645_SetPowerMode(client, true);
  821. if (res)
  822. GSE_ERR("Power on icm20645 error %d!\n", res);
  823. msleep(50);
  824. }
  825. res = ICM20645_ReadData(client, obj->data);
  826. if (res) {
  827. GSE_ERR("I2C error: ret value=%d\n", res);
  828. return -3;
  829. }
  830. obj->data[ICM20645_AXIS_X] += obj->cali_sw[ICM20645_AXIS_X];
  831. obj->data[ICM20645_AXIS_Y] += obj->cali_sw[ICM20645_AXIS_Y];
  832. obj->data[ICM20645_AXIS_Z] += obj->cali_sw[ICM20645_AXIS_Z];
  833. /*remap coordinate */
  834. acc[obj->cvt.map[ICM20645_AXIS_X]] = obj->cvt.sign[ICM20645_AXIS_X] * obj->data[ICM20645_AXIS_X];
  835. acc[obj->cvt.map[ICM20645_AXIS_Y]] = obj->cvt.sign[ICM20645_AXIS_Y] * obj->data[ICM20645_AXIS_Y];
  836. acc[obj->cvt.map[ICM20645_AXIS_Z]] = obj->cvt.sign[ICM20645_AXIS_Z] * obj->data[ICM20645_AXIS_Z];
  837. acc[ICM20645_AXIS_X] = acc[ICM20645_AXIS_X] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  838. acc[ICM20645_AXIS_Y] = acc[ICM20645_AXIS_Y] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  839. acc[ICM20645_AXIS_Z] = acc[ICM20645_AXIS_Z] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  840. sprintf(buf, "%04x %04x %04x", acc[ICM20645_AXIS_X], acc[ICM20645_AXIS_Y], acc[ICM20645_AXIS_Z]);
  841. if (atomic_read(&obj->trace) & ICM20645_TRC_IOCTL)
  842. GSE_LOG("gsensor data: %s!\n", buf);
  843. return 0;
  844. }
  845. /*----------------------------------------------------------------------------*/
  846. static int ICM20645_ReadRawData(struct i2c_client *client, char *buf)
  847. {
  848. struct icm20645_i2c_data *obj = (struct icm20645_i2c_data *)i2c_get_clientdata(client);
  849. int res = 0;
  850. if (!buf || !client)
  851. return -2;
  852. if (atomic_read(&obj->suspend))
  853. return -3;
  854. res = ICM20645_ReadData(client, obj->data);
  855. if (res) {
  856. GSE_ERR("I2C error: ret value=%d", res);
  857. return -1;
  858. }
  859. sprintf(buf, "%04x %04x %04x", obj->data[ICM20645_AXIS_X],
  860. obj->data[ICM20645_AXIS_Y], obj->data[ICM20645_AXIS_Z]);
  861. return 0;
  862. }
  863. /*----------------------------------------------------------------------------*/
  864. static int ICM20645_InitSelfTest(struct i2c_client *client)
  865. {
  866. int res = 0;
  867. u8 data = 0;
  868. #if 0
  869. res = ICM20645_SetBWRate(client, ICM20645_BW_184HZ);
  870. if (res != ICM20645_SUCCESS)
  871. return res;
  872. #endif
  873. res = mpu_i2c_read_block(client, ICM20645_ACC_CONFIG, &data, 1);
  874. if (res != ICM20645_SUCCESS)
  875. return res;
  876. return ICM20645_SUCCESS;
  877. }
  878. /*----------------------------------------------------------------------------*/
  879. static int ICM20645_JudgeTestResult(struct i2c_client *client, s32 prv[ICM20645_AXES_NUM], s32 nxt[ICM20645_AXES_NUM])
  880. {
  881. struct criteria {
  882. int min;
  883. int max;
  884. };
  885. struct criteria self[4][3] = {
  886. {{0, 540}, {0, 540}, {0, 875} },
  887. {{0, 270}, {0, 270}, {0, 438} },
  888. {{0, 135}, {0, 135}, {0, 219} },
  889. {{0, 67}, {0, 67}, {0, 110} },
  890. };
  891. struct criteria (*ptr)[3] = NULL;
  892. u8 format = 0;
  893. int res = 0;
  894. res = mpu_i2c_read_block(client, ICM20645_ACC_CONFIG, &format, 1);
  895. if (res)
  896. return res;
  897. format = format & ICM20645_RANGE_16G;
  898. switch (format) {
  899. case ICM20645_RANGE_2G:
  900. GSE_LOG("format use self[0]\n");
  901. ptr = &self[0];
  902. break;
  903. case ICM20645_RANGE_4G:
  904. GSE_LOG("format use self[1]\n");
  905. ptr = &self[1];
  906. break;
  907. case ICM20645_RANGE_8G:
  908. GSE_LOG("format use self[2]\n");
  909. ptr = &self[2];
  910. break;
  911. case ICM20645_RANGE_16G:
  912. GSE_LOG("format use self[3]\n");
  913. ptr = &self[3];
  914. break;
  915. default:
  916. GSE_LOG("invilad case\n");
  917. break;
  918. }
  919. if (!ptr) {
  920. GSE_ERR("null pointer\n");
  921. return -EINVAL;
  922. }
  923. GSE_LOG("format=0x%x\n", format);
  924. GSE_LOG("X diff is %ld\n", abs(nxt[ICM20645_AXIS_X] - prv[ICM20645_AXIS_X]));
  925. GSE_LOG("Y diff is %ld\n", abs(nxt[ICM20645_AXIS_Y] - prv[ICM20645_AXIS_Y]));
  926. GSE_LOG("Z diff is %ld\n", abs(nxt[ICM20645_AXIS_Z] - prv[ICM20645_AXIS_Z]));
  927. if ((abs(nxt[ICM20645_AXIS_X] - prv[ICM20645_AXIS_X]) > (*ptr)[ICM20645_AXIS_X].max) ||
  928. (abs(nxt[ICM20645_AXIS_X] - prv[ICM20645_AXIS_X]) < (*ptr)[ICM20645_AXIS_X].min)) {
  929. GSE_ERR("X is over range\n");
  930. res = -EINVAL;
  931. }
  932. if ((abs(nxt[ICM20645_AXIS_Y] - prv[ICM20645_AXIS_Y]) > (*ptr)[ICM20645_AXIS_Y].max) ||
  933. (abs(nxt[ICM20645_AXIS_Y] - prv[ICM20645_AXIS_Y]) < (*ptr)[ICM20645_AXIS_Y].min)) {
  934. GSE_ERR("Y is over range\n");
  935. res = -EINVAL;
  936. }
  937. if ((abs(nxt[ICM20645_AXIS_Z] - prv[ICM20645_AXIS_Z]) > (*ptr)[ICM20645_AXIS_Z].max) ||
  938. (abs(nxt[ICM20645_AXIS_Z] - prv[ICM20645_AXIS_Z]) < (*ptr)[ICM20645_AXIS_Z].min)) {
  939. GSE_ERR("Z is over range\n");
  940. res = -EINVAL;
  941. }
  942. return res;
  943. }
  944. /*----------------------------------------------------------------------------*/
  945. static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
  946. {
  947. struct i2c_client *client = icm20645_i2c_client;
  948. char strbuf[ICM20645_BUFSIZE];
  949. if (NULL == client) {
  950. GSE_ERR("i2c client is null!!\n");
  951. return 0;
  952. }
  953. if (sensor_power == false) {
  954. ICM20645_SetPowerMode(client, true);
  955. msleep(50);
  956. }
  957. ICM20645_ReadAllReg(client, strbuf, ICM20645_BUFSIZE);
  958. ICM20645_ReadChipInfo(client, strbuf, ICM20645_BUFSIZE);
  959. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  960. }
  961. /*----------------------------------------------------------------------------*/
  962. static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
  963. {
  964. struct i2c_client *client = icm20645_i2c_client;
  965. char strbuf[ICM20645_BUFSIZE];
  966. if (NULL == client) {
  967. GSE_ERR("i2c client is null!!\n");
  968. return 0;
  969. }
  970. ICM20645_ReadSensorData(client, strbuf, ICM20645_BUFSIZE);
  971. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  972. }
  973. /*----------------------------------------------------------------------------*/
  974. static ssize_t show_cali_value(struct device_driver *ddri, char *buf)
  975. {
  976. struct i2c_client *client = icm20645_i2c_client;
  977. struct icm20645_i2c_data *obj;
  978. int err = 0, len = 0, mul = 0;
  979. int tmp[ICM20645_AXES_NUM];
  980. if (NULL == client) {
  981. GSE_ERR("i2c client is null!!\n");
  982. return 0;
  983. }
  984. obj = i2c_get_clientdata(client);
  985. err = ICM20645_ReadOffset(client, obj->offset);
  986. if (err)
  987. return -EINVAL;
  988. err = ICM20645_ReadCalibration(client, tmp);
  989. if (err)
  990. return -EINVAL;
  991. mul = obj->reso->sensitivity / icm20645_offset_resolution.sensitivity;
  992. len +=
  993. snprintf(buf + len, PAGE_SIZE - len, "[HW ][%d] (%+3d, %+3d, %+3d) : (0x%02X, 0x%02X, 0x%02X)\n",
  994. mul, obj->offset[ICM20645_AXIS_X], obj->offset[ICM20645_AXIS_Y],
  995. obj->offset[ICM20645_AXIS_Z], obj->offset[ICM20645_AXIS_X], obj->offset[ICM20645_AXIS_Y],
  996. obj->offset[ICM20645_AXIS_Z]);
  997. len +=
  998. snprintf(buf + len, PAGE_SIZE - len, "[SW ][%d] (%+3d, %+3d, %+3d)\n", 1,
  999. obj->cali_sw[ICM20645_AXIS_X], obj->cali_sw[ICM20645_AXIS_Y],
  1000. obj->cali_sw[ICM20645_AXIS_Z]);
  1001. len += snprintf(buf + len, PAGE_SIZE - len, "[ALL] (%+3d, %+3d, %+3d) : (%+3d, %+3d, %+3d)\n",
  1002. obj->offset[ICM20645_AXIS_X] * mul + obj->cali_sw[ICM20645_AXIS_X],
  1003. obj->offset[ICM20645_AXIS_Y] * mul + obj->cali_sw[ICM20645_AXIS_Y],
  1004. obj->offset[ICM20645_AXIS_Z] * mul + obj->cali_sw[ICM20645_AXIS_Z],
  1005. tmp[ICM20645_AXIS_X], tmp[ICM20645_AXIS_Y], tmp[ICM20645_AXIS_Z]);
  1006. return len;
  1007. }
  1008. /*----------------------------------------------------------------------------*/
  1009. static ssize_t store_cali_value(struct device_driver *ddri, const char *buf, size_t count)
  1010. {
  1011. struct i2c_client *client = icm20645_i2c_client;
  1012. int err = 0, x = 0, y = 0, z = 0;
  1013. int dat[ICM20645_AXES_NUM];
  1014. if (!strncmp(buf, "rst", 3)) {
  1015. err = ICM20645_ResetCalibration(client);
  1016. if (err)
  1017. GSE_ERR("reset offset err = %d\n", err);
  1018. } else if (3 == sscanf(buf, "0x%02X 0x%02X 0x%02X", &x, &y, &z)) {
  1019. dat[ICM20645_AXIS_X] = x;
  1020. dat[ICM20645_AXIS_Y] = y;
  1021. dat[ICM20645_AXIS_Z] = z;
  1022. err = ICM20645_WriteCalibration(client, dat);
  1023. if (err)
  1024. GSE_ERR("write calibration err = %d\n", err);
  1025. } else
  1026. GSE_ERR("invalid format\n");
  1027. return count;
  1028. }
  1029. /*----------------------------------------------------------------------------*/
  1030. static ssize_t show_self_value(struct device_driver *ddri, char *buf)
  1031. {
  1032. struct i2c_client *client = icm20645_i2c_client;
  1033. if (NULL == client) {
  1034. GSE_ERR("i2c client is null!!\n");
  1035. return 0;
  1036. }
  1037. return snprintf(buf, 8, "%s\n", selftestRes);
  1038. }
  1039. /*----------------------------------------------------------------------------*/
  1040. static ssize_t store_self_value(struct device_driver *ddri, const char *buf, size_t count)
  1041. { /*write anything to this register will trigger the process */
  1042. int ret = 0;
  1043. struct item {
  1044. s16 raw[ICM20645_AXES_NUM];
  1045. };
  1046. struct i2c_client *client = icm20645_i2c_client;
  1047. int idx = 0, res = 0, num = 0;
  1048. struct item *prv = NULL, *nxt = NULL;
  1049. s32 avg_prv[ICM20645_AXES_NUM] = { 0, 0, 0 };
  1050. s32 avg_nxt[ICM20645_AXES_NUM] = { 0, 0, 0 };
  1051. ret = kstrtoint(buf, 10, &num);
  1052. if (ret != 0) {
  1053. GSE_ERR("parse number fail\n");
  1054. return count;
  1055. } else if (num == 0) {
  1056. GSE_ERR("invalid data count\n");
  1057. return count;
  1058. }
  1059. prv = kcalloc(num, sizeof(*prv), GFP_KERNEL);
  1060. prv = kcalloc(num, sizeof(*nxt), GFP_KERNEL);
  1061. if (!prv || !nxt)
  1062. goto exit;
  1063. GSE_LOG("NORMAL:\n");
  1064. ICM20645_SetPowerMode(client, true);
  1065. msleep(50);
  1066. for (idx = 0; idx < num; idx++) {
  1067. res = ICM20645_ReadData(client, prv[idx].raw);
  1068. if (res) {
  1069. GSE_ERR("read data fail: %d\n", res);
  1070. goto exit;
  1071. }
  1072. avg_prv[ICM20645_AXIS_X] += prv[idx].raw[ICM20645_AXIS_X];
  1073. avg_prv[ICM20645_AXIS_Y] += prv[idx].raw[ICM20645_AXIS_Y];
  1074. avg_prv[ICM20645_AXIS_Z] += prv[idx].raw[ICM20645_AXIS_Z];
  1075. GSE_LOG("[%5d %5d %5d]\n", prv[idx].raw[ICM20645_AXIS_X], prv[idx].raw[ICM20645_AXIS_Y],
  1076. prv[idx].raw[ICM20645_AXIS_Z]);
  1077. }
  1078. avg_prv[ICM20645_AXIS_X] /= num;
  1079. avg_prv[ICM20645_AXIS_Y] /= num;
  1080. avg_prv[ICM20645_AXIS_Z] /= num;
  1081. /*initial setting for self test */
  1082. GSE_LOG("SELFTEST:\n");
  1083. for (idx = 0; idx < num; idx++) {
  1084. res = ICM20645_ReadData(client, nxt[idx].raw);
  1085. if (res) {
  1086. GSE_ERR("read data fail: %d\n", res);
  1087. goto exit;
  1088. }
  1089. avg_nxt[ICM20645_AXIS_X] += nxt[idx].raw[ICM20645_AXIS_X];
  1090. avg_nxt[ICM20645_AXIS_Y] += nxt[idx].raw[ICM20645_AXIS_Y];
  1091. avg_nxt[ICM20645_AXIS_Z] += nxt[idx].raw[ICM20645_AXIS_Z];
  1092. GSE_LOG("[%5d %5d %5d]\n", nxt[idx].raw[ICM20645_AXIS_X], nxt[idx].raw[ICM20645_AXIS_Y],
  1093. nxt[idx].raw[ICM20645_AXIS_Z]);
  1094. }
  1095. avg_nxt[ICM20645_AXIS_X] /= num;
  1096. avg_nxt[ICM20645_AXIS_Y] /= num;
  1097. avg_nxt[ICM20645_AXIS_Z] /= num;
  1098. GSE_LOG("X: %5d - %5d = %5d\n", avg_nxt[ICM20645_AXIS_X], avg_prv[ICM20645_AXIS_X],
  1099. avg_nxt[ICM20645_AXIS_X] - avg_prv[ICM20645_AXIS_X]);
  1100. GSE_LOG("Y: %5d - %5d = %5d\n", avg_nxt[ICM20645_AXIS_Y], avg_prv[ICM20645_AXIS_Y],
  1101. avg_nxt[ICM20645_AXIS_Y] - avg_prv[ICM20645_AXIS_Y]);
  1102. GSE_LOG("Z: %5d - %5d = %5d\n", avg_nxt[ICM20645_AXIS_Z], avg_prv[ICM20645_AXIS_Z],
  1103. avg_nxt[ICM20645_AXIS_Z] - avg_prv[ICM20645_AXIS_Z]);
  1104. if (!ICM20645_JudgeTestResult(client, avg_prv, avg_nxt)) {
  1105. GSE_LOG("SELFTEST : PASS\n");
  1106. strcpy(selftestRes, "y");
  1107. } else {
  1108. GSE_LOG("SELFTEST : FAIL\n");
  1109. strcpy(selftestRes, "n");
  1110. }
  1111. exit:
  1112. /*restore the setting */
  1113. icm20645_init_client(client, 0);
  1114. kfree(prv);
  1115. kfree(nxt);
  1116. return count;
  1117. }
  1118. /*----------------------------------------------------------------------------*/
  1119. static ssize_t show_selftest_value(struct device_driver *ddri, char *buf)
  1120. {
  1121. struct i2c_client *client = icm20645_i2c_client;
  1122. struct icm20645_i2c_data *obj;
  1123. if (NULL == client) {
  1124. GSE_ERR("i2c client is null!!\n");
  1125. return 0;
  1126. }
  1127. obj = i2c_get_clientdata(client);
  1128. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&obj->selftest));
  1129. }
  1130. /*----------------------------------------------------------------------------*/
  1131. static ssize_t store_selftest_value(struct device_driver *ddri, const char *buf, size_t count)
  1132. {
  1133. struct icm20645_i2c_data *obj = obj_i2c_data;
  1134. int tmp = 0;
  1135. int ret = 0;
  1136. if (NULL == obj) {
  1137. GSE_ERR("i2c data obj is null!!\n");
  1138. return 0;
  1139. }
  1140. ret = kstrtoint(buf, 10, &tmp);
  1141. if (ret == 0) {
  1142. if (atomic_read(&obj->selftest) && !tmp)
  1143. icm20645_init_client(obj->client, 0);
  1144. else if (!atomic_read(&obj->selftest) && tmp)
  1145. ICM20645_InitSelfTest(obj->client);
  1146. GSE_LOG("selftest: %d => %d\n", atomic_read(&obj->selftest), tmp);
  1147. atomic_set(&obj->selftest, tmp);
  1148. } else
  1149. GSE_ERR("invalid content: '%s', length = %zu\n", buf, count);
  1150. return count;
  1151. }
  1152. /*----------------------------------------------------------------------------*/
  1153. static ssize_t show_firlen_value(struct device_driver *ddri, char *buf)
  1154. {
  1155. #ifdef CONFIG_ICM20645_LOWPASS
  1156. struct i2c_client *client = icm20645_i2c_client;
  1157. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  1158. if (atomic_read(&obj->firlen)) {
  1159. int idx, len = atomic_read(&obj->firlen);
  1160. GSE_LOG("len = %2d, idx = %2d\n", obj->fir.num, obj->fir.idx);
  1161. for (idx = 0; idx < len; idx++) {
  1162. GSE_LOG("[%5d %5d %5d]\n", obj->fir.raw[idx][ICM20645_AXIS_X],
  1163. obj->fir.raw[idx][ICM20645_AXIS_Y], obj->fir.raw[idx][ICM20645_AXIS_Z]);
  1164. }
  1165. GSE_LOG("sum = [%5d %5d %5d]\n", obj->fir.sum[ICM20645_AXIS_X], obj->fir.sum[ICM20645_AXIS_Y],
  1166. obj->fir.sum[ICM20645_AXIS_Z]);
  1167. GSE_LOG("avg = [%5d %5d %5d]\n", obj->fir.sum[ICM20645_AXIS_X] / len,
  1168. obj->fir.sum[ICM20645_AXIS_Y] / len, obj->fir.sum[ICM20645_AXIS_Z] / len);
  1169. }
  1170. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&obj->firlen));
  1171. #else
  1172. return snprintf(buf, PAGE_SIZE, "not support\n");
  1173. #endif
  1174. }
  1175. /*----------------------------------------------------------------------------*/
  1176. static ssize_t store_firlen_value(struct device_driver *ddri, const char *buf, size_t count)
  1177. {
  1178. #ifdef CONFIG_ICM20645_LOWPASS
  1179. struct i2c_client *client = icm20645_i2c_client;
  1180. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  1181. int firlen = 0;
  1182. int ret = 0;
  1183. ret = kstrtoint(buf, 10, &firlen);
  1184. if (ret != 0)
  1185. GSE_ERR("invallid format\n");
  1186. else if (firlen > C_MAX_FIR_LENGTH)
  1187. GSE_ERR("exceeds maximum filter length\n");
  1188. else {
  1189. atomic_set(&obj->firlen, firlen);
  1190. if (0 == firlen) {
  1191. atomic_set(&obj->fir_en, 0);
  1192. } else {
  1193. memset(&obj->fir, 0x00, sizeof(obj->fir));
  1194. atomic_set(&obj->fir_en, 1);
  1195. }
  1196. }
  1197. #endif
  1198. return count;
  1199. }
  1200. /*----------------------------------------------------------------------------*/
  1201. static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
  1202. {
  1203. ssize_t res = 0;
  1204. struct icm20645_i2c_data *obj = obj_i2c_data;
  1205. if (obj == NULL) {
  1206. GSE_ERR("i2c_data obj is null!!\n");
  1207. return 0;
  1208. }
  1209. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
  1210. return res;
  1211. }
  1212. /*----------------------------------------------------------------------------*/
  1213. static ssize_t store_trace_value(struct device_driver *ddri, const char *buf, size_t count)
  1214. {
  1215. struct icm20645_i2c_data *obj = obj_i2c_data;
  1216. int trace = 0;
  1217. if (obj == NULL) {
  1218. GSE_ERR("i2c_data obj is null!!\n");
  1219. return 0;
  1220. }
  1221. if (1 == sscanf(buf, "0x%x", &trace))
  1222. atomic_set(&obj->trace, trace);
  1223. else
  1224. GSE_ERR("invalid content: '%s', length = %zu\n", buf, count);
  1225. return count;
  1226. }
  1227. /*----------------------------------------------------------------------------*/
  1228. static ssize_t show_status_value(struct device_driver *ddri, char *buf)
  1229. {
  1230. ssize_t len = 0;
  1231. struct icm20645_i2c_data *obj = obj_i2c_data;
  1232. if (obj == NULL) {
  1233. GSE_ERR("i2c_data obj is null!!\n");
  1234. return 0;
  1235. }
  1236. if (obj->hw) {
  1237. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: %d %d (%d %d)\n",
  1238. obj->hw->i2c_num, obj->hw->direction, obj->hw->power_id, obj->hw->power_vol);
  1239. } else {
  1240. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: NULL\n");
  1241. }
  1242. return len;
  1243. }
  1244. static ssize_t show_chip_orientation(struct device_driver *ddri, char *buf)
  1245. {
  1246. ssize_t _tLength = 0;
  1247. struct acc_hw *_ptAccelHw = hw;
  1248. GSE_LOG("[%s] default direction: %d\n", __func__, _ptAccelHw->direction);
  1249. _tLength = snprintf(buf, PAGE_SIZE, "default direction = %d\n", _ptAccelHw->direction);
  1250. return _tLength;
  1251. }
  1252. static ssize_t store_chip_orientation(struct device_driver *ddri, const char *buf, size_t tCount)
  1253. {
  1254. int _nDirection = 0, ret = 0;
  1255. struct icm20645_i2c_data *_pt_i2c_obj = obj_i2c_data;
  1256. if (NULL == _pt_i2c_obj)
  1257. return 0;
  1258. ret = kstrtoint(buf, 10, &_nDirection);
  1259. if (ret == 0) {
  1260. if (hwmsen_get_convert(_nDirection, &_pt_i2c_obj->cvt))
  1261. GSE_ERR("ERR: fail to set direction\n");
  1262. }
  1263. GSE_LOG("[%s] set direction: %d\n", __func__, _nDirection);
  1264. return tCount;
  1265. }
  1266. /*----------------------------------------------------------------------------*/
  1267. static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
  1268. static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
  1269. static DRIVER_ATTR(cali, S_IWUSR | S_IRUGO, show_cali_value, store_cali_value);
  1270. static DRIVER_ATTR(self, S_IWUSR | S_IRUGO, show_selftest_value, store_selftest_value);
  1271. static DRIVER_ATTR(selftest, S_IWUSR | S_IRUGO, show_self_value, store_self_value);
  1272. static DRIVER_ATTR(firlen, S_IWUSR | S_IRUGO, show_firlen_value, store_firlen_value);
  1273. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, show_trace_value, store_trace_value);
  1274. static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
  1275. static DRIVER_ATTR(orientation, S_IWUSR | S_IRUGO, show_chip_orientation, store_chip_orientation);
  1276. /*----------------------------------------------------------------------------*/
  1277. static struct driver_attribute *icm20645_attr_list[] = {
  1278. &driver_attr_chipinfo, /*chip information */
  1279. &driver_attr_sensordata, /*dump sensor data */
  1280. &driver_attr_cali, /*show calibration data */
  1281. &driver_attr_self, /*self test demo */
  1282. &driver_attr_selftest, /*self control: 0: disable, 1: enable */
  1283. &driver_attr_firlen, /*filter length: 0: disable, others: enable */
  1284. &driver_attr_trace, /*trace log */
  1285. &driver_attr_status,
  1286. &driver_attr_orientation,
  1287. };
  1288. /*----------------------------------------------------------------------------*/
  1289. static int icm20645_create_attr(struct device_driver *driver)
  1290. {
  1291. int idx = 0, err = 0;
  1292. int num = (int)(sizeof(icm20645_attr_list) / sizeof(icm20645_attr_list[0]));
  1293. if (driver == NULL)
  1294. return -EINVAL;
  1295. for (idx = 0; idx < num; idx++) {
  1296. err = driver_create_file(driver, icm20645_attr_list[idx]);
  1297. if (0 != err) {
  1298. GSE_ERR("driver_create_file (%s) = %d\n", icm20645_attr_list[idx]->attr.name, err);
  1299. break;
  1300. }
  1301. }
  1302. return err;
  1303. }
  1304. /*----------------------------------------------------------------------------*/
  1305. static int icm20645_delete_attr(struct device_driver *driver)
  1306. {
  1307. int idx = 0, err = 0;
  1308. int num = (int)(sizeof(icm20645_attr_list) / sizeof(icm20645_attr_list[0]));
  1309. if (driver == NULL)
  1310. return -EINVAL;
  1311. for (idx = 0; idx < num; idx++)
  1312. driver_remove_file(driver, icm20645_attr_list[idx]);
  1313. return err;
  1314. }
  1315. static int icm20645_open(struct inode *inode, struct file *file)
  1316. {
  1317. file->private_data = icm20645_i2c_client;
  1318. if (file->private_data == NULL) {
  1319. GSE_ERR("null pointer!!\n");
  1320. return -EINVAL;
  1321. }
  1322. return nonseekable_open(inode, file);
  1323. }
  1324. /*----------------------------------------------------------------------------*/
  1325. static int icm20645_release(struct inode *inode, struct file *file)
  1326. {
  1327. file->private_data = NULL;
  1328. return 0;
  1329. }
  1330. /*----------------------------------------------------------------------------*/
  1331. #ifdef CONFIG_COMPAT
  1332. static long icm20645_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1333. {
  1334. long err = 0;
  1335. void __user *arg32 = compat_ptr(arg);
  1336. if (!file->f_op || !file->f_op->unlocked_ioctl)
  1337. return -ENOTTY;
  1338. switch (cmd) {
  1339. case COMPAT_GSENSOR_IOCTL_READ_SENSORDATA:
  1340. if (arg32 == NULL) {
  1341. err = -EINVAL;
  1342. break;
  1343. }
  1344. err = file->f_op->unlocked_ioctl(file, GSENSOR_IOCTL_READ_SENSORDATA, (unsigned long)arg32);
  1345. if (err) {
  1346. GSE_ERR("GSENSOR_IOCTL_READ_SENSORDATA unlocked_ioctl failed.");
  1347. return err;
  1348. }
  1349. break;
  1350. case COMPAT_GSENSOR_IOCTL_SET_CALI:
  1351. if (arg32 == NULL) {
  1352. err = -EINVAL;
  1353. break;
  1354. }
  1355. err = file->f_op->unlocked_ioctl(file, GSENSOR_IOCTL_SET_CALI, (unsigned long)arg32);
  1356. if (err) {
  1357. GSE_ERR("GSENSOR_IOCTL_SET_CALI unlocked_ioctl failed.");
  1358. return err;
  1359. }
  1360. break;
  1361. case COMPAT_GSENSOR_IOCTL_GET_CALI:
  1362. if (arg32 == NULL) {
  1363. err = -EINVAL;
  1364. break;
  1365. }
  1366. err = file->f_op->unlocked_ioctl(file, GSENSOR_IOCTL_GET_CALI, (unsigned long)arg32);
  1367. if (err) {
  1368. GSE_ERR("GSENSOR_IOCTL_GET_CALI unlocked_ioctl failed.");
  1369. return err;
  1370. }
  1371. break;
  1372. case COMPAT_GSENSOR_IOCTL_CLR_CALI:
  1373. if (arg32 == NULL) {
  1374. err = -EINVAL;
  1375. break;
  1376. }
  1377. err = file->f_op->unlocked_ioctl(file, GSENSOR_IOCTL_CLR_CALI, (unsigned long)arg32);
  1378. if (err) {
  1379. GSE_ERR("GSENSOR_IOCTL_CLR_CALI unlocked_ioctl failed.");
  1380. return err;
  1381. }
  1382. break;
  1383. default:
  1384. GSE_ERR("unknown IOCTL: 0x%08x\n", cmd);
  1385. err = -ENOIOCTLCMD;
  1386. break;
  1387. }
  1388. return err;
  1389. }
  1390. #endif
  1391. static long icm20645_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1392. {
  1393. struct i2c_client *client = (struct i2c_client *)file->private_data;
  1394. struct icm20645_i2c_data *obj = (struct icm20645_i2c_data *)i2c_get_clientdata(client);
  1395. char strbuf[ICM20645_BUFSIZE];
  1396. void __user *data;
  1397. long err = 0;
  1398. int cali[3];
  1399. struct SENSOR_DATA sensor_data = {0};
  1400. if (_IOC_DIR(cmd) & _IOC_READ)
  1401. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  1402. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  1403. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  1404. if (err) {
  1405. GSE_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  1406. return -EFAULT;
  1407. }
  1408. switch (cmd) {
  1409. case GSENSOR_IOCTL_INIT:
  1410. icm20645_init_client(client, 0);
  1411. break;
  1412. case GSENSOR_IOCTL_READ_CHIPINFO:
  1413. data = (void __user *)arg;
  1414. if (data == NULL) {
  1415. err = -EINVAL;
  1416. break;
  1417. }
  1418. ICM20645_ReadChipInfo(client, strbuf, ICM20645_BUFSIZE);
  1419. if (copy_to_user(data, strbuf, strlen(strbuf) + 1)) {
  1420. err = -EFAULT;
  1421. break;
  1422. }
  1423. break;
  1424. case GSENSOR_IOCTL_READ_SENSORDATA:
  1425. data = (void __user *)arg;
  1426. if (data == NULL) {
  1427. err = -EINVAL;
  1428. break;
  1429. }
  1430. ICM20645_ReadSensorData(client, strbuf, ICM20645_BUFSIZE);
  1431. if (copy_to_user(data, strbuf, strlen(strbuf) + 1)) {
  1432. err = -EFAULT;
  1433. break;
  1434. }
  1435. break;
  1436. case GSENSOR_IOCTL_READ_GAIN:
  1437. data = (void __user *)arg;
  1438. if (data == NULL) {
  1439. err = -EINVAL;
  1440. break;
  1441. }
  1442. if (copy_to_user(data, &gsensor_gain, sizeof(struct GSENSOR_VECTOR3D))) {
  1443. err = -EFAULT;
  1444. break;
  1445. }
  1446. break;
  1447. case GSENSOR_IOCTL_READ_RAW_DATA:
  1448. data = (void __user *)arg;
  1449. if (data == NULL) {
  1450. err = -EINVAL;
  1451. break;
  1452. }
  1453. if (atomic_read(&obj->suspend)) {
  1454. err = -EINVAL;
  1455. } else {
  1456. ICM20645_ReadRawData(client, strbuf);
  1457. if (copy_to_user(data, strbuf, strlen(strbuf) + 1)) {
  1458. err = -EFAULT;
  1459. break;
  1460. }
  1461. }
  1462. break;
  1463. case GSENSOR_IOCTL_SET_CALI:
  1464. data = (void __user *)arg;
  1465. if (data == NULL) {
  1466. err = -EINVAL;
  1467. break;
  1468. }
  1469. if (copy_from_user(&sensor_data, data, sizeof(sensor_data))) {
  1470. err = -EFAULT;
  1471. break;
  1472. }
  1473. if (atomic_read(&obj->suspend)) {
  1474. GSE_ERR("Perform calibration in suspend state!!\n");
  1475. err = -EINVAL;
  1476. } else {
  1477. cali[ICM20645_AXIS_X] = sensor_data.x * obj->reso->sensitivity / GRAVITY_EARTH_1000;
  1478. cali[ICM20645_AXIS_Y] = sensor_data.y * obj->reso->sensitivity / GRAVITY_EARTH_1000;
  1479. cali[ICM20645_AXIS_Z] = sensor_data.z * obj->reso->sensitivity / GRAVITY_EARTH_1000;
  1480. err = ICM20645_WriteCalibration(client, cali);
  1481. }
  1482. break;
  1483. case GSENSOR_IOCTL_CLR_CALI:
  1484. err = ICM20645_ResetCalibration(client);
  1485. break;
  1486. case GSENSOR_IOCTL_GET_CALI:
  1487. data = (void __user *)arg;
  1488. if (data == NULL) {
  1489. err = -EINVAL;
  1490. break;
  1491. }
  1492. err = ICM20645_ReadCalibration(client, cali);
  1493. if (err)
  1494. break;
  1495. sensor_data.x = cali[ICM20645_AXIS_X] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  1496. sensor_data.y = cali[ICM20645_AXIS_Y] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  1497. sensor_data.z = cali[ICM20645_AXIS_Z] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
  1498. if (copy_to_user(data, &sensor_data, sizeof(sensor_data))) {
  1499. err = -EFAULT;
  1500. break;
  1501. }
  1502. break;
  1503. default:
  1504. GSE_ERR("unknown IOCTL: 0x%08x\n", cmd);
  1505. err = -ENOIOCTLCMD;
  1506. break;
  1507. }
  1508. return err;
  1509. }
  1510. /*----------------------------------------------------------------------------*/
  1511. static const struct file_operations icm20645_fops = {
  1512. .open = icm20645_open,
  1513. .release = icm20645_release,
  1514. .unlocked_ioctl = icm20645_unlocked_ioctl,
  1515. #ifdef CONFIG_COMPAT
  1516. .compat_ioctl = icm20645_compat_ioctl,
  1517. #endif
  1518. };
  1519. /*----------------------------------------------------------------------------*/
  1520. static struct miscdevice icm20645_device = {
  1521. .minor = MISC_DYNAMIC_MINOR,
  1522. .name = "gsensor",
  1523. .fops = &icm20645_fops,
  1524. };
  1525. /*----------------------------------------------------------------------------*/
  1526. #ifndef USE_EARLY_SUSPEND
  1527. /*----------------------------------------------------------------------------*/
  1528. static int icm20645_suspend(struct i2c_client *client, pm_message_t msg)
  1529. {
  1530. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  1531. int err = 0;
  1532. GSE_FUN();
  1533. if (msg.event == PM_EVENT_SUSPEND) {
  1534. if (obj == NULL) {
  1535. GSE_ERR("null pointer!!\n");
  1536. return -EINVAL;
  1537. }
  1538. atomic_set(&obj->suspend, 1);
  1539. err = ICM20645_SetPowerMode(obj->client, false);
  1540. if (err) {
  1541. GSE_ERR("write power control fail!!\n");
  1542. return err;
  1543. }
  1544. ICM20645_power(obj->hw, 0);
  1545. GSE_LOG("icm20645_suspend ok\n");
  1546. }
  1547. return err;
  1548. }
  1549. /*----------------------------------------------------------------------------*/
  1550. static int icm20645_resume(struct i2c_client *client)
  1551. {
  1552. struct icm20645_i2c_data *obj = i2c_get_clientdata(client);
  1553. int err = 0;
  1554. GSE_FUN();
  1555. if (obj == NULL) {
  1556. GSE_ERR("null pointer!!\n");
  1557. return -EINVAL;
  1558. }
  1559. ICM20645_power(obj->hw, 1);
  1560. err = icm20645_init_client(client, 0);
  1561. if (err) {
  1562. GSE_ERR("initialize client fail!!\n");
  1563. return err;
  1564. }
  1565. atomic_set(&obj->suspend, 0);
  1566. GSE_LOG("icm20645_resume ok\n");
  1567. return 0;
  1568. }
  1569. /*----------------------------------------------------------------------------*/
  1570. #else /*CONFIG_HAS_EARLY_SUSPEND is defined */
  1571. /*----------------------------------------------------------------------------*/
  1572. static void icm20645_early_suspend(struct early_suspend *h)
  1573. {
  1574. struct icm20645_i2c_data *obj = container_of(h, struct icm20645_i2c_data, early_drv);
  1575. int err = 0;
  1576. GSE_FUN();
  1577. if (obj == NULL) {
  1578. GSE_ERR("null pointer!!\n");
  1579. return;
  1580. }
  1581. atomic_set(&obj->suspend, 1);
  1582. err = ICM20645_SetPowerMode(obj->client, false);
  1583. if (err) {
  1584. GSE_ERR("write power control fail!!\n");
  1585. return;
  1586. }
  1587. if (ICM20645_gyro_mode() == false)
  1588. ICM20645_Dev_Reset(obj->client);
  1589. obj->bandwidth = 0;
  1590. sensor_power = false;
  1591. ICM20645_power(obj->hw, 0);
  1592. }
  1593. /*----------------------------------------------------------------------------*/
  1594. static void icm20645_late_resume(struct early_suspend *h)
  1595. {
  1596. struct icm20645_i2c_data *obj = container_of(h, struct icm20645_i2c_data, early_drv);
  1597. int err = 0;
  1598. GSE_FUN();
  1599. if (obj == NULL) {
  1600. GSE_ERR("null pointer!!\n");
  1601. return;
  1602. }
  1603. ICM20645_power(obj->hw, 1);
  1604. err = icm20645_init_client(obj->client, 0);
  1605. if (err) {
  1606. GSE_ERR("initialize client fail!!\n");
  1607. return;
  1608. }
  1609. atomic_set(&obj->suspend, 0);
  1610. }
  1611. /*----------------------------------------------------------------------------*/
  1612. #endif /*CONFIG_HAS_EARLYSUSPEND */
  1613. /*----------------------------------------------------------------------------*/
  1614. static int icm20645_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  1615. {
  1616. strcpy(info->type, ICM20645_DEV_NAME);
  1617. return 0;
  1618. }
  1619. static int icm20645_open_report_data(int open)
  1620. {
  1621. return 0;
  1622. }
  1623. static int icm20645_enable_nodata(int en)
  1624. {
  1625. int res = 0;
  1626. int retry = 0;
  1627. bool power = false;
  1628. if (1 == en)
  1629. power = true;
  1630. if (0 == en)
  1631. power = false;
  1632. for (retry = 0; retry < 3; retry++) {
  1633. res = ICM20645_SetPowerMode(obj_i2c_data->client, power);
  1634. if (res == 0) {
  1635. GSE_LOG("ICM20645_SetPowerMode done\n");
  1636. break;
  1637. }
  1638. GSE_LOG("ICM20645_SetPowerMode fail\n");
  1639. }
  1640. if (res != ICM20645_SUCCESS) {
  1641. GSE_LOG("ICM20645_SetPowerMode fail!\n");
  1642. return -1;
  1643. }
  1644. GSE_LOG("icm20645_enable_nodata OK!\n");
  1645. return 0;
  1646. }
  1647. static int icm20645_set_delay(u64 ns)
  1648. {
  1649. #if 0
  1650. int value = 0;
  1651. int sample_delay = 0;
  1652. int err;
  1653. value = (int)ns / 1000 / 1000;
  1654. if (value <= 5)
  1655. sample_delay = ICM20645_BW_184HZ;
  1656. else if (value <= 10)
  1657. sample_delay = ICM20645_BW_94HZ;
  1658. else
  1659. sample_delay = ICM20645_BW_44HZ;
  1660. err = ICM20645_SetBWRate(obj_i2c_data->client, sample_delay);
  1661. if (err != ICM20645_SUCCESS) {
  1662. GSE_ERR("icm20645_set_delay Set delay parameter error!\n");
  1663. return -1;
  1664. }
  1665. GSE_LOG("icm20645_set_delay (%d)\n", value);
  1666. #endif
  1667. return 0;
  1668. }
  1669. static int icm20645_get_data(int *x, int *y, int *z, int *status)
  1670. {
  1671. char buff[ICM20645_BUFSIZE];
  1672. ICM20645_ReadSensorData(obj_i2c_data->client, buff, ICM20645_BUFSIZE);
  1673. if (3 == sscanf(buff, "%x %x %x", x, y, z))
  1674. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1675. return 0;
  1676. }
  1677. /*----------------------------------------------------------------------------*/
  1678. static int icm20645_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1679. {
  1680. struct i2c_client *new_client;
  1681. struct icm20645_i2c_data *obj;
  1682. int err = 0;
  1683. struct acc_control_path ctl = { 0 };
  1684. struct acc_data_path data = { 0 };
  1685. GSE_FUN();
  1686. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  1687. if (!obj) {
  1688. err = -ENOMEM;
  1689. goto exit;
  1690. }
  1691. memset(obj, 0, sizeof(struct icm20645_i2c_data));
  1692. obj->hw = hw;
  1693. err = hwmsen_get_convert(obj->hw->direction, &obj->cvt);
  1694. if (err) {
  1695. GSE_ERR("invalid direction: %d\n", obj->hw->direction);
  1696. goto exit;
  1697. }
  1698. obj_i2c_data = obj;
  1699. obj->client = client;
  1700. new_client = obj->client;
  1701. i2c_set_clientdata(new_client, obj);
  1702. atomic_set(&obj->trace, 0);
  1703. atomic_set(&obj->suspend, 0);
  1704. #ifdef CONFIG_ICM20645_LOWPASS
  1705. if (obj->hw->firlen > C_MAX_FIR_LENGTH)
  1706. atomic_set(&obj->firlen, C_MAX_FIR_LENGTH);
  1707. else
  1708. atomic_set(&obj->firlen, obj->hw->firlen);
  1709. if (atomic_read(&obj->firlen) > 0)
  1710. atomic_set(&obj->fir_en, 1);
  1711. #endif
  1712. icm20645_i2c_client = new_client;
  1713. ICM20645_Dev_Reset(new_client);
  1714. err = icm20645_init_client(new_client, 1);
  1715. if (err)
  1716. goto exit_init_failed;
  1717. err = misc_register(&icm20645_device);
  1718. if (err) {
  1719. GSE_ERR("icm20645_device register failed\n");
  1720. goto exit_misc_device_register_failed;
  1721. }
  1722. ctl.is_use_common_factory = false;
  1723. err = icm20645_create_attr(&(icm20645_init_info.platform_diver_addr->driver));
  1724. if (err) {
  1725. GSE_ERR("create attribute err = %d\n", err);
  1726. goto exit_create_attr_failed;
  1727. }
  1728. ctl.open_report_data = icm20645_open_report_data;
  1729. ctl.enable_nodata = icm20645_enable_nodata;
  1730. ctl.set_delay = icm20645_set_delay;
  1731. ctl.is_report_input_direct = false;
  1732. ctl.is_support_batch = obj->hw->is_batch_supported;
  1733. err = acc_register_control_path(&ctl);
  1734. if (err) {
  1735. GSE_ERR("register acc control path err\n");
  1736. goto exit_kfree;
  1737. }
  1738. data.get_data = icm20645_get_data;
  1739. data.vender_div = 1000;
  1740. err = acc_register_data_path(&data);
  1741. if (err) {
  1742. GSE_ERR("register acc data path err= %d\n", err);
  1743. goto exit_kfree;
  1744. }
  1745. #ifdef USE_EARLY_SUSPEND
  1746. obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 2,
  1747. obj->early_drv.suspend = icm20645_early_suspend,
  1748. obj->early_drv.resume = icm20645_late_resume, register_early_suspend(&obj->early_drv);
  1749. #endif
  1750. icm20645_init_flag = 0;
  1751. GSE_LOG("%s: OK\n", __func__);
  1752. return 0;
  1753. exit_create_attr_failed:
  1754. misc_deregister(&icm20645_device);
  1755. exit_misc_device_register_failed:
  1756. exit_init_failed:
  1757. /*i2c_detach_client(new_client);*/
  1758. exit_kfree:
  1759. kfree(obj);
  1760. exit:
  1761. GSE_ERR("%s: err = %d\n", __func__, err);
  1762. icm20645_init_flag = -1;
  1763. return err;
  1764. }
  1765. /*----------------------------------------------------------------------------*/
  1766. static int icm20645_i2c_remove(struct i2c_client *client)
  1767. {
  1768. int err = 0;
  1769. err = icm20645_delete_attr(&(icm20645_init_info.platform_diver_addr->driver));
  1770. if (err)
  1771. GSE_ERR("icm20645_delete_attr fail: %d\n", err);
  1772. err = misc_deregister(&icm20645_device);
  1773. if (err)
  1774. GSE_ERR("misc_deregister fail: %d\n", err);
  1775. err = hwmsen_detach(ID_ACCELEROMETER);
  1776. if (err)
  1777. GSE_ERR("hwmsen_detach fail: %d\n", err);
  1778. icm20645_i2c_client = NULL;
  1779. i2c_unregister_device(client);
  1780. kfree(i2c_get_clientdata(client));
  1781. return 0;
  1782. }
  1783. /*----------------------------------------------------------------------------*/
  1784. /*----------------------------------------------------------------------------*/
  1785. static int icm20645_remove(void)
  1786. {
  1787. GSE_FUN();
  1788. ICM20645_power(hw, 0);
  1789. i2c_del_driver(&icm20645_i2c_driver);
  1790. return 0;
  1791. }
  1792. /*----------------------------------------------------------------------------*/
  1793. static int icm20645_local_init(void)
  1794. {
  1795. ICM20645_power(hw, 1);
  1796. if (i2c_add_driver(&icm20645_i2c_driver)) {
  1797. GSE_ERR("add driver error\n");
  1798. return -1;
  1799. }
  1800. if (-1 == icm20645_init_flag)
  1801. return -1;
  1802. return 0;
  1803. }
  1804. /*----------------------------------------------------------------------------*/
  1805. static int __init icm20645gse_init(void)
  1806. {
  1807. const char *name = "mediatek,icm20645g";
  1808. hw = get_accel_dts_func(name, hw);
  1809. if (!hw)
  1810. GSE_ERR("get dts info fail\n");
  1811. acc_driver_add(&icm20645_init_info);
  1812. return 0;
  1813. }
  1814. /*----------------------------------------------------------------------------*/
  1815. static void __exit icm20645gse_exit(void)
  1816. {
  1817. GSE_FUN();
  1818. }
  1819. /*----------------------------------------------------------------------------*/
  1820. module_init(icm20645gse_init);
  1821. module_exit(icm20645gse_exit);
  1822. /*----------------------------------------------------------------------------*/
  1823. MODULE_LICENSE("GPL");
  1824. MODULE_DESCRIPTION("ICM20645 gse driver");
  1825. MODULE_AUTHOR("Yucong.Xiong@mediatek.com");