mag.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. #include "mag.h"
  2. #include "accel.h"
  3. struct mag_context *mag_context_obj = NULL;
  4. static struct mag_init_info *msensor_init_list[MAX_CHOOSE_G_NUM] = {0};
  5. static void initTimer(struct hrtimer *timer, enum hrtimer_restart (*callback)(struct hrtimer *))
  6. {
  7. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  8. timer->function = callback;
  9. }
  10. static void startTimer(struct hrtimer *timer, int delay_ms, bool first)
  11. {
  12. struct acc_context *obj = (struct acc_context *)container_of(timer, struct acc_context, hrTimer);
  13. if (obj == NULL) {
  14. MAG_ERR("NULL pointer\n");
  15. return;
  16. }
  17. if (first) {
  18. obj->target_ktime = ktime_add_ns(ktime_get(), (int64_t)delay_ms*1000000);
  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. }
  24. hrtimer_start(timer, obj->target_ktime, HRTIMER_MODE_ABS);
  25. }
  26. static void stopTimer(struct hrtimer *timer)
  27. {
  28. hrtimer_cancel(timer);
  29. }
  30. static void mag_work_func(struct work_struct *work)
  31. {
  32. struct mag_context *cxt = NULL;
  33. struct hwm_sensor_data sensor_data;
  34. int64_t m_pre_ns, o_pre_ns, cur_ns;
  35. int64_t delay_ms;
  36. struct timespec time;
  37. int err;
  38. int i;
  39. int x, y, z, status;
  40. cxt = mag_context_obj;
  41. delay_ms = atomic_read(&cxt->delay);
  42. memset(&sensor_data, 0, sizeof(sensor_data));
  43. time.tv_sec = time.tv_nsec = 0;
  44. get_monotonic_boottime(&time);
  45. cur_ns = time.tv_sec*1000000000LL+time.tv_nsec;
  46. for (i = 0; i < MAX_M_V_SENSOR; i++) {
  47. if (!(cxt->active_data_sensor&(0x01<<i))) {
  48. continue;
  49. }
  50. if (ID_M_V_MAGNETIC == i) {
  51. err = cxt->mag_dev_data.get_data_m(&x, &y, &z, &status);
  52. if (err) {
  53. MAG_ERR("get %d data fails!!\n" , i);
  54. return;
  55. }
  56. cxt->drv_data[i].mag_data.values[0] = x;
  57. cxt->drv_data[i].mag_data.values[1] = y;
  58. cxt->drv_data[i].mag_data.values[2] = z;
  59. cxt->drv_data[i].mag_data.status = status;
  60. m_pre_ns = cxt->drv_data[i].mag_data.time;
  61. cxt->drv_data[i].mag_data.time = cur_ns;
  62. if (true == cxt->is_first_data_after_enable) {
  63. m_pre_ns = cur_ns;
  64. cxt->is_first_data_after_enable = false;
  65. /* filter -1 value */
  66. if (MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[0] ||
  67. MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[1] ||
  68. MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[2]) {
  69. MAG_LOG(" read invalid data\n");
  70. continue;
  71. }
  72. }
  73. while ((cur_ns - m_pre_ns) >= delay_ms*1800000LL) {
  74. m_pre_ns += delay_ms*1000000LL;
  75. mag_data_report(MAGNETIC, cxt->drv_data[i].mag_data.values[0],
  76. cxt->drv_data[i].mag_data.values[1],
  77. cxt->drv_data[i].mag_data.values[2],
  78. cxt->drv_data[i].mag_data.status, m_pre_ns);
  79. }
  80. mag_data_report(MAGNETIC, cxt->drv_data[i].mag_data.values[0],
  81. cxt->drv_data[i].mag_data.values[1],
  82. cxt->drv_data[i].mag_data.values[2],
  83. cxt->drv_data[i].mag_data.status, cxt->drv_data[i].mag_data.time);
  84. /* MAG_LOG("mag_type(%d) data[%d,%d,%d]\n" ,i,cxt->drv_data[i].mag_data.values[0], */
  85. /* cxt->drv_data[i].mag_data.values[1],cxt->drv_data[i].mag_data.values[2]); */
  86. }
  87. if (ID_M_V_ORIENTATION == i) {
  88. err = cxt->mag_dev_data.get_data_o(&x, &y, &z, &status);
  89. if (err) {
  90. MAG_ERR("get %d data fails!!\n" , i);
  91. return;
  92. }
  93. cxt->drv_data[i].mag_data.values[0] = x;
  94. cxt->drv_data[i].mag_data.values[1] = y;
  95. cxt->drv_data[i].mag_data.values[2] = z;
  96. cxt->drv_data[i].mag_data.status = status;
  97. o_pre_ns = cxt->drv_data[i].mag_data.time;
  98. cxt->drv_data[i].mag_data.time = cur_ns;
  99. if (true == cxt->is_first_data_after_enable) {
  100. o_pre_ns = cur_ns;
  101. cxt->is_first_data_after_enable = false;
  102. /* filter -1 value */
  103. if (MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[0] ||
  104. MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[1] ||
  105. MAG_INVALID_VALUE == cxt->drv_data[i].mag_data.values[2]) {
  106. MAG_LOG(" read invalid data\n");
  107. continue;
  108. }
  109. }
  110. while ((cur_ns - o_pre_ns) >= delay_ms*1800000LL) {
  111. o_pre_ns += delay_ms*1000000LL;
  112. mag_data_report(ORIENTATION, cxt->drv_data[i].mag_data.values[0],
  113. cxt->drv_data[i].mag_data.values[1],
  114. cxt->drv_data[i].mag_data.values[2],
  115. cxt->drv_data[i].mag_data.status, o_pre_ns);
  116. }
  117. mag_data_report(ORIENTATION, cxt->drv_data[i].mag_data.values[0],
  118. cxt->drv_data[i].mag_data.values[1],
  119. cxt->drv_data[i].mag_data.values[2],
  120. cxt->drv_data[i].mag_data.status, cxt->drv_data[i].mag_data.time);
  121. /* MAG_LOG("mag_type(%d) data[%d,%d,%d]\n" ,i,cxt->drv_data[i].mag_data.values[0], */
  122. /* cxt->drv_data[i].mag_data.values[1],cxt->drv_data[i].mag_data.values[2]); */
  123. }
  124. }
  125. if (true == cxt->is_polling_run)
  126. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), false);
  127. }
  128. enum hrtimer_restart mag_poll(struct hrtimer *timer)
  129. {
  130. struct mag_context *obj = (struct mag_context *)container_of(timer, struct mag_context, hrTimer);
  131. queue_work(obj->mag_workqueue, &obj->report);
  132. return HRTIMER_NORESTART;
  133. }
  134. static struct mag_context *mag_context_alloc_object(void)
  135. {
  136. struct mag_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  137. MAG_LOG("mag_context_alloc_object++++\n");
  138. if (!obj) {
  139. MAG_ERR("Alloc magel object error!\n");
  140. return NULL;
  141. }
  142. atomic_set(&obj->delay, 200); /* set work queue delay time 200ms */
  143. atomic_set(&obj->wake, 0);
  144. INIT_WORK(&obj->report, mag_work_func);
  145. obj->mag_workqueue = NULL;
  146. obj->mag_workqueue = create_workqueue("mag_polling");
  147. if (!obj->mag_workqueue) {
  148. kfree(obj);
  149. return NULL;
  150. }
  151. initTimer(&obj->hrTimer, mag_poll);
  152. obj->is_first_data_after_enable = false;
  153. obj->is_polling_run = false;
  154. obj->active_data_sensor = 0;
  155. obj->active_nodata_sensor = 0;
  156. obj->is_batch_enable = false;
  157. mutex_init(&obj->mag_op_mutex);
  158. MAG_LOG("mag_context_alloc_object----\n");
  159. return obj;
  160. }
  161. static int mag_enable_data(int handle, int enable)
  162. {
  163. struct mag_context *cxt = NULL;
  164. cxt = mag_context_obj;
  165. if (NULL == cxt->drv_obj[handle] && NULL == cxt->mag_ctl.m_enable) {
  166. MAG_ERR("no real mag driver\n");
  167. return -1;
  168. }
  169. if (1 == enable) {
  170. MAG_LOG("MAG(%d) enable\n", handle);
  171. cxt->is_first_data_after_enable = true;
  172. cxt->active_data_sensor |= 1<<handle;
  173. if (ID_M_V_ORIENTATION == handle) {
  174. cxt->mag_ctl.o_enable(1);
  175. cxt->mag_ctl.o_open_report_data(1);
  176. }
  177. if (ID_M_V_MAGNETIC == handle) {
  178. cxt->mag_ctl.m_enable(1);
  179. cxt->mag_ctl.m_open_report_data(1);
  180. }
  181. if ((0 != cxt->active_data_sensor) && (false == cxt->is_polling_run) &&
  182. (false == cxt->is_batch_enable)) {
  183. if (false == cxt->mag_ctl.is_report_input_direct) {
  184. MAG_LOG("MAG(%d) mod timer\n", handle);
  185. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  186. cxt->is_polling_run = true;
  187. }
  188. }
  189. }
  190. if (0 == enable) {
  191. MAG_LOG("MAG(%d) disable\n", handle);
  192. cxt->active_data_sensor &= ~(1<<handle);
  193. if (ID_M_V_ORIENTATION == handle) {
  194. cxt->mag_ctl.o_enable(0);
  195. cxt->mag_ctl.o_open_report_data(0);
  196. }
  197. if (ID_M_V_MAGNETIC == handle) {
  198. cxt->mag_ctl.m_enable(0);
  199. cxt->mag_ctl.m_open_report_data(0);
  200. }
  201. if (0 == cxt->active_data_sensor && true == cxt->is_polling_run) {
  202. if (false == cxt->mag_ctl.is_report_input_direct) {
  203. MAG_LOG("MAG(%d) del timer\n", handle);
  204. cxt->is_polling_run = false;
  205. smp_mb();/*fo memory barrier*/
  206. stopTimer(&cxt->hrTimer);
  207. smp_mb();/*for memory barrier*/
  208. cancel_work_sync(&cxt->report);
  209. cxt->drv_data[handle].mag_data.values[0] = MAG_INVALID_VALUE;
  210. cxt->drv_data[handle].mag_data.values[1] = MAG_INVALID_VALUE;
  211. cxt->drv_data[handle].mag_data.values[2] = MAG_INVALID_VALUE;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. /*----------------------------------------------------------------------------*/
  218. static ssize_t mag_show_magdev(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. int len = 0;
  222. MAG_LOG("sensor test: mag function!\n");
  223. return len;
  224. }
  225. /*----------------------------------------------------------------------------*/
  226. static ssize_t mag_store_oactive(struct device *dev, struct device_attribute *attr,
  227. const char *buf, size_t count)
  228. {
  229. struct mag_context *cxt = NULL;
  230. MAG_LOG("mag_store_oactive buf=%s\n", buf);
  231. mutex_lock(&mag_context_obj->mag_op_mutex);
  232. cxt = mag_context_obj;
  233. if (NULL == cxt->mag_ctl.o_enable) {
  234. mutex_unlock(&mag_context_obj->mag_op_mutex);
  235. MAG_LOG("mag_ctl o-enable NULL\n");
  236. return count;
  237. }
  238. if (!strncmp(buf, "1", 1))
  239. mag_enable_data(ID_M_V_ORIENTATION, 1);
  240. else if (!strncmp(buf, "0", 1))
  241. mag_enable_data(ID_M_V_ORIENTATION, 0);
  242. else
  243. MAG_ERR(" mag_store_oactive error !!\n");
  244. mutex_unlock(&mag_context_obj->mag_op_mutex);
  245. MAG_LOG(" mag_store_oactive done\n");
  246. return count;
  247. }
  248. /*----------------------------------------------------------------------------*/
  249. static ssize_t mag_show_oactive(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct mag_context *cxt = NULL;
  253. int div = 0;
  254. cxt = mag_context_obj;
  255. div = cxt->mag_dev_data.div_o;
  256. ACC_LOG("acc mag_dev_data o_div value: %d\n", div);
  257. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  258. }
  259. static ssize_t mag_store_active(struct device *dev, struct device_attribute *attr,
  260. const char *buf, size_t count)
  261. {
  262. struct mag_context *cxt = NULL;
  263. MAG_LOG("mag_store_active buf=%s\n", buf);
  264. mutex_lock(&mag_context_obj->mag_op_mutex);
  265. cxt = mag_context_obj;
  266. if (NULL == cxt->mag_ctl.m_enable) {
  267. mutex_unlock(&mag_context_obj->mag_op_mutex);
  268. MAG_LOG("mag_ctl path is NULL\n");
  269. return count;
  270. }
  271. if (!strncmp(buf, "1", 1))
  272. mag_enable_data(ID_M_V_MAGNETIC, 1);
  273. else if (!strncmp(buf, "0", 1))
  274. mag_enable_data(ID_M_V_MAGNETIC, 0);
  275. else
  276. MAG_ERR(" mag_store_active error !!\n");
  277. mutex_unlock(&mag_context_obj->mag_op_mutex);
  278. MAG_LOG(" mag_store_active done\n");
  279. return count;
  280. }
  281. /*----------------------------------------------------------------------------*/
  282. static ssize_t mag_show_active(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. struct mag_context *cxt = NULL;
  286. int div = 0;
  287. cxt = mag_context_obj;
  288. div = cxt->mag_dev_data.div_m;
  289. ACC_LOG("acc mag_dev_data m_div value: %d\n", div);
  290. return snprintf(buf, PAGE_SIZE, "%d\n", div);
  291. }
  292. static ssize_t mag_store_odelay(struct device *dev, struct device_attribute *attr,
  293. const char *buf, size_t count)
  294. {
  295. int delay = 0;
  296. int mdelay = 0;
  297. int ret = 0;
  298. struct mag_context *cxt = NULL;
  299. mutex_lock(&mag_context_obj->mag_op_mutex);
  300. cxt = mag_context_obj;
  301. if (NULL == cxt->mag_ctl.o_set_delay) {
  302. mutex_unlock(&mag_context_obj->mag_op_mutex);
  303. MAG_LOG("mag_ctl o_delay NULL\n");
  304. return count;
  305. }
  306. MAG_LOG(" mag_odelay ++\n");
  307. ret = kstrtoint(buf, 10, &delay);
  308. if (ret != 0) {
  309. mutex_unlock(&mag_context_obj->mag_op_mutex);
  310. MAG_ERR("invalid format!!\n");
  311. return count;
  312. }
  313. if (false == cxt->mag_ctl.is_report_input_direct) {
  314. mdelay = (int)delay/1000/1000;
  315. atomic_set(&mag_context_obj->delay, mdelay);
  316. }
  317. cxt->mag_ctl.o_set_delay(delay);
  318. mutex_unlock(&mag_context_obj->mag_op_mutex);
  319. MAG_LOG(" mag_odelay %d ns done\n", delay);
  320. return count;
  321. }
  322. static ssize_t mag_show_odelay(struct device *dev,
  323. struct device_attribute *attr, char *buf)
  324. {
  325. int len = 0;
  326. MAG_LOG(" not support now\n");
  327. return len;
  328. }
  329. static ssize_t mag_store_delay(struct device *dev, struct device_attribute *attr,
  330. const char *buf, size_t count)
  331. {
  332. int64_t delay = 0;
  333. int64_t mdelay = 0;
  334. int ret = 0;
  335. struct mag_context *cxt = NULL;
  336. mutex_lock(&mag_context_obj->mag_op_mutex);
  337. cxt = mag_context_obj;
  338. if (NULL == cxt->mag_ctl.m_set_delay) {
  339. mutex_unlock(&mag_context_obj->mag_op_mutex);
  340. MAG_LOG("mag_ctl m_delay NULL\n");
  341. return count;
  342. }
  343. MAG_LOG(" mag_delay ++\n");
  344. ret = kstrtoll(buf, 10, &delay);
  345. if (ret != 0) {
  346. mutex_unlock(&mag_context_obj->mag_op_mutex);
  347. MAG_ERR("invalid format!!\n");
  348. return count;
  349. }
  350. if (false == cxt->mag_ctl.is_report_input_direct) {
  351. mdelay = delay;
  352. do_div(mdelay, 1000000);
  353. atomic_set(&mag_context_obj->delay, mdelay);
  354. }
  355. cxt->mag_ctl.m_set_delay(delay);
  356. mutex_unlock(&mag_context_obj->mag_op_mutex);
  357. MAG_LOG(" mag_delay %lld ns done\n", delay);
  358. return count;
  359. }
  360. static ssize_t mag_show_delay(struct device *dev,
  361. struct device_attribute *attr, char *buf)
  362. {
  363. int len = 0;
  364. MAG_LOG(" not support now\n");
  365. return len;
  366. }
  367. static ssize_t mag_store_batch(struct device *dev, struct device_attribute *attr,
  368. const char *buf, size_t count)
  369. {
  370. struct mag_context *cxt = NULL;
  371. MAG_LOG("mag_store_batch buf=%s\n", buf);
  372. mutex_lock(&mag_context_obj->mag_op_mutex);
  373. cxt = mag_context_obj;
  374. if (cxt->mag_ctl.is_support_batch) {
  375. if (!strncmp(buf, "1", 1)) {
  376. cxt->is_batch_enable = true;
  377. if (true == cxt->is_polling_run) {
  378. cxt->is_polling_run = false;
  379. smp_mb(); /* for memory barrier */
  380. stopTimer(&cxt->hrTimer);
  381. smp_mb(); /* for memory barrier */
  382. cancel_work_sync(&cxt->report);
  383. cxt->drv_data[ID_M_V_MAGNETIC].mag_data.values[0] = MAG_INVALID_VALUE;
  384. cxt->drv_data[ID_M_V_MAGNETIC].mag_data.values[1] = MAG_INVALID_VALUE;
  385. cxt->drv_data[ID_M_V_MAGNETIC].mag_data.values[2] = MAG_INVALID_VALUE;
  386. }
  387. } else if (!strncmp(buf, "0", 1)) {
  388. cxt->is_batch_enable = false;
  389. if (false == cxt->is_polling_run) {
  390. if (false == cxt->mag_ctl.is_report_input_direct) {
  391. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  392. cxt->is_polling_run = true;
  393. }
  394. }
  395. } else
  396. MAG_ERR(" mag_store_batch error !!\n");
  397. } else
  398. MAG_LOG(" mag_store_batch not supported\n");
  399. mutex_unlock(&mag_context_obj->mag_op_mutex);
  400. MAG_LOG(" mag_store_batch done: %d\n", cxt->is_batch_enable);
  401. return count;
  402. }
  403. static ssize_t mag_show_batch(struct device *dev,
  404. struct device_attribute *attr, char *buf)
  405. {
  406. int len = 0;
  407. MAG_LOG(" not support now\n");
  408. return len;
  409. }
  410. static ssize_t mag_store_flush(struct device *dev, struct device_attribute *attr,
  411. const char *buf, size_t count)
  412. {
  413. return count;
  414. }
  415. /* need work around again */
  416. static ssize_t mag_show_sensordevnum(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. unsigned int devnum;
  420. int ret;
  421. struct mag_context *cxt = NULL;
  422. const char *devname = NULL;
  423. cxt = mag_context_obj;
  424. devname = dev_name(&cxt->idev->dev);
  425. ret = sscanf(devname+5, "%d", &devnum);
  426. return snprintf(buf, PAGE_SIZE, "%d\n", devnum);
  427. }
  428. static ssize_t mag_show_flush(struct device *dev,
  429. struct device_attribute *attr, char *buf)
  430. {
  431. int len = 0;
  432. MAG_LOG(" not support now\n");
  433. return len;
  434. }
  435. static ssize_t mag_store_obatch(struct device *dev, struct device_attribute *attr,
  436. const char *buf, size_t count)
  437. {
  438. struct mag_context *cxt = NULL;
  439. MAG_LOG("mag_store_obatch buf=%s\n", buf);
  440. mutex_lock(&mag_context_obj->mag_op_mutex);
  441. cxt = mag_context_obj;
  442. if (cxt->mag_ctl.is_support_batch) {
  443. if (!strncmp(buf, "1", 1)) {
  444. cxt->is_batch_enable = true;
  445. if (true == cxt->is_polling_run) {
  446. cxt->is_polling_run = false;
  447. del_timer_sync(&cxt->timer);
  448. cancel_work_sync(&cxt->report);
  449. cxt->drv_data[ID_M_V_ORIENTATION].mag_data.values[0] = MAG_INVALID_VALUE;
  450. cxt->drv_data[ID_M_V_ORIENTATION].mag_data.values[1] = MAG_INVALID_VALUE;
  451. cxt->drv_data[ID_M_V_ORIENTATION].mag_data.values[2] = MAG_INVALID_VALUE;
  452. }
  453. } else if (!strncmp(buf, "0", 1)) {
  454. cxt->is_batch_enable = false;
  455. if (false == cxt->is_polling_run) {
  456. if (false == cxt->mag_ctl.is_report_input_direct && 0 !=
  457. (cxt->active_data_sensor&ID_M_V_ORIENTATION)) {
  458. startTimer(&cxt->hrTimer, atomic_read(&cxt->delay), true);
  459. cxt->is_polling_run = true;
  460. }
  461. }
  462. } else
  463. MAG_ERR(" mag_store_obatch error !!\n");
  464. } else
  465. MAG_LOG(" mag_store_obatch not supported\n");
  466. mutex_unlock(&mag_context_obj->mag_op_mutex);
  467. MAG_LOG(" mag_store_obatch done: %d\n", cxt->is_batch_enable);
  468. return count;
  469. }
  470. static ssize_t mag_show_obatch(struct device *dev,
  471. struct device_attribute *attr, char *buf)
  472. {
  473. int len = 0;
  474. MAG_LOG(" not support now\n");
  475. return len;
  476. }
  477. static ssize_t mag_store_oflush(struct device *dev, struct device_attribute *attr,
  478. const char *buf, size_t count)
  479. {
  480. return count;
  481. }
  482. static ssize_t mag_show_oflush(struct device *dev,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. int len = 0;
  486. MAG_LOG(" not support now\n");
  487. return len;
  488. }
  489. int mag_attach(int sensor, struct mag_drv_obj *obj)
  490. {
  491. int err = 0;
  492. MAG_FUN();
  493. mag_context_obj->drv_obj[sensor] = kzalloc(sizeof(struct mag_drv_obj), GFP_KERNEL);
  494. if (mag_context_obj->drv_obj[sensor] == NULL) {
  495. err = -EPERM;
  496. MAG_ERR(" mag attatch alloc fail\n");
  497. return err;
  498. }
  499. memcpy(mag_context_obj->drv_obj[sensor], obj, sizeof(*obj));
  500. if (NULL == mag_context_obj->drv_obj[sensor]) {
  501. err = -1;
  502. MAG_ERR(" mag attatch fail\n");
  503. }
  504. return err;
  505. }
  506. /*----------------------------------------------------------------------------*/
  507. EXPORT_SYMBOL_GPL(mag_attach);
  508. static int msensor_remove(struct platform_device *pdev)
  509. {
  510. MAG_LOG("msensor_remove\n");
  511. return 0;
  512. }
  513. static int msensor_probe(struct platform_device *pdev)
  514. {
  515. MAG_LOG("msensor_probe\n");
  516. return 0;
  517. }
  518. #ifdef CONFIG_OF
  519. static const struct of_device_id msensor_of_match[] = {
  520. { .compatible = "mediatek,msensor", },
  521. {},
  522. };
  523. #endif
  524. static struct platform_driver msensor_driver = {
  525. .probe = msensor_probe,
  526. .remove = msensor_remove,
  527. .driver = {
  528. .name = "msensor",
  529. #ifdef CONFIG_OF
  530. .of_match_table = msensor_of_match,
  531. #endif
  532. }
  533. };
  534. static int mag_real_driver_init(void)
  535. {
  536. int i = 0;
  537. int err = 0;
  538. MAG_LOG(" mag_real_driver_init +\n");
  539. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  540. MAG_LOG(" i=%d\n", i);
  541. if (0 != msensor_init_list[i]) {
  542. MAG_LOG(" mag try to init driver %s\n", msensor_init_list[i]->name);
  543. err = msensor_init_list[i]->init();
  544. if (0 == err) {
  545. MAG_LOG(" mag real driver %s probe ok\n", msensor_init_list[i]->name);
  546. break;
  547. }
  548. }
  549. }
  550. if (i == MAX_CHOOSE_G_NUM) {
  551. MAG_LOG(" mag_real_driver_init fail\n");
  552. err = -1;
  553. }
  554. return err;
  555. }
  556. int mag_driver_add(struct mag_init_info *obj)
  557. {
  558. int err = 0;
  559. int i = 0;
  560. MAG_FUN();
  561. if (!obj) {
  562. MAG_ERR("MAG driver add fail, mag_init_info is NULL\n");
  563. return -1;
  564. }
  565. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  566. if ((i == 0) && (NULL == msensor_init_list[0])) {
  567. MAG_LOG("register mensor driver for the first time\n");
  568. if (platform_driver_register(&msensor_driver))
  569. MAG_ERR("failed to register msensor driver already exist\n");
  570. }
  571. if (NULL == msensor_init_list[i]) {
  572. obj->platform_diver_addr = &msensor_driver;
  573. msensor_init_list[i] = obj;
  574. break;
  575. }
  576. }
  577. if (i >= MAX_CHOOSE_G_NUM) {
  578. MAG_ERR("MAG driver add err\n");
  579. err = -1;
  580. }
  581. return err;
  582. }
  583. EXPORT_SYMBOL_GPL(mag_driver_add);
  584. static int mag_misc_init(struct mag_context *cxt)
  585. {
  586. int err = 0;
  587. cxt->mdev.minor = MISC_DYNAMIC_MINOR;
  588. cxt->mdev.name = MAG_MISC_DEV_NAME;
  589. err = misc_register(&cxt->mdev);
  590. if (err)
  591. MAG_ERR("unable to register mag misc device!!\n");
  592. return err;
  593. }
  594. static void mag_input_destroy(struct mag_context *cxt)
  595. {
  596. struct input_dev *dev = cxt->idev;
  597. input_unregister_device(dev);
  598. input_free_device(dev);
  599. }
  600. static int mag_input_init(struct mag_context *cxt)
  601. {
  602. struct input_dev *dev;
  603. int err = 0;
  604. dev = input_allocate_device();
  605. if (NULL == dev)
  606. return -ENOMEM;
  607. dev->name = MAG_INPUTDEV_NAME;
  608. input_set_capability(dev, EV_ABS, EVENT_TYPE_MAGEL_X);
  609. input_set_capability(dev, EV_ABS, EVENT_TYPE_MAGEL_Y);
  610. input_set_capability(dev, EV_ABS, EVENT_TYPE_MAGEL_Z);
  611. input_set_capability(dev, EV_ABS, EVENT_TYPE_MAGEL_STATUS);
  612. input_set_capability(dev, EV_REL, EVENT_TYPE_MAGEL_UPDATE);
  613. input_set_capability(dev, EV_REL, EVENT_TYPE_MAG_TIMESTAMP_HI);
  614. input_set_capability(dev, EV_REL, EVENT_TYPE_MAG_TIMESTAMP_LO);
  615. input_set_capability(dev, EV_REL, EVENT_TYPE_ORIENT_UPDATE);
  616. input_set_capability(dev, EV_REL, EVENT_TYPE_ORIENT_TIMESTAMP_HI);
  617. input_set_capability(dev, EV_REL, EVENT_TYPE_ORIENT_TIMESTAMP_LO);
  618. input_set_capability(dev, EV_ABS, EVENT_TYPE_O_X);
  619. input_set_capability(dev, EV_ABS, EVENT_TYPE_O_Y);
  620. input_set_capability(dev, EV_ABS, EVENT_TYPE_O_Z);
  621. input_set_capability(dev, EV_ABS, EVENT_TYPE_O_STATUS);
  622. input_set_capability(dev, EV_REL, EVENT_TYPE_O_UPDATE);
  623. input_set_abs_params(dev, EVENT_TYPE_MAGEL_X, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  624. input_set_abs_params(dev, EVENT_TYPE_MAGEL_Y, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  625. input_set_abs_params(dev, EVENT_TYPE_MAGEL_Z, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  626. input_set_abs_params(dev, EVENT_TYPE_MAGEL_STATUS, MAG_STATUS_MIN, MAG_STATUS_MAX, 0, 0);
  627. input_set_abs_params(dev, EVENT_TYPE_O_X, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  628. input_set_abs_params(dev, EVENT_TYPE_O_Y, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  629. input_set_abs_params(dev, EVENT_TYPE_O_Z, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
  630. input_set_abs_params(dev, EVENT_TYPE_O_STATUS, MAG_STATUS_MIN, MAG_STATUS_MAX, 0, 0);
  631. input_set_drvdata(dev, cxt);
  632. err = input_register_device(dev);
  633. if (err < 0) {
  634. input_free_device(dev);
  635. return err;
  636. }
  637. cxt->idev = dev;
  638. return 0;
  639. }
  640. DEVICE_ATTR(magdev, S_IWUSR | S_IRUGO, mag_show_magdev, NULL);
  641. DEVICE_ATTR(magactive, S_IWUSR | S_IRUGO, mag_show_active, mag_store_active);
  642. DEVICE_ATTR(magdelay, S_IWUSR | S_IRUGO, mag_show_delay, mag_store_delay);
  643. DEVICE_ATTR(magoactive, S_IWUSR | S_IRUGO, mag_show_oactive, mag_store_oactive);
  644. DEVICE_ATTR(magodelay, S_IWUSR | S_IRUGO, mag_show_odelay, mag_store_odelay);
  645. DEVICE_ATTR(magbatch, S_IWUSR | S_IRUGO, mag_show_batch, mag_store_batch);
  646. DEVICE_ATTR(magflush, S_IWUSR | S_IRUGO, mag_show_flush, mag_store_flush);
  647. DEVICE_ATTR(magobatch, S_IWUSR | S_IRUGO, mag_show_obatch, mag_store_obatch);
  648. DEVICE_ATTR(magoflush, S_IWUSR | S_IRUGO, mag_show_oflush, mag_store_oflush);
  649. DEVICE_ATTR(magdevnum, S_IWUSR | S_IRUGO, mag_show_sensordevnum, NULL);
  650. static struct attribute *mag_attributes[] = {
  651. &dev_attr_magdev.attr,
  652. &dev_attr_magactive.attr,
  653. &dev_attr_magdelay.attr,
  654. &dev_attr_magbatch.attr,
  655. &dev_attr_magflush.attr,
  656. &dev_attr_magoactive.attr,
  657. &dev_attr_magodelay.attr,
  658. &dev_attr_magobatch.attr,
  659. &dev_attr_magoflush.attr,
  660. &dev_attr_magdevnum.attr,
  661. NULL
  662. };
  663. static struct attribute_group mag_attribute_group = {
  664. .attrs = mag_attributes
  665. };
  666. int mag_register_data_path(struct mag_data_path *data)
  667. {
  668. struct mag_context *cxt = NULL;
  669. cxt = mag_context_obj;
  670. cxt->mag_dev_data.div_m = data->div_m;
  671. cxt->mag_dev_data.div_o = data->div_o;
  672. cxt->mag_dev_data.get_data_m = data->get_data_m;
  673. cxt->mag_dev_data.get_data_o = data->get_data_o;
  674. cxt->mag_dev_data.get_raw_data = data->get_raw_data;
  675. MAG_LOG("mag register data path div_o: %d\n", cxt->mag_dev_data.div_o);
  676. MAG_LOG("mag register data path div_m: %d\n", cxt->mag_dev_data.div_m);
  677. return 0;
  678. }
  679. int mag_register_control_path(struct mag_control_path *ctl)
  680. {
  681. struct mag_context *cxt = NULL;
  682. int err = 0;
  683. cxt = mag_context_obj;
  684. cxt->mag_ctl.m_set_delay = ctl->m_set_delay;
  685. cxt->mag_ctl.m_enable = ctl->m_enable;
  686. cxt->mag_ctl.m_open_report_data = ctl->m_open_report_data;
  687. cxt->mag_ctl.o_set_delay = ctl->o_set_delay;
  688. cxt->mag_ctl.o_open_report_data = ctl->o_open_report_data;
  689. cxt->mag_ctl.o_enable = ctl->o_enable;
  690. cxt->mag_ctl.is_report_input_direct = ctl->is_report_input_direct;
  691. cxt->mag_ctl.is_support_batch = ctl->is_support_batch;
  692. cxt->mag_ctl.is_use_common_factory = ctl->is_use_common_factory;
  693. if (NULL == cxt->mag_ctl.m_set_delay || NULL == cxt->mag_ctl.m_enable
  694. || NULL == cxt->mag_ctl.m_open_report_data
  695. || NULL == cxt->mag_ctl.o_set_delay || NULL == cxt->mag_ctl.o_open_report_data
  696. || NULL == cxt->mag_ctl.o_enable) {
  697. MAG_LOG("mag register control path fail\n");
  698. return -1;
  699. }
  700. /* add misc dev for sensor hal control cmd */
  701. err = mag_misc_init(mag_context_obj);
  702. if (err) {
  703. MAG_ERR("unable to register mag misc device!!\n");
  704. return -2;
  705. }
  706. err = sysfs_create_group(&mag_context_obj->mdev.this_device->kobj,
  707. &mag_attribute_group);
  708. if (err < 0) {
  709. MAG_ERR("unable to create mag attribute file\n");
  710. return -3;
  711. }
  712. kobject_uevent(&mag_context_obj->mdev.this_device->kobj, KOBJ_ADD);
  713. return 0;
  714. }
  715. static int x1, y1, z1;
  716. static long pc;
  717. static long count;
  718. static int check_repeat_data(int x, int y, int z)
  719. {
  720. if ((x1 == x) && (y1 == y) && (z1 == z))
  721. pc++;
  722. else
  723. pc = 0;
  724. x1 = x; y1 = y; z1 = z;
  725. if (pc > 100) {
  726. MAG_ERR("Mag sensor output repeat data\n");
  727. pc = 0;
  728. }
  729. return 0;
  730. }
  731. static int check_abnormal_data(int x, int y, int z, int status)
  732. {
  733. long total;
  734. total = (x*x + y*y + z*z)/16;
  735. if ((total < 100) || (total > 10000)) {
  736. if (count % 10 == 0)
  737. MAG_ERR("mag sensor abnormal data: x=%d,y=%d,z=%d, status=%d\n", x, y, z, status);
  738. count++;
  739. if (count > 1000)
  740. count = 0;
  741. }
  742. return 0;
  743. }
  744. int mag_data_report(enum MAG_TYPE type, int x, int y, int z, int status, int64_t nt)
  745. {
  746. /* MAG_LOG("update!valus: %d, %d, %d, %d\n" , x, y, z, status); */
  747. struct mag_context *cxt = NULL;
  748. check_repeat_data(x, y, z);
  749. check_abnormal_data(x, y, z, status);
  750. cxt = mag_context_obj;
  751. if (MAGNETIC == type) {
  752. input_report_abs(cxt->idev, EVENT_TYPE_MAGEL_STATUS, status);
  753. input_report_abs(cxt->idev, EVENT_TYPE_MAGEL_X, x);
  754. input_report_abs(cxt->idev, EVENT_TYPE_MAGEL_Y, y);
  755. input_report_abs(cxt->idev, EVENT_TYPE_MAGEL_Z, z);
  756. input_report_rel(cxt->idev, EVENT_TYPE_MAGEL_UPDATE, 1);
  757. input_report_rel(cxt->idev, EVENT_TYPE_MAG_TIMESTAMP_HI, nt >> 32);
  758. input_report_rel(cxt->idev, EVENT_TYPE_MAG_TIMESTAMP_LO, nt & 0xFFFFFFFFLL);
  759. input_sync(cxt->idev);
  760. }
  761. if (ORIENTATION == type) {
  762. input_report_abs(cxt->idev, EVENT_TYPE_O_STATUS, status);
  763. input_report_abs(cxt->idev, EVENT_TYPE_O_X, x);
  764. input_report_abs(cxt->idev, EVENT_TYPE_O_Y, y);
  765. input_report_abs(cxt->idev, EVENT_TYPE_O_Z, z);
  766. input_report_rel(cxt->idev, EVENT_TYPE_O_UPDATE, 1);
  767. input_report_rel(cxt->idev, EVENT_TYPE_ORIENT_TIMESTAMP_HI, nt >> 32);
  768. input_report_rel(cxt->idev, EVENT_TYPE_ORIENT_TIMESTAMP_LO, nt & 0xFFFFFFFFLL);
  769. input_sync(cxt->idev);
  770. }
  771. return 0;
  772. }
  773. static int mag_probe(void)
  774. {
  775. int err;
  776. MAG_LOG("+++++++++++++mag_probe!!\n");
  777. mag_context_obj = mag_context_alloc_object();
  778. if (!mag_context_obj) {
  779. err = -ENOMEM;
  780. MAG_ERR("unable to allocate devobj!\n");
  781. goto exit_alloc_data_failed;
  782. }
  783. /* init real mageleration driver */
  784. err = mag_real_driver_init();
  785. if (err) {
  786. MAG_ERR("mag_real_driver_init fail\n");
  787. goto exit_alloc_data_failed;
  788. }
  789. err = mag_factory_device_init();
  790. if (err)
  791. MAG_ERR("mag_factory_device_init fail\n");
  792. /* init input dev */
  793. err = mag_input_init(mag_context_obj);
  794. if (err) {
  795. MAG_ERR("unable to register mag input device!\n");
  796. goto exit_alloc_input_dev_failed;
  797. }
  798. MAG_LOG("----magel_probe OK !!\n");
  799. return 0;
  800. exit_alloc_input_dev_failed:
  801. mag_input_destroy(mag_context_obj);
  802. exit_alloc_data_failed:
  803. kfree(mag_context_obj);
  804. MAG_ERR("----magel_probe fail !!!\n");
  805. return err;
  806. }
  807. static int mag_remove(void)
  808. {
  809. int err = 0;
  810. MAG_FUN(f);
  811. input_unregister_device(mag_context_obj->idev);
  812. sysfs_remove_group(&mag_context_obj->idev->dev.kobj,
  813. &mag_attribute_group);
  814. err = misc_deregister(&mag_context_obj->mdev);
  815. if (err)
  816. MAG_ERR("misc_deregister fail: %d\n", err);
  817. kfree(mag_context_obj);
  818. return 0;
  819. }
  820. static int __init mag_init(void)
  821. {
  822. MAG_FUN();
  823. if (mag_probe()) {
  824. MAG_ERR("failed to register mag driver\n");
  825. return -ENODEV;
  826. }
  827. return 0;
  828. }
  829. static void __exit mag_exit(void)
  830. {
  831. mag_remove();
  832. platform_driver_unregister(&msensor_driver);
  833. }
  834. late_initcall(mag_init);
  835. MODULE_LICENSE("GPL");
  836. MODULE_DESCRIPTION("MAGELEROMETER device driver");
  837. MODULE_AUTHOR("Mediatek");