mpu6500_input.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. $
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/input.h>
  20. #include <linux/input-polldev.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/irq.h>
  28. #include <linux/gpio.h>
  29. #include <linux/sched.h>
  30. #include <linux/fs.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/wakelock.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/of_gpio.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/sensors_core.h>
  37. #include <linux/sensor/mpu6500_platformdata.h>
  38. #include "./mpu6500_selftest.h"
  39. #include "./mpu6500_input.h"
  40. #define DEBUG 0
  41. #define MPU6500_MODE_NORMAL 0
  42. #define MPU6500_MODE_SLEEP 2
  43. #define MPU6500_MODE_WAKE_UP 3
  44. #define MAX_GYRO 32767
  45. #define MIN_GYRO -32768
  46. #define MPU6500_DEFAULT_DELAY 200000000LL
  47. #define MPU6500_LOGTIME 10
  48. #define LOG_RESULT_LOCATION(x) {\
  49. printk(KERN_ERR "%s:%s:%d result=%d\n",__FILE__,__func__,__LINE__, x);\
  50. }\
  51. #define CHECK_RESULT(x) {\
  52. result = x;\
  53. if (unlikely(result)) \
  54. LOG_RESULT_LOCATION(result);\
  55. }
  56. #ifndef MIN
  57. #define MIN(a, b) (((a) < (b))?(a):(b))
  58. #endif
  59. #ifndef MAX
  60. #define MAX(a, b) (((a) > (b))?(a):(b))
  61. #endif
  62. #define MPU6500_ACCEL_LPF_GAIN(x) (((x)*8)/10)
  63. #define INT_SRC_ORIENT 0x02
  64. #define INT_SRC_DISPLAY_ORIENT 0x08
  65. #define DMP_MASK_DIS_ORIEN 0xC0
  66. #define DMP_DIS_ORIEN_SHIFT 6
  67. #define MPU6500_GYRO_SPC_CFG 0x49
  68. #define MPU6500_REG_BANK_SEL 0x76
  69. #define MPU6500_CFG_SET_BIT 0x20
  70. #define MPU6500_CALIB_FILENAME "//data//mpu6500.cal"
  71. #define MPU6500_ACCEL_CAL_PATH "/efs/calibration_data"
  72. #define MPU6500_GYRO_CAL_PATH "/efs/gyro_cal_data"
  73. #define MODULE_NAME_ACCEL "accelerometer_sensor"
  74. #define MODULE_NAME_GYRO "gyro_sensor"
  75. struct mpu6500_v {
  76. union {
  77. s16 v[3];
  78. struct {
  79. s16 x;
  80. s16 y;
  81. s16 z;
  82. };
  83. };
  84. };
  85. struct motion_int_data {
  86. unsigned char pwr_mnt[2];
  87. unsigned char cfg;
  88. unsigned char accel_cfg;
  89. unsigned char gyro_cfg;
  90. unsigned char int_cfg;
  91. unsigned char smplrt_div;
  92. bool is_set;
  93. unsigned char accel_cfg2;
  94. };
  95. struct mpu6500_input_data {
  96. struct i2c_client *client;
  97. struct input_dev *accel_input;
  98. struct input_dev *gyro_input;
  99. struct motion_int_data mot_data;
  100. struct mutex mutex;
  101. struct wake_lock reactive_wake_lock;
  102. atomic_t accel_enable;
  103. ktime_t accel_delay;
  104. struct mpu6500_v acc_data;
  105. struct mpu6500_v gyro_data;
  106. atomic_t gyro_enable;
  107. ktime_t gyro_delay;
  108. atomic_t reactive_state;
  109. atomic_t reactive_enable;
  110. atomic_t motion_recg_enable;
  111. unsigned long motion_recg_st_time; //start-up time of motion interrupt
  112. unsigned char gyro_pwr_mgnt[2];
  113. unsigned char int_pin_cfg;
  114. u16 enabled_sensors;
  115. u16 sleep_sensors;
  116. u32 chip_pos;
  117. int current_delay;
  118. int irq;
  119. int count_logtime;
  120. int count_logtime_gyro;
  121. int gyro_bias[3];
  122. u8 mode;
  123. struct mpu6500_platform_data *pdata;
  124. #ifdef CONFIG_INPUT_MPU6500_POLLING
  125. struct delayed_work accel_work;
  126. struct delayed_work gyro_work;
  127. #endif
  128. struct device *accel_sensor_device;
  129. struct device *gyro_sensor_device;
  130. s16 acc_cal[3];
  131. bool factory_mode;
  132. struct regulator *lvs1_1p8;
  133. struct regulator *str_l19;
  134. };
  135. struct mpu6500_input_cfg {
  136. int dummy;
  137. };
  138. static struct mpu6500_input_data *gb_mpu_data;
  139. static int mpu6500_input_activate_devices(struct mpu6500_input_data *data,
  140. int sensors, bool enable);
  141. static void mpu6500_proc_msleep(unsigned int msecs,
  142. struct hrtimer_sleeper *sleeper, int sigs)
  143. {
  144. enum hrtimer_mode mode = HRTIMER_MODE_REL;
  145. int state = sigs ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  146. hrtimer_init(&sleeper->timer, CLOCK_MONOTONIC, mode);
  147. sleeper->timer._softexpires = ktime_set(0, msecs*NSEC_PER_MSEC);
  148. hrtimer_init_sleeper(sleeper, current);
  149. do {
  150. set_current_state(state);
  151. hrtimer_start(&sleeper->timer,
  152. sleeper->timer._softexpires, mode);
  153. if (sleeper->task)
  154. schedule();
  155. hrtimer_cancel(&sleeper->timer);
  156. mode = HRTIMER_MODE_ABS;
  157. } while (sleeper->task && !(sigs && signal_pending(current)));
  158. }
  159. void mpu6500_msleep(unsigned int msecs)
  160. {
  161. struct hrtimer_sleeper sleeper;
  162. mpu6500_proc_msleep(msecs, &sleeper, 0);
  163. }
  164. void mpu6500_msleep_interruptible(unsigned int msecs)
  165. {
  166. struct hrtimer_sleeper sleeper;
  167. mpu6500_proc_msleep(msecs, &sleeper, 1);
  168. }
  169. int mpu6500_i2c_write(struct i2c_client *i2c_client,
  170. unsigned int len, unsigned char *data)
  171. {
  172. struct i2c_msg msgs[1];
  173. int res;
  174. if (NULL == data || NULL == i2c_client)
  175. return -EINVAL;
  176. msgs[0].addr = i2c_client->addr;
  177. msgs[0].flags = 0; /* write */
  178. msgs[0].buf = (unsigned char *)data;
  179. msgs[0].len = len;
  180. res = i2c_transfer(i2c_client->adapter, msgs, 1);
  181. if (res < 1)
  182. return res;
  183. else
  184. return 0;
  185. }
  186. int mpu6500_i2c_read(struct i2c_client *i2c_client,
  187. unsigned int len, unsigned char *data)
  188. {
  189. struct i2c_msg msgs[2];
  190. int res;
  191. if (NULL == data || NULL == i2c_client)
  192. return -EINVAL;
  193. msgs[0].addr = i2c_client->addr;
  194. msgs[0].flags = I2C_M_RD;
  195. msgs[0].buf = data;
  196. msgs[0].len = len;
  197. res = i2c_transfer(i2c_client->adapter, msgs, 1);
  198. if (res < 1)
  199. return res;
  200. else
  201. return 0;
  202. }
  203. int mpu6500_i2c_write_single_reg(struct i2c_client *i2c_client,
  204. unsigned char reg, unsigned char value)
  205. {
  206. unsigned char data[2];
  207. data[0] = reg;
  208. data[1] = value;
  209. return mpu6500_i2c_write(i2c_client, 2, data);
  210. }
  211. int mpu6500_i2c_read_reg(struct i2c_client *i2c_client,
  212. unsigned char reg, unsigned int len,
  213. unsigned char *data)
  214. {
  215. struct i2c_msg msgs[2];
  216. int res;
  217. if (NULL == data || NULL == i2c_client)
  218. return -EINVAL;
  219. msgs[0].addr = i2c_client->addr;
  220. msgs[0].flags = 0; /* write */
  221. msgs[0].buf = &reg;
  222. msgs[0].len = 1;
  223. msgs[1].addr = i2c_client->addr;
  224. msgs[1].flags = I2C_M_RD;
  225. msgs[1].buf = data;
  226. msgs[1].len = (u16)len;
  227. res = i2c_transfer(i2c_client->adapter, msgs, 2);
  228. if (res < 1)
  229. return res;
  230. else
  231. return 0;
  232. }
  233. int mpu6500_i2c_read_fifo(struct i2c_client *i2c_client,
  234. unsigned short length, unsigned char *data)
  235. {
  236. int result;
  237. unsigned short bytes_read = 0;
  238. while (bytes_read < length) {
  239. unsigned short this_len = length - bytes_read;
  240. result =
  241. mpu6500_i2c_read_reg(i2c_client, MPUREG_FIFO_R_W, this_len,
  242. &data[bytes_read]);
  243. if (result) {
  244. return result;
  245. }
  246. bytes_read += this_len;
  247. }
  248. return 0;
  249. }
  250. int mpu6500_i2c_memory_write(struct i2c_client *i2c_client,
  251. unsigned short mem_addr, unsigned int len, unsigned char const *data)
  252. {
  253. unsigned char bank[2];
  254. unsigned char addr[2];
  255. unsigned char buf[513];
  256. struct i2c_msg msgs[3];
  257. int res;
  258. if (!data || !i2c_client)
  259. return -EINVAL;
  260. if (len >= (sizeof(buf) - 1))
  261. return -ENOMEM;
  262. bank[0] = MPUREG_BANK_SEL;
  263. bank[1] = mem_addr >> 8;
  264. addr[0] = MPUREG_MEM_START_ADDR;
  265. addr[1] = mem_addr & 0xFF;
  266. buf[0] = MPUREG_MEM_R_W;
  267. memcpy(buf + 1, data, len);
  268. /* write message */
  269. msgs[0].addr = i2c_client->addr;
  270. msgs[0].flags = 0;
  271. msgs[0].buf = bank;
  272. msgs[0].len = sizeof(bank);
  273. msgs[1].addr = i2c_client->addr;
  274. msgs[1].flags = 0;
  275. msgs[1].buf = addr;
  276. msgs[1].len = sizeof(addr);
  277. msgs[2].addr = i2c_client->addr;
  278. msgs[2].flags = 0;
  279. msgs[2].buf = (unsigned char *)buf;
  280. msgs[2].len = len + 1;
  281. res = i2c_transfer(i2c_client->adapter, msgs, 3);
  282. if (res != 3) {
  283. if (res >= 0)
  284. res = -EIO;
  285. return res;
  286. } else {
  287. return 0;
  288. }
  289. }
  290. int mpu6500_i2c_memory_read(struct i2c_client *i2c_client,
  291. unsigned short mem_addr, unsigned int len, unsigned char *data)
  292. {
  293. unsigned char bank[2];
  294. unsigned char addr[2];
  295. unsigned char buf;
  296. struct i2c_msg msgs[4];
  297. int res;
  298. if (!data || !i2c_client)
  299. return -EINVAL;
  300. bank[0] = MPUREG_BANK_SEL;
  301. bank[1] = mem_addr >> 8;
  302. addr[0] = MPUREG_MEM_START_ADDR;
  303. addr[1] = mem_addr & 0xFF;
  304. buf = MPUREG_MEM_R_W;
  305. /* write message */
  306. msgs[0].addr = i2c_client->addr;
  307. msgs[0].flags = 0;
  308. msgs[0].buf = bank;
  309. msgs[0].len = sizeof(bank);
  310. msgs[1].addr = i2c_client->addr;
  311. msgs[1].flags = 0;
  312. msgs[1].buf = addr;
  313. msgs[1].len = sizeof(addr);
  314. msgs[2].addr = i2c_client->addr;
  315. msgs[2].flags = 0;
  316. msgs[2].buf = &buf;
  317. msgs[2].len = 1;
  318. msgs[3].addr = i2c_client->addr;
  319. msgs[3].flags = I2C_M_RD;
  320. msgs[3].buf = data;
  321. msgs[3].len = len;
  322. res = i2c_transfer(i2c_client->adapter, msgs, 4);
  323. if (res != 4) {
  324. if (res >= 0)
  325. res = -EIO;
  326. return res;
  327. } else {
  328. return 0;
  329. }
  330. }
  331. static int mpu6500_input_set_mode(struct mpu6500_input_data *data, u8 mode)
  332. {
  333. int err = 0;
  334. data->mode = mode;
  335. if (mode == MPU6500_MODE_SLEEP) {
  336. err = mpu6500_input_activate_devices(data,
  337. MPU6500_SENSOR_ACCEL | MPU6500_SENSOR_GYRO, false);
  338. } else if (mode == MPU6500_MODE_NORMAL) {
  339. if (atomic_read(&data->accel_enable))
  340. err = mpu6500_input_activate_devices(data,
  341. MPU6500_SENSOR_ACCEL, true);
  342. if (atomic_read(&data->gyro_enable))
  343. err = mpu6500_input_activate_devices(data,
  344. MPU6500_SENSOR_GYRO, true);
  345. }
  346. return err;
  347. }
  348. static void mpu6500_input_report_accel_xyz(struct mpu6500_input_data *data)
  349. {
  350. u8 regs[6];
  351. int result;
  352. result = mpu6500_i2c_read_reg(data->client, MPUREG_ACCEL_XOUT_H, 6, regs);
  353. if (result) {
  354. pr_err("[SENSOR] %s: i2c_read err= %d\n", __func__, result);
  355. return;
  356. }
  357. data->acc_data.x = ((s16) ((s16) regs[0] << 8)) | regs[1];
  358. data->acc_data.y = ((s16) ((s16) regs[2] << 8)) | regs[3];
  359. data->acc_data.z = ((s16) ((s16) regs[4] << 8)) | regs[5];
  360. remap_sensor_data(data->acc_data.v, data->chip_pos);
  361. input_report_rel(data->accel_input, REL_X, data->acc_data.x - data->acc_cal[0]);
  362. input_report_rel(data->accel_input, REL_Y, data->acc_data.y - data->acc_cal[1]);
  363. input_report_rel(data->accel_input, REL_Z, data->acc_data.z - data->acc_cal[2]);
  364. if ((ktime_to_ns(data->accel_delay) * (int64_t)data->count_logtime)
  365. >= ((int64_t)MPU6500_LOGTIME * NSEC_PER_SEC)) {
  366. pr_info("[SENSOR] %s, %d, %d, %d (Count = %d)\n",
  367. __func__, data->acc_data.x, data->acc_data.y, data->acc_data.z, data->count_logtime);
  368. data->count_logtime = 0;
  369. } else
  370. data->count_logtime++;
  371. input_sync(data->accel_input);
  372. }
  373. static void mpu6500_input_report_gyro_xyz(struct mpu6500_input_data *data)
  374. {
  375. u8 regs[6];
  376. s16 raw_tmp[3];
  377. int result;
  378. result = mpu6500_i2c_read_reg(data->client, MPUREG_GYRO_XOUT_H, 6, regs);
  379. if (result) {
  380. pr_err("[SENSOR] %s: i2c_read err= %d\n", __func__, result);
  381. return;
  382. }
  383. raw_tmp[0] = (((s16) ((s16) regs[0] << 8)) | regs[1]);
  384. raw_tmp[1] = (((s16) ((s16) regs[2] << 8)) | regs[3]);
  385. raw_tmp[2] = (((s16) ((s16) regs[4] << 8)) | regs[5]);
  386. data->gyro_data.x = raw_tmp[0] - (s16) data->gyro_bias[0];
  387. data->gyro_data.y = raw_tmp[1] - (s16) data->gyro_bias[1];
  388. data->gyro_data.z = raw_tmp[2] - (s16) data->gyro_bias[2];
  389. if (!(data->gyro_data.x >> 15 == raw_tmp[0] >> 15) &&\
  390. !((s16) data->gyro_bias[0] >> 15 == raw_tmp[0] >> 15)) {
  391. pr_info("[SENSOR] %s GYRO X is overflowed!!!\n", __func__);
  392. data->gyro_data.x = (data->gyro_data.x >= 0 ? MIN_GYRO : MAX_GYRO);
  393. }
  394. if (!(data->gyro_data.y >> 15 == raw_tmp[1] >> 15) &&\
  395. !((s16) data->gyro_bias[1] >> 15 == raw_tmp[1] >> 15)) {
  396. pr_info("[SENSOR] %s GYRO Y is overflowed!!!\n", __func__);
  397. data->gyro_data.y = (data->gyro_data.y >= 0 ? MIN_GYRO : MAX_GYRO);
  398. }
  399. if (!(data->gyro_data.z >> 15 == raw_tmp[2] >> 15) &&\
  400. !((s16) data->gyro_bias[2] >> 15 == raw_tmp[2] >> 15)) {
  401. pr_info("[SENSOR] %s GYRO Z is overflowed!!!\n", __func__);
  402. data->gyro_data.z = (data->gyro_data.z >= 0 ? MIN_GYRO : MAX_GYRO);
  403. }
  404. remap_sensor_data(data->gyro_data.v, data->chip_pos);
  405. input_report_rel(data->gyro_input, REL_RX, data->gyro_data.x);
  406. input_report_rel(data->gyro_input, REL_RY, data->gyro_data.y);
  407. input_report_rel(data->gyro_input, REL_RZ, data->gyro_data.z);
  408. if ((ktime_to_ns(data->gyro_delay) * (int64_t)data->count_logtime)
  409. >= ((int64_t)MPU6500_LOGTIME * NSEC_PER_SEC)) {
  410. pr_info("[SENSOR] %s, %d, %d, %d (Count = %d)\n",
  411. __func__, data->gyro_data.x, data->gyro_data.y, data->gyro_data.z, data->count_logtime_gyro);
  412. data->count_logtime_gyro = 0;
  413. } else
  414. data->count_logtime_gyro++;
  415. input_sync(data->gyro_input);
  416. }
  417. static irqreturn_t mpu6500_input_irq_thread(int irq, void *dev)
  418. {
  419. struct mpu6500_input_data *data = (struct mpu6500_input_data *)dev;
  420. struct motion_int_data *mot_data = &data->mot_data;
  421. unsigned char reg;
  422. unsigned long timediff = 0;
  423. int result;
  424. if (!atomic_read(&data->reactive_enable)) {
  425. #ifdef CONFIG_INPUT_MPU6500_LP
  426. if (IS_LP_ENABLED(data->enabled_sensors))
  427. mpu6500_input_report_fifo_data(data);
  428. else {
  429. #ifndef CONFIG_INPUT_MPU6500_POLLING
  430. if (data->enabled_sensors & MPU6500_SENSOR_ACCEL)
  431. mpu6500_input_report_accel_xyz(data);
  432. if (data->enabled_sensors & MPU6500_SENSOR_GYRO)
  433. mpu6500_input_report_gyro_xyz(data);
  434. #endif
  435. }
  436. #else
  437. #ifndef CONFIG_INPUT_MPU6500_POLLING
  438. if (data->enabled_sensors & MPU6500_SENSOR_ACCEL)
  439. mpu6500_input_report_accel_xyz(data);
  440. if (data->enabled_sensors & MPU6500_SENSOR_GYRO)
  441. mpu6500_input_report_gyro_xyz(data);
  442. #endif
  443. #endif
  444. } else {
  445. result = mpu6500_i2c_read_reg(data->client,
  446. MPUREG_INT_STATUS, 1, &reg);
  447. if (result) {
  448. pr_err("[SENSOR] %s: i2c_read err= %d\n", __func__, result);
  449. goto done;
  450. }
  451. timediff = jiffies_to_msecs(jiffies - data->motion_recg_st_time);
  452. /* ignore motion interrupt happened in 100ms to skip intial erronous interrupt */
  453. if (timediff < 1000 && !(data->factory_mode)) {
  454. pr_debug("[SENSOR] %s: timediff = %ld msec\n",
  455. __func__, timediff);
  456. goto done;
  457. }
  458. if (reg & (1 << 6) || data->factory_mode) {
  459. /* handle motion recognition */
  460. atomic_set(&data->reactive_state, true);
  461. data->factory_mode = false;
  462. pr_info("[SENSOR] %s: motion interrupt happened\n", __func__);
  463. /* disable motion int */
  464. mpu6500_i2c_write_single_reg(data->client, MPUREG_INT_ENABLE, mot_data->int_cfg);
  465. wake_lock_timeout(&data->reactive_wake_lock, msecs_to_jiffies(2000));
  466. }
  467. }
  468. done:
  469. return IRQ_HANDLED;
  470. }
  471. static int mpu6500_input_set_fsr(struct mpu6500_input_data *data, int fsr)
  472. {
  473. unsigned char fsr_mask;
  474. int result;
  475. unsigned char reg;
  476. if (fsr <= 2000) {
  477. fsr_mask = 0x00;
  478. } else if (fsr <= 4000) {
  479. fsr_mask = 0x08;
  480. } else if (fsr <= 8000) {
  481. fsr_mask = 0x10;
  482. } else { /* fsr = [8001, oo) */
  483. fsr_mask = 0x18;
  484. }
  485. result =
  486. mpu6500_i2c_read_reg(data->client, MPUREG_ACCEL_CONFIG, 1, &reg);
  487. if (result) {
  488. LOG_RESULT_LOCATION(result);
  489. return result;
  490. }
  491. result =
  492. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG,
  493. reg | fsr_mask);
  494. if (result) {
  495. LOG_RESULT_LOCATION(result);
  496. return result;
  497. }
  498. return result;
  499. }
  500. #ifdef CONFIG_MPU6500_LP_MODE
  501. static int mpu6500_input_set_lp_mode(struct mpu6500_input_data *data,
  502. unsigned char lpa_freq)
  503. {
  504. unsigned char b = 0;
  505. /* Reducing the duration setting for lp mode */
  506. b = 0x1;
  507. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_INT_ENABLE, b);
  508. /* Setting the cycle bit and LPA wake up freq */
  509. mpu6500_i2c_read_reg(data->client, MPUREG_PWR_MGMT_1, 1, &b);
  510. b |= BIT_CYCLE | BIT_PD_PTAT;
  511. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1, b);
  512. mpu6500_i2c_read_reg(data->client, MPUREG_PWR_MGMT_2, 1, &b);
  513. b |= lpa_freq & BITS_LPA_WAKE_CTRL;
  514. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2, b);
  515. return 0;
  516. }
  517. #endif
  518. static int mpu6500_input_set_fp_mode(struct mpu6500_input_data *data)
  519. {
  520. unsigned char b;
  521. /* Resetting the cycle bit and LPA wake up freq */
  522. mpu6500_i2c_read_reg(data->client, MPUREG_PWR_MGMT_1, 1, &b);
  523. b &= ~BIT_CYCLE & ~BIT_PD_PTAT;
  524. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1, b);
  525. mpu6500_i2c_read_reg(data->client, MPUREG_PWR_MGMT_2, 1, &b);
  526. b &= ~BITS_LPA_WAKE_CTRL;
  527. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2, b);
  528. /* Resetting the duration setting for fp mode */
  529. b = (unsigned char)10 / ACCEL_MOT_DUR_LSB;
  530. mpu6500_i2c_write_single_reg(data->client,
  531. MPUREG_ACCEL_INTEL_ENABLE, b);
  532. return 0;
  533. }
  534. static int mpu6500_input_set_odr(struct mpu6500_input_data *data, int odr)
  535. {
  536. int result;
  537. unsigned char b;
  538. if (!data->enabled_sensors)
  539. return 0;
  540. b = (unsigned char)(odr);
  541. CHECK_RESULT(mpu6500_i2c_write_single_reg
  542. (data->client, MPUREG_SMPLRT_DIV, b));
  543. mpu6500_i2c_read_reg(data->client, MPUREG_PWR_MGMT_1, 1, &b);
  544. b &= BIT_CYCLE;
  545. if (b == BIT_CYCLE) {
  546. printk(KERN_INFO " Accel LP - > FP mode. \n ");
  547. mpu6500_input_set_fp_mode(data);
  548. }
  549. return result;
  550. }
  551. #if defined(CONFIG_MPU6500_ADJUST_SMART_ALERT)
  552. static int mpu6500_input_set_motion_interrupt(struct mpu6500_input_data *data,
  553. int enable, bool factory_test)
  554. {
  555. struct motion_int_data *mot_data = &data->mot_data;
  556. unsigned char reg;
  557. atomic_set(&data->reactive_state, false);
  558. if (enable) {
  559. mpu6500_i2c_read_reg(data->client, MPUREG_INT_STATUS, 1, &reg);
  560. printk(KERN_INFO "@@Initialize motion interrupt : INT_STATUS=%x\n", reg);
  561. reg = 0x01; // Make cycle and sleep bit 0
  562. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1, reg);
  563. msleep(50);
  564. reg = 0x0; // Clear gyro and accel config
  565. mpu6500_i2c_write_single_reg(data->client, MPUREG_CONFIG, reg);
  566. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG, reg);
  567. reg = 0x08; // Set accel fchoice 0 to use lp accel low power odr
  568. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG2, reg);
  569. reg = 0x05; // Set frequency of wake up (7.81Hz)
  570. mpu6500_i2c_write_single_reg(data->client, MPUREG_LP_ACCEL_ODR, reg);
  571. if (factory_test)
  572. reg = 0x41; // Enable motion interrupt and raw data ready
  573. else
  574. reg = 0x40; // Enable motion interrupt
  575. mpu6500_i2c_write_single_reg(data->client, MPUREG_INT_ENABLE, reg);
  576. reg = 0xC0; // Enable wake on motion detection logic
  577. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_INTEL_CTRL, reg);
  578. if (factory_test)
  579. reg = 0x00;
  580. else
  581. reg = 0x30; // Set Motion Threshold. (1LSB = 4mg)
  582. mpu6500_i2c_write_single_reg(data->client, MPUREG_WOM_THR, reg);
  583. if (!factory_test) {
  584. reg = 0x07; // Put gyro in standby and accel running
  585. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2, reg);
  586. reg = 0x1;
  587. reg |= 0x20; // Set the cycle bit to be 1 (LP Mode)
  588. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1, reg);
  589. }
  590. data->motion_recg_st_time = jiffies;
  591. } else {
  592. if (mot_data->is_set) {
  593. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1,
  594. mot_data->pwr_mnt[0]);
  595. msleep(50);
  596. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2,
  597. mot_data->pwr_mnt[1]);
  598. mpu6500_i2c_write_single_reg(data->client, MPUREG_CONFIG,
  599. mot_data->cfg);
  600. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG,
  601. mot_data->accel_cfg);
  602. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG2,
  603. mot_data->accel_cfg2);
  604. mpu6500_i2c_write_single_reg(data->client, MPUREG_GYRO_CONFIG,
  605. mot_data->gyro_cfg);
  606. mpu6500_i2c_write_single_reg(data->client, MPUREG_INT_ENABLE,
  607. mot_data->int_cfg);
  608. reg = 0xff; /* Motion Duration =1 ms */
  609. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_INTEL_ENABLE, reg);
  610. /* Motion Threshold =1mg, based on the data sheet. */
  611. reg = 0xff;
  612. mpu6500_i2c_write_single_reg(data->client, MPUREG_WOM_THR, reg);
  613. mpu6500_i2c_read_reg(data->client, MPUREG_INT_STATUS, 1, &reg);
  614. mpu6500_i2c_write_single_reg(data->client, MPUREG_SMPLRT_DIV, mot_data->smplrt_div);
  615. pr_info("%s: disable interrupt\n", __func__);
  616. }
  617. }
  618. mot_data->is_set = enable;
  619. return 0;
  620. }
  621. #else
  622. static int mpu6500_input_set_motion_interrupt(struct mpu6500_input_data *data,
  623. int enable, bool factory_test)
  624. {
  625. struct motion_int_data *mot_data = &data->mot_data;
  626. unsigned char reg;
  627. atomic_set(&data->reactive_state, false);
  628. if (enable) {
  629. /* 1) initialize */
  630. mpu6500_i2c_read_reg(data->client, MPUREG_INT_STATUS, 1, &reg);
  631. printk(KERN_INFO "@@Initialize motion interrupt : INT_STATUS=%x\n", reg);
  632. /* Power up the chip and clear the cycle bit. Full power */
  633. reg = 0x01;
  634. mpu6500_i2c_write_single_reg(data->client,
  635. MPUREG_PWR_MGMT_1, reg);
  636. mdelay(50);
  637. /* 2. mpu& accel config */
  638. if (factory_test)
  639. reg = 0x0; /*260Hz LPF */
  640. else
  641. reg = 0x1; /*44Hz LPF */
  642. mpu6500_i2c_write_single_reg(data->client, MPUREG_CONFIG, reg);
  643. reg = 0x0; /* Clear Accel Config. */
  644. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG, reg);
  645. reg = 0x08;
  646. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG2, reg);
  647. /* 3. set motion thr & dur */
  648. if (factory_test)
  649. reg = 0x41; /* Make the motion & drdy enable */
  650. else
  651. reg = 0x40; /* Make the motion interrupt enable */
  652. mpu6500_i2c_write_single_reg(data->client, MPUREG_INT_ENABLE, reg);
  653. reg = 4; // 3.91 Hz (low power accel odr)
  654. mpu6500_i2c_write_single_reg(data->client, MPUREG_LP_ACCEL_ODR, reg);
  655. reg = 0xC0; /* Motion Duration =1 ms */
  656. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_INTEL_CTRL, reg);
  657. /* Motion Threshold =1mg, based on the data sheet. */
  658. if (factory_test)
  659. reg = 0x00;
  660. else
  661. reg = 0x0C; // 0x4B;
  662. mpu6500_i2c_write_single_reg(data->client, MPUREG_WOM_THR, reg);
  663. if (!factory_test) {
  664. /* 5. */
  665. /* Steps to setup the lp mode for PWM-2 register */
  666. reg = mot_data->pwr_mnt[1];
  667. reg |= (BITS_LPA_WAKE_20HZ); /* the freq of wakeup */
  668. reg |= 0x07; /* put gyro in standby. */
  669. reg &= ~(BIT_STBY_XA | BIT_STBY_YA | BIT_STBY_ZA);
  670. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2, reg);
  671. reg = 0x1;
  672. reg |= 0x20; /* Set the cycle bit to be 1. LP MODE */
  673. reg &= ~0x08; /* Clear the temp disp bit. */
  674. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1, reg & ~BIT_SLEEP);
  675. }
  676. data->motion_recg_st_time = jiffies;
  677. } else {
  678. if (mot_data->is_set) {
  679. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_1,
  680. mot_data->pwr_mnt[0]);
  681. msleep(50);
  682. mpu6500_i2c_write_single_reg(data->client, MPUREG_PWR_MGMT_2,
  683. mot_data->pwr_mnt[1]);
  684. mpu6500_i2c_write_single_reg(data->client, MPUREG_CONFIG,
  685. mot_data->cfg);
  686. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG,
  687. mot_data->accel_cfg);
  688. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_CONFIG2,
  689. mot_data->accel_cfg2);
  690. mpu6500_i2c_write_single_reg(data->client, MPUREG_GYRO_CONFIG,
  691. mot_data->gyro_cfg);
  692. mpu6500_i2c_write_single_reg(data->client, MPUREG_INT_ENABLE,
  693. mot_data->int_cfg);
  694. reg = 0xff; /* Motion Duration =1 ms */
  695. mpu6500_i2c_write_single_reg(data->client, MPUREG_ACCEL_INTEL_ENABLE, reg);
  696. /* Motion Threshold =1mg, based on the data sheet. */
  697. reg = 0xff;
  698. mpu6500_i2c_write_single_reg(data->client, MPUREG_WOM_THR, reg);
  699. mpu6500_i2c_read_reg(data->client, MPUREG_INT_STATUS, 1, &reg);
  700. mpu6500_i2c_write_single_reg(data->client, MPUREG_SMPLRT_DIV, mot_data->smplrt_div);
  701. pr_info("%s: disable interrupt\n", __func__);
  702. }
  703. }
  704. mot_data->is_set = enable;
  705. return 0;
  706. }
  707. #endif
  708. static int mpu6500_input_set_irq(struct mpu6500_input_data *data, unsigned char irq)
  709. {
  710. int result;
  711. CHECK_RESULT(mpu6500_i2c_write_single_reg
  712. (data->client, MPUREG_INT_ENABLE, irq));
  713. return result;
  714. }
  715. static int mpu6500_input_suspend_accel(struct mpu6500_input_data *data)
  716. {
  717. unsigned char reg;
  718. int result;
  719. CHECK_RESULT(mpu6500_i2c_read_reg
  720. (data->client, MPUREG_PWR_MGMT_2, 1, &reg));
  721. reg |= (BIT_STBY_XA | BIT_STBY_YA | BIT_STBY_ZA);
  722. CHECK_RESULT(mpu6500_i2c_write_single_reg
  723. (data->client, MPUREG_PWR_MGMT_2, reg));
  724. return result;
  725. }
  726. static int mpu6500_input_resume_accel(struct mpu6500_input_data *data)
  727. {
  728. int result = 0;
  729. unsigned char reg;
  730. CHECK_RESULT(mpu6500_i2c_read_reg
  731. (data->client, MPUREG_PWR_MGMT_1, 1, &reg));
  732. if (reg & BIT_SLEEP) {
  733. CHECK_RESULT(mpu6500_i2c_write_single_reg(data->client,
  734. MPUREG_PWR_MGMT_1,
  735. reg & ~BIT_SLEEP));
  736. }
  737. msleep(2);
  738. CHECK_RESULT(mpu6500_i2c_read_reg
  739. (data->client, MPUREG_PWR_MGMT_2, 1, &reg));
  740. reg &= ~(BIT_STBY_XA | BIT_STBY_YA | BIT_STBY_ZA);
  741. CHECK_RESULT(mpu6500_i2c_write_single_reg
  742. (data->client, MPUREG_PWR_MGMT_2, reg));
  743. /* settings */
  744. /*----- LPF configuration : 41hz ---->*/
  745. reg = MPU_FILTER_41HZ;
  746. CHECK_RESULT(mpu6500_i2c_write_single_reg
  747. (data->client, MPUREG_ACCEL_CONFIG2, reg));
  748. /*<----- LPF configuration : 44hz ---- */
  749. CHECK_RESULT(mpu6500_i2c_read_reg
  750. (data->client, MPUREG_ACCEL_CONFIG, 1, &reg));
  751. CHECK_RESULT(mpu6500_i2c_write_single_reg
  752. (data->client, MPUREG_ACCEL_CONFIG, reg | 0x0));
  753. CHECK_RESULT(mpu6500_input_set_fsr(data, 2000));
  754. return result;
  755. }
  756. static int mpu6500_input_activate_accel(struct mpu6500_input_data *data,
  757. bool enable)
  758. {
  759. int result;
  760. if (enable) {
  761. result = mpu6500_input_resume_accel(data);
  762. if (result) {
  763. LOG_RESULT_LOCATION(result);
  764. return result;
  765. } else {
  766. data->enabled_sensors |= MPU6500_SENSOR_ACCEL;
  767. }
  768. } else {
  769. result = mpu6500_input_suspend_accel(data);
  770. if (result == 0) {
  771. data->enabled_sensors &= ~MPU6500_SENSOR_ACCEL;
  772. }
  773. }
  774. return result;
  775. }
  776. static int mpu6500_input_suspend_gyro(struct mpu6500_input_data *data)
  777. {
  778. int result = 0;
  779. CHECK_RESULT(mpu6500_i2c_read_reg
  780. (data->client, MPUREG_PWR_MGMT_1, 2, data->gyro_pwr_mgnt));
  781. data->gyro_pwr_mgnt[1] |= (BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
  782. CHECK_RESULT(mpu6500_i2c_write_single_reg
  783. (data->client, MPUREG_PWR_MGMT_2, data->gyro_pwr_mgnt[1]));
  784. return result;
  785. }
  786. static int mpu6500_input_resume_gyro(struct mpu6500_input_data *data)
  787. {
  788. int result = 0;
  789. unsigned regs[2] = { 0, };
  790. CHECK_RESULT(mpu6500_i2c_read_reg
  791. (data->client, MPUREG_PWR_MGMT_1, 2, data->gyro_pwr_mgnt));
  792. CHECK_RESULT(mpu6500_i2c_write_single_reg
  793. (data->client, MPUREG_PWR_MGMT_1,
  794. data->gyro_pwr_mgnt[0] & ~BIT_SLEEP));
  795. data->gyro_pwr_mgnt[1] &= ~(BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
  796. CHECK_RESULT(mpu6500_i2c_write_single_reg
  797. (data->client, MPUREG_PWR_MGMT_2, data->gyro_pwr_mgnt[1]));
  798. regs[0] = MPU_FS_500DPS << 3;
  799. CHECK_RESULT(mpu6500_i2c_write_single_reg
  800. (data->client, MPUREG_GYRO_CONFIG, regs[0]));
  801. regs[0] = MPU_FILTER_41HZ | 0x18;
  802. CHECK_RESULT(mpu6500_i2c_write_single_reg
  803. (data->client, MPUREG_CONFIG, regs[0]));
  804. return result;
  805. }
  806. static int mpu6500_input_activate_gyro(struct mpu6500_input_data *data,
  807. bool enable)
  808. {
  809. int result;
  810. if (enable) {
  811. result = mpu6500_input_resume_gyro(data);
  812. if (result) {
  813. LOG_RESULT_LOCATION(result);
  814. return result;
  815. } else {
  816. data->enabled_sensors |= MPU6500_SENSOR_GYRO;
  817. }
  818. } else {
  819. result = mpu6500_input_suspend_gyro(data);
  820. if (result == 0) {
  821. data->enabled_sensors &= ~MPU6500_SENSOR_GYRO;
  822. }
  823. }
  824. return result;
  825. }
  826. static int mpu6500_set_delay(struct mpu6500_input_data *data)
  827. {
  828. int result = 0;
  829. int delay = 200;
  830. if (data->enabled_sensors & MPU6500_SENSOR_ACCEL) {
  831. delay = MIN(delay, ktime_to_ms(data->accel_delay));
  832. }
  833. if (data->enabled_sensors & MPU6500_SENSOR_GYRO) {
  834. delay = MIN(delay, ktime_to_ms(data->gyro_delay));
  835. }
  836. data->current_delay = delay;
  837. if (data->enabled_sensors & MPU6500_SENSOR_ACCEL ||
  838. data->enabled_sensors & MPU6500_SENSOR_GYRO) {
  839. CHECK_RESULT(mpu6500_input_set_odr(data,
  840. data->current_delay));
  841. #ifndef CONFIG_INPUT_MPU6500_POLLING
  842. if (!atomic_read(&data->reactive_enable))
  843. CHECK_RESULT(mpu6500_input_set_irq(data,
  844. BIT_RAW_RDY_EN));
  845. #endif
  846. }
  847. return result;
  848. }
  849. static int mpu6500_input_activate_devices(struct mpu6500_input_data *data,
  850. int sensors, bool enable)
  851. {
  852. int result = 0;
  853. /* disable reactive alert when any sensors turn on */
  854. if (atomic_read(&data->motion_recg_enable)) {
  855. if (enable && (sensors != 0))
  856. mpu6500_input_set_motion_interrupt(data, false, false);
  857. }
  858. if (sensors & MPU6500_SENSOR_ACCEL) {
  859. CHECK_RESULT(mpu6500_input_activate_accel(data, enable));
  860. }
  861. if (sensors & MPU6500_SENSOR_GYRO) {
  862. CHECK_RESULT(mpu6500_input_activate_gyro(data, enable));
  863. }
  864. if (data->enabled_sensors) {
  865. CHECK_RESULT(mpu6500_set_delay(data));
  866. } else {
  867. unsigned char reg;
  868. data->accel_delay = ns_to_ktime(MPU6500_DEFAULT_DELAY);
  869. data->gyro_delay = ns_to_ktime(MPU6500_DEFAULT_DELAY);
  870. /*enable reactive alert when all sensors go off*/
  871. if (atomic_read(&data->motion_recg_enable))
  872. mpu6500_input_set_motion_interrupt(data, true,
  873. data->factory_mode);
  874. else {
  875. CHECK_RESULT(mpu6500_input_set_irq(data, 0x0));
  876. CHECK_RESULT(mpu6500_i2c_read_reg(data->client,
  877. MPUREG_PWR_MGMT_1, 1, &reg));
  878. if (!(reg & BIT_SLEEP))
  879. CHECK_RESULT(mpu6500_i2c_write_single_reg(
  880. data->client, MPUREG_PWR_MGMT_1,
  881. reg | BIT_SLEEP));
  882. }
  883. }
  884. return result;
  885. }
  886. static void mpu6500_set_65XX_gyro_config(struct i2c_client *i2c_client)
  887. {
  888. unsigned char d, cfg;
  889. mpu6500_i2c_read_reg(i2c_client, MPU6500_REG_BANK_SEL, 1, &cfg);
  890. mpu6500_i2c_write_single_reg(i2c_client, MPU6500_REG_BANK_SEL,
  891. cfg | MPU6500_CFG_SET_BIT);
  892. mpu6500_i2c_read_reg(i2c_client, MPU6500_GYRO_SPC_CFG, 1, &d);
  893. d |= 1;
  894. mpu6500_i2c_write_single_reg(i2c_client, MPU6500_GYRO_SPC_CFG, d);
  895. mpu6500_i2c_write_single_reg(i2c_client, MPU6500_REG_BANK_SEL, cfg);
  896. }
  897. static int __devinit mpu6500_input_initialize(struct mpu6500_input_data *data, const struct mpu6500_input_cfg
  898. *cfg)
  899. {
  900. int result;
  901. data->int_pin_cfg = BIT_INT_ANYRD_2CLEAR;
  902. data->current_delay = -1;
  903. data->enabled_sensors = 0;
  904. CHECK_RESULT(mpu6500_i2c_write_single_reg
  905. (data->client, MPUREG_PWR_MGMT_1, BIT_H_RESET));
  906. msleep(100);
  907. CHECK_RESULT(mpu6500_i2c_write_single_reg
  908. (data->client, MPUREG_INT_PIN_CFG,
  909. data->int_pin_cfg | BIT_BYPASS_EN));
  910. mpu6500_set_65XX_gyro_config(data->client);
  911. return mpu6500_input_set_mode(data, MPU6500_MODE_SLEEP);
  912. }
  913. static int accel_open_calibration(struct mpu6500_input_data *data)
  914. {
  915. struct file *cal_filp = NULL;
  916. int err = 0;
  917. mm_segment_t old_fs;
  918. old_fs = get_fs();
  919. set_fs(KERNEL_DS);
  920. cal_filp = filp_open(MPU6500_ACCEL_CAL_PATH,
  921. O_RDONLY, S_IRUGO | S_IWUSR | S_IWGRP);
  922. if (IS_ERR(cal_filp)) {
  923. pr_err("[SENSOR] %s: - Can't open calibration file\n", __func__);
  924. set_fs(old_fs);
  925. err = PTR_ERR(cal_filp);
  926. goto done;
  927. }
  928. err = cal_filp->f_op->read(cal_filp,
  929. (char *)&data->acc_cal,
  930. 3 * sizeof(s16), &cal_filp->f_pos);
  931. if (err != 3 * sizeof(s16)) {
  932. pr_err("[SENSOR] %s: - Can't read the cal data from file\n", __func__);
  933. err = -EIO;
  934. }
  935. pr_info("[SENSOR] %s: - (%d,%d,%d)\n", __func__,
  936. data->acc_cal[0], data->acc_cal[1], data->acc_cal[2]);
  937. filp_close(cal_filp, current->files);
  938. done:
  939. set_fs(old_fs);
  940. return err;
  941. }
  942. static ssize_t mpu6500_input_accel_enable_show(struct device *dev,
  943. struct device_attribute *attr,
  944. char *buf)
  945. {
  946. struct input_dev *input_data = to_input_dev(dev);
  947. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  948. return sprintf(buf, "%d\n", atomic_read(&data->accel_enable));
  949. }
  950. static ssize_t mpu6500_input_accel_enable_store(struct device *dev,
  951. struct device_attribute *attr,
  952. const char *buf, size_t count)
  953. {
  954. struct input_dev *input_data = to_input_dev(dev);
  955. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  956. #ifdef CONFIG_INPUT_MPU6500_POLLING
  957. struct motion_int_data *mot_data = &data->mot_data;
  958. #endif
  959. int value = simple_strtoul(buf, NULL, 10);
  960. if (value == 1)
  961. accel_open_calibration(data);
  962. pr_info("[SENSOR] %s : enable = %d\n", __func__, value);
  963. mutex_lock(&data->mutex);
  964. #ifdef CONFIG_INPUT_MPU6500_POLLING
  965. if (value && !atomic_read(&data->accel_enable)) {
  966. if (mot_data->is_set)
  967. mpu6500_input_set_motion_interrupt(
  968. data, false, data->factory_mode);
  969. mpu6500_input_activate_devices(data,
  970. MPU6500_SENSOR_ACCEL, true);
  971. schedule_delayed_work(&data->accel_work, msecs_to_jiffies(ktime_to_ms(data->accel_delay)));
  972. }
  973. if (!value && atomic_read(&data->accel_enable)) {
  974. cancel_delayed_work_sync(&data->accel_work);
  975. mpu6500_input_activate_devices(data,
  976. MPU6500_SENSOR_ACCEL, false);
  977. if (atomic_read(&data->reactive_enable))
  978. mpu6500_input_set_motion_interrupt(
  979. data, true, data->factory_mode);
  980. }
  981. #else
  982. if (value && !atomic_read(&data->accel_enable)) {
  983. mpu6500_input_activate_devices(data, MPU6500_SENSOR_ACCEL, true);
  984. }
  985. if (!value && atomic_read(&data->accel_enable)) {
  986. mpu6500_input_activate_devices(data, MPU6500_SENSOR_ACCEL, false);
  987. }
  988. #endif
  989. atomic_set(&data->accel_enable, value);
  990. mutex_unlock(&data->mutex);
  991. return count;
  992. }
  993. static ssize_t mpu6500_input_accel_delay_show(struct device *dev,
  994. struct device_attribute *attr,
  995. char *buf)
  996. {
  997. struct input_dev *input_data = to_input_dev(dev);
  998. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  999. return sprintf(buf, "%lld\n", ktime_to_ns(data->accel_delay));
  1000. }
  1001. static ssize_t mpu6500_input_accel_delay_store(struct device *dev,
  1002. struct device_attribute *attr,
  1003. const char *buf, size_t count)
  1004. {
  1005. struct input_dev *input_data = to_input_dev(dev);
  1006. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1007. int64_t delay;
  1008. int ret = 0;
  1009. ret = kstrtoll(buf, 10, &delay);
  1010. if (ret) {
  1011. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  1012. return ret;
  1013. }
  1014. pr_info("[SENSOR] %s : delay = %lld\n", __func__, delay);
  1015. mutex_lock(&data->mutex);
  1016. data->accel_delay = ns_to_ktime(delay);
  1017. mpu6500_set_delay(data);
  1018. mutex_unlock(&data->mutex);
  1019. return count;
  1020. }
  1021. static int gyro_open_calibration(struct mpu6500_input_data *data)
  1022. {
  1023. struct file *cal_filp = NULL;
  1024. int err = 0;
  1025. mm_segment_t old_fs;
  1026. old_fs = get_fs();
  1027. set_fs(KERNEL_DS);
  1028. cal_filp = filp_open(MPU6500_GYRO_CAL_PATH,
  1029. O_RDONLY, S_IRUGO | S_IWUSR | S_IWGRP);
  1030. if (IS_ERR(cal_filp)) {
  1031. pr_err("[SENSOR] %s: - Can't open calibration file\n", __func__);
  1032. set_fs(old_fs);
  1033. err = PTR_ERR(cal_filp);
  1034. goto done;
  1035. }
  1036. err = cal_filp->f_op->read(cal_filp,
  1037. (char *)&data->gyro_bias, 3 * sizeof(int),
  1038. &cal_filp->f_pos);
  1039. if (err != 3 * sizeof(int)) {
  1040. pr_err("[SENSOR] %s: - Can't read the cal data from file\n", __func__);
  1041. err = -EIO;
  1042. }
  1043. pr_info("[SENSOR] %s: - (%d,%d,%d)\n", __func__,
  1044. data->gyro_bias[0], data->gyro_bias[1], data->gyro_bias[2]);
  1045. filp_close(cal_filp, current->files);
  1046. done:
  1047. set_fs(old_fs);
  1048. return err;
  1049. }
  1050. static int gyro_do_calibrate(struct mpu6500_input_data *data)
  1051. {
  1052. struct file *cal_filp;
  1053. int err;
  1054. mm_segment_t old_fs;
  1055. /* selftest was doing 2000dps condition, change to 500dps */
  1056. data->gyro_bias[0] = data->gyro_bias[0] << 2;
  1057. data->gyro_bias[1] = data->gyro_bias[1] << 2;
  1058. data->gyro_bias[2] = data->gyro_bias[2] << 2;
  1059. pr_info("[SENSOR] %s: - cal data (%d,%d,%d)\n", __func__,
  1060. data->gyro_bias[0], data->gyro_bias[1], data->gyro_bias[2]);
  1061. old_fs = get_fs();
  1062. set_fs(KERNEL_DS);
  1063. cal_filp = filp_open(MPU6500_GYRO_CAL_PATH,
  1064. O_CREAT | O_TRUNC | O_WRONLY,
  1065. S_IRUGO | S_IWUSR | S_IWGRP);
  1066. if (IS_ERR(cal_filp)) {
  1067. pr_err("[SENSOR] %s: - Can't open calibration file\n", __func__);
  1068. set_fs(old_fs);
  1069. err = PTR_ERR(cal_filp);
  1070. goto done;
  1071. }
  1072. err = cal_filp->f_op->write(cal_filp,
  1073. (char *)&data->gyro_bias, 3 * sizeof(int),
  1074. &cal_filp->f_pos);
  1075. if (err != 3 * sizeof(int)) {
  1076. pr_err("[SENSOR] %s: - Can't write the cal data to file\n", __func__);
  1077. err = -EIO;
  1078. }
  1079. filp_close(cal_filp, current->files);
  1080. done:
  1081. set_fs(old_fs);
  1082. return err;
  1083. }
  1084. static ssize_t mpu6500_input_gyro_enable_show(struct device *dev,
  1085. struct device_attribute *attr,
  1086. char *buf)
  1087. {
  1088. struct input_dev *input_data = to_input_dev(dev);
  1089. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1090. return sprintf(buf, "%d\n", atomic_read(&data->gyro_enable));
  1091. }
  1092. static ssize_t mpu6500_input_gyro_enable_store(struct device *dev,
  1093. struct device_attribute *attr,
  1094. const char *buf, size_t count)
  1095. {
  1096. struct input_dev *input_data = to_input_dev(dev);
  1097. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1098. #ifdef CONFIG_INPUT_MPU6500_POLLING
  1099. struct motion_int_data *mot_data = &data->mot_data;
  1100. #endif
  1101. int value = simple_strtoul(buf, NULL, 10);
  1102. if (value == 1)
  1103. gyro_open_calibration(data);
  1104. pr_info("[SENSOR] %s : enable = %d\n", __func__, value);
  1105. mutex_lock(&data->mutex);
  1106. #ifdef CONFIG_INPUT_MPU6500_POLLING
  1107. if (value && !atomic_read(&data->gyro_enable)) {
  1108. if (mot_data->is_set)
  1109. mpu6500_input_set_motion_interrupt(
  1110. data, false, data->factory_mode);
  1111. mpu6500_input_activate_devices(data,
  1112. MPU6500_SENSOR_GYRO, true);
  1113. schedule_delayed_work(&data->gyro_work,
  1114. msecs_to_jiffies(ktime_to_ms(data->gyro_delay)));
  1115. }
  1116. if (!value && atomic_read(&data->gyro_enable)) {
  1117. cancel_delayed_work_sync(&data->gyro_work);
  1118. mpu6500_input_activate_devices(data,
  1119. MPU6500_SENSOR_GYRO, false);
  1120. if (atomic_read(&data->reactive_enable))
  1121. mpu6500_input_set_motion_interrupt(
  1122. data, true, data->factory_mode);
  1123. }
  1124. #endif
  1125. atomic_set(&data->gyro_enable, value);
  1126. mutex_unlock(&data->mutex);
  1127. return count;
  1128. }
  1129. static ssize_t mpu6500_input_gyro_delay_show(struct device *dev,
  1130. struct device_attribute *attr,
  1131. char *buf)
  1132. {
  1133. struct input_dev *input_data = to_input_dev(dev);
  1134. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1135. return sprintf(buf, "%lld\n", ktime_to_ns(data->gyro_delay));
  1136. }
  1137. static ssize_t mpu6500_input_gyro_delay_store(struct device *dev,
  1138. struct device_attribute *attr,
  1139. const char *buf, size_t count)
  1140. {
  1141. struct input_dev *input_data = to_input_dev(dev);
  1142. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1143. int64_t delay;
  1144. int ret = 0;
  1145. ret = kstrtoll(buf, 10, &delay);
  1146. if (ret) {
  1147. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  1148. return ret;
  1149. }
  1150. pr_info("[SENSOR] %s : delay = %lld\n", __func__, delay);
  1151. mutex_lock(&data->mutex);
  1152. data->gyro_delay = ns_to_ktime(delay);
  1153. mpu6500_set_delay(data);
  1154. mutex_unlock(&data->mutex);
  1155. return count;
  1156. }
  1157. static ssize_t mpu6500_input_motion_recg_enable_show(struct device *dev,
  1158. struct device_attribute
  1159. *attr, char *buf)
  1160. {
  1161. struct input_dev *input_data = to_input_dev(dev);
  1162. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1163. return sprintf(buf, "%d\n", atomic_read(&data->motion_recg_enable));
  1164. }
  1165. static ssize_t mpu6500_input_motion_recg_enable_store(struct device *dev,
  1166. struct device_attribute
  1167. *attr, const char *buf,
  1168. size_t count)
  1169. {
  1170. struct input_dev *input_data = to_input_dev(dev);
  1171. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1172. int value = simple_strtoul(buf, NULL, 10);
  1173. mutex_lock(&data->mutex);
  1174. atomic_set(&data->motion_recg_enable, value);
  1175. mpu6500_input_set_motion_interrupt(data, value, false);
  1176. mutex_unlock(&data->mutex);
  1177. return count;
  1178. }
  1179. static ssize_t mpu6500_input_gyro_self_test_show(struct device *dev,
  1180. struct device_attribute *attr,
  1181. char *buf)
  1182. {
  1183. struct input_dev *input_data = to_input_dev(dev);
  1184. struct mpu6500_input_data *data = input_get_drvdata(input_data);
  1185. int scaled_gyro_bias[3] = { 0 }; //absolute gyro bias scaled by 1000 times. (gyro_bias x 1000)
  1186. int scaled_gyro_rms[3] = { 0 }; //absolute gyro rms scaled by 1000 times. (gyro_bias x 1000)
  1187. int packet_count[3] = { 0 };
  1188. int result;
  1189. mutex_lock(&data->mutex);
  1190. result = mpu6500_selftest_run(data->client,
  1191. packet_count,
  1192. scaled_gyro_bias,
  1193. scaled_gyro_rms, data->gyro_bias);
  1194. if (!result) {
  1195. //store calibration to file
  1196. gyro_do_calibrate(data);
  1197. } else {
  1198. data->gyro_bias[0] = 0;
  1199. data->gyro_bias[1] = 0;
  1200. data->gyro_bias[2] = 0;
  1201. }
  1202. mutex_unlock(&data->mutex);
  1203. return sprintf(buf, "%d "
  1204. "%d %d %d "
  1205. "%d.%03d %d.%03d %d.%03d "
  1206. "%d.%03d %d.%03d %d.%03d ",
  1207. result,
  1208. packet_count[0], packet_count[1], packet_count[2],
  1209. (int)abs(scaled_gyro_bias[0] / 1000),
  1210. (int)abs(scaled_gyro_bias[0]) % 1000,
  1211. (int)abs(scaled_gyro_bias[1] / 1000),
  1212. (int)abs(scaled_gyro_bias[1]) % 1000,
  1213. (int)abs(scaled_gyro_bias[2] / 1000),
  1214. (int)abs(scaled_gyro_bias[2]) % 1000,
  1215. scaled_gyro_rms[0] / 1000,
  1216. (int)abs(scaled_gyro_rms[0]) % 1000,
  1217. scaled_gyro_rms[1] / 1000,
  1218. (int)abs(scaled_gyro_rms[1]) % 1000,
  1219. scaled_gyro_rms[2] / 1000,
  1220. (int)abs(scaled_gyro_rms[2]) % 1000);
  1221. }
  1222. static struct device_attribute dev_attr_acc_enable =
  1223. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  1224. mpu6500_input_accel_enable_show, mpu6500_input_accel_enable_store);
  1225. static struct device_attribute dev_attr_gyro_enable =
  1226. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  1227. mpu6500_input_gyro_enable_show, mpu6500_input_gyro_enable_store);
  1228. static struct device_attribute dev_attr_acc_delay =
  1229. __ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  1230. mpu6500_input_accel_delay_show, mpu6500_input_accel_delay_store);
  1231. static struct device_attribute dev_attr_gyro_delay =
  1232. __ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  1233. mpu6500_input_gyro_delay_show, mpu6500_input_gyro_delay_store);
  1234. static DEVICE_ATTR(self_test, S_IRUGO, mpu6500_input_gyro_self_test_show, NULL);
  1235. static DEVICE_ATTR(mot_recg_enable, S_IRUGO | S_IWUSR | S_IWGRP,
  1236. mpu6500_input_motion_recg_enable_show,
  1237. mpu6500_input_motion_recg_enable_store);
  1238. static struct attribute *accel_attributes[] = {
  1239. &dev_attr_acc_enable.attr,
  1240. &dev_attr_acc_delay.attr,
  1241. &dev_attr_mot_recg_enable.attr,
  1242. NULL
  1243. };
  1244. static struct attribute *gyro_attributes[] = {
  1245. &dev_attr_gyro_enable.attr,
  1246. &dev_attr_gyro_delay.attr,
  1247. &dev_attr_self_test.attr,
  1248. NULL
  1249. };
  1250. static struct attribute_group accel_attribute_group = {
  1251. .attrs = accel_attributes
  1252. };
  1253. static struct attribute_group gyro_attribute_group = {
  1254. .attrs = gyro_attributes
  1255. };
  1256. void mpu6500_vdd_on(struct mpu6500_input_data *data, bool onoff)
  1257. {
  1258. int ret;
  1259. if (data->str_l19 == NULL) {
  1260. data->str_l19 =regulator_get(&data->client->dev, "8226_l19");
  1261. if (IS_ERR(data->str_l19)){
  1262. pr_err("%s: regulator_get failed for 8226_l19\n", __func__);
  1263. return ;
  1264. }
  1265. ret = regulator_set_voltage(data->str_l19, 2850000, 2850000);
  1266. if (ret)
  1267. pr_err("%s: error vsensor_2p85 setting voltage ret=%d\n",__func__, ret);
  1268. }
  1269. if (data->lvs1_1p8 == NULL) {
  1270. data->lvs1_1p8 = regulator_get(&data->client->dev, "8226_lvs1");
  1271. if (IS_ERR(data->lvs1_1p8)){
  1272. pr_err("%s: regulator_get failed for 8226_l6\n", __func__);
  1273. return ;
  1274. }
  1275. ret = regulator_set_voltage(data->lvs1_1p8, 1800000, 1800000);
  1276. if (ret)
  1277. pr_err("%s: error vreg_2p8 setting voltage ret=%d\n",__func__, ret);
  1278. }
  1279. if (onoff == 1) {
  1280. ret = regulator_enable(data->str_l19);
  1281. if (ret)
  1282. pr_err("%s: error enablinig regulator info->vdd_2p85\n", __func__);
  1283. ret = regulator_enable(data->lvs1_1p8);
  1284. if (ret)
  1285. pr_err("%s: error enablinig regulator info->vreg_1p8\n", __func__);
  1286. }
  1287. else if (onoff == 0) {
  1288. if (regulator_is_enabled(data->str_l19)) {
  1289. ret = regulator_disable(data->str_l19);
  1290. if (ret)
  1291. pr_err("%s: error vdd_2p85 disabling regulator\n",__func__);
  1292. }
  1293. if (regulator_is_enabled(data->lvs1_1p8)) {
  1294. ret = regulator_disable(data->lvs1_1p8);
  1295. if (ret)
  1296. pr_err("%s: error vreg_1p8 disabling regulator\n",__func__);
  1297. }
  1298. }
  1299. msleep(30);
  1300. return;
  1301. }
  1302. static void mpu6500_request_gpio(struct mpu6500_platform_data *pdata)
  1303. {
  1304. int ret;
  1305. pr_info("[MPU6500] request gpio\n");
  1306. ret = gpio_request(pdata->gpio_scl, "mpu6500_scl");
  1307. if (ret) {
  1308. pr_err("[MPU6500]%s: unable to request mpu6500_scl [%d]\n",
  1309. __func__, pdata->gpio_scl);
  1310. return;
  1311. }
  1312. ret = gpio_request(pdata->gpio_sda, "mpu6500_sda");
  1313. if (ret) {
  1314. pr_err("[MPU6500]%s: unable to request mpu6500_sda [%d]\n",
  1315. __func__, pdata->gpio_sda);
  1316. return;
  1317. }
  1318. ret = gpio_request(pdata->gpio_int, "mpu6500_irq");
  1319. if (ret) {
  1320. pr_err("[MPU6500]%s: unable to request mpu6500_irq [%d]\n",
  1321. __func__, pdata->gpio_int);
  1322. return;
  1323. }
  1324. }
  1325. /* samsung factory test */
  1326. static int read_accel_raw_xyz(struct mpu6500_input_data *data)
  1327. {
  1328. u8 regs[6];
  1329. int result;
  1330. result = mpu6500_i2c_read_reg(data->client, MPUREG_ACCEL_XOUT_H, 6, regs);
  1331. if (result) {
  1332. pr_err("[SENSOR] %s: i2c_read err= %d\n", __func__, result);
  1333. return result;
  1334. }
  1335. data->acc_data.x = ((s16) ((s16) regs[0] << 8)) | regs[1];
  1336. data->acc_data.y = ((s16) ((s16) regs[2] << 8)) | regs[3];
  1337. data->acc_data.z = ((s16) ((s16) regs[4] << 8)) | regs[5];
  1338. remap_sensor_data(data->acc_data.v, data->chip_pos);
  1339. return 0;
  1340. }
  1341. static int accel_do_calibrate(struct mpu6500_input_data *data, int enable)
  1342. {
  1343. struct file *cal_filp;
  1344. int sum[3] = { 0, };
  1345. int err;
  1346. int i;
  1347. mm_segment_t old_fs = {0};
  1348. if (!(data->enabled_sensors & MPU6500_SENSOR_ACCEL)) {
  1349. mpu6500_input_resume_accel(data);
  1350. usleep_range(10000, 11000);
  1351. }
  1352. for (i = 0; i < ACC_CAL_TIME; i++) {
  1353. err = read_accel_raw_xyz(data);
  1354. if (err < 0) {
  1355. pr_err("[SENSOR] %s: accel_read_accel_raw_xyz() "
  1356. "failed in the %dth loop\n", __func__, i);
  1357. goto done;
  1358. }
  1359. usleep_range(10000, 11000);
  1360. sum[0] += data->acc_data.x/ACC_CAL_DIV;
  1361. sum[1] += data->acc_data.y/ACC_CAL_DIV;
  1362. sum[2] += data->acc_data.z/ACC_CAL_DIV;
  1363. }
  1364. if (!(data->enabled_sensors & MPU6500_SENSOR_ACCEL))
  1365. mpu6500_input_suspend_accel(data);
  1366. if (enable) {
  1367. data->acc_cal[0] =
  1368. (sum[0] / ACC_CAL_TIME) * ACC_CAL_DIV;
  1369. data->acc_cal[1] =
  1370. (sum[1] / ACC_CAL_TIME) * ACC_CAL_DIV;
  1371. if(sum[2] > 0)
  1372. data->acc_cal[2] =
  1373. ((sum[2] / ACC_CAL_TIME) - ACC_IDEAL) * ACC_CAL_DIV;
  1374. else if(sum[2] < 0)
  1375. data->acc_cal[2] =
  1376. ((sum[2] / ACC_CAL_TIME) + ACC_IDEAL) * ACC_CAL_DIV;
  1377. } else {
  1378. data->acc_cal[0] = 0;
  1379. data->acc_cal[1] = 0;
  1380. data->acc_cal[2] = 0;
  1381. }
  1382. pr_info("[SENSOR] %s: cal data (%d,%d,%d)\n", __func__,
  1383. data->acc_cal[0], data->acc_cal[1], data->acc_cal[2]);
  1384. old_fs = get_fs();
  1385. set_fs(KERNEL_DS);
  1386. cal_filp = filp_open(MPU6500_ACCEL_CAL_PATH,
  1387. O_CREAT | O_TRUNC | O_WRONLY,
  1388. S_IRUGO | S_IWUSR | S_IWGRP);
  1389. if (IS_ERR(cal_filp)) {
  1390. pr_err("[SENSOR] %s: Can't open calibration file\n", __func__);
  1391. set_fs(old_fs);
  1392. err = PTR_ERR(cal_filp);
  1393. goto done;
  1394. }
  1395. err = cal_filp->f_op->write(cal_filp,
  1396. (char *)&data->acc_cal, 3 * sizeof(s16),
  1397. &cal_filp->f_pos);
  1398. if (err != 3 * sizeof(s16)) {
  1399. pr_err("[SENSOR] %s: Can't write the cal data to file\n", __func__);
  1400. err = -EIO;
  1401. }
  1402. filp_close(cal_filp, current->files);
  1403. done:
  1404. set_fs(old_fs);
  1405. return err;
  1406. }
  1407. static ssize_t mpu6500_input_reactive_enable_show(struct device *dev,
  1408. struct device_attribute
  1409. *attr, char *buf)
  1410. {
  1411. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1412. return sprintf(buf, "%d\n",
  1413. atomic_read(&data->reactive_state));
  1414. }
  1415. static ssize_t mpu6500_input_reactive_enable_store(struct device *dev,
  1416. struct device_attribute
  1417. *attr, const char *buf,
  1418. size_t count)
  1419. {
  1420. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1421. bool onoff = false;
  1422. unsigned long value = 0;
  1423. struct motion_int_data *mot_data = &data->mot_data;
  1424. if (strict_strtoul(buf, 10, &value))
  1425. return -EINVAL;
  1426. if (value == 1) {
  1427. onoff = true;
  1428. } else if (value == 0) {
  1429. onoff = false;
  1430. } else if (value == 2) {
  1431. onoff = true;
  1432. data->factory_mode = true;
  1433. } else {
  1434. pr_err("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  1435. return -EINVAL;
  1436. }
  1437. #ifdef CONFIG_INPUT_MPU6500_POLLING
  1438. if (!value) {
  1439. disable_irq_wake(data->client->irq);
  1440. disable_irq(data->client->irq);
  1441. } else {
  1442. enable_irq(data->client->irq);
  1443. enable_irq_wake(data->client->irq);
  1444. }
  1445. #endif
  1446. mutex_lock(&data->mutex);
  1447. if (onoff) {
  1448. pr_info("[SENSOR] %s: enable\n", __func__);
  1449. atomic_set(&data->reactive_enable, true);
  1450. if (!mot_data->is_set) {
  1451. mpu6500_i2c_read_reg(data->client,
  1452. MPUREG_PWR_MGMT_1, 2,
  1453. mot_data->pwr_mnt);
  1454. mpu6500_i2c_read_reg(data->client,
  1455. MPUREG_CONFIG, 1, &mot_data->cfg);
  1456. mpu6500_i2c_read_reg(data->client,
  1457. MPUREG_ACCEL_CONFIG, 1,
  1458. &mot_data->accel_cfg);
  1459. mpu6500_i2c_read_reg(data->client,
  1460. MPUREG_GYRO_CONFIG, 1,
  1461. &mot_data->gyro_cfg);
  1462. mpu6500_i2c_read_reg(data->client,
  1463. MPUREG_INT_ENABLE, 1,
  1464. &mot_data->int_cfg);
  1465. mpu6500_i2c_read_reg(data->client,
  1466. MPUREG_SMPLRT_DIV, 1,
  1467. &mot_data->smplrt_div);
  1468. }
  1469. mpu6500_input_set_motion_interrupt(data,
  1470. true, data->factory_mode);
  1471. } else {
  1472. pr_info("[SENSOR] %s: disable\n", __func__);
  1473. mpu6500_input_set_motion_interrupt(data,
  1474. false, data->factory_mode);
  1475. atomic_set(&data->reactive_enable, false);
  1476. if (data->factory_mode)
  1477. data->factory_mode = false;
  1478. } mutex_unlock(&data->mutex);
  1479. pr_info("[SENSOR] %s: onoff = %d, state =%d OUT\n", __func__,
  1480. atomic_read(&data->motion_recg_enable),
  1481. atomic_read(&data->reactive_state));
  1482. return count;
  1483. }
  1484. static struct device_attribute dev_attr_reactive_alert =
  1485. __ATTR(reactive_alert, S_IRUGO | S_IWUSR | S_IWGRP,
  1486. mpu6500_input_reactive_enable_show,
  1487. mpu6500_input_reactive_enable_store);
  1488. static ssize_t mpu6500_power_on(struct device *dev,
  1489. struct device_attribute *attr, char *buf)
  1490. {
  1491. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1492. int count = 0;
  1493. dev_dbg(dev, "this_client = %d\n", (int)data->client);
  1494. count = sprintf(buf, "%d\n", (data->client != NULL ? 1 : 0));
  1495. return count;
  1496. }
  1497. static struct device_attribute dev_attr_power_on =
  1498. __ATTR(power_on, S_IRUSR | S_IRGRP, mpu6500_power_on,
  1499. NULL);
  1500. static ssize_t mpu6500_get_temp(struct device *dev,
  1501. struct device_attribute *attr, char *buf)
  1502. {
  1503. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1504. int count = 0;
  1505. short temperature = 0;
  1506. unsigned char reg[2];
  1507. int result;
  1508. CHECK_RESULT(mpu6500_i2c_read_reg
  1509. (data->client, MPUREG_TEMP_OUT_H, 2, reg));
  1510. temperature = (short) (((reg[0]) << 8) | reg[1]);
  1511. temperature = ((temperature / 334) + 21);
  1512. pr_info("[SENSOR] %s: read temperature = %d\n", __func__, temperature);
  1513. count = sprintf(buf, "%d\n", temperature);
  1514. return count;
  1515. }
  1516. static struct device_attribute dev_attr_temperature =
  1517. __ATTR(temperature, S_IRUSR | S_IRGRP, mpu6500_get_temp,
  1518. NULL);
  1519. static ssize_t mpu6500_input_gyro_selftest_show(struct device *dev,
  1520. struct device_attribute *attr,
  1521. char *buf)
  1522. {
  1523. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1524. /* absolute gyro bias scaled by 1000 times. (gyro_bias x 1000) */
  1525. int scaled_gyro_bias[3] = {0};
  1526. /* absolute gyro rms scaled by 1000 times. (gyro_bias x 1000) */
  1527. int scaled_gyro_rms[3] = {0};
  1528. int packet_count[3] = {0};
  1529. int ratio[3] = {0};
  1530. int hw_result;
  1531. int result;
  1532. mutex_lock(&data->mutex);
  1533. result = mpu6500_selftest_run(data->client,
  1534. packet_count,
  1535. scaled_gyro_bias,
  1536. scaled_gyro_rms, data->gyro_bias);
  1537. hw_result = mpu6500_gyro_hw_self_check(data->client, ratio);
  1538. pr_info("%s, result = %d, hw_result = %d\n", __func__,
  1539. result, hw_result);
  1540. if (!result) {
  1541. /* store calibration to file */
  1542. gyro_do_calibrate(data);
  1543. } else {
  1544. data->gyro_bias[0] = 0;
  1545. data->gyro_bias[1] = 0;
  1546. data->gyro_bias[2] = 0;
  1547. }
  1548. mutex_unlock(&data->mutex);
  1549. return sprintf(buf, "%d,"
  1550. "%d.%03d,%d.%03d,%d.%03d,"
  1551. "%d.%03d,%d.%03d,%d.%03d,"
  1552. "%d.%01d,%d.%01d,%d.%01d,"
  1553. "%d.%03d,%d.%03d,%d.%03d\n",
  1554. result | hw_result,
  1555. (int)abs(scaled_gyro_bias[0] / 1000),
  1556. (int)abs(scaled_gyro_bias[0]) % 1000,
  1557. (int)abs(scaled_gyro_bias[1] / 1000),
  1558. (int)abs(scaled_gyro_bias[1]) % 1000,
  1559. (int)abs(scaled_gyro_bias[2] / 1000),
  1560. (int)abs(scaled_gyro_bias[2]) % 1000,
  1561. scaled_gyro_rms[0] / 1000,
  1562. (int)abs(scaled_gyro_rms[0]) % 1000,
  1563. scaled_gyro_rms[1] / 1000,
  1564. (int)abs(scaled_gyro_rms[1]) % 1000,
  1565. scaled_gyro_rms[2] / 1000,
  1566. (int)abs(scaled_gyro_rms[2]) % 1000,
  1567. (int)abs(ratio[0]/10),
  1568. (int)abs(ratio[0])%10,
  1569. (int)abs(ratio[1]/10),
  1570. (int)abs(ratio[1])%10,
  1571. (int)abs(ratio[2]/10),
  1572. (int)abs(ratio[2])%10,
  1573. (int)abs(packet_count[0] / 100),
  1574. (int)abs(packet_count[0]) % 100,
  1575. (int)abs(packet_count[1] / 100),
  1576. (int)abs(packet_count[1]) % 100,
  1577. (int)abs(packet_count[2] / 100),
  1578. (int)abs(packet_count[2]) % 100);
  1579. }
  1580. static struct device_attribute dev_attr_selftest =
  1581. __ATTR(selftest, S_IRUSR | S_IRGRP,
  1582. mpu6500_input_gyro_selftest_show,
  1583. NULL);
  1584. static ssize_t acc_data_read(struct device *dev,
  1585. struct device_attribute *attr, char *buf)
  1586. {
  1587. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1588. if (!(data->enabled_sensors & MPU6500_SENSOR_ACCEL)) {
  1589. mpu6500_input_resume_accel(data);
  1590. usleep_range(10000, 11000);
  1591. read_accel_raw_xyz(data);
  1592. mpu6500_input_suspend_accel(data);
  1593. usleep_range(10000, 11000);
  1594. }
  1595. return sprintf(buf, "%d, %d, %d\n", data->acc_data.x - data->acc_cal[0],
  1596. data->acc_data.y - data->acc_cal[1], data->acc_data.z - data->acc_cal[2]);
  1597. }
  1598. static struct device_attribute dev_attr_raw_data =
  1599. __ATTR(raw_data, S_IRUSR | S_IRGRP, acc_data_read,
  1600. NULL);
  1601. static ssize_t accel_calibration_show(struct device *dev,
  1602. struct device_attribute *attr,
  1603. char *buf)
  1604. {
  1605. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1606. int count = 0;
  1607. s16 x, y, z;
  1608. int err;
  1609. x = data->acc_cal[0];
  1610. y = data->acc_cal[1];
  1611. z = data->acc_cal[2];
  1612. pr_info("[SENSOR]: accel_calibration_show %d %d %d\n",
  1613. x, y, z);
  1614. if (!x && !y && !z)
  1615. err = -1;
  1616. else
  1617. err = 1;
  1618. count = sprintf(buf, "%d %d %d %d\n", err, x, y, z);
  1619. return count;
  1620. }
  1621. static ssize_t accel_calibration_store(struct device *dev,
  1622. struct device_attribute *attr,
  1623. const char *buf, size_t size)
  1624. {
  1625. struct mpu6500_input_data *data = dev_get_drvdata(dev);
  1626. int err;
  1627. int count;
  1628. unsigned long enable;
  1629. s16 x;
  1630. s16 y;
  1631. s16 z;
  1632. char tmp[64];
  1633. if (strict_strtoul(buf, 10, &enable))
  1634. return -EINVAL;
  1635. err = accel_do_calibrate(data, enable);
  1636. if (err < 0)
  1637. pr_err("[SENSOR] %s: accel_do_calibrate() failed\n",
  1638. __func__);
  1639. x = data->acc_cal[0];
  1640. y = data->acc_cal[1];
  1641. z = data->acc_cal[2];
  1642. pr_info("[SENSOR]: accel_calibration_store %d %d %d\n",
  1643. x, y, z);
  1644. if (err > 0)
  1645. err = 0;
  1646. count = sprintf(tmp, "%d\n", err);
  1647. return count;
  1648. }
  1649. static struct device_attribute dev_attr_calibration =
  1650. __ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  1651. accel_calibration_show, accel_calibration_store);
  1652. static ssize_t accel_vendor_show(struct device *dev,
  1653. struct device_attribute *attr,
  1654. char *buf)
  1655. {
  1656. return sprintf(buf, "%s\n", "INVENSENSE");
  1657. }
  1658. static struct device_attribute dev_attr_accel_sensor_vendor =
  1659. __ATTR(vendor, S_IRUSR | S_IRGRP, accel_vendor_show, NULL);
  1660. static struct device_attribute dev_attr_gyro_sensor_vendor =
  1661. __ATTR(vendor, S_IRUSR | S_IRGRP, accel_vendor_show, NULL);
  1662. static ssize_t accel_name_show(struct device *dev,
  1663. struct device_attribute *attr,
  1664. char *buf)
  1665. {
  1666. return sprintf(buf, "%s\n", "MPU6500");
  1667. }
  1668. static struct device_attribute dev_attr_accel_sensor_name =
  1669. __ATTR(name, S_IRUSR | S_IRGRP, accel_name_show, NULL);
  1670. static struct device_attribute dev_attr_gyro_sensor_name =
  1671. __ATTR(name, S_IRUSR | S_IRGRP, accel_name_show, NULL);
  1672. static struct device_attribute *gyro_sensor_attrs[] = {
  1673. &dev_attr_power_on,
  1674. &dev_attr_temperature,
  1675. &dev_attr_gyro_sensor_vendor,
  1676. &dev_attr_gyro_sensor_name,
  1677. &dev_attr_selftest,
  1678. NULL,
  1679. };
  1680. static struct device_attribute *accel_sensor_attrs[] = {
  1681. &dev_attr_raw_data,
  1682. &dev_attr_calibration,
  1683. &dev_attr_reactive_alert,
  1684. &dev_attr_accel_sensor_vendor,
  1685. &dev_attr_accel_sensor_name,
  1686. NULL,
  1687. };
  1688. static int mpu6500_misc_open(struct inode *inode, struct file *file)
  1689. {
  1690. int ret = 0;
  1691. #if DEBUG
  1692. pr_info("%s\n", __func__);
  1693. #endif
  1694. return ret;
  1695. }
  1696. static int mpu6500_misc_release(struct inode *inode, struct file *file)
  1697. {
  1698. #if DEBUG
  1699. pr_info("%s\n", __func__);
  1700. #endif
  1701. return 0;
  1702. }
  1703. static const struct file_operations mpu6500_misc_fops = {
  1704. .owner = THIS_MODULE,
  1705. .open = mpu6500_misc_open,
  1706. .release = mpu6500_misc_release,
  1707. };
  1708. static struct miscdevice mpu6500_misc_device = {
  1709. .minor = MISC_DYNAMIC_MINOR,
  1710. .name = "mpu6500",
  1711. .fops = &mpu6500_misc_fops,
  1712. };
  1713. #ifdef CONFIG_INPUT_MPU6500_POLLING
  1714. static void mpu6500_work_func_acc(struct work_struct *work)
  1715. {
  1716. struct mpu6500_input_data *data =
  1717. container_of((struct delayed_work *)work,
  1718. struct mpu6500_input_data, accel_work);
  1719. mpu6500_input_report_accel_xyz(data);
  1720. if (ktime_to_ms(data->accel_delay) < 60) {
  1721. usleep_range(ktime_to_ms(data->accel_delay) * 1000,
  1722. ktime_to_ms(data->accel_delay) * 1100);
  1723. schedule_delayed_work(&data->accel_work, 0);
  1724. } else {
  1725. schedule_delayed_work(&data->accel_work,
  1726. msecs_to_jiffies(
  1727. ktime_to_ms(data->accel_delay)));
  1728. }
  1729. }
  1730. static void mpu6500_work_func_gyro(struct work_struct *work)
  1731. {
  1732. struct mpu6500_input_data *data =
  1733. container_of((struct delayed_work *)work,
  1734. struct mpu6500_input_data, gyro_work);
  1735. mpu6500_input_report_gyro_xyz(data);
  1736. if (ktime_to_ms(data->gyro_delay) < 60) {
  1737. usleep_range(ktime_to_ms(data->gyro_delay) * 1000,
  1738. ktime_to_ms(data->gyro_delay) * 1100);
  1739. schedule_delayed_work(&data->gyro_work, 0);
  1740. } else {
  1741. schedule_delayed_work(&data->gyro_work,
  1742. msecs_to_jiffies(
  1743. ktime_to_ms(data->gyro_delay)));
  1744. }
  1745. }
  1746. #endif
  1747. #ifdef CONFIG_OF
  1748. /* device tree parsing function */
  1749. static int mpu6500_parse_dt(struct device *dev,
  1750. struct mpu6500_platform_data *pdata)
  1751. {
  1752. struct device_node *np = dev->of_node;
  1753. /*scl,sda and irq */
  1754. pdata->i2c_pull_up = of_property_read_bool(np, "invensense,i2c-pull-up");
  1755. pdata->gpio_scl = of_get_named_gpio_flags(np,"invensense,scl-gpio",
  1756. 0, &pdata->scl_gpio_flags);
  1757. pdata->gpio_sda = of_get_named_gpio_flags(np, "invensense,sda-gpio",
  1758. 0, &pdata->sda_gpio_flags);
  1759. /*device tree node properties can be parsed this way*/
  1760. pdata->gpio_int = of_get_named_gpio_flags(np, "invensense,irq-gpio",
  1761. 0, &pdata->irq_gpio_flags);
  1762. /* accel and gyro calibration path */
  1763. pdata->acc_cal_path = of_get_property(np, "invensense,acc_cal_path",
  1764. NULL);
  1765. pdata->gyro_cal_path = of_get_property(np, "invensense,gyro_cal_path",
  1766. NULL);
  1767. pr_err("mpu6500_parse_dt complete, SCL:%d SDA:%d IRQ:%d\n",pdata->gpio_scl, pdata->gpio_sda, pdata->gpio_int );
  1768. return 0;
  1769. }
  1770. #else
  1771. static int mpu6500_parse_dt(struct device *dev,
  1772. struct mpu6k_input_platform_data)
  1773. {
  1774. return -ENODEV;
  1775. }
  1776. #endif
  1777. static int mpu6500_accel_input_init(struct mpu6500_input_data *data)
  1778. {
  1779. struct input_dev *idev;
  1780. int error;
  1781. idev = input_allocate_device();
  1782. if (!idev)
  1783. return -ENOMEM;
  1784. data->accel_input = idev;
  1785. idev->name = MODULE_NAME_ACCEL;
  1786. idev->id.bustype = BUS_I2C;
  1787. input_set_capability(data->accel_input, EV_REL, REL_X);
  1788. input_set_capability(data->accel_input, EV_REL, REL_Y);
  1789. input_set_capability(data->accel_input, EV_REL, REL_Z);
  1790. input_set_drvdata(data->accel_input, data);
  1791. error = input_register_device(data->accel_input);
  1792. if (error) {
  1793. input_free_device(data->accel_input);
  1794. return error;
  1795. }
  1796. error = sensors_create_symlink(&data->accel_input->dev.kobj, idev->name);
  1797. if (error < 0) {
  1798. input_unregister_device(data->accel_input);
  1799. return error;
  1800. }
  1801. error = sysfs_create_group(&idev->dev.kobj,
  1802. &accel_attribute_group);
  1803. if (error) {
  1804. pr_err("%s: could not create sysfs group\n", __func__);
  1805. sensors_remove_symlink(&data->accel_input->dev.kobj,
  1806. data->accel_input->name);
  1807. input_unregister_device(data->accel_input);
  1808. return error;
  1809. }
  1810. atomic_set(&data->accel_enable, 0);
  1811. data->accel_delay = ns_to_ktime(MPU6500_DEFAULT_DELAY);
  1812. data->gyro_delay = ns_to_ktime(MPU6500_DEFAULT_DELAY);
  1813. atomic_set(&data->motion_recg_enable, 0);
  1814. atomic_set(&data->reactive_state, 0);
  1815. return 0;
  1816. }
  1817. static int mpu6500_gyro_input_init(struct mpu6500_input_data *data)
  1818. {
  1819. struct input_dev *idev;
  1820. int error;
  1821. idev = input_allocate_device();
  1822. if (!idev)
  1823. return -ENOMEM;
  1824. data->gyro_input = idev;
  1825. idev->name = MODULE_NAME_GYRO;
  1826. idev->id.bustype = BUS_I2C;
  1827. input_set_capability(data->gyro_input, EV_REL, REL_RX);
  1828. input_set_capability(data->gyro_input, EV_REL, REL_RY);
  1829. input_set_capability(data->gyro_input, EV_REL, REL_RZ);
  1830. input_set_drvdata(data->gyro_input, data);
  1831. error = input_register_device(data->gyro_input);
  1832. if (error) {
  1833. input_free_device(data->gyro_input);
  1834. return error;
  1835. }
  1836. error = sensors_create_symlink(&data->gyro_input->dev.kobj, idev->name);
  1837. if (error < 0) {
  1838. input_unregister_device(data->gyro_input);
  1839. return error;
  1840. }
  1841. error = sysfs_create_group(&idev->dev.kobj,
  1842. &gyro_attribute_group);
  1843. if (error) {
  1844. pr_err("%s: could not create sysfs group\n", __func__);
  1845. sensors_remove_symlink(&data->gyro_input->dev.kobj,
  1846. data->gyro_input->name);
  1847. input_unregister_device(data->gyro_input);
  1848. return error;
  1849. }
  1850. return 0;
  1851. }
  1852. static int __devinit mpu6500_input_probe(struct i2c_client *client,
  1853. const struct i2c_device_id *id)
  1854. {
  1855. const struct mpu6500_input_cfg *cfg;
  1856. struct mpu6500_input_data *data;
  1857. struct mpu6500_platform_data *pdata;
  1858. int error = 0;
  1859. unsigned char whoami = 0;
  1860. int ret = -ENODEV;
  1861. int retry = 5;
  1862. pr_info("[SENSOR] %s: IN\n", __func__);
  1863. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1864. pr_err("[SENSOR] %s: - i2c_check_functionality error\n",
  1865. __func__);
  1866. goto exit;
  1867. }
  1868. if (client-> dev.of_node) {
  1869. pdata = devm_kzalloc (&client->dev ,
  1870. sizeof (struct mpu6500_platform_data), GFP_KERNEL);
  1871. if (!pdata) {
  1872. dev_err(&client->dev, "Failed to allocate memory\n");
  1873. return -ENOMEM;
  1874. }
  1875. error = mpu6500_parse_dt(&client->dev, pdata);
  1876. if (error)
  1877. return error;
  1878. }
  1879. else
  1880. {
  1881. /* get platform data */
  1882. pdata = client->dev.platform_data;
  1883. if (!pdata)
  1884. return -EINVAL;
  1885. }
  1886. mpu6500_request_gpio(pdata);
  1887. data = kzalloc(sizeof(struct mpu6500_input_data), GFP_KERNEL);
  1888. if (!data)
  1889. goto err_kzalloc;
  1890. wake_lock_init(&data->reactive_wake_lock, WAKE_LOCK_SUSPEND,
  1891. "reactive_wake_lock");
  1892. i2c_set_clientdata(client, data);
  1893. data->client = client;
  1894. data->pdata = pdata;
  1895. #if defined(CONFIG_SEC_BERLUTI_PROJECT)
  1896. data->chip_pos = MPU6500_BOTTOM_LEFT_LOWER;
  1897. #elif defined(CONFIG_SEC_S3VE_PROJECT) || defined(CONFIG_SEC_VICTOR3GDSDTV_PROJECT)
  1898. data->chip_pos = MPU6500_TOP_LEFT_UPPER;
  1899. #else
  1900. data->chip_pos = MPU6500_BOTTOM_RIGHT_LOWER;
  1901. #endif
  1902. gb_mpu_data = data;
  1903. mpu6500_vdd_on(data, 1);
  1904. do {
  1905. retry--;
  1906. /* Check if the device is there or not. */
  1907. error = mpu6500_i2c_read_reg(client, MPUREG_WHOAMI, 1, &whoami);
  1908. pr_info("%s : addr = 0x%x, whoami = 0x%x, error = 0x%x \n", __func__,
  1909. client->addr,whoami, error);
  1910. if (error < 0) {
  1911. pr_err("%s: checking i2c error.(%d), retry %d\n",
  1912. __func__, error, retry);
  1913. msleep(20);
  1914. } else {
  1915. break;
  1916. }
  1917. } while (retry);
  1918. if (error < 0) {
  1919. pr_err("[SENSOR] %s: failed : threre is no such device.\n",
  1920. __func__);
  1921. goto err_checking_device;
  1922. }
  1923. if (!(whoami == MPU6500_ID || whoami == MPU6515_ID)) {
  1924. pr_err("[SENSOR] %s: mpu6500 probe failed\n", __func__);
  1925. error = -EIO;
  1926. goto err_checking_device;
  1927. }
  1928. error = mpu6500_input_initialize(data, cfg);
  1929. if (error)
  1930. goto err_checking_device;
  1931. #ifdef CONFIG_INPUT_MPU6500_POLLING
  1932. INIT_DELAYED_WORK(&data->accel_work, mpu6500_work_func_acc);
  1933. INIT_DELAYED_WORK(&data->gyro_work, mpu6500_work_func_gyro);
  1934. #endif
  1935. mutex_init(&data->mutex);
  1936. error = mpu6500_accel_input_init(data);
  1937. if (error)
  1938. goto err_accel_input_init;
  1939. error = mpu6500_gyro_input_init(data);
  1940. if (error)
  1941. goto err_gyro_input_init;
  1942. error = misc_register(&mpu6500_misc_device);
  1943. if (error)
  1944. goto err_misc_register_failed;
  1945. pm_runtime_enable(&client->dev);
  1946. error = sensors_register(data->accel_sensor_device,
  1947. data, accel_sensor_attrs,
  1948. "accelerometer_sensor");
  1949. if (error) {
  1950. pr_err("[SENSOR] %s: cound not register\
  1951. accelerometer sensor device(%d).\n",
  1952. __func__, error);
  1953. goto acc_sensor_register_failed;
  1954. }
  1955. error = sensors_register(data->gyro_sensor_device,
  1956. data, gyro_sensor_attrs,
  1957. "gyro_sensor");
  1958. if (error) {
  1959. pr_err("[SENSOR] %s: cound not register\
  1960. gyro sensor device(%d).\n",
  1961. __func__, error);
  1962. goto gyro_sensor_register_failed;
  1963. }
  1964. ret = request_threaded_irq(client->irq, NULL, mpu6500_input_irq_thread,
  1965. IRQF_TRIGGER_RISING | IRQF_ONESHOT, MPU6500_INPUT_DRIVER, data);
  1966. if (ret < 0) {
  1967. pr_err("[SENSOR] %s: - can't allocate irq.\n", __func__);
  1968. goto exit_reactive_irq;
  1969. }
  1970. disable_irq(client->irq);
  1971. pr_info("[SENSOR] %s: success\n", __func__);
  1972. return 0;
  1973. exit_reactive_irq:
  1974. sensors_unregister(data->gyro_sensor_device, accel_sensor_attrs);
  1975. gyro_sensor_register_failed:
  1976. sensors_unregister(data->accel_sensor_device, accel_sensor_attrs);
  1977. acc_sensor_register_failed:
  1978. misc_deregister(&mpu6500_misc_device);
  1979. err_misc_register_failed:
  1980. sensors_remove_symlink(&data->gyro_input->dev.kobj,
  1981. data->gyro_input->name);
  1982. input_unregister_device(data->gyro_input);
  1983. err_gyro_input_init:
  1984. sensors_remove_symlink(&data->accel_input->dev.kobj,
  1985. data->accel_input->name);
  1986. input_unregister_device(data->accel_input);
  1987. err_accel_input_init:
  1988. mutex_destroy(&data->mutex);
  1989. err_checking_device:
  1990. wake_lock_destroy(&data->reactive_wake_lock);
  1991. kfree(data);
  1992. err_kzalloc:
  1993. exit:
  1994. pr_err("[SENSOR] %s: - Probe fail!\n", __func__);
  1995. return error;
  1996. }
  1997. static int __devexit mpu6500_input_remove(struct i2c_client *client)
  1998. {
  1999. struct mpu6500_input_data *data = i2c_get_clientdata(client);
  2000. if (data == NULL)
  2001. return 0;
  2002. pm_runtime_disable(&client->dev);
  2003. if (client->irq > 0) {
  2004. free_irq(client->irq, data);
  2005. input_unregister_device(data->accel_input);
  2006. input_unregister_device(data->gyro_input);
  2007. }
  2008. kfree(data);
  2009. return 0;
  2010. }
  2011. static int mpu6500_input_suspend(struct device *dev)
  2012. {
  2013. struct i2c_client *client = to_i2c_client(dev);
  2014. struct mpu6500_input_data *data = i2c_get_clientdata(client);
  2015. #ifdef CONFIG_INPUT_MPU6500_POLLING
  2016. if (atomic_read(&data->accel_enable))
  2017. cancel_delayed_work_sync(&data->accel_work);
  2018. if (atomic_read(&data->gyro_enable))
  2019. cancel_delayed_work_sync(&data->gyro_work);
  2020. #endif
  2021. if (!atomic_read(&data->reactive_enable)) {
  2022. #ifndef CONFIG_INPUT_MPU6500_POLLING
  2023. disable_irq_wake(client->irq);
  2024. disable_irq(client->irq);
  2025. #endif
  2026. if (atomic_read(&data->accel_enable) ||
  2027. atomic_read(&data->gyro_enable))
  2028. mpu6500_input_set_mode(data, MPU6500_MODE_SLEEP);
  2029. } else {
  2030. disable_irq(client->irq);
  2031. }
  2032. return 0;
  2033. }
  2034. static int mpu6500_input_resume(struct device *dev)
  2035. {
  2036. struct i2c_client *client = to_i2c_client(dev);
  2037. struct mpu6500_input_data *data = i2c_get_clientdata(client);
  2038. if (data == NULL)
  2039. return 0;
  2040. if (!atomic_read(&data->reactive_enable)) {
  2041. #ifndef CONFIG_INPUT_MPU6500_POLLING
  2042. enable_irq(client->irq);
  2043. enable_irq_wake(client->irq);
  2044. #endif
  2045. if (atomic_read(&data->accel_enable) ||
  2046. atomic_read(&data->gyro_enable))
  2047. mpu6500_input_set_mode(data, MPU6500_MODE_NORMAL);
  2048. } else {
  2049. enable_irq(client->irq);
  2050. }
  2051. #ifdef CONFIG_INPUT_MPU6500_POLLING
  2052. if (atomic_read(&data->accel_enable))
  2053. schedule_delayed_work(&data->accel_work, 0);
  2054. if (atomic_read(&data->gyro_enable))
  2055. schedule_delayed_work(&data->gyro_work, 0);
  2056. #endif
  2057. return 0;
  2058. }
  2059. #ifdef CONFIG_OF
  2060. static struct of_device_id mpu6500_match_table[] = {
  2061. { .compatible = "invensense,mpu_6500",},
  2062. {},
  2063. };
  2064. #else
  2065. #define mpu6500_match_table NULL
  2066. #endif
  2067. static const struct i2c_device_id mpu6500_input_id[] = {
  2068. {"mpu6500_input", 0},
  2069. {}
  2070. };
  2071. //MODULE_DEVICE_TABLE(i2c, mpu6500_input_id);
  2072. static const struct dev_pm_ops mpu6500_dev_pm_ops = {
  2073. .suspend = mpu6500_input_suspend,
  2074. .resume = mpu6500_input_resume,
  2075. };
  2076. static struct i2c_driver mpu6500_input_driver = {
  2077. .driver = {
  2078. .owner = THIS_MODULE,
  2079. .name = MPU6500_INPUT_DRIVER,
  2080. .of_match_table = mpu6500_match_table,
  2081. .pm = &mpu6500_dev_pm_ops
  2082. },
  2083. .class = I2C_CLASS_HWMON,
  2084. .id_table = mpu6500_input_id,
  2085. .probe = mpu6500_input_probe,
  2086. .remove = __devexit_p(mpu6500_input_remove),
  2087. };
  2088. static int __init mpu6500_init(void)
  2089. {
  2090. int result = i2c_add_driver(&mpu6500_input_driver);
  2091. printk(KERN_INFO "[SENSOR] mpu6500_init()\n");
  2092. return result;
  2093. }
  2094. static void __exit mpu6500_exit(void)
  2095. {
  2096. printk(KERN_INFO "[SENSOR] mpu6500_exit()\n");
  2097. i2c_del_driver(&mpu6500_input_driver);
  2098. }
  2099. MODULE_AUTHOR("Tae-Soo Kim <tskim@invensense.com>");
  2100. MODULE_DESCRIPTION("MPU6500 driver");
  2101. MODULE_LICENSE("GPL");
  2102. module_init(mpu6500_init);
  2103. module_exit(mpu6500_exit);