barometer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. #include "inc/barometer.h"
  2. struct baro_context *baro_context_obj = NULL;
  3. static void initTimer(struct hrtimer *timer, enum hrtimer_restart (*callback)(struct hrtimer *))
  4. {
  5. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  6. timer->function = callback;
  7. }
  8. static void startTimer(struct hrtimer *timer, int delay_ms, bool first)
  9. {
  10. struct baro_context *obj = (struct baro_context *)container_of(timer, struct baro_context, hrTimer);
  11. if (obj == NULL) {
  12. BARO_ERR("NULL pointer\n");
  13. return;
  14. }
  15. if (first) {
  16. obj->target_ktime = ktime_add_ns(ktime_get(), (int64_t)delay_ms*1000000);
  17. /* BARO_ERR("cur_ns = %lld, first_target_ns = %lld\n",
  18. ktime_to_ns(ktime_get()), ktime_to_ns(obj->target_ktime)); */
  19. } else {
  20. do {
  21. obj->target_ktime = ktime_add_ns(obj->target_ktime, (int64_t)delay_ms*1000000);
  22. } while (ktime_to_ns(obj->target_ktime) < ktime_to_ns(ktime_get()));
  23. /* BARO_ERR("cur_ns = %lld, target_ns = %lld\n", ktime_to_ns(ktime_get()),
  24. ktime_to_ns(obj->target_ktime)); */
  25. }
  26. hrtimer_start(timer, obj->target_ktime, HRTIMER_MODE_ABS);
  27. }
  28. static void stopTimer(struct hrtimer *timer)
  29. {
  30. hrtimer_cancel(timer);
  31. }
  32. static struct baro_init_info *barometer_init_list[MAX_CHOOSE_BARO_NUM] = { 0 };
  33. static void baro_work_func(struct work_struct *work)
  34. {
  35. struct baro_context *cxt = NULL;
  36. /* hwm_sensor_data sensor_data; */
  37. int value, status;
  38. int64_t pre_ns, cur_ns;
  39. int64_t delay_ms;
  40. struct timespec time;
  41. int err;
  42. cxt = baro_context_obj;
  43. delay_ms = atomic_read(&cxt->delay);
  44. if (NULL == cxt->baro_data.get_data)
  45. BARO_LOG("baro driver not register data path\n");
  46. time.tv_sec = time.tv_nsec = 0;
  47. get_monotonic_boottime(&time);
  48. cur_ns = time.tv_sec*1000000000LL+time.tv_nsec;
  49. /* add wake lock to make sure data can be read before system suspend */
  50. err = cxt->baro_data.get_data(&value, &status);
  51. if (err) {
  52. BARO_ERR("get baro data fails!!\n");
  53. goto baro_loop;
  54. } else {
  55. {
  56. cxt->drv_data.baro_data.values[0] = value;
  57. cxt->drv_data.baro_data.status = status;
  58. pre_ns = cxt->drv_data.baro_data.time;
  59. cxt->drv_data.baro_data.time = cur_ns;
  60. }
  61. }
  62. if (true == cxt->is_first_data_after_enable) {
  63. pre_ns = cur_ns;
  64. cxt->is_first_data_after_enable = false;
  65. /* filter -1 value */
  66. if (BARO_INVALID_VALUE == cxt->drv_data.baro_data.values[0]) {
  67. BARO_LOG(" read invalid data\n");
  68. goto baro_loop;
  69. }
  70. }
  71. /* report data to input device */
  72. /*BARO_LOG("new baro work run....\n"); */
  73. /*BARO_LOG("baro data[%d].\n", cxt->drv_data.baro_data.values[0]); */
  74. while ((cur_ns - pre_ns) >= delay_ms*1800000LL) {
  75. pre_ns += delay_ms*1000000LL;
  76. baro_data_report(cxt->idev,
  77. cxt->drv_data.baro_data.values[0],
  78. cxt->drv_data.baro_data.status, pre_ns);
  79. }
  80. baro_data_report(cxt->idev,
  81. cxt->drv_data.baro_data.values[0],
  82. cxt->drv_data.baro_data.status, cxt->drv_data.baro_data.time);
  83. baro_loop:
  84. if (true == cxt->is_polling_run) {
  85. {
  86. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), false);
  87. }
  88. }
  89. }
  90. enum hrtimer_restart baro_poll(struct hrtimer *timer)
  91. {
  92. struct baro_context *obj = (struct baro_context *)container_of(timer, struct baro_context, hrTimer);
  93. queue_work(obj->baro_workqueue, &obj->report);
  94. return HRTIMER_NORESTART;
  95. }
  96. static struct baro_context *baro_context_alloc_object(void)
  97. {
  98. struct baro_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  99. BARO_LOG("baro_context_alloc_object++++\n");
  100. if (!obj) {
  101. BARO_ERR("Alloc baro object error!\n");
  102. return NULL;
  103. }
  104. atomic_set(&obj->delay, 200); /*5Hz set work queue delay time 200 ms */
  105. atomic_set(&obj->wake, 0);
  106. INIT_WORK(&obj->report, baro_work_func);
  107. obj->baro_workqueue = NULL;
  108. obj->baro_workqueue = create_workqueue("baro_polling");
  109. if (!obj->baro_workqueue) {
  110. kfree(obj);
  111. return NULL;
  112. }
  113. initTimer(&obj->hrTimer, baro_poll);
  114. obj->is_first_data_after_enable = false;
  115. obj->is_polling_run = false;
  116. mutex_init(&obj->baro_op_mutex);
  117. obj->is_batch_enable = false; /* for batch mode init */
  118. BARO_LOG("baro_context_alloc_object----\n");
  119. return obj;
  120. }
  121. static int baro_real_enable(int enable)
  122. {
  123. int err = 0;
  124. struct baro_context *cxt = NULL;
  125. cxt = baro_context_obj;
  126. if (1 == enable) {
  127. if (true == cxt->is_active_data || true == cxt->is_active_nodata) {
  128. err = cxt->baro_ctl.enable_nodata(1);
  129. if (err) {
  130. err = cxt->baro_ctl.enable_nodata(1);
  131. if (err) {
  132. err = cxt->baro_ctl.enable_nodata(1);
  133. if (err)
  134. BARO_ERR("baro enable(%d) err 3 timers = %d\n",
  135. enable, err);
  136. }
  137. }
  138. BARO_LOG("baro real enable\n");
  139. }
  140. }
  141. if (0 == enable) {
  142. if (false == cxt->is_active_data && false == cxt->is_active_nodata) {
  143. err = cxt->baro_ctl.enable_nodata(0);
  144. if (err)
  145. BARO_ERR("baro enable(%d) err = %d\n", enable, err);
  146. BARO_LOG("baro real disable\n");
  147. }
  148. }
  149. return err;
  150. }
  151. static int baro_enable_data(int enable)
  152. {
  153. struct baro_context *cxt = NULL;
  154. cxt = baro_context_obj;
  155. if (NULL == cxt->baro_ctl.open_report_data) {
  156. BARO_ERR("no baro control path\n");
  157. return -1;
  158. }
  159. if (1 == enable) {
  160. BARO_LOG("BARO enable data\n");
  161. cxt->is_active_data = true;
  162. cxt->is_first_data_after_enable = true;
  163. cxt->baro_ctl.open_report_data(1);
  164. baro_real_enable(enable);
  165. if (false == cxt->is_polling_run && cxt->is_batch_enable == false) {
  166. if (false == cxt->baro_ctl.is_report_input_direct) {
  167. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  168. cxt->is_polling_run = true;
  169. }
  170. }
  171. }
  172. if (0 == enable) {
  173. BARO_LOG("BARO disable\n");
  174. cxt->is_active_data = false;
  175. cxt->baro_ctl.open_report_data(0);
  176. if (true == cxt->is_polling_run) {
  177. if (false == cxt->baro_ctl.is_report_input_direct) {
  178. cxt->is_polling_run = false;
  179. smp_mb(); /* for memory barrier */
  180. stopTimer(&cxt->hrTimer);
  181. smp_mb(); /* for memory barrier */
  182. cancel_work_sync(&cxt->report);
  183. cxt->drv_data.baro_data.values[0] = BARO_INVALID_VALUE;
  184. }
  185. }
  186. baro_real_enable(enable);
  187. }
  188. return 0;
  189. }
  190. int baro_enable_nodata(int enable)
  191. {
  192. struct baro_context *cxt = NULL;
  193. cxt = baro_context_obj;
  194. if (NULL == cxt->baro_ctl.enable_nodata) {
  195. BARO_ERR("baro_enable_nodata:baro ctl path is NULL\n");
  196. return -1;
  197. }
  198. if (1 == enable)
  199. cxt->is_active_nodata = true;
  200. if (0 == enable)
  201. cxt->is_active_nodata = false;
  202. baro_real_enable(enable);
  203. return 0;
  204. }
  205. static ssize_t baro_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
  206. {
  207. int len = 0;
  208. BARO_LOG(" not support now\n");
  209. return len;
  210. }
  211. static ssize_t baro_store_enable_nodata(struct device *dev, struct device_attribute *attr,
  212. const char *buf, size_t count)
  213. {
  214. struct baro_context *cxt = NULL;
  215. BARO_LOG("baro_store_enable nodata buf=%s\n", buf);
  216. mutex_lock(&baro_context_obj->baro_op_mutex);
  217. cxt = baro_context_obj;
  218. if (NULL == cxt->baro_ctl.enable_nodata) {
  219. BARO_LOG("baro_ctl enable nodata NULL\n");
  220. mutex_unlock(&baro_context_obj->baro_op_mutex);
  221. return count;
  222. }
  223. if (!strncmp(buf, "1", 1))
  224. baro_enable_nodata(1);
  225. else if (!strncmp(buf, "0", 1))
  226. baro_enable_nodata(0);
  227. else
  228. BARO_ERR(" baro_store enable nodata cmd error !!\n");
  229. mutex_unlock(&baro_context_obj->baro_op_mutex);
  230. return count;
  231. }
  232. static ssize_t baro_store_active(struct device *dev, struct device_attribute *attr,
  233. const char *buf, size_t count)
  234. {
  235. struct baro_context *cxt = NULL;
  236. BARO_LOG("baro_store_active buf=%s\n", buf);
  237. mutex_lock(&baro_context_obj->baro_op_mutex);
  238. cxt = baro_context_obj;
  239. if (NULL == cxt->baro_ctl.open_report_data) {
  240. BARO_LOG("baro_ctl enable NULL\n");
  241. mutex_unlock(&baro_context_obj->baro_op_mutex);
  242. return count;
  243. }
  244. if (!strncmp(buf, "1", 1))
  245. baro_enable_data(1);
  246. else if (!strncmp(buf, "0", 1))
  247. baro_enable_data(0);
  248. else
  249. BARO_ERR(" baro_store_active error !!\n");
  250. mutex_unlock(&baro_context_obj->baro_op_mutex);
  251. BARO_LOG(" baro_store_active done\n");
  252. return count;
  253. }
  254. /*----------------------------------------------------------------------------*/
  255. static ssize_t baro_show_active(struct device *dev, struct device_attribute *attr, char *buf)
  256. {
  257. struct baro_context *cxt = NULL;
  258. int div;
  259. cxt = baro_context_obj;
  260. div = cxt->baro_data.vender_div;
  261. BARO_LOG("baro vender_div value: %d\n", div);
  262. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  263. }
  264. static ssize_t baro_store_delay(struct device *dev, struct device_attribute *attr,
  265. const char *buf, size_t count)
  266. {
  267. int64_t delay;
  268. int64_t mdelay = 0;
  269. int ret = 0;
  270. struct baro_context *cxt = NULL;
  271. mutex_lock(&baro_context_obj->baro_op_mutex);
  272. cxt = baro_context_obj;
  273. if (NULL == cxt->baro_ctl.set_delay) {
  274. BARO_LOG("baro_ctl set_delay NULL\n");
  275. mutex_unlock(&baro_context_obj->baro_op_mutex);
  276. return count;
  277. }
  278. ret = kstrtoll(buf, 10, &delay);
  279. if (ret != 0) {
  280. BARO_ERR("invalid format!!\n");
  281. mutex_unlock(&baro_context_obj->baro_op_mutex);
  282. return count;
  283. }
  284. if (false == cxt->baro_ctl.is_report_input_direct) {
  285. mdelay = delay;
  286. do_div(mdelay, 1000000);
  287. atomic_set(&baro_context_obj->delay, mdelay);
  288. }
  289. cxt->baro_ctl.set_delay(delay);
  290. BARO_LOG(" baro_delay %lld ns\n", delay);
  291. mutex_unlock(&baro_context_obj->baro_op_mutex);
  292. return count;
  293. }
  294. static ssize_t baro_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  295. {
  296. int len = 0;
  297. BARO_LOG(" not support now\n");
  298. return len;
  299. }
  300. static ssize_t baro_store_batch(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. struct baro_context *cxt = NULL;
  304. BARO_LOG("baro_store_batch buf=%s\n", buf);
  305. mutex_lock(&baro_context_obj->baro_op_mutex);
  306. cxt = baro_context_obj;
  307. if (!strncmp(buf, "1", 1)) {
  308. cxt->is_batch_enable = true;
  309. if (true == cxt->is_polling_run) {
  310. cxt->is_polling_run = false;
  311. smp_mb(); /* for memory barrier */
  312. stopTimer(&cxt->hrTimer);
  313. smp_mb(); /* for memory barrier */
  314. cancel_work_sync(&cxt->report);
  315. cxt->drv_data.baro_data.values[0] = BARO_INVALID_VALUE;
  316. cxt->drv_data.baro_data.values[1] = BARO_INVALID_VALUE;
  317. cxt->drv_data.baro_data.values[2] = BARO_INVALID_VALUE;
  318. }
  319. } else if (!strncmp(buf, "0", 1)) {
  320. cxt->is_batch_enable = false;
  321. if (false == cxt->is_polling_run) {
  322. if (false == cxt->baro_ctl.is_report_input_direct && true == cxt->is_active_data) {
  323. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  324. cxt->is_polling_run = true;
  325. }
  326. }
  327. } else {
  328. BARO_ERR(" baro_store_batch error !!\n");
  329. }
  330. mutex_unlock(&baro_context_obj->baro_op_mutex);
  331. BARO_LOG(" baro_store_batch done: %d\n", cxt->is_batch_enable);
  332. return count;
  333. }
  334. static ssize_t baro_show_batch(struct device *dev, struct device_attribute *attr, char *buf)
  335. {
  336. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  337. }
  338. static ssize_t baro_store_flush(struct device *dev, struct device_attribute *attr,
  339. const char *buf, size_t count)
  340. {
  341. mutex_lock(&baro_context_obj->baro_op_mutex);
  342. /* struct baro_context *devobj = (struct baro_context*)dev_get_drvdata(dev); */
  343. /* do read FIFO data function and report data immediately */
  344. mutex_unlock(&baro_context_obj->baro_op_mutex);
  345. return count;
  346. }
  347. static ssize_t baro_show_flush(struct device *dev, struct device_attribute *attr, char *buf)
  348. {
  349. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  350. }
  351. static ssize_t baro_show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  352. {
  353. const char *devname = NULL;
  354. devname = dev_name(&baro_context_obj->idev->dev);
  355. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5);
  356. }
  357. static int barometer_remove(struct platform_device *pdev)
  358. {
  359. BARO_LOG("barometer_remove\n");
  360. return 0;
  361. }
  362. static int barometer_probe(struct platform_device *pdev)
  363. {
  364. BARO_LOG("barometer_probe\n");
  365. return 0;
  366. }
  367. #ifdef CONFIG_OF
  368. static const struct of_device_id barometer_of_match[] = {
  369. {.compatible = "mediatek,barometer",},
  370. {},
  371. };
  372. #endif
  373. static struct platform_driver barometer_driver = {
  374. .probe = barometer_probe,
  375. .remove = barometer_remove,
  376. .driver = {
  377. .name = "barometer",
  378. #ifdef CONFIG_OF
  379. .of_match_table = barometer_of_match,
  380. #endif
  381. }
  382. };
  383. static int baro_real_driver_init(void)
  384. {
  385. int i = 0;
  386. int err = 0;
  387. BARO_LOG(" baro_real_driver_init +\n");
  388. for (i = 0; i < MAX_CHOOSE_BARO_NUM; i++) {
  389. BARO_LOG(" i=%d\n", i);
  390. if (0 != barometer_init_list[i]) {
  391. BARO_LOG(" baro try to init driver %s\n", barometer_init_list[i]->name);
  392. err = barometer_init_list[i]->init();
  393. if (0 == err) {
  394. BARO_LOG(" baro real driver %s probe ok\n",
  395. barometer_init_list[i]->name);
  396. break;
  397. }
  398. }
  399. }
  400. if (i == MAX_CHOOSE_BARO_NUM) {
  401. BARO_LOG(" baro_real_driver_init fail\n");
  402. err = -1;
  403. }
  404. return err;
  405. }
  406. int baro_driver_add(struct baro_init_info *obj)
  407. {
  408. int err = 0;
  409. int i = 0;
  410. BARO_FUN();
  411. if (!obj) {
  412. BARO_ERR("BARO driver add fail, baro_init_info is NULL\n");
  413. return -1;
  414. }
  415. for (i = 0; i < MAX_CHOOSE_BARO_NUM; i++) {
  416. if (i == 0) {
  417. BARO_LOG("register barometer driver for the first time\n");
  418. if (platform_driver_register(&barometer_driver))
  419. BARO_ERR("failed to register gensor driver already exist\n");
  420. }
  421. if (NULL == barometer_init_list[i]) {
  422. obj->platform_diver_addr = &barometer_driver;
  423. barometer_init_list[i] = obj;
  424. break;
  425. }
  426. }
  427. if (i >= MAX_CHOOSE_BARO_NUM) {
  428. BARO_ERR("BARO driver add err\n");
  429. err = -1;
  430. }
  431. return err;
  432. } EXPORT_SYMBOL_GPL(baro_driver_add);
  433. static int baro_misc_init(struct baro_context *cxt)
  434. {
  435. int err = 0;
  436. cxt->mdev.minor = MISC_DYNAMIC_MINOR;
  437. cxt->mdev.name = BARO_MISC_DEV_NAME;
  438. err = misc_register(&cxt->mdev);
  439. if (err)
  440. BARO_ERR("unable to register baro misc device!!\n");
  441. return err;
  442. }
  443. static void baro_input_destroy(struct baro_context *cxt)
  444. {
  445. struct input_dev *dev = cxt->idev;
  446. input_unregister_device(dev);
  447. input_free_device(dev);
  448. }
  449. static int baro_input_init(struct baro_context *cxt)
  450. {
  451. struct input_dev *dev;
  452. int err = 0;
  453. dev = input_allocate_device();
  454. if (NULL == dev)
  455. return -ENOMEM;
  456. dev->name = BARO_INPUTDEV_NAME;
  457. set_bit(EV_REL, dev->evbit);
  458. input_set_capability(dev, EV_REL, EVENT_TYPE_BARO_VALUE);
  459. input_set_capability(dev, EV_ABS, EVENT_TYPE_BARO_STATUS);
  460. input_set_capability(dev, EV_REL, EVENT_TYPE_BARO_TIMESTAMP_HI);
  461. input_set_capability(dev, EV_REL, EVENT_TYPE_BARO_TIMESTAMP_LO);
  462. /* input_set_abs_params(dev, EVENT_TYPE_BARO_VALUE, BARO_VALUE_MIN, BARO_VALUE_MAX, 0, 0); */
  463. input_set_abs_params(dev, EVENT_TYPE_BARO_STATUS, BARO_STATUS_MIN, BARO_STATUS_MAX, 0, 0);
  464. input_set_drvdata(dev, cxt);
  465. err = input_register_device(dev);
  466. if (err < 0) {
  467. input_free_device(dev);
  468. return err;
  469. }
  470. cxt->idev = dev;
  471. return 0;
  472. }
  473. DEVICE_ATTR(baroenablenodata, S_IWUSR | S_IRUGO, baro_show_enable_nodata, baro_store_enable_nodata);
  474. DEVICE_ATTR(baroactive, S_IWUSR | S_IRUGO, baro_show_active, baro_store_active);
  475. DEVICE_ATTR(barodelay, S_IWUSR | S_IRUGO, baro_show_delay, baro_store_delay);
  476. DEVICE_ATTR(barobatch, S_IWUSR | S_IRUGO, baro_show_batch, baro_store_batch);
  477. DEVICE_ATTR(baroflush, S_IWUSR | S_IRUGO, baro_show_flush, baro_store_flush);
  478. DEVICE_ATTR(barodevnum, S_IWUSR | S_IRUGO, baro_show_devnum, NULL);
  479. static struct attribute *baro_attributes[] = {
  480. &dev_attr_baroenablenodata.attr,
  481. &dev_attr_baroactive.attr,
  482. &dev_attr_barodelay.attr,
  483. &dev_attr_barobatch.attr,
  484. &dev_attr_baroflush.attr,
  485. &dev_attr_barodevnum.attr,
  486. NULL
  487. };
  488. static struct attribute_group baro_attribute_group = {
  489. .attrs = baro_attributes
  490. };
  491. int baro_register_data_path(struct baro_data_path *data)
  492. {
  493. struct baro_context *cxt = NULL;
  494. cxt = baro_context_obj;
  495. cxt->baro_data.get_data = data->get_data;
  496. cxt->baro_data.vender_div = data->vender_div;
  497. cxt->baro_data.get_raw_data = data->get_raw_data;
  498. BARO_LOG("baro register data path vender_div: %d\n", cxt->baro_data.vender_div);
  499. if (NULL == cxt->baro_data.get_data) {
  500. BARO_LOG("baro register data path fail\n");
  501. return -1;
  502. }
  503. return 0;
  504. }
  505. int baro_register_control_path(struct baro_control_path *ctl)
  506. {
  507. struct baro_context *cxt = NULL;
  508. int err = 0;
  509. cxt = baro_context_obj;
  510. cxt->baro_ctl.set_delay = ctl->set_delay;
  511. cxt->baro_ctl.open_report_data = ctl->open_report_data;
  512. cxt->baro_ctl.enable_nodata = ctl->enable_nodata;
  513. cxt->baro_ctl.is_support_batch = ctl->is_support_batch;
  514. cxt->baro_ctl.is_report_input_direct = ctl->is_report_input_direct;
  515. cxt->baro_ctl.is_support_batch = ctl->is_support_batch;
  516. cxt->baro_ctl.is_use_common_factory = ctl->is_use_common_factory;
  517. if (NULL == cxt->baro_ctl.set_delay || NULL == cxt->baro_ctl.open_report_data
  518. || NULL == cxt->baro_ctl.enable_nodata) {
  519. BARO_LOG("baro register control path fail\n");
  520. return -1;
  521. }
  522. /* add misc dev for sensor hal control cmd */
  523. err = baro_misc_init(baro_context_obj);
  524. if (err) {
  525. BARO_ERR("unable to register baro misc device!!\n");
  526. return -2;
  527. }
  528. err = sysfs_create_group(&baro_context_obj->mdev.this_device->kobj, &baro_attribute_group);
  529. if (err < 0) {
  530. BARO_ERR("unable to create baro attribute file\n");
  531. return -3;
  532. }
  533. kobject_uevent(&baro_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  534. return 0;
  535. }
  536. int baro_data_report(struct input_dev *dev, int value, int status, int64_t nt)
  537. {
  538. /* BARO_LOG("+baro_data_report! %d, %d, %d, %d\n",x,y,z,status); */
  539. input_report_rel(dev, EVENT_TYPE_BARO_VALUE, value);
  540. input_report_abs(dev, EVENT_TYPE_BARO_STATUS, status);
  541. input_report_rel(dev, EVENT_TYPE_BARO_TIMESTAMP_HI, nt >> 32);
  542. input_report_rel(dev, EVENT_TYPE_BARO_TIMESTAMP_LO, nt & 0xFFFFFFFFLL);
  543. input_sync(dev);
  544. return 0;
  545. }
  546. static int baro_probe(struct platform_device *pdev)
  547. {
  548. int err;
  549. BARO_LOG("+++++++++++++baro_probe!!\n");
  550. baro_context_obj = baro_context_alloc_object();
  551. if (!baro_context_obj) {
  552. err = -ENOMEM;
  553. BARO_ERR("unable to allocate devobj!\n");
  554. goto exit_alloc_data_failed;
  555. }
  556. /* init real baro driver */
  557. err = baro_real_driver_init();
  558. if (err) {
  559. BARO_ERR("baro real driver init fail\n");
  560. goto real_driver_init_fail;
  561. }
  562. /* init baro common factory mode misc device */
  563. err = baro_factory_device_init();
  564. if (err)
  565. BARO_ERR("baro factory device already registed\n");
  566. /* init input dev */
  567. err = baro_input_init(baro_context_obj);
  568. if (err) {
  569. BARO_ERR("unable to register baro input device!\n");
  570. goto exit_alloc_input_dev_failed;
  571. }
  572. BARO_LOG("----baro_probe OK !!\n");
  573. return 0;
  574. /* exit_hwmsen_create_attr_failed: */
  575. /* exit_misc_register_failed: */
  576. /* exit_err_sysfs: */
  577. if (err) {
  578. BARO_ERR("sysfs node creation error\n");
  579. baro_input_destroy(baro_context_obj);
  580. }
  581. real_driver_init_fail:
  582. exit_alloc_input_dev_failed:
  583. kfree(baro_context_obj);
  584. baro_context_obj = NULL;
  585. exit_alloc_data_failed:
  586. BARO_LOG("----baro_probe fail !!!\n");
  587. return err;
  588. }
  589. static int baro_remove(struct platform_device *pdev)
  590. {
  591. int err = 0;
  592. BARO_FUN(f);
  593. input_unregister_device(baro_context_obj->idev);
  594. sysfs_remove_group(&baro_context_obj->idev->dev.kobj, &baro_attribute_group);
  595. err = misc_deregister(&baro_context_obj->mdev);
  596. if (err)
  597. BARO_ERR("misc_deregister fail: %d\n", err);
  598. kfree(baro_context_obj);
  599. return 0;
  600. }
  601. static int baro_suspend(struct platform_device *dev, pm_message_t state)
  602. {
  603. return 0;
  604. }
  605. /*----------------------------------------------------------------------------*/
  606. static int baro_resume(struct platform_device *dev)
  607. {
  608. return 0;
  609. }
  610. #ifdef CONFIG_OF
  611. static const struct of_device_id m_baro_pl_of_match[] = {
  612. {.compatible = "mediatek,m_baro_pl",},
  613. {},
  614. };
  615. #endif
  616. static struct platform_driver baro_driver = {
  617. .probe = baro_probe,
  618. .remove = baro_remove,
  619. .suspend = baro_suspend,
  620. .resume = baro_resume,
  621. .driver = {
  622. .name = BARO_PL_DEV_NAME,
  623. #ifdef CONFIG_OF
  624. .of_match_table = m_baro_pl_of_match,
  625. #endif
  626. }
  627. };
  628. static int __init baro_init(void)
  629. {
  630. BARO_FUN();
  631. if (platform_driver_register(&baro_driver)) {
  632. BARO_ERR("failed to register baro driver\n");
  633. return -ENODEV;
  634. }
  635. return 0;
  636. }
  637. static void __exit baro_exit(void)
  638. {
  639. platform_driver_unregister(&baro_driver);
  640. platform_driver_unregister(&barometer_driver);
  641. }
  642. late_initcall(baro_init);
  643. /* module_init(baro_init); */
  644. /* module_exit(baro_exit); */
  645. MODULE_LICENSE("GPL");
  646. MODULE_DESCRIPTION("BAROMETER device driver");
  647. MODULE_AUTHOR("Mediatek");