gmrv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. #include "gmrv.h"
  2. static struct gmrv_context *gmrv_context_obj;
  3. static struct gmrv_init_info *gmrvsensor_init_list[MAX_CHOOSE_GMRV_NUM] = { 0 }; /* modified */
  4. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  5. static void gmrv_early_suspend(struct early_suspend *h);
  6. static void gmrv_late_resume(struct early_suspend *h);
  7. #endif
  8. static void gmrv_work_func(struct work_struct *work)
  9. {
  10. struct gmrv_context *cxt = NULL;
  11. int x, y, z, scalar, status;
  12. int64_t nt;
  13. struct timespec time;
  14. int err;
  15. cxt = gmrv_context_obj;
  16. if (NULL == cxt->gmrv_data.get_data)
  17. GMRV_LOG("gmrv driver not register data path\n");
  18. time.tv_sec = time.tv_nsec = 0;
  19. time = get_monotonic_coarse();
  20. nt = time.tv_sec * 1000000000LL + time.tv_nsec;
  21. err = cxt->gmrv_data.get_data(&x, &y, &z, &scalar, &status);
  22. if (err) {
  23. GMRV_ERR("get gmrv data fails!!\n");
  24. goto gmrv_loop;
  25. } else {
  26. {
  27. if (0 == x && 0 == y && 0 == z)
  28. goto gmrv_loop;
  29. cxt->drv_data.gmrv_data.values[0] = x;
  30. cxt->drv_data.gmrv_data.values[1] = y;
  31. cxt->drv_data.gmrv_data.values[2] = z;
  32. cxt->drv_data.gmrv_data.values[3] = scalar;
  33. cxt->drv_data.gmrv_data.status = status;
  34. cxt->drv_data.gmrv_data.time = nt;
  35. }
  36. }
  37. if (true == cxt->is_first_data_after_enable) {
  38. cxt->is_first_data_after_enable = false;
  39. /* filter -1 value */
  40. if (GMRV_INVALID_VALUE == cxt->drv_data.gmrv_data.values[0] ||
  41. GMRV_INVALID_VALUE == cxt->drv_data.gmrv_data.values[1] ||
  42. GMRV_INVALID_VALUE == cxt->drv_data.gmrv_data.values[2] ||
  43. GMRV_INVALID_VALUE == cxt->drv_data.gmrv_data.values[3]
  44. ) {
  45. GMRV_LOG(" read invalid data\n");
  46. goto gmrv_loop;
  47. }
  48. }
  49. /* report data to input device */
  50. /* printk("new gmrv work run....\n"); */
  51. /* GMRV_LOG("gmrv data[%d,%d,%d]\n" ,cxt->drv_data.gmrv_data.values[0], */
  52. /* cxt->drv_data.gmrv_data.values[1],cxt->drv_data.gmrv_data.values[2]); */
  53. gmrv_data_report(cxt->drv_data.gmrv_data.values[0],
  54. cxt->drv_data.gmrv_data.values[1],
  55. cxt->drv_data.gmrv_data.values[2],
  56. cxt->drv_data.gmrv_data.values[3], cxt->drv_data.gmrv_data.status);
  57. gmrv_loop:
  58. if (true == cxt->is_polling_run)
  59. mod_timer(&cxt->timer, jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
  60. }
  61. static void gmrv_poll(unsigned long data)
  62. {
  63. struct gmrv_context *obj = (struct gmrv_context *)data;
  64. if (obj != NULL)
  65. schedule_work(&obj->report);
  66. }
  67. static struct gmrv_context *gmrv_context_alloc_object(void)
  68. {
  69. struct gmrv_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  70. GMRV_LOG("gmrv_context_alloc_object++++\n");
  71. if (!obj) {
  72. GMRV_ERR("Alloc gmrv object error!\n");
  73. return NULL;
  74. }
  75. atomic_set(&obj->delay, 200); /*5Hz set work queue delay time 200ms */
  76. atomic_set(&obj->wake, 0);
  77. INIT_WORK(&obj->report, gmrv_work_func);
  78. init_timer(&obj->timer);
  79. obj->timer.expires = jiffies + atomic_read(&obj->delay) / (1000 / HZ);
  80. obj->timer.function = gmrv_poll;
  81. obj->timer.data = (unsigned long)obj;
  82. obj->is_first_data_after_enable = false;
  83. obj->is_polling_run = false;
  84. mutex_init(&obj->gmrv_op_mutex);
  85. obj->is_batch_enable = false; /* for batch mode init */
  86. GMRV_LOG("gmrv_context_alloc_object----\n");
  87. return obj;
  88. }
  89. static int gmrv_real_enable(int enable)
  90. {
  91. int err = 0;
  92. struct gmrv_context *cxt = NULL;
  93. cxt = gmrv_context_obj;
  94. if (1 == enable) {
  95. if (true == cxt->is_active_data || true == cxt->is_active_nodata) {
  96. err = cxt->gmrv_ctl.enable_nodata(1);
  97. if (err) {
  98. err = cxt->gmrv_ctl.enable_nodata(1);
  99. if (err) {
  100. err = cxt->gmrv_ctl.enable_nodata(1);
  101. if (err)
  102. GMRV_ERR("gmrv enable(%d) err 3 timers = %d\n",
  103. enable, err);
  104. }
  105. }
  106. GMRV_LOG("gmrv real enable\n");
  107. }
  108. }
  109. if (0 == enable) {
  110. if (false == cxt->is_active_data && false == cxt->is_active_nodata) {
  111. err = cxt->gmrv_ctl.enable_nodata(0);
  112. if (err)
  113. GMRV_ERR("gmrv enable(%d) err = %d\n", enable, err);
  114. GMRV_LOG("gmrv real disable\n");
  115. }
  116. }
  117. return err;
  118. }
  119. static int gmrv_enable_data(int enable)
  120. {
  121. struct gmrv_context *cxt = NULL;
  122. /* int err =0; */
  123. cxt = gmrv_context_obj;
  124. if (NULL == cxt->gmrv_ctl.open_report_data) {
  125. GMRV_ERR("no gmrv control path\n");
  126. return -1;
  127. }
  128. if (1 == enable) {
  129. GMRV_LOG("GMRV enable data\n");
  130. cxt->is_active_data = true;
  131. cxt->is_first_data_after_enable = true;
  132. cxt->gmrv_ctl.open_report_data(1);
  133. gmrv_real_enable(enable);
  134. if (false == cxt->is_polling_run && cxt->is_batch_enable == false) {
  135. if (false == cxt->gmrv_ctl.is_report_input_direct) {
  136. mod_timer(&cxt->timer,
  137. jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
  138. cxt->is_polling_run = true;
  139. }
  140. }
  141. }
  142. if (0 == enable) {
  143. GMRV_LOG("GMRV disable\n");
  144. cxt->is_active_data = false;
  145. cxt->gmrv_ctl.open_report_data(0);
  146. if (true == cxt->is_polling_run) {
  147. if (false == cxt->gmrv_ctl.is_report_input_direct) {
  148. cxt->is_polling_run = false;
  149. del_timer_sync(&cxt->timer);
  150. cancel_work_sync(&cxt->report);
  151. cxt->drv_data.gmrv_data.values[0] = GMRV_INVALID_VALUE;
  152. cxt->drv_data.gmrv_data.values[1] = GMRV_INVALID_VALUE;
  153. cxt->drv_data.gmrv_data.values[2] = GMRV_INVALID_VALUE;
  154. }
  155. }
  156. gmrv_real_enable(enable);
  157. }
  158. return 0;
  159. }
  160. int gmrv_enable_nodata(int enable)
  161. {
  162. struct gmrv_context *cxt = NULL;
  163. /* int err =0; */
  164. cxt = gmrv_context_obj;
  165. if (NULL == cxt->gmrv_ctl.enable_nodata) {
  166. GMRV_ERR("gmrv_enable_nodata:gmrv ctl path is NULL\n");
  167. return -1;
  168. }
  169. if (1 == enable)
  170. cxt->is_active_nodata = true;
  171. if (0 == enable)
  172. cxt->is_active_nodata = false;
  173. gmrv_real_enable(enable);
  174. return 0;
  175. }
  176. static ssize_t gmrv_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. int len = 0;
  179. GMRV_LOG(" not support now\n");
  180. return len;
  181. }
  182. static ssize_t gmrv_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  183. const char *buf, size_t count)
  184. {
  185. struct gmrv_context *cxt = NULL;
  186. /* int err =0; */
  187. GMRV_LOG("gmrv_store_enable nodata buf=%s\n", buf);
  188. mutex_lock(&gmrv_context_obj->gmrv_op_mutex);
  189. cxt = gmrv_context_obj;
  190. if (NULL == cxt->gmrv_ctl.enable_nodata) {
  191. GMRV_LOG("gmrv_ctl enable nodata NULL\n");
  192. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  193. return count;
  194. }
  195. if (!strncmp(buf, "1", 1)) {
  196. /* cxt->gmrv_ctl.enable_nodata(1); */
  197. gmrv_enable_nodata(1);
  198. } else if (!strncmp(buf, "0", 1)) {
  199. /* cxt->gmrv_ctl.enable_nodata(0); */
  200. gmrv_enable_nodata(0);
  201. } else {
  202. GMRV_ERR(" gmrv_store enable nodata cmd error !!\n");
  203. }
  204. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  205. return 0;
  206. }
  207. static ssize_t gmrv_store_active(struct device *dev, struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. struct gmrv_context *cxt = NULL;
  211. /* int err =0; */
  212. GMRV_LOG("gmrv_store_active buf=%s\n", buf);
  213. mutex_lock(&gmrv_context_obj->gmrv_op_mutex);
  214. cxt = gmrv_context_obj;
  215. if (NULL == cxt->gmrv_ctl.open_report_data) {
  216. GMRV_LOG("gmrv_ctl enable NULL\n");
  217. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  218. return count;
  219. }
  220. if (!strncmp(buf, "1", 1)) {
  221. /* cxt->gmrv_ctl.enable(1); */
  222. gmrv_enable_data(1);
  223. } else if (!strncmp(buf, "0", 1)) {
  224. /* cxt->gmrv_ctl.enable(0); */
  225. gmrv_enable_data(0);
  226. } else {
  227. GMRV_ERR(" gmrv_store_active error !!\n");
  228. }
  229. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  230. GMRV_LOG(" gmrv_store_active done\n");
  231. return count;
  232. }
  233. /*----------------------------------------------------------------------------*/
  234. static ssize_t gmrv_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  235. {
  236. struct gmrv_context *cxt = NULL;
  237. int div;
  238. cxt = gmrv_context_obj;
  239. div = cxt->gmrv_data.vender_div;
  240. GMRV_LOG("gmrv vender_div value: %d\n", div);
  241. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  242. }
  243. static ssize_t gmrv_store_delay(struct device *dev, struct device_attribute *attr,
  244. const char *buf, size_t count)
  245. {
  246. /* struct gmrv_context *devobj = (struct gmrv_context*)dev_get_drvdata(dev); */
  247. int delay;
  248. int mdelay = 0;
  249. struct gmrv_context *cxt = NULL;
  250. int err;
  251. mutex_lock(&gmrv_context_obj->gmrv_op_mutex);
  252. /* int err =0; */
  253. cxt = gmrv_context_obj;
  254. if (NULL == cxt->gmrv_ctl.set_delay) {
  255. GMRV_LOG("gmrv_ctl set_delay NULL\n");
  256. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  257. return count;
  258. }
  259. err = kstrtoint(buf, 10, &delay);
  260. if (err != 0) {
  261. GMRV_ERR("invalid format!!\n");
  262. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  263. return count;
  264. }
  265. if (false == cxt->gmrv_ctl.is_report_input_direct) {
  266. mdelay = (int)delay / 1000 / 1000;
  267. atomic_set(&gmrv_context_obj->delay, mdelay);
  268. }
  269. cxt->gmrv_ctl.set_delay(delay);
  270. GMRV_LOG(" gmrv_delay %d ns\n", delay);
  271. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  272. return count;
  273. }
  274. static ssize_t gmrv_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  275. {
  276. int len = 0;
  277. GMRV_LOG(" not support now\n");
  278. return len;
  279. }
  280. static ssize_t gmrv_show_sensordevnum(struct device *dev, struct device_attribute *attr, char *buf)
  281. {
  282. struct gmrv_context *cxt = NULL;
  283. char *devname = NULL;
  284. cxt = gmrv_context_obj;
  285. devname = (char *)dev_name(&cxt->idev->dev);
  286. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5);
  287. }
  288. static ssize_t gmrv_store_batch(struct device *dev, struct device_attribute *attr,
  289. const char *buf, size_t count)
  290. {
  291. struct gmrv_context *cxt = NULL;
  292. /* int err =0; */
  293. GMRV_LOG("gmrv_store_batch buf=%s\n", buf);
  294. mutex_lock(&gmrv_context_obj->gmrv_op_mutex);
  295. cxt = gmrv_context_obj;
  296. if (cxt->gmrv_ctl.is_support_batch) {
  297. if (!strncmp(buf, "1", 1)) {
  298. cxt->is_batch_enable = true;
  299. /* MTK problem fix - start */
  300. if (cxt->is_active_data && cxt->is_polling_run) {
  301. cxt->is_polling_run = false;
  302. del_timer_sync(&cxt->timer);
  303. cancel_work_sync(&cxt->report);
  304. }
  305. /* MTK problem fix - end */
  306. } else if (!strncmp(buf, "0", 1)) {
  307. cxt->is_batch_enable = false;
  308. /* MTK problem fix - start */
  309. if (cxt->is_active_data)
  310. gmrv_enable_data(true);
  311. /* MTK problem fix - end */
  312. } else {
  313. GMRV_ERR(" gmrv_store_batch error !!\n");
  314. }
  315. } else {
  316. GMRV_LOG(" gmrv_store_batch mot supported\n");
  317. }
  318. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  319. GMRV_LOG(" gmrv_store_batch done: %d\n", cxt->is_batch_enable);
  320. return count;
  321. }
  322. static ssize_t gmrv_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  323. {
  324. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  325. }
  326. static ssize_t gmrv_store_flush(struct device *dev, struct device_attribute *attr,
  327. const char *buf, size_t count)
  328. {
  329. /* struct gmrv_context *devobj = (struct gmrv_context*)dev_get_drvdata(dev); */
  330. mutex_lock(&gmrv_context_obj->gmrv_op_mutex);
  331. /* do read FIFO data function and report data immediately */
  332. mutex_unlock(&gmrv_context_obj->gmrv_op_mutex);
  333. return count;
  334. }
  335. static ssize_t gmrv_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  336. {
  337. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  338. }
  339. static int gmrvsensor_remove(struct platform_device *pdev)
  340. {
  341. GMRV_LOG("gmrvsensor_remove\n");
  342. return 0;
  343. }
  344. static int gmrvsensor_probe(struct platform_device *pdev)
  345. {
  346. GMRV_LOG("gmrvsensor_probe\n");
  347. return 0;
  348. }
  349. #ifdef CONFIG_OF
  350. static const struct of_device_id gmrvsensor_of_match[] = {
  351. {.compatible = "mediatek,gmrvsensor",},
  352. {},
  353. };
  354. #endif
  355. static struct platform_driver gmrvsensor_driver = {
  356. .probe = gmrvsensor_probe,
  357. .remove = gmrvsensor_remove,
  358. .driver = {
  359. .name = "gmrvsensor",
  360. #ifdef CONFIG_OF
  361. .of_match_table = gmrvsensor_of_match,
  362. #endif
  363. }
  364. };
  365. static int gmrv_real_driver_init(void)
  366. {
  367. int i = 0;
  368. int err = 0;
  369. GMRV_LOG(" gmrv_real_driver_init +\n");
  370. for (i = 0; i < MAX_CHOOSE_GMRV_NUM; i++) {
  371. GMRV_LOG(" i=%d\n", i);
  372. if (0 != gmrvsensor_init_list[i]) {
  373. GMRV_LOG(" gmrv try to init driver %s\n", gmrvsensor_init_list[i]->name);
  374. err = gmrvsensor_init_list[i]->init();
  375. if (0 == err) {
  376. GMRV_LOG(" gmrv real driver %s probe ok\n",
  377. gmrvsensor_init_list[i]->name);
  378. break;
  379. }
  380. }
  381. }
  382. if (i == MAX_CHOOSE_GMRV_NUM) {
  383. GMRV_LOG(" gmrv_real_driver_init fail\n");
  384. err = -1;
  385. }
  386. return err;
  387. }
  388. static int gmrv_misc_init(struct gmrv_context *cxt)
  389. {
  390. int err = 0;
  391. cxt->mdev.minor = MISC_DYNAMIC_MINOR;
  392. cxt->mdev.name = GMRV_MISC_DEV_NAME;
  393. err = misc_register(&cxt->mdev);
  394. if (err)
  395. GMRV_ERR("unable to register gmrv misc device!!\n");
  396. /* dev_set_drvdata(cxt->mdev.this_device, cxt); */
  397. return err;
  398. }
  399. static void gmrv_input_destroy(struct gmrv_context *cxt)
  400. {
  401. struct input_dev *dev = cxt->idev;
  402. input_unregister_device(dev);
  403. input_free_device(dev);
  404. }
  405. static int gmrv_input_init(struct gmrv_context *cxt)
  406. {
  407. struct input_dev *dev;
  408. int err = 0;
  409. dev = input_allocate_device();
  410. if (NULL == dev)
  411. return -ENOMEM;
  412. dev->name = GMRV_INPUTDEV_NAME;
  413. input_set_capability(dev, EV_ABS, EVENT_TYPE_GMRV_X);
  414. input_set_capability(dev, EV_ABS, EVENT_TYPE_GMRV_Y);
  415. input_set_capability(dev, EV_ABS, EVENT_TYPE_GMRV_Z);
  416. input_set_capability(dev, EV_ABS, EVENT_TYPE_GMRV_SCALAR);
  417. input_set_capability(dev, EV_REL, EVENT_TYPE_GMRV_STATUS);
  418. input_set_abs_params(dev, EVENT_TYPE_GMRV_X, GMRV_VALUE_MIN, GMRV_VALUE_MAX, 0, 0);
  419. input_set_abs_params(dev, EVENT_TYPE_GMRV_Y, GMRV_VALUE_MIN, GMRV_VALUE_MAX, 0, 0);
  420. input_set_abs_params(dev, EVENT_TYPE_GMRV_Z, GMRV_VALUE_MIN, GMRV_VALUE_MAX, 0, 0);
  421. input_set_abs_params(dev, EVENT_TYPE_GMRV_SCALAR, GMRV_VALUE_MIN, GMRV_VALUE_MAX, 0, 0);
  422. input_set_drvdata(dev, cxt);
  423. input_set_events_per_packet(dev, 32); /* test */
  424. err = input_register_device(dev);
  425. if (err < 0) {
  426. input_free_device(dev);
  427. return err;
  428. }
  429. cxt->idev = dev;
  430. return 0;
  431. }
  432. DEVICE_ATTR(gmrvenablenodata, S_IWUSR | S_IRUGO, gmrv_show_enable_nodata, gmrv_store_enable_nodata);
  433. DEVICE_ATTR(gmrvactive, S_IWUSR | S_IRUGO, gmrv_show_active, gmrv_store_active);
  434. DEVICE_ATTR(gmrvdelay, S_IWUSR | S_IRUGO, gmrv_show_delay, gmrv_store_delay);
  435. DEVICE_ATTR(gmrvbatch, S_IWUSR | S_IRUGO, gmrv_show_batch, gmrv_store_batch);
  436. DEVICE_ATTR(gmrvflush, S_IWUSR | S_IRUGO, gmrv_show_flush, gmrv_store_flush);
  437. DEVICE_ATTR(gmrvdevnum, S_IWUSR | S_IRUGO, gmrv_show_sensordevnum, NULL);
  438. static struct attribute *gmrv_attributes[] = {
  439. &dev_attr_gmrvenablenodata.attr,
  440. &dev_attr_gmrvactive.attr,
  441. &dev_attr_gmrvdelay.attr,
  442. &dev_attr_gmrvbatch.attr,
  443. &dev_attr_gmrvflush.attr,
  444. &dev_attr_gmrvdevnum.attr,
  445. NULL
  446. };
  447. static struct attribute_group gmrv_attribute_group = {
  448. .attrs = gmrv_attributes
  449. };
  450. int gmrv_register_data_path(struct gmrv_data_path *data)
  451. {
  452. struct gmrv_context *cxt = NULL;
  453. /* int err =0; */
  454. cxt = gmrv_context_obj;
  455. cxt->gmrv_data.get_data = data->get_data;
  456. cxt->gmrv_data.vender_div = data->vender_div;
  457. GMRV_LOG("gmrv register data path vender_div: %d\n", cxt->gmrv_data.vender_div);
  458. if (NULL == cxt->gmrv_data.get_data) {
  459. GMRV_LOG("gmrv register data path fail\n");
  460. return -1;
  461. }
  462. return 0;
  463. }
  464. int gmrv_register_control_path(struct gmrv_control_path *ctl)
  465. {
  466. struct gmrv_context *cxt = NULL;
  467. int err = 0;
  468. cxt = gmrv_context_obj;
  469. cxt->gmrv_ctl.set_delay = ctl->set_delay;
  470. cxt->gmrv_ctl.open_report_data = ctl->open_report_data;
  471. cxt->gmrv_ctl.enable_nodata = ctl->enable_nodata;
  472. cxt->gmrv_ctl.is_support_batch = ctl->is_support_batch;
  473. cxt->gmrv_ctl.is_report_input_direct = ctl->is_report_input_direct;
  474. if (NULL == cxt->gmrv_ctl.set_delay || NULL == cxt->gmrv_ctl.open_report_data
  475. || NULL == cxt->gmrv_ctl.enable_nodata) {
  476. GMRV_LOG("gmrv register control path fail\n");
  477. return -1;
  478. }
  479. /* add misc dev for sensor hal control cmd */
  480. err = gmrv_misc_init(gmrv_context_obj);
  481. if (err) {
  482. GMRV_ERR("unable to register gmrv misc device!!\n");
  483. return -2;
  484. }
  485. err = sysfs_create_group(&gmrv_context_obj->mdev.this_device->kobj, &gmrv_attribute_group);
  486. if (err < 0) {
  487. GMRV_ERR("unable to create gmrv attribute file\n");
  488. return -3;
  489. }
  490. kobject_uevent(&gmrv_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  491. return 0;
  492. }
  493. int gmrv_data_report(int x, int y, int z, int scalar, int status)
  494. {
  495. /* GMRV_LOG("+gmrv_data_report! %d, %d, %d, %d\n",x,y,z,status); */
  496. struct gmrv_context *cxt = NULL;
  497. cxt = gmrv_context_obj;
  498. input_report_abs(cxt->idev, EVENT_TYPE_GMRV_X, x);
  499. input_report_abs(cxt->idev, EVENT_TYPE_GMRV_Y, y);
  500. input_report_abs(cxt->idev, EVENT_TYPE_GMRV_Z, z);
  501. input_report_abs(cxt->idev, EVENT_TYPE_GMRV_SCALAR, scalar);
  502. /* input_report_rel(cxt->idev, EVENT_TYPE_GMRV_STATUS, status); */
  503. input_sync(cxt->idev);
  504. return 0;
  505. }
  506. static int gmrv_probe(struct platform_device *pdev)
  507. {
  508. int err;
  509. GMRV_LOG("+++++++++++++gmrv_probe!!\n");
  510. gmrv_context_obj = gmrv_context_alloc_object();
  511. if (!gmrv_context_obj) {
  512. err = -ENOMEM;
  513. GMRV_ERR("unable to allocate devobj!\n");
  514. goto exit_alloc_data_failed;
  515. }
  516. /* init real gmrveleration driver */
  517. err = gmrv_real_driver_init();
  518. if (err) {
  519. GMRV_ERR("gmrv real driver init fail\n");
  520. goto real_driver_init_fail;
  521. }
  522. /* init input dev */
  523. err = gmrv_input_init(gmrv_context_obj);
  524. if (err) {
  525. GMRV_ERR("unable to register gmrv input device!\n");
  526. goto exit_alloc_input_dev_failed;
  527. }
  528. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  529. atomic_set(&(gmrv_context_obj->early_suspend), 0);
  530. gmrv_context_obj->early_drv.level = 1; /* EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1, */
  531. gmrv_context_obj->early_drv.suspend = gmrv_early_suspend,
  532. gmrv_context_obj->early_drv.resume = gmrv_late_resume,
  533. register_early_suspend(&gmrv_context_obj->early_drv);
  534. #endif
  535. GMRV_LOG("----gmrv_probe OK !!\n");
  536. return 0;
  537. /* exit_hwmsen_create_attr_failed: */
  538. /* exit_misc_register_failed: */
  539. /* exit_err_sysfs: */
  540. if (err) {
  541. GMRV_ERR("sysfs node creation error\n");
  542. gmrv_input_destroy(gmrv_context_obj);
  543. }
  544. real_driver_init_fail:
  545. exit_alloc_input_dev_failed:
  546. kfree(gmrv_context_obj);
  547. exit_alloc_data_failed:
  548. GMRV_LOG("----gmrv_probe fail !!!\n");
  549. return err;
  550. }
  551. static int gmrv_remove(struct platform_device *pdev)
  552. {
  553. int err = 0;
  554. GMRV_FUN(f);
  555. input_unregister_device(gmrv_context_obj->idev);
  556. sysfs_remove_group(&gmrv_context_obj->idev->dev.kobj, &gmrv_attribute_group);
  557. err = misc_deregister(&gmrv_context_obj->mdev);
  558. if (err)
  559. GMRV_ERR("misc_deregister fail: %d\n", err);
  560. kfree(gmrv_context_obj);
  561. return 0;
  562. }
  563. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  564. static void gmrv_early_suspend(struct early_suspend *h)
  565. {
  566. atomic_set(&(gmrv_context_obj->early_suspend), 1);
  567. GMRV_LOG(" gmrv_early_suspend ok------->hwm_obj->early_suspend=%d\n",
  568. atomic_read(&(gmrv_context_obj->early_suspend)));
  569. }
  570. /*----------------------------------------------------------------------------*/
  571. static void gmrv_late_resume(struct early_suspend *h)
  572. {
  573. atomic_set(&(gmrv_context_obj->early_suspend), 0);
  574. GMRV_LOG(" gmrv_late_resume ok------->hwm_obj->early_suspend=%d\n",
  575. atomic_read(&(gmrv_context_obj->early_suspend)));
  576. }
  577. #endif
  578. static int gmrv_suspend(struct platform_device *dev, pm_message_t state)
  579. {
  580. return 0;
  581. }
  582. /*----------------------------------------------------------------------------*/
  583. static int gmrv_resume(struct platform_device *dev)
  584. {
  585. return 0;
  586. }
  587. #ifdef CONFIG_OF
  588. static const struct of_device_id m_gmrv_pl_of_match[] = {
  589. {.compatible = "mediatek,m_gmrv_pl",},
  590. {},
  591. };
  592. #endif
  593. static struct platform_driver gmrv_driver = {
  594. .probe = gmrv_probe,
  595. .remove = gmrv_remove,
  596. .suspend = gmrv_suspend,
  597. .resume = gmrv_resume,
  598. .driver = {
  599. .name = GMRV_PL_DEV_NAME,
  600. #ifdef CONFIG_OF
  601. .of_match_table = m_gmrv_pl_of_match,
  602. #endif
  603. }
  604. };
  605. int gmrv_driver_add(struct gmrv_init_info *obj)
  606. {
  607. int err = 0;
  608. int i = 0;
  609. GMRV_FUN();
  610. for (i = 0; i < MAX_CHOOSE_GMRV_NUM; i++) {
  611. if ((i == 0) && (NULL == gmrvsensor_init_list[0])) {
  612. GMRV_LOG("register gensor driver for the first time\n");
  613. if (platform_driver_register(&gmrvsensor_driver))
  614. GMRV_ERR("failed to register gensor driver already exist\n");
  615. }
  616. if (NULL == gmrvsensor_init_list[i]) {
  617. obj->platform_diver_addr = &gmrvsensor_driver;
  618. gmrvsensor_init_list[i] = obj;
  619. break;
  620. }
  621. }
  622. if (i >= MAX_CHOOSE_GMRV_NUM) {
  623. GMRV_ERR("GMRV driver add err\n");
  624. err = -1;
  625. }
  626. return err;
  627. } EXPORT_SYMBOL_GPL(gmrv_driver_add);
  628. static int __init gmrv_init(void)
  629. {
  630. GMRV_FUN();
  631. if (platform_driver_register(&gmrv_driver)) {
  632. GMRV_ERR("failed to register gmrv driver\n");
  633. return -ENODEV;
  634. }
  635. return 0;
  636. }
  637. static void __exit gmrv_exit(void)
  638. {
  639. platform_driver_unregister(&gmrv_driver);
  640. platform_driver_unregister(&gmrvsensor_driver);
  641. }
  642. late_initcall(gmrv_init);
  643. /* module_init(gmrv_init); */
  644. /* module_exit(gmrv_exit); */
  645. MODULE_LICENSE("GPL");
  646. MODULE_DESCRIPTION("GMRVCOPE device driver");
  647. MODULE_AUTHOR("Mediatek");