hwmsen_dev.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * Copyright(C)2014 MediaTek Inc.
  3. * Modification based on code covered by the below mentioned copyright
  4. * and/or permission notice(S).
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/delay.h>
  11. #include <linux/input.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/wait.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <hwmsensor.h>
  17. #include <hwmsen_helper.h>
  18. #include <hwmsen_dev.h>
  19. /* add for fix resume issue */
  20. #ifdef CONFIG_PM_WAKELOCKS
  21. #include <linux/pm_wakeup.h>
  22. #else
  23. #include <linux/wakelock.h>
  24. #endif
  25. /* add for fix resume issue end */
  26. #include <cust_alsps.h>
  27. #include <aal_control.h>
  28. #define SENSOR_INVALID_VALUE -1
  29. #define MAX_CHOOSE_G_NUM 5
  30. #define MAX_CHOOSE_M_NUM 5
  31. #if defined(CONFIG_HAS_EARLYSUSPEND)
  32. static void hwmsen_early_suspend(struct early_suspend *h);
  33. static void hwmsen_late_resume(struct early_suspend *h);
  34. #endif
  35. static void update_workqueue_polling_rate(int newDelay);
  36. struct workqueue_struct *sensor_workqueue = NULL;
  37. /******************************************************************************
  38. * structure / enumeration / macro / definition
  39. *****************************************************************************/
  40. struct sensor_delay {
  41. int handle;
  42. uint32_t delay;
  43. };
  44. struct hwmsen_context { /*sensor context */
  45. atomic_t enable;
  46. atomic_t delay;
  47. uint32_t delayCountSet;
  48. uint32_t delayCount;
  49. struct hwmsen_object obj;
  50. };
  51. #if defined(CONFIG_MTK_AUTO_DETECT_ACCELEROMETER)
  52. static char gsensor_name[25];
  53. static struct sensor_init_info *gsensor_init_list[MAX_CHOOSE_G_NUM] = { 0 }; /*modified*/
  54. #endif
  55. #if defined(CONFIG_MTK_AUTO_DETECT_MAGNETOMETER)
  56. static char msensor_name[25];
  57. static struct sensor_init_info *msensor_init_list[MAX_CHOOSE_G_NUM] = { 0 }; /*modified*/
  58. #endif
  59. #if defined(CONFIG_MTK_AUTO_DETECT_ALSPS)
  60. static char alsps_name[25];
  61. static struct sensor_init_info *alsps_init_list[MAX_CHOOSE_G_NUM] = { 0 }; /*modified*/
  62. #endif
  63. /*----------------------------------------------------------------------------*/
  64. struct dev_context {
  65. int polling_running;
  66. struct mutex lock;
  67. struct hwmsen_context *cxt[MAX_ANDROID_SENSOR_NUM + 1];
  68. };
  69. /*-------------Sensor daa-----------------------------------------------------*/
  70. struct hwmsen_data {
  71. struct hwm_sensor_data sensors_data[MAX_ANDROID_SENSOR_NUM + 1];
  72. int data_updata[MAX_ANDROID_SENSOR_NUM + 1];
  73. struct mutex lock;
  74. };
  75. /*----------------------------------------------------------------------------*/
  76. enum HWM_TRC {
  77. HWM_TRC_REPORT_NUM = 0x0001,
  78. HWM_TRC_REPORT_EVT = 0x0002,
  79. HWM_TRC_REPORT_INF = 0X0004,
  80. };
  81. /*----------------------------------------------------------------------------*/
  82. #define C_MAX_OBJECT_NUM 1
  83. struct hwmdev_object {
  84. struct input_dev *idev;
  85. struct miscdevice mdev;
  86. struct dev_context *dc;
  87. struct work_struct report;
  88. atomic_t delay; /*polling period for reporting input event */
  89. atomic_t wake; /*user-space request to wake-up, used with stop */
  90. struct hrtimer hrTimer;
  91. ktime_t target_ktime;
  92. atomic_t trace;
  93. struct workqueue_struct *hwmsen_workqueue;
  94. uint64_t active_sensor; /* Active, but hwmsen don't need data sensor. Maybe other need it's data.*/
  95. uint64_t active_data_sensor; /* Active and hwmsen need data sensor.*/
  96. #if defined(CONFIG_HAS_EARLYSUSPEND)
  97. /* add for fix resume issue */
  98. struct early_suspend early_drv;
  99. #ifdef CONFIG_PM_WAKELOCKS
  100. struct wakeup_source read_data_wake_lock;
  101. #else
  102. struct wake_lock read_data_wake_lock;
  103. #endif
  104. atomic_t early_suspend;
  105. /* add for fix resume end */
  106. #endif /*#if defined(CONFIG_HAS_EARLYSUSPEND)*/
  107. };
  108. static bool enable_again;
  109. static struct hwmdev_object *hwm_obj;
  110. /******************************************************************************
  111. * static variables
  112. *****************************************************************************/
  113. static struct hwmsen_data obj_data = {
  114. .lock = __MUTEX_INITIALIZER(obj_data.lock),
  115. };
  116. static struct dev_context dev_cxt = {
  117. .lock = __MUTEX_INITIALIZER(dev_cxt.lock),
  118. };
  119. /*----------------------------------------------------------------------------*/
  120. static void initTimer(struct hrtimer *timer, enum hrtimer_restart (*callback)(struct hrtimer *))
  121. {
  122. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  123. timer->function = callback;
  124. }
  125. static void startTimer(struct hrtimer *timer, int delay_ms, bool first)
  126. {
  127. struct hwmdev_object *obj = (struct hwmdev_object *)container_of(timer, struct hwmdev_object, hrTimer);
  128. if (obj == NULL) {
  129. HWM_ERR("NULL pointer\n");
  130. return;
  131. }
  132. if (first)
  133. obj->target_ktime = ktime_add_ns(ktime_get(), (int64_t)delay_ms*1000000);
  134. else
  135. obj->target_ktime = ktime_add_ns(obj->target_ktime, (int64_t)delay_ms*1000000);
  136. hrtimer_start(timer, obj->target_ktime, HRTIMER_MODE_ABS);
  137. }
  138. static void stopTimer(struct hrtimer *timer)
  139. {
  140. hrtimer_cancel(timer);
  141. }
  142. /* AAL functions********************************************************************** */
  143. int hwmsen_aal_enable(int en)
  144. {
  145. struct hwmsen_context *cxt = NULL;
  146. int err = 0;
  147. if (!hwm_obj) {
  148. HWM_ERR("AAL hwmdev obj pointer is NULL!\n");
  149. return -EINVAL;
  150. } else if ((hwm_obj->dc->cxt[ID_LIGHT]) == NULL) {
  151. HWM_ERR("the sensor (%d) is not attached!!\n", ID_LIGHT);
  152. return -ENODEV;
  153. }
  154. mutex_lock(&hwm_obj->dc->lock);
  155. cxt = hwm_obj->dc->cxt[ID_LIGHT];
  156. if (en == 1) {
  157. if ((hwm_obj->active_data_sensor & (1LL << ID_LIGHT)) == 0) {
  158. HWM_LOG("enable sensor(%d) by AAL operation\n", ID_LIGHT);
  159. if (cxt->obj.
  160. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &en, sizeof(int), NULL, 0,
  161. NULL) != 0) {
  162. HWM_ERR("enable sensor(%d) err = %d\n", ID_LIGHT, err);
  163. err = -EINVAL;
  164. }
  165. }
  166. } else {
  167. if ((hwm_obj->active_data_sensor & (1LL << ID_LIGHT)) == 0) {
  168. HWM_LOG("disable sensor(%d) by AAL operation\n", ID_LIGHT);
  169. if (cxt->obj.
  170. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &en, sizeof(int), NULL, 0,
  171. NULL) != 0) {
  172. HWM_ERR("disable sensor(%d) err = %d\n", ID_LIGHT, err);
  173. err = -EINVAL;
  174. }
  175. }
  176. }
  177. mutex_unlock(&hwm_obj->dc->lock);
  178. return err;
  179. }
  180. int hwmsen_aal_get_data(void)
  181. {
  182. struct hwmsen_context *cxt = NULL;
  183. int err = 0;
  184. int out_size;
  185. struct hwm_sensor_data sensor_data;
  186. int als_data = 0;
  187. HWM_LOG("hwmsen_aal_get_data1\n");
  188. if (!hwm_obj) {
  189. HWM_ERR("AAL hwmdev obj pointer is NULL!\n");
  190. return -EINVAL;
  191. } else if ((hwm_obj->dc->cxt[ID_LIGHT]) == NULL) {
  192. HWM_ERR("the sensor (%d) is not attached!!\n", ID_LIGHT);
  193. return -ENODEV;
  194. }
  195. mutex_lock(&hwm_obj->dc->lock);
  196. cxt = hwm_obj->dc->cxt[ID_LIGHT];
  197. HWM_LOG("hwmsen_aal_get_data2\n");
  198. err = cxt->obj.sensor_operate(cxt->obj.self, SENSOR_GET_DATA, NULL, 0,
  199. &sensor_data, sizeof(struct hwm_sensor_data), &out_size);
  200. if (err) {
  201. mutex_unlock(&hwm_obj->dc->lock);
  202. HWM_ERR("get data from sensor (%d) fails!!\n", ID_LIGHT);
  203. return -ENODEV;
  204. }
  205. als_data = sensor_data.values[0];
  206. mutex_unlock(&hwm_obj->dc->lock);
  207. HWM_LOG("hwmsen_aal_get_data3\n");
  208. return als_data;
  209. }
  210. int hwmsen_aal_get_status(void)
  211. {
  212. return 0;
  213. }
  214. /* ************************************************************************************ */
  215. /******************************************************************************
  216. * Local functions
  217. *****************************************************************************/
  218. static void hwmsen_work_func(struct work_struct *work)
  219. {
  220. /* HWM_LOG("+++++++++++++++++++++++++hwmsen_work_func workqueue performed!+++++++++++++++++++++++++++++\n"); */
  221. /* struct hwmdev_object *obj = container_of(work, struct hwmdev_object, report); */
  222. struct hwmdev_object *obj = hwm_obj;
  223. struct hwmsen_context *cxt = NULL;
  224. int out_size;
  225. struct hwm_sensor_data sensor_data;
  226. uint64_t event_type = 0;
  227. int64_t nt;
  228. struct timespec time;
  229. int err, idx;
  230. /* int trc = atomic_read(&obj->trace); */
  231. if (obj == NULL) {
  232. HWM_ERR("obj point is NULL!\n");
  233. return;
  234. }
  235. if (atomic_read(&obj->wake)) {
  236. input_event(obj->idev, EV_SYN, SYN_CONFIG, 0);
  237. atomic_set(&obj->wake, 0);
  238. return;
  239. }
  240. memset(&sensor_data, 0, sizeof(sensor_data));
  241. time.tv_sec = time.tv_nsec = 0;
  242. get_monotonic_boottime(&time);
  243. nt = time.tv_sec * 1000000000LL + time.tv_nsec;
  244. /* mutex_lock(&obj_data.lock); */
  245. for (idx = 0; idx < MAX_ANDROID_SENSOR_NUM; idx++) {
  246. cxt = obj->dc->cxt[idx];
  247. if ((cxt == NULL) || (cxt->obj.sensor_operate == NULL)
  248. || !(obj->active_data_sensor & (1LL << idx))) {
  249. continue;
  250. }
  251. /* Interrupt sensor */
  252. if (cxt->obj.polling == 0) {
  253. if (obj_data.data_updata[idx] == 1) {
  254. mutex_lock(&obj_data.lock);
  255. event_type |= (1LL << idx);
  256. obj_data.data_updata[idx] = 0;
  257. mutex_unlock(&obj_data.lock);
  258. }
  259. /* Evne if interrupt mode, try to take the initiative in querying a valid sensor data. */
  260. else if (obj_data.sensors_data[idx].values[0] != SENSOR_INVALID_VALUE)
  261. continue;
  262. }
  263. /* added to surpport set delay to specified sensor */
  264. if (cxt->delayCount > 0) {
  265. /*HWM_LOG("sensor(%d) delayCount = %d\n",idx,cxt->delayCount);*/
  266. cxt->delayCount--;
  267. if (0 == cxt->delayCount) {
  268. cxt->delayCount = cxt->delayCountSet;
  269. /*HWM_LOG("sensor(%d) go to get data\n",idx);*/
  270. } else {
  271. /*HWM_LOG("sensor(%d) wait for next work\n",idx);*/
  272. continue;
  273. }
  274. }
  275. err = cxt->obj.sensor_operate(cxt->obj.self, SENSOR_GET_DATA, NULL, 0,
  276. &sensor_data, sizeof(struct hwm_sensor_data), &out_size);
  277. if (err) {
  278. HWM_ERR("get data from sensor (%d) fails!!\n", idx);
  279. continue;
  280. } else {
  281. if ((idx == ID_LIGHT) || (idx == ID_PRESSURE)
  282. || (idx == ID_PROXIMITY) || (idx == ID_TEMPRERATURE)) {
  283. /* data changed, update the data */
  284. if (sensor_data.values[0] != obj_data.sensors_data[idx].values[0]) {
  285. mutex_lock(&obj_data.lock);
  286. obj_data.sensors_data[idx].values[0] =
  287. sensor_data.values[0];
  288. obj_data.sensors_data[idx].value_divide =
  289. sensor_data.value_divide;
  290. obj_data.sensors_data[idx].status = sensor_data.status;
  291. obj_data.sensors_data[idx].time = nt;
  292. event_type |= (1LL << idx);
  293. mutex_unlock(&obj_data.lock);
  294. /* HWM_LOG("get %d sensor, values: %d!\n", idx, sensor_data.values[0]); */
  295. }
  296. } else {
  297. /* data changed, update the data */
  298. if ((sensor_data.values[0] != obj_data.sensors_data[idx].values[0])
  299. || (sensor_data.values[1] !=
  300. obj_data.sensors_data[idx].values[1])
  301. || (sensor_data.values[2] !=
  302. obj_data.sensors_data[idx].values[2])
  303. || (idx == ID_MAGNETIC) || (idx == ID_ACCELEROMETER)
  304. || (idx == ID_GYROSCOPE) || (idx == ID_ORIENTATION)) {
  305. if ((0 == sensor_data.values[0]
  306. && 0 == sensor_data.values[1]
  307. && 0 == sensor_data.values[2])
  308. && (idx != ID_GYROSCOPE)) {
  309. continue;
  310. }
  311. mutex_lock(&obj_data.lock);
  312. obj_data.sensors_data[idx].values[0] =
  313. sensor_data.values[0];
  314. obj_data.sensors_data[idx].values[1] =
  315. sensor_data.values[1];
  316. obj_data.sensors_data[idx].values[2] =
  317. sensor_data.values[2];
  318. obj_data.sensors_data[idx].value_divide =
  319. sensor_data.value_divide;
  320. obj_data.sensors_data[idx].status = sensor_data.status;
  321. obj_data.sensors_data[idx].time = nt;
  322. event_type |= (1LL << idx);
  323. mutex_unlock(&obj_data.lock);
  324. /*HWM_LOG("get %d sensor, values: %d, %d, %d!\n", idx,*/
  325. /*sensor_data.values[0], sensor_data.values[1], sensor_data.values[2]);*/
  326. }
  327. }
  328. }
  329. }
  330. /* */
  331. /* mutex_unlock(&obj_data.lock); */
  332. if (enable_again == true) {
  333. event_type = obj->active_data_sensor;
  334. enable_again = false;
  335. /* filter -1 value */
  336. for (idx = 0; idx <= MAX_ANDROID_SENSOR_NUM; idx++) {
  337. if (ID_ACCELEROMETER == idx || ID_MAGNETIC == idx || ID_ORIENTATION == idx
  338. || ID_GYROSCOPE == idx || ID_TEMPRERATURE == idx
  339. || ID_LINEAR_ACCELERATION == idx || ID_ROTATION_VECTOR == idx
  340. || ID_GRAVITY == idx) {
  341. if (SENSOR_INVALID_VALUE == obj_data.sensors_data[idx].values[0] ||
  342. SENSOR_INVALID_VALUE == obj_data.sensors_data[idx].values[1] ||
  343. SENSOR_INVALID_VALUE == obj_data.sensors_data[idx].values[2]) {
  344. event_type &= ~(1LL << idx);
  345. /*HWM_LOG("idx=%d,obj->active_sensor after clear: %d\n",idx);*/
  346. }
  347. }
  348. if (ID_PROXIMITY == idx || ID_LIGHT == idx || ID_PRESSURE == idx) {
  349. if (SENSOR_INVALID_VALUE == obj_data.sensors_data[idx].values[0]) {
  350. event_type &= ~(1LL << idx);
  351. /*HWM_LOG("idx=%d,obj->active_sensor after clear: %d\n",idx);*/
  352. }
  353. }
  354. }
  355. /* HWM_LOG("event type after enable: %d\n", event_type); */
  356. }
  357. if ((event_type & (1LL << ID_PROXIMITY))
  358. && SENSOR_INVALID_VALUE == obj_data.sensors_data[ID_PROXIMITY].values[0]) {
  359. event_type &= ~(1LL << ID_PROXIMITY);
  360. /* HWM_LOG("remove ps event!!!!!!!!!!!\n"); */
  361. }
  362. if (event_type != 0) {
  363. input_report_rel(obj->idev, EVENT_TYPE_SENSOR, event_type);
  364. input_report_rel(obj->idev, EVENT_TYPE_SENSOR_EXT, event_type >> 32);
  365. input_sync(obj->idev); /*modified*/
  366. /*HWM_LOG("event type: %d\n", event_type);*/
  367. } else {
  368. /*HWM_LOG("no available sensor!!\n");*/
  369. }
  370. if (obj->dc->polling_running == 1)
  371. startTimer(&obj->hrTimer, atomic_read(&obj->delay), false);
  372. }
  373. /******************************************************************************
  374. * export functions
  375. *****************************************************************************/
  376. int hwmsen_get_interrupt_data(int sensor, struct hwm_sensor_data *data)
  377. {
  378. /*HWM_LOG("++++++++++++++++++++++++++++hwmsen_get_interrupt_data function sensor = %d\n",sensor);*/
  379. struct dev_context *mcxt = &dev_cxt;
  380. struct hwmdev_object *obj = hwm_obj;
  381. int64_t nt;
  382. struct timespec time;
  383. if ((sensor > MAX_ANDROID_SENSOR_NUM) || (mcxt->cxt[sensor] == NULL)
  384. || (mcxt->cxt[sensor]->obj.polling != 0)) {
  385. HWM_ERR("sensor %d!\n", sensor);
  386. return -EINVAL;
  387. }
  388. time.tv_sec = time.tv_nsec = 0;
  389. get_monotonic_boottime(&time);
  390. nt = time.tv_sec * 1000000000LL + time.tv_nsec;
  391. if ((sensor == ID_LIGHT) || (sensor == ID_PRESSURE)
  392. || (sensor == ID_PROXIMITY) || (sensor == ID_TEMPRERATURE)) {
  393. /* data changed, update the data */
  394. if (data->values[0] != obj_data.sensors_data[sensor].values[0]) {
  395. mutex_lock(&obj_data.lock);
  396. obj_data.data_updata[sensor] = 1;
  397. obj_data.sensors_data[sensor].values[0] = data->values[0];
  398. obj_data.sensors_data[sensor].time = nt;
  399. obj_data.sensors_data[sensor].value_divide = data->value_divide;
  400. mutex_unlock(&obj_data.lock);
  401. }
  402. } else {
  403. /* data changed, update the data */
  404. if ((data->values[0] != obj_data.sensors_data[sensor].values[0])
  405. || (data->values[1] != obj_data.sensors_data[sensor].values[1])
  406. || (data->values[2] != obj_data.sensors_data[sensor].values[2])) {
  407. mutex_lock(&obj_data.lock);
  408. obj_data.sensors_data[sensor].values[0] = data->values[0];
  409. obj_data.sensors_data[sensor].values[1] = data->values[1];
  410. obj_data.sensors_data[sensor].values[2] = data->values[2];
  411. obj_data.sensors_data[sensor].value_divide = data->value_divide;
  412. obj_data.data_updata[sensor] = 1;
  413. obj_data.sensors_data[sensor].time = nt;
  414. mutex_unlock(&obj_data.lock);
  415. }
  416. }
  417. if (obj->dc->polling_running == 1)
  418. hwmsen_work_func(NULL);
  419. return 0;
  420. }
  421. /*----------------------------------------------------------------------------*/
  422. EXPORT_SYMBOL_GPL(hwmsen_get_interrupt_data);
  423. /*----------------------------------------------------------------------------*/
  424. enum hrtimer_restart hwmsen_poll(struct hrtimer *timer)
  425. {
  426. struct hwmdev_object *obj = (struct hwmdev_object *)container_of(timer, struct hwmdev_object, hrTimer);
  427. queue_work(obj->hwmsen_workqueue, &obj->report);
  428. return HRTIMER_NORESTART;
  429. }
  430. /*----------------------------------------------------------------------------*/
  431. static struct hwmdev_object *hwmsen_alloc_object(void)
  432. {
  433. struct hwmdev_object *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  434. HWM_FUN(f);
  435. if (!obj) {
  436. HWM_ERR("Alloc hwmsen object error!\n");
  437. return NULL;
  438. }
  439. obj->dc = &dev_cxt;
  440. obj->active_data_sensor = 0;
  441. obj->active_sensor = 0;
  442. atomic_set(&obj->delay, 200); /*5Hz *//* set work queue delay time 200ms*/
  443. atomic_set(&obj->wake, 0);
  444. sensor_workqueue = create_singlethread_workqueue("sensor_polling");
  445. if (!sensor_workqueue) {
  446. kfree(obj);
  447. return NULL;
  448. }
  449. INIT_WORK(&obj->report, hwmsen_work_func);
  450. obj->hwmsen_workqueue = NULL;
  451. obj->hwmsen_workqueue = create_workqueue("hwmsen_polling");
  452. if (!obj->hwmsen_workqueue) {
  453. kfree(obj);
  454. return NULL;
  455. }
  456. initTimer(&obj->hrTimer, hwmsen_poll);
  457. return obj;
  458. }
  459. /*Sensor device driver attach to hwmsen device------------------------------------------------*/
  460. int hwmsen_attach(int sensor, struct hwmsen_object *obj)
  461. {
  462. struct dev_context *mcxt = &dev_cxt;
  463. int err = 0;
  464. HWM_FUN(f);
  465. if ((mcxt == NULL) || (sensor > MAX_ANDROID_SENSOR_NUM)) {
  466. err = -EINVAL;
  467. goto err_exit;
  468. }
  469. mutex_lock(&mcxt->lock);
  470. if (mcxt->cxt[sensor] != NULL) {
  471. err = -EEXIST;
  472. goto err_exit;
  473. } else {
  474. mcxt->cxt[sensor] = kzalloc(sizeof(struct hwmsen_context), GFP_KERNEL);
  475. if (mcxt->cxt[sensor] == NULL) {
  476. err = -EPERM;
  477. goto err_exit;
  478. }
  479. atomic_set(&mcxt->cxt[sensor]->enable, 0);
  480. memcpy(&mcxt->cxt[sensor]->obj, obj, sizeof(*obj));
  481. /* add for android2.3 set sensors default polling delay time is 200ms*/
  482. atomic_set(&mcxt->cxt[sensor]->delay, 200);
  483. }
  484. err_exit:
  485. mutex_unlock(&mcxt->lock);
  486. return err;
  487. }
  488. /*----------------------------------------------------------------------------*/
  489. EXPORT_SYMBOL_GPL(hwmsen_attach);
  490. /*----------------------------------------------------------------------------*/
  491. int hwmsen_detach(int sensor)
  492. {
  493. int err = 0;
  494. struct dev_context *mcxt = &dev_cxt;
  495. HWM_FUN(f);
  496. if ((sensor > MAX_ANDROID_SENSOR_NUM) || (mcxt->cxt[sensor] == NULL)) {
  497. err = -EINVAL;
  498. goto err_exit;
  499. }
  500. mutex_lock(&mcxt->lock);
  501. kfree(mcxt->cxt[sensor]);
  502. mcxt->cxt[sensor] = NULL;
  503. err_exit:
  504. mutex_unlock(&mcxt->lock);
  505. return 0;
  506. }
  507. /*----------------------------------------------------------------------------*/
  508. EXPORT_SYMBOL_GPL(hwmsen_detach);
  509. /*----------------------------------------------------------------------------*/
  510. static int hwmsen_enable(struct hwmdev_object *obj, int sensor, int enable)
  511. {
  512. struct hwmsen_context *cxt = NULL;
  513. int err = 0;
  514. uint64_t sensor_type;
  515. sensor_type = 1LL << sensor;
  516. if (sensor > MAX_ANDROID_SENSOR_NUM || sensor < 0) {
  517. HWM_ERR("handle %d!\n", sensor);
  518. return -EINVAL;
  519. }
  520. if (!obj) {
  521. HWM_ERR("hwmdev obj pointer is NULL!\n");
  522. return -EINVAL;
  523. } else if (obj->dc->cxt[sensor] == NULL) {
  524. HWM_ERR("the sensor (%d) is not attached!!\n", sensor);
  525. return -ENODEV;
  526. }
  527. if (sensor > MAX_ANDROID_SENSOR_NUM) {
  528. HWM_ERR("sensor %d!\n", sensor);
  529. return -EINVAL;
  530. }
  531. mutex_lock(&obj->dc->lock);
  532. cxt = obj->dc->cxt[sensor];
  533. if (enable == 1) {
  534. /*{@for mt6582 blocking issue work around*/
  535. if (sensor == 7) {
  536. HWM_LOG("P-sensor disable LDO low power\n");
  537. pmic_ldo_suspend_enable(0);
  538. }
  539. /*@}*/
  540. enable_again = true;
  541. obj->active_data_sensor |= sensor_type;
  542. if ((obj->active_sensor & sensor_type) == 0) {/* no no-data active*/
  543. if (cxt->obj.
  544. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  545. 0, NULL) != 0) {
  546. if (cxt->obj.
  547. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  548. sizeof(int), NULL, 0, NULL) != 0) {
  549. if (cxt->obj.
  550. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  551. sizeof(int), NULL, 0, NULL) != 0) {
  552. HWM_ERR("activate sensor(%d) 3 times err = %d\n",
  553. sensor, err);
  554. err = -EINVAL;
  555. goto exit;
  556. }
  557. }
  558. }
  559. update_workqueue_polling_rate(200);
  560. atomic_set(&cxt->enable, 1);
  561. }
  562. /* Need to complete the interrupt sensor work */
  563. if ((0 == obj->dc->polling_running) && (obj->active_data_sensor != 0)) {
  564. obj->dc->polling_running = 1;
  565. /* obj->timer.expires = jiffies + atomic_read(&obj->delay)/(1000/HZ); */
  566. /* add_timer(&obj->timer); */
  567. startTimer(&obj->hrTimer, atomic_read(&obj->delay), true);
  568. }
  569. } else if ((enable == 0)) {
  570. /*{@for mt6582 blocking issue work around*/
  571. if (sensor == 7) {
  572. HWM_LOG("P-sensor enable LDO low power\n");
  573. pmic_ldo_suspend_enable(1);
  574. }
  575. /*@}*/
  576. obj->active_data_sensor &= ~sensor_type;
  577. if ((obj->active_sensor & sensor_type) == 0) {/* no no-data active*/
  578. #ifdef CONFIG_CUSTOM_KERNEL_ALSPS
  579. if (sensor == 4 && aal_use == 1) {
  580. HWM_ERR("AAL is used ingnore common light disable\n");
  581. err = 0;
  582. } else
  583. #endif
  584. {
  585. if (cxt->obj.
  586. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  587. sizeof(int), NULL, 0, NULL) != 0) {
  588. HWM_ERR("deactiva sensor(%d) err = %d\n", sensor, err);
  589. err = -EINVAL;
  590. goto exit;
  591. }
  592. }
  593. atomic_set(&cxt->enable, 0);
  594. update_workqueue_polling_rate(200); /* re-update workqueue polling rate*/
  595. }
  596. if ((1 == obj->dc->polling_running) && (obj->active_data_sensor == 0)) {
  597. obj->dc->polling_running = 0;
  598. stopTimer(&obj->hrTimer);
  599. cancel_work_sync(&obj->report);
  600. }
  601. obj_data.sensors_data[sensor].values[0] = SENSOR_INVALID_VALUE;
  602. obj_data.sensors_data[sensor].values[1] = SENSOR_INVALID_VALUE;
  603. obj_data.sensors_data[sensor].values[2] = SENSOR_INVALID_VALUE;
  604. }
  605. HWM_LOG("sensor(%d), flag(%d)\n", sensor, enable);
  606. exit:
  607. mutex_unlock(&obj->dc->lock);
  608. return err;
  609. }
  610. /*-------------no data sensor enable/disable--------------------------------------*/
  611. static int hwmsen_enable_nodata(struct hwmdev_object *obj, int sensor, int enable)
  612. {
  613. struct hwmsen_context *cxt = NULL;
  614. int err = 0;
  615. uint64_t sensor_type;
  616. HWM_FUN(f);
  617. sensor_type = 1LL << sensor;
  618. if (sensor > MAX_ANDROID_SENSOR_NUM || sensor < 0) {
  619. HWM_ERR("handle %d!\n", sensor);
  620. return -EINVAL;
  621. }
  622. if (NULL == obj) {
  623. HWM_ERR("hwmdev obj pointer is NULL!\n");
  624. return -EINVAL;
  625. } else if (obj->dc->cxt[sensor] == NULL) {
  626. HWM_ERR("the sensor (%d) is not attached!!\n", sensor);
  627. return -ENODEV;
  628. }
  629. if (sensor > MAX_ANDROID_SENSOR_NUM) {
  630. HWM_ERR("sensor %d!\n", sensor);
  631. return -EINVAL;
  632. }
  633. mutex_lock(&obj->dc->lock);
  634. cxt = obj->dc->cxt[sensor];
  635. if (enable == 1) {
  636. obj->active_sensor |= sensor_type;
  637. if ((obj->active_data_sensor & sensor_type) == 0) {/* no data active*/
  638. if (cxt->obj.
  639. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  640. 0, NULL) != 0) {
  641. HWM_ERR("activate sensor(%d) err = %d\n", sensor, err);
  642. err = -EINVAL;
  643. goto exit;
  644. }
  645. atomic_set(&cxt->enable, 1);
  646. }
  647. } else {
  648. obj->active_sensor &= ~sensor_type;
  649. if ((obj->active_data_sensor & sensor_type) == 0) {/* no data active*/
  650. if (cxt->obj.
  651. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  652. 0, NULL) != 0) {
  653. HWM_ERR("Deactivate sensor(%d) err = %d\n", sensor, err);
  654. err = -EINVAL;
  655. goto exit;
  656. }
  657. atomic_set(&cxt->enable, 0);
  658. }
  659. }
  660. exit:
  661. mutex_unlock(&obj->dc->lock);
  662. return err;
  663. }
  664. /*------------set delay--------------------------------------------------------*/
  665. static int hwmsen_set_delay(int delay, int handle)
  666. {
  667. int err = 0;
  668. struct hwmsen_context *cxt = NULL;
  669. if (handle > MAX_ANDROID_SENSOR_NUM || handle < 0) {
  670. HWM_ERR("handle %d!\n", handle);
  671. return -EINVAL;
  672. }
  673. if (handle > MAX_ANDROID_SENSOR_NUM) {
  674. HWM_ERR("handle %d!\n", handle);
  675. return -EINVAL;
  676. }
  677. cxt = hwm_obj->dc->cxt[handle];
  678. if (NULL == cxt || (cxt->obj.sensor_operate == NULL)) {
  679. HWM_ERR("have no this sensor %d or operator point is null!\r\n", handle);
  680. } else {/*if(atomic_read(&cxt->enable) != 0)*/ /*always update delay even sensor is not enabled.*/
  681. if (cxt->obj.
  682. sensor_operate(cxt->obj.self, SENSOR_DELAY, &delay, sizeof(int), NULL, 0,
  683. NULL) != 0) {
  684. HWM_ERR("%d sensor's sensor_operate function error %d!\r\n", handle, err);
  685. return err;
  686. }
  687. /* record sensor delay */
  688. atomic_set(&cxt->delay, delay);
  689. }
  690. return err;
  691. }
  692. /*----------------------------------------------------------------------------*/
  693. static int hwmsen_wakeup(struct hwmdev_object *obj)
  694. {
  695. HWM_LOG("hwmsen_wakeup\n");
  696. if (obj == NULL) {
  697. HWM_ERR("null pointer!!\n");
  698. return -EINVAL;
  699. }
  700. input_event(obj->idev, EV_SYN, SYN_CONFIG, 0);
  701. return 0;
  702. }
  703. /*----------------------------------------------------------------------------*/
  704. static ssize_t hwmsen_show_hwmdev(struct device *dev, struct device_attribute *attr, char *buf)
  705. {
  706. int len = 0;
  707. HWM_LOG("sensor test: hwmsen_show_hwmdev function!\n");
  708. /*
  709. if (!devobj || !devobj->dc) {
  710. HWM_ERR("null pointer: %p, %p", devobj, (!devobj) ? (NULL) : (devobj->dc));
  711. return 0;
  712. }
  713. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  714. len += snprintf(buf+len, PAGE_SIZE-len, " %d", idx);
  715. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  716. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  717. len += snprintf(buf+len, PAGE_SIZE-len, " %d", atomic_read(&devobj->dc->cxt[idx].enable));
  718. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  719. */
  720. return len;
  721. }
  722. /*----------------------------------------------------------------------------*/
  723. static ssize_t hwmsen_store_active(struct device *dev, struct device_attribute *attr,
  724. const char *buf, size_t count)
  725. {
  726. /*
  727. printk("sensor test: hwmsen_store_active function!\n");
  728. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  729. int sensor, enable, err, idx;
  730. if (!devobj || !devobj->dc) {
  731. HWM_ERR("null pointer!!\n");
  732. return count;
  733. }
  734. if (!strncmp(buf, "all-start", 9)) {
  735. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  736. hwmsen_enable(devobj, idx, 1);
  737. } else if (!strncmp(buf, "all-stop", 8)) {
  738. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  739. hwmsen_enable(devobj, idx, 0);
  740. } else if (2 == sscanf(buf, "%d %d", &sensor, &enable)) {
  741. if ((err = hwmsen_enable(devobj, sensor, enable)))
  742. HWM_ERR("sensor enable failed: %d\n", err);
  743. }
  744. */
  745. return count;
  746. }
  747. /*----------------------------------------------------------------------------*/
  748. static ssize_t hwmsen_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  749. {
  750. /*
  751. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  752. printk("sensor test: hwmsen_show_delay function!\n");
  753. if (!devobj || !devobj->dc) {
  754. HWM_ERR("null pointer!!\n");
  755. return 0;
  756. }
  757. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&devobj->delay));
  758. */
  759. return 0;
  760. }
  761. /*----------------------------------------------------------------------------*/
  762. static ssize_t hwmsen_store_delay(struct device *dev, struct device_attribute *attr,
  763. const char *buf, size_t count)
  764. {
  765. /*
  766. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  767. int delay;
  768. printk("sensor test: hwmsen_show_delay function!\n");
  769. if (!devobj || !devobj->dc) {
  770. HWM_ERR("null pointer!!\n");
  771. return count;
  772. }
  773. if (1 != sscanf(buf, "%d", &delay)) {
  774. HWM_ERR("invalid format!!\n");
  775. return count;
  776. }
  777. atomic_set(&devobj->delay, delay);
  778. */
  779. return count;
  780. }
  781. /*----------------------------------------------------------------------------*/
  782. static ssize_t hwmsen_show_wake(struct device *dev, struct device_attribute *attr, char *buf)
  783. {
  784. /*
  785. printk("sensor test: hwmsen_show_wake function!\n");
  786. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  787. if (!devobj || !devobj->dc) {
  788. HWM_ERR("null pointer!!\n");
  789. return 0;
  790. }
  791. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&devobj->wake));
  792. */
  793. return 0;
  794. }
  795. /*----------------------------------------------------------------------------*/
  796. static ssize_t hwmsen_store_wake(struct device *dev, struct device_attribute *attr,
  797. const char *buf, size_t count)
  798. {
  799. /*
  800. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  801. int wake, err;
  802. printk("sensor test: hwmsen_store_wake function!\n");
  803. if (!devobj || !devobj->dc) {
  804. HWM_ERR("null pointer!!\n");
  805. return count;
  806. }
  807. if (1 != sscanf(buf, "%d", &wake)) {
  808. HWM_ERR("invalid format!!\n");
  809. return count;
  810. }
  811. if ((err = hwmsen_wakeup(devobj))) {
  812. HWM_ERR("wakeup sensor fail, %d\n", err);
  813. return count;
  814. }
  815. */
  816. return count;
  817. }
  818. /*----------------------------------------------------------------------------*/
  819. static ssize_t hwmsen_show_trace(struct device *dev, struct device_attribute *attr, char *buf)
  820. {
  821. struct i2c_client *client = to_i2c_client(dev);
  822. struct hwmdev_object *obj = i2c_get_clientdata(client);
  823. HWM_FUN(f);
  824. return snprintf(buf, PAGE_SIZE, "0x%08X\n", atomic_read(&obj->trace));
  825. }
  826. /*----------------------------------------------------------------------------*/
  827. static ssize_t hwmsen_store_trace(struct device *dev,
  828. struct device_attribute *attr, const char *buf, size_t count)
  829. {
  830. struct i2c_client *client = to_i2c_client(dev);
  831. struct hwmdev_object *obj = i2c_get_clientdata(client);
  832. int trc;
  833. HWM_FUN(f);
  834. if (1 == sscanf(buf, "0x%x\n", &trc))
  835. atomic_set(&obj->trace, trc);
  836. else
  837. HWM_ERR("set trace level fail!!\n");
  838. return count;
  839. }
  840. /*----------------------------------------------------------------------------*/
  841. static ssize_t hwmsen_show_sensordevnum(struct device *dev,
  842. struct device_attribute *attr, char *buf)
  843. {
  844. const char *devname = NULL;
  845. devname = dev_name(&hwm_obj->idev->dev);
  846. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5);
  847. }
  848. DEVICE_ATTR(hwmdev, S_IRUGO, hwmsen_show_hwmdev, NULL);
  849. DEVICE_ATTR(active, S_IWUSR | S_IRUGO, hwmsen_show_hwmdev, hwmsen_store_active);
  850. DEVICE_ATTR(delay, S_IWUSR | S_IRUGO, hwmsen_show_delay, hwmsen_store_delay);
  851. DEVICE_ATTR(wake, S_IWUSR | S_IRUGO, hwmsen_show_wake, hwmsen_store_wake);
  852. DEVICE_ATTR(trace, S_IWUSR | S_IRUGO, hwmsen_show_trace, hwmsen_store_trace);
  853. DEVICE_ATTR(hwmsensordevnum, S_IRUGO, hwmsen_show_sensordevnum, NULL);
  854. /*----------------------------------------------------------------------------*/
  855. static struct device_attribute *hwmsen_attr_list[] = {
  856. &dev_attr_hwmdev,
  857. &dev_attr_active,
  858. &dev_attr_delay,
  859. &dev_attr_wake,
  860. &dev_attr_trace,
  861. &dev_attr_hwmsensordevnum,
  862. };
  863. /*----------------------------------------------------------------------------*/
  864. static int hwmsen_create_attr(struct device *dev)
  865. {
  866. int idx, err = 0;
  867. int num = (int)(sizeof(hwmsen_attr_list) / sizeof(hwmsen_attr_list[0]));
  868. HWM_FUN();
  869. if (!dev)
  870. return -EINVAL;
  871. for (idx = 0; idx < num; idx++) {
  872. err = device_create_file(dev, hwmsen_attr_list[idx]);
  873. if (err) {
  874. HWM_ERR("device_create_file (%s) = %d\n", hwmsen_attr_list[idx]->attr.name,
  875. err);
  876. break;
  877. }
  878. }
  879. return err;
  880. }
  881. /*----------------------------------------------------------------------------*/
  882. static int hwmsen_delete_attr(struct device *dev)
  883. {
  884. int idx, err = 0;
  885. int num = (int)(sizeof(hwmsen_attr_list) / sizeof(hwmsen_attr_list[0]));
  886. HWM_FUN(f);
  887. if (!dev)
  888. return -EINVAL;
  889. for (idx = 0; idx < num; idx++)
  890. device_remove_file(dev, hwmsen_attr_list[idx]);
  891. return err;
  892. }
  893. /*----------------------------------------------------------------*/
  894. static int init_static_data(void)
  895. {
  896. int i = 0;
  897. /* memset(&obj_data, 0, sizeof(struct hwmsen_data));*/
  898. /* obj_data.lock = __MUTEX_INITIALIZER(obj_data.lock); */
  899. for (i = 0; i < MAX_ANDROID_SENSOR_NUM; i++) {
  900. /*dev_cxt.cxt[i] = NULL; */
  901. memset(&obj_data.sensors_data[i], SENSOR_INVALID_VALUE, sizeof(struct hwm_sensor_data));
  902. obj_data.sensors_data[i].sensor = i;
  903. }
  904. return 0;
  905. }
  906. /*----------------------------------------------------------------*/
  907. static int hwmsen_open(struct inode *node, struct file *fp)
  908. {
  909. HWM_FUN(f);
  910. /*struct file_private* data = kzalloc(sizeof(struct file_private), GFP_KERNEL);*/
  911. /* fp->private_data = data;*/
  912. fp->private_data = NULL;
  913. return nonseekable_open(node, fp);
  914. }
  915. /*----------------------------------------------------------------------------*/
  916. static int hwmsen_release(struct inode *node, struct file *fp)
  917. {
  918. HWM_FUN(f);
  919. kfree(fp->private_data);
  920. fp->private_data = NULL;
  921. return 0;
  922. }
  923. /*----------------------------------------------------------------------------*/
  924. static void update_workqueue_polling_rate(int newDelay)
  925. {
  926. atomic_t delaytemp;
  927. int i = 0;
  928. int idx = 0;
  929. struct hwmsen_context *cxt = NULL;
  930. struct hwmdev_object *obj = hwm_obj;
  931. HWM_FUN(f);
  932. atomic_set(&delaytemp, 200); /*used to finding fastest sensor polling rate*/
  933. for (i = 0; i < MAX_ANDROID_SENSOR_NUM; i++) {
  934. if (hwm_obj->active_data_sensor & 1LL << i) {
  935. if (atomic_read(&delaytemp) > atomic_read(&(hwm_obj->dc->cxt[i]->delay)))
  936. /* work queue polling delay base time*/
  937. atomic_set(&delaytemp, atomic_read(&(hwm_obj->dc->cxt[i]->delay)));
  938. }
  939. }
  940. /*use the fastest sensor polling delay as work queue polling delay base time*/
  941. if (atomic_read(&delaytemp) > newDelay) {
  942. atomic_set(&hwm_obj->delay, newDelay); /* work queue polling delay base time*/
  943. HWM_LOG("set new workqueue base time=%d\n", atomic_read(&hwm_obj->delay));
  944. } else {
  945. atomic_set(&hwm_obj->delay, atomic_read(&delaytemp));
  946. HWM_LOG("set old fastest sensor delay as workqueue base time=%d\n",
  947. atomic_read(&hwm_obj->delay));
  948. }
  949. /*upadate all sensors delayCountSet*/
  950. for (idx = 0; idx < MAX_ANDROID_SENSOR_NUM; idx++) {
  951. cxt = obj->dc->cxt[idx];
  952. if ((cxt == NULL) || (cxt->obj.sensor_operate == NULL)
  953. || !(obj->active_data_sensor & (1LL << idx))) {
  954. continue;
  955. }
  956. if (0 == atomic_read(&cxt->delay)) {
  957. cxt->delayCount = cxt->delayCountSet = 0;
  958. HWM_LOG("%s,set delayCountSet=0 delay =%d handle=%d\r\n", __func__,
  959. atomic_read(&cxt->delay), idx);
  960. }
  961. if (atomic_read(&cxt->delay) <= atomic_read(&hwm_obj->delay)) {
  962. cxt->delayCount = cxt->delayCountSet = 0;
  963. HWM_LOG("%s,set delayCountSet=0 delay =%d handle=%d\r\n", __func__,
  964. atomic_read(&cxt->delay), idx);
  965. } else {
  966. i = atomic_read(&cxt->delay) / atomic_read(&hwm_obj->delay);
  967. cxt->delayCount = cxt->delayCountSet = i;
  968. HWM_LOG("%s:set delayCountSet=%d delay =%d handle=%d\r\n", __func__, i,
  969. atomic_read(&cxt->delay), idx);
  970. #if 0
  971. switch (i) {
  972. case 3:
  973. /* 200/60 ;60/20 */
  974. cxt->delayCount = cxt->delayCountSet = 3;
  975. HWM_LOG("%s:set delayCountSet=3 delay =%d handle=%d\r\n", __func__,
  976. atomic_read(&cxt->delay), idx);
  977. break;
  978. case 10:
  979. /* 200/20 */
  980. cxt->delayCount = cxt->delayCountSet = 10;
  981. HWM_LOG("%s:set delayCountSet=10 delay =%d handle=%d\r\n", __func__,
  982. atomic_read(&cxt->delay), idx);
  983. break;
  984. }
  985. #endif
  986. }
  987. }
  988. }
  989. /* static int hwmsen_ioctl(struct inode *node, struct file *fp, */
  990. /* unsigned int cmd, unsigned long arg) */
  991. static long hwmsen_unlocked_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  992. {
  993. /*HWM_LOG("IO parament %d!\r\n", cmd); */
  994. void __user *argp = (void __user *)arg;
  995. uint32_t flag;
  996. struct sensor_delay delayPara;
  997. struct hwm_trans_data hwm_sensors_data;
  998. int i = 0;
  999. int idx = 0;
  1000. atomic_t delaytemp;
  1001. atomic_set(&delaytemp, 200); /*used to finding fastest sensor polling rate*/
  1002. /*int delaytemp=200;*/ /*used to finding fastest sensor polling rate*/
  1003. if (!hwm_obj) {
  1004. HWM_ERR("null pointer!!\n");
  1005. return -EINVAL;
  1006. }
  1007. switch (cmd) {
  1008. case HWM_IO_SET_DELAY:
  1009. /* android2.3 sensor system has 4 sample delay 0ms 20ms 60ms 200ms */
  1010. if (copy_from_user(&delayPara, argp, sizeof(delayPara))) {
  1011. HWM_ERR("copy_from_user fail!!\n");
  1012. return -EFAULT;
  1013. }
  1014. HWM_LOG("ioctl delay handle=%d,delay =%d\n", delayPara.handle, delayPara.delay);
  1015. hwmsen_set_delay(delayPara.delay, delayPara.handle); /*modified for android2.3*/
  1016. update_workqueue_polling_rate(delayPara.delay);
  1017. break;
  1018. case HWM_IO_SET_WAKE:
  1019. hwmsen_wakeup(hwm_obj);
  1020. break;
  1021. case HWM_IO_ENABLE_SENSOR:
  1022. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1023. HWM_ERR("copy_from_user fail!!\n");
  1024. return -EFAULT;
  1025. }
  1026. hwmsen_enable(hwm_obj, flag, 1);
  1027. break;
  1028. case HWM_IO_DISABLE_SENSOR:
  1029. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1030. HWM_ERR("copy_from_user fail!!\n");
  1031. return -EFAULT;
  1032. }
  1033. hwmsen_enable(hwm_obj, flag, 0);
  1034. break;
  1035. case HWM_IO_GET_SENSORS_DATA:
  1036. if (copy_from_user(&hwm_sensors_data, argp, sizeof(hwm_sensors_data))) {
  1037. HWM_ERR("copy_from_user fail!!\n");
  1038. return -EFAULT;
  1039. }
  1040. mutex_lock(&obj_data.lock);
  1041. /*memcpy(hwm_sensors_data.data, &(obj_data.sensors_data),
  1042. sizeof(struct hwm_sensor_data) * MAX_ANDROID_SENSOR_NUM);*/
  1043. for (i = 0, idx = 0;
  1044. i < MAX_ANDROID_SENSOR_NUM && idx < MAX_SENSOR_DATA_UPDATE_ONCE; i++) {
  1045. if (hwm_sensors_data.data_type & (1LL << i)) {
  1046. memcpy(&hwm_sensors_data.data[idx], &(obj_data.sensors_data[i]),
  1047. sizeof(struct hwm_sensor_data));
  1048. hwm_sensors_data.data[idx].update = 1;
  1049. idx++;
  1050. }
  1051. }
  1052. if (idx < MAX_SENSOR_DATA_UPDATE_ONCE)
  1053. hwm_sensors_data.data[idx].update = 0;
  1054. mutex_unlock(&obj_data.lock);
  1055. if (copy_to_user(argp, &hwm_sensors_data, sizeof(hwm_sensors_data))) {
  1056. HWM_ERR("copy_to_user fail!!\n");
  1057. return -EFAULT;
  1058. }
  1059. break;
  1060. case HWM_IO_ENABLE_SENSOR_NODATA:
  1061. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1062. HWM_ERR("copy_from_user fail!!\n");
  1063. return -EFAULT;
  1064. }
  1065. hwmsen_enable_nodata(hwm_obj, flag, 1);
  1066. break;
  1067. case HWM_IO_DISABLE_SENSOR_NODATA:
  1068. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1069. HWM_ERR("copy_from_user fail!!\n");
  1070. return -EFAULT;
  1071. }
  1072. hwmsen_enable_nodata(hwm_obj, flag, 0);
  1073. break;
  1074. default:
  1075. HWM_ERR("have no this paramenter %d!!\n", cmd);
  1076. return -ENOIOCTLCMD;
  1077. }
  1078. return 0;
  1079. }
  1080. /*----------------------------------------------------------------------------*/
  1081. static const struct file_operations hwmsen_fops = {
  1082. /* .owner = THIS_MODULE,*/
  1083. .open = hwmsen_open,
  1084. .release = hwmsen_release,
  1085. /* .ioctl = hwmsen_ioctl,*/
  1086. .unlocked_ioctl = hwmsen_unlocked_ioctl,
  1087. };
  1088. /*----------------------------------------------------------------------------*/
  1089. static int hwmsen_probe(void)
  1090. {
  1091. int err;
  1092. /* HWM_LOG("+++++++++++++++++hwmsen_probe!!\n"); */
  1093. HWM_FUN(f);
  1094. init_static_data();
  1095. hwm_obj = hwmsen_alloc_object();
  1096. if (!hwm_obj) {
  1097. err = -ENOMEM;
  1098. HWM_ERR("unable to allocate devobj!\n");
  1099. goto exit_alloc_data_failed;
  1100. }
  1101. hwm_obj->idev = input_allocate_device();
  1102. if (!hwm_obj->idev) {
  1103. err = -ENOMEM;
  1104. HWM_ERR("unable to allocate input device!\n");
  1105. goto exit_alloc_input_dev_failed;
  1106. }
  1107. set_bit(EV_REL, hwm_obj->idev->evbit);
  1108. set_bit(EV_SYN, hwm_obj->idev->evbit);
  1109. input_set_capability(hwm_obj->idev, EV_REL, EVENT_TYPE_SENSOR);
  1110. input_set_capability(hwm_obj->idev, EV_REL, EVENT_TYPE_SENSOR_EXT);
  1111. hwm_obj->idev->name = HWM_INPUTDEV_NAME;
  1112. err = input_register_device(hwm_obj->idev);
  1113. if (err) {
  1114. HWM_ERR("unable to register input device!\n");
  1115. goto exit_input_register_device_failed;
  1116. }
  1117. input_set_drvdata(hwm_obj->idev, hwm_obj);
  1118. hwm_obj->mdev.minor = MISC_DYNAMIC_MINOR;
  1119. hwm_obj->mdev.name = HWM_SENSOR_DEV_NAME;
  1120. hwm_obj->mdev.fops = &hwmsen_fops;
  1121. err = misc_register(&hwm_obj->mdev);
  1122. if (err) {
  1123. HWM_ERR("unable to register sensor device!!\n");
  1124. goto exit_misc_register_failed;
  1125. }
  1126. dev_set_drvdata(hwm_obj->mdev.this_device, hwm_obj);
  1127. if (hwmsen_create_attr(hwm_obj->mdev.this_device) != 0) {
  1128. HWM_ERR("unable to create attributes!!\n");
  1129. goto exit_hwmsen_create_attr_failed;
  1130. }
  1131. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  1132. /* add for fix resume bug */
  1133. atomic_set(&(hwm_obj->early_suspend), 0);
  1134. hwm_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  1135. hwm_obj->early_drv.suspend = hwmsen_early_suspend,
  1136. hwm_obj->early_drv.resume = hwmsen_late_resume,
  1137. register_early_suspend(&hwm_obj->early_drv);
  1138. #ifdef CONFIG_PM_WAKELOCKS
  1139. wakeup_source_init(&(hwm_obj->read_data_wake_lock), "read_data_wake_lock");
  1140. #else
  1141. wake_lock_init(&(hwm_obj->read_data_wake_lock), WAKE_LOCK_SUSPEND, "read_data_wake_lock");
  1142. #endif
  1143. /* add for fix resume bug end*/
  1144. #endif /*#if defined(CONFIG_HAS_EARLYSUSPEND)*/
  1145. return 0;
  1146. exit_hwmsen_create_attr_failed:
  1147. exit_misc_register_failed:
  1148. /* exit_get_hwmsen_info_failed:*/
  1149. exit_input_register_device_failed:
  1150. input_free_device(hwm_obj->idev);
  1151. exit_alloc_input_dev_failed:
  1152. kfree(hwm_obj);
  1153. exit_alloc_data_failed:
  1154. return err;
  1155. }
  1156. /*----------------------------------------------------------------------------*/
  1157. static int hwmsen_remove(void)
  1158. {
  1159. HWM_FUN(f);
  1160. input_unregister_device(hwm_obj->idev);
  1161. hwmsen_delete_attr(hwm_obj->mdev.this_device);
  1162. misc_deregister(&hwm_obj->mdev);
  1163. kfree(hwm_obj);
  1164. return 0;
  1165. }
  1166. #if defined(CONFIG_HAS_EARLYSUSPEND)
  1167. static void hwmsen_early_suspend(struct early_suspend *h)
  1168. {
  1169. /*HWM_FUN(f);*/
  1170. atomic_set(&(hwm_obj->early_suspend), 1);
  1171. HWM_LOG(" hwmsen_early_suspend ok------->hwm_obj->early_suspend=%d.\n",
  1172. atomic_read(&hwm_obj->early_suspend));
  1173. }
  1174. /*----------------------------------------------------------------------------*/
  1175. static void hwmsen_late_resume(struct early_suspend *h)
  1176. {
  1177. /*HWM_FUN(f);*/
  1178. atomic_set(&(hwm_obj->early_suspend), 0);
  1179. HWM_LOG(" hwmsen_late_resume ok------->hwm_obj->early_suspend=%d.\n",
  1180. atomic_read(&hwm_obj->early_suspend));
  1181. }
  1182. #endif /*#if defined(CONFIG_HAS_EARLYSUSPEND)*/
  1183. /*----------------------------------------------------------------------------*/
  1184. #if defined(CONFIG_MTK_AUTO_DETECT_MAGNETOMETER)
  1185. int hwmsen_msensor_remove(struct platform_device *pdev)
  1186. {
  1187. int err = 0;
  1188. int i = 0;
  1189. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1190. if (0 == strcmp(msensor_name, msensor_init_list[i]->name)) {
  1191. if (NULL == msensor_init_list[i]->uninit) {
  1192. HWM_LOG(" hwmsen_msensor_remove null pointer.\n");
  1193. return -1;
  1194. }
  1195. msensor_init_list[i]->uninit();
  1196. }
  1197. }
  1198. return 0;
  1199. }
  1200. static int msensor_probe(struct platform_device *pdev)
  1201. {
  1202. int i = 0;
  1203. int err = 0;
  1204. HWM_LOG(" msensor_probe +\n");
  1205. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1206. if (NULL != msensor_init_list[i]) {
  1207. err = msensor_init_list[i]->init();
  1208. if (0 == err) {
  1209. strcpy(msensor_name, msensor_init_list[i]->name);
  1210. HWM_LOG(" msensor %s probe ok\n", msensor_name);
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. return 0;
  1216. }
  1217. static struct platform_driver msensor_driver = {
  1218. .probe = msensor_probe,
  1219. .remove = hwmsen_msensor_remove,
  1220. .driver = {
  1221. .name = "msensor",
  1222. /* .owner = THIS_MODULE,*/
  1223. }
  1224. };
  1225. int hwmsen_msensor_add(struct sensor_init_info *obj)
  1226. {
  1227. int err = 0;
  1228. int i = 0;
  1229. HWM_FUN(f);
  1230. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1231. if (NULL == msensor_init_list[i]) {
  1232. msensor_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1233. if (NULL == msensor_init_list[i]) {
  1234. HWM_ERR("kzalloc error");
  1235. return -1;
  1236. }
  1237. obj->platform_diver_addr = &msensor_driver;
  1238. msensor_init_list[i] = obj;
  1239. break;
  1240. }
  1241. }
  1242. return err;
  1243. }
  1244. EXPORT_SYMBOL_GPL(hwmsen_msensor_add);
  1245. #endif
  1246. #if defined(CONFIG_MTK_AUTO_DETECT_ACCELEROMETER)
  1247. int hwmsen_gsensor_remove(struct platform_device *pdev)
  1248. {
  1249. int i = 0;
  1250. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1251. if (0 == strcmp(gsensor_name, gsensor_init_list[i]->name)) {
  1252. if (NULL == gsensor_init_list[i]->uninit) {
  1253. HWM_LOG(" hwmsen_gsensor_remove null pointer +\n");
  1254. return -1;
  1255. }
  1256. gsensor_init_list[i]->uninit();
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. static int gsensor_probe(struct platform_device *pdev)
  1262. {
  1263. int i = 0;
  1264. int err = 0;
  1265. HWM_LOG(" gsensor_probe +\n");
  1266. /* */
  1267. /*
  1268. for(i = 0; i < MAX_CHOOSE_G_NUM; i++)
  1269. {
  1270. HWM_LOG(" gsensor_init_list[i]=%d\n",gsensor_init_list[i]);
  1271. }
  1272. */
  1273. /* */
  1274. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1275. HWM_LOG(" i=%d\n", i);
  1276. if (0 != gsensor_init_list[i]) {
  1277. HWM_LOG(" !!!!!!!!\n");
  1278. err = gsensor_init_list[i]->init();
  1279. if (0 == err) {
  1280. strcpy(gsensor_name, gsensor_init_list[i]->name);
  1281. HWM_LOG(" gsensor %s probe ok\n", gsensor_name);
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. if (i == MAX_CHOOSE_G_NUM)
  1287. HWM_LOG(" gsensor probe fail\n");
  1288. return 0;
  1289. }
  1290. static struct platform_driver gsensor_driver = {
  1291. .probe = gsensor_probe,
  1292. .remove = hwmsen_gsensor_remove,
  1293. .driver = {
  1294. .name = "gsensor",
  1295. /* .owner = THIS_MODULE,*/
  1296. }
  1297. };
  1298. int hwmsen_gsensor_add(struct sensor_init_info *obj)
  1299. {
  1300. int err = 0;
  1301. int i = 0;
  1302. HWM_FUN(f);
  1303. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1304. if (NULL == gsensor_init_list[i]) {
  1305. gsensor_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1306. if (NULL == gsensor_init_list[i]) {
  1307. HWM_ERR("kzalloc error");
  1308. return -1;
  1309. }
  1310. obj->platform_diver_addr = &gsensor_driver;
  1311. gsensor_init_list[i] = obj;
  1312. break;
  1313. }
  1314. }
  1315. return err;
  1316. }
  1317. EXPORT_SYMBOL_GPL(hwmsen_gsensor_add);
  1318. #endif
  1319. #if defined(CONFIG_MTK_AUTO_DETECT_ALSPS)
  1320. int hwmsen_alsps_sensor_remove(struct platform_device *pdev)
  1321. {
  1322. int err = 0;
  1323. int i = 0;
  1324. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1325. if (0 == strcmp(alsps_name, alsps_init_list[i]->name)) {
  1326. if (NULL == alsps_init_list[i]->uninit) {
  1327. HWM_LOG(" hwmsen_alsps_sensor_remove null pointer.\n");
  1328. return -1;
  1329. }
  1330. alsps_init_list[i]->uninit();
  1331. }
  1332. }
  1333. return 0;
  1334. }
  1335. static int alsps_sensor_probe(struct platform_device *pdev)
  1336. {
  1337. int i = 0;
  1338. int err = 0;
  1339. HWM_LOG(" als_ps sensor_probe +\n");
  1340. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1341. if (NULL != alsps_init_list[i]) {
  1342. err = alsps_init_list[i]->init();
  1343. if (0 == err) {
  1344. strcpy(alsps_name, alsps_init_list[i]->name);
  1345. HWM_LOG(" alsps sensor %s probe ok\n", alsps_name);
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. static struct platform_driver alsps_sensor_driver = {
  1353. .probe = alsps_sensor_probe,
  1354. .remove = hwmsen_alsps_sensor_remove,
  1355. .driver = {
  1356. .name = "als_ps",
  1357. }
  1358. };
  1359. int hwmsen_alsps_sensor_add(struct sensor_init_info *obj)
  1360. {
  1361. int err = 0;
  1362. int i = 0;
  1363. HWM_FUN(f);
  1364. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1365. if (NULL == alsps_init_list[i]) {
  1366. alsps_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1367. if (NULL == alsps_init_list[i]) {
  1368. HWM_ERR("kzalloc error");
  1369. return -1;
  1370. }
  1371. obj->platform_diver_addr = &alsps_sensor_driver;
  1372. alsps_init_list[i] = obj;
  1373. break;
  1374. }
  1375. }
  1376. return err;
  1377. }
  1378. EXPORT_SYMBOL_GPL(hwmsen_alsps_sensor_add);
  1379. #endif
  1380. /*----------------------------------------------------------------------------*/
  1381. static int __init hwmsen_init(void)
  1382. {
  1383. HWM_FUN(f);
  1384. if (0 != hwmsen_probe()) {
  1385. HWM_ERR("failed to register sensor driver");
  1386. return -ENODEV;
  1387. }
  1388. #if defined(CONFIG_MTK_AUTO_DETECT_ACCELEROMETER)
  1389. if (platform_driver_register(&gsensor_driver)) {
  1390. HWM_ERR("failed to register gensor driver");
  1391. return -ENODEV;
  1392. }
  1393. #endif
  1394. #if defined(CONFIG_MTK_AUTO_DETECT_MAGNETOMETER)
  1395. if (platform_driver_register(&msensor_driver)) {
  1396. HWM_ERR("failed to register mensor driver");
  1397. return -ENODEV;
  1398. }
  1399. #endif
  1400. #if defined(CONFIG_MTK_AUTO_DETECT_ALSPS)
  1401. if (platform_driver_register(&alsps_sensor_driver)) {
  1402. HWM_ERR("failed to register alsps_sensor_driver driver");
  1403. return -ENODEV;
  1404. }
  1405. #endif
  1406. return 0;
  1407. }
  1408. /*----------------------------------------------------------------------------*/
  1409. static void __exit hwmsen_exit(void)
  1410. {
  1411. hwmsen_remove();
  1412. }
  1413. /*----------------------------------------------------------------------------*/
  1414. late_initcall(hwmsen_init);
  1415. MODULE_LICENSE("GPL");
  1416. MODULE_DESCRIPTION("sensor device driver");
  1417. MODULE_AUTHOR("MTK");