epl2182.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. /* drivers/hwmon/mt6516/amit/epl2182.c - EPL2182 ALS/PS driver
  2. *
  3. * Author: MingHsien Hsieh <minghsien.hsieh@mediatek.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/interrupt.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/irq.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/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 <linux/atomic.h>
  27. #include <linux/io.h>
  28. #include "epl2182.h"
  29. #include "upmu_sw.h"
  30. #include "upmu_common.h"
  31. #include <linux/gpio.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/wakelock.h>
  34. #include <linux/sched.h>
  35. #include <alsps.h>
  36. #ifdef CUSTOM_KERNEL_SENSORHUB
  37. #include <SCP_sensorHub.h>
  38. #endif
  39. /******************************************************************************
  40. * extern functions
  41. *******************************************************************************/
  42. #define COMPATIABLE_NAME "mediatek,epl2182"
  43. /******************************************************************************
  44. * configuration
  45. *******************************************************************************/
  46. /* TODO: change ps/als integrationtime */
  47. int PS_INTT = 4;
  48. int ALS_INTT = 7;
  49. #define TXBYTES 2
  50. #define RXBYTES 2
  51. #define PACKAGE_SIZE 2
  52. #define I2C_RETRY_COUNT 3
  53. /* TODO: change delay time */
  54. #define PS_DELAY 10
  55. #define ALS_DELAY 40
  56. /* TODO: parameters for lux equation y = ax + b */
  57. #define LUX_PER_COUNT 1100 /* 1100 = 1.1 * 1000 */
  58. #define IPI_WAIT_RSP_TIMEOUT (HZ/10) /* 100ms */
  59. static DEFINE_MUTEX(epl2182_mutex);
  60. struct epl_raw_data {
  61. u8 raw_bytes[PACKAGE_SIZE];
  62. u16 ps_raw;
  63. u16 ps_state;
  64. u16 ps_int_state;
  65. u16 als_ch0_raw;
  66. u16 als_ch1_raw;
  67. };
  68. #define EPL2182_DEV_NAME "EPL2182"
  69. /*----------------------------------------------------------------------------*/
  70. #define APS_TAG "[ALS/PS] "
  71. #define APS_FUN(f) pr_debug(APS_TAG"%s\n", __func__)
  72. #define APS_ERR(fmt, args...) pr_err(APS_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
  73. #define APS_LOG(fmt, args...) pr_debug(APS_TAG fmt, ##args)
  74. #define APS_DBG(fmt, args...) pr_debug(APS_TAG fmt, ##args)
  75. #define FTM_CUST_ALSPS "/data/epl2182"
  76. #define POWER_NONE_MACRO MT65XX_POWER_NONE
  77. static struct i2c_client *epl2182_i2c_client;
  78. static struct alsps_hw alsps_cust;
  79. static struct alsps_hw *hw = &alsps_cust;
  80. /*----------------------------------------------------------------------------*/
  81. static const struct i2c_device_id epl2182_i2c_id[] = { {"EPL2182", 0}, {} };
  82. /* static struct i2c_board_info __initdata i2c_EPL2182= { I2C_BOARD_INFO("EPL2182", (0X92>>1))}; */
  83. /*----------------------------------------------------------------------------*/
  84. static int epl2182_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  85. static int epl2182_i2c_remove(struct i2c_client *client);
  86. static int epl2182_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
  87. static int alsps_local_init(void);
  88. static int alsps_remove(void);
  89. /*----------------------------------------------------------------------------*/
  90. static int epl2182_i2c_suspend(struct i2c_client *client, pm_message_t msg);
  91. static int epl2182_i2c_resume(struct i2c_client *client);
  92. static irqreturn_t epl2182_eint_func(int irq, void *desc);
  93. static int set_psensor_intr_threshold(uint16_t low_thd, uint16_t high_thd);
  94. static int set_psensor_threshold(struct i2c_client *client);
  95. static struct epl2182_priv *g_epl2182_ptr;
  96. static bool isInterrupt;
  97. static long long int_top_time;
  98. static int int_flag;
  99. /*----------------------------------------------------------------------------*/
  100. enum CMC_TRC {
  101. CMC_TRC_ALS_DATA = 0x0001,
  102. CMC_TRC_PS_DATA = 0X0002,
  103. CMC_TRC_EINT = 0x0004,
  104. CMC_TRC_IOCTL = 0x0008,
  105. CMC_TRC_I2C = 0x0010,
  106. CMC_TRC_CVT_ALS = 0x0020,
  107. CMC_TRC_CVT_PS = 0x0040,
  108. CMC_TRC_DEBUG = 0x0800,
  109. };
  110. /*----------------------------------------------------------------------------*/
  111. enum CMC_BIT {
  112. CMC_BIT_ALS = 1,
  113. CMC_BIT_PS = 2,
  114. };
  115. /*----------------------------------------------------------------------------*/
  116. struct epl2182_i2c_addr { /*define a series of i2c slave address */
  117. u8 write_addr;
  118. u8 ps_thd; /*PS INT threshold */
  119. };
  120. /*----------------------------------------------------------------------------*/
  121. struct epl2182_priv {
  122. struct alsps_hw *hw;
  123. struct i2c_client *client;
  124. struct work_struct eint_work;
  125. struct work_struct data_work;
  126. #ifdef CUSTOM_KERNEL_SENSORHUB
  127. struct work_struct init_done_work;
  128. #endif
  129. /*i2c address group */
  130. struct epl2182_i2c_addr addr;
  131. int enable_pflag;
  132. int enable_lflag;
  133. struct device_node *irq_node;
  134. int irq;
  135. /*misc */
  136. atomic_t trace;
  137. atomic_t i2c_retry;
  138. atomic_t als_suspend;
  139. atomic_t als_debounce; /*debounce time after enabling als */
  140. atomic_t als_deb_on; /*indicates if the debounce is on */
  141. atomic_t als_deb_end; /*the jiffies representing the end of debounce */
  142. atomic_t ps_mask; /*mask ps: always return far away */
  143. atomic_t ps_debounce; /*debounce time after enabling ps */
  144. atomic_t ps_deb_on; /*indicates if the debounce is on */
  145. atomic_t ps_deb_end; /*the jiffies representing the end of debounce */
  146. atomic_t ps_suspend;
  147. /*data */
  148. u16 als;
  149. u16 ps;
  150. u16 lux_per_count;
  151. bool als_enable; /*record current als status */
  152. bool ps_enable; /*record current ps status */
  153. ulong enable; /*record HAL enalbe status */
  154. ulong pending_intr; /*pending interrupt */
  155. /* ulong first_read; // record first read ps and als */
  156. /*data */
  157. u16 als_level_num;
  158. u16 als_value_num;
  159. u32 als_level[C_CUST_ALS_LEVEL - 1];
  160. u32 als_value[C_CUST_ALS_LEVEL];
  161. int ps_cali;
  162. atomic_t ps_thd_val_high; /*the cmd value can't be read, stored in ram */
  163. atomic_t ps_thd_val_low; /*the cmd value can't be read, stored in ram */
  164. };
  165. #ifdef CONFIG_OF
  166. static const struct of_device_id alsps_of_match[] = {
  167. {.compatible = "mediatek,alsps"},
  168. {},
  169. };
  170. #endif
  171. /*----------------------------------------------------------------------------*/
  172. static struct i2c_driver epl2182_i2c_driver = {
  173. .probe = epl2182_i2c_probe,
  174. .remove = epl2182_i2c_remove,
  175. .detect = epl2182_i2c_detect,
  176. .suspend = epl2182_i2c_suspend,
  177. .resume = epl2182_i2c_resume,
  178. .id_table = epl2182_i2c_id,
  179. .driver = {
  180. .name = EPL2182_DEV_NAME,
  181. #ifdef CONFIG_OF
  182. .of_match_table = alsps_of_match,
  183. #endif
  184. },
  185. };
  186. static struct epl2182_priv *epl2182_obj;
  187. static struct epl_raw_data gRawData;
  188. static int alsps_init_flag = -1; /* 0<==>OK -1 <==> fail */
  189. static struct alsps_init_info epl2182_init_info = {
  190. .name = EPL2182_DEV_NAME,
  191. .init = alsps_local_init,
  192. .uninit = alsps_remove,
  193. };
  194. static DECLARE_WAIT_QUEUE_HEAD(wait_rsp_wq);
  195. /* static struct wake_lock als_lock; Bob.chen add for if ps run, the system forbid to goto sleep mode. */
  196. /*
  197. //====================I2C write operation===============//
  198. //regaddr: ELAN epl2182 Register Address.
  199. //bytecount: How many bytes to be written to epl2182 register via i2c bus.
  200. //txbyte: I2C bus transmit byte(s). Single byte(0X01) transmit only slave address.
  201. //data: setting value.
  202. //
  203. // Example: If you want to write single byte to 0x1D register address, show below
  204. // elan_epl2182_I2C_Write(client,0x1D,0x01,0X02,0xff);
  205. //
  206. */
  207. static int elan_epl2182_I2C_Write(struct i2c_client *client, uint8_t regaddr, uint8_t bytecount,
  208. uint8_t txbyte, uint8_t data)
  209. {
  210. uint8_t buffer[2];
  211. int ret = 0;
  212. int retry;
  213. /* APS_DBG("[ELAN epl2182] %s\n", __func__); */
  214. mutex_lock(&epl2182_mutex);
  215. buffer[0] = (regaddr << 3) | bytecount;
  216. buffer[1] = data;
  217. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  218. ret = i2c_master_send(client, buffer, txbyte);
  219. if (ret >= 0)
  220. break;
  221. APS_DBG("epl2182 i2c write error,TXBYTES %d\r\n", ret);
  222. mdelay(10);
  223. }
  224. if (retry >= I2C_RETRY_COUNT) {
  225. mutex_unlock(&epl2182_mutex);
  226. APS_DBG(KERN_ERR "[ELAN epl2182 error] %s i2c write retry over %d\n", __func__,
  227. I2C_RETRY_COUNT);
  228. return -EINVAL;
  229. }
  230. mutex_unlock(&epl2182_mutex);
  231. return ret;
  232. }
  233. /*
  234. //====================I2C read operation===============//
  235. */
  236. static int elan_epl2182_I2C_Read(struct i2c_client *client, uint8_t regaddr, uint8_t bytecount,
  237. uint8_t rxbyte, uint8_t *data)
  238. {
  239. uint8_t buffer[RXBYTES];
  240. int ret = 0, i = 0;
  241. int retry;
  242. /* APS_DBG("[ELAN epl2182] %s\n", __func__); */
  243. mutex_lock(&epl2182_mutex);
  244. buffer[0] = (regaddr << 3) | bytecount;
  245. for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
  246. ret = hwmsen_read_block(client, buffer[0], buffer, rxbyte);
  247. if (ret >= 0)
  248. break;
  249. APS_ERR("epl2182 i2c read error,RXBYTES %d\r\n", ret);
  250. mdelay(10);
  251. }
  252. if (retry >= I2C_RETRY_COUNT) {
  253. APS_ERR(KERN_ERR "[ELAN epl2182 error] %s i2c read retry over %d\n", __func__,
  254. I2C_RETRY_COUNT);
  255. mutex_unlock(&epl2182_mutex);
  256. return -EINVAL;
  257. }
  258. for (i = 0; i < PACKAGE_SIZE; i++)
  259. *data++ = buffer[i];
  260. mutex_unlock(&epl2182_mutex);
  261. return ret;
  262. }
  263. static int elan_epl2182_psensor_enable(struct epl2182_priv *epl_data, int enable)
  264. {
  265. int ret = 0;
  266. uint8_t regdata;
  267. uint8_t read_data[2];
  268. int ps_state;
  269. struct i2c_client *client = epl_data->client;
  270. APS_DBG("[ELAN epl2182] %s enable = %d\n", __func__, enable);
  271. epl_data->enable_pflag = enable;
  272. ret =
  273. elan_epl2182_I2C_Write(client, REG_9, W_SINGLE_BYTE, 0x02,
  274. EPL_INT_DISABLE | EPL_DRIVE_120MA);
  275. if (enable) {
  276. regdata = EPL_SENSING_2_TIME | EPL_PS_MODE | EPL_L_GAIN;
  277. regdata = regdata | (isInterrupt ? EPL_C_SENSING_MODE : EPL_S_SENSING_MODE);
  278. ret = elan_epl2182_I2C_Write(client, REG_0, W_SINGLE_BYTE, 0X02, regdata);
  279. regdata = PS_INTT << 4 | EPL_PST_1_TIME | EPL_10BIT_ADC;
  280. ret = elan_epl2182_I2C_Write(client, REG_1, W_SINGLE_BYTE, 0X02, regdata);
  281. /* set_psensor_intr_threshold(epl_data->hw ->ps_threshold_low,epl_data->hw ->ps_threshold_high); */
  282. set_psensor_threshold(client);
  283. ret = elan_epl2182_I2C_Write(client, REG_7, W_SINGLE_BYTE, 0X02, EPL_C_RESET);
  284. ret = elan_epl2182_I2C_Write(client, REG_7, W_SINGLE_BYTE, 0x02, EPL_C_START_RUN);
  285. ret = elan_epl2182_I2C_Write(client, REG_9, W_SINGLE_BYTE, 0x02, EPL_INT_ACTIVE_LOW | EPL_DRIVE_120MA);
  286. msleep(PS_DELAY);
  287. ret = elan_epl2182_I2C_Read(client, REG_13, R_SINGLE_BYTE, 0x01, read_data);
  288. ps_state = !((read_data[0] & 0x04) >> 2);
  289. APS_DBG("epl2182 ps state = %d, gRawData.ps_state = %d, %s\n", ps_state,
  290. gRawData.ps_state, __func__);
  291. int_flag = ps_state;
  292. schedule_work(&epl_data->data_work);
  293. gRawData.ps_state = ps_state; /* update ps state */
  294. /* APS_LOG("epl2182 gRawData.ps_state = %d, %s\n", gRawData.ps_state, __func__); */
  295. } else {
  296. regdata = EPL_SENSING_2_TIME | EPL_PS_MODE | EPL_L_GAIN;
  297. regdata = regdata | EPL_S_SENSING_MODE;
  298. ret = elan_epl2182_I2C_Write(client, REG_0, W_SINGLE_BYTE, 0X02, regdata);
  299. ret = elan_epl2182_I2C_Write(client, REG_9, W_SINGLE_BYTE, 0x02, EPL_INT_DISABLE | EPL_DRIVE_120MA);
  300. }
  301. if (ret < 0)
  302. APS_ERR("[ELAN epl2182 error]%s: ps enable %d fail\n", __func__, ret);
  303. else
  304. ret = 0;
  305. return ret;
  306. }
  307. static int elan_epl2182_lsensor_enable(struct epl2182_priv *epl_data, int enable)
  308. {
  309. int ret = 0;
  310. uint8_t regdata;
  311. struct i2c_client *client = epl_data->client;
  312. /* APS_LOG("[ELAN epl2182] %s enable = %d\n", __func__, enable); */
  313. epl_data->enable_lflag = enable;
  314. if (enable) {
  315. regdata = EPL_INT_DISABLE;
  316. ret = elan_epl2182_I2C_Write(client, REG_9, W_SINGLE_BYTE, 0x02, regdata);
  317. regdata = EPL_S_SENSING_MODE | EPL_SENSING_4_TIME | EPL_ALS_MODE | EPL_AUTO_GAIN;
  318. ret = elan_epl2182_I2C_Write(client, REG_0, W_SINGLE_BYTE, 0X02, regdata);
  319. regdata = ALS_INTT << 4 | EPL_PST_1_TIME | EPL_10BIT_ADC;
  320. ret = elan_epl2182_I2C_Write(client, REG_1, W_SINGLE_BYTE, 0X02, regdata);
  321. ret = elan_epl2182_I2C_Write(client, REG_10, W_SINGLE_BYTE, 0X02, 0x3e);
  322. ret = elan_epl2182_I2C_Write(client, REG_11, W_SINGLE_BYTE, 0x02, 0x3e);
  323. ret = elan_epl2182_I2C_Write(client, REG_7, W_SINGLE_BYTE, 0X02, EPL_C_RESET);
  324. ret = elan_epl2182_I2C_Write(client, REG_7, W_SINGLE_BYTE, 0x02, EPL_C_START_RUN);
  325. msleep(ALS_DELAY);
  326. }
  327. if (ret < 0)
  328. APS_ERR("[ELAN epl2182 error]%s: als_enable %d fail\n", __func__, ret);
  329. else
  330. ret = 0;
  331. return ret;
  332. }
  333. /* convert raw to lux */
  334. static int epl2182_get_als_value(struct epl2182_priv *obj, u16 als)
  335. {
  336. int idx;
  337. int invalid = 0;
  338. int lux = 0;
  339. if (als < 15)
  340. return 0;
  341. lux = (als * obj->lux_per_count) / 1000;
  342. for (idx = 0; idx < obj->als_level_num; idx++) {
  343. if (lux < obj->hw->als_level[idx])
  344. break;
  345. }
  346. if (idx >= obj->als_value_num) {
  347. APS_ERR("epl2182 exceed range\n");
  348. idx = obj->als_value_num - 1;
  349. }
  350. if (1 == atomic_read(&obj->als_deb_on)) {
  351. unsigned long endt = atomic_read(&obj->als_deb_end);
  352. if (time_after(jiffies, endt))
  353. atomic_set(&obj->als_deb_on, 0);
  354. if (1 == atomic_read(&obj->als_deb_on))
  355. invalid = 1;
  356. }
  357. if (!invalid) {
  358. #if defined(MTK_AAL_SUPPORT)
  359. int level_high = obj->hw->als_level[idx];
  360. int level_low = (idx > 0) ? obj->hw->als_level[idx - 1] : 0;
  361. int level_diff = level_high - level_low;
  362. int value_high = obj->hw->als_value[idx];
  363. int value_low = (idx > 0) ? obj->hw->als_value[idx - 1] : 0;
  364. int value_diff = value_high - value_low;
  365. int value = 0;
  366. if ((level_low >= level_high) || (value_low >= value_high))
  367. value = value_low;
  368. else
  369. value =
  370. (level_diff * value_low + (als - level_low) * value_diff +
  371. ((level_diff + 1) >> 1)) / level_diff;
  372. return value;
  373. #endif
  374. /* APS_DBG("ALS: %05d => %05d\n", als, obj->hw->als_value[idx]); */
  375. return obj->hw->als_value[idx];
  376. }
  377. APS_ERR("ALS: %05d => %05d (-1)\n", als, obj->hw->als_value[idx]);
  378. return -1;
  379. }
  380. static int set_psensor_intr_threshold(uint16_t low_thd, uint16_t high_thd)
  381. {
  382. int ret = 0;
  383. #ifdef CUSTOM_KERNEL_SENSORHUB
  384. SCP_SENSOR_HUB_DATA data;
  385. union EPL2182_CUST_DATA *pCustData;
  386. int len;
  387. /* ps_cali would be add back in SCP side. */
  388. low_thd -= epl2182_obj->ps_cali;
  389. high_thd -= epl2182_obj->ps_cali;
  390. data.set_cust_req.sensorType = ID_PROXIMITY;
  391. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  392. pCustData = (union EPL2182_CUST_DATA *) (&data.set_cust_req.custData);
  393. pCustData->setPSThreshold.action = EPL2182_CUST_ACTION_SET_PS_THRESHODL;
  394. pCustData->setPSThreshold.threshold[0] = low_thd;
  395. pCustData->setPSThreshold.threshold[1] = high_thd;
  396. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->setPSThreshold);
  397. ret = SCP_sensorHub_req_send(&data, &len, 1);
  398. #else /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  399. struct epl2182_priv *epld = epl2182_obj;
  400. struct i2c_client *client = epld->client;
  401. uint8_t high_msb, high_lsb, low_msb, low_lsb;
  402. /* APS_LOG("epl2182 %s: low_thd = 0x%X, high_thd = 0x%x\n",__func__, low_thd, high_thd); */
  403. high_msb = (uint8_t) (high_thd >> 8);
  404. high_lsb = (uint8_t) (high_thd & 0x00ff);
  405. low_msb = (uint8_t) (low_thd >> 8);
  406. low_lsb = (uint8_t) (low_thd & 0x00ff);
  407. elan_epl2182_I2C_Write(client, REG_2, W_SINGLE_BYTE, 0x02, high_lsb);
  408. elan_epl2182_I2C_Write(client, REG_3, W_SINGLE_BYTE, 0x02, high_msb);
  409. elan_epl2182_I2C_Write(client, REG_4, W_SINGLE_BYTE, 0x02, low_lsb);
  410. elan_epl2182_I2C_Write(client, REG_5, W_SINGLE_BYTE, 0x02, low_msb);
  411. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  412. return ret;
  413. }
  414. /*----------------------------------------------------------------------------*/
  415. /*
  416. static void epl2182_dumpReg(struct i2c_client *client)
  417. {
  418. APS_LOG("chip id REG 0x00 value = %8x\n", i2c_smbus_read_byte_data(client, 0x00));
  419. APS_LOG("chip id REG 0x01 value = %8x\n", i2c_smbus_read_byte_data(client, 0x08));
  420. APS_LOG("chip id REG 0x02 value = %8x\n", i2c_smbus_read_byte_data(client, 0x10));
  421. APS_LOG("chip id REG 0x03 value = %8x\n", i2c_smbus_read_byte_data(client, 0x18));
  422. APS_LOG("chip id REG 0x04 value = %8x\n", i2c_smbus_read_byte_data(client, 0x20));
  423. APS_LOG("chip id REG 0x05 value = %8x\n", i2c_smbus_read_byte_data(client, 0x28));
  424. APS_LOG("chip id REG 0x06 value = %8x\n", i2c_smbus_read_byte_data(client, 0x30));
  425. APS_LOG("chip id REG 0x07 value = %8x\n", i2c_smbus_read_byte_data(client, 0x38));
  426. APS_LOG("chip id REG 0x09 value = %8x\n", i2c_smbus_read_byte_data(client, 0x48));
  427. APS_LOG("chip id REG 0x0D value = %8x\n", i2c_smbus_read_byte_data(client, 0x68));
  428. APS_LOG("chip id REG 0x0E value = %8x\n", i2c_smbus_read_byte_data(client, 0x70));
  429. APS_LOG("chip id REG 0x0F value = %8x\n", i2c_smbus_read_byte_data(client, 0x71));
  430. APS_LOG("chip id REG 0x10 value = %8x\n", i2c_smbus_read_byte_data(client, 0x80));
  431. APS_LOG("chip id REG 0x11 value = %8x\n", i2c_smbus_read_byte_data(client, 0x88));
  432. APS_LOG("chip id REG 0x13 value = %8x\n", i2c_smbus_read_byte_data(client, 0x98));
  433. }
  434. */
  435. /*----------------------------------------------------------------------------*/
  436. int hw8k_init_device(struct i2c_client *client)
  437. {
  438. APS_LOG("hw8k_init_device.........\r\n");
  439. epl2182_i2c_client = client;
  440. APS_LOG("epl2182 I2C Addr==[0x%x],line=%d\n", epl2182_i2c_client->addr, __LINE__);
  441. return 0;
  442. }
  443. /*----------------------------------------------------------------------------*/
  444. int epl2182_get_addr(struct alsps_hw *hw, struct epl2182_i2c_addr *addr)
  445. {
  446. if (!hw || !addr)
  447. return -EFAULT;
  448. addr->write_addr = hw->i2c_addr[0];
  449. return 0;
  450. }
  451. /*----------------------------------------------------------------------------*/
  452. static void epl2182_power(struct alsps_hw *hw, unsigned int on)
  453. {
  454. static unsigned int power_on;
  455. APS_FUN();
  456. if (hw->power_id != POWER_NONE_MACRO) {
  457. if (power_on == on) {
  458. APS_LOG("ignore power control: %d\n", on);
  459. } else if (on) {
  460. if (!hwPowerOn(hw->power_id, hw->power_vol, "EPL2182"))
  461. APS_ERR("power on fails!!\n");
  462. } else {
  463. if (!hwPowerDown(hw->power_id, "EPL2182"))
  464. APS_ERR("power off fail!!\n");
  465. }
  466. }
  467. power_on = on;
  468. }
  469. /*----------------------------------------------------------------------------*/
  470. int epl2182_read_als(struct i2c_client *client, u16 *data)
  471. {
  472. struct epl2182_priv *obj = i2c_get_clientdata(client);
  473. uint8_t read_data[2];
  474. if (client == NULL) {
  475. APS_DBG("CLIENT CANN'T EQUL NULL\n");
  476. return -1;
  477. }
  478. elan_epl2182_I2C_Read(obj->client, REG_14, R_TWO_BYTE, 0x02, read_data);
  479. gRawData.als_ch0_raw = (read_data[1] << 8) | read_data[0];
  480. elan_epl2182_I2C_Read(obj->client, REG_16, R_TWO_BYTE, 0x02, read_data);
  481. gRawData.als_ch1_raw = (read_data[1] << 8) | read_data[0];
  482. *data = gRawData.als_ch1_raw;
  483. /* APS_LOG("epl2182 read als raw data = %d\n", gRawData.als_ch1_raw); */
  484. return 0;
  485. }
  486. /*----------------------------------------------------------------------------*/
  487. long epl2182_read_ps(struct i2c_client *client, u16 *data)
  488. {
  489. struct epl2182_priv *obj = i2c_get_clientdata(client);
  490. uint8_t read_data[2];
  491. if (client == NULL) {
  492. APS_DBG("CLIENT CANN'T EQUL NULL\n");
  493. return -1;
  494. }
  495. /* elan_epl2182_I2C_Read(client,REG_13,R_SINGLE_BYTE,0x01,read_data); */
  496. /* APS_DBG("epl2182_read_als read REG_13 raw_bytes: 0x%x\n",read_data[0]); */
  497. /* setting = read_data[0]; */
  498. /* if((setting&(3<<4))!=0x10) */
  499. /* { */
  500. /* APS_ERR("epl2182 read ps data in wrong mode\n"); */
  501. /* } */
  502. /* gRawData.ps_state= !((read_data[0]&0x04)>>2); */
  503. /* APS_LOG("epl2182 ps state = %d, %s\n", gRawData.ps_state, __func__); */
  504. elan_epl2182_I2C_Read(obj->client, REG_16, R_TWO_BYTE, 0x02, read_data);
  505. gRawData.ps_raw = (read_data[1] << 8) | read_data[0];
  506. if (gRawData.ps_raw < obj->ps_cali)
  507. *data = 0;
  508. else
  509. *data = gRawData.ps_raw - obj->ps_cali;
  510. /* APS_LOG("epl2182 read ps raw data = %d\n", gRawData.ps_raw); */
  511. /* APS_LOG("epl2182 read ps binary data = %d\n", gRawData.ps_state); */
  512. return 0;
  513. }
  514. /*----------------------------------------------------------------------------*/
  515. #ifdef CUSTOM_KERNEL_SENSORHUB
  516. static void alsps_init_done_work(struct work_struct *work)
  517. {
  518. struct epl2182_priv *obj = epl2182_obj;
  519. union EPL2182_CUST_DATA *p_cust_data;
  520. SCP_SENSOR_HUB_DATA data;
  521. int max_cust_data_size_per_packet;
  522. int i;
  523. uint sizeOfCustData;
  524. uint len;
  525. char *p = (char *)obj->hw;
  526. APS_FUN();
  527. p_cust_data = (union EPL2182_CUST_DATA *) data.set_cust_req.custData;
  528. sizeOfCustData = sizeof(*(obj->hw));
  529. max_cust_data_size_per_packet =
  530. sizeof(data.set_cust_req.custData) - offsetof(struct EPL2182_SET_CUST, data);
  531. for (i = 0; sizeOfCustData > 0; i++) {
  532. data.set_cust_req.sensorType = ID_PROXIMITY;
  533. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  534. p_cust_data->setCust.action = EPL2182_CUST_ACTION_SET_CUST;
  535. p_cust_data->setCust.part = i;
  536. if (sizeOfCustData > max_cust_data_size_per_packet)
  537. len = max_cust_data_size_per_packet;
  538. else
  539. len = sizeOfCustData;
  540. memcpy(p_cust_data->setCust.data, p, len);
  541. sizeOfCustData -= len;
  542. p += len;
  543. len +=
  544. offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + offsetof(struct EPL2182_SET_CUST,
  545. data);
  546. SCP_sensorHub_req_send(&data, &len, 1);
  547. }
  548. data.set_cust_req.sensorType = ID_PROXIMITY;
  549. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  550. p_cust_data->setEintInfo.action = EPL2182_CUST_ACTION_SET_EINT_INFO;
  551. p_cust_data->setEintInfo.gpio_mode = GPIO_ALS_EINT_PIN_M_EINT;
  552. p_cust_data->setEintInfo.gpio_pin = GPIO_ALS_EINT_PIN;
  553. p_cust_data->setEintInfo.eint_num = CUST_EINT_ALS_NUM;
  554. p_cust_data->setEintInfo.eint_is_deb_en = CUST_EINT_ALS_DEBOUNCE_EN;
  555. p_cust_data->setEintInfo.eint_type = CUST_EINT_ALS_TYPE;
  556. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(p_cust_data->setEintInfo);
  557. SCP_sensorHub_req_send(&data, &len, 1);
  558. }
  559. #endif
  560. /*----------------------------------------------------------------------------*/
  561. #ifndef CUSTOM_KERNEL_SENSORHUB
  562. static irqreturn_t epl2182_eint_func(int irq, void *desc)
  563. {
  564. struct epl2182_priv *obj = g_epl2182_ptr;
  565. int_top_time = sched_clock();
  566. if (!obj)
  567. return IRQ_HANDLED;
  568. #ifndef FPGA_EARLY_PORTING
  569. disable_irq_nosync(epl2182_obj->irq);
  570. #endif /* #ifndef FPGA_EARLY_PORTING */
  571. schedule_work(&obj->eint_work);
  572. return IRQ_HANDLED;
  573. }
  574. #else
  575. static int alsps_irq_handler(void *data, uint len)
  576. {
  577. struct epl2182_priv *obj = epl2182_obj;
  578. SCP_SENSOR_HUB_DATA_P rsp = (SCP_SENSOR_HUB_DATA_P) data;
  579. if (!obj)
  580. return -1;
  581. APS_ERR("len = %d, type = %d, sction = %d, event = %d, data = %d\n", len,
  582. rsp->rsp.sensorType, rsp->rsp.action, rsp->rsp.errCode, rsp->notify_rsp.data[1]);
  583. switch (rsp->rsp.action) {
  584. case SENSOR_HUB_NOTIFY:
  585. switch (rsp->notify_rsp.event) {
  586. case SCP_INIT_DONE:
  587. schedule_work(&obj->init_done_work);
  588. break;
  589. case SCP_NOTIFY:
  590. if (EPL2182_NOTIFY_PROXIMITY_CHANGE == rsp->notify_rsp.data[0]) {
  591. gRawData.ps_state = rsp->notify_rsp.data[1];
  592. schedule_work(&obj->eint_work);
  593. } else if (EPL2182_NOTIFY_PROXIMITY_NOT_CHANGE == rsp->notify_rsp.data[0]) {
  594. gRawData.ps_state = rsp->notify_rsp.data[1];
  595. schedule_work(&obj->data_work);
  596. } else {
  597. APS_ERR("Unknown notify\n");
  598. }
  599. break;
  600. default:
  601. APS_ERR("Error sensor hub notify\n");
  602. break;
  603. }
  604. break;
  605. default:
  606. APS_ERR("Error sensor hub action\n");
  607. break;
  608. }
  609. return 0;
  610. }
  611. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  612. /*----------------------------------------------------------------------------*/
  613. static void epl2182_eint_work(struct work_struct *work)
  614. {
  615. #ifdef CUSTOM_KERNEL_SENSORHUB
  616. int res = 0;
  617. res = ps_report_interrupt_data(gRawData.ps_state);
  618. if (res != 0)
  619. APS_ERR("epl2182_eint_work err: %d\n", res);
  620. #else /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  621. struct epl2182_priv *epld = g_epl2182_ptr;
  622. int err;
  623. uint8_t read_data[2];
  624. int flag;
  625. if (epld->enable_pflag == 0)
  626. goto exit;
  627. APS_ERR("epl2182 int top half time = %lld\n", int_top_time);
  628. elan_epl2182_I2C_Read(epld->client, REG_16, R_TWO_BYTE, 0x02, read_data);
  629. gRawData.ps_raw = (read_data[1] << 8) | read_data[0];
  630. APS_LOG("epl2182 ps raw_data = %d\n", gRawData.ps_raw);
  631. elan_epl2182_I2C_Read(epld->client, REG_13, R_SINGLE_BYTE, 0x01, read_data);
  632. flag = !((read_data[0] & 0x04) >> 2);
  633. if (flag != gRawData.ps_state) {
  634. APS_LOG("epl2182 eint work gRawData.ps_state = %d, flag = %d, %s\n",
  635. gRawData.ps_state, flag, __func__);
  636. gRawData.ps_state = flag; /* update ps state */
  637. /* let up layer to know */
  638. err = ps_report_interrupt_data(flag);
  639. if (err)
  640. APS_ERR("epl2182 call ps_report_interrupt_data fail = %d\n", err);
  641. /* APS_LOG("epl2182 xxxxx eint work\n"); */
  642. } else {
  643. APS_LOG("epl2182 eint data won't update");
  644. }
  645. exit:
  646. elan_epl2182_I2C_Write(epld->client, REG_7, W_SINGLE_BYTE, 0x02, EPL_DATA_UNLOCK);
  647. if (test_bit(CMC_BIT_ALS, &epld->enable)) {
  648. /* APS_DBG("als enable eint mask ps!\n"); */
  649. #ifndef FPGA_EARLY_PORTING
  650. disable_irq(epl2182_obj->irq);
  651. #endif /* #ifndef FPGA_EARLY_PORTING */
  652. } else {
  653. /* APS_DBG("als disable eint unmask ps!\n"); */
  654. #ifndef FPGA_EARLY_PORTING
  655. enable_irq(epl2182_obj->irq);
  656. #endif /* #ifndef FPGA_EARLY_PORTING */
  657. }
  658. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  659. }
  660. /*----------------------------------------------------------------------------*/
  661. int epl2182_setup_eint(struct i2c_client *client)
  662. {
  663. #ifdef CUSTOM_KERNEL_SENSORHUB
  664. int err = 0;
  665. err = SCP_sensorHub_rsp_registration(ID_PROXIMITY, alsps_irq_handler);
  666. #else /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  667. struct epl2182_priv *obj = i2c_get_clientdata(client);
  668. int ret;
  669. u32 ints[2] = { 0, 0 };
  670. struct pinctrl *pinctrl;
  671. struct pinctrl_state *pins_default;
  672. struct pinctrl_state *pins_cfg;
  673. struct platform_device *alsps_pdev = get_alsps_platformdev();
  674. APS_LOG("epl2182_setup_eint\n");
  675. g_epl2182_ptr = obj;
  676. /*configure to GPIO function, external interrupt */
  677. #ifndef FPGA_EARLY_PORTING
  678. /* gpio setting */
  679. pinctrl = devm_pinctrl_get(&alsps_pdev->dev);
  680. if (IS_ERR(pinctrl)) {
  681. ret = PTR_ERR(pinctrl);
  682. APS_ERR("Cannot find alsps pinctrl!\n");
  683. }
  684. pins_default = pinctrl_lookup_state(pinctrl, "pin_default");
  685. if (IS_ERR(pins_default)) {
  686. ret = PTR_ERR(pins_default);
  687. APS_ERR("Cannot find alsps pinctrl default!\n");
  688. }
  689. pins_cfg = pinctrl_lookup_state(pinctrl, "pin_cfg");
  690. if (IS_ERR(pins_cfg)) {
  691. ret = PTR_ERR(pins_cfg);
  692. APS_ERR("Cannot find alsps pinctrl pin_cfg!\n");
  693. }
  694. pinctrl_select_state(pinctrl, pins_cfg);
  695. if (epl2182_obj->irq_node) {
  696. of_property_read_u32_array(epl2182_obj->irq_node, "debounce", ints,
  697. ARRAY_SIZE(ints));
  698. gpio_request(ints[0], "p-sensor");
  699. gpio_set_debounce(ints[0], ints[1]);
  700. APS_LOG("ints[0] = %d, ints[1] = %d!!\n", ints[0], ints[1]);
  701. epl2182_obj->irq = irq_of_parse_and_map(epl2182_obj->irq_node, 0);
  702. APS_LOG("cm36652_obj->irq = %d\n", epl2182_obj->irq);
  703. if (!epl2182_obj->irq) {
  704. APS_ERR("irq_of_parse_and_map fail!!\n");
  705. return -EINVAL;
  706. }
  707. if (request_irq
  708. (epl2182_obj->irq, epl2182_eint_func, IRQF_TRIGGER_NONE, "ALS-eint", NULL)) {
  709. APS_ERR("IRQ LINE NOT AVAILABLE!!\n");
  710. return -EINVAL;
  711. }
  712. enable_irq(epl2182_obj->irq);
  713. } else {
  714. APS_ERR("null irq node!!\n");
  715. return -EINVAL;
  716. }
  717. enable_irq(epl2182_obj->irq);
  718. #endif /* #ifndef FPGA_EARLY_PORTING */
  719. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  720. return 0;
  721. }
  722. /*----------------------------------------------------------------------------*/
  723. static int epl2182_init_client(struct i2c_client *client)
  724. {
  725. struct epl2182_priv *obj = i2c_get_clientdata(client);
  726. int err = 0;
  727. APS_LOG("epl2182 [Agold spl] I2C Addr==[0x%x],line=%d\n", epl2182_i2c_client->addr,
  728. __LINE__);
  729. /* interrupt mode */
  730. APS_FUN();
  731. if (obj->hw->polling_mode_ps == 0) {
  732. #ifndef FPGA_EARLY_PORTING
  733. disable_irq(epl2182_obj->irq);
  734. #endif /* #ifndef FPGA_EARLY_PORTING */
  735. err = epl2182_setup_eint(client);
  736. if (err) {
  737. APS_ERR("setup eint: %d\n", err);
  738. return err;
  739. }
  740. APS_LOG("epl2182 interrupt setup\n");
  741. }
  742. err = hw8k_init_device(client);
  743. if (err != 0) {
  744. APS_ERR("init dev: %d\n", err);
  745. return err;
  746. }
  747. return err;
  748. }
  749. static void epl2182_check_ps_data(struct work_struct *work)
  750. {
  751. #ifdef CUSTOM_KERNEL_SENSORHUB
  752. int res = 0;
  753. res = ps_report_interrupt_data(gRawData.ps_state);
  754. if (res != 0) {
  755. APS_ERR("epl2182_eint_work err: %d\n", res);
  756. return res;
  757. }
  758. #else
  759. int flag;
  760. uint8_t read_data[2];
  761. int err = 0;
  762. struct epl2182_priv *epld = epl2182_obj;
  763. elan_epl2182_I2C_Read(epld->client, REG_13, R_SINGLE_BYTE, 0x01, read_data);
  764. flag = !((read_data[0] & 0x04) >> 2);
  765. if (flag != int_flag) {
  766. APS_ERR("epl2182 call hwmsen_get_interrupt_data fail = %d\n", err);
  767. goto exit;
  768. } else {
  769. /* let up layer to know */
  770. APS_LOG("epl2182 int_flag state = %d, %s\n", int_flag, __func__);
  771. err = ps_report_interrupt_data(int_flag);
  772. if (err != 0) {
  773. APS_ERR("epl2182 call ps_report_interrupt_data fail = %d\n", err);
  774. goto exit;
  775. }
  776. }
  777. exit:
  778. return;
  779. #endif
  780. }
  781. /*----------------------------------------------------------------------------*/
  782. static ssize_t epl2182_show_reg(struct device_driver *ddri, char *buf)
  783. {
  784. struct i2c_client *client = epl2182_obj->client;
  785. ssize_t len = 0;
  786. if (!epl2182_obj) {
  787. APS_ERR("epl2182_obj is null!!\n");
  788. return 0;
  789. }
  790. len +=
  791. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x00 value = %8x\n",
  792. i2c_smbus_read_byte_data(client, 0x00));
  793. len +=
  794. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x01 value = %8x\n",
  795. i2c_smbus_read_byte_data(client, 0x08));
  796. len +=
  797. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x02 value = %8x\n",
  798. i2c_smbus_read_byte_data(client, 0x10));
  799. len +=
  800. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x03 value = %8x\n",
  801. i2c_smbus_read_byte_data(client, 0x18));
  802. len +=
  803. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x04 value = %8x\n",
  804. i2c_smbus_read_byte_data(client, 0x20));
  805. len +=
  806. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x05 value = %8x\n",
  807. i2c_smbus_read_byte_data(client, 0x28));
  808. len +=
  809. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x06 value = %8x\n",
  810. i2c_smbus_read_byte_data(client, 0x30));
  811. len +=
  812. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x07 value = %8x\n",
  813. i2c_smbus_read_byte_data(client, 0x38));
  814. len +=
  815. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x09 value = %8x\n",
  816. i2c_smbus_read_byte_data(client, 0x48));
  817. len +=
  818. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x0D value = %8x\n",
  819. i2c_smbus_read_byte_data(client, 0x68));
  820. len +=
  821. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x0E value = %8x\n",
  822. i2c_smbus_read_byte_data(client, 0x70));
  823. len +=
  824. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x0F value = %8x\n",
  825. i2c_smbus_read_byte_data(client, 0x71));
  826. len +=
  827. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x10 value = %8x\n",
  828. i2c_smbus_read_byte_data(client, 0x80));
  829. len +=
  830. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x11 value = %8x\n",
  831. i2c_smbus_read_byte_data(client, 0x88));
  832. len +=
  833. snprintf(buf + len, PAGE_SIZE - len, "chip id REG 0x13 value = %8x\n",
  834. i2c_smbus_read_byte_data(client, 0x98));
  835. return len;
  836. }
  837. /*----------------------------------------------------------------------------*/
  838. static ssize_t epl2182_show_status(struct device_driver *ddri, char *buf)
  839. {
  840. ssize_t len = 0;
  841. struct epl2182_priv *epld = epl2182_obj;
  842. uint8_t read_data[2];
  843. if (!epl2182_obj) {
  844. APS_ERR("epl2182_obj is null!!\n");
  845. return 0;
  846. }
  847. elan_epl2182_I2C_Write(epld->client, REG_7, W_SINGLE_BYTE, 0x02, EPL_DATA_LOCK);
  848. elan_epl2182_I2C_Read(epld->client, REG_16, R_TWO_BYTE, 0x02, read_data);
  849. gRawData.ps_raw = (read_data[1] << 8) | read_data[0];
  850. APS_LOG("ch1 raw_data = %d\n", gRawData.ps_raw);
  851. elan_epl2182_I2C_Write(epld->client, REG_7, W_SINGLE_BYTE, 0x02, EPL_DATA_UNLOCK);
  852. len += snprintf(buf + len, PAGE_SIZE - len, "ch1 raw is %d\n", gRawData.ps_raw);
  853. return len;
  854. }
  855. /*----------------------------------------------------------------------------*/
  856. static ssize_t epl2182_store_als_int_time(struct device_driver *ddri, const char *buf, size_t count)
  857. {
  858. if (!epl2182_obj) {
  859. APS_ERR("epl2182_obj is null!!\n");
  860. return 0;
  861. }
  862. if (kstrtoint(buf, 10, &ALS_INTT))
  863. APS_LOG("als int time is %d\n", ALS_INTT);
  864. return count;
  865. }
  866. /*----------------------------------------------------------------------------*/
  867. static ssize_t epl2182_store_ps_int_time(struct device_driver *ddri, const char *buf, size_t count)
  868. {
  869. if (!epl2182_obj) {
  870. APS_ERR("epl2182_obj is null!!\n");
  871. return 0;
  872. }
  873. if (kstrtoint(buf, 10, &PS_INTT))
  874. APS_LOG("ps int time is %d\n", PS_INTT);
  875. return count;
  876. }
  877. /*----------------------------------------------------------------------------*/
  878. static DRIVER_ATTR(status, S_IWUSR | S_IRUGO, epl2182_show_status, NULL);
  879. static DRIVER_ATTR(reg, S_IWUSR | S_IRUGO, epl2182_show_reg, NULL);
  880. static DRIVER_ATTR(als_int_time, S_IWUSR | S_IRUGO, NULL, epl2182_store_als_int_time);
  881. static DRIVER_ATTR(ps_int_time, S_IWUSR | S_IRUGO, NULL, epl2182_store_ps_int_time);
  882. /*----------------------------------------------------------------------------*/
  883. static struct driver_attribute *epl2182_attr_list[] = {
  884. &driver_attr_status,
  885. &driver_attr_reg,
  886. &driver_attr_als_int_time,
  887. &driver_attr_ps_int_time,
  888. };
  889. /*----------------------------------------------------------------------------*/
  890. static int epl2182_create_attr(struct device_driver *driver)
  891. {
  892. int idx, err = 0;
  893. int num = (int)(sizeof(epl2182_attr_list) / sizeof(epl2182_attr_list[0]));
  894. if (driver == NULL)
  895. return -EINVAL;
  896. for (idx = 0; idx < num; idx++) {
  897. err = driver_create_file(driver, epl2182_attr_list[idx]);
  898. if (err) {
  899. APS_ERR("driver_create_file (%s) = %d\n", epl2182_attr_list[idx]->attr.name,
  900. err);
  901. break;
  902. }
  903. }
  904. return err;
  905. }
  906. /*----------------------------------------------------------------------------*/
  907. static int epl2182_delete_attr(struct device_driver *driver)
  908. {
  909. int idx, err = 0;
  910. int num = (int)(sizeof(epl2182_attr_list) / sizeof(epl2182_attr_list[0]));
  911. if (!driver)
  912. return -EINVAL;
  913. for (idx = 0; idx < num; idx++)
  914. driver_remove_file(driver, epl2182_attr_list[idx]);
  915. return err;
  916. }
  917. /******************************************************************************
  918. * Function Configuration
  919. ******************************************************************************/
  920. static int epl2182_open(struct inode *inode, struct file *file)
  921. {
  922. file->private_data = epl2182_i2c_client;
  923. APS_FUN();
  924. if (!file->private_data) {
  925. APS_ERR("null pointer!!\n");
  926. return -EINVAL;
  927. }
  928. return nonseekable_open(inode, file);
  929. }
  930. /*----------------------------------------------------------------------------*/
  931. static int epl2182_release(struct inode *inode, struct file *file)
  932. {
  933. APS_FUN();
  934. file->private_data = NULL;
  935. return 0;
  936. }
  937. /*----------------------------------------------------------------------------*/
  938. static int set_psensor_threshold(struct i2c_client *client)
  939. {
  940. struct epl2182_priv *obj = i2c_get_clientdata(client);
  941. int res = 0;
  942. #ifdef CUSTOM_KERNEL_SENSORHUB
  943. SCP_SENSOR_HUB_DATA data;
  944. union EPL2182_CUST_DATA *pCustData;
  945. int len;
  946. int32_t ps_thd_val_low, ps_thd_val_high;
  947. ps_thd_val_low = atomic_read(&obj->ps_thd_val_low);
  948. ps_thd_val_high = atomic_read(&obj->ps_thd_val_high);
  949. ps_thd_val_low -= obj->ps_cali;
  950. ps_thd_val_high -= obj->ps_cali;
  951. data.set_cust_req.sensorType = ID_PROXIMITY;
  952. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  953. pCustData = (union EPL2182_CUST_DATA *) (&data.set_cust_req.custData);
  954. pCustData->setPSThreshold.action = EPL2182_CUST_ACTION_SET_PS_THRESHODL;
  955. pCustData->setPSThreshold.threshold[0] = ps_thd_val_low;
  956. pCustData->setPSThreshold.threshold[1] = ps_thd_val_high;
  957. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->setPSThreshold);
  958. res = SCP_sensorHub_req_send(&data, &len, 1);
  959. #else
  960. int databuf[2];
  961. APS_ERR("set_psensor_threshold function high: 0x%x, low:0x%x\n",
  962. atomic_read(&obj->ps_thd_val_high), atomic_read(&obj->ps_thd_val_low));
  963. databuf[0] = atomic_read(&obj->ps_thd_val_low);
  964. databuf[1] = atomic_read(&obj->ps_thd_val_high); /* threshold value need to confirm */
  965. res = set_psensor_intr_threshold(databuf[0], databuf[1]);
  966. #endif
  967. return res;
  968. }
  969. /*----------------------------------------------------------------------------*/
  970. static long epl2182_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  971. {
  972. struct i2c_client *client = (struct i2c_client *)file->private_data;
  973. struct epl2182_priv *obj = i2c_get_clientdata(client);
  974. int err = 0;
  975. void __user *ptr = (void __user *)arg;
  976. int dat;
  977. uint32_t enable;
  978. int ps_result;
  979. int ps_cali;
  980. int threshold[2];
  981. #ifdef CUSTOM_KERNEL_SENSORHUB
  982. SCP_SENSOR_HUB_DATA data;
  983. union EPL2182_CUST_DATA *pCustData;
  984. int len;
  985. data.set_cust_req.sensorType = ID_PROXIMITY;
  986. data.set_cust_req.action = SENSOR_HUB_SET_CUST;
  987. pCustData = (union EPL2182_CUST_DATA *) (&data.set_cust_req.custData);
  988. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  989. switch (cmd) {
  990. case ALSPS_SET_PS_MODE:
  991. if (copy_from_user(&enable, ptr, sizeof(enable))) {
  992. err = -EFAULT;
  993. goto err_out;
  994. }
  995. if (enable) {
  996. if (isInterrupt) {
  997. err = elan_epl2182_psensor_enable(obj, 1);
  998. if (err != 0) {
  999. APS_ERR("enable ps fail: %d\n", err);
  1000. return -1;
  1001. }
  1002. }
  1003. set_bit(CMC_BIT_PS, &obj->enable);
  1004. } else {
  1005. if (isInterrupt) {
  1006. err = elan_epl2182_psensor_enable(obj, 0);
  1007. if (err != 0) {
  1008. APS_ERR("disable ps fail: %d\n", err);
  1009. return -1;
  1010. }
  1011. }
  1012. clear_bit(CMC_BIT_PS, &obj->enable);
  1013. }
  1014. break;
  1015. case ALSPS_GET_PS_MODE:
  1016. enable = test_bit(CMC_BIT_PS, &obj->enable);
  1017. if (copy_to_user(ptr, &enable, sizeof(enable))) {
  1018. err = -EFAULT;
  1019. goto err_out;
  1020. }
  1021. break;
  1022. case ALSPS_GET_PS_DATA:
  1023. err = elan_epl2182_psensor_enable(obj, 1);
  1024. if (err != 0) {
  1025. APS_ERR("enable ps fail: %d\n", err);
  1026. return -1;
  1027. }
  1028. epl2182_read_ps(obj->client, &obj->ps);
  1029. dat = gRawData.ps_state;
  1030. APS_LOG("ioctl ps state value = %d\n", dat);
  1031. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  1032. err = -EFAULT;
  1033. goto err_out;
  1034. }
  1035. break;
  1036. case ALSPS_GET_PS_RAW_DATA:
  1037. err = elan_epl2182_psensor_enable(obj, 1);
  1038. if (err != 0) {
  1039. APS_ERR("enable ps fail: %d\n", err);
  1040. return -1;
  1041. }
  1042. epl2182_read_ps(obj->client, &obj->ps);
  1043. dat = obj->ps;
  1044. APS_LOG("ioctl ps raw value = %d\n", dat);
  1045. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  1046. err = -EFAULT;
  1047. goto err_out;
  1048. }
  1049. break;
  1050. case ALSPS_SET_ALS_MODE:
  1051. if (copy_from_user(&enable, ptr, sizeof(enable))) {
  1052. err = -EFAULT;
  1053. goto err_out;
  1054. }
  1055. if (enable)
  1056. set_bit(CMC_BIT_ALS, &obj->enable);
  1057. else
  1058. clear_bit(CMC_BIT_ALS, &obj->enable);
  1059. break;
  1060. case ALSPS_GET_ALS_MODE:
  1061. enable = test_bit(CMC_BIT_ALS, &obj->enable);
  1062. if (copy_to_user(ptr, &enable, sizeof(enable))) {
  1063. err = -EFAULT;
  1064. goto err_out;
  1065. }
  1066. break;
  1067. case ALSPS_GET_ALS_DATA:
  1068. err = elan_epl2182_lsensor_enable(obj, 1);
  1069. if (err != 0) {
  1070. APS_ERR("disable als fail: %d\n", err);
  1071. return -1;
  1072. }
  1073. epl2182_read_als(obj->client, &obj->als);
  1074. dat = epl2182_get_als_value(obj, obj->als);
  1075. APS_LOG("ioctl get als data = %d\n", dat);
  1076. if (obj->enable_pflag && isInterrupt) {
  1077. err = elan_epl2182_psensor_enable(obj, 1);
  1078. if (err != 0) {
  1079. APS_ERR("disable ps fail: %d\n", err);
  1080. return -1;
  1081. }
  1082. }
  1083. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  1084. err = -EFAULT;
  1085. goto err_out;
  1086. }
  1087. break;
  1088. case ALSPS_GET_ALS_RAW_DATA:
  1089. err = elan_epl2182_lsensor_enable(obj, 1);
  1090. if (err != 0) {
  1091. APS_ERR("disable als fail: %d\n", err);
  1092. return -1;
  1093. }
  1094. epl2182_read_als(obj->client, &obj->als);
  1095. dat = obj->als;
  1096. APS_DBG("ioctl get als raw data = %d\n", dat);
  1097. if (obj->enable_pflag && isInterrupt) {
  1098. err = elan_epl2182_psensor_enable(obj, 1);
  1099. if (err != 0) {
  1100. APS_ERR("disable ps fail: %d\n", err);
  1101. return -1;
  1102. }
  1103. }
  1104. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  1105. err = -EFAULT;
  1106. goto err_out;
  1107. }
  1108. break;
  1109. case ALSPS_GET_PS_TEST_RESULT:
  1110. err = epl2182_read_ps(obj->client, &obj->ps);
  1111. if (err)
  1112. goto err_out;
  1113. if (obj->ps > atomic_read(&obj->ps_thd_val_high))
  1114. ps_result = 0;
  1115. else
  1116. ps_result = 1;
  1117. if (copy_to_user(ptr, &ps_result, sizeof(ps_result))) {
  1118. err = -EFAULT;
  1119. goto err_out;
  1120. }
  1121. break;
  1122. case ALSPS_IOCTL_CLR_CALI:
  1123. if (copy_from_user(&dat, ptr, sizeof(dat))) {
  1124. err = -EFAULT;
  1125. goto err_out;
  1126. }
  1127. if (dat == 0)
  1128. obj->ps_cali = 0;
  1129. #ifdef CUSTOM_KERNEL_SENSORHUB
  1130. pCustData->clearCali.action = EPL2182_CUST_ACTION_CLR_CALI;
  1131. len =
  1132. offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->clearCali);
  1133. err = SCP_sensorHub_req_send(&data, &len, 1);
  1134. #endif
  1135. break;
  1136. case ALSPS_IOCTL_GET_CALI:
  1137. ps_cali = obj->ps_cali;
  1138. APS_ERR("%s set ps_calix%x\n", __func__, obj->ps_cali);
  1139. if (copy_to_user(ptr, &ps_cali, sizeof(ps_cali))) {
  1140. err = -EFAULT;
  1141. goto err_out;
  1142. }
  1143. break;
  1144. case ALSPS_IOCTL_SET_CALI:
  1145. if (copy_from_user(&ps_cali, ptr, sizeof(ps_cali))) {
  1146. err = -EFAULT;
  1147. goto err_out;
  1148. }
  1149. obj->ps_cali = ps_cali;
  1150. #ifdef CUSTOM_KERNEL_SENSORHUB
  1151. pCustData->setCali.action = EPL2182_CUST_ACTION_SET_CALI;
  1152. pCustData->setCali.cali = ps_cali;
  1153. len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->setCali);
  1154. err = SCP_sensorHub_req_send(&data, &len, 1);
  1155. #endif
  1156. APS_ERR("%s set ps_calix%x\n", __func__, obj->ps_cali);
  1157. break;
  1158. case ALSPS_SET_PS_THRESHOLD:
  1159. if (copy_from_user(threshold, ptr, sizeof(threshold))) {
  1160. err = -EFAULT;
  1161. goto err_out;
  1162. }
  1163. APS_ERR("%s set threshold high: 0x%x, low: 0x%x\n", __func__, threshold[0],
  1164. threshold[1]);
  1165. atomic_set(&obj->ps_thd_val_high, (threshold[0] + obj->ps_cali));
  1166. atomic_set(&obj->ps_thd_val_low, (threshold[1] + obj->ps_cali)); /* need to confirm */
  1167. set_psensor_threshold(obj->client);
  1168. break;
  1169. case ALSPS_GET_PS_THRESHOLD_HIGH:
  1170. APS_ERR("%s get threshold high before cali: 0x%x\n", __func__,
  1171. atomic_read(&obj->ps_thd_val_high));
  1172. threshold[0] = atomic_read(&obj->ps_thd_val_high) - obj->ps_cali;
  1173. APS_ERR("%s set ps_calix%x\n", __func__, obj->ps_cali);
  1174. APS_ERR("%s get threshold high: 0x%x\n", __func__, threshold[0]);
  1175. if (copy_to_user(ptr, &threshold[0], sizeof(threshold[0]))) {
  1176. err = -EFAULT;
  1177. goto err_out;
  1178. }
  1179. break;
  1180. case ALSPS_GET_PS_THRESHOLD_LOW:
  1181. APS_ERR("%s get threshold low before cali: 0x%x\n", __func__,
  1182. atomic_read(&obj->ps_thd_val_low));
  1183. threshold[0] = atomic_read(&obj->ps_thd_val_low) - obj->ps_cali;
  1184. APS_ERR("%s set ps_calix%x\n", __func__, obj->ps_cali);
  1185. APS_ERR("%s get threshold low: 0x%x\n", __func__, threshold[0]);
  1186. if (copy_to_user(ptr, &threshold[0], sizeof(threshold[0]))) {
  1187. err = -EFAULT;
  1188. goto err_out;
  1189. }
  1190. break;
  1191. default:
  1192. APS_ERR("%s not supported = 0x%04x", __func__, cmd);
  1193. err = -ENOIOCTLCMD;
  1194. break;
  1195. }
  1196. err_out:
  1197. return err;
  1198. }
  1199. /*----------------------------------------------------------------------------*/
  1200. static const struct file_operations epl2182_fops = {
  1201. .owner = THIS_MODULE,
  1202. .open = epl2182_open,
  1203. .release = epl2182_release,
  1204. .unlocked_ioctl = epl2182_unlocked_ioctl,
  1205. };
  1206. /*----------------------------------------------------------------------------*/
  1207. static struct miscdevice epl2182_device = {
  1208. .minor = MISC_DYNAMIC_MINOR,
  1209. .name = "als_ps",
  1210. .fops = &epl2182_fops,
  1211. };
  1212. /*----------------------------------------------------------------------------*/
  1213. static int epl2182_i2c_suspend(struct i2c_client *client, pm_message_t msg)
  1214. {
  1215. /* struct epl2182_priv *obj = i2c_get_clientdata(client); */
  1216. int err = 0;
  1217. APS_FUN();
  1218. return err;
  1219. }
  1220. /*----------------------------------------------------------------------------*/
  1221. static int epl2182_i2c_resume(struct i2c_client *client)
  1222. {
  1223. /* struct epl2182_priv *obj = i2c_get_clientdata(client); */
  1224. int err = 0;
  1225. APS_FUN();
  1226. return err;
  1227. }
  1228. /*--------------------------------------------------------------------------------*/
  1229. static int als_open_report_data(int open)
  1230. {
  1231. /* should queuq work to report event if is_report_input_direct=true */
  1232. return 0;
  1233. }
  1234. /*--------------------------------------------------------------------------------*/
  1235. /* if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL */
  1236. static int als_enable_nodata(int en)
  1237. {
  1238. int res = 0;
  1239. #ifdef CUSTOM_KERNEL_SENSORHUB
  1240. SCP_SENSOR_HUB_DATA req;
  1241. int len;
  1242. #endif
  1243. APS_LOG("epl2182_obj als enable value = %d\n", en);
  1244. #ifdef CUSTOM_KERNEL_SENSORHUB
  1245. req.activate_req.sensorType = ID_LIGHT;
  1246. req.activate_req.action = SENSOR_HUB_ACTIVATE;
  1247. req.activate_req.enable = en;
  1248. len = sizeof(req.activate_req);
  1249. res = SCP_sensorHub_req_send(&req, &len, 1);
  1250. #else
  1251. if (!epl2182_obj) {
  1252. APS_ERR("epl2182_obj is null!!\n");
  1253. return -1;
  1254. }
  1255. APS_LOG("epl2182_obj als enable value = %d\n", en);
  1256. if (en) {
  1257. set_bit(CMC_BIT_ALS, &epl2182_obj->enable);
  1258. #ifndef FPGA_EARLY_PORTING
  1259. disable_irq(epl2182_obj->irq);
  1260. #endif /* #ifndef FPGA_EARLY_PORTING */
  1261. /* APS_DBG("enable als mask ps!\n"); */
  1262. } else {
  1263. #ifndef FPGA_EARLY_PORTING
  1264. enable_irq(epl2182_obj->irq);
  1265. #endif /* #ifndef FPGA_EARLY_PORTING */
  1266. /* APS_DBG("disable als unmask ps!\n"); */
  1267. clear_bit(CMC_BIT_ALS, &epl2182_obj->enable);
  1268. if (epl2182_obj->enable_pflag && isInterrupt) {
  1269. res = elan_epl2182_psensor_enable(epl2182_obj, 1);
  1270. if (res != 0) {
  1271. APS_ERR("enable ps fail: %d\n", res);
  1272. return -1;
  1273. }
  1274. }
  1275. }
  1276. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  1277. if (res) {
  1278. APS_ERR("als_enable_nodata is failed!!\n");
  1279. return -1;
  1280. }
  1281. return 0;
  1282. }
  1283. /*--------------------------------------------------------------------------------*/
  1284. static int als_set_delay(u64 ns)
  1285. {
  1286. return 0;
  1287. }
  1288. /*--------------------------------------------------------------------------------*/
  1289. static int als_get_data(int *value, int *status)
  1290. {
  1291. int err = 0;
  1292. #ifdef CUSTOM_KERNEL_SENSORHUB
  1293. SCP_SENSOR_HUB_DATA req;
  1294. int len;
  1295. #else
  1296. /* struct epl2182_priv *obj =NULL; */
  1297. #endif
  1298. #ifdef CUSTOM_KERNEL_SENSORHUB
  1299. req.get_data_req.sensorType = ID_LIGHT;
  1300. req.get_data_req.action = SENSOR_HUB_GET_DATA;
  1301. len = sizeof(req.get_data_req);
  1302. err = SCP_sensorHub_req_send(&req, &len, 1);
  1303. if (err) {
  1304. APS_ERR("SCP_sensorHub_req_send fail!\n");
  1305. } else {
  1306. *value = req.get_data_rsp.int16_Data[0];
  1307. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1308. }
  1309. if (atomic_read(&epl2182_obj->trace) & CMC_TRC_PS_DATA)
  1310. APS_LOG("value = %d\n", *value);
  1311. #else
  1312. if (!epl2182_obj) {
  1313. APS_ERR("epl2182_obj is null!!\n");
  1314. return -1;
  1315. }
  1316. if (0 == atomic_read(&epl2182_obj->als_suspend)) {
  1317. err = elan_epl2182_lsensor_enable(epl2182_obj, 1);
  1318. if (err != 0) {
  1319. APS_ERR("enable als fail: %d\n", err);
  1320. return -1;
  1321. }
  1322. epl2182_read_als(epl2182_obj->client, &epl2182_obj->als);
  1323. *value = epl2182_get_als_value(epl2182_obj, epl2182_obj->als);
  1324. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1325. /* APS_LOG("get als data->values[0] = %d\n", sensor_data->values[0]); */
  1326. } else {
  1327. APS_LOG("epl2182 sensor in suspend!\n");
  1328. return -1;
  1329. }
  1330. if (epl2182_obj->enable_pflag && isInterrupt) {
  1331. err = elan_epl2182_psensor_enable(epl2182_obj, 1);
  1332. if (err != 0) {
  1333. APS_ERR("enable ps fail: %d\n", err);
  1334. return -1;
  1335. }
  1336. }
  1337. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  1338. return err;
  1339. }
  1340. /*--------------------------------------------------------------------------------*/
  1341. /* if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL */
  1342. static int ps_open_report_data(int open)
  1343. {
  1344. /* should queuq work to report event if is_report_input_direct=true */
  1345. return 0;
  1346. }
  1347. /*--------------------------------------------------------------------------------*/
  1348. /* if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL */
  1349. static int ps_enable_nodata(int en)
  1350. {
  1351. int res = 0;
  1352. #ifdef CUSTOM_KERNEL_SENSORHUB
  1353. SCP_SENSOR_HUB_DATA req;
  1354. int len;
  1355. #endif
  1356. APS_LOG("epl2182_obj als enable value = %d\n", en);
  1357. #ifdef CUSTOM_KERNEL_SENSORHUB
  1358. req.activate_req.sensorType = ID_PROXIMITY;
  1359. req.activate_req.action = SENSOR_HUB_ACTIVATE;
  1360. req.activate_req.enable = en;
  1361. len = sizeof(req.activate_req);
  1362. res = SCP_sensorHub_req_send(&req, &len, 1);
  1363. #else
  1364. if (!epl2182_obj) {
  1365. APS_ERR("epl2182_obj is null!!\n");
  1366. return -1;
  1367. }
  1368. APS_LOG("epl2182_obj als enable value = %d\n", en);
  1369. if (en) {
  1370. if (isInterrupt) {
  1371. res = elan_epl2182_psensor_enable(epl2182_obj, 1);
  1372. if (res != 0) {
  1373. APS_ERR("enable ps fail: %d\n", res);
  1374. return -1;
  1375. }
  1376. }
  1377. set_bit(CMC_BIT_PS, &epl2182_obj->enable);
  1378. } else {
  1379. if (isInterrupt) {
  1380. res = elan_epl2182_psensor_enable(epl2182_obj, 0);
  1381. if (res != 0) {
  1382. APS_ERR("disable ps fail: %d\n", res);
  1383. return -1;
  1384. }
  1385. }
  1386. clear_bit(CMC_BIT_PS, &epl2182_obj->enable);
  1387. }
  1388. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  1389. if (res) {
  1390. APS_ERR("als_enable_nodata is failed!!\n");
  1391. return -1;
  1392. }
  1393. return 0;
  1394. }
  1395. /*--------------------------------------------------------------------------------*/
  1396. static int ps_set_delay(u64 ns)
  1397. {
  1398. return 0;
  1399. }
  1400. /*--------------------------------------------------------------------------------*/
  1401. static int ps_get_data(int *value, int *status)
  1402. {
  1403. int err = 0;
  1404. #ifdef CUSTOM_KERNEL_SENSORHUB
  1405. SCP_SENSOR_HUB_DATA req;
  1406. int len;
  1407. #endif
  1408. #ifdef CUSTOM_KERNEL_SENSORHUB
  1409. req.get_data_req.sensorType = ID_PROXIMITY;
  1410. req.get_data_req.action = SENSOR_HUB_GET_DATA;
  1411. len = sizeof(req.get_data_req);
  1412. err = SCP_sensorHub_req_send(&req, &len, 1);
  1413. if (err) {
  1414. APS_ERR("SCP_sensorHub_req_send fail!\n");
  1415. } else {
  1416. *value = req.get_data_rsp.int16_Data[0];
  1417. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1418. APS_LOG("get_data_rsp.int16_Data[0]= 0x%x\n", req.get_data_rsp.int16_Data[0]);
  1419. }
  1420. if (atomic_read(&epl2182_obj->trace) & CMC_TRC_PS_DATA)
  1421. APS_LOG("value = %d\n", *value);
  1422. #else
  1423. if (!epl2182_obj) {
  1424. APS_ERR("epl2182_obj is null!!\n");
  1425. return -1;
  1426. }
  1427. err = elan_epl2182_psensor_enable(epl2182_obj, 1);
  1428. if (err != 0) {
  1429. APS_ERR("enable ps fail: %d\n", err);
  1430. return -1;
  1431. }
  1432. epl2182_read_ps(epl2182_obj->client, &epl2182_obj->ps);
  1433. *value = gRawData.ps_state;
  1434. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1435. #endif /* #ifdef CUSTOM_KERNEL_SENSORHUB */
  1436. return err;
  1437. }
  1438. /*----------------------------------------------------------------------------*/
  1439. static int epl2182_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  1440. {
  1441. strcpy(info->type, EPL2182_DEV_NAME);
  1442. return 0;
  1443. }
  1444. /*----------------------------------------------------------------------------*/
  1445. static int epl2182_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1446. {
  1447. struct epl2182_priv *obj;
  1448. struct als_control_path als_ctl = { 0 };
  1449. struct als_data_path als_data = { 0 };
  1450. struct ps_control_path ps_ctl = { 0 };
  1451. struct ps_data_path ps_data = { 0 };
  1452. int err = 0;
  1453. APS_FUN();
  1454. /* epl2182_dumpReg(client); */
  1455. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  1456. if (!obj) {
  1457. err = -ENOMEM;
  1458. goto exit;
  1459. }
  1460. memset(obj, 0, sizeof(*obj));
  1461. epl2182_obj = obj;
  1462. obj->hw = hw;
  1463. epl2182_get_addr(obj->hw, &obj->addr);
  1464. APS_ERR("addr is 0x%x!\n", obj->addr.write_addr);
  1465. epl2182_obj->als_level_num =
  1466. sizeof(epl2182_obj->hw->als_level) / sizeof(epl2182_obj->hw->als_level[0]);
  1467. epl2182_obj->als_value_num =
  1468. sizeof(epl2182_obj->hw->als_value) / sizeof(epl2182_obj->hw->als_value[0]);
  1469. BUG_ON(sizeof(epl2182_obj->als_level) != sizeof(epl2182_obj->hw->als_level));
  1470. memcpy(epl2182_obj->als_level, epl2182_obj->hw->als_level, sizeof(epl2182_obj->als_level));
  1471. BUG_ON(sizeof(epl2182_obj->als_value) != sizeof(epl2182_obj->hw->als_value));
  1472. memcpy(epl2182_obj->als_value, epl2182_obj->hw->als_value, sizeof(epl2182_obj->als_value));
  1473. INIT_WORK(&obj->eint_work, epl2182_eint_work);
  1474. #ifdef CUSTOM_KERNEL_SENSORHUB
  1475. INIT_WORK(&obj->init_done_work, alsps_init_done_work);
  1476. #endif
  1477. INIT_WORK(&obj->data_work, epl2182_check_ps_data);
  1478. init_waitqueue_head(&wait_rsp_wq);
  1479. obj->client = client;
  1480. #ifdef FPGA_EARLY_PORTING
  1481. obj->client->timing = 100;
  1482. #else
  1483. obj->client->timing = 400;
  1484. #endif
  1485. i2c_set_clientdata(client, obj);
  1486. atomic_set(&obj->als_debounce, 2000);
  1487. atomic_set(&obj->als_deb_on, 0);
  1488. atomic_set(&obj->als_deb_end, 0);
  1489. atomic_set(&obj->ps_debounce, 1000);
  1490. atomic_set(&obj->ps_deb_on, 0);
  1491. atomic_set(&obj->ps_deb_end, 0);
  1492. atomic_set(&obj->ps_mask, 0);
  1493. atomic_set(&obj->trace, 0x00);
  1494. atomic_set(&obj->als_suspend, 0);
  1495. atomic_set(&obj->ps_thd_val_high, obj->hw->ps_threshold_high);
  1496. atomic_set(&obj->ps_thd_val_low, obj->hw->ps_threshold_low);
  1497. obj->irq_node = of_find_compatible_node(NULL, NULL, "mediatek, als-eint");
  1498. obj->ps_cali = 0;
  1499. obj->ps_enable = 0;
  1500. obj->als_enable = 0;
  1501. obj->lux_per_count = LUX_PER_COUNT;
  1502. obj->enable = 0;
  1503. obj->pending_intr = 0;
  1504. gRawData.ps_state = -1;
  1505. atomic_set(&obj->i2c_retry, 3);
  1506. epl2182_i2c_client = client;
  1507. elan_epl2182_I2C_Write(client, REG_0, W_SINGLE_BYTE, 0x02, EPL_S_SENSING_MODE);
  1508. elan_epl2182_I2C_Write(client, REG_9, W_SINGLE_BYTE, 0x02, EPL_INT_DISABLE);
  1509. err = epl2182_init_client(client);
  1510. if (err)
  1511. goto exit_init_failed;
  1512. APS_ERR("epl2182_init_client OK!\n");
  1513. err = misc_register(&epl2182_device);
  1514. if (err) {
  1515. APS_ERR("epl2182_device register failed\n");
  1516. goto exit_misc_device_register_failed;
  1517. }
  1518. err = epl2182_create_attr(&epl2182_init_info.platform_diver_addr->driver);
  1519. if (err) {
  1520. APS_ERR("create attribute err = %d\n", err);
  1521. goto exit_create_attr_failed;
  1522. }
  1523. if (obj->hw->polling_mode_ps != 1)
  1524. isInterrupt = true;
  1525. als_ctl.open_report_data = als_open_report_data;
  1526. als_ctl.enable_nodata = als_enable_nodata;
  1527. als_ctl.set_delay = als_set_delay;
  1528. als_ctl.is_report_input_direct = false;
  1529. #ifdef CUSTOM_KERNEL_SENSORHUB
  1530. als_ctl.is_support_batch = obj->hw->is_batch_supported_als;
  1531. #else
  1532. als_ctl.is_support_batch = false;
  1533. #endif
  1534. err = als_register_control_path(&als_ctl);
  1535. if (err) {
  1536. APS_ERR("register fail = %d\n", err);
  1537. goto exit_sensor_obj_attach_fail;
  1538. }
  1539. als_data.get_data = als_get_data;
  1540. als_data.vender_div = 100;
  1541. err = als_register_data_path(&als_data);
  1542. if (err) {
  1543. APS_ERR("tregister fail = %d\n", err);
  1544. goto exit_sensor_obj_attach_fail;
  1545. }
  1546. ps_ctl.open_report_data = ps_open_report_data;
  1547. ps_ctl.enable_nodata = ps_enable_nodata;
  1548. ps_ctl.set_delay = ps_set_delay;
  1549. ps_ctl.is_report_input_direct = false;
  1550. #ifdef CUSTOM_KERNEL_SENSORHUB
  1551. ps_ctl.is_support_batch = obj->hw->is_batch_supported_ps;
  1552. #else
  1553. ps_ctl.is_support_batch = false;
  1554. #endif
  1555. err = ps_register_control_path(&ps_ctl);
  1556. if (err) {
  1557. APS_ERR("register fail = %d\n", err);
  1558. goto exit_sensor_obj_attach_fail;
  1559. }
  1560. ps_data.get_data = ps_get_data;
  1561. ps_data.vender_div = 100;
  1562. err = ps_register_data_path(&ps_data);
  1563. if (err) {
  1564. APS_ERR("tregister fail = %d\n", err);
  1565. goto exit_sensor_obj_attach_fail;
  1566. }
  1567. err = batch_register_support_info(ID_LIGHT, als_ctl.is_support_batch, 1, 0);
  1568. if (err) {
  1569. APS_ERR("register light batch support err = %d\n", err);
  1570. goto exit_sensor_obj_attach_fail;
  1571. }
  1572. err = batch_register_support_info(ID_PROXIMITY, ps_ctl.is_support_batch, 1, 0);
  1573. if (err) {
  1574. APS_ERR("register proximity batch support err = %d\n", err);
  1575. goto exit_sensor_obj_attach_fail;
  1576. }
  1577. alsps_init_flag = 0;
  1578. APS_LOG("%s: OK\n", __func__);
  1579. return 0;
  1580. exit_create_attr_failed:
  1581. exit_sensor_obj_attach_fail:
  1582. misc_deregister(&epl2182_device);
  1583. exit_misc_device_register_failed:
  1584. exit_init_failed:
  1585. kfree(obj);
  1586. exit:
  1587. epl2182_i2c_client = NULL;
  1588. APS_ERR("%s: err = %d\n", __func__, err);
  1589. alsps_init_flag = -1;
  1590. return err;
  1591. }
  1592. /*----------------------------------------------------------------------------*/
  1593. static int epl2182_i2c_remove(struct i2c_client *client)
  1594. {
  1595. int err;
  1596. err = epl2182_delete_attr(&epl2182_init_info.platform_diver_addr->driver);
  1597. if (err)
  1598. APS_ERR("epl2182_delete_attr fail: %d\n", err);
  1599. err = misc_deregister(&epl2182_device);
  1600. if (err)
  1601. APS_ERR("misc_deregister fail: %d\n", err);
  1602. epl2182_i2c_client = NULL;
  1603. i2c_unregister_device(client);
  1604. kfree(i2c_get_clientdata(client));
  1605. return 0;
  1606. }
  1607. /*----------------------------------------------------------------------------*/
  1608. static int alsps_local_init(void)
  1609. {
  1610. APS_FUN();
  1611. epl2182_power(hw, 1);
  1612. if (i2c_add_driver(&epl2182_i2c_driver)) {
  1613. APS_ERR("add driver error\n");
  1614. return -1;
  1615. }
  1616. APS_ERR("add driver epl2182_i2c_driver ok!\n");
  1617. if (-1 == alsps_init_flag)
  1618. return -1;
  1619. /* APS_DBG("fwq loccal init---\n"); */
  1620. return 0;
  1621. }
  1622. /*----------------------------------------------------------------------------*/
  1623. static int alsps_remove(void)
  1624. {
  1625. APS_FUN();
  1626. epl2182_power(hw, 0);
  1627. APS_ERR("EPL2182 remove\n");
  1628. i2c_del_driver(&epl2182_i2c_driver);
  1629. return 0;
  1630. }
  1631. /*----------------------------------------------------------------------------*/
  1632. static int __init epl2182_init(void)
  1633. {
  1634. hw = get_alsps_dts_func(COMPATIABLE_NAME, hw);
  1635. APS_LOG("%s: i2c_number=%d\n", __func__, hw->i2c_num);
  1636. alsps_driver_add(&epl2182_init_info);
  1637. return 0;
  1638. }
  1639. /*----------------------------------------------------------------------------*/
  1640. static void __exit epl2182_exit(void)
  1641. {
  1642. APS_FUN();
  1643. }
  1644. /*----------------------------------------------------------------------------*/
  1645. module_init(epl2182_init);
  1646. module_exit(epl2182_exit);
  1647. /*----------------------------------------------------------------------------*/
  1648. MODULE_AUTHOR("yucong.xiong@mediatek.com");
  1649. MODULE_DESCRIPTION("EPL2182 ALSPS driver");
  1650. MODULE_LICENSE("GPL");