bmg160.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  1. /* BOSCH Gyroscope 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. * History: V1.0 --- [2013.01.29]Driver creation
  13. * V1.1 --- [2013.03.28]
  14. * 1.Instead late_resume, use resume to make sure
  15. * driver resume is ealier than processes resume.
  16. * 2.Add power mode setting in read data.
  17. * V1.2 --- [2013.06.28]Add self test function.
  18. * V1.3 --- [2013.07.26]Fix the bug of wrong axis remapping
  19. * in rawdata inode.
  20. * V1.4 --- [2014.01.11]Add fifo interfaces.
  21. */
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <linux/irq.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/delay.h>
  30. #include <linux/input.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/kobject.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/atomic.h>
  35. #include <linux/mutex.h>
  36. #include <linux/module.h>
  37. #ifdef CONFIG_OF
  38. //#define BMG160_SINGLE_USED
  39. #include <gyroscope.h>
  40. #endif
  41. #include <cust_gyro.h>
  42. #include "bmg160.h"
  43. #if !defined(BMG160_SINGLE_USED)
  44. extern int bsx_algo_gyro_open_report_data(int open);
  45. extern int bsx_algo_gyro_enable_nodata(int en);
  46. extern int bsx_algo_gyro_set_delay(u64 ns);
  47. extern int bsx_algo_gyro_get_data(int* x ,int* y,int* z, int* status);
  48. #endif
  49. /* sensor type */
  50. enum SENSOR_TYPE_ENUM {
  51. BMG160_TYPE = 0x0,
  52. INVALID_TYPE = 0xff
  53. };
  54. /*
  55. *data rate
  56. *boundary defined by daemon
  57. */
  58. enum BMG_DATARATE_ENUM {
  59. BMG_DATARATE_100HZ = 0x0,
  60. BMG_DATARATE_400HZ,
  61. BMG_UNDEFINED_DATARATE = 0xff
  62. };
  63. /* range */
  64. enum BMG_RANGE_ENUM {
  65. BMG_RANGE_2000 = 0x0, /* +/- 2000 degree/s */
  66. BMG_RANGE_1000, /* +/- 1000 degree/s */
  67. BMG_RANGE_500, /* +/- 500 degree/s */
  68. BMG_RANGE_250, /* +/- 250 degree/s */
  69. BMG_RANGE_125, /* +/- 125 degree/s */
  70. BMG_UNDEFINED_RANGE = 0xff
  71. };
  72. /* power mode */
  73. enum BMG_POWERMODE_ENUM {
  74. BMG_SUSPEND_MODE = 0x0,
  75. BMG_NORMAL_MODE,
  76. BMG_UNDEFINED_POWERMODE = 0xff
  77. };
  78. /* debug infomation flags */
  79. enum GYRO_TRC {
  80. GYRO_TRC_FILTER = 0x01,
  81. GYRO_TRC_RAWDATA = 0x02,
  82. GYRO_TRC_IOCTL = 0x04,
  83. GYRO_TRC_CALI = 0x08,
  84. GYRO_TRC_INFO = 0x10,
  85. };
  86. /* s/w data filter */
  87. struct data_filter {
  88. s16 raw[C_MAX_FIR_LENGTH][BMG_AXES_NUM];
  89. int sum[BMG_AXES_NUM];
  90. int num;
  91. int idx;
  92. };
  93. /* bmg i2c client data */
  94. struct bmg_i2c_data {
  95. struct i2c_client *client;
  96. struct gyro_hw *hw;
  97. struct hwmsen_convert cvt;
  98. /* sensor info */
  99. u8 sensor_name[MAX_SENSOR_NAME];
  100. enum SENSOR_TYPE_ENUM sensor_type;
  101. enum BMG_POWERMODE_ENUM power_mode;
  102. enum BMG_RANGE_ENUM range;
  103. enum BMG_DATARATE_ENUM datarate;
  104. /* sensitivity = 2^bitnum/range
  105. [+/-2000 = 4000; +/-1000 = 2000; +/-500 = 1000;
  106. +/-250 = 500; +/-125 = 250 ] */
  107. u16 sensitivity;
  108. u8 fifo_count;
  109. /*misc*/
  110. struct mutex lock;
  111. atomic_t trace;
  112. atomic_t suspend;
  113. atomic_t filter;
  114. s16 cali_sw[BMG_AXES_NUM+1];/* unmapped axis value */
  115. /* hw offset */
  116. s8 offset[BMG_AXES_NUM+1];/*+1: for 4-byte alignment*/
  117. #if defined(CONFIG_BMG_LOWPASS)
  118. atomic_t firlen;
  119. atomic_t fir_en;
  120. struct data_filter fir;
  121. #endif
  122. };
  123. /* log macro */
  124. #define GYRO_TAG "[gyroscope] "
  125. #define GYRO_FUN(f) printk(KERN_INFO GYRO_TAG"%s\n", __func__)
  126. #define GYRO_ERR(fmt, args...) \
  127. printk(KERN_ERR GYRO_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
  128. #define GYRO_LOG(fmt, args...) printk(KERN_INFO GYRO_TAG fmt, ##args)
  129. #define DMA_BUFFER_SIZE 1024
  130. static unsigned char *I2CDMABuf_va = NULL;
  131. static volatile unsigned int I2CDMABuf_pa = NULL;
  132. static struct mutex i2c_lock;
  133. /* Maintain cust info here */
  134. struct gyro_hw gyro_cust;
  135. static struct gyro_hw *hw = &gyro_cust;
  136. //static struct platform_driver bmg_gyroscope_driver;
  137. static struct i2c_driver bmg_i2c_driver;
  138. static struct bmg_i2c_data *obj_i2c_data;
  139. static int bmg_set_powermode(struct i2c_client *client,
  140. enum BMG_POWERMODE_ENUM power_mode);
  141. static int gyroscope_local_init(void);
  142. static int gyroscope_remove(void);
  143. static int bmg160_init_flag =-1; // 0<==>OK -1 <==> fail
  144. static struct gyro_init_info gyroscope_init_info = {
  145. .name = BMG_DEV_NAME,
  146. .init = gyroscope_local_init,
  147. .uninit = gyroscope_remove,
  148. };
  149. static const struct i2c_device_id bmg_i2c_id[] = {
  150. {BMG_DEV_NAME, 0},
  151. {}
  152. };
  153. static struct i2c_board_info __initdata bmg_i2c_info = {
  154. I2C_BOARD_INFO(BMG_DEV_NAME, BMG160_I2C_ADDRESS)
  155. };
  156. static int i2c_dma_read(struct i2c_client *client, unsigned char regaddr, unsigned char *readbuf, int readlen)
  157. {
  158. int ret=0;
  159. if(readlen > DMA_BUFFER_SIZE)
  160. {
  161. GYRO_ERR("Read length cann't exceed dma buffer size!\n");
  162. return -EINVAL;
  163. }
  164. mutex_lock(&i2c_lock);
  165. //write the register address
  166. ret= i2c_master_send(client, &regaddr, 1);
  167. if (ret < 0) {
  168. GYRO_ERR("send command error!!\n");
  169. return -EFAULT;
  170. }
  171. //dma read
  172. client->addr = client->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;
  173. ret = i2c_master_recv(client, (unsigned char *)I2CDMABuf_pa, readlen);
  174. //clear DMA flag once transfer done
  175. client->addr = client->addr & I2C_MASK_FLAG & (~ I2C_DMA_FLAG); //client->ext_flag = client->ext_flag & (~I2C_DMA_FLAG);
  176. if (ret < 0) {
  177. GYRO_ERR("dma receive data error!!\n");
  178. return -EFAULT;
  179. }
  180. memcpy(readbuf, I2CDMABuf_va, readlen);
  181. mutex_unlock(&i2c_lock);
  182. return ret;
  183. }
  184. /* I2C operation functions */
  185. static int bmg_i2c_read_block(struct i2c_client *client, u8 addr,
  186. u8 *data, u8 len)
  187. {
  188. u8 beg = addr;
  189. struct i2c_msg msgs[2] = {
  190. {
  191. .addr = client->addr, .flags = 0,
  192. .len = 1, .buf = &beg
  193. },
  194. {
  195. .addr = client->addr, .flags = I2C_M_RD,
  196. .len = len, .buf = data,
  197. }
  198. };
  199. int err;
  200. if (!client)
  201. return -EINVAL;
  202. /*
  203. else if (len > I2C_BUFFER_SIZE) {
  204. GYRO_ERR(" length %d exceeds %d\n", len, I2C_BUFFER_SIZE);
  205. return -EINVAL;
  206. }
  207. */
  208. err = i2c_transfer(client->adapter, msgs, sizeof(msgs)/sizeof(msgs[0]));
  209. if (err != 2) {
  210. GYRO_ERR("i2c_transfer error: (%d %p %d) %d\n",
  211. addr, data, len, err);
  212. err = -EIO;
  213. } else {
  214. err = 0;/*no error*/
  215. }
  216. return err;
  217. }
  218. static int bmg_i2c_write_block(struct i2c_client *client, u8 addr,
  219. u8 *data, u8 len)
  220. {
  221. int err, idx = 0, num = 0;
  222. char buf[I2C_BUFFER_SIZE];
  223. if (!client)
  224. return -EINVAL;
  225. /*
  226. else if (len >= I2C_BUFFER_SIZE) {
  227. GYRO_ERR("length %d exceeds %d\n", len, I2C_BUFFER_SIZE);
  228. return -EINVAL;
  229. }
  230. */
  231. buf[num++] = addr;
  232. for (idx = 0; idx < len; idx++)
  233. buf[num++] = data[idx];
  234. err = i2c_master_send(client, buf, num);
  235. if (err < 0) {
  236. GYRO_ERR("send command error!!\n");
  237. return -EFAULT;
  238. } else {
  239. err = 0;/*no error*/
  240. }
  241. return err;
  242. }
  243. static void bmg_power(struct gyro_hw *hw, unsigned int on)
  244. {
  245. }
  246. static int bmg_read_raw_data(struct i2c_client *client, s16 data[BMG_AXES_NUM])
  247. {
  248. struct bmg_i2c_data *priv = i2c_get_clientdata(client);
  249. int err = 0;
  250. if (priv->power_mode == BMG_SUSPEND_MODE) {
  251. err = bmg_set_powermode(client,
  252. (enum BMG_POWERMODE_ENUM)BMG_NORMAL_MODE);
  253. if (err < 0) {
  254. GYRO_ERR("set power mode failed, err = %d\n", err);
  255. return err;
  256. }
  257. }
  258. memset(data, 0, sizeof(s16)*BMG_AXES_NUM);
  259. if (NULL == client) {
  260. err = -EINVAL;
  261. return err;
  262. }
  263. if (priv->sensor_type == BMG160_TYPE) {/* BMG160 */
  264. u8 buf_tmp[BMG_DATA_LEN] = {0};
  265. err = bmg_i2c_read_block(client, BMG160_RATE_X_LSB_ADDR,
  266. buf_tmp, 6);
  267. if (err) {
  268. GYRO_ERR("[%s]read raw data failed, err = %d\n",
  269. priv->sensor_name, err);
  270. return err;
  271. }
  272. data[BMG_AXIS_X] =
  273. (s16)(((u16)buf_tmp[1]) << 8 | buf_tmp[0]);
  274. data[BMG_AXIS_Y] =
  275. (s16)(((u16)buf_tmp[3]) << 8 | buf_tmp[2]);
  276. data[BMG_AXIS_Z] =
  277. (s16)(((u16)buf_tmp[5]) << 8 | buf_tmp[4]);
  278. if (atomic_read(&priv->trace) & GYRO_TRC_RAWDATA) {
  279. GYRO_LOG("[%s][16bit raw]"
  280. "[%08X %08X %08X] => [%5d %5d %5d]\n",
  281. priv->sensor_name,
  282. data[BMG_AXIS_X],
  283. data[BMG_AXIS_Y],
  284. data[BMG_AXIS_Z],
  285. data[BMG_AXIS_X],
  286. data[BMG_AXIS_Y],
  287. data[BMG_AXIS_Z]);
  288. }
  289. }
  290. #ifdef CONFIG_BMG_LOWPASS
  291. /*
  292. *Example: firlen = 16, filter buffer = [0] ... [15],
  293. *when 17th data come, replace [0] with this new data.
  294. *Then, average this filter buffer and report average value to upper layer.
  295. */
  296. if (atomic_read(&priv->filter)) {
  297. if (atomic_read(&priv->fir_en) &&
  298. !atomic_read(&priv->suspend)) {
  299. int idx, firlen = atomic_read(&priv->firlen);
  300. if (priv->fir.num < firlen) {
  301. priv->fir.raw[priv->fir.num][BMG_AXIS_X] =
  302. data[BMG_AXIS_X];
  303. priv->fir.raw[priv->fir.num][BMG_AXIS_Y] =
  304. data[BMG_AXIS_Y];
  305. priv->fir.raw[priv->fir.num][BMG_AXIS_Z] =
  306. data[BMG_AXIS_Z];
  307. priv->fir.sum[BMG_AXIS_X] += data[BMG_AXIS_X];
  308. priv->fir.sum[BMG_AXIS_Y] += data[BMG_AXIS_Y];
  309. priv->fir.sum[BMG_AXIS_Z] += data[BMG_AXIS_Z];
  310. if (atomic_read(&priv->trace)&GYRO_TRC_FILTER) {
  311. GYRO_LOG("add [%2d]"
  312. "[%5d %5d %5d] => [%5d %5d %5d]\n",
  313. priv->fir.num,
  314. priv->fir.raw
  315. [priv->fir.num][BMG_AXIS_X],
  316. priv->fir.raw
  317. [priv->fir.num][BMG_AXIS_Y],
  318. priv->fir.raw
  319. [priv->fir.num][BMG_AXIS_Z],
  320. priv->fir.sum[BMG_AXIS_X],
  321. priv->fir.sum[BMG_AXIS_Y],
  322. priv->fir.sum[BMG_AXIS_Z]);
  323. }
  324. priv->fir.num++;
  325. priv->fir.idx++;
  326. } else {
  327. idx = priv->fir.idx % firlen;
  328. priv->fir.sum[BMG_AXIS_X] -=
  329. priv->fir.raw[idx][BMG_AXIS_X];
  330. priv->fir.sum[BMG_AXIS_Y] -=
  331. priv->fir.raw[idx][BMG_AXIS_Y];
  332. priv->fir.sum[BMG_AXIS_Z] -=
  333. priv->fir.raw[idx][BMG_AXIS_Z];
  334. priv->fir.raw[idx][BMG_AXIS_X] =
  335. data[BMG_AXIS_X];
  336. priv->fir.raw[idx][BMG_AXIS_Y] =
  337. data[BMG_AXIS_Y];
  338. priv->fir.raw[idx][BMG_AXIS_Z] =
  339. data[BMG_AXIS_Z];
  340. priv->fir.sum[BMG_AXIS_X] +=
  341. data[BMG_AXIS_X];
  342. priv->fir.sum[BMG_AXIS_Y] +=
  343. data[BMG_AXIS_Y];
  344. priv->fir.sum[BMG_AXIS_Z] +=
  345. data[BMG_AXIS_Z];
  346. priv->fir.idx++;
  347. data[BMG_AXIS_X] =
  348. priv->fir.sum[BMG_AXIS_X]/firlen;
  349. data[BMG_AXIS_Y] =
  350. priv->fir.sum[BMG_AXIS_Y]/firlen;
  351. data[BMG_AXIS_Z] =
  352. priv->fir.sum[BMG_AXIS_Z]/firlen;
  353. if (atomic_read(&priv->trace)&GYRO_TRC_FILTER) {
  354. GYRO_LOG("add [%2d]"
  355. "[%5d %5d %5d] =>"
  356. "[%5d %5d %5d] : [%5d %5d %5d]\n", idx,
  357. priv->fir.raw[idx][BMG_AXIS_X],
  358. priv->fir.raw[idx][BMG_AXIS_Y],
  359. priv->fir.raw[idx][BMG_AXIS_Z],
  360. priv->fir.sum[BMG_AXIS_X],
  361. priv->fir.sum[BMG_AXIS_Y],
  362. priv->fir.sum[BMG_AXIS_Z],
  363. data[BMG_AXIS_X],
  364. data[BMG_AXIS_Y],
  365. data[BMG_AXIS_Z]);
  366. }
  367. }
  368. }
  369. }
  370. #endif
  371. return err;
  372. }
  373. /* get hardware offset value from chip register */
  374. /*
  375. static int bmg_get_hw_offset(struct i2c_client *client,
  376. s8 offset[BMG_AXES_NUM + 1])
  377. {
  378. int err = 0;
  379. /* HW calibration is under construction */
  380. /* GYRO_LOG("hw offset x=%x, y=%x, z=%x\n",
  381. offset[BMG_AXIS_X], offset[BMG_AXIS_Y], offset[BMG_AXIS_Z]);
  382. return err;
  383. }
  384. */
  385. /* set hardware offset value to chip register*/
  386. /*
  387. static int bmg_set_hw_offset(struct i2c_client *client,
  388. s8 offset[BMG_AXES_NUM + 1])
  389. {
  390. int err = 0;
  391. /* HW calibration is under construction */
  392. /* GYRO_LOG("hw offset x=%x, y=%x, z=%x\n",
  393. offset[BMG_AXIS_X], offset[BMG_AXIS_Y], offset[BMG_AXIS_Z]);
  394. return err;
  395. }
  396. */
  397. static int bmg_reset_calibration(struct i2c_client *client)
  398. {
  399. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  400. int err = 0;
  401. #ifdef SW_CALIBRATION
  402. #else
  403. err = bmg_set_hw_offset(client, obj->offset);
  404. if (err) {
  405. GYRO_ERR("read hw offset failed, %d\n", err);
  406. return err;
  407. }
  408. #endif
  409. memset(obj->cali_sw, 0x00, sizeof(obj->cali_sw));
  410. memset(obj->offset, 0x00, sizeof(obj->offset));
  411. return err;
  412. }
  413. static int bmg_read_calibration(struct i2c_client *client,
  414. int act[BMG_AXES_NUM], int raw[BMG_AXES_NUM])
  415. {
  416. /*
  417. *raw: the raw calibration data, unmapped;
  418. *act: the actual calibration data, mapped
  419. */
  420. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  421. int err = 0;
  422. int mul;
  423. #ifdef SW_CALIBRATION
  424. /* only sw calibration, disable hw calibration */
  425. mul = 0;
  426. #else
  427. err = bmg_get_hw_offset(client, obj->offset);
  428. if (err) {
  429. GYRO_ERR("read hw offset failed, %d\n", err);
  430. return err;
  431. }
  432. mul = 1; /* mul = sensor sensitivity / offset sensitivity */
  433. #endif
  434. raw[BMG_AXIS_X] =
  435. obj->offset[BMG_AXIS_X]*mul + obj->cali_sw[BMG_AXIS_X];
  436. raw[BMG_AXIS_Y] =
  437. obj->offset[BMG_AXIS_Y]*mul + obj->cali_sw[BMG_AXIS_Y];
  438. raw[BMG_AXIS_Z] =
  439. obj->offset[BMG_AXIS_Z]*mul + obj->cali_sw[BMG_AXIS_Z];
  440. act[obj->cvt.map[BMG_AXIS_X]] =
  441. obj->cvt.sign[BMG_AXIS_X]*raw[BMG_AXIS_X];
  442. act[obj->cvt.map[BMG_AXIS_Y]] =
  443. obj->cvt.sign[BMG_AXIS_Y]*raw[BMG_AXIS_Y];
  444. act[obj->cvt.map[BMG_AXIS_Z]] =
  445. obj->cvt.sign[BMG_AXIS_Z]*raw[BMG_AXIS_Z];
  446. return err;
  447. }
  448. static int bmg_write_calibration(struct i2c_client *client,
  449. int dat[BMG_AXES_NUM])
  450. {
  451. /* dat array : Android coordinate system, mapped, unit:LSB */
  452. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  453. int err = 0;
  454. int cali[BMG_AXES_NUM], raw[BMG_AXES_NUM];
  455. /*offset will be updated in obj->offset */
  456. err = bmg_read_calibration(client, cali, raw);
  457. if (err) {
  458. GYRO_ERR("read offset fail, %d\n", err);
  459. return err;
  460. }
  461. GYRO_LOG("OLD OFFSET:"
  462. "unmapped raw offset(%+3d %+3d %+3d),"
  463. "unmapped hw offset(%+3d %+3d %+3d),"
  464. "unmapped cali_sw (%+3d %+3d %+3d)\n",
  465. raw[BMG_AXIS_X], raw[BMG_AXIS_Y], raw[BMG_AXIS_Z],
  466. obj->offset[BMG_AXIS_X],
  467. obj->offset[BMG_AXIS_Y],
  468. obj->offset[BMG_AXIS_Z],
  469. obj->cali_sw[BMG_AXIS_X],
  470. obj->cali_sw[BMG_AXIS_Y],
  471. obj->cali_sw[BMG_AXIS_Z]);
  472. /* calculate the real offset expected by caller */
  473. cali[BMG_AXIS_X] += dat[BMG_AXIS_X];
  474. cali[BMG_AXIS_Y] += dat[BMG_AXIS_Y];
  475. cali[BMG_AXIS_Z] += dat[BMG_AXIS_Z];
  476. GYRO_LOG("UPDATE: add mapped data(%+3d %+3d %+3d)\n",
  477. dat[BMG_AXIS_X], dat[BMG_AXIS_Y], dat[BMG_AXIS_Z]);
  478. #ifdef SW_CALIBRATION
  479. /* obj->cali_sw array : chip coordinate system, unmapped,unit:LSB */
  480. obj->cali_sw[BMG_AXIS_X] =
  481. obj->cvt.sign[BMG_AXIS_X]*(cali[obj->cvt.map[BMG_AXIS_X]]);
  482. obj->cali_sw[BMG_AXIS_Y] =
  483. obj->cvt.sign[BMG_AXIS_Y]*(cali[obj->cvt.map[BMG_AXIS_Y]]);
  484. obj->cali_sw[BMG_AXIS_Z] =
  485. obj->cvt.sign[BMG_AXIS_Z]*(cali[obj->cvt.map[BMG_AXIS_Z]]);
  486. #else
  487. int divisor = 1; /* divisor = sensor sensitivity / offset sensitivity */
  488. obj->offset[BMG_AXIS_X] = (s8)(obj->cvt.sign[BMG_AXIS_X]*
  489. (cali[obj->cvt.map[BMG_AXIS_X]])/(divisor));
  490. obj->offset[BMG_AXIS_Y] = (s8)(obj->cvt.sign[BMG_AXIS_Y]*
  491. (cali[obj->cvt.map[BMG_AXIS_Y]])/(divisor));
  492. obj->offset[BMG_AXIS_Z] = (s8)(obj->cvt.sign[BMG_AXIS_Z]*
  493. (cali[obj->cvt.map[BMG_AXIS_Z]])/(divisor));
  494. /*convert software calibration using standard calibration*/
  495. obj->cali_sw[BMG_AXIS_X] = obj->cvt.sign[BMG_AXIS_X]*
  496. (cali[obj->cvt.map[BMG_AXIS_X]])%(divisor);
  497. obj->cali_sw[BMG_AXIS_Y] = obj->cvt.sign[BMG_AXIS_Y]*
  498. (cali[obj->cvt.map[BMG_AXIS_Y]])%(divisor);
  499. obj->cali_sw[BMG_AXIS_Z] = obj->cvt.sign[BMG_AXIS_Z]*
  500. (cali[obj->cvt.map[BMG_AXIS_Z]])%(divisor);
  501. GYRO_LOG("NEW OFFSET:"
  502. "unmapped raw offset(%+3d %+3d %+3d),"
  503. "unmapped hw offset(%+3d %+3d %+3d),"
  504. "unmapped cali_sw(%+3d %+3d %+3d)\n",
  505. obj->offset[BMG_AXIS_X]*divisor + obj->cali_sw[BMG_AXIS_X],
  506. obj->offset[BMG_AXIS_Y]*divisor + obj->cali_sw[BMG_AXIS_Y],
  507. obj->offset[BMG_AXIS_Z]*divisor + obj->cali_sw[BMG_AXIS_Z],
  508. obj->offset[BMG_AXIS_X],
  509. obj->offset[BMG_AXIS_Y],
  510. obj->offset[BMG_AXIS_Z],
  511. obj->cali_sw[BMG_AXIS_X],
  512. obj->cali_sw[BMG_AXIS_Y],
  513. obj->cali_sw[BMG_AXIS_Z]);
  514. /* HW calibration is under construction */
  515. err = bmg_set_hw_offset(client, obj->offset);
  516. if (err) {
  517. GYRO_ERR("read hw offset failed, %d\n", err);
  518. return err;
  519. }
  520. #endif
  521. return err;
  522. }
  523. /* get chip type */
  524. static int bmg_get_chip_type(struct i2c_client *client)
  525. {
  526. int err = 0;
  527. u8 chip_id = 0;
  528. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  529. GYRO_FUN(f);
  530. /* twice */
  531. err = bmg_i2c_read_block(client, BMG_CHIP_ID_REG, &chip_id, 0x01);
  532. err = bmg_i2c_read_block(client, BMG_CHIP_ID_REG, &chip_id, 0x01);
  533. if (err != 0)
  534. return err;
  535. switch (chip_id) {
  536. case BMG160_CHIP_ID:
  537. obj->sensor_type = BMG160_TYPE;
  538. strcpy(obj->sensor_name, "bmg160");
  539. break;
  540. default:
  541. obj->sensor_type = INVALID_TYPE;
  542. strcpy(obj->sensor_name, "unknown sensor");
  543. break;
  544. }
  545. GYRO_LOG("[%s]chip id = %#x, sensor name = %s\n",
  546. __func__, chip_id, obj->sensor_name);
  547. if (obj->sensor_type == INVALID_TYPE) {
  548. GYRO_ERR("unknown gyroscope\n");
  549. return -1;
  550. }
  551. return 0;
  552. }
  553. /* set power mode */
  554. static int bmg_set_powermode(struct i2c_client *client,
  555. enum BMG_POWERMODE_ENUM power_mode)
  556. {
  557. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  558. u8 err = 0, data = 0, actual_power_mode = 0;
  559. GYRO_LOG("[%s] power_mode = %d, old power_mode = %d\n",
  560. __func__, power_mode, obj->power_mode);
  561. if (power_mode == obj->power_mode)
  562. return 0;
  563. mutex_lock(&obj->lock);
  564. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  565. if (power_mode == BMG_SUSPEND_MODE) {
  566. actual_power_mode = BMG160_SUSPEND_MODE;
  567. } else if (power_mode == BMG_NORMAL_MODE) {
  568. actual_power_mode = BMG160_NORMAL_MODE;
  569. } else {
  570. err = -EINVAL;
  571. GYRO_ERR("invalid power mode = %d\n", power_mode);
  572. mutex_unlock(&obj->lock);
  573. return err;
  574. }
  575. err = bmg_i2c_read_block(client,
  576. BMG160_MODE_LPM1__REG, &data, 1);
  577. data = BMG_SET_BITSLICE(data,
  578. BMG160_MODE_LPM1, actual_power_mode);
  579. err += bmg_i2c_write_block(client,
  580. BMG160_MODE_LPM1__REG, &data, 1);
  581. mdelay(50);
  582. }
  583. if (err < 0)
  584. GYRO_ERR("set power mode failed, err = %d, sensor name = %s\n",
  585. err, obj->sensor_name);
  586. else
  587. obj->power_mode = power_mode;
  588. mutex_unlock(&obj->lock);
  589. return err;
  590. }
  591. static int bmg_set_range(struct i2c_client *client, enum BMG_RANGE_ENUM range)
  592. {
  593. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  594. u8 err = 0, data = 0, actual_range = 0;
  595. GYRO_LOG("[%s] range = %d, old range = %d\n",
  596. __func__, range, obj->range);
  597. if (range == obj->range)
  598. return 0;
  599. mutex_lock(&obj->lock);
  600. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  601. if (range == BMG_RANGE_2000)
  602. actual_range = BMG160_RANGE_2000;
  603. else if (range == BMG_RANGE_1000)
  604. actual_range = BMG160_RANGE_1000;
  605. else if (range == BMG_RANGE_500)
  606. actual_range = BMG160_RANGE_500;
  607. else if (range == BMG_RANGE_500)
  608. actual_range = BMG160_RANGE_250;
  609. else if (range == BMG_RANGE_500)
  610. actual_range = BMG160_RANGE_125;
  611. else {
  612. err = -EINVAL;
  613. GYRO_ERR("invalid range = %d\n", range);
  614. mutex_unlock(&obj->lock);
  615. return err;
  616. }
  617. err = bmg_i2c_read_block(client,
  618. BMG160_RANGE_ADDR_RANGE__REG, &data, 1);
  619. data = BMG_SET_BITSLICE(data,
  620. BMG160_RANGE_ADDR_RANGE, actual_range);
  621. err += bmg_i2c_write_block(client,
  622. BMG160_RANGE_ADDR_RANGE__REG, &data, 1);
  623. mdelay(1);
  624. if (err < 0)
  625. GYRO_ERR("set range failed,"
  626. "err = %d, sensor name = %s\n", err, obj->sensor_name);
  627. else {
  628. obj->range = range;
  629. /* bitnum: 16bit */
  630. switch (range) {
  631. case BMG_RANGE_2000:
  632. obj->sensitivity = 16;
  633. break;
  634. case BMG_RANGE_1000:
  635. obj->sensitivity = 33;
  636. break;
  637. case BMG_RANGE_500:
  638. obj->sensitivity = 66;
  639. break;
  640. case BMG_RANGE_250:
  641. obj->sensitivity = 131;
  642. break;
  643. case BMG_RANGE_125:
  644. obj->sensitivity = 262;
  645. break;
  646. default:
  647. obj->sensitivity = 16;
  648. break;
  649. }
  650. }
  651. }
  652. mutex_unlock(&obj->lock);
  653. return err;
  654. }
  655. static int bmg_set_datarate(struct i2c_client *client,
  656. enum BMG_DATARATE_ENUM datarate)
  657. {
  658. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  659. u8 err = 0, data = 0, bandwidth = 0;
  660. GYRO_LOG("[%s] datarate = %d, old datarate = %d\n",
  661. __func__, datarate, obj->datarate);
  662. if (datarate == obj->datarate)
  663. return 0;
  664. mutex_lock(&obj->lock);
  665. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  666. if (datarate == BMG_DATARATE_100HZ)
  667. bandwidth = C_BMG160_BW_32Hz_U8X;
  668. else if (datarate == BMG_DATARATE_400HZ)
  669. bandwidth = C_BMG160_BW_47Hz_U8X;
  670. else {
  671. err = -EINVAL;
  672. GYRO_ERR("invalid datarate = %d\n", datarate);
  673. mutex_unlock(&obj->lock);
  674. return err;
  675. }
  676. err = bmg_i2c_read_block(client,
  677. BMG160_BW_ADDR__REG, &data, 1);
  678. data = BMG_SET_BITSLICE(data,
  679. BMG160_BW_ADDR, bandwidth);
  680. err += bmg_i2c_write_block(client,
  681. BMG160_BW_ADDR__REG, &data, 1);
  682. }
  683. if (err < 0)
  684. GYRO_ERR("set bandwidth failed,"
  685. "err = %d,sensor type = %d\n", err, obj->sensor_type);
  686. else
  687. obj->datarate = datarate;
  688. mutex_unlock(&obj->lock);
  689. return err;
  690. }
  691. static int bmg_get_fifo_mode(struct i2c_client *client,
  692. u8 *fifo_mode)
  693. {
  694. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  695. u8 err = 0, data = 0;
  696. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  697. err = bmg_i2c_read_block(client,
  698. BMG160_FIFO_CGF0_ADDR_MODE__REG, &data, 1);
  699. *fifo_mode = BMG_GET_BITSLICE(data,
  700. BMG160_FIFO_CGF0_ADDR_MODE);
  701. }
  702. if (err < 0)
  703. GYRO_ERR("get fifo mode failed,"
  704. "err = %d,sensor type = %d\n", err, obj->sensor_type);
  705. /*
  706. else
  707. GYRO_LOG("[%s] fifo mode = %d\n", __func__, *fifo_mode);
  708. */
  709. return err;
  710. }
  711. static int bmg_set_fifo_mode(struct i2c_client *client,
  712. u8 fifo_mode)
  713. {
  714. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  715. u8 err = 0, data = 0;
  716. /*
  717. GYRO_LOG("[%s] fifo_mode = %d\n",
  718. __func__, fifo_mode);
  719. */
  720. if (fifo_mode >= 4)
  721. return -EINVAL;;
  722. mutex_lock(&obj->lock);
  723. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  724. err = bmg_i2c_read_block(client,
  725. BMG160_FIFO_CGF0_ADDR_MODE__REG, &data, 1);
  726. data = BMG_SET_BITSLICE(data,
  727. BMG160_FIFO_CGF0_ADDR_MODE, fifo_mode);
  728. err += bmg_i2c_write_block(client,
  729. BMG160_FIFO_CGF0_ADDR_MODE__REG, &data, 1);
  730. }
  731. if (err < 0)
  732. GYRO_ERR("set fifo mode failed,"
  733. "err = %d,sensor type = %d\n", err, obj->sensor_type);
  734. mutex_unlock(&obj->lock);
  735. return err;
  736. }
  737. static int bmg_get_fifo_framecount(struct i2c_client *client,
  738. u8 *fifo_framecount)
  739. {
  740. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  741. u8 err = 0, data = 0;
  742. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  743. err = bmg_i2c_read_block(client,
  744. BMG160_FIFO_STATUS_FRAME_COUNTER__REG, &data, 1);
  745. *fifo_framecount = BMG_GET_BITSLICE(data,
  746. BMG160_FIFO_STATUS_FRAME_COUNTER);
  747. }
  748. if (err < 0)
  749. GYRO_ERR("get fifo framecount failed,"
  750. "err = %d,sensor type = %d\n", err, obj->sensor_type);
  751. /*
  752. else
  753. GYRO_LOG("[%s] fifo framecount = %d\n", __func__, *fifo_framecount);
  754. */
  755. return err;
  756. }
  757. static int bmg_selftest(struct i2c_client *client, u8 *result)
  758. {
  759. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  760. int err = 0 ;
  761. u8 data1 = 0, data2 = 0;
  762. err = bmg_set_powermode(client,
  763. (enum BMG_POWERMODE_ENUM)BMG_NORMAL_MODE);
  764. mutex_lock(&obj->lock);
  765. if (obj->sensor_type == BMG160_TYPE) {/* BMG160 */
  766. err = bmg_i2c_read_block(client,
  767. BMG160_SELF_TEST_ADDR, &data1, 1);
  768. data2 = BMG_GET_BITSLICE(data1, BMG160_SELF_TEST_ADDR_RATEOK);
  769. data1 = BMG_SET_BITSLICE(data1,
  770. BMG160_SELF_TEST_ADDR_TRIGBIST, 1);
  771. err += bmg_i2c_write_block(client,
  772. BMG160_SELF_TEST_ADDR_TRIGBIST__REG, &data1, 1);
  773. /* waiting time to complete the selftest process */
  774. mdelay(10);
  775. /* reading Selftest result bir bist_failure */
  776. err = bmg_i2c_read_block(client, \
  777. BMG160_SELF_TEST_ADDR_BISTFAIL__REG, &data1, 1);
  778. data1 = BMG_GET_BITSLICE(data1,
  779. BMG160_SELF_TEST_ADDR_BISTFAIL);
  780. if ((data1 == 0x00) && (data2 == 0x01))
  781. *result = 1;/* success */
  782. else
  783. *result = 0;/* failure */
  784. }
  785. GYRO_LOG("%s: %s\n", __func__,
  786. *result ? "success" : "failure");
  787. mutex_unlock(&obj->lock);
  788. return err;
  789. }
  790. /* bmg setting initialization */
  791. static int bmg_init_client(struct i2c_client *client, int reset_cali)
  792. {
  793. #ifdef CONFIG_BMG_LOWPASS
  794. struct bmg_i2c_data *obj =
  795. (struct bmg_i2c_data *)i2c_get_clientdata(client);
  796. #endif
  797. int err = 0;
  798. GYRO_FUN(f);
  799. err = bmg_get_chip_type(client);
  800. if (err < 0) {
  801. GYRO_ERR("get chip type failed, err = %d\n", err);
  802. return err;
  803. }
  804. err = bmg_set_datarate(client,
  805. (enum BMG_DATARATE_ENUM)BMG_DATARATE_100HZ);
  806. if (err < 0) {
  807. GYRO_ERR("set bandwidth failed, err = %d\n", err);
  808. return err;
  809. }
  810. err = bmg_set_range(client, (enum BMG_RANGE_ENUM)BMG_RANGE_2000);
  811. if (err < 0) {
  812. GYRO_ERR("set range failed, err = %d\n", err);
  813. return err;
  814. }
  815. err = bmg_set_powermode(client,
  816. (enum BMG_POWERMODE_ENUM)BMG_SUSPEND_MODE);
  817. if (err < 0) {
  818. GYRO_ERR("set power mode failed, err = %d\n", err);
  819. return err;
  820. }
  821. if (0 != reset_cali) {
  822. /*reset calibration only in power on*/
  823. err = bmg_reset_calibration(client);
  824. if (err < 0) {
  825. GYRO_ERR("reset calibration failed, err = %d\n", err);
  826. return err;
  827. }
  828. }
  829. #ifdef CONFIG_BMG_LOWPASS
  830. memset(&obj->fir, 0x00, sizeof(obj->fir));
  831. #endif
  832. return 0;
  833. }
  834. /*
  835. *Returns compensated and mapped value. unit is :degree/second
  836. */
  837. static int bmg_read_sensor_data(struct i2c_client *client,
  838. char *buf, int bufsize)
  839. {
  840. struct bmg_i2c_data *obj =
  841. (struct bmg_i2c_data *)i2c_get_clientdata(client);
  842. s16 databuf[BMG_AXES_NUM];
  843. int gyro[BMG_AXES_NUM];
  844. int err = 0;
  845. memset(databuf, 0, sizeof(s16)*BMG_AXES_NUM);
  846. memset(gyro, 0, sizeof(int)*BMG_AXES_NUM);
  847. if (NULL == buf)
  848. return -1;
  849. if (NULL == client) {
  850. *buf = 0;
  851. return -2;
  852. }
  853. err = bmg_read_raw_data(client, databuf);
  854. if (err) {
  855. GYRO_ERR("bmg read raw data failed, err = %d\n", err);
  856. return -3;
  857. } else {
  858. /* compensate data */
  859. databuf[BMG_AXIS_X] += obj->cali_sw[BMG_AXIS_X];
  860. databuf[BMG_AXIS_Y] += obj->cali_sw[BMG_AXIS_Y];
  861. databuf[BMG_AXIS_Z] += obj->cali_sw[BMG_AXIS_Z];
  862. /* remap coordinate */
  863. gyro[obj->cvt.map[BMG_AXIS_X]] =
  864. obj->cvt.sign[BMG_AXIS_X]*databuf[BMG_AXIS_X];
  865. gyro[obj->cvt.map[BMG_AXIS_Y]] =
  866. obj->cvt.sign[BMG_AXIS_Y]*databuf[BMG_AXIS_Y];
  867. gyro[obj->cvt.map[BMG_AXIS_Z]] =
  868. obj->cvt.sign[BMG_AXIS_Z]*databuf[BMG_AXIS_Z];
  869. /* convert: LSB -> degree/second(o/s) */
  870. gyro[BMG_AXIS_X] = gyro[BMG_AXIS_X] / obj->sensitivity;
  871. gyro[BMG_AXIS_Y] = gyro[BMG_AXIS_Y] / obj->sensitivity;
  872. gyro[BMG_AXIS_Z] = gyro[BMG_AXIS_Z] / obj->sensitivity;
  873. sprintf(buf, "%04x %04x %04x",
  874. gyro[BMG_AXIS_X], gyro[BMG_AXIS_Y], gyro[BMG_AXIS_Z]);
  875. if (atomic_read(&obj->trace) & GYRO_TRC_IOCTL)
  876. GYRO_LOG("gyroscope data: %s\n", buf);
  877. }
  878. return 0;
  879. }
  880. static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
  881. {
  882. struct bmg_i2c_data *obj = obj_i2c_data;
  883. if (NULL == obj) {
  884. GYRO_ERR("bmg i2c data pointer is null\n");
  885. return 0;
  886. }
  887. return snprintf(buf, PAGE_SIZE, "%s\n", obj->sensor_name);
  888. }
  889. /*
  890. * sensor data format is hex, unit:degree/second
  891. */
  892. static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
  893. {
  894. struct bmg_i2c_data *obj = obj_i2c_data;
  895. char strbuf[BMG_BUFSIZE] = "";
  896. if (NULL == obj) {
  897. GYRO_ERR("bmg i2c data pointer is null\n");
  898. return 0;
  899. }
  900. bmg_read_sensor_data(obj->client, strbuf, BMG_BUFSIZE);
  901. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  902. }
  903. /*
  904. * raw data format is s16, unit:LSB, axis mapped
  905. */
  906. static ssize_t show_rawdata_value(struct device_driver *ddri, char *buf)
  907. {
  908. struct bmg_i2c_data *obj = obj_i2c_data;
  909. s16 databuf[BMG_AXES_NUM];
  910. s16 dataraw[BMG_AXES_NUM];
  911. if (NULL == obj) {
  912. GYRO_ERR("bmg i2c data pointer is null\n");
  913. return 0;
  914. }
  915. bmg_read_raw_data(obj->client, dataraw);
  916. /*remap coordinate*/
  917. databuf[obj->cvt.map[BMG_AXIS_X]] =
  918. obj->cvt.sign[BMG_AXIS_X]*dataraw[BMG_AXIS_X];
  919. databuf[obj->cvt.map[BMG_AXIS_Y]] =
  920. obj->cvt.sign[BMG_AXIS_Y]*dataraw[BMG_AXIS_Y];
  921. databuf[obj->cvt.map[BMG_AXIS_Z]] =
  922. obj->cvt.sign[BMG_AXIS_Z]*dataraw[BMG_AXIS_Z];
  923. return snprintf(buf, PAGE_SIZE, "%hd %hd %hd\n",
  924. databuf[BMG_AXIS_X],
  925. databuf[BMG_AXIS_Y],
  926. databuf[BMG_AXIS_Z]);
  927. }
  928. static ssize_t show_cali_value(struct device_driver *ddri, char *buf)
  929. {
  930. struct bmg_i2c_data *obj = obj_i2c_data;
  931. int err, len = 0, mul;
  932. int act[BMG_AXES_NUM], raw[BMG_AXES_NUM];
  933. if (NULL == obj) {
  934. GYRO_ERR("bmg i2c data pointer is null\n");
  935. return 0;
  936. }
  937. err = bmg_read_calibration(obj->client, act, raw);
  938. if (err)
  939. return -EINVAL;
  940. else {
  941. mul = 1; /* mul = sensor sensitivity / offset sensitivity */
  942. len += snprintf(buf+len, PAGE_SIZE-len,
  943. "[HW ][%d] (%+3d, %+3d, %+3d) : (0x%02X, 0x%02X, 0x%02X)\n",
  944. mul,
  945. obj->offset[BMG_AXIS_X],
  946. obj->offset[BMG_AXIS_Y],
  947. obj->offset[BMG_AXIS_Z],
  948. obj->offset[BMG_AXIS_X],
  949. obj->offset[BMG_AXIS_Y],
  950. obj->offset[BMG_AXIS_Z]);
  951. len += snprintf(buf+len, PAGE_SIZE-len,
  952. "[SW ][%d] (%+3d, %+3d, %+3d)\n", 1,
  953. obj->cali_sw[BMG_AXIS_X],
  954. obj->cali_sw[BMG_AXIS_Y],
  955. obj->cali_sw[BMG_AXIS_Z]);
  956. len += snprintf(buf+len, PAGE_SIZE-len,
  957. "[ALL]unmapped(%+3d, %+3d, %+3d), mapped(%+3d, %+3d, %+3d)\n",
  958. raw[BMG_AXIS_X], raw[BMG_AXIS_Y], raw[BMG_AXIS_Z],
  959. act[BMG_AXIS_X], act[BMG_AXIS_Y], act[BMG_AXIS_Z]);
  960. return len;
  961. }
  962. }
  963. /*
  964. *unit:mapped LSB
  965. *Example:
  966. * if only force +1 LSB to android z-axis via s/w calibration,
  967. type command in terminal:
  968. * >echo 0x0 0x0 0x1 > cali
  969. * if only force -1(32 bit hex is 0xFFFFFFFF) LSB, type:
  970. * >echo 0x0 0x0 0xFFFFFFFF > cali
  971. */
  972. static ssize_t store_cali_value(struct device_driver *ddri,
  973. const char *buf, size_t count)
  974. {
  975. struct bmg_i2c_data *obj = obj_i2c_data;
  976. int err = 0;
  977. int dat[BMG_AXES_NUM];
  978. if (NULL == obj) {
  979. GYRO_ERR("bmg i2c data pointer is null\n");
  980. return 0;
  981. }
  982. if (!strncmp(buf, "rst", 3)) {
  983. err = bmg_reset_calibration(obj->client);
  984. if (err)
  985. GYRO_ERR("reset offset err = %d\n", err);
  986. } else if (BMG_AXES_NUM == sscanf(buf, "0x%02X 0x%02X 0x%02X",
  987. &dat[BMG_AXIS_X], &dat[BMG_AXIS_Y], &dat[BMG_AXIS_Z])) {
  988. err = bmg_write_calibration(obj->client, dat);
  989. if (err) {
  990. GYRO_ERR("bmg write calibration failed, err = %d\n",
  991. err);
  992. }
  993. } else {
  994. GYRO_ERR("invalid format\n");
  995. }
  996. return count;
  997. }
  998. static ssize_t show_firlen_value(struct device_driver *ddri, char *buf)
  999. {
  1000. #ifdef CONFIG_BMG_LOWPASS
  1001. struct i2c_client *client = bmg222_i2c_client;
  1002. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  1003. if (atomic_read(&obj->firlen)) {
  1004. int idx, len = atomic_read(&obj->firlen);
  1005. GYRO_LOG("len = %2d, idx = %2d\n", obj->fir.num, obj->fir.idx);
  1006. for (idx = 0; idx < len; idx++) {
  1007. GYRO_LOG("[%5d %5d %5d]\n",
  1008. obj->fir.raw[idx][BMG_AXIS_X],
  1009. obj->fir.raw[idx][BMG_AXIS_Y],
  1010. obj->fir.raw[idx][BMG_AXIS_Z]);
  1011. }
  1012. GYRO_LOG("sum = [%5d %5d %5d]\n",
  1013. obj->fir.sum[BMG_AXIS_X],
  1014. obj->fir.sum[BMG_AXIS_Y],
  1015. obj->fir.sum[BMG_AXIS_Z]);
  1016. GYRO_LOG("avg = [%5d %5d %5d]\n",
  1017. obj->fir.sum[BMG_AXIS_X]/len,
  1018. obj->fir.sum[BMG_AXIS_Y]/len,
  1019. obj->fir.sum[BMG_AXIS_Z]/len);
  1020. }
  1021. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&obj->firlen));
  1022. #else
  1023. return snprintf(buf, PAGE_SIZE, "not support\n");
  1024. #endif
  1025. }
  1026. static ssize_t store_firlen_value(struct device_driver *ddri,
  1027. const char *buf, size_t count)
  1028. {
  1029. #ifdef CONFIG_BMG_LOWPASS
  1030. struct i2c_client *client = bmg222_i2c_client;
  1031. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  1032. int firlen;
  1033. if (1 != sscanf(buf, "%d", &firlen)) {
  1034. GYRO_ERR("invallid format\n");
  1035. } else if (firlen > C_MAX_FIR_LENGTH) {
  1036. GYRO_ERR("exceeds maximum filter length\n");
  1037. } else {
  1038. atomic_set(&obj->firlen, firlen);
  1039. if (NULL == firlen) {
  1040. atomic_set(&obj->fir_en, 0);
  1041. } else {
  1042. memset(&obj->fir, 0x00, sizeof(obj->fir));
  1043. atomic_set(&obj->fir_en, 1);
  1044. }
  1045. }
  1046. #endif
  1047. return count;
  1048. }
  1049. static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
  1050. {
  1051. ssize_t res;
  1052. struct bmg_i2c_data *obj = obj_i2c_data;
  1053. if (obj == NULL) {
  1054. GYRO_ERR("bmg i2c data pointer is null\n");
  1055. return 0;
  1056. }
  1057. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
  1058. return res;
  1059. }
  1060. static ssize_t store_trace_value(struct device_driver *ddri,
  1061. const char *buf, size_t count)
  1062. {
  1063. struct bmg_i2c_data *obj = obj_i2c_data;
  1064. int trace;
  1065. if (obj == NULL) {
  1066. GYRO_ERR("bmg i2c data pointer is null\n");
  1067. return 0;
  1068. }
  1069. if (1 == sscanf(buf, "0x%x", &trace))
  1070. atomic_set(&obj->trace, trace);
  1071. else
  1072. GYRO_ERR("invalid content: '%s', length = %d\n", buf, count);
  1073. return count;
  1074. }
  1075. static ssize_t show_status_value(struct device_driver *ddri, char *buf)
  1076. {
  1077. ssize_t len = 0;
  1078. struct bmg_i2c_data *obj = obj_i2c_data;
  1079. if (obj == NULL) {
  1080. GYRO_ERR("bmg i2c data pointer is null\n");
  1081. return 0;
  1082. }
  1083. if (obj->hw)
  1084. len += snprintf(buf+len, PAGE_SIZE-len,
  1085. "CUST: %d %d (%d %d)\n",
  1086. obj->hw->i2c_num, obj->hw->direction,
  1087. obj->hw->power_id, obj->hw->power_vol);
  1088. else
  1089. len += snprintf(buf+len, PAGE_SIZE-len, "CUST: NULL\n");
  1090. len += snprintf(buf+len, PAGE_SIZE-len, "i2c addr:%#x,ver:%s\n",
  1091. obj->client->addr, BMG_DRIVER_VERSION);
  1092. return len;
  1093. }
  1094. static ssize_t show_power_mode_value(struct device_driver *ddri, char *buf)
  1095. {
  1096. ssize_t len = 0;
  1097. struct bmg_i2c_data *obj = obj_i2c_data;
  1098. if (obj == NULL) {
  1099. GYRO_ERR("bmg i2c data pointer is null\n");
  1100. return 0;
  1101. }
  1102. len += snprintf(buf+len, PAGE_SIZE-len, "%s mode\n",
  1103. obj->power_mode == BMG_NORMAL_MODE ? "normal" : "suspend");
  1104. return len;
  1105. }
  1106. static ssize_t store_power_mode_value(struct device_driver *ddri,
  1107. const char *buf, size_t count)
  1108. {
  1109. struct bmg_i2c_data *obj = obj_i2c_data;
  1110. unsigned long power_mode;
  1111. int err;
  1112. if (obj == NULL) {
  1113. GYRO_ERR("bmg i2c data pointer is null\n");
  1114. return 0;
  1115. }
  1116. err = kstrtoul(buf, 10, &power_mode);
  1117. if (err == 0) {
  1118. err = bmg_set_powermode(obj->client,
  1119. (enum BMG_POWERMODE_ENUM)(!!(power_mode)));
  1120. if (err)
  1121. return err;
  1122. return count;
  1123. }
  1124. return err;
  1125. }
  1126. static ssize_t show_range_value(struct device_driver *ddri, char *buf)
  1127. {
  1128. ssize_t len = 0;
  1129. struct bmg_i2c_data *obj = obj_i2c_data;
  1130. if (obj == NULL) {
  1131. GYRO_ERR("bmg i2c data pointer is null\n");
  1132. return 0;
  1133. }
  1134. len += snprintf(buf+len, PAGE_SIZE-len, "%d\n", obj->range);
  1135. return len;
  1136. }
  1137. static ssize_t store_range_value(struct device_driver *ddri,
  1138. const char *buf, size_t count)
  1139. {
  1140. struct bmg_i2c_data *obj = obj_i2c_data;
  1141. unsigned long range;
  1142. int err;
  1143. if (obj == NULL) {
  1144. GYRO_ERR("bmg i2c data pointer is null\n");
  1145. return 0;
  1146. }
  1147. err = kstrtoul(buf, 10, &range);
  1148. if (err == 0) {
  1149. if ((range == BMG_RANGE_2000)
  1150. || (range == BMG_RANGE_1000)
  1151. || (range == BMG_RANGE_500)
  1152. || (range == BMG_RANGE_250)
  1153. || (range == BMG_RANGE_125)) {
  1154. err = bmg_set_range(obj->client, range);
  1155. if (err)
  1156. return err;
  1157. return count;
  1158. }
  1159. }
  1160. return err;
  1161. }
  1162. static ssize_t show_datarate_value(struct device_driver *ddri, char *buf)
  1163. {
  1164. ssize_t len = 0;
  1165. struct bmg_i2c_data *obj = obj_i2c_data;
  1166. if (obj == NULL) {
  1167. GYRO_ERR("bmg i2c data pointer is null\n");
  1168. return 0;
  1169. }
  1170. len += snprintf(buf+len, PAGE_SIZE-len, "%d\n", obj->datarate);
  1171. return len;
  1172. }
  1173. static ssize_t store_datarate_value(struct device_driver *ddri,
  1174. const char *buf, size_t count)
  1175. {
  1176. struct bmg_i2c_data *obj = obj_i2c_data;
  1177. unsigned long datarate;
  1178. int err;
  1179. if (obj == NULL) {
  1180. GYRO_ERR("bmg i2c data pointer is null\n");
  1181. return 0;
  1182. }
  1183. err = kstrtoul(buf, 10, &datarate);
  1184. if (err == 0) {
  1185. if ((datarate == BMG_DATARATE_100HZ)
  1186. || (datarate == BMG_DATARATE_400HZ)) {
  1187. err = bmg_set_datarate(obj->client, datarate);
  1188. if (err)
  1189. return err;
  1190. return count;
  1191. }
  1192. }
  1193. return err;
  1194. }
  1195. /*
  1196. * return value:
  1197. * 0 --> failure
  1198. * 1 --> success
  1199. */
  1200. static ssize_t show_selftest_value(struct device_driver *ddri, char *buf)
  1201. {
  1202. ssize_t len = 0;
  1203. u8 result = 0;
  1204. struct bmg_i2c_data *obj = obj_i2c_data;
  1205. if (obj == NULL) {
  1206. GYRO_ERR("bmg i2c data pointer is null\n");
  1207. return 0;
  1208. }
  1209. bmg_selftest(obj->client, &result);
  1210. len += snprintf(buf+len, PAGE_SIZE-len, "%d\n", result);
  1211. return len;
  1212. }
  1213. static ssize_t show_fifo_mode_value(struct device_driver *ddri, char *buf)
  1214. {
  1215. ssize_t len = 0;
  1216. struct bmg_i2c_data *obj = obj_i2c_data;
  1217. u8 fifo_mode = 0;
  1218. if (obj == NULL) {
  1219. GYRO_ERR("bmg i2c data pointer is null\n");
  1220. return 0;
  1221. }
  1222. bmg_get_fifo_mode(obj->client, &fifo_mode);
  1223. len += snprintf(buf+len, PAGE_SIZE-len, "%d\n", fifo_mode);
  1224. return len;
  1225. }
  1226. static ssize_t store_fifo_mode_value(struct device_driver *ddri,
  1227. const char *buf, size_t count)
  1228. {
  1229. struct bmg_i2c_data *obj = obj_i2c_data;
  1230. unsigned long fifo_mode;
  1231. int err;
  1232. if (obj == NULL) {
  1233. GYRO_ERR("bmg i2c data pointer is null\n");
  1234. return 0;
  1235. }
  1236. err = kstrtoul(buf, 10, &fifo_mode);
  1237. if (err == 0) {
  1238. err = bmg_set_fifo_mode(obj->client, fifo_mode);
  1239. if (err)
  1240. return err;
  1241. return count;
  1242. }
  1243. return err;
  1244. }
  1245. static ssize_t show_fifo_framecount_value(struct device_driver *ddri, char *buf)
  1246. {
  1247. ssize_t len = 0;
  1248. struct bmg_i2c_data *obj = obj_i2c_data;
  1249. u8 fifo_framecount = 0;
  1250. if (obj == NULL) {
  1251. GYRO_ERR("bmg i2c data pointer is null\n");
  1252. return 0;
  1253. }
  1254. bmg_get_fifo_framecount(obj->client, &fifo_framecount);
  1255. len += snprintf(buf+len, PAGE_SIZE-len, "%d\n", fifo_framecount);
  1256. return len;
  1257. }
  1258. static ssize_t store_fifo_framecount_value(struct device_driver *ddri,
  1259. const char *buf, size_t count)
  1260. {
  1261. struct bmg_i2c_data *obj = obj_i2c_data;
  1262. unsigned long fifo_framecount;
  1263. int err;
  1264. if (obj == NULL) {
  1265. GYRO_ERR("bmg i2c data pointer is null\n");
  1266. return 0;
  1267. }
  1268. err = kstrtoul(buf, 10, &fifo_framecount);
  1269. if (err == 0) {
  1270. mutex_lock(&obj->lock);
  1271. obj->fifo_count = fifo_framecount;
  1272. mutex_unlock(&obj->lock);
  1273. return count;
  1274. }
  1275. return err;
  1276. }
  1277. static ssize_t show_fifo_data_frame_value(struct device *dev,
  1278. struct device_attribute *attr, char *buf)
  1279. {
  1280. int err = 0, i, j, len = 0;
  1281. unsigned char addr;
  1282. s16 gyro[BMG_AXES_NUM];
  1283. s16 databuf[BMG_AXES_NUM];
  1284. signed char g_fifo_data_out[MAX_FIFO_F_LEVEL * MAX_FIFO_F_BYTES] = {0};
  1285. /* select X Y Z axis data output for every fifo frame, not single axis data */
  1286. unsigned char f_len = 6;/* FIXME: ONLY USE 3-AXIS */
  1287. struct bmg_i2c_data *obj = obj_i2c_data;
  1288. if (obj->fifo_count == 0) {
  1289. return -ENOENT;
  1290. }
  1291. /*
  1292. if(bmg_i2c_read_block(obj->client, BMG160_FIFO_DATA_ADDR,
  1293. g_fifo_data_out, obj->fifo_count * f_len) < 0)
  1294. */
  1295. addr = BMG160_FIFO_DATA_ADDR;
  1296. if(i2c_dma_read(obj->client,addr,g_fifo_data_out,obj->fifo_count * f_len)<0)
  1297. {
  1298. printk("[g]fatal error\n");
  1299. return sprintf(buf, "Read byte block error\n");
  1300. }
  1301. /* please give attention to the fifo output data format*/
  1302. if (f_len == 6) {
  1303. /* Select X Y Z axis data output for every frame */
  1304. for (i = 0; i < obj->fifo_count; i++) {
  1305. databuf[BMG_AXIS_X] = ((unsigned char)g_fifo_data_out[i * f_len + 1] << 8 |
  1306. (unsigned char)g_fifo_data_out[i * f_len + 0]);
  1307. databuf[BMG_AXIS_Y] = ((unsigned char)g_fifo_data_out[i * f_len + 3] << 8 |
  1308. (unsigned char)g_fifo_data_out[i * f_len + 2]);
  1309. databuf[BMG_AXIS_Z] = ((unsigned char)g_fifo_data_out[i * f_len + 5] << 8 |
  1310. (unsigned char)g_fifo_data_out[i * f_len + 4]);
  1311. /*remap coordinate*/
  1312. gyro[obj->cvt.map[BMG_AXIS_X]] = obj->cvt.sign[BMG_AXIS_X]*databuf[BMG_AXIS_X];
  1313. gyro[obj->cvt.map[BMG_AXIS_Y]] = obj->cvt.sign[BMG_AXIS_Y]*databuf[BMG_AXIS_Y];
  1314. gyro[obj->cvt.map[BMG_AXIS_Z]] = obj->cvt.sign[BMG_AXIS_Z]*databuf[BMG_AXIS_Z];
  1315. len = sprintf(buf, "%d %d %d ", gyro[BMG_AXIS_X], gyro[BMG_AXIS_Y], gyro[BMG_AXIS_Z]);
  1316. buf += len;
  1317. err += len;
  1318. }
  1319. }
  1320. return err;
  1321. }
  1322. static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
  1323. static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
  1324. static DRIVER_ATTR(rawdata, S_IRUGO, show_rawdata_value, NULL);
  1325. static DRIVER_ATTR(cali, S_IWUSR | S_IRUGO, show_cali_value, store_cali_value);
  1326. static DRIVER_ATTR(firlen, S_IWUSR | S_IRUGO,
  1327. show_firlen_value, store_firlen_value);
  1328. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO,
  1329. show_trace_value, store_trace_value);
  1330. static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
  1331. static DRIVER_ATTR(powermode, S_IWUSR | S_IRUGO,
  1332. show_power_mode_value, store_power_mode_value);
  1333. static DRIVER_ATTR(range, S_IWUSR | S_IRUGO,
  1334. show_range_value, store_range_value);
  1335. static DRIVER_ATTR(datarate, S_IWUSR | S_IRUGO,
  1336. show_datarate_value, store_datarate_value);
  1337. static DRIVER_ATTR(selftest, S_IRUGO, show_selftest_value, NULL);
  1338. static DRIVER_ATTR(fifo_mode, S_IWUSR | S_IRUGO,
  1339. show_fifo_mode_value, store_fifo_mode_value);
  1340. static DRIVER_ATTR(fifo_framecount, S_IWUSR | S_IRUGO,
  1341. show_fifo_framecount_value, store_fifo_framecount_value);
  1342. static DRIVER_ATTR(fifo_data_frame, S_IRUGO,
  1343. show_fifo_data_frame_value, NULL);
  1344. static struct driver_attribute *bmg_attr_list[] = {
  1345. /* chip information */
  1346. &driver_attr_chipinfo,
  1347. /* dump sensor data */
  1348. &driver_attr_sensordata,
  1349. /* dump raw data */
  1350. &driver_attr_rawdata,
  1351. /* show calibration data */
  1352. &driver_attr_cali,
  1353. /* filter length: 0: disable, others: enable */
  1354. &driver_attr_firlen,
  1355. /* trace flag */
  1356. &driver_attr_trace,
  1357. /* get hw configuration */
  1358. &driver_attr_status,
  1359. /* get power mode */
  1360. &driver_attr_powermode,
  1361. /* get range */
  1362. &driver_attr_range,
  1363. /* get data rate */
  1364. &driver_attr_datarate,
  1365. /* self test */
  1366. &driver_attr_selftest,
  1367. /* fifo operation mode */
  1368. &driver_attr_fifo_mode,
  1369. /* fifo frame count */
  1370. &driver_attr_fifo_framecount,
  1371. /* get fifo content */
  1372. &driver_attr_fifo_data_frame,
  1373. };
  1374. static int bmg_create_attr(struct device_driver *driver)
  1375. {
  1376. int idx, err = 0;
  1377. int num = (int)(sizeof(bmg_attr_list)/sizeof(bmg_attr_list[0]));
  1378. if (driver == NULL)
  1379. return -EINVAL;
  1380. for (idx = 0; idx < num; idx++) {
  1381. err = driver_create_file(driver, bmg_attr_list[idx]);
  1382. if (err) {
  1383. GYRO_ERR("driver_create_file (%s) = %d\n",
  1384. bmg_attr_list[idx]->attr.name, err);
  1385. break;
  1386. }
  1387. }
  1388. return err;
  1389. }
  1390. static int bmg_delete_attr(struct device_driver *driver)
  1391. {
  1392. int idx, err = 0;
  1393. int num = (int)(sizeof(bmg_attr_list)/sizeof(bmg_attr_list[0]));
  1394. if (driver == NULL)
  1395. return -EINVAL;
  1396. for (idx = 0; idx < num; idx++)
  1397. driver_remove_file(driver, bmg_attr_list[idx]);
  1398. return err;
  1399. }
  1400. static int bmg_open(struct inode *inode, struct file *file)
  1401. {
  1402. file->private_data = obj_i2c_data;
  1403. if (file->private_data == NULL) {
  1404. GYRO_ERR("null pointer\n");
  1405. return -EINVAL;
  1406. }
  1407. return nonseekable_open(inode, file);
  1408. }
  1409. static int bmg_release(struct inode *inode, struct file *file)
  1410. {
  1411. file->private_data = NULL;
  1412. return 0;
  1413. }
  1414. static long bmg_unlocked_ioctl(struct file *file, unsigned int cmd,
  1415. unsigned long arg)
  1416. {
  1417. struct bmg_i2c_data *obj = (struct bmg_i2c_data *)file->private_data;
  1418. struct i2c_client *client = obj->client;
  1419. char strbuf[BMG_BUFSIZE] = "";
  1420. int raw_offset[BMG_BUFSIZE] = {0};
  1421. void __user *data;
  1422. struct SENSOR_DATA sensor_data;
  1423. long err = 0;
  1424. int cali[BMG_AXES_NUM];
  1425. if (obj == NULL)
  1426. return -EFAULT;
  1427. if (_IOC_DIR(cmd) & _IOC_READ)
  1428. err = !access_ok(VERIFY_WRITE,
  1429. (void __user *)arg, _IOC_SIZE(cmd));
  1430. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  1431. err = !access_ok(VERIFY_READ,
  1432. (void __user *)arg, _IOC_SIZE(cmd));
  1433. if (err) {
  1434. GYRO_ERR("access error: %08x, (%2d, %2d)\n",
  1435. cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  1436. return -EFAULT;
  1437. }
  1438. switch (cmd) {
  1439. case GYROSCOPE_IOCTL_INIT:
  1440. bmg_init_client(client, 0);
  1441. err = bmg_set_powermode(client, BMG_NORMAL_MODE);
  1442. if (err) {
  1443. err = -EFAULT;
  1444. break;
  1445. }
  1446. break;
  1447. case GYROSCOPE_IOCTL_READ_SENSORDATA:
  1448. data = (void __user *) arg;
  1449. if (data == NULL) {
  1450. err = -EINVAL;
  1451. break;
  1452. }
  1453. bmg_read_sensor_data(client, strbuf, BMG_BUFSIZE);
  1454. if (copy_to_user(data, strbuf, strlen(strbuf) + 1)) {
  1455. err = -EFAULT;
  1456. break;
  1457. }
  1458. break;
  1459. case GYROSCOPE_IOCTL_SET_CALI:
  1460. /* data unit is degree/second */
  1461. data = (void __user *)arg;
  1462. if (data == NULL) {
  1463. err = -EINVAL;
  1464. break;
  1465. }
  1466. if (copy_from_user(&sensor_data, data, sizeof(sensor_data))) {
  1467. err = -EFAULT;
  1468. break;
  1469. }
  1470. if (atomic_read(&obj->suspend)) {
  1471. GYRO_ERR("perform calibration in suspend mode\n");
  1472. err = -EINVAL;
  1473. } else {
  1474. /* convert: degree/second -> LSB */
  1475. cali[BMG_AXIS_X] = sensor_data.x * obj->sensitivity;
  1476. cali[BMG_AXIS_Y] = sensor_data.y * obj->sensitivity;
  1477. cali[BMG_AXIS_Z] = sensor_data.z * obj->sensitivity;
  1478. err = bmg_write_calibration(client, cali);
  1479. }
  1480. break;
  1481. case GYROSCOPE_IOCTL_CLR_CALI:
  1482. err = bmg_reset_calibration(client);
  1483. break;
  1484. case GYROSCOPE_IOCTL_GET_CALI:
  1485. data = (void __user *)arg;
  1486. if (data == NULL) {
  1487. err = -EINVAL;
  1488. break;
  1489. }
  1490. err = bmg_read_calibration(client, cali, raw_offset);
  1491. if (err)
  1492. break;
  1493. sensor_data.x = cali[BMG_AXIS_X] * obj->sensitivity;
  1494. sensor_data.y = cali[BMG_AXIS_Y] * obj->sensitivity;
  1495. sensor_data.z = cali[BMG_AXIS_Z] * obj->sensitivity;
  1496. if (copy_to_user(data, &sensor_data, sizeof(sensor_data))) {
  1497. err = -EFAULT;
  1498. break;
  1499. }
  1500. break;
  1501. default:
  1502. GYRO_ERR("unknown IOCTL: 0x%08x\n", cmd);
  1503. err = -ENOIOCTLCMD;
  1504. break;
  1505. }
  1506. return err;
  1507. }
  1508. static const struct file_operations bmg_fops = {
  1509. .owner = THIS_MODULE,
  1510. .open = bmg_open,
  1511. .release = bmg_release,
  1512. .unlocked_ioctl = bmg_unlocked_ioctl,
  1513. };
  1514. static struct miscdevice bmg_device = {
  1515. .minor = MISC_DYNAMIC_MINOR,
  1516. .name = "gyroscope",
  1517. .fops = &bmg_fops,
  1518. };
  1519. static int bmg_suspend(struct i2c_client *client, pm_message_t msg)
  1520. {
  1521. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  1522. int err = 0;
  1523. GYRO_FUN();
  1524. if (msg.event == PM_EVENT_SUSPEND) {
  1525. if (obj == NULL) {
  1526. GYRO_ERR("null pointer\n");
  1527. return -EINVAL;
  1528. }
  1529. atomic_set(&obj->suspend, 1);
  1530. err = bmg_set_powermode(obj->client, BMG_SUSPEND_MODE);
  1531. if (err) {
  1532. GYRO_ERR("bmg set suspend mode failed, err = %d\n",
  1533. err);
  1534. return err;
  1535. }
  1536. bmg_power(obj->hw, 0);
  1537. }
  1538. return err;
  1539. }
  1540. static int bmg_resume(struct i2c_client *client)
  1541. {
  1542. struct bmg_i2c_data *obj = i2c_get_clientdata(client);
  1543. int err;
  1544. GYRO_FUN();
  1545. if (obj == NULL) {
  1546. GYRO_ERR("null pointer\n");
  1547. return -EINVAL;
  1548. }
  1549. bmg_power(obj->hw, 1);
  1550. err = bmg_init_client(client, 0);
  1551. if (err) {
  1552. GYRO_ERR("initialize client failed, err = %d\n", err);
  1553. return err;
  1554. }
  1555. atomic_set(&obj->suspend, 0);
  1556. return 0;
  1557. }
  1558. static int bmg_i2c_detect(struct i2c_client *client,
  1559. struct i2c_board_info *info)
  1560. {
  1561. strcpy(info->type, BMG_DEV_NAME);
  1562. return 0;
  1563. }
  1564. #ifdef BMG160_SINGLE_USED
  1565. // if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL
  1566. static int bmg160_gyro_open_report_data(int open)
  1567. {
  1568. //should queuq work to report event if is_report_input_direct=true
  1569. return 0;
  1570. }
  1571. // if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL
  1572. static int bmg160_gyro_enable_nodata(int en)
  1573. {
  1574. int res =0;
  1575. int retry = 0;
  1576. bool power=false;
  1577. if(1==en)
  1578. {
  1579. power=true;
  1580. }
  1581. if(0==en)
  1582. {
  1583. power =false;
  1584. }
  1585. for(retry = 0; retry < 3; retry++){
  1586. res = bmg_set_powermode(obj_i2c_data->client, power);
  1587. if(res == 0)
  1588. {
  1589. GYRO_LOG("bmg160_gyro_SetPowerMode done\n");
  1590. break;
  1591. }
  1592. GYRO_LOG("bmg160_gyro_SetPowerMode fail\n");
  1593. }
  1594. if(res != 0)
  1595. {
  1596. GYRO_LOG("bmg160_gyro_SetPowerMode fail!\n");
  1597. return -1;
  1598. }
  1599. GYRO_LOG("bmg160_gyro_enable_nodata OK!\n");
  1600. return 0;
  1601. }
  1602. static int bmg160_gyro_set_delay(u64 ns)
  1603. {
  1604. int err;
  1605. int value = (int)ns/1000/1000 ;
  1606. /* Currently, fix data rate to 100Hz. */
  1607. int sample_delay = BMG_DATARATE_100HZ;
  1608. struct bmg_i2c_data *priv = obj_i2c_data;
  1609. GYRO_LOG("sensor delay command: %d, sample_delay = %d\n",
  1610. value, sample_delay);
  1611. err = bmg_set_datarate(priv->client, sample_delay);
  1612. if (err < 0)
  1613. GYRO_ERR("set delay parameter error\n");
  1614. if (value >= 40)
  1615. atomic_set(&priv->filter, 0);
  1616. else {
  1617. #if defined(CONFIG_BMG_LOWPASS)
  1618. priv->fir.num = 0;
  1619. priv->fir.idx = 0;
  1620. priv->fir.sum[BMG_AXIS_X] = 0;
  1621. priv->fir.sum[BMG_AXIS_Y] = 0;
  1622. priv->fir.sum[BMG_AXIS_Z] = 0;
  1623. atomic_set(&priv->filter, 1);
  1624. #endif
  1625. }
  1626. return 0;
  1627. }
  1628. static int bmg160_gyro_get_data(int* x ,int* y,int* z, int* status)
  1629. {
  1630. char buff[BMG_BUFSIZE];
  1631. bmg_read_sensor_data(obj_i2c_data->client, buff, BMG_BUFSIZE);
  1632. sscanf(buff, "%x %x %x", x, y, z);
  1633. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1634. return 0;
  1635. }
  1636. #endif
  1637. static int bmg_i2c_probe(struct i2c_client *client,
  1638. const struct i2c_device_id *id)
  1639. {
  1640. struct gyro_control_path ctl={0};
  1641. struct gyro_data_path data={0};
  1642. struct bmg_i2c_data *obj;
  1643. int err = 0;
  1644. GYRO_FUN();
  1645. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  1646. if (!obj) {
  1647. err = -ENOMEM;
  1648. goto exit;
  1649. }
  1650. obj->hw = hw;
  1651. err = hwmsen_get_convert(obj->hw->direction, &obj->cvt);
  1652. if (err) {
  1653. GYRO_ERR("invalid direction: %d\n", obj->hw->direction);
  1654. goto exit_hwmsen_get_convert_failed;
  1655. }
  1656. obj_i2c_data = obj;
  1657. obj->client = client;
  1658. i2c_set_clientdata(client, obj);
  1659. #if 0
  1660. //allocate DMA buffer
  1661. I2CDMABuf_va = (u8 *)dma_alloc_coherent(NULL, DMA_BUFFER_SIZE, &I2CDMABuf_pa, GFP_KERNEL);
  1662. if(I2CDMABuf_va == NULL)
  1663. {
  1664. err = -ENOMEM;
  1665. GYRO_ERR("Allocate DMA I2C Buffer failed! error = %d\n", err);
  1666. goto exit_dma_alloc_failed;
  1667. }
  1668. #endif
  1669. atomic_set(&obj->trace, 0);
  1670. atomic_set(&obj->suspend, 0);
  1671. obj->power_mode = BMG_UNDEFINED_POWERMODE;
  1672. obj->range = BMG_UNDEFINED_RANGE;
  1673. obj->datarate = BMG_UNDEFINED_DATARATE;
  1674. mutex_init(&obj->lock);
  1675. #ifdef CONFIG_BMG_LOWPASS
  1676. if (obj->hw->firlen > C_MAX_FIR_LENGTH)
  1677. atomic_set(&obj->firlen, C_MAX_FIR_LENGTH);
  1678. else
  1679. atomic_set(&obj->firlen, obj->hw->firlen);
  1680. if (atomic_read(&obj->firlen) > 0)
  1681. atomic_set(&obj->fir_en, 1);
  1682. #endif
  1683. err = bmg_init_client(client, 1);
  1684. if (err)
  1685. goto exit_init_client_failed;
  1686. err = misc_register(&bmg_device);
  1687. if (err) {
  1688. GYRO_ERR("misc device register failed, err = %d\n", err);
  1689. goto exit_misc_device_register_failed;
  1690. }
  1691. err = bmg_create_attr(&gyroscope_init_info.platform_diver_addr->driver);
  1692. if (err) {
  1693. GYRO_ERR("create attribute failed, err = %d\n", err);
  1694. goto exit_create_attr_failed;
  1695. }
  1696. #if !defined(BMG160_SINGLE_USED)
  1697. ctl.open_report_data= bsx_algo_gyro_open_report_data;
  1698. ctl.enable_nodata = bsx_algo_gyro_enable_nodata;
  1699. ctl.set_delay = bsx_algo_gyro_set_delay;
  1700. ctl.is_report_input_direct = false;
  1701. ctl.is_support_batch = obj->hw->is_batch_supported;
  1702. err = gyro_register_control_path(&ctl);
  1703. if(err) {
  1704. GYRO_ERR("register gyro control path err\n");
  1705. goto exit_create_attr_failed;
  1706. }
  1707. data.get_data = bsx_algo_gyro_get_data;
  1708. data.vender_div = 938;
  1709. err = gyro_register_data_path(&data);
  1710. if(err) {
  1711. GYRO_ERR("gyro_register_data_path fail = %d\n", err);
  1712. goto exit_create_attr_failed;
  1713. }
  1714. #elif defined(BMG160_SINGLE_USED)
  1715. ctl.open_report_data= bmg160_gyro_open_report_data;
  1716. ctl.enable_nodata = bmg160_gyro_enable_nodata;
  1717. ctl.set_delay = bmg160_gyro_set_delay;
  1718. ctl.is_report_input_direct = false;
  1719. ctl.is_support_batch = obj->hw->is_batch_supported;
  1720. err = gyro_register_control_path(&ctl);
  1721. if(err) {
  1722. GYRO_ERR("register gyro control path err\n");
  1723. goto exit_create_attr_failed;
  1724. }
  1725. data.get_data = bmg160_gyro_get_data;
  1726. data.vender_div = DEGREE_TO_RAD;
  1727. err = gyro_register_data_path(&data);
  1728. if(err) {
  1729. GYRO_ERR("gyro_register_data_path fail = %d\n", err);
  1730. goto exit_create_attr_failed;
  1731. }
  1732. #endif
  1733. bmg160_init_flag =1;
  1734. GYRO_LOG("%s: OK\n", __func__);
  1735. return 0;
  1736. /*
  1737. exit_hwmsen_attach_failed:
  1738. bmg_delete_attr(&bmg_gyroscope_driver.driver);
  1739. */
  1740. exit_create_attr_failed:
  1741. misc_deregister(&bmg_device);
  1742. exit_misc_device_register_failed:
  1743. exit_init_client_failed:
  1744. exit_hwmsen_get_convert_failed:
  1745. kfree(obj);
  1746. exit:
  1747. GYRO_ERR("err = %d\n", err);
  1748. return err;
  1749. }
  1750. static int bmg_i2c_remove(struct i2c_client *client)
  1751. {
  1752. int err = 0;
  1753. err = bmg_delete_attr(&gyroscope_init_info.platform_diver_addr->driver);
  1754. if (err)
  1755. GYRO_ERR("bmg_delete_attr failed, err = %d\n", err);
  1756. err = misc_deregister(&bmg_device);
  1757. if (err)
  1758. GYRO_ERR("misc_deregister failed, err = %d\n", err);
  1759. #if 0
  1760. //free DMA buffer
  1761. dma_free_coherent(NULL, DMA_BUFFER_SIZE, I2CDMABuf_va, I2CDMABuf_pa);
  1762. I2CDMABuf_va = NULL;
  1763. I2CDMABuf_pa = 0;
  1764. #endif
  1765. obj_i2c_data = NULL;
  1766. i2c_unregister_device(client);
  1767. kfree(i2c_get_clientdata(client));
  1768. return 0;
  1769. }
  1770. #ifdef CONFIG_OF
  1771. static const struct of_device_id gyro_of_match[] = {
  1772. {.compatible = "mediatek,gyro"},
  1773. {},
  1774. };
  1775. #endif
  1776. static struct i2c_driver bmg_i2c_driver = {
  1777. .driver = {
  1778. .name = BMG_DEV_NAME,
  1779. #ifdef CONFIG_OF
  1780. .of_match_table = gyro_of_match,
  1781. #endif
  1782. },
  1783. .probe = bmg_i2c_probe,
  1784. .remove = bmg_i2c_remove,
  1785. .detect = bmg_i2c_detect,
  1786. .suspend = bmg_suspend,
  1787. .resume = bmg_resume,
  1788. .id_table = bmg_i2c_id,
  1789. };
  1790. static int gyroscope_local_init(void)
  1791. {
  1792. GYRO_FUN();
  1793. bmg_power(hw, 1);
  1794. if (i2c_add_driver(&bmg_i2c_driver)) {
  1795. GYRO_ERR("add i2c driver failed\n");
  1796. return -1;
  1797. }
  1798. if(-1 == bmg160_init_flag)
  1799. {
  1800. return -1;
  1801. }
  1802. return 0;
  1803. }
  1804. static int gyroscope_remove(void)
  1805. {
  1806. GYRO_FUN();
  1807. bmg_power(hw, 0);
  1808. i2c_del_driver(&bmg_i2c_driver);
  1809. return 0;
  1810. }
  1811. static int __init bmg_init(void)
  1812. {
  1813. const char *name = "mediatek,bmg160_new";
  1814. hw = get_gyro_dts_func(name, hw);
  1815. GYRO_LOG("%s: bosch gyroscope driver version: %s\n",
  1816. __func__, BMG_DRIVER_VERSION);
  1817. i2c_register_board_info(hw->i2c_num, &bmg_i2c_info, 1);
  1818. gyro_driver_add(&gyroscope_init_info);
  1819. return 0;
  1820. }
  1821. static void __exit bmg_exit(void)
  1822. {
  1823. GYRO_FUN();
  1824. }
  1825. module_init(bmg_init);
  1826. module_exit(bmg_exit);
  1827. MODULE_LICENSE("GPLv2");
  1828. MODULE_DESCRIPTION("BMG I2C Driver");
  1829. MODULE_AUTHOR("deliang.tao@bosch-sensortec.com");
  1830. MODULE_VERSION(BMG_DRIVER_VERSION);