hwmsen_dev.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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 (!obj) {
  517. HWM_ERR("hwmdev obj pointer is NULL!\n");
  518. return -EINVAL;
  519. } else if (obj->dc->cxt[sensor] == NULL) {
  520. HWM_ERR("the sensor (%d) is not attached!!\n", sensor);
  521. return -ENODEV;
  522. }
  523. if (sensor > MAX_ANDROID_SENSOR_NUM) {
  524. HWM_ERR("sensor %d!\n", sensor);
  525. return -EINVAL;
  526. }
  527. mutex_lock(&obj->dc->lock);
  528. cxt = obj->dc->cxt[sensor];
  529. if (enable == 1) {
  530. /*{@for mt6582 blocking issue work around*/
  531. if (sensor == 7) {
  532. HWM_LOG("P-sensor disable LDO low power\n");
  533. pmic_ldo_suspend_enable(0);
  534. }
  535. /*@}*/
  536. enable_again = true;
  537. obj->active_data_sensor |= sensor_type;
  538. if ((obj->active_sensor & sensor_type) == 0) {/* no no-data active*/
  539. if (cxt->obj.
  540. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  541. 0, NULL) != 0) {
  542. if (cxt->obj.
  543. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  544. sizeof(int), NULL, 0, NULL) != 0) {
  545. if (cxt->obj.
  546. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  547. sizeof(int), NULL, 0, NULL) != 0) {
  548. HWM_ERR("activate sensor(%d) 3 times err = %d\n",
  549. sensor, err);
  550. err = -EINVAL;
  551. goto exit;
  552. }
  553. }
  554. }
  555. update_workqueue_polling_rate(200);
  556. atomic_set(&cxt->enable, 1);
  557. }
  558. /* Need to complete the interrupt sensor work */
  559. if ((0 == obj->dc->polling_running) && (obj->active_data_sensor != 0)) {
  560. obj->dc->polling_running = 1;
  561. /* obj->timer.expires = jiffies + atomic_read(&obj->delay)/(1000/HZ); */
  562. /* add_timer(&obj->timer); */
  563. startTimer(&obj->hrTimer, atomic_read(&obj->delay), true);
  564. }
  565. } else if ((enable == 0)) {
  566. /*{@for mt6582 blocking issue work around*/
  567. if (sensor == 7) {
  568. HWM_LOG("P-sensor enable LDO low power\n");
  569. pmic_ldo_suspend_enable(1);
  570. }
  571. /*@}*/
  572. obj->active_data_sensor &= ~sensor_type;
  573. if ((obj->active_sensor & sensor_type) == 0) {/* no no-data active*/
  574. #ifdef CONFIG_CUSTOM_KERNEL_ALSPS
  575. if (sensor == 4 && aal_use == 1) {
  576. HWM_ERR("AAL is used ingnore common light disable\n");
  577. err = 0;
  578. } else
  579. #endif
  580. {
  581. if (cxt->obj.
  582. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable,
  583. sizeof(int), NULL, 0, NULL) != 0) {
  584. HWM_ERR("deactiva sensor(%d) err = %d\n", sensor, err);
  585. err = -EINVAL;
  586. goto exit;
  587. }
  588. }
  589. atomic_set(&cxt->enable, 0);
  590. update_workqueue_polling_rate(200); /* re-update workqueue polling rate*/
  591. }
  592. if ((1 == obj->dc->polling_running) && (obj->active_data_sensor == 0)) {
  593. obj->dc->polling_running = 0;
  594. stopTimer(&obj->hrTimer);
  595. cancel_work_sync(&obj->report);
  596. }
  597. obj_data.sensors_data[sensor].values[0] = SENSOR_INVALID_VALUE;
  598. obj_data.sensors_data[sensor].values[1] = SENSOR_INVALID_VALUE;
  599. obj_data.sensors_data[sensor].values[2] = SENSOR_INVALID_VALUE;
  600. }
  601. HWM_LOG("sensor(%d), flag(%d)\n", sensor, enable);
  602. exit:
  603. mutex_unlock(&obj->dc->lock);
  604. return err;
  605. }
  606. /*-------------no data sensor enable/disable--------------------------------------*/
  607. static int hwmsen_enable_nodata(struct hwmdev_object *obj, int sensor, int enable)
  608. {
  609. struct hwmsen_context *cxt = NULL;
  610. int err = 0;
  611. uint64_t sensor_type;
  612. HWM_FUN(f);
  613. sensor_type = 1LL << sensor;
  614. if (NULL == obj) {
  615. HWM_ERR("hwmdev obj pointer is NULL!\n");
  616. return -EINVAL;
  617. } else if (obj->dc->cxt[sensor] == NULL) {
  618. HWM_ERR("the sensor (%d) is not attached!!\n", sensor);
  619. return -ENODEV;
  620. }
  621. if (sensor > MAX_ANDROID_SENSOR_NUM) {
  622. HWM_ERR("sensor %d!\n", sensor);
  623. return -EINVAL;
  624. }
  625. mutex_lock(&obj->dc->lock);
  626. cxt = obj->dc->cxt[sensor];
  627. if (enable == 1) {
  628. obj->active_sensor |= sensor_type;
  629. if ((obj->active_data_sensor & sensor_type) == 0) {/* no data active*/
  630. if (cxt->obj.
  631. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  632. 0, NULL) != 0) {
  633. HWM_ERR("activate sensor(%d) err = %d\n", sensor, err);
  634. err = -EINVAL;
  635. goto exit;
  636. }
  637. atomic_set(&cxt->enable, 1);
  638. }
  639. } else {
  640. obj->active_sensor &= ~sensor_type;
  641. if ((obj->active_data_sensor & sensor_type) == 0) {/* no data active*/
  642. if (cxt->obj.
  643. sensor_operate(cxt->obj.self, SENSOR_ENABLE, &enable, sizeof(int), NULL,
  644. 0, NULL) != 0) {
  645. HWM_ERR("Deactivate sensor(%d) err = %d\n", sensor, err);
  646. err = -EINVAL;
  647. goto exit;
  648. }
  649. atomic_set(&cxt->enable, 0);
  650. }
  651. }
  652. exit:
  653. mutex_unlock(&obj->dc->lock);
  654. return err;
  655. }
  656. /*------------set delay--------------------------------------------------------*/
  657. static int hwmsen_set_delay(int delay, int handle)
  658. {
  659. int err = 0;
  660. struct hwmsen_context *cxt = NULL;
  661. if (handle > MAX_ANDROID_SENSOR_NUM) {
  662. HWM_ERR("handle %d!\n", handle);
  663. return -EINVAL;
  664. }
  665. cxt = hwm_obj->dc->cxt[handle];
  666. if (NULL == cxt || (cxt->obj.sensor_operate == NULL)) {
  667. HWM_ERR("have no this sensor %d or operator point is null!\r\n", handle);
  668. } else {/*if(atomic_read(&cxt->enable) != 0)*/ /*always update delay even sensor is not enabled.*/
  669. if (cxt->obj.
  670. sensor_operate(cxt->obj.self, SENSOR_DELAY, &delay, sizeof(int), NULL, 0,
  671. NULL) != 0) {
  672. HWM_ERR("%d sensor's sensor_operate function error %d!\r\n", handle, err);
  673. return err;
  674. }
  675. /* record sensor delay */
  676. atomic_set(&cxt->delay, delay);
  677. }
  678. return err;
  679. }
  680. /*----------------------------------------------------------------------------*/
  681. static int hwmsen_wakeup(struct hwmdev_object *obj)
  682. {
  683. HWM_LOG("hwmsen_wakeup\n");
  684. if (obj == NULL) {
  685. HWM_ERR("null pointer!!\n");
  686. return -EINVAL;
  687. }
  688. input_event(obj->idev, EV_SYN, SYN_CONFIG, 0);
  689. return 0;
  690. }
  691. /*----------------------------------------------------------------------------*/
  692. static ssize_t hwmsen_show_hwmdev(struct device *dev, struct device_attribute *attr, char *buf)
  693. {
  694. int len = 0;
  695. HWM_LOG("sensor test: hwmsen_show_hwmdev function!\n");
  696. /*
  697. if (!devobj || !devobj->dc) {
  698. HWM_ERR("null pointer: %p, %p", devobj, (!devobj) ? (NULL) : (devobj->dc));
  699. return 0;
  700. }
  701. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  702. len += snprintf(buf+len, PAGE_SIZE-len, " %d", idx);
  703. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  704. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  705. len += snprintf(buf+len, PAGE_SIZE-len, " %d", atomic_read(&devobj->dc->cxt[idx].enable));
  706. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  707. */
  708. return len;
  709. }
  710. /*----------------------------------------------------------------------------*/
  711. static ssize_t hwmsen_store_active(struct device *dev, struct device_attribute *attr,
  712. const char *buf, size_t count)
  713. {
  714. /*
  715. printk("sensor test: hwmsen_store_active function!\n");
  716. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  717. int sensor, enable, err, idx;
  718. if (!devobj || !devobj->dc) {
  719. HWM_ERR("null pointer!!\n");
  720. return count;
  721. }
  722. if (!strncmp(buf, "all-start", 9)) {
  723. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  724. hwmsen_enable(devobj, idx, 1);
  725. } else if (!strncmp(buf, "all-stop", 8)) {
  726. for (idx = 0; idx < C_MAX_HWMSEN_NUM; idx++)
  727. hwmsen_enable(devobj, idx, 0);
  728. } else if (2 == sscanf(buf, "%d %d", &sensor, &enable)) {
  729. if ((err = hwmsen_enable(devobj, sensor, enable)))
  730. HWM_ERR("sensor enable failed: %d\n", err);
  731. }
  732. */
  733. return count;
  734. }
  735. /*----------------------------------------------------------------------------*/
  736. static ssize_t hwmsen_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
  737. {
  738. /*
  739. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  740. printk("sensor test: hwmsen_show_delay function!\n");
  741. if (!devobj || !devobj->dc) {
  742. HWM_ERR("null pointer!!\n");
  743. return 0;
  744. }
  745. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&devobj->delay));
  746. */
  747. return 0;
  748. }
  749. /*----------------------------------------------------------------------------*/
  750. static ssize_t hwmsen_store_delay(struct device *dev, struct device_attribute *attr,
  751. const char *buf, size_t count)
  752. {
  753. /*
  754. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  755. int delay;
  756. printk("sensor test: hwmsen_show_delay function!\n");
  757. if (!devobj || !devobj->dc) {
  758. HWM_ERR("null pointer!!\n");
  759. return count;
  760. }
  761. if (1 != sscanf(buf, "%d", &delay)) {
  762. HWM_ERR("invalid format!!\n");
  763. return count;
  764. }
  765. atomic_set(&devobj->delay, delay);
  766. */
  767. return count;
  768. }
  769. /*----------------------------------------------------------------------------*/
  770. static ssize_t hwmsen_show_wake(struct device *dev, struct device_attribute *attr, char *buf)
  771. {
  772. /*
  773. printk("sensor test: hwmsen_show_wake function!\n");
  774. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  775. if (!devobj || !devobj->dc) {
  776. HWM_ERR("null pointer!!\n");
  777. return 0;
  778. }
  779. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&devobj->wake));
  780. */
  781. return 0;
  782. }
  783. /*----------------------------------------------------------------------------*/
  784. static ssize_t hwmsen_store_wake(struct device *dev, struct device_attribute *attr,
  785. const char *buf, size_t count)
  786. {
  787. /*
  788. struct hwmdev_object *devobj = (struct hwmdev_object*)dev_get_drvdata(dev);
  789. int wake, err;
  790. printk("sensor test: hwmsen_store_wake function!\n");
  791. if (!devobj || !devobj->dc) {
  792. HWM_ERR("null pointer!!\n");
  793. return count;
  794. }
  795. if (1 != sscanf(buf, "%d", &wake)) {
  796. HWM_ERR("invalid format!!\n");
  797. return count;
  798. }
  799. if ((err = hwmsen_wakeup(devobj))) {
  800. HWM_ERR("wakeup sensor fail, %d\n", err);
  801. return count;
  802. }
  803. */
  804. return count;
  805. }
  806. /*----------------------------------------------------------------------------*/
  807. static ssize_t hwmsen_show_trace(struct device *dev, struct device_attribute *attr, char *buf)
  808. {
  809. struct i2c_client *client = to_i2c_client(dev);
  810. struct hwmdev_object *obj = i2c_get_clientdata(client);
  811. HWM_FUN(f);
  812. return snprintf(buf, PAGE_SIZE, "0x%08X\n", atomic_read(&obj->trace));
  813. }
  814. /*----------------------------------------------------------------------------*/
  815. static ssize_t hwmsen_store_trace(struct device *dev,
  816. struct device_attribute *attr, const char *buf, size_t count)
  817. {
  818. struct i2c_client *client = to_i2c_client(dev);
  819. struct hwmdev_object *obj = i2c_get_clientdata(client);
  820. int trc;
  821. HWM_FUN(f);
  822. if (1 == sscanf(buf, "0x%x\n", &trc))
  823. atomic_set(&obj->trace, trc);
  824. else
  825. HWM_ERR("set trace level fail!!\n");
  826. return count;
  827. }
  828. /*----------------------------------------------------------------------------*/
  829. static ssize_t hwmsen_show_sensordevnum(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. const char *devname = NULL;
  833. devname = dev_name(&hwm_obj->idev->dev);
  834. return snprintf(buf, PAGE_SIZE, "%s\n", devname + 5);
  835. }
  836. DEVICE_ATTR(hwmdev, S_IRUGO, hwmsen_show_hwmdev, NULL);
  837. DEVICE_ATTR(active, S_IWUSR | S_IRUGO, hwmsen_show_hwmdev, hwmsen_store_active);
  838. DEVICE_ATTR(delay, S_IWUSR | S_IRUGO, hwmsen_show_delay, hwmsen_store_delay);
  839. DEVICE_ATTR(wake, S_IWUSR | S_IRUGO, hwmsen_show_wake, hwmsen_store_wake);
  840. DEVICE_ATTR(trace, S_IWUSR | S_IRUGO, hwmsen_show_trace, hwmsen_store_trace);
  841. DEVICE_ATTR(hwmsensordevnum, S_IRUGO, hwmsen_show_sensordevnum, NULL);
  842. /*----------------------------------------------------------------------------*/
  843. static struct device_attribute *hwmsen_attr_list[] = {
  844. &dev_attr_hwmdev,
  845. &dev_attr_active,
  846. &dev_attr_delay,
  847. &dev_attr_wake,
  848. &dev_attr_trace,
  849. &dev_attr_hwmsensordevnum,
  850. };
  851. /*----------------------------------------------------------------------------*/
  852. static int hwmsen_create_attr(struct device *dev)
  853. {
  854. int idx, err = 0;
  855. int num = (int)(sizeof(hwmsen_attr_list) / sizeof(hwmsen_attr_list[0]));
  856. HWM_FUN();
  857. if (!dev)
  858. return -EINVAL;
  859. for (idx = 0; idx < num; idx++) {
  860. err = device_create_file(dev, hwmsen_attr_list[idx]);
  861. if (err) {
  862. HWM_ERR("device_create_file (%s) = %d\n", hwmsen_attr_list[idx]->attr.name,
  863. err);
  864. break;
  865. }
  866. }
  867. return err;
  868. }
  869. /*----------------------------------------------------------------------------*/
  870. static int hwmsen_delete_attr(struct device *dev)
  871. {
  872. int idx, err = 0;
  873. int num = (int)(sizeof(hwmsen_attr_list) / sizeof(hwmsen_attr_list[0]));
  874. HWM_FUN(f);
  875. if (!dev)
  876. return -EINVAL;
  877. for (idx = 0; idx < num; idx++)
  878. device_remove_file(dev, hwmsen_attr_list[idx]);
  879. return err;
  880. }
  881. /*----------------------------------------------------------------*/
  882. static int init_static_data(void)
  883. {
  884. int i = 0;
  885. /* memset(&obj_data, 0, sizeof(struct hwmsen_data));*/
  886. /* obj_data.lock = __MUTEX_INITIALIZER(obj_data.lock); */
  887. for (i = 0; i < MAX_ANDROID_SENSOR_NUM; i++) {
  888. /*dev_cxt.cxt[i] = NULL; */
  889. memset(&obj_data.sensors_data[i], SENSOR_INVALID_VALUE, sizeof(struct hwm_sensor_data));
  890. obj_data.sensors_data[i].sensor = i;
  891. }
  892. return 0;
  893. }
  894. /*----------------------------------------------------------------*/
  895. static int hwmsen_open(struct inode *node, struct file *fp)
  896. {
  897. HWM_FUN(f);
  898. /*struct file_private* data = kzalloc(sizeof(struct file_private), GFP_KERNEL);*/
  899. /* fp->private_data = data;*/
  900. fp->private_data = NULL;
  901. return nonseekable_open(node, fp);
  902. }
  903. /*----------------------------------------------------------------------------*/
  904. static int hwmsen_release(struct inode *node, struct file *fp)
  905. {
  906. HWM_FUN(f);
  907. kfree(fp->private_data);
  908. fp->private_data = NULL;
  909. return 0;
  910. }
  911. /*----------------------------------------------------------------------------*/
  912. static void update_workqueue_polling_rate(int newDelay)
  913. {
  914. atomic_t delaytemp;
  915. int i = 0;
  916. int idx = 0;
  917. struct hwmsen_context *cxt = NULL;
  918. struct hwmdev_object *obj = hwm_obj;
  919. HWM_FUN(f);
  920. atomic_set(&delaytemp, 200); /*used to finding fastest sensor polling rate*/
  921. for (i = 0; i < MAX_ANDROID_SENSOR_NUM; i++) {
  922. if (hwm_obj->active_data_sensor & 1LL << i) {
  923. if (atomic_read(&delaytemp) > atomic_read(&(hwm_obj->dc->cxt[i]->delay)))
  924. /* work queue polling delay base time*/
  925. atomic_set(&delaytemp, atomic_read(&(hwm_obj->dc->cxt[i]->delay)));
  926. }
  927. }
  928. /*use the fastest sensor polling delay as work queue polling delay base time*/
  929. if (atomic_read(&delaytemp) > newDelay) {
  930. atomic_set(&hwm_obj->delay, newDelay); /* work queue polling delay base time*/
  931. HWM_LOG("set new workqueue base time=%d\n", atomic_read(&hwm_obj->delay));
  932. } else {
  933. atomic_set(&hwm_obj->delay, atomic_read(&delaytemp));
  934. HWM_LOG("set old fastest sensor delay as workqueue base time=%d\n",
  935. atomic_read(&hwm_obj->delay));
  936. }
  937. /*upadate all sensors delayCountSet*/
  938. for (idx = 0; idx < MAX_ANDROID_SENSOR_NUM; idx++) {
  939. cxt = obj->dc->cxt[idx];
  940. if ((cxt == NULL) || (cxt->obj.sensor_operate == NULL)
  941. || !(obj->active_data_sensor & (1LL << idx))) {
  942. continue;
  943. }
  944. if (0 == atomic_read(&cxt->delay)) {
  945. cxt->delayCount = cxt->delayCountSet = 0;
  946. HWM_LOG("%s,set delayCountSet=0 delay =%d handle=%d\r\n", __func__,
  947. atomic_read(&cxt->delay), idx);
  948. }
  949. if (atomic_read(&cxt->delay) <= atomic_read(&hwm_obj->delay)) {
  950. cxt->delayCount = cxt->delayCountSet = 0;
  951. HWM_LOG("%s,set delayCountSet=0 delay =%d handle=%d\r\n", __func__,
  952. atomic_read(&cxt->delay), idx);
  953. } else {
  954. i = atomic_read(&cxt->delay) / atomic_read(&hwm_obj->delay);
  955. cxt->delayCount = cxt->delayCountSet = i;
  956. HWM_LOG("%s:set delayCountSet=%d delay =%d handle=%d\r\n", __func__, i,
  957. atomic_read(&cxt->delay), idx);
  958. #if 0
  959. switch (i) {
  960. case 3:
  961. /* 200/60 ;60/20 */
  962. cxt->delayCount = cxt->delayCountSet = 3;
  963. HWM_LOG("%s:set delayCountSet=3 delay =%d handle=%d\r\n", __func__,
  964. atomic_read(&cxt->delay), idx);
  965. break;
  966. case 10:
  967. /* 200/20 */
  968. cxt->delayCount = cxt->delayCountSet = 10;
  969. HWM_LOG("%s:set delayCountSet=10 delay =%d handle=%d\r\n", __func__,
  970. atomic_read(&cxt->delay), idx);
  971. break;
  972. }
  973. #endif
  974. }
  975. }
  976. }
  977. /* static int hwmsen_ioctl(struct inode *node, struct file *fp, */
  978. /* unsigned int cmd, unsigned long arg) */
  979. static long hwmsen_unlocked_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  980. {
  981. /*HWM_LOG("IO parament %d!\r\n", cmd); */
  982. void __user *argp = (void __user *)arg;
  983. uint32_t flag;
  984. struct sensor_delay delayPara;
  985. struct hwm_trans_data hwm_sensors_data;
  986. int i = 0;
  987. int idx = 0;
  988. atomic_t delaytemp;
  989. atomic_set(&delaytemp, 200); /*used to finding fastest sensor polling rate*/
  990. /*int delaytemp=200;*/ /*used to finding fastest sensor polling rate*/
  991. if (!hwm_obj) {
  992. HWM_ERR("null pointer!!\n");
  993. return -EINVAL;
  994. }
  995. switch (cmd) {
  996. case HWM_IO_SET_DELAY:
  997. /* android2.3 sensor system has 4 sample delay 0ms 20ms 60ms 200ms */
  998. if (copy_from_user(&delayPara, argp, sizeof(delayPara))) {
  999. HWM_ERR("copy_from_user fail!!\n");
  1000. return -EFAULT;
  1001. }
  1002. HWM_LOG("ioctl delay handle=%d,delay =%d\n", delayPara.handle, delayPara.delay);
  1003. hwmsen_set_delay(delayPara.delay, delayPara.handle); /*modified for android2.3*/
  1004. update_workqueue_polling_rate(delayPara.delay);
  1005. break;
  1006. case HWM_IO_SET_WAKE:
  1007. hwmsen_wakeup(hwm_obj);
  1008. break;
  1009. case HWM_IO_ENABLE_SENSOR:
  1010. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1011. HWM_ERR("copy_from_user fail!!\n");
  1012. return -EFAULT;
  1013. }
  1014. hwmsen_enable(hwm_obj, flag, 1);
  1015. break;
  1016. case HWM_IO_DISABLE_SENSOR:
  1017. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1018. HWM_ERR("copy_from_user fail!!\n");
  1019. return -EFAULT;
  1020. }
  1021. hwmsen_enable(hwm_obj, flag, 0);
  1022. break;
  1023. case HWM_IO_GET_SENSORS_DATA:
  1024. if (copy_from_user(&hwm_sensors_data, argp, sizeof(hwm_sensors_data))) {
  1025. HWM_ERR("copy_from_user fail!!\n");
  1026. return -EFAULT;
  1027. }
  1028. mutex_lock(&obj_data.lock);
  1029. /*memcpy(hwm_sensors_data.data, &(obj_data.sensors_data),
  1030. sizeof(struct hwm_sensor_data) * MAX_ANDROID_SENSOR_NUM);*/
  1031. for (i = 0, idx = 0;
  1032. i < MAX_ANDROID_SENSOR_NUM && idx < MAX_SENSOR_DATA_UPDATE_ONCE; i++) {
  1033. if (hwm_sensors_data.data_type & (1LL << i)) {
  1034. memcpy(&hwm_sensors_data.data[idx], &(obj_data.sensors_data[i]),
  1035. sizeof(struct hwm_sensor_data));
  1036. hwm_sensors_data.data[idx].update = 1;
  1037. idx++;
  1038. }
  1039. }
  1040. if (idx < MAX_SENSOR_DATA_UPDATE_ONCE)
  1041. hwm_sensors_data.data[idx].update = 0;
  1042. mutex_unlock(&obj_data.lock);
  1043. if (copy_to_user(argp, &hwm_sensors_data, sizeof(hwm_sensors_data))) {
  1044. HWM_ERR("copy_to_user fail!!\n");
  1045. return -EFAULT;
  1046. }
  1047. break;
  1048. case HWM_IO_ENABLE_SENSOR_NODATA:
  1049. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1050. HWM_ERR("copy_from_user fail!!\n");
  1051. return -EFAULT;
  1052. }
  1053. hwmsen_enable_nodata(hwm_obj, flag, 1);
  1054. break;
  1055. case HWM_IO_DISABLE_SENSOR_NODATA:
  1056. if (copy_from_user(&flag, argp, sizeof(flag))) {
  1057. HWM_ERR("copy_from_user fail!!\n");
  1058. return -EFAULT;
  1059. }
  1060. hwmsen_enable_nodata(hwm_obj, flag, 0);
  1061. break;
  1062. default:
  1063. HWM_ERR("have no this paramenter %d!!\n", cmd);
  1064. return -ENOIOCTLCMD;
  1065. }
  1066. return 0;
  1067. }
  1068. /*----------------------------------------------------------------------------*/
  1069. static const struct file_operations hwmsen_fops = {
  1070. /* .owner = THIS_MODULE,*/
  1071. .open = hwmsen_open,
  1072. .release = hwmsen_release,
  1073. /* .ioctl = hwmsen_ioctl,*/
  1074. .unlocked_ioctl = hwmsen_unlocked_ioctl,
  1075. };
  1076. /*----------------------------------------------------------------------------*/
  1077. static int hwmsen_probe(void)
  1078. {
  1079. int err;
  1080. /* HWM_LOG("+++++++++++++++++hwmsen_probe!!\n"); */
  1081. HWM_FUN(f);
  1082. init_static_data();
  1083. hwm_obj = hwmsen_alloc_object();
  1084. if (!hwm_obj) {
  1085. err = -ENOMEM;
  1086. HWM_ERR("unable to allocate devobj!\n");
  1087. goto exit_alloc_data_failed;
  1088. }
  1089. hwm_obj->idev = input_allocate_device();
  1090. if (!hwm_obj->idev) {
  1091. err = -ENOMEM;
  1092. HWM_ERR("unable to allocate input device!\n");
  1093. goto exit_alloc_input_dev_failed;
  1094. }
  1095. set_bit(EV_REL, hwm_obj->idev->evbit);
  1096. set_bit(EV_SYN, hwm_obj->idev->evbit);
  1097. input_set_capability(hwm_obj->idev, EV_REL, EVENT_TYPE_SENSOR);
  1098. input_set_capability(hwm_obj->idev, EV_REL, EVENT_TYPE_SENSOR_EXT);
  1099. hwm_obj->idev->name = HWM_INPUTDEV_NAME;
  1100. err = input_register_device(hwm_obj->idev);
  1101. if (err) {
  1102. HWM_ERR("unable to register input device!\n");
  1103. goto exit_input_register_device_failed;
  1104. }
  1105. input_set_drvdata(hwm_obj->idev, hwm_obj);
  1106. hwm_obj->mdev.minor = MISC_DYNAMIC_MINOR;
  1107. hwm_obj->mdev.name = HWM_SENSOR_DEV_NAME;
  1108. hwm_obj->mdev.fops = &hwmsen_fops;
  1109. err = misc_register(&hwm_obj->mdev);
  1110. if (err) {
  1111. HWM_ERR("unable to register sensor device!!\n");
  1112. goto exit_misc_register_failed;
  1113. }
  1114. dev_set_drvdata(hwm_obj->mdev.this_device, hwm_obj);
  1115. if (hwmsen_create_attr(hwm_obj->mdev.this_device) != 0) {
  1116. HWM_ERR("unable to create attributes!!\n");
  1117. goto exit_hwmsen_create_attr_failed;
  1118. }
  1119. #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
  1120. /* add for fix resume bug */
  1121. atomic_set(&(hwm_obj->early_suspend), 0);
  1122. hwm_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
  1123. hwm_obj->early_drv.suspend = hwmsen_early_suspend,
  1124. hwm_obj->early_drv.resume = hwmsen_late_resume,
  1125. register_early_suspend(&hwm_obj->early_drv);
  1126. #ifdef CONFIG_PM_WAKELOCKS
  1127. wakeup_source_init(&(hwm_obj->read_data_wake_lock), "read_data_wake_lock");
  1128. #else
  1129. wake_lock_init(&(hwm_obj->read_data_wake_lock), WAKE_LOCK_SUSPEND, "read_data_wake_lock");
  1130. #endif
  1131. /* add for fix resume bug end*/
  1132. #endif /*#if defined(CONFIG_HAS_EARLYSUSPEND)*/
  1133. return 0;
  1134. exit_hwmsen_create_attr_failed:
  1135. exit_misc_register_failed:
  1136. /* exit_get_hwmsen_info_failed:*/
  1137. exit_input_register_device_failed:
  1138. input_free_device(hwm_obj->idev);
  1139. exit_alloc_input_dev_failed:
  1140. kfree(hwm_obj);
  1141. exit_alloc_data_failed:
  1142. return err;
  1143. }
  1144. /*----------------------------------------------------------------------------*/
  1145. static int hwmsen_remove(void)
  1146. {
  1147. HWM_FUN(f);
  1148. input_unregister_device(hwm_obj->idev);
  1149. hwmsen_delete_attr(hwm_obj->mdev.this_device);
  1150. misc_deregister(&hwm_obj->mdev);
  1151. kfree(hwm_obj);
  1152. return 0;
  1153. }
  1154. #if defined(CONFIG_HAS_EARLYSUSPEND)
  1155. static void hwmsen_early_suspend(struct early_suspend *h)
  1156. {
  1157. /*HWM_FUN(f);*/
  1158. atomic_set(&(hwm_obj->early_suspend), 1);
  1159. HWM_LOG(" hwmsen_early_suspend ok------->hwm_obj->early_suspend=%d.\n",
  1160. atomic_read(&hwm_obj->early_suspend));
  1161. }
  1162. /*----------------------------------------------------------------------------*/
  1163. static void hwmsen_late_resume(struct early_suspend *h)
  1164. {
  1165. /*HWM_FUN(f);*/
  1166. atomic_set(&(hwm_obj->early_suspend), 0);
  1167. HWM_LOG(" hwmsen_late_resume ok------->hwm_obj->early_suspend=%d.\n",
  1168. atomic_read(&hwm_obj->early_suspend));
  1169. }
  1170. #endif /*#if defined(CONFIG_HAS_EARLYSUSPEND)*/
  1171. /*----------------------------------------------------------------------------*/
  1172. #if defined(CONFIG_MTK_AUTO_DETECT_MAGNETOMETER)
  1173. int hwmsen_msensor_remove(struct platform_device *pdev)
  1174. {
  1175. int err = 0;
  1176. int i = 0;
  1177. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1178. if (0 == strcmp(msensor_name, msensor_init_list[i]->name)) {
  1179. if (NULL == msensor_init_list[i]->uninit) {
  1180. HWM_LOG(" hwmsen_msensor_remove null pointer.\n");
  1181. return -1;
  1182. }
  1183. msensor_init_list[i]->uninit();
  1184. }
  1185. }
  1186. return 0;
  1187. }
  1188. static int msensor_probe(struct platform_device *pdev)
  1189. {
  1190. int i = 0;
  1191. int err = 0;
  1192. HWM_LOG(" msensor_probe +\n");
  1193. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1194. if (NULL != msensor_init_list[i]) {
  1195. err = msensor_init_list[i]->init();
  1196. if (0 == err) {
  1197. strcpy(msensor_name, msensor_init_list[i]->name);
  1198. HWM_LOG(" msensor %s probe ok\n", msensor_name);
  1199. break;
  1200. }
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. static struct platform_driver msensor_driver = {
  1206. .probe = msensor_probe,
  1207. .remove = hwmsen_msensor_remove,
  1208. .driver = {
  1209. .name = "msensor",
  1210. /* .owner = THIS_MODULE,*/
  1211. }
  1212. };
  1213. int hwmsen_msensor_add(struct sensor_init_info *obj)
  1214. {
  1215. int err = 0;
  1216. int i = 0;
  1217. HWM_FUN(f);
  1218. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1219. if (NULL == msensor_init_list[i]) {
  1220. msensor_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1221. if (NULL == msensor_init_list[i]) {
  1222. HWM_ERR("kzalloc error");
  1223. return -1;
  1224. }
  1225. obj->platform_diver_addr = &msensor_driver;
  1226. msensor_init_list[i] = obj;
  1227. break;
  1228. }
  1229. }
  1230. return err;
  1231. }
  1232. EXPORT_SYMBOL_GPL(hwmsen_msensor_add);
  1233. #endif
  1234. #if defined(CONFIG_MTK_AUTO_DETECT_ACCELEROMETER)
  1235. int hwmsen_gsensor_remove(struct platform_device *pdev)
  1236. {
  1237. int i = 0;
  1238. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1239. if (0 == strcmp(gsensor_name, gsensor_init_list[i]->name)) {
  1240. if (NULL == gsensor_init_list[i]->uninit) {
  1241. HWM_LOG(" hwmsen_gsensor_remove null pointer +\n");
  1242. return -1;
  1243. }
  1244. gsensor_init_list[i]->uninit();
  1245. }
  1246. }
  1247. return 0;
  1248. }
  1249. static int gsensor_probe(struct platform_device *pdev)
  1250. {
  1251. int i = 0;
  1252. int err = 0;
  1253. HWM_LOG(" gsensor_probe +\n");
  1254. /* */
  1255. /*
  1256. for(i = 0; i < MAX_CHOOSE_G_NUM; i++)
  1257. {
  1258. HWM_LOG(" gsensor_init_list[i]=%d\n",gsensor_init_list[i]);
  1259. }
  1260. */
  1261. /* */
  1262. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1263. HWM_LOG(" i=%d\n", i);
  1264. if (0 != gsensor_init_list[i]) {
  1265. HWM_LOG(" !!!!!!!!\n");
  1266. err = gsensor_init_list[i]->init();
  1267. if (0 == err) {
  1268. strcpy(gsensor_name, gsensor_init_list[i]->name);
  1269. HWM_LOG(" gsensor %s probe ok\n", gsensor_name);
  1270. break;
  1271. }
  1272. }
  1273. }
  1274. if (i == MAX_CHOOSE_G_NUM)
  1275. HWM_LOG(" gsensor probe fail\n");
  1276. return 0;
  1277. }
  1278. static struct platform_driver gsensor_driver = {
  1279. .probe = gsensor_probe,
  1280. .remove = hwmsen_gsensor_remove,
  1281. .driver = {
  1282. .name = "gsensor",
  1283. /* .owner = THIS_MODULE,*/
  1284. }
  1285. };
  1286. int hwmsen_gsensor_add(struct sensor_init_info *obj)
  1287. {
  1288. int err = 0;
  1289. int i = 0;
  1290. HWM_FUN(f);
  1291. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1292. if (NULL == gsensor_init_list[i]) {
  1293. gsensor_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1294. if (NULL == gsensor_init_list[i]) {
  1295. HWM_ERR("kzalloc error");
  1296. return -1;
  1297. }
  1298. obj->platform_diver_addr = &gsensor_driver;
  1299. gsensor_init_list[i] = obj;
  1300. break;
  1301. }
  1302. }
  1303. return err;
  1304. }
  1305. EXPORT_SYMBOL_GPL(hwmsen_gsensor_add);
  1306. #endif
  1307. #if defined(CONFIG_MTK_AUTO_DETECT_ALSPS)
  1308. int hwmsen_alsps_sensor_remove(struct platform_device *pdev)
  1309. {
  1310. int err = 0;
  1311. int i = 0;
  1312. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1313. if (0 == strcmp(alsps_name, alsps_init_list[i]->name)) {
  1314. if (NULL == alsps_init_list[i]->uninit) {
  1315. HWM_LOG(" hwmsen_alsps_sensor_remove null pointer.\n");
  1316. return -1;
  1317. }
  1318. alsps_init_list[i]->uninit();
  1319. }
  1320. }
  1321. return 0;
  1322. }
  1323. static int alsps_sensor_probe(struct platform_device *pdev)
  1324. {
  1325. int i = 0;
  1326. int err = 0;
  1327. HWM_LOG(" als_ps sensor_probe +\n");
  1328. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1329. if (NULL != alsps_init_list[i]) {
  1330. err = alsps_init_list[i]->init();
  1331. if (0 == err) {
  1332. strcpy(alsps_name, alsps_init_list[i]->name);
  1333. HWM_LOG(" alsps sensor %s probe ok\n", alsps_name);
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. return 0;
  1339. }
  1340. static struct platform_driver alsps_sensor_driver = {
  1341. .probe = alsps_sensor_probe,
  1342. .remove = hwmsen_alsps_sensor_remove,
  1343. .driver = {
  1344. .name = "als_ps",
  1345. }
  1346. };
  1347. int hwmsen_alsps_sensor_add(struct sensor_init_info *obj)
  1348. {
  1349. int err = 0;
  1350. int i = 0;
  1351. HWM_FUN(f);
  1352. for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
  1353. if (NULL == alsps_init_list[i]) {
  1354. alsps_init_list[i] = kzalloc(sizeof(struct sensor_init_info), GFP_KERNEL);
  1355. if (NULL == alsps_init_list[i]) {
  1356. HWM_ERR("kzalloc error");
  1357. return -1;
  1358. }
  1359. obj->platform_diver_addr = &alsps_sensor_driver;
  1360. alsps_init_list[i] = obj;
  1361. break;
  1362. }
  1363. }
  1364. return err;
  1365. }
  1366. EXPORT_SYMBOL_GPL(hwmsen_alsps_sensor_add);
  1367. #endif
  1368. /*----------------------------------------------------------------------------*/
  1369. static int __init hwmsen_init(void)
  1370. {
  1371. HWM_FUN(f);
  1372. if (0 != hwmsen_probe()) {
  1373. HWM_ERR("failed to register sensor driver");
  1374. return -ENODEV;
  1375. }
  1376. #if defined(CONFIG_MTK_AUTO_DETECT_ACCELEROMETER)
  1377. if (platform_driver_register(&gsensor_driver)) {
  1378. HWM_ERR("failed to register gensor driver");
  1379. return -ENODEV;
  1380. }
  1381. #endif
  1382. #if defined(CONFIG_MTK_AUTO_DETECT_MAGNETOMETER)
  1383. if (platform_driver_register(&msensor_driver)) {
  1384. HWM_ERR("failed to register mensor driver");
  1385. return -ENODEV;
  1386. }
  1387. #endif
  1388. #if defined(CONFIG_MTK_AUTO_DETECT_ALSPS)
  1389. if (platform_driver_register(&alsps_sensor_driver)) {
  1390. HWM_ERR("failed to register alsps_sensor_driver driver");
  1391. return -ENODEV;
  1392. }
  1393. #endif
  1394. return 0;
  1395. }
  1396. /*----------------------------------------------------------------------------*/
  1397. static void __exit hwmsen_exit(void)
  1398. {
  1399. hwmsen_remove();
  1400. }
  1401. /*----------------------------------------------------------------------------*/
  1402. late_initcall(hwmsen_init);
  1403. MODULE_LICENSE("GPL");
  1404. MODULE_DESCRIPTION("sensor device driver");
  1405. MODULE_AUTHOR("MTK");