SHARE
TWEET

Untitled

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