Advertisement
Guest User

Untitled

a guest
May 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 72.04 KB | None | 0 0
  1. /*
  2. * Author: chester hsu (TXC) <chesterhsu@txc.com.tw>
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/gpio.h>
  19. #include <linux/miscdevice.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/delay.h>
  22. #include <linux/input.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/kobject.h>
  25. #include <linux/platform_device.h>
  26. #include <asm/atomic.h>
  27. #include <asm/io.h>
  28. #include <cust_alsps.h>
  29. #include <alsps.h>
  30. #include "pa12201001.h"
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/sched.h>
  35.  
  36. /******************************************************************************
  37. * configuration
  38. *******************************************************************************/
  39. /**Global Variable**/
  40. u8 ps_count=0;
  41. u8 ps_offset=0;
  42. u16 als_count=0;
  43.  
  44. #ifdef CUSTOM_KERNEL_SENSORHUB
  45. #include <SCP_sensorHub.h>
  46. #endif
  47. /*----------------------------------------------------------------------------*/
  48. #define PA12201001_DEV_NAME "pa12201001"
  49. /*----------------------------------------------------------------------------*/
  50. #define APS_TAG "[ALS/PS] "
  51. #define APS_FUN(f) printk(APS_TAG"%s\n", __FUNCTION__)
  52. #define APS_ERR(fmt, args...) printk(APS_TAG"%s %d : "fmt, __FUNCTION__, __LINE__, ##args)
  53. #define APS_LOG(fmt, args...) printk(APS_TAG fmt, ##args)
  54. #define APS_DBG(fmt, args...) printk(APS_TAG fmt, ##args)
  55.  
  56. #define I2C_FLAG_WRITE 0
  57. #define I2C_FLAG_READ 1
  58.  
  59. #ifndef AGOLD_DEFINED_ALSPS_PA12201001_PS_THD_VALUE
  60. #define AGOLD_DEFINED_ALSPS_PA12201001_PS_THD_VALUE "0x60"
  61. #endif
  62. #define AGOLD_PA12201001_THRESHOLD_MIN 0X30
  63. extern long agold_strtol(char *str);
  64.  
  65. static unsigned int PA12201001_Current_Ps_Thd_Value = 0x60;
  66. static int intr_flag_value = 0;
  67. #if defined(AGOLD_PROX_CALI_ENABLE)
  68. static DEFINE_SPINLOCK(ps_cali_lock);
  69. #endif
  70.  
  71. static int g_interrupt_flag = 0;
  72.  
  73. static int pa12201001_local_init(void);
  74. static int pa12201001_remove(void);
  75. static int pa12201001_init_flag =-1; // 0<==>OK -1 <==> fail
  76. static struct alsps_init_info pa12201001_init_info = {
  77. .name = "pa12201001",
  78. .init = pa12201001_local_init,
  79. .uninit = pa12201001_remove,
  80.  
  81. };
  82.  
  83. /*----------------------------------------------------------------------------*/
  84. static int pa12201001_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  85. static int pa12201001_i2c_remove(struct i2c_client *client);
  86. static int pa12201001_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
  87. static int pa12201001_i2c_suspend(struct device *dev);
  88. static int pa12201001_i2c_resume(struct device *dev);
  89.  
  90. static const struct i2c_device_id pa12201001_i2c_id[] = { {PA12201001_DEV_NAME, 0}, {} };
  91.  
  92. static int pa12201001_read_als(struct i2c_client *client, u16 *data);
  93. static int pa12201001_read_ps(struct i2c_client *client, u8 *data);
  94. /*----------------------------------------------------------------------------*/
  95. struct pa12201001_priv {
  96. struct alsps_hw hw;
  97. struct i2c_client *client;
  98. struct work_struct eint_work;
  99.  
  100. /*misc*/
  101. u16 als_modulus;
  102. atomic_t i2c_retry;
  103. atomic_t als_suspend;
  104. atomic_t als_debounce; /*debounce time after enabling als*/
  105. atomic_t als_deb_on; /*indicates if the debounce is on*/
  106. #ifdef CONFIG_64BIT
  107. atomic64_t als_deb_end; /*the jiffies representing the end of debounce */
  108. #else
  109. atomic_t als_deb_end; /*the jiffies representing the end of debounce */
  110. #endif
  111. atomic_t ps_mask; /*mask ps: always return far away*/
  112. atomic_t ps_debounce; /*debounce time after enabling ps*/
  113. atomic_t ps_deb_on; /*indicates if the debounce is on*/
  114. #ifdef CONFIG_64BIT
  115. atomic64_t ps_deb_end; /*the jiffies representing the end of debounce */
  116. #else
  117. atomic_t ps_deb_end; /*the jiffies representing the end of debounce */
  118. #endif
  119. atomic_t ps_suspend;
  120. atomic_t trace;
  121.  
  122. #ifdef CUSTOM_KERNEL_SENSORHUB
  123. struct work_struct init_done_work;
  124. #endif
  125. struct device_node *irq_node;
  126. int irq;
  127.  
  128. /*data*/
  129. u16 als;
  130. u8 ps;
  131. u8 _align;
  132. u16 als_level_num;
  133. u16 als_value_num;
  134. u32 als_level[C_CUST_ALS_LEVEL-1];
  135. u32 als_value[C_CUST_ALS_LEVEL];
  136.  
  137. atomic_t als_cmd_val; /*the cmd value can't be read, stored in ram*/
  138. atomic_t ps_cmd_val; /*the cmd value can't be read, stored in ram*/
  139. atomic_t ps_thd_val_high; /*the cmd value can't be read, stored in ram*/
  140. atomic_t ps_thd_val_low; /*the cmd value can't be read, stored in ram*/
  141. atomic_t als_thd_val_high; /*the cmd value can't be read, stored in ram*/
  142. atomic_t als_thd_val_low; /*the cmd value can't be read, stored in ram*/
  143. atomic_t ps_thd_val;
  144. ulong enable; /*enable mask*/
  145. ulong pending_intr; /*pending interrupt*/
  146. int ps_cali;
  147.  
  148. };
  149. /*----------------------------------------------------------------------------*/
  150. #ifdef CONFIG_OF
  151. static const struct of_device_id alsps_of_match[] = {
  152. {.compatible = "mediatek,alsps"},
  153. {},
  154. };
  155. #endif
  156.  
  157. #ifdef CONFIG_PM_SLEEP
  158. static const struct dev_pm_ops pa12201001_pm_ops = {
  159. SET_SYSTEM_SLEEP_PM_OPS(pa12201001_i2c_suspend, pa12201001_i2c_resume)
  160. };
  161. #endif
  162.  
  163. static struct i2c_driver pa12201001_i2c_driver = {
  164. .probe = pa12201001_i2c_probe,
  165. .remove = pa12201001_i2c_remove,
  166. .detect = pa12201001_i2c_detect,
  167. .id_table = pa12201001_i2c_id,
  168. .driver = {
  169. .name = PA12201001_DEV_NAME,
  170. #ifdef CONFIG_PM_SLEEP
  171. .pm = &pa12201001_pm_ops,
  172. #endif
  173. #ifdef CONFIG_OF
  174. .of_match_table = alsps_of_match,
  175. #endif
  176. },
  177. };
  178.  
  179.  
  180. /*----------------------------------------------------------------------------*/
  181.  
  182. static struct i2c_client *pa12201001_i2c_client = NULL;
  183. static struct pa12201001_priv *g_pa12201001_ptr = NULL; //Trash
  184. static struct pa12201001_priv *pa12201001_obj = NULL;
  185.  
  186. /*----------------------------------------------------------------------------*/
  187.  
  188. static DEFINE_MUTEX(pa12201001_mutex); //TBD
  189.  
  190. /*----------------------------------------------------------------------------*/
  191. typedef enum {
  192. CMC_BIT_ALS = 1,
  193. CMC_BIT_PS = 2,
  194. }CMC_BIT;
  195. /*-----------------------------CMC for debugging-------------------------------*/
  196. typedef enum {
  197. CMC_TRC_ALS_DATA= 0x0001,
  198. CMC_TRC_PS_DATA = 0x0002,
  199. CMC_TRC_EINT = 0x0004,
  200. CMC_TRC_IOCTL = 0x0008,
  201. CMC_TRC_I2C = 0x0010,
  202. CMC_TRC_CVT_ALS = 0x0020,
  203. CMC_TRC_CVT_PS = 0x0040,
  204. CMC_TRC_DEBUG = 0x8000,
  205. } CMC_TRC;
  206. /*-----------------------------------------------------------------------------*/
  207. static int pa12201001_enable_ps(struct i2c_client *client, int enable)
  208. {
  209. struct pa12201001_priv *obj = i2c_get_clientdata(client);
  210. int res;
  211. u8 regdata=0;
  212. u8 sendvalue=0;
  213.  
  214. if(enable == 1)
  215. {
  216. APS_LOG("pa12201001 enable ps sensor\n");
  217. res=hwmsen_read_byte(client,REG_CFG0,&regdata); //Read Status
  218. if(res<0)
  219. {
  220. APS_ERR("i2c_read function err\n");
  221. return -1;
  222. }
  223. else
  224. {
  225. APS_LOG("CFG0 Status: %d\n",regdata);
  226. sendvalue=regdata & 0xFD; //clear bit
  227. sendvalue=sendvalue | 0x02; //0x02 PS Flag
  228. res=hwmsen_write_byte(client,REG_CFG0,sendvalue); //Write PS enable
  229. if(res<0)
  230. {
  231. APS_ERR("i2c_write function err\n");
  232. return res;
  233. }
  234. }
  235.  
  236. atomic_set(&obj->ps_deb_on, 1);
  237. atomic_set(&obj->ps_deb_end, jiffies+atomic_read(&obj->ps_debounce)/(1000/HZ));
  238.  
  239. }
  240. else
  241. {
  242.  
  243. APS_LOG("pa12201001 disaple ps sensor\n");
  244. res=hwmsen_read_byte(client,REG_CFG0,&regdata); //Read Status
  245. if(res<0)
  246. {
  247. APS_ERR("i2c_read function err\n");
  248. return res;
  249. }
  250. else
  251. {
  252. APS_LOG("CFG0 Status: %d\n",regdata);
  253. sendvalue=regdata & 0xFD; //clear bit
  254.  
  255. res=hwmsen_write_byte(client,REG_CFG0,sendvalue); //Write PS enable
  256. if(res<0)
  257. {
  258. APS_ERR("i2c_write function err\n");
  259. return res;
  260. }
  261. }
  262.  
  263. atomic_set(&obj->ps_deb_on, 0);
  264. }
  265.  
  266. return 0;
  267. }
  268. /********************************************************************/
  269. static int pa12201001_enable_als(struct i2c_client *client, int enable)
  270. {
  271. struct pa12201001_priv *obj = i2c_get_clientdata(client);
  272. int res;
  273. u8 regdata=0;
  274. u8 sendvalue=0;
  275.  
  276. if(enable == 1)
  277. {
  278. APS_LOG("pa12201001 enable als sensor\n");
  279. res=hwmsen_read_byte(client,REG_CFG0,&regdata); //Read Status
  280. if(res<0)
  281. {
  282. APS_ERR("i2c_read function err\n");
  283. return -1;
  284. }
  285. else
  286. {
  287. APS_LOG("CFG0 Status: %d\n",regdata);
  288. sendvalue=regdata & 0xFE; //clear bit
  289. sendvalue=sendvalue | 0x01; //0x02 PS Flag
  290. res=hwmsen_write_byte(client,REG_CFG0,sendvalue); //Write ALS enable
  291. if(res<0)
  292. {
  293. APS_ERR("i2c_write function err\n");
  294. return res;
  295. }
  296. }
  297.  
  298. atomic_set(&obj->als_deb_on, 1);
  299. atomic_set(&obj->als_deb_end, jiffies+atomic_read(&obj->als_debounce)/(1000/HZ));
  300.  
  301. }
  302. else
  303. {
  304.  
  305. APS_LOG("pa12201001 disaple als sensor\n");
  306. res=hwmsen_read_byte(client,REG_CFG0,&regdata); //Read Status
  307. if(res<0)
  308. {
  309. APS_ERR("i2c_read function err\n");
  310. return res;
  311.  
  312. }
  313. else
  314. {
  315. APS_LOG("CFG0 Status: %d\n",regdata);
  316. sendvalue=regdata & 0xFE; //clear bit
  317.  
  318. res=hwmsen_write_byte(client,REG_CFG0,sendvalue); //Write PS enable
  319. if(res<0)
  320. {
  321. APS_ERR("i2c_write function err\n");
  322. return res;
  323. }
  324. }
  325.  
  326. atomic_set(&obj->als_deb_on, 0);
  327. }
  328.  
  329. return 0;
  330.  
  331. }
  332. /********************************************************************/
  333. static int pa12201001_read_ps(struct i2c_client *client, u8 *data)
  334. {
  335. int res;
  336.  
  337. APS_FUN(f);
  338. res = hwmsen_read_byte(client,REG_PS_DATA,data); //Read PS Data
  339. if(res < 0)
  340. {
  341. APS_ERR("i2c_send function err\n");
  342. }
  343. APS_LOG("[mcz] read ps = 0x%x\n",*data);
  344. return res;
  345. }
  346. /********************************************************************/
  347. static int pa12201001_read_als(struct i2c_client *client, u16 *data)
  348. {
  349. int res;
  350. u8 dataLSB;
  351. u8 dataMSB;
  352.  
  353. APS_FUN(f);
  354.  
  355. res = hwmsen_read_byte(client,REG_ALS_DATA_LSB,&dataLSB); //Read ALS Data LSB
  356. if(res < 0)
  357. {
  358. APS_ERR("i2c_send function err\n");
  359. return res;
  360. }
  361.  
  362. res = hwmsen_read_byte(client,REG_ALS_DATA_MSB,&dataMSB); //Read ALS Data MSB
  363. if(res < 0)
  364. {
  365. APS_ERR("i2c_send function err\n");
  366. return res;
  367. }
  368.  
  369. APS_LOG("PA12201001_ALS_DATA value_low = %x, value_high = %x\n",dataLSB,dataMSB);
  370.  
  371. *data = ((dataMSB<<8)|dataLSB);
  372.  
  373. return 0;
  374.  
  375. }
  376. /**Change to lux ****************************************************/
  377. static int pa12201001_get_ps_value(struct pa12201001_priv *obj, u8 ps)
  378. {
  379. int val;
  380. int invalid;
  381. int val_temp;
  382. int mask = atomic_read(&obj->ps_mask);
  383.  
  384. APS_LOG("[mcz] get ps = 0x%x\n",ps);
  385. printk("pa12201001_get_ps_value, ps = %d\n",ps);
  386.  
  387. if(ps >= PA12201001_Current_Ps_Thd_Value)
  388. {
  389. val = 0; /*close*/
  390. val_temp = 0;
  391. intr_flag_value = 0;
  392. }
  393. else if(ps < PA12201001_Current_Ps_Thd_Value)
  394. {
  395. val = 1; /*far away*/
  396. val_temp = 1;
  397. intr_flag_value = 1;
  398. }
  399. else
  400. {
  401. val = val_temp;
  402. }
  403.  
  404. APS_LOG("[mcz]atomic_read(&obj->ps_deb_on) = 0x%x\n",atomic_read(&obj->ps_deb_on));
  405. APS_LOG("[mcz]atomic_read(&obj->ps_suspend) = 0x%x\n",atomic_read(&obj->ps_suspend));
  406. if(atomic_read(&obj->ps_suspend))
  407. {
  408. invalid = 1;
  409. }
  410.  
  411. else if(atomic_read(&obj->ps_deb_on) == 1)
  412. {
  413. unsigned long endt = atomic_read(&obj->ps_deb_end);
  414. APS_LOG("[mcz] endt = 0x%lu\n",endt);
  415. if(time_after(jiffies, endt))
  416. {
  417. atomic_set(&obj->ps_deb_on, 0);
  418. }
  419.  
  420. if (atomic_read(&obj->ps_deb_on) == 1)
  421. {
  422. invalid = 1;
  423. }
  424.  
  425. }
  426. APS_LOG("[mcz] invalid = 0x%x \n",invalid);
  427. if(!invalid)
  428. {
  429. APS_LOG("[mcz] !invalid \n");
  430. if(unlikely(atomic_read(&obj->trace) & CMC_TRC_CVT_PS))
  431. {
  432. if(mask)
  433. {
  434. APS_DBG("PS: %05d => %05d [M] \n", ps, val);
  435. }
  436. else
  437. {
  438. APS_DBG("PS: %05d => %05d\n", ps, val);
  439. }
  440. }
  441. if(test_bit(CMC_BIT_PS, &obj->enable) == 1)
  442. {
  443. //if ps is disable do not report value
  444. APS_DBG("PS: not enable and do not report this value\n");
  445. return -1;
  446. }
  447. else
  448. {
  449. APS_LOG("[mcz] return val = %d\n",val);
  450. return val;
  451. }
  452.  
  453. }
  454. else
  455. {
  456. if(unlikely(atomic_read(&obj->trace) & CMC_TRC_CVT_PS))
  457. {
  458. APS_DBG("PS: %05d => %05d (-1)\n", ps, val);
  459. }
  460. return -1;
  461. }
  462. }
  463. /**Change to far/near************************************************/
  464. static int pa12201001_get_als_value(struct pa12201001_priv *obj, u16 als)
  465. {
  466. int idx;
  467. int invalid = 0;
  468. for(idx = 0; idx < obj->als_level_num; idx++)
  469. {
  470. if(als < obj->hw.als_level[idx])
  471. {
  472. break;
  473. }
  474. }
  475. if(idx >= obj->als_value_num)
  476. {
  477. APS_ERR("exceed range\n");
  478. idx = obj->als_value_num - 1;
  479. }
  480.  
  481. if(atomic_read(&obj->als_deb_on) == 1)
  482. {
  483. unsigned long endt = atomic_read(&obj->als_deb_end);
  484. if(time_after(jiffies, endt))
  485. {
  486. atomic_set(&obj->als_deb_on, 0);
  487. }
  488.  
  489. if(atomic_read(&obj->als_deb_on) == 1)
  490. {
  491. invalid = 1;
  492. }
  493. }
  494.  
  495. if(!invalid)
  496. {
  497. if (atomic_read(&obj->trace) & CMC_TRC_CVT_ALS)
  498. {
  499. APS_DBG("ALS: %05d => %05d\n", als, obj->hw.als_value[idx]);
  500. }
  501.  
  502. return obj->hw.als_value[idx];
  503. }
  504. else
  505. {
  506. if(atomic_read(&obj->trace) & CMC_TRC_CVT_ALS)
  507. {
  508. APS_DBG("ALS: %05d => %05d (-1)\n", als, obj->hw.als_value[idx]);
  509. }
  510. return -1;
  511. }
  512.  
  513. }
  514.  
  515. static int pa12201001_get_ps_raw_data(int *psdata)
  516. {
  517. u8 raw_ps = 0;
  518. pa12201001_read_ps(pa12201001_obj->client, &raw_ps );
  519. *psdata = raw_ps;
  520. return 0;
  521. }
  522.  
  523. static int pa12201001_get_als_raw_data(int *psdata)
  524. {
  525. u16 raw_als = 0;
  526. pa12201001_read_als(pa12201001_obj->client, &raw_als );
  527. *psdata = raw_als;
  528. return 0;
  529. }
  530.  
  531.  
  532. /*-------------------------------attribute file for debugging----------------------------------*/
  533.  
  534. /******************************************************************************
  535. * Sysfs attributes
  536. *******************************************************************************/
  537. static ssize_t pa12201001_show_config(struct device_driver *ddri, char *buf)
  538. {
  539. ssize_t res;
  540.  
  541. if(!pa12201001_obj)
  542. {
  543. APS_ERR("pa12201001_obj is null!!\n");
  544. return 0;
  545. }
  546.  
  547. res = snprintf(buf, PAGE_SIZE, "(%d %d %d %d %d)\n",
  548. atomic_read(&pa12201001_obj->i2c_retry), atomic_read(&pa12201001_obj->als_debounce),
  549. atomic_read(&pa12201001_obj->ps_mask), atomic_read(&pa12201001_obj->ps_thd_val), atomic_read(&pa12201001_obj->ps_debounce));
  550. return res;
  551. }
  552. /*----------------------------------------------------------------------------*/
  553. static ssize_t pa12201001_store_config(struct device_driver *ddri, const char *buf, size_t count)
  554. {
  555. int retry, als_deb, ps_deb, mask, thres;
  556. if(!pa12201001_obj)
  557. {
  558. APS_ERR("pa12201001_obj is null!!\n");
  559. return 0;
  560. }
  561.  
  562. if(sscanf(buf, "%d %d %d %d %d", &retry, &als_deb, &mask, &thres, &ps_deb) == 5)
  563. {
  564. atomic_set(&pa12201001_obj->i2c_retry, retry);
  565. atomic_set(&pa12201001_obj->als_debounce, als_deb);
  566. atomic_set(&pa12201001_obj->ps_mask, mask);
  567. atomic_set(&pa12201001_obj->ps_thd_val, thres);
  568. atomic_set(&pa12201001_obj->ps_debounce, ps_deb);
  569. }
  570. else
  571. {
  572. APS_ERR("invalid content: '%s', length = %ld\n", buf, count);
  573. }
  574. return count;
  575. }
  576. /*----------------------------------------------------------------------------*/
  577. static ssize_t pa12201001_show_trace(struct device_driver *ddri, char *buf)
  578. {
  579. ssize_t res;
  580. if(!pa12201001_obj)
  581. {
  582. APS_ERR("pa12201001_obj is null!!\n");
  583. return 0;
  584. }
  585.  
  586. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&pa12201001_obj->trace));
  587. return res;
  588. }
  589. /*----------------------------------------------------------------------------*/
  590. static ssize_t pa12201001_store_trace(struct device_driver *ddri, const char *buf, size_t count)
  591. {
  592. int trace;
  593. if(!pa12201001_obj)
  594. {
  595. APS_ERR("pa12201001_obj is null!!\n");
  596. return 0;
  597. }
  598.  
  599. if(sscanf(buf, "0x%x", &trace) == 1)
  600. {
  601. atomic_set(&pa12201001_obj->trace, trace);
  602. }
  603. else
  604. {
  605. APS_ERR("invalid content: '%s', length = %ld\n", buf, count);
  606. }
  607. return count;
  608. }
  609. /*----------------------------------------------------------------------------*/
  610. static ssize_t pa12201001_show_als(struct device_driver *ddri, char *buf)
  611. {
  612. int res;
  613.  
  614. if(!pa12201001_obj)
  615. {
  616. APS_ERR("pa12201001_obj is null!!\n");
  617. return 0;
  618. }
  619.  
  620. res = pa12201001_read_als(pa12201001_obj->client, &pa12201001_obj->als);
  621. if(res)
  622. {
  623. return snprintf(buf, PAGE_SIZE, "ERROR: %d\n", res);
  624. }
  625. else
  626. {
  627. return snprintf(buf, PAGE_SIZE, "0x%04X\n", pa12201001_obj->als);
  628. }
  629. }
  630. /*----------------------------------------------------------------------------*/
  631. static ssize_t pa12201001_show_ps(struct device_driver *ddri, char *buf)
  632. {
  633. int res;
  634. ssize_t dat;
  635. u8 raw_ps;
  636. if(!pa12201001_obj)
  637. {
  638. APS_ERR("cm3623_obj is null!!\n");
  639. return 0;
  640. }
  641.  
  642. res = pa12201001_read_ps(pa12201001_obj->client, &raw_ps);
  643. if(res)
  644. {
  645. return snprintf(buf, PAGE_SIZE, "ERROR: %d\n", res);
  646. }
  647. else
  648. {
  649. #if defined(AGOLD_PROX_CALI_ENABLE)
  650. if( raw_ps > PA12201001_Current_Ps_Thd_Value)
  651. #else
  652. if( raw_ps > (ssize_t)atomic_read(&pa12201001_obj->ps_thd_val_high))
  653. #endif
  654. {
  655. dat = 0x80;
  656. }
  657. else
  658. {
  659. dat = 0x00;
  660. }
  661. return snprintf(buf, PAGE_SIZE, "%ld\n", dat);
  662. }
  663. }
  664. /*----------------------------------------------------------------------------*/
  665. static ssize_t pa12201001_show_reg(struct device_driver *ddri, char *buf)
  666. {
  667. u8 regdata;
  668. int i;
  669. int res=0;
  670. int count=0;
  671. if (!pa12201001_obj)
  672. {
  673. APS_ERR("pa12201001_obj is null!!\n");
  674. return 0;
  675. }
  676. for (i = 0; i < 14; i++)
  677. {
  678. res=hwmsen_read_byte(pa12201001_obj->client,0x00+i,&regdata);
  679. if (res < 0)
  680. {
  681. break;
  682. }
  683. else
  684. {
  685. count+=sprintf(buf+count,"[%x] = (%x)\n",0x00+i,regdata);
  686. }
  687. }
  688. return count;
  689. }
  690. /*----------------------------------------------------------------------------*/
  691. static ssize_t pa12201001_show_send(struct device_driver *ddri, char *buf)
  692. {
  693. return 0;
  694. }
  695. /*----------------------------------------------------------------------------*/
  696. static ssize_t pa12201001_store_send(struct device_driver *ddri, const char *buf, size_t count)
  697. {
  698. int addr, cmd;
  699. u8 dat;
  700.  
  701. if(!pa12201001_obj)
  702. {
  703. APS_ERR("pa12201001_obj is null!!\n");
  704. return 0;
  705. }
  706. else if(sscanf(buf, "%x %x", &addr, &cmd) != 2)
  707. {
  708. APS_ERR("invalid format: '%s'\n", buf);
  709. return 0;
  710. }
  711.  
  712. dat = (u8)cmd;
  713.  
  714. return count;
  715. }
  716. /*----------------------------------------------------------------------------*/
  717. static ssize_t pa12201001_show_recv(struct device_driver *ddri, char *buf)
  718. {
  719. return 0;
  720. }
  721. /*----------------------------------------------------------------------------*/
  722. static ssize_t pa12201001_store_recv(struct device_driver *ddri, const char *buf, size_t count)
  723. {
  724. int addr;
  725.  
  726. if(!pa12201001_obj)
  727. {
  728. APS_ERR("pa12201001_obj is null!!\n");
  729. return 0;
  730. }
  731. else if(1 != sscanf(buf, "%x", &addr))
  732. {
  733. APS_ERR("invalid format: '%s'\n", buf);
  734. return 0;
  735. }
  736.  
  737. return count;
  738. }
  739. /*----------------------------------------------------------------------------*/
  740. static ssize_t pa12201001_show_status(struct device_driver *ddri, char *buf)
  741. {
  742. ssize_t len = 0;
  743.  
  744. if(!pa12201001_obj)
  745. {
  746. APS_ERR("pa12201001_obj is null!!\n");
  747. return 0;
  748. }
  749.  
  750. len += snprintf(buf+len, PAGE_SIZE-len, "CUST: %d, (%d %d)\n",
  751. pa12201001_obj->hw.i2c_num, pa12201001_obj->hw.power_id, pa12201001_obj->hw.power_vol);
  752.  
  753. len += snprintf(buf+len, PAGE_SIZE-len, "REGS: %02X %02X %02X %02lX %02lX\n",
  754. atomic_read(&pa12201001_obj->als_cmd_val), atomic_read(&pa12201001_obj->ps_cmd_val),
  755. atomic_read(&pa12201001_obj->ps_thd_val),pa12201001_obj->enable, pa12201001_obj->pending_intr);
  756.  
  757. len += snprintf(buf+len, PAGE_SIZE-len, "MISC: %d %d\n", atomic_read(&pa12201001_obj->als_suspend), atomic_read(&pa12201001_obj->ps_suspend));
  758.  
  759. return len;
  760. }
  761. /*----------------------------------------------------------------------------*/
  762. #define IS_SPACE(CH) (((CH) == ' ') || ((CH) == '\n'))
  763. /*----------------------------------------------------------------------------*/
  764. static int read_int_from_buf(struct pa12201001_priv *obj, const char* buf, size_t count, u32 data[], int len)
  765. {
  766. int idx = 0;
  767. char *cur = (char*)buf, *end = (char*)(buf+count);
  768.  
  769. while(idx < len)
  770. {
  771. while((cur < end) && IS_SPACE(*cur))
  772. {
  773. cur++;
  774. }
  775.  
  776. if(1 != sscanf(cur, "%d", &data[idx]))
  777. {
  778. break;
  779. }
  780.  
  781. idx++;
  782. while((cur < end) && !IS_SPACE(*cur))
  783. {
  784. cur++;
  785. }
  786. }
  787. return idx;
  788. }
  789. /*----------------------------------------------------------------------------*/
  790. static ssize_t pa12201001_show_alslv(struct device_driver *ddri, char *buf)
  791. {
  792. ssize_t len = 0;
  793. int idx;
  794. if(!pa12201001_obj)
  795. {
  796. APS_ERR("pa12201001_obj is null!!\n");
  797. return 0;
  798. }
  799.  
  800. for(idx = 0; idx < pa12201001_obj->als_level_num; idx++)
  801. {
  802. len += snprintf(buf+len, PAGE_SIZE-len, "%d ", pa12201001_obj->hw.als_level[idx]);
  803. }
  804. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  805. return len;
  806. }
  807. /*----------------------------------------------------------------------------*/
  808. static ssize_t pa12201001_store_alslv(struct device_driver *ddri, const char *buf, size_t count)
  809. {
  810. if(!pa12201001_obj)
  811. {
  812. APS_ERR("pa12201001_obj is null!!\n");
  813. return 0;
  814. }
  815. else if(!strcmp(buf, "def"))
  816. {
  817. memcpy(pa12201001_obj->als_level, pa12201001_obj->hw.als_level, sizeof(pa12201001_obj->als_level));
  818. }
  819. else if (pa12201001_obj->als_level_num != read_int_from_buf(pa12201001_obj, buf, count,
  820. pa12201001_obj->hw.als_level, pa12201001_obj->als_level_num))
  821. {
  822. APS_ERR("invalid format: '%s'\n", buf);
  823. }
  824. return count;
  825. }
  826. /*----------------------------------------------------------------------------*/
  827. static ssize_t pa12201001_show_alsval(struct device_driver *ddri, char *buf)
  828. {
  829. ssize_t len = 0;
  830. int idx;
  831. if(!pa12201001_obj)
  832. {
  833. APS_ERR("pa12201001_obj is null!!\n");
  834. return 0;
  835. }
  836.  
  837. for(idx = 0; idx < pa12201001_obj->als_value_num; idx++)
  838. {
  839. len += snprintf(buf+len, PAGE_SIZE-len, "%d ", pa12201001_obj->hw.als_value[idx]);
  840. }
  841. len += snprintf(buf+len, PAGE_SIZE-len, "\n");
  842. return len;
  843. }
  844.  
  845. static ssize_t pa12201001_store_alsval(struct device_driver *ddri, const char *buf, size_t count)
  846. {
  847. if(!pa12201001_obj)
  848. {
  849. APS_ERR("pa12201001_obj is null!!\n");
  850. return 0;
  851. }
  852. else if(!strcmp(buf, "def"))
  853. {
  854. memcpy(pa12201001_obj->als_value, pa12201001_obj->hw.als_value, sizeof(pa12201001_obj->als_value));
  855. }
  856. else if(pa12201001_obj->als_value_num != read_int_from_buf(pa12201001_obj, buf, count,
  857. pa12201001_obj->hw.als_value, pa12201001_obj->als_value_num))
  858. {
  859. APS_ERR("invalid format: '%s'\n", buf);
  860. }
  861. return count;
  862. }
  863.  
  864. static ssize_t pa12201001_show_chipinfo(struct device_driver *ddri, char *buf)
  865. {
  866. ssize_t len = 0;
  867.  
  868. len += snprintf(buf+len,PAGE_SIZE-len,"PA12201001\n");
  869.  
  870. return len;
  871.  
  872. }
  873.  
  874. /*---Offset At-------------------------------------------------------------------------*/
  875. static ssize_t pa12201001_show_ps_offset(struct device_driver *ddri, char *buf)
  876. {
  877. return 0;
  878. }
  879.  
  880. static ssize_t pa12201001_set_ps_offset(struct device_driver *ddri, const char *buf, size_t count)
  881. {
  882. return 0;
  883. }
  884.  
  885. /*---------------------------------------------------------------------------------------*/
  886. static DRIVER_ATTR(als, S_IWUSR | S_IRUGO, pa12201001_show_als, NULL);
  887. static DRIVER_ATTR(ps, S_IWUSR | S_IRUGO, pa12201001_show_ps, NULL);
  888. static DRIVER_ATTR(config, S_IWUSR | S_IRUGO, pa12201001_show_config, pa12201001_store_config);
  889. static DRIVER_ATTR(alslv, S_IWUSR | S_IRUGO, pa12201001_show_alslv, pa12201001_store_alslv);
  890. static DRIVER_ATTR(alsval, S_IWUSR | S_IRUGO, pa12201001_show_alsval, pa12201001_store_alsval);
  891. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, pa12201001_show_trace, pa12201001_store_trace);
  892. static DRIVER_ATTR(status, S_IWUSR | S_IRUGO, pa12201001_show_status, NULL);
  893. static DRIVER_ATTR(send, S_IWUSR | S_IRUGO, pa12201001_show_send, pa12201001_store_send); // No func
  894. static DRIVER_ATTR(recv, S_IWUSR | S_IRUGO, pa12201001_show_recv, pa12201001_store_recv); // No func
  895. static DRIVER_ATTR(reg, S_IWUSR | S_IRUGO, pa12201001_show_reg, NULL);
  896. static DRIVER_ATTR(setpsoffset, S_IWUSR | S_IRUGO, pa12201001_show_ps_offset,pa12201001_set_ps_offset);
  897. static DRIVER_ATTR(chipinfo, S_IWUSR | S_IRUGO, pa12201001_show_chipinfo, NULL);
  898.  
  899. /*----------------------------------------------------------------------------*/
  900. static struct driver_attribute *pa12201001_attr_list[] = {
  901. &driver_attr_als,
  902. &driver_attr_ps,
  903. &driver_attr_trace, /*trace log*/
  904. &driver_attr_config,
  905. &driver_attr_alslv,
  906. &driver_attr_alsval,
  907. &driver_attr_status,
  908. &driver_attr_send,
  909. &driver_attr_recv,
  910. &driver_attr_reg,
  911. &driver_attr_setpsoffset,
  912. &driver_attr_chipinfo,
  913. };
  914.  
  915. /*----------------------------------------------------------------------------*/
  916. static int pa12201001_create_attr(struct device_driver *driver)
  917. {
  918. int idx, err = 0;
  919. int num = (int)(ARRAY_SIZE(pa12201001_attr_list));
  920. if (driver == NULL)
  921. {
  922. return -EINVAL;
  923. }
  924.  
  925. for(idx = 0; idx < num; idx++)
  926. {
  927. if((err = driver_create_file(driver, pa12201001_attr_list[idx])))
  928. {
  929. APS_ERR("driver_create_file (%s) = %d\n", pa12201001_attr_list[idx]->attr.name, err);
  930. break;
  931. }
  932. }
  933. return err;
  934. }
  935. /*----------------------------------------------------------------------------*/
  936. static int pa12201001_delete_attr(struct device_driver *driver)
  937. {
  938. int idx ,err = 0;
  939. int num = (int)(ARRAY_SIZE(pa12201001_attr_list));
  940.  
  941. if (!driver)
  942. return -EINVAL;
  943.  
  944. for (idx = 0; idx < num; idx++)
  945. {
  946. driver_remove_file(driver, pa12201001_attr_list[idx]);
  947. }
  948.  
  949. return err;
  950. }
  951.  
  952. /*----------------------------------interrupt functions-----------------------*/
  953. static int intr_flag = 0;
  954. /*----------------------------------------------------------------------------*/
  955. #ifndef CUSTOM_KERNEL_SENSORHUB
  956. static int pa12201001_check_intr(struct i2c_client *client)
  957. {
  958. int res;
  959. u8 psdata=0;
  960. u8 cfgdata=0;
  961.  
  962. res = hwmsen_read_byte(client,REG_PS_DATA,&psdata);
  963. if(res<0)
  964. {
  965. APS_ERR("i2c_read function err res = %d\n",res);
  966. return -1;
  967. }
  968.  
  969. APS_LOG("PA12201001_REG_PS_DATA value = %x \n",psdata);
  970.  
  971. res = hwmsen_read_byte(client,REG_CFG2,&cfgdata);
  972. if (res < 0)
  973. {
  974. APS_ERR("i2c_read function err res = %d\n",res);
  975. return -1;
  976. }
  977.  
  978. APS_LOG("PA12201001_READ INT STATUS:CFG2 cfgdata = 0x%x\n",cfgdata);
  979.  
  980. if(cfgdata & 0x02)
  981. {
  982. APS_LOG("[mcz] if\n");
  983. intr_flag = 0;//for close
  984. }
  985. else
  986. {
  987. APS_LOG("[mcz] else\n");
  988. intr_flag = 1;//for away
  989. }
  990.  
  991. return 0;
  992. }
  993.  
  994. static int pa12201001_clear_int_flag(struct i2c_client *client)
  995. {
  996. //Clear INT FLAG
  997. int res = 0;
  998. u8 cfgdata = 0;
  999. res = hwmsen_read_byte(client,REG_CFG2,&cfgdata);
  1000. if(res<0)
  1001. {
  1002. APS_ERR("i2c_read function err res = %d\n",res);
  1003. return -1;
  1004. }
  1005.  
  1006. APS_LOG("PA12201001_READ INT STATUS:CFG2 cfgdata = 0x%x\n",cfgdata);
  1007. if(cfgdata & 0x02)
  1008. {
  1009. cfgdata=cfgdata & 0xFD ; //Clear PS INT
  1010. res = hwmsen_write_byte(client,REG_CFG2,cfgdata);
  1011. if(res<0)
  1012. {
  1013. APS_ERR("i2c_send function err res = %d\n",res);
  1014. return -1;
  1015. }
  1016. }
  1017. APS_LOG("PA12201001_REG_INT_FLAG clear!\n");
  1018.  
  1019. return 0;
  1020. }
  1021. #endif //#ifndef CUSTOM_KERNEL_SENSORHUB
  1022. /*----------------------------------------------------------------------------*/
  1023. static void pa12201001_eint_work(struct work_struct *work)
  1024. {
  1025. int res = 0;
  1026. int sendvalue = 0;
  1027. u8 cfgdata=0;
  1028.  
  1029. #ifdef CUSTOM_KERNEL_SENSORHUB
  1030. int psvalue = 0;
  1031. if((pa12201001_read_ps(obj->client, &obj->ps)))
  1032. {
  1033. printk("pa12201001 read ps data: %d\n", err);
  1034. }
  1035. psvalue = pa12201001_get_ps_value(obj, obj->ps);
  1036. res = ps_report_interrupt_data(intr_flag_value);
  1037. if(res != 0)
  1038. {
  1039. printk("pa12201001_eint_work err\n");
  1040. }
  1041. #else //#ifdef CUSTOM_KERNEL_SENSORHUB
  1042.  
  1043. struct pa12201001_priv *obj = (struct pa12201001_priv *)container_of(work, struct pa12201001_priv, eint_work);
  1044.  
  1045. res = pa12201001_check_intr(obj->client);
  1046. if((pa12201001_read_ps(obj->client, &obj->ps)))
  1047. {
  1048. printk("pa12201001 read ps data err\n");
  1049. }
  1050. pa12201001_get_ps_value(obj, obj->ps);
  1051. if(!intr_flag_value)
  1052. {
  1053. g_interrupt_flag = 1;
  1054. printk("[ALS/PS][ljj] closing...\n");
  1055. }
  1056. if(!intr_flag_value)
  1057. {
  1058. APS_LOG("[mcz] AGOLD_PROX_CALI_ENABLE intr_flag_value\n");
  1059. // Set PS threshold
  1060. sendvalue=0xff;
  1061. res=hwmsen_write_byte(obj->client,REG_PS_TH,sendvalue); //set TH threshold
  1062.  
  1063. sendvalue=(u8)(PA12201001_Current_Ps_Thd_Value & 0xFF);
  1064. res=hwmsen_write_byte(obj->client,REG_PS_TL,sendvalue); //set TL threshold
  1065. }
  1066. else
  1067. {
  1068. APS_LOG("[mcz]else AGOLD_PROX_CALI_ENABLE intr_flag_value \n");
  1069. // Set PS threshold
  1070. sendvalue=(u8)(PA12201001_Current_Ps_Thd_Value & 0xFF);
  1071. res=hwmsen_write_byte(obj->client,REG_PS_TH,sendvalue); //set TH threshold
  1072.  
  1073. sendvalue=0;
  1074. res=hwmsen_write_byte(obj->client,REG_PS_TL,sendvalue); //set TL threshold
  1075.  
  1076. }
  1077. pa12201001_clear_int_flag(obj->client);
  1078. res = hwmsen_read_byte(obj->client,REG_CFG2,&cfgdata);
  1079. if(res<0)
  1080. {
  1081. APS_ERR("i2c_read function err res = %d\n",res);
  1082. }
  1083.  
  1084. APS_LOG("[eint_work][after clear int flsg] CFG2 cfgdata = 0x%x\n",cfgdata);
  1085.  
  1086. if(res != 0)
  1087. {
  1088. goto EXIT_INTR_ERR;
  1089. }
  1090. else
  1091. {
  1092. printk("pa12201001 interrupt value = %d\n", intr_flag_value);
  1093. res = ps_report_interrupt_data(intr_flag_value);
  1094. }
  1095.  
  1096. enable_irq(obj->irq);
  1097. return;
  1098.  
  1099. EXIT_INTR_ERR:
  1100. pa12201001_check_intr(obj->client);
  1101. enable_irq(obj->irq);
  1102. printk("pa12201001_eint_work err: %d\n", res);
  1103. return;
  1104. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1105.  
  1106. }
  1107.  
  1108.  
  1109.  
  1110. #ifdef CUSTOM_KERNEL_SENSORHUB
  1111. static void pa12201001_init_done_work(struct work_struct *work)
  1112. {
  1113. struct pa12201001_priv *obj = pa12201001_obj;
  1114. PA12_CUST_DATA *p_cust_data;
  1115. SCP_SENSOR_HUB_DATA data;
  1116. int max_cust_data_size_per_packet;
  1117. int i;
  1118. uint sizeOfCustData;
  1119. uint len;
  1120. char *p = (char *)obj->hw;
  1121.  
  1122. APS_FUN();
  1123.  
  1124. p_cust_data = (PA12_CUST_DATA *)data.set_cust_req.custData;
  1125. sizeOfCustData = sizeof(*(obj->hw));
  1126. max_cust_data_size_per_packet = sizeof(data.set_cust_req.custData) - offsetof(PA12_SET_CUST, data);
  1127.  
  1128. for (i=0;sizeOfCustData>0;i++)
  1129. {
  1130. data.set_cust_req.sensorType = ID_PROXIMITY;
  1131. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  1132. p_cust_data->setCust.action = PA12_CUST_ACTION_SET_CUST;
  1133. p_cust_data->setCust.part = i;
  1134.  
  1135. if (sizeOfCustData > max_cust_data_size_per_packet)
  1136. {
  1137. len = max_cust_data_size_per_packet;
  1138. }
  1139. else
  1140. {
  1141. len = sizeOfCustData;
  1142. }
  1143.  
  1144. memcpy(p_cust_data->setCust.data, p, len);
  1145. sizeOfCustData -= len;
  1146. p += len;
  1147.  
  1148. len += offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + offsetof(PA12_SET_CUST, data);
  1149. SCP_sensorHub_req_send(&data, &len, 1);
  1150. }
  1151.  
  1152. data.set_cust_req.sensorType = ID_PROXIMITY;
  1153. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  1154. p_cust_data->setEintInfo.action = PA12_CUST_ACTION_SET_EINT_INFO;
  1155. p_cust_data->setEintInfo.gpio_mode = GPIO_ALS_EINT_PIN_M_EINT;
  1156. p_cust_data->setEintInfo.gpio_pin = GPIO_ALS_EINT_PIN;
  1157. p_cust_data->setEintInfo.eint_num = CUST_EINT_ALS_NUM;
  1158. p_cust_data->setEintInfo.eint_is_deb_en = CUST_EINT_ALS_DEBOUNCE_EN;
  1159. p_cust_data->setEintInfo.eint_type = CUST_EINT_ALS_TYPE;
  1160. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(p_cust_data->setEintInfo);
  1161. SCP_sensorHub_req_send(&data, &len, 1);
  1162.  
  1163. }
  1164. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1165.  
  1166. /*----------------------------------------------------------------------------*/
  1167. #ifdef CUSTOM_KERNEL_SENSORHUB
  1168. static int pa12201001_irq_handler(void* data, uint len)
  1169. {
  1170. struct pa12201001_priv *obj = pa12201001_obj;
  1171. SCP_SENSOR_HUB_DATA_P rsp = (SCP_SENSOR_HUB_DATA_P)data;
  1172.  
  1173. if(!obj)
  1174. {
  1175. return -1;
  1176. }
  1177.  
  1178. printk("len = %d, type = %d, action = %d, errCode = %d\n", len, rsp->rsp.sensorType, rsp->rsp.action, rsp->rsp.errCode);
  1179.  
  1180. switch(rsp->rsp.action)
  1181. {
  1182. case SENSOR_HUB_NOTIFY:
  1183. switch(rsp->notify_rsp.event)
  1184. {
  1185. case SCP_INIT_DONE:
  1186. schedule_work(&obj->init_done_work);
  1187. //schedule_delayed_work(&obj->init_done_work, HZ);
  1188. break;
  1189. case SCP_NOTIFY:
  1190. if (PA12_NOTIFY_PROXIMITY_CHANGE == rsp->notify_rsp.data[0])
  1191. {
  1192. intr_flag_value = rsp->notify_rsp.data[1];
  1193. pa12201001_eint_func();
  1194. }
  1195. else
  1196. {
  1197. printk("Unknow notify");
  1198. }
  1199. break;
  1200. default:
  1201. printk("Error sensor hub notify");
  1202. break;
  1203. }
  1204. break;
  1205. default:
  1206. printk("Error sensor hub action");
  1207. break;
  1208. }
  1209.  
  1210. return 0;
  1211. }
  1212. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1213.  
  1214.  
  1215. /*----------------------------------------------------------------------------*/
  1216.  
  1217. static void pa12201001_eint_func(void)
  1218. {
  1219. struct pa12201001_priv *obj = g_pa12201001_ptr;
  1220. if(!obj)
  1221. {
  1222. return;
  1223. }
  1224. schedule_work(&obj->eint_work);
  1225. }
  1226. /*-------------------------------MISC device related------------------------------------------*/
  1227. #if defined(CONFIG_OF)
  1228. static irqreturn_t pa12201001_eint_handler(int irq, void *desc)
  1229. {
  1230. pa12201001_eint_func();
  1231. disable_irq_nosync(pa12201001_obj->irq);
  1232.  
  1233. return IRQ_HANDLED;
  1234. }
  1235. #endif
  1236. /*----------------------------------------------------------------------------*/
  1237. int pa12201001_setup_eint(struct i2c_client *client)
  1238. {
  1239. #if defined(CONFIG_OF)
  1240. u32 ints[2] = { 0, 0 };
  1241. #endif
  1242.  
  1243. int ret;
  1244. struct pinctrl *pinctrl;
  1245. struct pinctrl_state *pins_default;
  1246. struct pinctrl_state *pins_cfg;
  1247.  
  1248. struct pa12201001_priv *obj = i2c_get_clientdata(client);
  1249.  
  1250. g_pa12201001_ptr = obj;
  1251.  
  1252. pinctrl = devm_pinctrl_get(&client->dev);
  1253. if (IS_ERR(pinctrl))
  1254. {
  1255. ret = PTR_ERR(pinctrl);
  1256. APS_ERR("Cannot find alsps pinctrl!\n");
  1257. }
  1258. pins_default = pinctrl_lookup_state(pinctrl, "pin_default");
  1259. if (IS_ERR(pins_default))
  1260. {
  1261. ret = PTR_ERR(pins_default);
  1262. APS_ERR("Cannot find alsps pinctrl default!\n");
  1263.  
  1264. }
  1265.  
  1266. pins_cfg = pinctrl_lookup_state(pinctrl, "pin_cfg");
  1267. if (IS_ERR(pins_cfg))
  1268. {
  1269. ret = PTR_ERR(pins_cfg);
  1270. APS_ERR("Cannot find alsps pinctrl pin_cfg!\n");
  1271.  
  1272. }
  1273. pinctrl_select_state(pinctrl, pins_cfg);
  1274.  
  1275. if (pa12201001_obj->irq_node)
  1276. {
  1277. of_property_read_u32_array(pa12201001_obj->irq_node, "debounce", ints,
  1278. ARRAY_SIZE(ints));
  1279. gpio_request(ints[0], "p-sensor");
  1280. gpio_set_debounce(ints[0], ints[1]);
  1281. APS_LOG("ints[0] = %d, ints[1] = %d!!\n", ints[0], ints[1]);
  1282.  
  1283. pa12201001_obj->irq = irq_of_parse_and_map(pa12201001_obj->irq_node, 0);
  1284. APS_LOG("pa12201001_obj->irq = %d\n", pa12201001_obj->irq);
  1285. if (!pa12201001_obj->irq)
  1286. {
  1287. APS_ERR("irq_of_parse_and_map fail!!\n");
  1288. return -EINVAL;
  1289. }
  1290.  
  1291. if (request_irq(pa12201001_obj->irq, pa12201001_eint_handler, IRQF_TRIGGER_NONE, "ALS-eint", NULL))
  1292. {
  1293. APS_ERR("IRQ LINE NOT AVAILABLE!!\n");
  1294. return -EINVAL;
  1295. }
  1296.  
  1297. enable_irq_wake(pa12201001_obj->irq);
  1298. enable_irq(pa12201001_obj->irq);
  1299. }
  1300. else
  1301. {
  1302. APS_ERR("null irq node!!\n");
  1303. return -EINVAL;
  1304. }
  1305.  
  1306. return 0;
  1307. }
  1308.  
  1309. #if 0
  1310. /************************************************************/
  1311. static int pa12201001_open(struct inode *inode, struct file *file)
  1312. {
  1313. file->private_data = pa12201001_i2c_client;
  1314.  
  1315. if (!file->private_data)
  1316. {
  1317. APS_ERR("null pointer!!\n");
  1318. return -EINVAL;
  1319. }
  1320. return nonseekable_open(inode, file);
  1321. }
  1322. /************************************************************/
  1323.  
  1324. static int pa12201001_release(struct inode *inode, struct file *file)
  1325. {
  1326. file->private_data = NULL;
  1327. return 0;
  1328. }
  1329. #endif
  1330.  
  1331. int pa12201001_set_ps_threshold_factory(struct i2c_client *client,int ps_thd_value,int high_th)
  1332. {
  1333. u8 databuf[2];
  1334. int res = 0;
  1335. if(high_th == 1)
  1336. {
  1337. databuf[0] = REG_PS_TH;
  1338. databuf[1] = (u8)(ps_thd_value & 0xFF);
  1339. res = i2c_master_send(client, databuf, 0x2);
  1340. if(res <= 0)
  1341. {
  1342. return res;
  1343. }
  1344. }
  1345. else
  1346. {
  1347. databuf[0] = REG_PS_TL;
  1348. databuf[1] = (u8)(ps_thd_value & 0xFF);
  1349. res = i2c_master_send(client, databuf, 0x2);
  1350. if(res <= 0)
  1351. {
  1352. return res;
  1353. }
  1354. }
  1355. return 0;
  1356. }
  1357.  
  1358. /************************************************************/
  1359.  
  1360. #if defined(AGOLD_PROX_CALI_ENABLE)
  1361. int PA12201001_set_ps_threshold(struct i2c_client *client,int ps_thd_value)
  1362. {
  1363. int res = 0;
  1364. u8 ps_value = 0;
  1365.  
  1366. // Set PS threshold
  1367. APS_LOG("[mcz]PA12201001_set_ps_threshold\n");
  1368. res=hwmsen_write_byte(client,REG_PS_TH,ps_thd_value); //set TH threshold
  1369. if(res < 0)
  1370. {
  1371. return res;
  1372. }
  1373. //低门限值是不是应该设为0
  1374. res=hwmsen_write_byte(client,REG_PS_TL,ps_thd_value); //set TL threshold
  1375. if(res < 0)
  1376. {
  1377. return res;
  1378. }
  1379.  
  1380. res=hwmsen_read_byte(client,REG_PS_TH,&ps_value);
  1381. if(res < 0)
  1382. {
  1383. return res;
  1384. }
  1385. APS_LOG("[mcz][set]REG_PS_TH = 0x%x\n",ps_value);
  1386. res=hwmsen_read_byte(client,REG_PS_TL,&ps_value);
  1387. if(res < 0)
  1388. {
  1389. return res;
  1390. }
  1391. APS_LOG("[mcz][set]REG_PS_TL = 0x%x\n",ps_value);
  1392.  
  1393. return 0;
  1394. }
  1395.  
  1396. #define PA12201001_PPCOUNT_MIN 0
  1397. #define PA12201001_PPCOUNT_MAX 4
  1398. #define PA12201001_READ_TIMES 5
  1399. #define PA12201001_MAX_PS_VALUE 0xFF
  1400. #define PS_CALI_TIMES 25
  1401.  
  1402. static int pcount1 = 0;
  1403. static int pcount2 = 0;
  1404.  
  1405. #if 1
  1406. static int PA12201001_get_ps_data_for_cali(struct i2c_client *client, int flag)
  1407. {
  1408. u8 ps_tmp_dat = 0;
  1409. int max_ps_value = 0;
  1410. int min_ps_value = 0;
  1411. u8 i = 0 ;
  1412. int res = 0;
  1413.  
  1414. printk("[Agold spl] PA12201001_get_ps_data_for_cali Start, flag = %d.\n", flag);
  1415.  
  1416. disable_irq_nosync(pa12201001_obj->irq);
  1417.  
  1418. if((res = pa12201001_enable_ps(client, 1)))
  1419. {
  1420. APS_ERR("enable als fail: %d\n", res);
  1421. return res;
  1422. }
  1423.  
  1424. if(1 == flag)
  1425. {
  1426. for(i=0; i<PS_CALI_TIMES; i++)
  1427. {
  1428. res = pa12201001_read_ps(client, &ps_tmp_dat);
  1429. if(res)
  1430. {
  1431. APS_LOG("[Bruce] Read ps data err.\n");
  1432. return res;
  1433. }
  1434. APS_LOG("[Bruce] ps_tmp_dat = 0x%x.\n",ps_tmp_dat);
  1435. if(i == 0)
  1436. {
  1437. min_ps_value = ps_tmp_dat;
  1438. }
  1439. if(min_ps_value > ps_tmp_dat)
  1440. {
  1441. min_ps_value = 0xff&ps_tmp_dat;
  1442. }
  1443. msleep(1);
  1444. }
  1445. res = min_ps_value ;
  1446. }
  1447. else if(2 == flag)
  1448. {
  1449. for(i=0; i<PS_CALI_TIMES; i++)
  1450. {
  1451. res = pa12201001_read_ps(client, &ps_tmp_dat);
  1452. if(res)
  1453. {
  1454. APS_LOG("[Bruce] Read ps data err.\n");
  1455. return res;
  1456. }
  1457. APS_LOG("[Bruce] ps_tmp_dat = 0x%x.\n",ps_tmp_dat);
  1458. if(max_ps_value < ps_tmp_dat)
  1459. {
  1460. max_ps_value = 0xff&ps_tmp_dat;
  1461. }
  1462. msleep(1);
  1463. }
  1464. res = max_ps_value;
  1465. }
  1466.  
  1467. printk("[Agold spl] PA12201001_get_ps_data_for_cali end.\n");
  1468.  
  1469. enable_irq(pa12201001_obj->irq);
  1470.  
  1471. return res;
  1472. }
  1473.  
  1474. #else
  1475. static int PA12201001_get_ps_data_for_cali(struct i2c_client *client, int flag)
  1476. {
  1477. u8 ps_tmp_dat = 0;
  1478. int ps_dat[2] = {0,0};
  1479. int max_ps_value = 0;
  1480. int i = 0 ,pcount = 0;
  1481. int res = 0;
  1482. int ps_count_data[30] = {0};
  1483. char databuf[2] = {0,0};
  1484. int ps_raw =0 ;
  1485.  
  1486.  
  1487. //first we shold disable als ps and set wait time min
  1488. if((res = pa12201001_enable_als(client, 0)))
  1489. {
  1490. APS_ERR("enable als fail: %ld\n", res);
  1491. return res;
  1492. }
  1493. if((res = pa12201001_enable_ps(client, 0)))
  1494. {
  1495. APS_ERR("enable ps fail: %ld\n", res);
  1496. return res;
  1497. }
  1498.  
  1499. if((res = pa12201001_enable_ps(client, 1)))
  1500. {
  1501. APS_ERR("enable ps fail: %ld\n", res);
  1502. return res;
  1503. }
  1504.  
  1505. //set LED current 100mA
  1506. APS_LOG("[mcz] pcount = 0x%x\n",pcount);
  1507.  
  1508. mdelay(130);
  1509. max_ps_value = 0;
  1510. for(i = 0;i < PA12201001_READ_TIMES ; i++)
  1511. {
  1512. if(res = pa12201001_read_ps(client, &ps_tmp_dat))
  1513. {
  1514. APS_LOG("[Agold mcz] Read ps data err.\n");
  1515. return res;
  1516. }
  1517. APS_LOG("[Agold mcz] ps_tmp_dat = 0x%x.\n",ps_tmp_dat);
  1518. if(max_ps_value < ps_tmp_dat)
  1519. {
  1520. max_ps_value = 0xff&ps_tmp_dat;
  1521. }
  1522. msleep(1);
  1523. }
  1524.  
  1525. APS_LOG("[Agold mcz] max_ps_value = 0x%x\n",max_ps_value);
  1526.  
  1527. //last we shold enable als and set wait time 200ms
  1528. if((res = pa12201001_enable_ps(client, 0)))
  1529. {
  1530. APS_ERR("enable als fail: %ld\n", res);
  1531. return res;
  1532. }
  1533. mdelay(200);
  1534. if((res = pa12201001_enable_ps(client, 1)))
  1535. {
  1536. APS_ERR("enable als fail: %ld\n", res);
  1537. return res;
  1538. }
  1539. if((res = pa12201001_enable_als(client, 1)))
  1540. {
  1541. APS_ERR("enable als fail: %ld\n", res);
  1542. return res;
  1543. }
  1544. return 0;
  1545. }
  1546. #endif
  1547. #endif
  1548.  
  1549. static int set_psensor_threshold(struct i2c_client *client)
  1550. {
  1551. return 0;
  1552. }
  1553.  
  1554. #if 0
  1555. static long pa12201001_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1556. {
  1557. struct i2c_client *client = (struct i2c_client*)file->private_data;
  1558. struct pa12201001_priv *obj = i2c_get_clientdata(client);
  1559. long err = 0;
  1560. void __user *ptr = (void __user*) arg;
  1561. u8 dat;
  1562. uint32_t enable;
  1563. int ps_result;
  1564. int ps_dat[2] = {0,0};
  1565. int ps_cali_data;
  1566. #ifdef CUSTOM_KERNEL_SENSORHUB
  1567. SCP_SENSOR_HUB_DATA data;
  1568. PA12_CUST_DATA *pCustData;
  1569. int len;
  1570.  
  1571. data.set_cust_req.sensorType = ID_PROXIMITY;
  1572. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  1573. pCustData = (PA12_CUST_DATA *)(&data.set_cust_req.custData);
  1574. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1575.  
  1576. switch (cmd)
  1577. {
  1578. case ALSPS_SET_PS_MODE:
  1579. if(copy_from_user(&enable, ptr, sizeof(enable)))
  1580. {
  1581. err = -EFAULT;
  1582. goto err_out;
  1583. }
  1584. if(enable)
  1585. {
  1586. if((err = pa12201001_enable_ps(obj->client, 1)))
  1587. {
  1588. APS_ERR("enable ps fail: %ld\n", err);
  1589. goto err_out;
  1590. }
  1591.  
  1592. set_bit(CMC_BIT_PS, &obj->enable);
  1593. }
  1594. else
  1595. {
  1596. if((err = pa12201001_enable_ps(obj->client, 0)))
  1597. {
  1598. APS_ERR("disable ps fail: %ld\n", err);
  1599. goto err_out;
  1600. }
  1601. clear_bit(CMC_BIT_PS, &obj->enable);
  1602. }
  1603. break;
  1604.  
  1605. case ALSPS_GET_PS_MODE:
  1606. enable = test_bit(CMC_BIT_PS, &obj->enable) ? (1) : (0);
  1607. if(copy_to_user(ptr, &enable, sizeof(enable)))
  1608. {
  1609. err = -EFAULT;
  1610. goto err_out;
  1611. }
  1612. break;
  1613.  
  1614. case ALSPS_GET_PS_DATA:
  1615. if((err = pa12201001_read_ps(obj->client, &obj->ps)))
  1616. {
  1617. goto err_out;
  1618. }
  1619.  
  1620. dat = pa12201001_get_ps_value(obj, obj->ps);
  1621. if(copy_to_user(ptr, &dat, sizeof(dat)))
  1622. {
  1623. err = -EFAULT;
  1624. goto err_out;
  1625. }
  1626. break;
  1627.  
  1628. case ALSPS_GET_PS_RAW_DATA:
  1629.  
  1630. #if defined(AGOLD_PROX_CALI_ENABLE)
  1631. err = pa12201001_read_ps(obj->client, &dat);
  1632. if(err)
  1633. {
  1634. goto err_out;
  1635. }
  1636. ps_dat[0] = dat;
  1637. ps_dat[1] = PA12201001_Current_Ps_Thd_Value;
  1638. APS_LOG("[Agold mcz] ps_dat[0] = %d , ps_dat[1] = %d.\n",ps_dat[0],ps_dat[1]);
  1639. if(copy_to_user(ptr, ps_dat, sizeof(ps_dat)))
  1640. {
  1641. err = -EFAULT;
  1642. goto err_out;
  1643. }
  1644. #else
  1645.  
  1646. if((err = pa12201001_read_ps(obj->client, &obj->ps)))
  1647. {
  1648. goto err_out;
  1649. }
  1650.  
  1651. dat = obj->ps;
  1652. if(copy_to_user(ptr, &dat, sizeof(dat)))
  1653. {
  1654. err = -EFAULT;
  1655. goto err_out;
  1656. }
  1657. #endif
  1658. break;
  1659.  
  1660. case ALSPS_SET_ALS_MODE:
  1661.  
  1662. if(copy_from_user(&enable, ptr, sizeof(enable)))
  1663. {
  1664. err = -EFAULT;
  1665. goto err_out;
  1666. }
  1667. if(enable)
  1668. {
  1669. if((err = pa12201001_enable_als(obj->client, 1)))
  1670. {
  1671. APS_ERR("enable als fail: %ld\n", err);
  1672. goto err_out;
  1673. }
  1674. set_bit(CMC_BIT_ALS, &obj->enable);
  1675. }
  1676. else
  1677. {
  1678. if((err = pa12201001_enable_als(obj->client, 0)))
  1679. {
  1680. APS_ERR("disable als fail: %ld\n", err);
  1681. goto err_out;
  1682. }
  1683. clear_bit(CMC_BIT_ALS, &obj->enable);
  1684. }
  1685. break;
  1686.  
  1687. case ALSPS_GET_ALS_MODE:
  1688. enable = test_bit(CMC_BIT_ALS, &obj->enable) ? (1) : (0);
  1689. if(copy_to_user(ptr, &enable, sizeof(enable)))
  1690. {
  1691. err = -EFAULT;
  1692. goto err_out;
  1693. }
  1694. break;
  1695.  
  1696. case ALSPS_GET_ALS_DATA:
  1697. if((err = pa12201001_read_als(obj->client, &obj->als)))
  1698. {
  1699. goto err_out;
  1700. }
  1701.  
  1702. dat = pa12201001_get_als_value(obj, obj->als);
  1703. if(copy_to_user(ptr, &dat, sizeof(dat)))
  1704. {
  1705. err = -EFAULT;
  1706. goto err_out;
  1707. }
  1708. break;
  1709.  
  1710. case ALSPS_GET_ALS_RAW_DATA:
  1711. if((err = pa12201001_read_als(obj->client, &obj->als)))
  1712. {
  1713. goto err_out;
  1714. }
  1715.  
  1716. dat = obj->als;
  1717. if(copy_to_user(ptr, &dat, sizeof(dat)))
  1718. {
  1719. err = -EFAULT;
  1720. goto err_out;
  1721. }
  1722. break;
  1723.  
  1724. /*----------------------------------for factory mode test---------------------------------------*/
  1725. case ALSPS_GET_PS_TEST_RESULT:
  1726. if((err = pa12201001_read_ps(obj->client, &obj->ps)))
  1727. {
  1728. goto err_out;
  1729. }
  1730. if(obj->ps > atomic_read(&obj->ps_thd_val_high))
  1731. {
  1732. ps_result = 0;
  1733. }
  1734. else ps_result = 1;
  1735.  
  1736. if(copy_to_user(ptr, &ps_result, sizeof(ps_result)))
  1737. {
  1738. err = -EFAULT;
  1739. goto err_out;
  1740. }
  1741. break;
  1742. /*------------------------------------------------------------------------------------------*/
  1743. case ALSPS_IOCTL_CLR_CALI:
  1744. if(copy_from_user(&dat, ptr, sizeof(dat)))
  1745. {
  1746. err = -EFAULT;
  1747. goto err_out;
  1748. }
  1749. if(dat == 0)
  1750. obj->ps_cali = 0;
  1751.  
  1752. #ifdef CUSTOM_KERNEL_SENSORHUB
  1753. pCustData->clearCali.action = PA12_CUST_ACTION_CLR_CALI;
  1754. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->clearCali);
  1755.  
  1756. err = SCP_sensorHub_req_send(&data, &len, 1);
  1757. #endif
  1758.  
  1759. break;
  1760. case ALSPS_IOCTL_SET_CALI:
  1761. if(copy_from_user(&ps_cali_data, ptr, sizeof(ps_cali_data)))
  1762. {
  1763. err = -EFAULT;
  1764. goto err_out;
  1765. }
  1766.  
  1767. obj->ps_cali = ps_cali_data;
  1768.  
  1769. #ifdef CUSTOM_KERNEL_SENSORHUB
  1770. pCustData->setCali.action = PA12_CUST_ACTION_SET_CALI;
  1771. pCustData->setCali.cali = ps_cali_data;
  1772. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->setCali);
  1773.  
  1774. err = SCP_sensorHub_req_send(&data, &len, 1);
  1775. #endif
  1776.  
  1777.  
  1778. default:
  1779. APS_ERR("%s not supported = 0x%04x", __FUNCTION__, cmd);
  1780. err = -ENOIOCTLCMD;
  1781. break;
  1782. }
  1783.  
  1784. err_out:
  1785. return err;
  1786. }
  1787. #endif
  1788.  
  1789. #if defined(AGOLD_PROX_CALI_ENABLE)
  1790. static void agold_pa12201001_set_ps_cali(AGOLD_PS_CALI_DATA_STRUCT ps_cali_dat)
  1791. {
  1792.  
  1793. disable_irq_nosync(pa12201001_obj->irq);
  1794.  
  1795. if(ps_cali_dat.close > AGOLD_PA12201001_THRESHOLD_MIN && ps_cali_dat.close <= 0xF0)
  1796. {
  1797. spin_lock(&ps_cali_lock);
  1798. PA12201001_Current_Ps_Thd_Value = ps_cali_dat.close;
  1799. spin_unlock(&ps_cali_lock);
  1800. }
  1801. else
  1802. {
  1803. spin_lock(&ps_cali_lock);
  1804. #if defined(AGOLD_DEFINED_ALSPS_PA12201001_PS_THD_VALUE)
  1805. PA12201001_Current_Ps_Thd_Value = agold_strtol(AGOLD_DEFINED_ALSPS_PA12201001_PS_THD_VALUE);
  1806.  
  1807. if( PA12201001_Current_Ps_Thd_Value < AGOLD_PA12201001_THRESHOLD_MIN )
  1808. PA12201001_Current_Ps_Thd_Value = AGOLD_PA12201001_THRESHOLD_MIN;
  1809. else if ( PA12201001_Current_Ps_Thd_Value > 0xF0 )
  1810. PA12201001_Current_Ps_Thd_Value = 0xF0;
  1811.  
  1812. ps_cali_dat.close = PA12201001_Current_Ps_Thd_Value;
  1813. ps_cali_dat.far_away = PA12201001_Current_Ps_Thd_Value;
  1814. #else
  1815. PA12201001_Current_Ps_Thd_Value = AGOLD_PA12201001_THRESHOLD_MIN;
  1816. ps_cali_dat.close = AGOLD_PA12201001_THRESHOLD_MIN;
  1817. ps_cali_dat.far_away = AGOLD_PA12201001_THRESHOLD_MIN;
  1818. #endif
  1819. spin_unlock(&ps_cali_lock);
  1820. }
  1821. printk("[Bruce] %d, ps_cali_dat.close = 0x%x ,ps_cali_dat.ppcount = 0x%x\n ",__LINE__,ps_cali_dat.close,ps_cali_dat.ppcount);
  1822. printk("[Bruce] ps_cali_dat.valid = %d,PA12201001_Current_Ps_Thd_Value = 0x%x \n ",ps_cali_dat.valid, PA12201001_Current_Ps_Thd_Value);
  1823.  
  1824. PA12201001_set_ps_threshold(pa12201001_i2c_client,(unsigned int)(PA12201001_Current_Ps_Thd_Value));
  1825.  
  1826. enable_irq(pa12201001_obj->irq);
  1827. }
  1828.  
  1829. static int agold_pa12201001_get_ps_deep_cali_data(void)
  1830. {
  1831.  
  1832. pcount1 = PA12201001_get_ps_data_for_cali(pa12201001_i2c_client, 1);
  1833. printk("pcount1 = %d\n",pcount1);
  1834. return 1;
  1835. }
  1836.  
  1837. static int agold_pa12201001_get_ps_raw_data_for_cali(AGOLD_PS_CALI_DATA_STRUCT *ps_cali_dat)
  1838. {
  1839. int res = 0;
  1840.  
  1841. pcount2 = PA12201001_get_ps_data_for_cali(pa12201001_i2c_client, 2);
  1842. disable_irq_nosync(pa12201001_obj->irq);
  1843.  
  1844. printk("[Agold spl] agold_pa12201001_get_ps_raw_data_for_cali start, pcount1 = %d, pcount2 = %d\n",pcount1, pcount2);
  1845. if(pcount1 > 0xF0)//stucture is not good
  1846. {
  1847. ps_cali_dat->deep_cali_flag = DEEP_CALI_STRUCTE_NOT_GOOD;
  1848. ps_cali_dat->close = 0;
  1849. ps_cali_dat->far_away = 0;
  1850. ps_cali_dat->valid = 0;
  1851. ps_cali_dat->ppcount = 0;
  1852. ps_cali_dat->no_barrier_flag = 0;
  1853. printk("[Bruce] Warning the structure not good,ps_cali_dat->deep_cali_flag = %d\n",ps_cali_dat->deep_cali_flag);
  1854. res = DEEP_CALI_STRUCTE_NOT_GOOD;
  1855. }
  1856. else if((pcount1 - pcount2 >= 0x02)||pcount1 > 0xA0) //get ps raw error,maybe you have place obstacle when getting ps raw data
  1857. {
  1858. ps_cali_dat->deep_cali_flag = DEEP_CALI_GET_PS_RAW_DATA_ERROR;
  1859. ps_cali_dat->close = 0;
  1860. ps_cali_dat->far_away = 0;
  1861. ps_cali_dat->valid = 0;
  1862. ps_cali_dat->ppcount = 0;
  1863. ps_cali_dat->no_barrier_flag = 0;
  1864. printk("[Bruce] get ps raw error,maybe you have place obstacle when getting ps raw data,ps_cali_dat->deep_cali_flag = %d\n",ps_cali_dat->deep_cali_flag);
  1865. res = DEEP_CALI_GET_PS_RAW_DATA_ERROR;
  1866. }
  1867. else if(pcount2 -pcount1 < 0x15)//too far or no barrier,need set near
  1868. {
  1869. ps_cali_dat->deep_cali_flag = DEEP_CALI_SET_NEAR;
  1870. ps_cali_dat->close = 0;
  1871. ps_cali_dat->far_away = 0;
  1872. ps_cali_dat->valid = 0;
  1873. ps_cali_dat->ppcount = 0;
  1874. ps_cali_dat->no_barrier_flag = 0;
  1875. printk("[Bruce] too far,need set near,ps_cali_dat->deep_cali_flag = %d\n",ps_cali_dat->deep_cali_flag);
  1876. res = DEEP_CALI_SET_NEAR;
  1877. }
  1878. else
  1879. {
  1880. ps_cali_dat->close = pcount2;
  1881. if(ps_cali_dat->close >= 0xf0)
  1882. {
  1883. ps_cali_dat->close = 0xf0;
  1884. }
  1885. ps_cali_dat->far_away = ps_cali_dat->close ;//ps_tmp_dat;
  1886. ps_cali_dat->ppcount = 0;
  1887. ps_cali_dat->valid = 1;
  1888. res = DEEP_CALI_SUCCESS;
  1889. }
  1890.  
  1891. enable_irq(pa12201001_obj->irq);
  1892. printk("[Agold spl] agold_pa12201001_get_ps_raw_data_for_cali end, res = %d\n",res);
  1893. return res;
  1894. }
  1895. #endif
  1896.  
  1897. #if 0
  1898. /*--------------------misc device related operation functions--------------------------*/
  1899. static struct file_operations pa12201001_fops = {
  1900. .owner = THIS_MODULE,
  1901. .open = pa12201001_open,
  1902. .release = pa12201001_release,
  1903. .unlocked_ioctl = pa12201001_unlocked_ioctl,
  1904. };
  1905.  
  1906. static struct miscdevice pa12201001_device = {
  1907. .minor = MISC_DYNAMIC_MINOR,
  1908. .name = "pa12201001",
  1909. .fops = &pa12201001_fops,
  1910. };
  1911. #endif
  1912.  
  1913. /*--------------------------------------------------------------------------------------*/
  1914.  
  1915. // if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL
  1916. static int als_open_report_data(int open)
  1917. {
  1918. //should queuq work to report event if is_report_input_direct=true
  1919. return 0;
  1920. }
  1921.  
  1922. // if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL
  1923.  
  1924. static int als_enable_nodata(int en)
  1925. {
  1926. int res = 0;
  1927. #ifdef CUSTOM_KERNEL_SENSORHUB
  1928. SCP_SENSOR_HUB_DATA req;
  1929. int len;
  1930. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1931.  
  1932. printk("pa12201001_obj als enable value = %d\n", en);
  1933.  
  1934. #ifdef CUSTOM_KERNEL_SENSORHUB
  1935. req.activate_req.sensorType = ID_LIGHT;
  1936. req.activate_req.action = SENSOR_HUB_ACTIVATE;
  1937. req.activate_req.enable = en;
  1938. len = sizeof(req.activate_req);
  1939. res = SCP_sensorHub_req_send(&req, &len, 1);
  1940. #else //#ifdef CUSTOM_KERNEL_SENSORHUB
  1941. if(!pa12201001_obj)
  1942. {
  1943. printk("pa12201001_obj is null!!\n");
  1944. return -1;
  1945. }
  1946. res = pa12201001_enable_als(pa12201001_obj->client, en);
  1947. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1948. if(res){
  1949. printk("als_enable_nodata is failed!!\n");
  1950. return -1;
  1951. }
  1952. return 0;
  1953. }
  1954.  
  1955. static int als_set_delay(u64 ns)
  1956. {
  1957. return 0;
  1958. }
  1959.  
  1960. static int als_batch(int flag, int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs)
  1961. {
  1962. return als_set_delay(samplingPeriodNs);
  1963. }
  1964.  
  1965. static int als_flush(void)
  1966. {
  1967. return als_flush_report();
  1968. }
  1969.  
  1970. static int als_get_data(int* value, int* status)
  1971. {
  1972. int err = 0;
  1973. #ifdef CUSTOM_KERNEL_SENSORHUB
  1974. SCP_SENSOR_HUB_DATA req;
  1975. int len;
  1976. #else
  1977. struct pa12201001_priv *obj = NULL;
  1978. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  1979.  
  1980. #ifdef CUSTOM_KERNEL_SENSORHUB
  1981. req.get_data_req.sensorType = ID_LIGHT;
  1982. req.get_data_req.action = SENSOR_HUB_GET_DATA;
  1983. len = sizeof(req.get_data_req);
  1984. err = SCP_sensorHub_req_send(&req, &len, 1);
  1985. if (err)
  1986. {
  1987. printk("SCP_sensorHub_req_send fail!\n");
  1988. }
  1989. else
  1990. {
  1991. *value = req.get_data_rsp.int16_Data[0];
  1992. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1993. }
  1994.  
  1995. if(atomic_read(&pa12201001_obj->trace) & CMC_TRC_PS_DATA)
  1996. {
  1997. printk("value = %d\n", *value);
  1998. //show data
  1999. }
  2000. #else //#ifdef CUSTOM_KERNEL_SENSORHUB
  2001. if(!pa12201001_obj)
  2002. {
  2003. printk("pa12201001_obj is null!!\n");
  2004. return -1;
  2005. }
  2006. obj = pa12201001_obj;
  2007. if((err = pa12201001_read_als(obj->client, &obj->als)))
  2008. {
  2009. err = -1;
  2010. }
  2011. else
  2012. {
  2013. *value = pa12201001_get_als_value(obj, obj->als);
  2014. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  2015. }
  2016. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  2017.  
  2018. return err;
  2019. }
  2020.  
  2021. // if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL
  2022. static int ps_open_report_data(int open)
  2023. {
  2024. //should queuq work to report event if is_report_input_direct=true
  2025. return 0;
  2026. }
  2027.  
  2028. // if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL
  2029.  
  2030. static int ps_enable_nodata(int en)
  2031. {
  2032. int res = 0;
  2033. #ifdef CUSTOM_KERNEL_SENSORHUB
  2034. SCP_SENSOR_HUB_DATA req;
  2035. int len;
  2036. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  2037.  
  2038. printk("pa12201001_obj als enable value = %d\n", en);
  2039.  
  2040. #ifdef CUSTOM_KERNEL_SENSORHUB
  2041. req.activate_req.sensorType = ID_PROXIMITY;
  2042. req.activate_req.action = SENSOR_HUB_ACTIVATE;
  2043. req.activate_req.enable = en;
  2044. len = sizeof(req.activate_req);
  2045. res = SCP_sensorHub_req_send(&req, &len, 1);
  2046. #else //#ifdef CUSTOM_KERNEL_SENSORHUB
  2047. if(!pa12201001_obj)
  2048. {
  2049. printk("pa12201001_obj is null!!\n");
  2050. return -1;
  2051. }
  2052. res= pa12201001_enable_ps(pa12201001_obj->client, en);
  2053. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  2054.  
  2055. if(res){
  2056. printk("als_enable_nodata is failed!!\n");
  2057. return -1;
  2058. }
  2059. return 0;
  2060.  
  2061. }
  2062.  
  2063. static int ps_set_delay(u64 ns)
  2064. {
  2065. return 0;
  2066. }
  2067.  
  2068. static int ps_batch(int flag, int64_t samplingPeriodNs, int64_t maxBatchReportLatencyNs)
  2069. {
  2070. return 0;
  2071. }
  2072.  
  2073. static int ps_flush(void)
  2074. {
  2075. return ps_flush_report();
  2076. }
  2077.  
  2078. static int ps_get_data(int* value, int* status)
  2079. {
  2080. int err = 0;
  2081. #ifdef CUSTOM_KERNEL_SENSORHUB
  2082. SCP_SENSOR_HUB_DATA req;
  2083. int len;
  2084. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  2085. #ifdef CUSTOM_KERNEL_SENSORHUB
  2086. req.get_data_req.sensorType = ID_PROXIMITY;
  2087. req.get_data_req.action = SENSOR_HUB_GET_DATA;
  2088. len = sizeof(req.get_data_req);
  2089. err = SCP_sensorHub_req_send(&req, &len, 1);
  2090. if (err)
  2091. {
  2092. printk("SCP_sensorHub_req_send fail!\n");
  2093. }
  2094. else
  2095. {
  2096. *value = req.get_data_rsp.int16_Data[0];
  2097. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  2098. }
  2099.  
  2100. if(atomic_read(&pa12201001_obj->trace) & CMC_TRC_PS_DATA)
  2101. {
  2102. printk("value = %d\n", *value);
  2103. //show data
  2104. }
  2105. #else //#ifdef CUSTOM_KERNEL_SENSORHUB
  2106. if(!pa12201001_obj)
  2107. {
  2108. printk("pa12201001_obj is null!!\n");
  2109. return -1;
  2110. }
  2111.  
  2112. if((err = pa12201001_read_ps(pa12201001_obj->client, &pa12201001_obj->ps)))
  2113. {
  2114. err = -1;;
  2115. }
  2116. else
  2117. {
  2118. *value = pa12201001_get_ps_value(pa12201001_obj, pa12201001_obj->ps);
  2119. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  2120. }
  2121. #endif //#ifdef CUSTOM_KERNEL_SENSORHUB
  2122.  
  2123. return 0;
  2124. }
  2125.  
  2126. static int pa12201001_als_factory_enable_sensor(bool enable_disable, int64_t sample_periods_ms)
  2127. {
  2128. int err = 0;
  2129.  
  2130. err = als_enable_nodata(enable_disable ? 1 : 0);
  2131. if (err) {
  2132. APS_ERR("%s:%s failed\n", __func__, enable_disable ? "enable" : "disable");
  2133. return -1;
  2134. }
  2135. err = als_batch(0, sample_periods_ms * 1000000, 0);
  2136. if (err) {
  2137. APS_ERR("%s set_batch failed\n", __func__);
  2138. return -1;
  2139. }
  2140. return 0;
  2141. }
  2142. static int pa12201001_als_factory_get_data(int32_t *data)
  2143. {
  2144. int status;
  2145.  
  2146. return als_get_data(data, &status);
  2147. }
  2148. static int pa12201001_als_factory_get_raw_data(int32_t *data)
  2149. {
  2150. int err = 0;
  2151. struct pa12201001_priv *obj = pa12201001_obj;
  2152.  
  2153. if (!obj) {
  2154. APS_ERR("obj is null!!\n");
  2155. return -1;
  2156. }
  2157.  
  2158. err = pa12201001_read_als(obj->client, &obj->als);
  2159. if (err) {
  2160. APS_ERR("%s failed\n", __func__);
  2161. return -1;
  2162. }
  2163. *data = pa12201001_obj->als;
  2164.  
  2165. return 0;
  2166. }
  2167. static int pa12201001_als_factory_enable_calibration(void)
  2168. {
  2169. return 0;
  2170. }
  2171. static int pa12201001_als_factory_clear_cali(void)
  2172. {
  2173. return 0;
  2174. }
  2175. static int pa12201001_als_factory_set_cali(int32_t offset)
  2176. {
  2177. return 0;
  2178. }
  2179. static int pa12201001_als_factory_get_cali(int32_t *offset)
  2180. {
  2181. return 0;
  2182. }
  2183. static int pa12201001_ps_factory_enable_sensor(bool enable_disable, int64_t sample_periods_ms)
  2184. {
  2185. int err = 0;
  2186.  
  2187. err = ps_enable_nodata(enable_disable ? 1 : 0);
  2188. if (err) {
  2189. APS_ERR("%s:%s failed\n", __func__, enable_disable ? "enable" : "disable");
  2190. return -1;
  2191. }
  2192. err = ps_batch(0, sample_periods_ms * 1000000, 0);
  2193. if (err) {
  2194. APS_ERR("%s set_batch failed\n", __func__);
  2195. return -1;
  2196. }
  2197. return err;
  2198. }
  2199. static int pa12201001_ps_factory_get_data(int32_t *data)
  2200. {
  2201. int err = 0, status = 0;
  2202.  
  2203. err = ps_get_data(data, &status);
  2204. if (err < 0)
  2205. return -1;
  2206. return 0;
  2207. }
  2208. static int pa12201001_ps_factory_get_raw_data(int32_t *data)
  2209. {
  2210. int err = 0;
  2211. struct pa12201001_priv *obj = pa12201001_obj;
  2212.  
  2213. err = pa12201001_read_ps(obj->client, &obj->ps);
  2214. if (err) {
  2215. APS_ERR("%s failed\n", __func__);
  2216. return -1;
  2217. }
  2218. *data = pa12201001_obj->ps;
  2219. return 0;
  2220. }
  2221. static int pa12201001_ps_factory_enable_calibration(void)
  2222. {
  2223. return 0;
  2224. }
  2225. static int pa12201001_ps_factory_clear_cali(void)
  2226. {
  2227. struct pa12201001_priv *obj = pa12201001_obj;
  2228.  
  2229. obj->ps_cali = 0;
  2230. return 0;
  2231. }
  2232. static int pa12201001_ps_factory_set_cali(int32_t offset)
  2233. {
  2234. struct pa12201001_priv *obj = pa12201001_obj;
  2235.  
  2236. obj->ps_cali = offset;
  2237. return 0;
  2238. }
  2239. static int pa12201001_ps_factory_get_cali(int32_t *offset)
  2240. {
  2241. struct pa12201001_priv *obj = pa12201001_obj;
  2242.  
  2243. *offset = obj->ps_cali;
  2244. return 0;
  2245. }
  2246. static int pa12201001_ps_factory_set_threashold(int32_t threshold[2])
  2247. {
  2248. int err = 0;
  2249. struct pa12201001_priv *obj = pa12201001_obj;
  2250.  
  2251. APS_ERR("%s set threshold high: 0x%x, low: 0x%x\n", __func__, threshold[0], threshold[1]);
  2252. atomic_set(&obj->ps_thd_val_high, (threshold[0] + obj->ps_cali));
  2253. atomic_set(&obj->ps_thd_val_low, (threshold[1] + obj->ps_cali));
  2254. err = set_psensor_threshold(obj->client);
  2255.  
  2256. if (err < 0) {
  2257. APS_ERR("set_psensor_threshold fail\n");
  2258. return -1;
  2259. }
  2260. return 0;
  2261. }
  2262. static int pa12201001_ps_factory_get_threashold(int32_t threshold[2])
  2263. {
  2264. struct pa12201001_priv *obj = pa12201001_obj;
  2265.  
  2266. threshold[0] = atomic_read(&obj->ps_thd_val_high) - obj->ps_cali;
  2267. threshold[1] = atomic_read(&obj->ps_thd_val_low) - obj->ps_cali;
  2268. return 0;
  2269. }
  2270.  
  2271. static struct alsps_factory_fops pa12201001_factory_fops = {
  2272. .als_enable_sensor = pa12201001_als_factory_enable_sensor,
  2273. .als_get_data = pa12201001_als_factory_get_data,
  2274. .als_get_raw_data = pa12201001_als_factory_get_raw_data,
  2275. .als_enable_calibration = pa12201001_als_factory_enable_calibration,
  2276. .als_clear_cali = pa12201001_als_factory_clear_cali,
  2277. .als_set_cali = pa12201001_als_factory_set_cali,
  2278. .als_get_cali = pa12201001_als_factory_get_cali,
  2279.  
  2280. .ps_enable_sensor = pa12201001_ps_factory_enable_sensor,
  2281. .ps_get_data = pa12201001_ps_factory_get_data,
  2282. .ps_get_raw_data = pa12201001_ps_factory_get_raw_data,
  2283. .ps_enable_calibration = pa12201001_ps_factory_enable_calibration,
  2284. .ps_clear_cali = pa12201001_ps_factory_clear_cali,
  2285. .ps_set_cali = pa12201001_ps_factory_set_cali,
  2286. .ps_get_cali = pa12201001_ps_factory_get_cali,
  2287. .ps_set_threashold = pa12201001_ps_factory_set_threashold,
  2288. .ps_get_threashold = pa12201001_ps_factory_get_threashold,
  2289. };
  2290.  
  2291. static struct alsps_factory_public pa12201001_factory_device = {
  2292. .gain = 1,
  2293. .sensitivity = 1,
  2294. .fops = &pa12201001_factory_fops,
  2295. };
  2296.  
  2297. static int pa12201001_init_client(struct i2c_client *client)
  2298. {
  2299. struct pa12201001_priv *obj = i2c_get_clientdata(client);
  2300.  
  2301. u8 sendvalue=0;
  2302. int res = 0;
  2303. u8 test=0;
  2304. int intmode;
  2305. APS_LOG("[mcz]enter %s\n",__func__);
  2306. //Initialize Sensor
  2307. sendvalue=(PA12_ALS_GAIN << 4);
  2308. res=hwmsen_write_byte(client,REG_CFG0,sendvalue);
  2309.  
  2310. sendvalue=((PA12_LED_CURR << 4)| (PA12_PS_PRST << 2)| (PA12_ALS_PRST));
  2311. res=hwmsen_write_byte(client,REG_CFG1,sendvalue);
  2312.  
  2313. sendvalue=((PA12_PS_MODE << 6)| (PA12_PS_SET << 2));
  2314.  
  2315. APS_LOG("[mcz] REG_CFG2 sendvalue=0x%x\n",sendvalue);
  2316. res=hwmsen_write_byte(client,REG_CFG2,sendvalue);
  2317.  
  2318. sendvalue=((PA12_INT_TYPE << 6)| (PA12_PS_PERIOD << 3)| (PA12_ALS_PERIOD));
  2319. res=hwmsen_write_byte(client,REG_CFG3,sendvalue);
  2320.  
  2321. res=hwmsen_write_byte(client,REG_PS_SET,0x03); //PSET
  2322.  
  2323. // Set ALS threshold
  2324. sendvalue=(PA12_ALS_TH_HIGH >> 8);
  2325. res=hwmsen_write_byte(client,REG_ALS_TH_MSB,sendvalue); //set TH threshold
  2326.  
  2327. sendvalue=(PA12_ALS_TH_HIGH & 0xFF);
  2328. res=hwmsen_write_byte(client,REG_ALS_TH_LSB,sendvalue); //set TH threshold
  2329.  
  2330. sendvalue=(PA12_ALS_TH_LOW >> 8);
  2331. res=hwmsen_write_byte(client,REG_ALS_TL_MSB,sendvalue); //set TL threshold
  2332.  
  2333. sendvalue=(PA12_ALS_TH_LOW & 0xFF);
  2334. res=hwmsen_write_byte(client,REG_ALS_TL_LSB,sendvalue); //set TL threshold
  2335.  
  2336. // Set PS threshold
  2337. sendvalue=PA12_PS_TH_HIGH;
  2338. res=hwmsen_write_byte(client,REG_PS_TH,sendvalue); //set TH threshold
  2339.  
  2340. res=hwmsen_read_byte(client,REG_PS_TH,&sendvalue);
  2341.  
  2342. APS_LOG("[MCZ][PROBE]REG_PS_TH value = 0X%X\n",sendvalue);
  2343.  
  2344. sendvalue=PA12_PS_TH_LOW;
  2345. res=hwmsen_write_byte(client,REG_PS_TL,sendvalue); //set TL threshold
  2346.  
  2347. // Polling Setting
  2348. intmode = (((obj->hw.polling_mode_ps)<<1) | ((obj->hw.polling_mode_als)));
  2349. APS_LOG("[mcz]intmode = 0x%x\n",intmode);
  2350. res=hwmsen_read_byte(client,REG_CFG2,&sendvalue);
  2351. APS_LOG("[mcz] read: REG_CFG2 sendvalue=0x%x\n",sendvalue);
  2352.  
  2353. sendvalue=sendvalue & 0xF3;//new:0xF8;// //clear int set
  2354.  
  2355. APS_LOG("[mcz] after clear int set: REG_CFG2 sendvalue=0x%x\n",sendvalue);
  2356. /*
  2357. switch(intmode)
  2358. {
  2359.  
  2360. case 1:
  2361. //sendvalue=sendvalue | 0x06;//0x00;//new:
  2362. APS_LOG("[mcz] case 1 sendvalue=0x%x\n",sendvalue);
  2363. res=hwmsen_write_byte(client,REG_CFG2,sendvalue); //set int mode:PS int only,ALS polling
  2364. break;
  2365. case 2:
  2366. sendvalue=sendvalue & 0x04;//new: | 0x01;//
  2367. APS_LOG("[mcz] case 2 sendvalue=0x%x\n",sendvalue);
  2368. res=hwmsen_write_byte(client,REG_CFG2,sendvalue); //set int mode:PS polling , ALS int
  2369. break;
  2370. case 3:
  2371. APS_LOG("[mcz] case 3 sendvalue=0x%x\n",sendvalue);
  2372. sendvalue=sendvalue | 0x0C;//new: 0x08;//
  2373. res=hwmsen_write_byte(client,REG_CFG2,sendvalue); //set int mode:ALS PS polling
  2374. break;
  2375. default:
  2376. break;
  2377. }
  2378. */
  2379. sendvalue = 0xC6;
  2380. res=hwmsen_write_byte(client,REG_CFG2,sendvalue); //set int mode:PS int only,ALS polling
  2381.  
  2382. hwmsen_read_byte(client,REG_CFG2,&test);
  2383. APS_LOG("[mcz] after write: REG_CFG2 value=0x%x\n",test);
  2384.  
  2385. if(res < 0)
  2386. {
  2387. APS_ERR("i2c_send function err\n");
  2388. goto EXIT_ERR;
  2389. }
  2390.  
  2391. // Regsit int
  2392. res = pa12201001_setup_eint(client);
  2393. if(res != 0)
  2394. {
  2395. APS_ERR("PA12201001 setup eint: %d\n", res);
  2396. return res;
  2397. }
  2398.  
  2399. return 0;
  2400.  
  2401. EXIT_ERR:
  2402. APS_ERR("pa12201001 init dev fail!!!!: %d\n", res);
  2403. return res;
  2404. }
  2405. /*--------------------------------------------------------------------------------*/
  2406.  
  2407. #if 0
  2408. int pa12201001_ps_operate(void* self, uint32_t command, void* buff_in, int size_in,
  2409. void* buff_out, int size_out, int* actualout)
  2410. {
  2411. int err = 0;
  2412. int value;
  2413. struct hwm_sensor_data* sensor_data;
  2414. struct pa12201001_priv *obj = (struct pa12201001_priv *)self;
  2415. APS_FUN(f);
  2416. switch (command)
  2417. {
  2418. case SENSOR_DELAY:
  2419. APS_ERR("pa12201001 ps delay command!\n");
  2420. if((buff_in == NULL) || (size_in < sizeof(int)))
  2421. {
  2422. APS_ERR("Set delay parameter error!\n");
  2423. err = -EINVAL;
  2424. }
  2425. break;
  2426.  
  2427. case SENSOR_ENABLE:
  2428. APS_ERR("pa12201001 ps enable command!\n");
  2429. if((buff_in == NULL) || (size_in < sizeof(int)))
  2430. {
  2431. APS_ERR("Enable sensor parameter error!\n");
  2432. err = -EINVAL;
  2433. }
  2434. else
  2435. {
  2436. value = *(int *)buff_in;
  2437. if(value)
  2438. {
  2439. if((err = pa12201001_enable_ps(obj->client, 1)))
  2440. {
  2441. APS_ERR("enable ps fail: %d\n", err);
  2442. return -1;
  2443. }
  2444. set_bit(CMC_BIT_PS, &obj->enable);
  2445. }
  2446. else
  2447. {
  2448. if((err = pa12201001_enable_ps(obj->client, 0)))
  2449. {
  2450. APS_ERR("disable ps fail: %d\n", err);
  2451. return -1;
  2452. }
  2453. clear_bit(CMC_BIT_PS, &obj->enable);
  2454. }
  2455. }
  2456. break;
  2457.  
  2458. case SENSOR_GET_DATA:
  2459. APS_ERR("pa12201001 ps get data command!\n");
  2460. if((buff_out == NULL) || (size_out< sizeof(struct hwm_sensor_data)))
  2461. {
  2462. APS_ERR("get sensor data parameter error!\n");
  2463. err = -EINVAL;
  2464. }
  2465. else
  2466. {
  2467. sensor_data = (struct hwm_sensor_data *)buff_out;
  2468.  
  2469. if((err = pa12201001_read_ps(obj->client, &obj->ps)))
  2470. {
  2471. err = -1;;
  2472. }
  2473. else
  2474. {
  2475. sensor_data->values[0] = pa12201001_get_ps_value(obj, obj->ps);
  2476. sensor_data->value_divide = 1;
  2477. sensor_data->status = SENSOR_STATUS_ACCURACY_MEDIUM;
  2478. }
  2479. }
  2480. break;
  2481. default:
  2482. APS_ERR("proxmy sensor operate function no this parameter %d!\n", command);
  2483. err = -1;
  2484. break;
  2485. }
  2486. return err;
  2487. }
  2488.  
  2489. int pa12201001_als_operate(void* self, uint32_t command, void* buff_in, int size_in,
  2490. void* buff_out, int size_out, int* actualout)
  2491. {
  2492. int err = 0;
  2493. int value;
  2494. struct hwm_sensor_data* sensor_data;
  2495. struct pa12201001_priv *obj = (struct pa12201001_priv *)self;
  2496. APS_FUN(f);
  2497. switch (command)
  2498. {
  2499. case SENSOR_DELAY:
  2500. APS_ERR("pa12201001 als delay command!\n");
  2501. if((buff_in == NULL) || (size_in < sizeof(int)))
  2502. {
  2503. APS_ERR("Set delay parameter error!\n");
  2504. err = -EINVAL;
  2505. }
  2506. break;
  2507.  
  2508. case SENSOR_ENABLE:
  2509. APS_ERR("pa12201001 als enable command!\n");
  2510. if((buff_in == NULL) || (size_in < sizeof(int)))
  2511. {
  2512. APS_ERR("Enable sensor parameter error!\n");
  2513. err = -EINVAL;
  2514. }
  2515. else
  2516. {
  2517. value = *(int *)buff_in;
  2518. if(value)
  2519. {
  2520. if((err = pa12201001_enable_als(obj->client, 1)))
  2521. {
  2522. APS_ERR("enable als fail: %d\n", err);
  2523. return -1;
  2524. }
  2525. set_bit(CMC_BIT_ALS, &obj->enable);
  2526. }
  2527. else
  2528. {
  2529. if((err = pa12201001_enable_als(obj->client, 0)))
  2530. {
  2531. APS_ERR("disable als fail: %d\n", err);
  2532. return -1;
  2533. }
  2534. clear_bit(CMC_BIT_ALS, &obj->enable);
  2535. }
  2536.  
  2537. }
  2538. break;
  2539.  
  2540. case SENSOR_GET_DATA:
  2541. APS_ERR("pa12201001 als get data command!\n");
  2542. if((buff_out == NULL) || (size_out< sizeof(struct hwm_sensor_data)))
  2543. {
  2544. APS_ERR("get sensor data parameter error!\n");
  2545. err = -EINVAL;
  2546. }
  2547. else
  2548. {
  2549. sensor_data = (struct hwm_sensor_data *)buff_out;
  2550.  
  2551. if((err = pa12201001_read_als(obj->client, &obj->als)))
  2552. {
  2553. err = -1;;
  2554. }
  2555. else
  2556. {
  2557. #if defined(MTK_AAL_SUPPORT)
  2558. sensor_data->values[0] = obj->als;
  2559. #else
  2560. sensor_data->values[0] = pa12201001_get_als_value(obj, obj->als);
  2561. #endif
  2562. sensor_data->value_divide = 1;
  2563. sensor_data->status = SENSOR_STATUS_ACCURACY_MEDIUM;
  2564. }
  2565. }
  2566. break;
  2567. default:
  2568. APS_ERR("light sensor operate function no this parameter %d!\n", command);
  2569. err = -1;
  2570. break;
  2571. }
  2572.  
  2573. return err;
  2574.  
  2575. }
  2576. #endif
  2577.  
  2578. /*--------------------------------------------------------------------------------*/
  2579.  
  2580. static int pa12201001_get_ps_threshold_setting(int type, int value[2])
  2581. {
  2582. int ps_result = -1;
  2583.  
  2584. if(GET_TH_HIGH == type)
  2585. {
  2586. value[0] = atomic_read(&pa12201001_obj->ps_thd_val_high);
  2587. printk("[ALS/PS][ljj] pa12201001_obj->ps_thd_val_high = %d \n",value[0]);
  2588. }
  2589. else if ( GET_TH_LOW == type)
  2590. {
  2591. value[0] = atomic_read(&pa12201001_obj->ps_thd_val_low);
  2592. printk("[ALS/PS][ljj] pa12201001_obj->ps_thd_val_low = %d \n",value[0]);
  2593. }
  2594. else if ( SET_TH == type)
  2595. {
  2596. //TODO
  2597. }
  2598. else if ( GET_TH_RESULT == type)
  2599. {
  2600. if(pa12201001_read_ps(pa12201001_obj->client, &pa12201001_obj->ps))
  2601. {
  2602. printk("[ALS/PS][ljj] \n");
  2603. return -1;
  2604. }
  2605. if(pa12201001_obj->ps > atomic_read(&pa12201001_obj->ps_thd_val_high))
  2606. {
  2607. ps_result = 1;
  2608. }
  2609. else
  2610. {
  2611. ps_result = 0;
  2612. }
  2613. value[0] = ps_result;
  2614. }
  2615. return 0;
  2616. }
  2617.  
  2618. /*-----------------------------------i2c operations----------------------------------*/
  2619. static int pa12201001_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  2620. {
  2621. struct pa12201001_priv *obj = NULL;
  2622.  
  2623. int err = 0;
  2624. u8 value = 0;
  2625. int ret = 0;
  2626.  
  2627. struct als_control_path als_ctl = { 0 };
  2628. struct als_data_path als_data = { 0 };
  2629. struct ps_control_path ps_ctl = { 0 };
  2630. struct ps_data_path ps_data = { 0 };
  2631.  
  2632. /* get customization and power on */
  2633. if(!(obj = kzalloc(sizeof(*obj), GFP_KERNEL)))
  2634. {
  2635. err = -ENOMEM;
  2636. goto exit;
  2637. }
  2638.  
  2639. err = get_alsps_dts_func(client->dev.of_node, &obj->hw);
  2640. if (err < 0) {
  2641. APS_ERR("get customization info from dts failed\n");
  2642. goto exit_init_failed;
  2643. }
  2644.  
  2645. pa12201001_obj = obj;
  2646.  
  2647. INIT_WORK(&obj->eint_work, pa12201001_eint_work);
  2648.  
  2649. #ifdef CUSTOM_KERNEL_SENSORHUB
  2650. INIT_WORK(&obj->init_done_work, pa12201001_init_done_work);
  2651. #endif
  2652.  
  2653. obj->client = client;
  2654. i2c_set_clientdata(client, obj);
  2655.  
  2656. /*-----------------------------value need to be confirmed-----------------------------------------*/
  2657. atomic_set(&obj->als_debounce, 200);
  2658. atomic_set(&obj->als_deb_on, 0);
  2659. atomic_set(&obj->als_deb_end, 0);
  2660. atomic_set(&obj->ps_debounce, 50);//200
  2661. atomic_set(&obj->ps_deb_on, 0);
  2662. atomic_set(&obj->ps_deb_end, 0);
  2663. atomic_set(&obj->ps_mask, 0);
  2664. atomic_set(&obj->als_suspend, 0);
  2665. atomic_set(&obj->als_cmd_val, 0xDF);
  2666. atomic_set(&obj->ps_cmd_val, 0xC1);
  2667. atomic_set(&obj->ps_thd_val_high, obj->hw.ps_threshold_high);
  2668. atomic_set(&obj->ps_thd_val_low, obj->hw.ps_threshold_low);
  2669. atomic_set(&obj->als_thd_val_high, obj->hw.als_threshold_high);
  2670. atomic_set(&obj->als_thd_val_low, obj->hw.als_threshold_low);
  2671. obj->irq_node = client->dev.of_node;
  2672.  
  2673. obj->enable = 0;
  2674. obj->pending_intr = 0;
  2675. obj->als_level_num = ARRAY_SIZE(obj->hw.als_level);
  2676. obj->als_value_num = ARRAY_SIZE(obj->hw.als_value);
  2677.  
  2678. /*-----------------------------value need to be confirmed-----------------------------------------*/
  2679.  
  2680. BUG_ON(sizeof(obj->als_level) != sizeof(obj->hw.als_level));
  2681. memcpy(obj->als_level, obj->hw.als_level, sizeof(obj->als_level));
  2682. BUG_ON(sizeof(obj->als_value) != sizeof(obj->hw.als_value));
  2683. memcpy(obj->als_value, obj->hw.als_value, sizeof(obj->als_value));
  2684. atomic_set(&obj->i2c_retry, 3);
  2685. set_bit(CMC_BIT_ALS, &obj->enable);
  2686. set_bit(CMC_BIT_PS, &obj->enable);
  2687.  
  2688. pa12201001_i2c_client = client;
  2689.  
  2690.  
  2691. ret=hwmsen_read_byte(client,REG_CFG2,&value);
  2692. APS_LOG("[mcz] ret = 0x%x, value = 0x%x\n",ret, value);
  2693.  
  2694. err = pa12201001_init_client(client);
  2695. if (err)
  2696. {
  2697. goto exit_init_failed;
  2698. }
  2699. APS_LOG("pa12201001_init_client() OK!\n");
  2700.  
  2701. /* err = misc_register(&pa12201001_device); */
  2702. err = alsps_factory_device_register(&pa12201001_factory_device);
  2703. if (err)
  2704. {
  2705. APS_ERR("pa12201001_device register failed\n");
  2706. goto exit_misc_device_register_failed;
  2707. }
  2708. APS_LOG("pa12201001_device misc_register OK!\n");
  2709.  
  2710. /*------------------------pa12201001 attribute file for debug--------------------------------------*/
  2711.  
  2712. err = pa12201001_create_attr(&(pa12201001_init_info.platform_diver_addr->driver));
  2713. if (err)
  2714. {
  2715. APS_ERR("create attribute err = %d\n", err);
  2716. goto exit_create_attr_failed;
  2717. }
  2718. /*------------------------pa12201001 attribute file for debug--------------------------------------*/
  2719.  
  2720.  
  2721. als_ctl.open_report_data= als_open_report_data;
  2722. als_ctl.enable_nodata = als_enable_nodata;
  2723. als_ctl.set_delay = als_set_delay;
  2724. als_ctl.batch = als_batch;
  2725. als_ctl.flush = als_flush;
  2726. als_ctl.is_report_input_direct = false;
  2727. #ifdef CUSTOM_KERNEL_SENSORHUB
  2728. als_ctl.is_support_batch = obj->hw->is_batch_supported_als;
  2729. #else
  2730. als_ctl.is_support_batch = false;
  2731. #endif
  2732.  
  2733. err = als_register_control_path(&als_ctl);
  2734. if(err)
  2735. {
  2736. printk("register fail = %d\n", err);
  2737. goto exit;
  2738. }
  2739.  
  2740. als_data.get_data = als_get_data;
  2741. als_data.als_get_raw_data = pa12201001_get_als_raw_data;
  2742. als_data.vender_div = 100;
  2743. err = als_register_data_path(&als_data);
  2744. if(err)
  2745. {
  2746. printk("tregister fail = %d\n", err);
  2747. goto exit;
  2748. }
  2749.  
  2750. ps_ctl.open_report_data= ps_open_report_data;
  2751. ps_ctl.enable_nodata = ps_enable_nodata;
  2752. ps_ctl.set_delay = ps_set_delay;
  2753. ps_ctl.batch = ps_batch;
  2754. ps_ctl.flush = ps_flush;
  2755. ps_ctl.is_report_input_direct = true;
  2756. #ifdef CUSTOM_KERNEL_SENSORHUB
  2757. ps_ctl.is_support_batch = obj->hw->is_batch_supported_ps;
  2758. #else
  2759. ps_ctl.is_support_batch = false;
  2760. #endif
  2761.  
  2762. ps_ctl.ps_threshold_setting = pa12201001_get_ps_threshold_setting;
  2763.  
  2764. #if defined(AGOLD_PROX_CALI_ENABLE)
  2765. ps_ctl.agold_alsps_set_ps_cali = agold_pa12201001_set_ps_cali;
  2766. ps_ctl.agold_alsps_get_ps_deep_cali_data = agold_pa12201001_get_ps_deep_cali_data;
  2767. ps_ctl.agold_alsps_get_ps_raw_data_for_cali = agold_pa12201001_get_ps_raw_data_for_cali;
  2768. #endif
  2769.  
  2770. err = ps_register_control_path(&ps_ctl);
  2771. if(err)
  2772. {
  2773. printk("register fail = %d\n", err);
  2774. goto exit;
  2775. }
  2776.  
  2777. ps_data.get_data = ps_get_data;
  2778. ps_data.ps_get_raw_data = pa12201001_get_ps_raw_data;
  2779. ps_data.vender_div = 100;
  2780. err = ps_register_data_path(&ps_data);
  2781. if(err)
  2782. {
  2783. printk("tregister fail = %d\n", err);
  2784. goto exit;
  2785. }
  2786.  
  2787. pa12201001_init_flag = 0;
  2788. APS_LOG("%s: OK\n", __func__);
  2789. return 0;
  2790.  
  2791. exit_create_attr_failed:
  2792. exit_misc_device_register_failed:
  2793. /* misc_deregister(&pa12201001_device); */
  2794. exit_init_failed:
  2795. kfree(obj);
  2796. exit:
  2797. obj = NULL;
  2798. pa12201001_obj = NULL;
  2799. pa12201001_i2c_client = NULL;
  2800. APS_ERR("%s: err = %d\n", __func__, err);
  2801. pa12201001_init_flag = -1;
  2802. return err;
  2803. }
  2804.  
  2805. static int pa12201001_i2c_remove(struct i2c_client *client)
  2806. {
  2807. int err;
  2808.  
  2809. err = pa12201001_delete_attr(&pa12201001_i2c_driver.driver);
  2810. if(err)
  2811. {
  2812. APS_ERR("pa12201001_delete_attr fail: %d\n", err);
  2813. }
  2814.  
  2815. /* misc_deregister(&pa12201001_device); */
  2816. alsps_factory_device_deregister(&pa12201001_factory_device);
  2817.  
  2818. pa12201001_i2c_client = NULL;
  2819. i2c_unregister_device(client);
  2820. kfree(i2c_get_clientdata(client));
  2821. return 0;
  2822.  
  2823. }
  2824.  
  2825. static int pa12201001_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  2826. {
  2827. strlcpy(info->type, PA12201001_DEV_NAME, sizeof(info->type));
  2828. return 0;
  2829. }
  2830.  
  2831. static int pa12201001_i2c_suspend(struct device *dev)
  2832. {
  2833. APS_FUN();
  2834. return 0;
  2835. }
  2836.  
  2837. static int pa12201001_i2c_resume(struct device *dev)
  2838. {
  2839. APS_FUN();
  2840. return 0;
  2841. }
  2842.  
  2843. /*----------------------------------------------------------------------------*/
  2844. static int pa12201001_local_init(void)
  2845. {
  2846. if(i2c_add_driver(&pa12201001_i2c_driver))
  2847. {
  2848. APS_ERR("add driver error\n");
  2849. return -1;
  2850. }
  2851. if(-1 == pa12201001_init_flag)
  2852. {
  2853. APS_ERR("[mcz] i2c del driver.\n");
  2854. i2c_del_driver(&pa12201001_i2c_driver);
  2855. return -1;
  2856. }
  2857. return 0;
  2858. }
  2859.  
  2860. /*----------------------------------------------------------------------------*/
  2861. static int pa12201001_remove(void)
  2862. {
  2863. i2c_del_driver(&pa12201001_i2c_driver);
  2864. return 0;
  2865. }
  2866.  
  2867. /*----------------------------------------------------------------------------*/
  2868. static int __init pa12201001_init(void)
  2869. {
  2870. alsps_driver_add(&pa12201001_init_info);
  2871. return 0;
  2872. }
  2873.  
  2874. /*----------------------------------------------------------------------------*/
  2875. static void __exit pa12201001_exit(void)
  2876. {
  2877. APS_FUN();
  2878. }
  2879.  
  2880. /*----------------------------------------------------------------------------*/
  2881. module_init(pa12201001_init);
  2882. module_exit(pa12201001_exit);
  2883.  
  2884. /*----------------------------------------------------------------------------*/
  2885. MODULE_AUTHOR("TXC Corp");
  2886. MODULE_DESCRIPTION("pa12201001 driver");
  2887. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement