gravity.c 20 KB

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