rotationvector.c 23 KB

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