gyroscope.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #include "inc/gyroscope.h"
  2. struct gyro_context *gyro_context_obj = NULL;
  3. static struct platform_device *pltfm_dev;
  4. static struct gyro_init_info *gyroscope_init_list[MAX_CHOOSE_GYRO_NUM] = {0};
  5. static int64_t getCurNS(void)
  6. {
  7. int64_t ns;
  8. struct timespec time;
  9. time.tv_sec = time.tv_nsec = 0;
  10. get_monotonic_boottime(&time);
  11. ns = time.tv_sec * 1000000000LL + time.tv_nsec;
  12. return ns;
  13. }
  14. static void initTimer(struct hrtimer *timer, enum hrtimer_restart (*callback)(struct hrtimer *))
  15. {
  16. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  17. timer->function = callback;
  18. }
  19. static void startTimer(struct hrtimer *timer, int delay_ms, bool first)
  20. {
  21. struct gyro_context *obj = (struct gyro_context *)container_of(timer, struct gyro_context, hrTimer);
  22. static int count;
  23. if (obj == NULL) {
  24. GYRO_ERR("NULL pointer\n");
  25. return;
  26. }
  27. if (first) {
  28. obj->target_ktime = ktime_add_ns(ktime_get(), (int64_t)delay_ms*1000000);
  29. /* GYRO_LOG("%d, cur_nt = %lld, delay_ms = %d, target_nt = %lld\n", count,
  30. getCurNT(), delay_ms, ktime_to_us(obj->target_ktime)); */
  31. count = 0;
  32. } else {
  33. do {
  34. obj->target_ktime = ktime_add_ns(obj->target_ktime, (int64_t)delay_ms*1000000);
  35. } while (ktime_to_ns(obj->target_ktime) < ktime_to_ns(ktime_get()));
  36. /* GYRO_LOG("%d, cur_nt = %lld, delay_ms = %d, target_nt = %lld\n", count,
  37. getCurNT(), delay_ms, ktime_to_us(obj->target_ktime)); */
  38. count++;
  39. }
  40. hrtimer_start(timer, obj->target_ktime, HRTIMER_MODE_ABS);
  41. }
  42. static void stopTimer(struct hrtimer *timer)
  43. {
  44. hrtimer_cancel(timer);
  45. }
  46. static void gyro_work_func(struct work_struct *work)
  47. {
  48. struct gyro_context *cxt = NULL;
  49. int x, y, z, status;
  50. int64_t pre_ns, cur_ns;
  51. int64_t delay_ms;
  52. int err = 0;
  53. cxt = gyro_context_obj;
  54. delay_ms = atomic_read(&cxt->delay);
  55. if (NULL == cxt->gyro_data.get_data)
  56. GYRO_ERR("gyro driver not register data path\n");
  57. cur_ns = getCurNS();
  58. /* add wake lock to make sure data can be read before system suspend */
  59. cxt->gyro_data.get_data(&x, &y, &z, &status);
  60. if (err) {
  61. GYRO_ERR("get gyro data fails!!\n");
  62. goto gyro_loop;
  63. } else {
  64. cxt->drv_data.gyro_data.values[0] = x+cxt->cali_sw[0];
  65. cxt->drv_data.gyro_data.values[1] = y+cxt->cali_sw[1];
  66. cxt->drv_data.gyro_data.values[2] = z+cxt->cali_sw[2];
  67. cxt->drv_data.gyro_data.status = status;
  68. pre_ns = cxt->drv_data.gyro_data.time;
  69. cxt->drv_data.gyro_data.time = cur_ns;
  70. }
  71. if (true == cxt->is_first_data_after_enable) {
  72. pre_ns = cur_ns;
  73. cxt->is_first_data_after_enable = false;
  74. /* filter -1 value */
  75. if (GYRO_INVALID_VALUE == cxt->drv_data.gyro_data.values[0] ||
  76. GYRO_INVALID_VALUE == cxt->drv_data.gyro_data.values[1] ||
  77. GYRO_INVALID_VALUE == cxt->drv_data.gyro_data.values[2]) {
  78. GYRO_LOG(" read invalid data\n");
  79. goto gyro_loop;
  80. }
  81. }
  82. /* GYRO_LOG("gyro data[%d,%d,%d]\n" ,cxt->drv_data.gyro_data.values[0], */
  83. /* cxt->drv_data.gyro_data.values[1],cxt->drv_data.gyro_data.values[2]); */
  84. while ((cur_ns - pre_ns) >= delay_ms*1800000LL) {
  85. pre_ns += delay_ms*1000000LL;
  86. gyro_data_report(cxt->drv_data.gyro_data.values[0],
  87. cxt->drv_data.gyro_data.values[1], cxt->drv_data.gyro_data.values[2],
  88. cxt->drv_data.gyro_data.status, pre_ns);
  89. }
  90. gyro_data_report(cxt->drv_data.gyro_data.values[0],
  91. cxt->drv_data.gyro_data.values[1], cxt->drv_data.gyro_data.values[2],
  92. cxt->drv_data.gyro_data.status, cxt->drv_data.gyro_data.time);
  93. gyro_loop:
  94. if (true == cxt->is_polling_run)
  95. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), false);
  96. }
  97. enum hrtimer_restart gyro_poll(struct hrtimer *timer)
  98. {
  99. struct gyro_context *obj = (struct gyro_context *)container_of(timer, struct gyro_context, hrTimer);
  100. queue_work(obj->gyro_workqueue, &obj->report);
  101. /* GYRO_LOG("cur_nt = %lld\n", getCurNT()); */
  102. return HRTIMER_NORESTART;
  103. }
  104. static struct gyro_context *gyro_context_alloc_object(void)
  105. {
  106. struct gyro_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  107. GYRO_LOG("gyro_context_alloc_object++++\n");
  108. if (!obj) {
  109. GYRO_ERR("Alloc gyro object error!\n");
  110. return NULL;
  111. }
  112. atomic_set(&obj->delay, 200); /*5Hz, set work queue delay time 200ms */
  113. atomic_set(&obj->wake, 0);
  114. INIT_WORK(&obj->report, gyro_work_func);
  115. obj->gyro_workqueue = NULL;
  116. obj->gyro_workqueue = create_workqueue("gyro_polling");
  117. if (!obj->gyro_workqueue) {
  118. kfree(obj);
  119. return NULL;
  120. }
  121. initTimer(&obj->hrTimer, gyro_poll);
  122. obj->is_first_data_after_enable = false;
  123. obj->is_polling_run = false;
  124. obj->is_batch_enable = false;
  125. obj->cali_sw[GYRO_AXIS_X] = 0;
  126. obj->cali_sw[GYRO_AXIS_Y] = 0;
  127. obj->cali_sw[GYRO_AXIS_Z] = 0;
  128. mutex_init(&obj->gyro_op_mutex);
  129. GYRO_LOG("gyro_context_alloc_object----\n");
  130. return obj;
  131. }
  132. static int gyro_real_enable(int enable)
  133. {
  134. int err = 0;
  135. struct gyro_context *cxt = NULL;
  136. cxt = gyro_context_obj;
  137. if (1 == enable) {
  138. if (true == cxt->is_active_data || true == cxt->is_active_nodata) {
  139. err = cxt->gyro_ctl.enable_nodata(1);
  140. if (err) {
  141. err = cxt->gyro_ctl.enable_nodata(1);
  142. if (err) {
  143. err = cxt->gyro_ctl.enable_nodata(1);
  144. if (err)
  145. GYRO_ERR("gyro enable(%d) err 3 timers = %d\n", enable, err);
  146. }
  147. }
  148. GYRO_LOG("gyro real enable\n");
  149. }
  150. }
  151. if (0 == enable) {
  152. if (false == cxt->is_active_data && false == cxt->is_active_nodata) {
  153. err = cxt->gyro_ctl.enable_nodata(0);
  154. if (err)
  155. GYRO_ERR("gyro enable(%d) err = %d\n", enable, err);
  156. GYRO_LOG("gyro real disable\n");
  157. }
  158. }
  159. return err;
  160. }
  161. static int gyro_enable_data(int enable)
  162. {
  163. struct gyro_context *cxt = NULL;
  164. cxt = gyro_context_obj;
  165. if (NULL == cxt->gyro_ctl.open_report_data) {
  166. GYRO_ERR("no gyro control path\n");
  167. return -1;
  168. }
  169. if (1 == enable) {
  170. GYRO_LOG("gyro enable data\n");
  171. cxt->is_active_data = true;
  172. cxt->is_first_data_after_enable = true;
  173. cxt->gyro_ctl.open_report_data(1);
  174. gyro_real_enable(enable);
  175. if (false == cxt->is_polling_run && cxt->is_batch_enable == false) {
  176. if (false == cxt->gyro_ctl.is_report_input_direct) {
  177. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  178. cxt->is_polling_run = true;
  179. }
  180. }
  181. }
  182. if (0 == enable) {
  183. GYRO_LOG("gyro disable\n");
  184. cxt->is_active_data = false;
  185. cxt->gyro_ctl.open_report_data(0);
  186. if (true == cxt->is_polling_run) {
  187. if (false == cxt->gyro_ctl.is_report_input_direct) {
  188. cxt->is_polling_run = false;
  189. smp_mb(); /* for memory barrier */
  190. stopTimer(&cxt->hrTimer);
  191. smp_mb();/* for memory barrier */
  192. cancel_work_sync(&cxt->report);
  193. cxt->drv_data.gyro_data.values[0] = GYRO_INVALID_VALUE;
  194. cxt->drv_data.gyro_data.values[1] = GYRO_INVALID_VALUE;
  195. cxt->drv_data.gyro_data.values[2] = GYRO_INVALID_VALUE;
  196. }
  197. }
  198. gyro_real_enable(enable);
  199. }
  200. return 0;
  201. }
  202. int gyro_enable_nodata(int enable)
  203. {
  204. struct gyro_context *cxt = NULL;
  205. cxt = gyro_context_obj;
  206. if (NULL == cxt->gyro_ctl.enable_nodata) {
  207. GYRO_ERR("gyro_enable_nodata:gyro ctl path is NULL\n");
  208. return -1;
  209. }
  210. if (1 == enable)
  211. cxt->is_active_nodata = true;
  212. if (0 == enable)
  213. cxt->is_active_nodata = false;
  214. gyro_real_enable(enable);
  215. return 0;
  216. }
  217. static ssize_t gyro_show_enable_nodata(struct device *dev,
  218. struct device_attribute *attr, char *buf)
  219. {
  220. int len = 0;
  221. GYRO_LOG(" not support now\n");
  222. return len;
  223. }
  224. static ssize_t gyro_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  225. const char *buf, size_t count)
  226. {
  227. struct gyro_context *cxt = NULL;
  228. GYRO_LOG("gyro_store_enable nodata buf=%s\n", buf);
  229. mutex_lock(&gyro_context_obj->gyro_op_mutex);
  230. cxt = gyro_context_obj;
  231. if (NULL == cxt->gyro_ctl.enable_nodata) {
  232. GYRO_LOG("gyro_ctl enable nodata NULL\n");
  233. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  234. return count;
  235. }
  236. if (!strncmp(buf, "1", 1))
  237. gyro_enable_nodata(1);
  238. else if (!strncmp(buf, "0", 1))
  239. gyro_enable_nodata(0);
  240. else
  241. GYRO_ERR(" gyro_store enable nodata cmd error !!\n");
  242. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  243. return count;
  244. }
  245. static ssize_t gyro_store_active(struct device *dev, struct device_attribute *attr,
  246. const char *buf, size_t count)
  247. {
  248. struct gyro_context *cxt = NULL;
  249. GYRO_LOG("gyro_store_active buf=%s\n", buf);
  250. mutex_lock(&gyro_context_obj->gyro_op_mutex);
  251. cxt = gyro_context_obj;
  252. if (NULL == cxt->gyro_ctl.open_report_data) {
  253. GYRO_LOG("gyro_ctl enable NULL\n");
  254. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  255. return count;
  256. }
  257. if (!strncmp(buf, "1", 1))
  258. gyro_enable_data(1);
  259. else if (!strncmp(buf, "0", 1))
  260. gyro_enable_data(0);
  261. else
  262. GYRO_ERR(" gyro_store_active error !!\n");
  263. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  264. GYRO_LOG(" gyro_store_active done\n");
  265. return count;
  266. }
  267. /*----------------------------------------------------------------------------*/
  268. static ssize_t gyro_show_active(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct gyro_context *cxt = NULL;
  272. int div = 0;
  273. cxt = gyro_context_obj;
  274. GYRO_LOG("gyro show active not support now\n");
  275. div = cxt->gyro_data.vender_div;
  276. GYRO_LOG("gyro vender_div value: %d\n", div);
  277. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  278. }
  279. static ssize_t gyro_store_delay(struct device *dev, struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. int64_t delay;
  283. int64_t mdelay = 0;
  284. int ret = 0;
  285. struct gyro_context *cxt = NULL;
  286. mutex_lock(&gyro_context_obj->gyro_op_mutex);
  287. cxt = gyro_context_obj;
  288. if (NULL == cxt->gyro_ctl.set_delay) {
  289. GYRO_LOG("gyro_ctl set_delay NULL\n");
  290. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  291. return count;
  292. }
  293. ret = kstrtoll(buf, 10, &delay);
  294. if (ret != 0) {
  295. GYRO_ERR("invalid format!!\n");
  296. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  297. return count;
  298. }
  299. if (false == cxt->gyro_ctl.is_report_input_direct) {
  300. mdelay = delay;
  301. do_div(mdelay, 1000000);
  302. atomic_set(&gyro_context_obj->delay, mdelay);
  303. }
  304. cxt->gyro_ctl.set_delay(delay);
  305. GYRO_LOG(" gyro_delay %lld ns\n", delay);
  306. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  307. return count;
  308. }
  309. static ssize_t gyro_show_delay(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. int len = 0;
  313. GYRO_LOG(" not support now\n");
  314. return len;
  315. }
  316. static ssize_t gyro_store_batch(struct device *dev, struct device_attribute *attr,
  317. const char *buf, size_t count)
  318. {
  319. struct gyro_context *cxt = NULL;
  320. GYRO_LOG("gyro_store_batch buf=%s\n", buf);
  321. mutex_lock(&gyro_context_obj->gyro_op_mutex);
  322. cxt = gyro_context_obj;
  323. if (cxt->gyro_ctl.is_support_batch) {
  324. if (!strncmp(buf, "1", 1)) {
  325. cxt->is_batch_enable = true;
  326. if (true == cxt->is_polling_run) {
  327. cxt->is_polling_run = false;
  328. smp_mb(); /* for memory barrier */
  329. stopTimer(&cxt->hrTimer);
  330. smp_mb(); /* for memory barrier */
  331. cancel_work_sync(&cxt->report);
  332. cxt->drv_data.gyro_data.values[0] = GYRO_INVALID_VALUE;
  333. cxt->drv_data.gyro_data.values[1] = GYRO_INVALID_VALUE;
  334. cxt->drv_data.gyro_data.values[2] = GYRO_INVALID_VALUE;
  335. }
  336. } else if (!strncmp(buf, "0", 1)) {
  337. cxt->is_batch_enable = false;
  338. if (false == cxt->is_polling_run) {
  339. if (false == cxt->gyro_ctl.is_report_input_direct) {
  340. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  341. cxt->is_polling_run = true;
  342. }
  343. }
  344. } else
  345. GYRO_ERR(" gyro_store_batch error !!\n");
  346. } else
  347. GYRO_LOG(" gyro_store_batch not support\n");
  348. mutex_unlock(&gyro_context_obj->gyro_op_mutex);
  349. GYRO_LOG(" gyro_store_batch done: %d\n", cxt->is_batch_enable);
  350. return count;
  351. }
  352. static ssize_t gyro_show_batch(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  356. }
  357. static ssize_t gyro_store_flush(struct device *dev, struct device_attribute *attr,
  358. const char *buf, size_t count)
  359. {
  360. /* mutex_lock(&gyro_context_obj->gyro_op_mutex); */
  361. /* struct gyro_context *devobj = (struct gyro_context*)dev_get_drvdata(dev); */
  362. /* do read FIFO data function and report data immediately */
  363. /* mutex_unlock(&gyro_context_obj->gyro_op_mutex); */
  364. return count;
  365. }
  366. static ssize_t gyro_show_flush(struct device *dev,
  367. struct device_attribute *attr, char *buf)
  368. {
  369. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  370. }
  371. /* need work around again */
  372. static ssize_t gyro_show_devnum(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. unsigned int devnum;
  376. const char *devname = NULL;
  377. int ret = 0;
  378. devname = dev_name(&gyro_context_obj->idev->dev);
  379. ret = sscanf(devname+5, "%d", &devnum);
  380. return snprintf(buf, PAGE_SIZE, "%d\n", devnum);
  381. }
  382. static int gyroscope_remove(struct platform_device *pdev)
  383. {
  384. GYRO_LOG("gyroscope_remove\n");
  385. return 0;
  386. }
  387. static int gyroscope_probe(struct platform_device *pdev)
  388. {
  389. GYRO_LOG("gyroscope_probe\n");
  390. pltfm_dev = pdev;
  391. return 0;
  392. }
  393. #ifdef CONFIG_OF
  394. static const struct of_device_id gyroscope_of_match[] = {
  395. { .compatible = "mediatek,gyroscope", },
  396. {},
  397. };
  398. #endif
  399. static struct platform_driver gyroscope_driver = {
  400. .probe = gyroscope_probe,
  401. .remove = gyroscope_remove,
  402. .driver = {
  403. .name = "gyroscope",
  404. #ifdef CONFIG_OF
  405. .of_match_table = gyroscope_of_match,
  406. #endif
  407. }
  408. };
  409. static int gyro_real_driver_init(struct platform_device *pdev)
  410. {
  411. int i = 0;
  412. int err = 0;
  413. GYRO_LOG("gyro_real_driver_init +\n");
  414. for (i = 0; i < MAX_CHOOSE_GYRO_NUM; i++) {
  415. GYRO_LOG("i=%d\n", i);
  416. if (0 != gyroscope_init_list[i]) {
  417. GYRO_LOG("gyro try to init driver %s\n", gyroscope_init_list[i]->name);
  418. err = gyroscope_init_list[i]->init(pdev);
  419. if (0 == err) {
  420. GYRO_LOG("gyro real driver %s probe ok\n", gyroscope_init_list[i]->name);
  421. break;
  422. }
  423. }
  424. }
  425. if (i == MAX_CHOOSE_GYRO_NUM) {
  426. GYRO_LOG(" gyro_real_driver_init fail\n");
  427. err = -1;
  428. }
  429. return err;
  430. }
  431. int gyro_driver_add(struct gyro_init_info *obj)
  432. {
  433. int err = 0;
  434. int i = 0;
  435. if (!obj) {
  436. GYRO_ERR("gyro driver add fail, gyro_init_info is NULL\n");
  437. return -1;
  438. }
  439. for (i = 0; i < MAX_CHOOSE_GYRO_NUM; i++) {
  440. if ((i == 0) && (NULL == gyroscope_init_list[0])) {
  441. GYRO_LOG("register gyro driver for the first time\n");
  442. if (platform_driver_register(&gyroscope_driver))
  443. GYRO_ERR("failed to register gyro driver already exist\n");
  444. }
  445. if (NULL == gyroscope_init_list[i]) {
  446. obj->platform_diver_addr = &gyroscope_driver;
  447. gyroscope_init_list[i] = obj;
  448. break;
  449. }
  450. }
  451. if (i >= MAX_CHOOSE_GYRO_NUM) {
  452. GYRO_ERR("gyro driver add err\n");
  453. err = -1;
  454. }
  455. return err;
  456. }
  457. EXPORT_SYMBOL_GPL(gyro_driver_add);
  458. static int gyro_misc_init(struct gyro_context *cxt)
  459. {
  460. int err = 0;
  461. cxt->mdev.minor = MISC_DYNAMIC_MINOR;
  462. cxt->mdev.name = GYRO_MISC_DEV_NAME;
  463. err = misc_register(&cxt->mdev);
  464. if (err)
  465. GYRO_ERR("unable to register gyro misc device!!\n");
  466. return err;
  467. }
  468. static void gyro_input_destroy(struct gyro_context *cxt)
  469. {
  470. struct input_dev *dev = cxt->idev;
  471. input_unregister_device(dev);
  472. input_free_device(dev);
  473. }
  474. static int gyro_input_init(struct gyro_context *cxt)
  475. {
  476. struct input_dev *dev;
  477. int err = 0;
  478. dev = input_allocate_device();
  479. if (NULL == dev)
  480. return -ENOMEM;
  481. dev->name = GYRO_INPUTDEV_NAME;
  482. input_set_capability(dev, EV_ABS, EVENT_TYPE_GYRO_X);
  483. input_set_capability(dev, EV_ABS, EVENT_TYPE_GYRO_Y);
  484. input_set_capability(dev, EV_ABS, EVENT_TYPE_GYRO_Z);
  485. input_set_capability(dev, EV_ABS, EVENT_TYPE_GYRO_STATUS);
  486. input_set_capability(dev, EV_REL, EVENT_TYPE_GYRO_UPDATE);
  487. input_set_capability(dev, EV_REL, EVENT_TYPE_GYRO_TIMESTAMP_HI);
  488. input_set_capability(dev, EV_REL, EVENT_TYPE_GYRO_TIMESTAMP_LO);
  489. input_set_abs_params(dev, EVENT_TYPE_GYRO_X, GYRO_VALUE_MIN, GYRO_VALUE_MAX, 0, 0);
  490. input_set_abs_params(dev, EVENT_TYPE_GYRO_Y, GYRO_VALUE_MIN, GYRO_VALUE_MAX, 0, 0);
  491. input_set_abs_params(dev, EVENT_TYPE_GYRO_Z, GYRO_VALUE_MIN, GYRO_VALUE_MAX, 0, 0);
  492. input_set_abs_params(dev, EVENT_TYPE_GYRO_STATUS, GYRO_STATUS_MIN, GYRO_STATUS_MAX, 0, 0);
  493. input_set_drvdata(dev, cxt);
  494. err = input_register_device(dev);
  495. if (err < 0) {
  496. input_free_device(dev);
  497. return err;
  498. }
  499. cxt->idev = dev;
  500. return 0;
  501. }
  502. DEVICE_ATTR(gyroenablenodata, S_IWUSR | S_IRUGO, gyro_show_enable_nodata, gyro_store_enable_nodata);
  503. DEVICE_ATTR(gyroactive, S_IWUSR | S_IRUGO, gyro_show_active, gyro_store_active);
  504. DEVICE_ATTR(gyrodelay, S_IWUSR | S_IRUGO, gyro_show_delay, gyro_store_delay);
  505. DEVICE_ATTR(gyrobatch, S_IWUSR | S_IRUGO, gyro_show_batch, gyro_store_batch);
  506. DEVICE_ATTR(gyroflush, S_IWUSR | S_IRUGO, gyro_show_flush, gyro_store_flush);
  507. DEVICE_ATTR(gyrodevnum, S_IWUSR | S_IRUGO, gyro_show_devnum, NULL);
  508. static struct attribute *gyro_attributes[] = {
  509. &dev_attr_gyroenablenodata.attr,
  510. &dev_attr_gyroactive.attr,
  511. &dev_attr_gyrodelay.attr,
  512. &dev_attr_gyrobatch.attr,
  513. &dev_attr_gyroflush.attr,
  514. &dev_attr_gyrodevnum.attr,
  515. NULL
  516. };
  517. static struct attribute_group gyro_attribute_group = {
  518. .attrs = gyro_attributes
  519. };
  520. int gyro_register_data_path(struct gyro_data_path *data)
  521. {
  522. struct gyro_context *cxt = NULL;
  523. cxt = gyro_context_obj;
  524. cxt->gyro_data.get_data = data->get_data;
  525. cxt->gyro_data.vender_div = data->vender_div;
  526. cxt->gyro_data.get_raw_data = data->get_raw_data;
  527. GYRO_LOG("gyro register data path vender_div: %d\n", cxt->gyro_data.vender_div);
  528. if (NULL == cxt->gyro_data.get_data) {
  529. GYRO_LOG("gyro register data path fail\n");
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. int gyro_register_control_path(struct gyro_control_path *ctl)
  535. {
  536. struct gyro_context *cxt = NULL;
  537. int err = 0;
  538. cxt = gyro_context_obj;
  539. cxt->gyro_ctl.set_delay = ctl->set_delay;
  540. cxt->gyro_ctl.open_report_data = ctl->open_report_data;
  541. cxt->gyro_ctl.enable_nodata = ctl->enable_nodata;
  542. cxt->gyro_ctl.is_support_batch = ctl->is_support_batch;
  543. cxt->gyro_ctl.gyro_calibration = ctl->gyro_calibration;
  544. cxt->gyro_ctl.is_use_common_factory = ctl->is_use_common_factory;
  545. if (NULL == cxt->gyro_ctl.set_delay || NULL == cxt->gyro_ctl.open_report_data
  546. || NULL == cxt->gyro_ctl.enable_nodata) {
  547. GYRO_LOG("gyro register control path fail\n");
  548. return -1;
  549. }
  550. /* add misc dev for sensor hal control cmd */
  551. err = gyro_misc_init(gyro_context_obj);
  552. if (err) {
  553. GYRO_ERR("unable to register gyro misc device!!\n");
  554. return -2;
  555. }
  556. err = sysfs_create_group(&gyro_context_obj->mdev.this_device->kobj,
  557. &gyro_attribute_group);
  558. if (err < 0) {
  559. GYRO_ERR("unable to create gyro attribute file\n");
  560. return -3;
  561. }
  562. kobject_uevent(&gyro_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  563. return 0;
  564. }
  565. int x_t = 0;
  566. int y_t = 0;
  567. int z_t = 0;
  568. long pc = 0;
  569. static int check_repeat_data(int x, int y, int z)
  570. {
  571. if ((x_t == x) && (y_t == y) && (z_t == z))
  572. pc++;
  573. else
  574. pc = 0;
  575. x_t = x; y_t = y; z_t = z;
  576. if (pc > 100) {
  577. GYRO_ERR("Gyro sensor output repeat data\n");
  578. pc = 0;
  579. }
  580. return 0;
  581. }
  582. int gyro_data_report(int x, int y, int z, int status, int64_t nt)
  583. {
  584. struct gyro_context *cxt = NULL;
  585. int err = 0;
  586. check_repeat_data(x, y, z);
  587. cxt = gyro_context_obj;
  588. input_report_abs(cxt->idev, EVENT_TYPE_GYRO_X, x);
  589. input_report_abs(cxt->idev, EVENT_TYPE_GYRO_Y, y);
  590. input_report_abs(cxt->idev, EVENT_TYPE_GYRO_Z, z);
  591. input_report_abs(cxt->idev, EVENT_TYPE_GYRO_STATUS, status);
  592. input_report_rel(cxt->idev, EVENT_TYPE_GYRO_UPDATE, 1);
  593. input_report_rel(cxt->idev, EVENT_TYPE_GYRO_TIMESTAMP_HI, nt >> 32);
  594. input_report_rel(cxt->idev, EVENT_TYPE_GYRO_TIMESTAMP_LO, nt & 0xFFFFFFFFLL);
  595. input_sync(cxt->idev);
  596. return err;
  597. }
  598. static int gyro_probe(void)
  599. {
  600. int err;
  601. GYRO_LOG("+++++++++++++gyro_probe!!\n");
  602. gyro_context_obj = gyro_context_alloc_object();
  603. if (!gyro_context_obj) {
  604. err = -ENOMEM;
  605. GYRO_ERR("unable to allocate devobj!\n");
  606. goto exit_alloc_data_failed;
  607. }
  608. /* init real gyroeleration driver */
  609. err = gyro_real_driver_init(pltfm_dev);
  610. if (err) {
  611. GYRO_ERR("gyro real driver init fail\n");
  612. goto real_driver_init_fail;
  613. }
  614. err = gyro_factory_device_init();
  615. if (err)
  616. GYRO_ERR("gyro_factory_device_init fail\n");
  617. /* init input dev */
  618. err = gyro_input_init(gyro_context_obj);
  619. if (err) {
  620. GYRO_ERR("unable to register gyro input device!\n");
  621. goto exit_alloc_input_dev_failed;
  622. }
  623. GYRO_LOG("----gyro_probe OK !!\n");
  624. return 0;
  625. if (err) {
  626. GYRO_ERR("sysfs node creation error\n");
  627. gyro_input_destroy(gyro_context_obj);
  628. }
  629. real_driver_init_fail:
  630. exit_alloc_input_dev_failed:
  631. kfree(gyro_context_obj);
  632. exit_alloc_data_failed:
  633. GYRO_ERR("----gyro_probe fail !!!\n");
  634. return err;
  635. }
  636. static int gyro_remove(void)
  637. {
  638. int err = 0;
  639. input_unregister_device(gyro_context_obj->idev);
  640. sysfs_remove_group(&gyro_context_obj->idev->dev.kobj,
  641. &gyro_attribute_group);
  642. err = misc_deregister(&gyro_context_obj->mdev);
  643. if (err)
  644. GYRO_ERR("misc_deregister fail: %d\n", err);
  645. kfree(gyro_context_obj);
  646. return 0;
  647. }
  648. static int __init gyro_init(void)
  649. {
  650. GYRO_LOG("gyro_init\n");
  651. if (gyro_probe()) {
  652. GYRO_ERR("failed to register gyro driver\n");
  653. return -ENODEV;
  654. }
  655. return 0;
  656. }
  657. static void __exit gyro_exit(void)
  658. {
  659. gyro_remove();
  660. platform_driver_unregister(&gyroscope_driver);
  661. }
  662. late_initcall(gyro_init);
  663. MODULE_LICENSE("GPL");
  664. MODULE_DESCRIPTION("GYROSCOPE device driver");
  665. MODULE_AUTHOR("Mediatek");