step_counter.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. #include "step_counter.h"
  2. static struct step_c_context *step_c_context_obj;
  3. static struct step_c_init_info *step_counter_init_list[MAX_CHOOSE_STEP_C_NUM] = { 0 }; /* modified */
  4. static void step_c_early_suspend(struct early_suspend *h);
  5. static void step_c_late_resume(struct early_suspend *h);
  6. static void step_c_work_func(struct work_struct *work)
  7. {
  8. struct step_c_context *cxt = NULL;
  9. int out_size;
  10. /* hwm_sensor_data sensor_data; */
  11. int value, status, div;
  12. int64_t nt;
  13. struct timespec time;
  14. int err, idx;
  15. cxt = step_c_context_obj;
  16. if (NULL == cxt->step_c_data.get_data)
  17. STEP_C_LOG("step_c 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. /* add wake lock to make sure data can be read before system suspend */
  22. err = cxt->step_c_data.get_data(&value, &status);
  23. if (err) {
  24. STEP_C_ERR("get step_c data fails!!\n");
  25. goto step_c_loop;
  26. } else {
  27. {
  28. cxt->drv_data.step_c_data.values[0] = value;
  29. cxt->drv_data.step_c_data.status = status;
  30. cxt->drv_data.step_c_data.time = nt;
  31. }
  32. }
  33. if (true == cxt->is_first_data_after_enable) {
  34. cxt->is_first_data_after_enable = false;
  35. /* filter -1 value */
  36. if (STEP_C_INVALID_VALUE == cxt->drv_data.step_c_data.values[0]) {
  37. STEP_C_LOG(" read invalid data\n");
  38. goto step_c_loop;
  39. }
  40. }
  41. /* report data to input device */
  42. /* printk("new step_c work run....\n"); */
  43. STEP_C_LOG("step_c data[%d]\n", cxt->drv_data.step_c_data.values[0]);
  44. step_c_data_report(cxt->idev,
  45. cxt->drv_data.step_c_data.values[0], cxt->drv_data.step_c_data.status);
  46. step_c_loop:
  47. if (true == cxt->is_polling_run)
  48. mod_timer(&cxt->timer, jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
  49. }
  50. static void step_c_poll(unsigned long data)
  51. {
  52. struct step_c_context *obj = (struct step_c_context *)data;
  53. if (obj != NULL)
  54. schedule_work(&obj->report);
  55. }
  56. static struct step_c_context *step_c_context_alloc_object(void)
  57. {
  58. struct step_c_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  59. STEP_C_LOG("step_c_context_alloc_object++++\n");
  60. if (!obj) {
  61. STEP_C_ERR("Alloc step_c object error!\n");
  62. return NULL;
  63. }
  64. atomic_set(&obj->delay, 200); /*5Hz set work queue delay time 200ms */
  65. atomic_set(&obj->wake, 0);
  66. INIT_WORK(&obj->report, step_c_work_func);
  67. init_timer(&obj->timer);
  68. obj->timer.expires = jiffies + atomic_read(&obj->delay) / (1000 / HZ);
  69. obj->timer.function = step_c_poll;
  70. obj->timer.data = (unsigned long)obj;
  71. obj->is_first_data_after_enable = false;
  72. obj->is_polling_run = false;
  73. mutex_init(&obj->step_c_op_mutex);
  74. obj->is_batch_enable = false; /* for batch mode init */
  75. STEP_C_LOG("step_c_context_alloc_object----\n");
  76. return obj;
  77. }
  78. int step_notify(STEP_NOTIFY_TYPE type)
  79. {
  80. int err = 0;
  81. int value = 0;
  82. struct step_c_context *cxt = NULL;
  83. cxt = step_c_context_obj;
  84. STEP_C_LOG("step_notify++++\n");
  85. if (type == TYPE_STEP_DETECTOR) {
  86. STEP_C_LOG("fwq TYPE_STEP_DETECTOR notify\n");
  87. /* cxt->step_c_data.get_data_step_d(&value); */
  88. /* step_c_data_report(cxt->idev,value,3); */
  89. value = 1;
  90. input_report_rel(cxt->idev, EVENT_TYPE_STEP_DETECTOR_VALUE, value);
  91. input_sync(cxt->idev);
  92. }
  93. if (type == TYPE_SIGNIFICANT) {
  94. STEP_C_LOG("fwq TYPE_SIGNIFICANT notify\n");
  95. /* cxt->step_c_data.get_data_significant(&value); */
  96. value = 1;
  97. input_report_rel(cxt->idev, EVENT_TYPE_SIGNIFICANT_VALUE, value);
  98. input_sync(cxt->idev);
  99. }
  100. return err;
  101. }
  102. static int step_d_real_enable(int enable)
  103. {
  104. int err = 0;
  105. struct step_c_context *cxt = NULL;
  106. cxt = step_c_context_obj;
  107. if (1 == enable) {
  108. err = cxt->step_c_ctl.enable_step_detect(1);
  109. if (err) {
  110. err = cxt->step_c_ctl.enable_step_detect(1);
  111. if (err) {
  112. err = cxt->step_c_ctl.enable_step_detect(1);
  113. if (err)
  114. STEP_C_ERR("step_d enable(%d) err 3 timers = %d\n", enable,
  115. err);
  116. }
  117. }
  118. STEP_C_LOG("step_d real enable\n");
  119. }
  120. if (0 == enable) {
  121. err = cxt->step_c_ctl.enable_step_detect(0);
  122. if (err)
  123. STEP_C_ERR("step_d enable(%d) err = %d\n", enable, err);
  124. STEP_C_LOG("step_d real disable\n");
  125. }
  126. return err;
  127. }
  128. static int significant_real_enable(int enable)
  129. {
  130. int err = 0;
  131. struct step_c_context *cxt = NULL;
  132. cxt = step_c_context_obj;
  133. if (1 == enable) {
  134. err = cxt->step_c_ctl.enable_significant(1);
  135. if (err) {
  136. err = cxt->step_c_ctl.enable_significant(1);
  137. if (err) {
  138. err = cxt->step_c_ctl.enable_significant(1);
  139. if (err)
  140. STEP_C_ERR
  141. ("enable_significant enable(%d) err 3 timers = %d\n",
  142. enable, err);
  143. }
  144. }
  145. STEP_C_LOG("enable_significant real enable\n");
  146. }
  147. if (0 == enable) {
  148. err = cxt->step_c_ctl.enable_significant(0);
  149. if (err)
  150. STEP_C_ERR("enable_significantenable(%d) err = %d\n", enable, err);
  151. STEP_C_LOG("enable_significant real disable\n");
  152. }
  153. return err;
  154. }
  155. static int step_c_real_enable(int enable)
  156. {
  157. int err = 0;
  158. struct step_c_context *cxt = NULL;
  159. cxt = step_c_context_obj;
  160. if (1 == enable) {
  161. if (true == cxt->is_active_data || true == cxt->is_active_nodata) {
  162. err = cxt->step_c_ctl.enable_nodata(1);
  163. if (err) {
  164. err = cxt->step_c_ctl.enable_nodata(1);
  165. if (err) {
  166. err = cxt->step_c_ctl.enable_nodata(1);
  167. if (err)
  168. STEP_C_ERR("step_c enable(%d) err 3 timers = %d\n",
  169. enable, err);
  170. }
  171. }
  172. STEP_C_LOG("step_c real enable\n");
  173. }
  174. }
  175. if (0 == enable) {
  176. if (false == cxt->is_active_data && false == cxt->is_active_nodata) {
  177. err = cxt->step_c_ctl.enable_nodata(0);
  178. if (err)
  179. STEP_C_ERR("step_c enable(%d) err = %d\n", enable, err);
  180. STEP_C_LOG("step_c real disable\n");
  181. }
  182. }
  183. return err;
  184. }
  185. static int step_c_enable_data(int enable)
  186. {
  187. struct step_c_context *cxt = NULL;
  188. int err = 0;
  189. cxt = step_c_context_obj;
  190. if (NULL == cxt->step_c_ctl.open_report_data) {
  191. STEP_C_ERR("no step_c control path\n");
  192. return -1;
  193. }
  194. if (1 == enable) {
  195. STEP_C_LOG("STEP_C enable data\n");
  196. cxt->is_active_data = true;
  197. cxt->is_first_data_after_enable = true;
  198. cxt->step_c_ctl.open_report_data(1);
  199. if (false == cxt->is_polling_run && cxt->is_batch_enable == false) {
  200. if (false == cxt->step_c_ctl.is_report_input_direct) {
  201. mod_timer(&cxt->timer,
  202. jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
  203. cxt->is_polling_run = true;
  204. }
  205. }
  206. }
  207. if (0 == enable) {
  208. STEP_C_LOG("STEP_C disable\n");
  209. cxt->is_active_data = false;
  210. cxt->step_c_ctl.open_report_data(0);
  211. if (true == cxt->is_polling_run) {
  212. if (false == cxt->step_c_ctl.is_report_input_direct) {
  213. cxt->is_polling_run = false;
  214. del_timer_sync(&cxt->timer);
  215. cancel_work_sync(&cxt->report);
  216. cxt->drv_data.step_c_data.values[0] = STEP_C_INVALID_VALUE;
  217. }
  218. }
  219. }
  220. step_c_real_enable(enable);
  221. return 0;
  222. }
  223. int step_c_enable_nodata(int enable)
  224. {
  225. struct step_c_context *cxt = NULL;
  226. int err = 0;
  227. cxt = step_c_context_obj;
  228. if (NULL == cxt->step_c_ctl.enable_nodata) {
  229. STEP_C_ERR("step_c_enable_nodata:step_c ctl path is NULL\n");
  230. return -1;
  231. }
  232. if (1 == enable)
  233. cxt->is_active_nodata = true;
  234. if (0 == enable)
  235. cxt->is_active_nodata = false;
  236. step_c_real_enable(enable);
  237. return 0;
  238. }
  239. static ssize_t step_c_show_enable_nodata(struct device *dev,
  240. struct device_attribute *attr, char *buf)
  241. {
  242. int len = 0;
  243. STEP_C_LOG(" not support now\n");
  244. return len;
  245. }
  246. static ssize_t step_c_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  247. const char *buf, size_t count)
  248. {
  249. STEP_C_LOG("step_c_store_enable nodata buf=%s\n", buf);
  250. mutex_lock(&step_c_context_obj->step_c_op_mutex);
  251. struct step_c_context *cxt = NULL;
  252. int err = 0;
  253. cxt = step_c_context_obj;
  254. if (NULL == cxt->step_c_ctl.enable_nodata) {
  255. STEP_C_LOG("step_c_ctl enable nodata NULL\n");
  256. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  257. return count;
  258. }
  259. if (!strncmp(buf, "1", 1))
  260. step_c_enable_nodata(1);
  261. else if (!strncmp(buf, "0", 1))
  262. step_c_enable_nodata(0);
  263. else
  264. STEP_C_ERR(" step_c_store enable nodata cmd error !!\n");
  265. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  266. }
  267. static ssize_t step_c_store_active(struct device *dev, struct device_attribute *attr,
  268. const char *buf, size_t count)
  269. {
  270. STEP_C_LOG("step_c_store_active buf=%s\n", buf);
  271. struct step_c_context *cxt = NULL;
  272. int res = 0;
  273. int handle = 0;
  274. int en = 0;
  275. mutex_lock(&step_c_context_obj->step_c_op_mutex);
  276. cxt = step_c_context_obj;
  277. if (NULL == cxt->step_c_ctl.open_report_data) {
  278. STEP_C_LOG("step_c_ctl enable NULL\n");
  279. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  280. return count;
  281. }
  282. res = sscanf(buf, "%d,%d", &handle, &en);
  283. if (res != 2)
  284. STEP_C_LOG(" step_store_active param error: res = %d\n", res);
  285. STEP_C_LOG(" step_store_active handle=%d ,en=%d\n", handle, en);
  286. switch (handle) {
  287. case ID_STEP_COUNTER:
  288. if (1 == en)
  289. step_c_enable_data(1);
  290. else if (0 == en)
  291. step_c_enable_data(0);
  292. else
  293. STEP_C_ERR(" step_c_store_active error !!\n");
  294. break;
  295. case ID_STEP_DETECTOR:
  296. if (1 == en)
  297. step_d_real_enable(1);
  298. else if (0 == en)
  299. step_d_real_enable(0);
  300. else
  301. STEP_C_ERR(" step_d_real_enable error !!\n");
  302. break;
  303. case ID_SIGNIFICANT_MOTION:
  304. if (1 == en)
  305. significant_real_enable(1);
  306. else if (0 == en)
  307. significant_real_enable(0);
  308. else
  309. STEP_C_ERR(" significant_real_enable error !!\n");
  310. break;
  311. }
  312. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  313. STEP_C_LOG(" step_c_store_active done\n");
  314. return count;
  315. }
  316. /*----------------------------------------------------------------------------*/
  317. static ssize_t step_c_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  318. {
  319. struct step_c_context *cxt = NULL;
  320. cxt = step_c_context_obj;
  321. int div = cxt->step_c_data.vender_div;
  322. STEP_C_LOG("step_c vender_div value: %d\n", div);
  323. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  324. }
  325. static ssize_t step_c_store_delay(struct device *dev, struct device_attribute *attr,
  326. const char *buf, size_t count)
  327. {
  328. mutex_lock(&step_c_context_obj->step_c_op_mutex);
  329. struct step_c_context *devobj = (struct step_c_context *)dev_get_drvdata(dev);
  330. int delay;
  331. int mdelay = 0;
  332. struct step_c_context *cxt = NULL;
  333. int err = 0;
  334. cxt = step_c_context_obj;
  335. if (NULL == cxt->step_c_ctl.set_delay) {
  336. STEP_C_LOG("step_c_ctl set_delay NULL\n");
  337. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  338. return count;
  339. }
  340. err = kstrtoint(buf, 10, &delay);
  341. if (err != 0) {
  342. STEP_C_ERR("invalid format!!\n");
  343. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  344. return count;
  345. }
  346. if (false == cxt->step_c_ctl.is_report_input_direct) {
  347. mdelay = (int)delay / 1000 / 1000;
  348. atomic_set(&step_c_context_obj->delay, mdelay);
  349. }
  350. cxt->step_c_ctl.set_delay(delay);
  351. STEP_C_LOG(" step_c_delay %d ns\n", delay);
  352. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  353. return count;
  354. }
  355. static ssize_t step_c_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  356. {
  357. int len = 0;
  358. STEP_C_LOG(" not support now\n");
  359. return len;
  360. }
  361. static ssize_t step_c_store_batch(struct device *dev, struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. STEP_C_LOG("step_c_store_batch buf=%s\n", buf);
  365. mutex_lock(&step_c_context_obj->step_c_op_mutex);
  366. struct step_c_context *cxt = NULL;
  367. int err = 0;
  368. cxt = step_c_context_obj;
  369. if (!strncmp(buf, "1", 1)) {
  370. cxt->is_batch_enable = true;
  371. if (true == cxt->is_polling_run) {
  372. cxt->is_polling_run = false;
  373. del_timer_sync(&cxt->timer);
  374. cancel_work_sync(&cxt->report);
  375. cxt->drv_data.step_c_data.values[0] = STEP_C_INVALID_VALUE;
  376. cxt->drv_data.step_c_data.values[1] = STEP_C_INVALID_VALUE;
  377. cxt->drv_data.step_c_data.values[2] = STEP_C_INVALID_VALUE;
  378. }
  379. } else if (!strncmp(buf, "0", 1)) {
  380. cxt->is_batch_enable = false;
  381. if (false == cxt->is_polling_run) {
  382. if (false == cxt->step_c_ctl.is_report_input_direct) {
  383. mod_timer(&cxt->timer,
  384. jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
  385. cxt->is_polling_run = true;
  386. }
  387. }
  388. } else {
  389. STEP_C_ERR(" step_c_store_batch error !!\n");
  390. }
  391. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  392. STEP_C_LOG(" step_c_store_batch done: %d\n", cxt->is_batch_enable);
  393. return count;
  394. }
  395. static ssize_t step_c_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  396. {
  397. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  398. }
  399. static ssize_t step_c_store_flush(struct device *dev, struct device_attribute *attr,
  400. const char *buf, size_t count)
  401. {
  402. mutex_lock(&step_c_context_obj->step_c_op_mutex);
  403. struct step_c_context *devobj = (struct step_c_context *)dev_get_drvdata(dev);
  404. /* do read FIFO data function and report data immediately */
  405. mutex_unlock(&step_c_context_obj->step_c_op_mutex);
  406. return count;
  407. }
  408. static ssize_t step_c_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  409. {
  410. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  411. }
  412. static ssize_t step_c_show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  413. {
  414. char *devname = NULL;
  415. devname = dev_name(&step_c_context_obj->idev->dev);
  416. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5);
  417. }
  418. static int step_counter_remove(struct platform_device *pdev)
  419. {
  420. STEP_C_LOG("step_counter_remove\n");
  421. return 0;
  422. }
  423. static int step_counter_probe(struct platform_device *pdev)
  424. {
  425. STEP_C_LOG("step_counter_probe\n");
  426. return 0;
  427. }
  428. #ifdef CONFIG_OF
  429. static const struct of_device_id step_counter_of_match[] = {
  430. {.compatible = "mediatek,step_counter",},
  431. {},
  432. };
  433. #endif
  434. static struct platform_driver step_counter_driver = {
  435. .probe = step_counter_probe,
  436. .remove = step_counter_remove,
  437. .driver = {
  438. .name = "step_counter",
  439. #ifdef CONFIG_OF
  440. .of_match_table = step_counter_of_match,
  441. #endif
  442. }
  443. };
  444. static int step_c_real_driver_init(void)
  445. {
  446. int i = 0;
  447. int err = 0;
  448. STEP_C_LOG(" step_c_real_driver_init +\n");
  449. for (i = 0; i < MAX_CHOOSE_STEP_C_NUM; i++) {
  450. STEP_C_LOG(" i=%d\n", i);
  451. if (0 != step_counter_init_list[i]) {
  452. STEP_C_LOG(" step_c try to init driver %s\n",
  453. step_counter_init_list[i]->name);
  454. err = step_counter_init_list[i]->init();
  455. if (0 == err) {
  456. STEP_C_LOG(" step_c real driver %s probe ok\n",
  457. step_counter_init_list[i]->name);
  458. break;
  459. }
  460. }
  461. }
  462. if (i == MAX_CHOOSE_STEP_C_NUM) {
  463. STEP_C_LOG(" step_c_real_driver_init fail\n");
  464. err = -1;
  465. }
  466. return err;
  467. }
  468. int step_c_driver_add(struct step_c_init_info *obj)
  469. {
  470. int err = 0;
  471. int i = 0;
  472. STEP_C_FUN();
  473. for (i = 0; i < MAX_CHOOSE_STEP_C_NUM; i++) {
  474. if (i == 0) {
  475. STEP_C_LOG("register step_counter driver for the first time\n");
  476. if (platform_driver_register(&step_counter_driver))
  477. STEP_C_ERR("failed to register gensor driver already exist\n");
  478. }
  479. if (NULL == step_counter_init_list[i]) {
  480. obj->platform_diver_addr = &step_counter_driver;
  481. step_counter_init_list[i] = obj;
  482. break;
  483. }
  484. }
  485. if (NULL == step_counter_init_list[i]) {
  486. STEP_C_ERR("STEP_C driver add err\n");
  487. err = -1;
  488. }
  489. return err;
  490. } EXPORT_SYMBOL_GPL(step_c_driver_add);
  491. static int step_c_misc_init(struct step_c_context *cxt)
  492. {
  493. int err = 0;
  494. /* kernel-3.10\include\linux\Miscdevice.h */
  495. /* use MISC_DYNAMIC_MINOR exceed 64 */
  496. cxt->mdev.minor = M_STEP_C_MISC_MINOR;
  497. cxt->mdev.name = STEP_C_MISC_DEV_NAME;
  498. err = misc_register(&cxt->mdev);
  499. if (err)
  500. STEP_C_ERR("unable to register step_c misc device!!\n");
  501. return err;
  502. }
  503. static void step_c_input_destroy(struct step_c_context *cxt)
  504. {
  505. struct input_dev *dev = cxt->idev;
  506. input_unregister_device(dev);
  507. input_free_device(dev);
  508. }
  509. static int step_c_input_init(struct step_c_context *cxt)
  510. {
  511. struct input_dev *dev;
  512. int err = 0;
  513. dev = input_allocate_device();
  514. if (NULL == dev)
  515. return -ENOMEM;
  516. dev->name = STEP_C_INPUTDEV_NAME;
  517. input_set_capability(dev, EV_REL, EVENT_TYPE_STEP_DETECTOR_VALUE);
  518. input_set_capability(dev, EV_REL, EVENT_TYPE_SIGNIFICANT_VALUE);
  519. input_set_capability(dev, EV_ABS, EVENT_TYPE_STEP_C_VALUE);
  520. input_set_capability(dev, EV_ABS, EVENT_TYPE_STEP_C_STATUS);
  521. input_set_abs_params(dev, EVENT_TYPE_STEP_C_VALUE, STEP_C_VALUE_MIN, STEP_C_VALUE_MAX, 0,
  522. 0);
  523. input_set_abs_params(dev, EVENT_TYPE_STEP_C_STATUS, STEP_C_STATUS_MIN, STEP_C_STATUS_MAX, 0,
  524. 0);
  525. input_set_drvdata(dev, cxt);
  526. set_bit(EV_REL, dev->evbit);
  527. err = input_register_device(dev);
  528. if (err < 0) {
  529. input_free_device(dev);
  530. return err;
  531. }
  532. cxt->idev = dev;
  533. return 0;
  534. }
  535. DEVICE_ATTR(step_cenablenodata, S_IWUSR | S_IRUGO, step_c_show_enable_nodata,
  536. step_c_store_enable_nodata);
  537. DEVICE_ATTR(step_cactive, S_IWUSR | S_IRUGO, step_c_show_active, step_c_store_active);
  538. DEVICE_ATTR(step_cdelay, S_IWUSR | S_IRUGO, step_c_show_delay, step_c_store_delay);
  539. DEVICE_ATTR(step_cbatch, S_IWUSR | S_IRUGO, step_c_show_batch, step_c_store_batch);
  540. DEVICE_ATTR(step_cflush, S_IWUSR | S_IRUGO, step_c_show_flush, step_c_store_flush);
  541. DEVICE_ATTR(step_cdevnum, S_IWUSR | S_IRUGO, step_c_show_devnum, NULL);
  542. static struct attribute *step_c_attributes[] = {
  543. &dev_attr_step_cenablenodata.attr,
  544. &dev_attr_step_cactive.attr,
  545. &dev_attr_step_cdelay.attr,
  546. &dev_attr_step_cbatch.attr,
  547. &dev_attr_step_cflush.attr,
  548. &dev_attr_step_cdevnum.attr,
  549. NULL
  550. };
  551. static struct attribute_group step_c_attribute_group = {
  552. .attrs = step_c_attributes
  553. };
  554. int step_c_register_data_path(struct step_c_data_path *data)
  555. {
  556. struct step_c_context *cxt = NULL;
  557. int err = 0;
  558. cxt = step_c_context_obj;
  559. cxt->step_c_data.get_data = data->get_data;
  560. cxt->step_c_data.vender_div = data->vender_div;
  561. cxt->step_c_data.get_data_significant = data->get_data_significant;
  562. cxt->step_c_data.get_data_step_d = data->get_data_step_d;
  563. STEP_C_LOG("step_c register data path vender_div: %d\n", cxt->step_c_data.vender_div);
  564. if (NULL == cxt->step_c_data.get_data
  565. || NULL == cxt->step_c_data.get_data_significant
  566. || NULL == cxt->step_c_data.get_data_step_d) {
  567. STEP_C_LOG("step_c register data path fail\n");
  568. return -1;
  569. }
  570. return 0;
  571. }
  572. int step_c_register_control_path(struct step_c_control_path *ctl)
  573. {
  574. struct step_c_context *cxt = NULL;
  575. int err = 0;
  576. cxt = step_c_context_obj;
  577. cxt->step_c_ctl.set_delay = ctl->set_delay;
  578. cxt->step_c_ctl.open_report_data = ctl->open_report_data;
  579. cxt->step_c_ctl.enable_nodata = ctl->enable_nodata;
  580. cxt->step_c_ctl.is_support_batch = ctl->is_support_batch;
  581. cxt->step_c_ctl.is_report_input_direct = ctl->is_report_input_direct;
  582. cxt->step_c_ctl.is_support_batch = ctl->is_support_batch;
  583. cxt->step_c_ctl.enable_significant = ctl->enable_significant;
  584. cxt->step_c_ctl.enable_step_detect = ctl->enable_step_detect;
  585. if (NULL == cxt->step_c_ctl.set_delay || NULL == cxt->step_c_ctl.open_report_data
  586. || NULL == cxt->step_c_ctl.enable_nodata
  587. || NULL == cxt->step_c_ctl.enable_significant
  588. || NULL == cxt->step_c_ctl.enable_step_detect) {
  589. STEP_C_LOG("step_c register control path fail\n");
  590. return -1;
  591. }
  592. /* add misc dev for sensor hal control cmd */
  593. err = step_c_misc_init(step_c_context_obj);
  594. if (err) {
  595. STEP_C_ERR("unable to register step_c misc device!!\n");
  596. return -2;
  597. }
  598. err = sysfs_create_group(&step_c_context_obj->mdev.this_device->kobj,
  599. &step_c_attribute_group);
  600. if (err < 0) {
  601. STEP_C_ERR("unable to create step_c attribute file\n");
  602. return -3;
  603. }
  604. kobject_uevent(&step_c_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  605. return 0;
  606. }
  607. int step_c_data_report(struct input_dev *dev, int value, int status)
  608. {
  609. /* STEP_C_LOG("+step_c_data_report! %d, %d, %d, %d\n",x,y,z,status); */
  610. input_report_abs(dev, EVENT_TYPE_STEP_C_VALUE, value);
  611. input_report_abs(dev, EVENT_TYPE_STEP_C_STATUS, status);
  612. input_sync(dev);
  613. }
  614. static int step_c_probe(struct platform_device *pdev)
  615. {
  616. int err;
  617. STEP_C_LOG("+++++++++++++step_c_probe!!\n");
  618. step_c_context_obj = step_c_context_alloc_object();
  619. if (!step_c_context_obj) {
  620. err = -ENOMEM;
  621. STEP_C_ERR("unable to allocate devobj!\n");
  622. goto exit_alloc_data_failed;
  623. }
  624. /* init real step_c driver */
  625. err = step_c_real_driver_init();
  626. if (err) {
  627. STEP_C_ERR("step_c real driver init fail\n");
  628. goto real_driver_init_fail;
  629. }
  630. /* init input dev */
  631. err = step_c_input_init(step_c_context_obj);
  632. if (err) {
  633. STEP_C_ERR("unable to register step_c input device!\n");
  634. goto exit_alloc_input_dev_failed;
  635. }
  636. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  637. atomic_set(&(step_c_context_obj->early_suspend), 0);
  638. step_c_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  639. step_c_context_obj->early_drv.suspend = step_c_early_suspend,
  640. step_c_context_obj->early_drv.resume = step_c_late_resume,
  641. register_early_suspend(&step_c_context_obj->early_drv);
  642. #endif
  643. STEP_C_LOG("----step_c_probe OK !!\n");
  644. return 0;
  645. exit_hwmsen_create_attr_failed:
  646. exit_misc_register_failed:
  647. exit_err_sysfs:
  648. if (err) {
  649. STEP_C_ERR("sysfs node creation error\n");
  650. step_c_input_destroy(step_c_context_obj);
  651. }
  652. real_driver_init_fail:
  653. exit_alloc_input_dev_failed:
  654. kfree(step_c_context_obj);
  655. exit_alloc_data_failed:
  656. STEP_C_LOG("----step_c_probe fail !!!\n");
  657. return err;
  658. }
  659. static int step_c_remove(struct platform_device *pdev)
  660. {
  661. int err = 0;
  662. STEP_C_FUN(f);
  663. input_unregister_device(step_c_context_obj->idev);
  664. sysfs_remove_group(&step_c_context_obj->idev->dev.kobj, &step_c_attribute_group);
  665. err = misc_deregister(&step_c_context_obj->mdev);
  666. if (err)
  667. STEP_C_ERR("misc_deregister fail: %d\n", err);
  668. kfree(step_c_context_obj);
  669. return 0;
  670. }
  671. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  672. static void step_c_early_suspend(struct early_suspend *h)
  673. {
  674. atomic_set(&(step_c_context_obj->early_suspend), 1);
  675. STEP_C_LOG(" step_c_early_suspend ok------->hwm_obj->early_suspend=%d\n",
  676. atomic_read(&(step_c_context_obj->early_suspend)));
  677. }
  678. /*----------------------------------------------------------------------------*/
  679. static void step_c_late_resume(struct early_suspend *h)
  680. {
  681. atomic_set(&(step_c_context_obj->early_suspend), 0);
  682. STEP_C_LOG(" step_c_late_resume ok------->hwm_obj->early_suspend=%d\n",
  683. atomic_read(&(step_c_context_obj->early_suspend)));
  684. }
  685. #endif
  686. static int step_c_suspend(struct platform_device *dev, pm_message_t state)
  687. {
  688. return 0;
  689. }
  690. /*----------------------------------------------------------------------------*/
  691. static int step_c_resume(struct platform_device *dev)
  692. {
  693. return 0;
  694. }
  695. #ifdef CONFIG_OF
  696. static const struct of_device_id m_step_c_pl_of_match[] = {
  697. {.compatible = "mediatek,m_step_c_pl",},
  698. {},
  699. };
  700. #endif
  701. static struct platform_driver step_c_driver = {
  702. .probe = step_c_probe,
  703. .remove = step_c_remove,
  704. .suspend = step_c_suspend,
  705. .resume = step_c_resume,
  706. .driver = {
  707. .name = STEP_C_PL_DEV_NAME,
  708. #ifdef CONFIG_OF
  709. .of_match_table = m_step_c_pl_of_match,
  710. #endif
  711. }
  712. };
  713. static int __init step_c_init(void)
  714. {
  715. STEP_C_FUN();
  716. if (platform_driver_register(&step_c_driver)) {
  717. STEP_C_ERR("failed to register step_c driver\n");
  718. return -ENODEV;
  719. }
  720. return 0;
  721. }
  722. static void __exit step_c_exit(void)
  723. {
  724. platform_driver_unregister(&step_c_driver);
  725. platform_driver_unregister(&step_counter_driver);
  726. }
  727. late_initcall(step_c_init);
  728. /* module_init(step_c_init); */
  729. /* module_exit(step_c_exit); */
  730. MODULE_LICENSE("GPL");
  731. MODULE_DESCRIPTION("STEP_CMETER device driver");
  732. MODULE_AUTHOR("Mediatek");