tmd27723.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /*
  2. * Copyright (c) 2010 SAMSUNG
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  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. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/i2c.h>
  22. #include <linux/fs.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <linux/delay.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/leds.h>
  29. #include <linux/gpio.h>
  30. #include <linux/wakelock.h>
  31. #include <linux/slab.h>
  32. #include <linux/input.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/module.h>
  36. #include <linux/sensors_core.h>
  37. #include <linux/sensor/tmd27723.h>
  38. #include <linux/regulator/consumer.h>
  39. #include <linux/of_gpio.h>
  40. /* Note about power vs enable/disable:
  41. * The chip has two functions, proximity and ambient light sensing.
  42. * There is no separate power enablement to the two functions (unlike
  43. * the Capella CM3602/3623).
  44. * This module implements two drivers: /dev/proximity and /dev/light.
  45. * When either driver is enabled (via sysfs attributes), we give power
  46. * to the chip. When both are disabled, we remove power from the chip.
  47. * In suspend, we remove power if light is disabled but not if proximity is
  48. * enabled (proximity is allowed to wakeup from suspend).
  49. *
  50. * There are no ioctls for either driver interfaces. Output is via
  51. * input device framework and control via sysfs attributes.
  52. */
  53. /* taos debug */
  54. /*#define DEBUG 1*/
  55. /*#define TAOS_DEBUG*/
  56. #define taos_dbgmsg(str, args...) pr_info("%s: " str, __func__, ##args)
  57. #define TAOS_DEBUG
  58. #ifdef TAOS_DEBUG
  59. #define gprintk(fmt, x...) \
  60. printk(KERN_INFO "%s(%d):" fmt, __func__, __LINE__, ## x)
  61. #else
  62. #define gprintk(x...) do { } while (0)
  63. #endif
  64. #define VENDOR_NAME "TAOS"
  65. #ifdef CONFIG_OPTICAL_TAOS_TMD2672X
  66. #define CHIP_NAME "TMD2672X"
  67. #define CHIP_ID 0x3B
  68. #else
  69. #define CHIP_NAME "TMD27723"
  70. #define CHIP_ID 0x39
  71. #endif
  72. #define GPIO_PROX_LED_EN 8
  73. #define OFFSET_FILE_PATH "/efs/prox_cal"
  74. /* Triton register offsets */
  75. #define CNTRL 0x00
  76. #define ALS_TIME 0X01
  77. #define PRX_TIME 0x02
  78. #define WAIT_TIME 0x03
  79. #define ALS_MINTHRESHLO 0X04
  80. #define ALS_MINTHRESHHI 0X05
  81. #define ALS_MAXTHRESHLO 0X06
  82. #define ALS_MAXTHRESHHI 0X07
  83. #define PRX_MINTHRESHLO 0X08
  84. #define PRX_MINTHRESHHI 0X09
  85. #define PRX_MAXTHRESHLO 0X0A
  86. #define PRX_MAXTHRESHHI 0X0B
  87. #define INTERRUPT 0x0C
  88. #define PRX_CFG 0x0D
  89. #define PRX_COUNT 0x0E
  90. #define GAIN 0x0F
  91. #define REVID 0x11
  92. #define CHIPID 0x12
  93. #define STATUS 0x13
  94. #define ALS_CHAN0LO 0x14
  95. #define ALS_CHAN0HI 0x15
  96. #define ALS_CHAN1LO 0x16
  97. #define ALS_CHAN1HI 0x17
  98. #define PRX_LO 0x18
  99. #define PRX_HI 0x19
  100. #define PRX_OFFSET 0x1E
  101. #define TEST_STATUS 0x1F
  102. /*Triton cmd reg masks*/
  103. #define CMD_REG 0X80
  104. #define CMD_BYTE_RW 0x00
  105. #define CMD_WORD_BLK_RW 0x20
  106. #define CMD_SPL_FN 0x60
  107. #define CMD_PROX_INTCLR 0X05
  108. #define CMD_ALS_INTCLR 0X06
  109. #define CMD_PROXALS_INTCLR 0X07
  110. #define CMD_TST_REG 0X08
  111. #define CMD_USER_REG 0X09
  112. /* Triton cntrl reg masks */
  113. #define CNTL_REG_CLEAR 0x00
  114. #define CNTL_PROX_INT_ENBL 0X20
  115. #define CNTL_ALS_INT_ENBL 0X10
  116. #define CNTL_WAIT_TMR_ENBL 0X08
  117. #define CNTL_PROX_DET_ENBL 0X04
  118. #define CNTL_ADC_ENBL 0x02
  119. #define CNTL_PWRON 0x01
  120. #define CNTL_ALSPON_ENBL 0x03
  121. #define CNTL_INTALSPON_ENBL 0x13
  122. #define CNTL_PROXPON_ENBL 0x0F
  123. #define CNTL_INTPROXPON_ENBL 0x2F
  124. /* Triton status reg masks */
  125. #define STA_ADCVALID 0x01
  126. #define STA_PRXVALID 0x02
  127. #define STA_ADC_PRX_VALID 0x03
  128. #define STA_ADCINTR 0x10
  129. #define STA_PRXINTR 0x20
  130. #define MAX_LUX 40000
  131. #define MIN 1
  132. #define ADC_BUFFER_NUM 6
  133. #define PROX_AVG_COUNT 40
  134. enum {
  135. LIGHT_ENABLED = BIT(0),
  136. PROXIMITY_ENABLED = BIT(1),
  137. };
  138. #define TAOS_PROX_MAX 1023
  139. #define TAOS_PROX_MIN 0
  140. #define OFFSET_ARRAY_LENGTH 10
  141. /* driver data */
  142. struct taos_data {
  143. struct input_dev *proximity_input_dev;
  144. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  145. struct input_dev *light_input_dev;
  146. #endif
  147. struct taos_platform_data *pdata;
  148. struct i2c_client *i2c_client;
  149. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  150. struct device *light_dev;
  151. #endif
  152. struct device *proximity_dev;
  153. int irq;
  154. struct work_struct work_light;
  155. struct work_struct work_prox;
  156. struct work_struct work_prox_avg;
  157. struct mutex prox_mutex;
  158. /* TAOS_PRX_DISTANCE_STATUS taos_prox_dist;*/
  159. struct hrtimer timer;
  160. struct hrtimer prox_avg_timer;
  161. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  162. ktime_t light_poll_delay;
  163. #endif
  164. ktime_t prox_polling_time;
  165. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  166. int adc_value_buf[ADC_BUFFER_NUM];
  167. int adc_index_count;
  168. bool adc_buf_initialized;
  169. #endif
  170. u8 power_state;
  171. struct mutex power_lock;
  172. struct wake_lock prx_wake_lock;
  173. struct workqueue_struct *wq;
  174. struct workqueue_struct *wq_avg;
  175. int avg[3];
  176. int prox_avg_enable;
  177. int cleardata;
  178. int irdata;
  179. /* Auto Calibration */
  180. u8 initial_offset;
  181. u8 offset_value;
  182. int cal_result;
  183. int threshold_high;
  184. int threshold_low;
  185. int proximity_value;
  186. bool offset_cal_high;
  187. bool chip_on_success;
  188. bool is_requested;
  189. int err_cnt;
  190. };
  191. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  192. static int lightsensor_get_adcvalue(struct taos_data *taos);
  193. #endif
  194. #ifdef CONFIG_OPTICAL_TAOS_TMD2672X
  195. static int proximity_get_channelvalue(struct taos_data *taos);
  196. #endif
  197. static void set_prox_offset(struct taos_data *taos, u8 offset);
  198. static int proximity_open_offset(struct taos_data *data);
  199. static int proximity_adc_read(struct taos_data *taos);
  200. static int opt_i2c_write(struct taos_data *taos, u8 reg, u8 *val)
  201. {
  202. int ret;
  203. ret = i2c_smbus_write_byte_data(taos->i2c_client,
  204. (CMD_REG | reg), *val);
  205. return ret;
  206. }
  207. static int opt_i2c_read(struct taos_data *taos, u8 reg , u8 *val)
  208. {
  209. int ret;
  210. i2c_smbus_write_byte(taos->i2c_client, (CMD_REG | reg));
  211. ret = i2c_smbus_read_byte(taos->i2c_client);
  212. *val = ret;
  213. return ret;
  214. }
  215. static int opt_i2c_write_command(struct taos_data *taos, u8 val)
  216. {
  217. int ret;
  218. ret = i2c_smbus_write_byte(taos->i2c_client, val);
  219. gprintk("[TAOS Command] val=[0x%x] - ret=[0x%x]\n", val, ret);
  220. return ret;
  221. }
  222. static void taos_thresh_set(struct taos_data *taos)
  223. {
  224. int i = 0;
  225. int ret = 0;
  226. u8 prox_int_thresh[4] = {0, };
  227. /* Setting for proximity interrupt */
  228. if (taos->proximity_value == 1) {
  229. prox_int_thresh[0] = (taos->threshold_low) & 0xFF;
  230. prox_int_thresh[1] = (taos->threshold_low >> 8) & 0xFF;
  231. prox_int_thresh[2] = (0xFFFF) & 0xFF;
  232. prox_int_thresh[3] = (0xFFFF >> 8) & 0xFF;
  233. } else if (taos->proximity_value == 0) {
  234. prox_int_thresh[0] = (0x0000) & 0xFF;
  235. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  236. prox_int_thresh[2] = (taos->threshold_high) & 0xff;
  237. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xff;
  238. }
  239. for (i = 0; i < 4; i++) {
  240. ret = opt_i2c_write(taos,
  241. (CMD_REG|(PRX_MINTHRESHLO + i)),
  242. &prox_int_thresh[i]);
  243. if (ret < 0)
  244. gprintk("opt_i2c_write failed, err = %d\n", ret);
  245. }
  246. }
  247. static int taos_chip_on(struct taos_data *taos)
  248. {
  249. int i = 0;
  250. int ret = 0;
  251. int num_try_init = 0;
  252. int fail_num = 0;
  253. u8 temp_val;
  254. u8 reg_cntrl;
  255. u8 prox_int_thresh[4];
  256. for (num_try_init = 0; num_try_init < 3 ; num_try_init++) {
  257. fail_num = 0;
  258. temp_val = CNTL_REG_CLEAR;
  259. ret = opt_i2c_write(taos, (CMD_REG|CNTRL), &temp_val);
  260. if (ret < 0) {
  261. gprintk("opt_i2c_write to clr ctrl reg failed\n");
  262. fail_num++;
  263. goto err_chipon_i2c_error;
  264. }
  265. #ifdef CONFIG_OPTICAL_TAOS_TMD2672X
  266. temp_val = 0xF6;/* 27ms */
  267. #else
  268. temp_val = taos->pdata->als_time;
  269. #endif
  270. ret = opt_i2c_write(taos, (CMD_REG|ALS_TIME), &temp_val);
  271. if (ret < 0) {
  272. gprintk("opt_i2c_write to als time reg failed\n");
  273. fail_num++;
  274. goto err_chipon_i2c_error;
  275. }
  276. temp_val = 0xff;
  277. ret = opt_i2c_write(taos, (CMD_REG|PRX_TIME), &temp_val);
  278. if (ret < 0) {
  279. gprintk("opt_i2c_write to prx time reg failed\n");
  280. fail_num++;
  281. goto err_chipon_i2c_error;
  282. }
  283. temp_val = 0xff;
  284. ret = opt_i2c_write(taos, (CMD_REG|WAIT_TIME), &temp_val);
  285. if (ret < 0) {
  286. gprintk("opt_i2c_write to wait time reg failed\n");
  287. fail_num++;
  288. goto err_chipon_i2c_error;
  289. }
  290. temp_val = taos->pdata->intr_filter;
  291. ret = opt_i2c_write(taos, (CMD_REG|INTERRUPT), &temp_val);
  292. if (ret < 0) {
  293. gprintk("opt_i2c_write to interrupt reg failed\n");
  294. fail_num++;
  295. goto err_chipon_i2c_error;
  296. }
  297. temp_val = 0x0;
  298. ret = opt_i2c_write(taos, (CMD_REG|PRX_CFG), &temp_val);
  299. if (ret < 0) {
  300. gprintk("opt_i2c_write to prox cfg reg failed\n");
  301. fail_num++;
  302. goto err_chipon_i2c_error;
  303. }
  304. temp_val = taos->pdata->prox_pulsecnt;
  305. ret = opt_i2c_write(taos, (CMD_REG|PRX_COUNT), &temp_val);
  306. if (ret < 0) {
  307. gprintk("opt_i2c_write to prox cnt reg failed\n");
  308. fail_num++;
  309. goto err_chipon_i2c_error;
  310. }
  311. temp_val = taos->pdata->prox_gain;
  312. ret = opt_i2c_write(taos, (CMD_REG|GAIN), &temp_val);
  313. if (ret < 0) {
  314. gprintk("opt_i2c_write to prox gain reg failed\n");
  315. fail_num++;
  316. goto err_chipon_i2c_error;
  317. }
  318. /* Setting for proximity interrupt */
  319. prox_int_thresh[0] = 0;
  320. prox_int_thresh[1] = 0;
  321. prox_int_thresh[2] = (taos->threshold_high) & 0xff;
  322. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xff;
  323. for (i = 0; i < 4; i++) {
  324. ret = opt_i2c_write(taos,
  325. (CMD_REG|(PRX_MINTHRESHLO + i)),
  326. &prox_int_thresh[i]);
  327. if (ret < 0) {
  328. gprintk("opt_i2c_write failed, err = %d\n",
  329. ret);
  330. fail_num++;
  331. goto err_chipon_i2c_error;
  332. }
  333. }
  334. reg_cntrl = CNTL_INTPROXPON_ENBL;
  335. ret = opt_i2c_write(taos, (CMD_REG|CNTRL), &reg_cntrl);
  336. if (ret < 0) {
  337. gprintk("opt_i2c_write to ctrl reg failed\n");
  338. fail_num++;
  339. goto err_chipon_i2c_error;
  340. }
  341. err_chipon_i2c_error:
  342. msleep(20);
  343. if (fail_num == 0)
  344. break;
  345. }
  346. if (ret < 0)
  347. pr_info("[SENSOR] %s: chip_on Failed!\n"\
  348. "fail_num : %d, ret : %d\n", __func__, fail_num, ret);
  349. return ret;
  350. }
  351. static int taos_chip_off(struct taos_data *taos)
  352. {
  353. int ret = 0;
  354. u8 reg_cntrl;
  355. if (taos->chip_on_success) {
  356. reg_cntrl = CNTL_REG_CLEAR;
  357. taos->chip_on_success = false;
  358. ret = opt_i2c_write(taos, (CMD_REG | CNTRL), &reg_cntrl);
  359. if (ret < 0) {
  360. gprintk("opt_i2c_write to ctrl reg failed\n");
  361. return ret;
  362. }
  363. } else {
  364. pr_err("[SENSOR] %s: chip is already turn off!\n", __func__);
  365. }
  366. return ret;
  367. }
  368. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  369. static int taos_get_lux(struct taos_data *taos)
  370. {
  371. int als_gain = 1;
  372. int CPL = 0;
  373. int lux1 = 0, lux2 = 0;
  374. int irdata = 0;
  375. int cleardata = 0;
  376. int calculated_lux = 0;
  377. u8 prox_int_thresh[4];
  378. int i;
  379. int proximity_value = 0;
  380. int coef_atime = taos->pdata->coef_atime;
  381. int ga = taos->pdata->ga;
  382. int coef_a = taos->pdata->coef_a;
  383. int coef_b = taos->pdata->coef_b;
  384. int coef_c = taos->pdata->coef_c;
  385. int coef_d = taos->pdata->coef_d;
  386. bool max_data = false;
  387. if (taos->pdata->max_data)
  388. max_data = taos->pdata->max_data;
  389. cleardata = i2c_smbus_read_word_data(taos->i2c_client,
  390. (CMD_REG | ALS_CHAN0LO));
  391. irdata = i2c_smbus_read_word_data(taos->i2c_client,
  392. (CMD_REG | ALS_CHAN1LO));
  393. if (cleardata < 0 || irdata < 0) {
  394. taos->err_cnt++;
  395. pr_err("[SENSOR] %s: i2c err_cnt:%d. cleardata:%d, irdata:%d\n",
  396. __func__, taos->err_cnt, cleardata, irdata);
  397. return -1;
  398. }
  399. pr_debug("[SENSOR] %s, cleardata = %d, irdata = %d\n",
  400. __func__, cleardata, irdata);
  401. taos->cleardata = cleardata;
  402. taos->irdata = irdata;
  403. /* calculate lux */
  404. CPL = (coef_atime * als_gain * 1000) / ga;
  405. lux1 = (int)((coef_a * cleardata - coef_b * irdata) / CPL);
  406. lux2 = (int)((coef_c * cleardata - coef_d * irdata) / CPL);
  407. if (max_data) { /* Maximum data */
  408. if (lux1 > lux2)
  409. calculated_lux = lux1;
  410. else if (lux2 >= lux1)
  411. calculated_lux = lux2;
  412. } else { /* Minimum data */
  413. if (lux1 < lux2)
  414. calculated_lux = lux1;
  415. else if (lux2 <= lux1)
  416. calculated_lux = lux2;
  417. }
  418. if (calculated_lux < 0)
  419. calculated_lux = 0;
  420. /*
  421. * protection code for abnormal proximity operation
  422. * under the strong sunlight
  423. */
  424. if (cleardata >= 18000 || irdata >= 18000) {
  425. calculated_lux = MAX_LUX;
  426. proximity_value = 0;
  427. prox_int_thresh[0] = (0x0000) & 0xFF;
  428. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  429. prox_int_thresh[2] = (taos->pdata->prox_thresh_hi) & 0xFF;
  430. prox_int_thresh[3] = (taos->pdata->prox_thresh_hi >> 8) & 0xFF;
  431. for (i = 0; i < 4; i++)
  432. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  433. &prox_int_thresh[i]);
  434. input_report_abs(taos->proximity_input_dev,
  435. ABS_DISTANCE, !proximity_value);
  436. input_sync(taos->proximity_input_dev);
  437. }
  438. return calculated_lux;
  439. }
  440. static void taos_light_enable(struct taos_data *taos)
  441. {
  442. taos_dbgmsg("starting poll timer, delay %lldns\n",
  443. ktime_to_ns(taos->light_poll_delay));
  444. taos->err_cnt = 0;
  445. if (taos->chip_on_success)
  446. hrtimer_start(&taos->timer, taos->light_poll_delay,
  447. HRTIMER_MODE_REL);
  448. else
  449. pr_err("[SENSOR] %s : taos chip on failed!\n", __func__);
  450. }
  451. static void taos_light_disable(struct taos_data *taos)
  452. {
  453. taos_dbgmsg("cancelling poll timer\n");
  454. hrtimer_cancel(&taos->timer);
  455. cancel_work_sync(&taos->work_light);
  456. }
  457. static ssize_t poll_delay_show(struct device *dev,
  458. struct device_attribute *attr, char *buf)
  459. {
  460. struct taos_data *taos = dev_get_drvdata(dev);
  461. return sprintf(buf, "%lld\n", ktime_to_ns(taos->light_poll_delay));
  462. }
  463. static ssize_t poll_delay_store(struct device *dev,
  464. struct device_attribute *attr,
  465. const char *buf, size_t size)
  466. {
  467. struct taos_data *taos = dev_get_drvdata(dev);
  468. int64_t new_delay;
  469. int err;
  470. err = kstrtoll(buf, 10, &new_delay);
  471. if (err < 0)
  472. return err;
  473. /* new_delay *= NSEC_PER_MSEC; */
  474. taos_dbgmsg("new delay = %lldns, old delay = %lldns\n",
  475. new_delay, ktime_to_ns(taos->light_poll_delay));
  476. mutex_lock(&taos->power_lock);
  477. if (new_delay != ktime_to_ns(taos->light_poll_delay)) {
  478. taos->light_poll_delay = ns_to_ktime(new_delay);
  479. if (taos->power_state & LIGHT_ENABLED) {
  480. taos_light_disable(taos);
  481. taos_light_enable(taos);
  482. }
  483. }
  484. mutex_unlock(&taos->power_lock);
  485. return size;
  486. }
  487. static ssize_t light_enable_show(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. struct taos_data *taos = dev_get_drvdata(dev);
  491. return sprintf(buf, "%d\n",
  492. (taos->power_state & LIGHT_ENABLED) ? 1 : 0);
  493. }
  494. #endif
  495. static ssize_t proximity_enable_show(struct device *dev,
  496. struct device_attribute *attr, char *buf)
  497. {
  498. struct taos_data *taos = dev_get_drvdata(dev);
  499. return sprintf(buf, "%d\n",
  500. (taos->power_state & PROXIMITY_ENABLED) ? 1 : 0);
  501. }
  502. static void taos_power_enable(int en)
  503. {
  504. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  505. int rc;
  506. static struct regulator *ldo19;
  507. static struct regulator *ldo6;
  508. static struct regulator *lvs1;
  509. printk(KERN_ERR "%s %s\n", __func__, (en) ? "on" : "off");
  510. if (!ldo19) {
  511. ldo19 = regulator_get(NULL, "8226_l19");
  512. rc = regulator_set_voltage(ldo19, 2850000, 2850000);
  513. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  514. if (rc)
  515. printk(KERN_ERR "%s: set_level failed ldo19 (%d)\n",
  516. __func__, rc);
  517. }
  518. if (!ldo6) {
  519. ldo6 = regulator_get(NULL, "8226_l6");
  520. rc = regulator_set_voltage(ldo6, 1800000, 1800000);
  521. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  522. if (rc)
  523. printk(KERN_ERR "%s: set_level failed ldo6 (%d)\n",
  524. __func__, rc);
  525. }
  526. if (!lvs1) {
  527. lvs1 = regulator_get(NULL, "8226_lvs1");
  528. rc = regulator_set_voltage(lvs1, 1800000, 1800000);
  529. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  530. if (rc)
  531. printk(KERN_ERR "%s: set_level failed lvs1 (%d)\n",
  532. __func__, rc);
  533. }
  534. if (en) {
  535. rc = regulator_enable(ldo19);
  536. if (rc)
  537. printk(KERN_ERR "%s: enable failed ldo19 (%d)\n",
  538. __func__, rc);
  539. rc = regulator_enable(ldo6);
  540. if (rc)
  541. printk(KERN_ERR "%s: enable failed ldo6 (%d)\n",
  542. __func__, rc);
  543. rc = regulator_enable(lvs1);
  544. if (rc)
  545. printk(KERN_ERR "%s: enable failed lvs1 (%d)\n",
  546. __func__, rc);
  547. } else {
  548. rc = regulator_disable(ldo19);
  549. if (rc)
  550. printk(KERN_ERR "%s: disable failed ldo19 (%d)\n",
  551. __func__, rc);
  552. }
  553. #elif defined(CONFIG_MACH_FRESCONEOLTE_CTC) ||\
  554. defined(CONFIG_SEC_GNOTE_PROJECT)
  555. int rc;
  556. static struct regulator *ldo15;
  557. static struct regulator *ldo19;
  558. static struct regulator *lvs1;
  559. printk(KERN_ERR "%s %s\n", __func__, (en) ? "on" : "off");
  560. if (!ldo15) {
  561. ldo15 = regulator_get(NULL, "8226_l15");
  562. rc = regulator_set_voltage(ldo15, 2800000, 2800000);
  563. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  564. if (rc)
  565. printk(KERN_ERR "%s: set_level failed ldo15 (%d)\n",
  566. __func__, rc);
  567. }
  568. if (!ldo19) {
  569. ldo19 = regulator_get(NULL, "8226_l19");
  570. rc = regulator_set_voltage(ldo19, 2850000, 2850000);
  571. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  572. if (rc)
  573. printk(KERN_ERR "%s: set_level failed ldo19 (%d)\n",
  574. __func__, rc);
  575. }
  576. if (!lvs1) {
  577. lvs1 = regulator_get(NULL, "8226_lvs1");
  578. rc = regulator_set_voltage(lvs1, 1800000, 1800000);
  579. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  580. if (rc)
  581. printk(KERN_ERR "%s: set_level failed lvs1 (%d)\n",
  582. __func__, rc);
  583. }
  584. if (en) {
  585. rc = regulator_enable(ldo15);
  586. if (rc)
  587. printk(KERN_ERR "%s: enable failed ldo15 (%d)\n",
  588. __func__, rc);
  589. rc = regulator_enable(ldo19);
  590. if (rc)
  591. printk(KERN_ERR "%s: enable failed ldo19 (%d)\n",
  592. __func__, rc);
  593. rc = regulator_enable(lvs1);
  594. if (rc)
  595. printk(KERN_ERR "%s: enable failed lvs1 (%d)\n",
  596. __func__, rc);
  597. } else {
  598. rc = regulator_disable(ldo15);
  599. if (rc)
  600. printk(KERN_ERR "%s: disable failed ldo15 (%d)\n",
  601. __func__, rc);
  602. }
  603. #elif defined(CONFIG_MACH_ATLANTICLTE_ATT) ||\
  604. defined(CONFIG_SEC_HESTIA_PROJECT) ||\
  605. defined(CONFIG_MACH_ATLANTIC3GEUR_OPEN)
  606. int rc;
  607. static struct regulator *ldo19;
  608. static struct regulator *lvs1;
  609. printk(KERN_ERR "%s %s\n", __func__, (en) ? "on" : "off");
  610. if (!ldo19) {
  611. ldo19 = regulator_get(NULL, "8226_l19");
  612. rc = regulator_set_voltage(ldo19, 2850000, 2850000);
  613. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  614. if (rc)
  615. printk(KERN_ERR "%s: set_level failed ldo19 (%d)\n",
  616. __func__, rc);
  617. }
  618. if (!lvs1) {
  619. lvs1 = regulator_get(NULL, "8226_lvs1");
  620. rc = regulator_set_voltage(lvs1, 1800000, 1800000);
  621. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  622. if (rc)
  623. printk(KERN_ERR "%s: set_level failed lvs1 (%d)\n",
  624. __func__, rc);
  625. }
  626. if (en) {
  627. rc = regulator_enable(ldo19);
  628. if (rc)
  629. printk(KERN_ERR "%s: enable failed ldo19 (%d)\n",
  630. __func__, rc);
  631. rc = regulator_enable(lvs1);
  632. if (rc)
  633. printk(KERN_ERR "%s: enable failed lvs1 (%d)\n",
  634. __func__, rc);
  635. } else {
  636. rc = regulator_disable(ldo19);
  637. if (rc)
  638. printk(KERN_ERR "%s: disable failed ldo19 (%d)\n",
  639. __func__, rc);
  640. }
  641. #else
  642. int rc;
  643. static struct regulator *ldo18;
  644. static struct regulator *lvs1;
  645. printk(KERN_ERR "%s %s\n", __func__, (en) ? "on" : "off");
  646. if (!ldo18) {
  647. ldo18 = regulator_get(NULL, "8941_l18");
  648. rc = regulator_set_voltage(ldo18, 2850000, 2850000);
  649. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  650. if (rc)
  651. printk(KERN_ERR "%s: set_level failed ldo18 (%d)\n",
  652. __func__, rc);
  653. }
  654. if (!lvs1) {
  655. lvs1 = regulator_get(NULL, "8941_lvs1");
  656. rc = regulator_set_voltage(lvs1, 1800000, 1800000);
  657. pr_info("[TMP] %s, %d\n", __func__, __LINE__);
  658. if (rc)
  659. printk(KERN_ERR "%s: set_level failed lvs1 (%d)\n",
  660. __func__, rc);
  661. }
  662. if (en) {
  663. rc = regulator_enable(ldo18);
  664. if (rc)
  665. printk(KERN_ERR "%s: enable failed ldo18 (%d)\n",
  666. __func__, rc);
  667. rc = regulator_enable(lvs1);
  668. if (rc)
  669. printk(KERN_ERR "%s: enable failed lvs1 (%d)\n",
  670. __func__, rc);
  671. } else {
  672. rc = regulator_disable(ldo18);
  673. if (rc)
  674. printk(KERN_ERR "%s: disable failed ldo18 (%d)\n",
  675. __func__, rc);
  676. }
  677. #endif
  678. }
  679. static void taos_request_gpio(struct taos_data *taos)
  680. {
  681. int ret = 0;
  682. ret = gpio_request(taos->pdata->en, "prox_en");
  683. if (ret) {
  684. taos->is_requested = false;
  685. pr_err("[taos]%s: unable to request prox_en [%d]\n",
  686. __func__, taos->pdata->en);
  687. return;
  688. } else {
  689. taos->is_requested = true;
  690. }
  691. ret = gpio_direction_output(taos->pdata->en, 0);
  692. if (ret)
  693. pr_err("[taos]%s: unable to set_direction for prox_en [%d]\n",
  694. __func__, taos->pdata->en);
  695. pr_info("%s: en: %u\n", __func__, taos->pdata->en);
  696. }
  697. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  698. static ssize_t light_enable_store(struct device *dev,
  699. struct device_attribute *attr,
  700. const char *buf, size_t size)
  701. {
  702. struct taos_data *taos = dev_get_drvdata(dev);
  703. bool new_value;
  704. if (sysfs_streq(buf, "1")) {
  705. new_value = true;
  706. } else if (sysfs_streq(buf, "0")) {
  707. new_value = false;
  708. } else {
  709. pr_err("%s: invalid value %d\n", __func__, *buf);
  710. return -EINVAL;
  711. }
  712. mutex_lock(&taos->power_lock);
  713. taos_dbgmsg("new_value = %d, old state = %d\n",
  714. new_value, (taos->power_state & LIGHT_ENABLED) ? 1 : 0);
  715. if (new_value && !(taos->power_state & LIGHT_ENABLED)) {
  716. if (!taos->power_state) {
  717. taos_power_enable(1);
  718. msleep(20);
  719. taos->chip_on_success = (taos_chip_on(taos) >= 0) ?
  720. true : false;
  721. }
  722. if (taos->chip_on_success) {
  723. taos->power_state |= LIGHT_ENABLED;
  724. taos_light_enable(taos);
  725. }
  726. } else if (!new_value && (taos->power_state & LIGHT_ENABLED)) {
  727. taos_light_disable(taos);
  728. taos->power_state &= ~LIGHT_ENABLED;
  729. if (!taos->power_state) {
  730. taos_chip_off(taos);
  731. taos_power_enable(0);
  732. }
  733. }
  734. mutex_unlock(&taos->power_lock);
  735. return size;
  736. }
  737. #endif
  738. static ssize_t proximity_enable_store(struct device *dev,
  739. struct device_attribute *attr,
  740. const char *buf, size_t size)
  741. {
  742. struct taos_data *taos = dev_get_drvdata(dev);
  743. bool new_value;
  744. int temp = 0, ret = 0;
  745. if (sysfs_streq(buf, "1")) {
  746. new_value = true;
  747. } else if (sysfs_streq(buf, "0")) {
  748. new_value = false;
  749. } else {
  750. pr_err("%s: invalid value %d\n", __func__, *buf);
  751. return -EINVAL;
  752. }
  753. mutex_lock(&taos->power_lock);
  754. taos_dbgmsg("new_value = %d, old state = %d\n",
  755. new_value, (taos->power_state & PROXIMITY_ENABLED) ? 1 : 0);
  756. if (new_value && !(taos->power_state & PROXIMITY_ENABLED)) {
  757. if (!taos->power_state)
  758. taos_power_enable(1);
  759. if (taos->is_requested)
  760. gpio_set_value(taos->pdata->en, 1);
  761. usleep_range(5000, 6000);
  762. ret = proximity_open_offset(taos);
  763. if (ret < 0 && ret != -ENOENT)
  764. pr_err("%s: proximity_open_offset() failed\n",
  765. __func__);
  766. /* set prox_threshold from board file */
  767. if (taos->offset_value != taos->initial_offset) {
  768. if (taos->pdata->prox_th_hi_cal &&
  769. taos->pdata->prox_th_low_cal) {
  770. taos->threshold_high =
  771. taos->pdata->prox_th_hi_cal;
  772. taos->threshold_low =
  773. taos->pdata->prox_th_low_cal;
  774. }
  775. }
  776. pr_err("%s: th_hi = %d, th_low = %d\n", __func__,
  777. taos->threshold_high, taos->threshold_low);
  778. taos->power_state |= PROXIMITY_ENABLED;
  779. /* interrupt clearing */
  780. temp = (CMD_REG|CMD_SPL_FN|CMD_PROXALS_INTCLR);
  781. ret = opt_i2c_write_command(taos, temp);
  782. if (ret < 0)
  783. gprintk("opt_i2c_write failed, err = %d\n", ret);
  784. taos->chip_on_success = (taos_chip_on(taos) >= 0) ?
  785. true : false;
  786. input_report_abs(taos->proximity_input_dev, ABS_DISTANCE, 1);
  787. input_sync(taos->proximity_input_dev);
  788. enable_irq(taos->irq);
  789. enable_irq_wake(taos->irq);
  790. } else if (!new_value && (taos->power_state & PROXIMITY_ENABLED)) {
  791. disable_irq_wake(taos->irq);
  792. disable_irq(taos->irq);
  793. taos->power_state &= ~PROXIMITY_ENABLED;
  794. if (!taos->power_state) {
  795. taos_chip_off(taos);
  796. taos_power_enable(0);
  797. }
  798. if (taos->is_requested)
  799. gpio_set_value(taos->pdata->en, 0);
  800. }
  801. mutex_unlock(&taos->power_lock);
  802. return size;
  803. }
  804. static ssize_t proximity_state_show(struct device *dev,
  805. struct device_attribute *attr,
  806. char *buf)
  807. {
  808. struct taos_data *taos = dev_get_drvdata(dev);
  809. int adc = 0;
  810. adc = i2c_smbus_read_word_data(taos->i2c_client,
  811. CMD_REG | PRX_LO);
  812. if (adc > TAOS_PROX_MAX)
  813. adc = TAOS_PROX_MAX;
  814. return sprintf(buf, "%d\n", adc);
  815. }
  816. static void set_prox_offset(struct taos_data *taos, u8 offset)
  817. {
  818. int ret = 0;
  819. ret = opt_i2c_write(taos, (CMD_REG|PRX_OFFSET), &offset);
  820. if (ret < 0)
  821. gprintk("opt_i2c_write to prx offset reg failed\n");
  822. }
  823. static int proximity_open_offset(struct taos_data *data)
  824. {
  825. struct file *offset_filp = NULL;
  826. int err = 0;
  827. mm_segment_t old_fs;
  828. old_fs = get_fs();
  829. set_fs(KERNEL_DS);
  830. offset_filp = filp_open(OFFSET_FILE_PATH, O_RDONLY, 0666);
  831. if (IS_ERR(offset_filp)) {
  832. pr_err("%s: no offset file\n", __func__);
  833. err = PTR_ERR(offset_filp);
  834. if (err != -ENOENT)
  835. pr_err("%s: Can't open cancelation file\n", __func__);
  836. set_fs(old_fs);
  837. return err;
  838. }
  839. err = offset_filp->f_op->read(offset_filp,
  840. (char *)&data->offset_value, sizeof(u8), &offset_filp->f_pos);
  841. if (err != sizeof(u8)) {
  842. pr_err("%s: Can't read the cancel data from file\n", __func__);
  843. err = -EIO;
  844. }
  845. pr_info("%s: data->offset_value = %d\n",
  846. __func__, data->offset_value);
  847. set_prox_offset(data, data->offset_value);
  848. filp_close(offset_filp, current->files);
  849. set_fs(old_fs);
  850. return err;
  851. }
  852. static int proximity_adc_read(struct taos_data *taos)
  853. {
  854. int sum[OFFSET_ARRAY_LENGTH];
  855. int i = 0;
  856. int avg = 0;
  857. int min = 0;
  858. int max = 0;
  859. int total = 0;
  860. mutex_lock(&taos->prox_mutex);
  861. for (i = 0; i < OFFSET_ARRAY_LENGTH; i++) {
  862. usleep_range(11000, 11000);
  863. sum[i] = i2c_smbus_read_word_data(taos->i2c_client,
  864. CMD_REG | PRX_LO);
  865. if (i == 0) {
  866. min = sum[i];
  867. max = sum[i];
  868. } else {
  869. if (sum[i] < min)
  870. min = sum[i];
  871. else if (sum[i] > max)
  872. max = sum[i];
  873. }
  874. total += sum[i];
  875. }
  876. mutex_unlock(&taos->prox_mutex);
  877. total -= (min + max);
  878. avg = (int)(total / (OFFSET_ARRAY_LENGTH - 2));
  879. return avg;
  880. }
  881. static int proximity_store_offset(struct device *dev, bool do_calib)
  882. {
  883. struct taos_data *taos = dev_get_drvdata(dev);
  884. struct file *offset_filp = NULL;
  885. mm_segment_t old_fs;
  886. int err = 0;
  887. int adc = 0;
  888. u8 reg_cntrl = 0x25;
  889. int target_xtalk = 150;
  890. int offset_change = 0x20;
  891. bool offset_cal_baseline = true;
  892. if (do_calib) {
  893. /* tap offset button */
  894. pr_info("%s: offset\n", __func__);
  895. taos->offset_value = 0x3F;
  896. err = opt_i2c_write(taos, (CMD_REG|CNTRL), &reg_cntrl);
  897. if (err < 0)
  898. gprintk("opt_i2c_write to ctrl reg failed\n");
  899. usleep_range(12000, 15000);
  900. while (1) {
  901. adc = proximity_adc_read(taos);
  902. pr_info("%s: crosstalk = %d\n", __func__, adc);
  903. if (offset_cal_baseline) {
  904. if (adc >= 250) {
  905. taos->offset_cal_high = true;
  906. } else {
  907. taos->offset_cal_high = false;
  908. taos->offset_value =
  909. taos->initial_offset;
  910. break;
  911. }
  912. offset_cal_baseline = false;
  913. } else {
  914. if (taos->offset_cal_high) {
  915. if (adc > target_xtalk) {
  916. taos->offset_value +=
  917. offset_change;
  918. } else {
  919. taos->offset_value -=
  920. offset_change;
  921. }
  922. } else {
  923. if (adc > target_xtalk) {
  924. taos->offset_value -=
  925. offset_change;
  926. } else {
  927. taos->offset_value +=
  928. offset_change;
  929. }
  930. }
  931. offset_change = (int)(offset_change / 2);
  932. if (offset_change == 0)
  933. break;
  934. }
  935. set_prox_offset(taos, taos->offset_value);
  936. pr_info("%s: P_OFFSET = %d, change = %d\n", __func__,
  937. taos->offset_value, offset_change);
  938. }
  939. adc = proximity_adc_read(taos);
  940. if (taos->offset_value >= 121 &&
  941. taos->offset_value < 128) {
  942. taos->cal_result = 0;
  943. pr_err("%s: cal fail return -1, adc = %d\n",
  944. __func__, adc);
  945. } else {
  946. if (taos->offset_value != taos->initial_offset &&
  947. taos->offset_cal_high == true) {
  948. if (taos->pdata->prox_th_hi_cal) {
  949. taos->threshold_high =
  950. taos->pdata->prox_th_hi_cal;
  951. }
  952. if (taos->pdata->prox_th_low_cal) {
  953. taos->threshold_low =
  954. taos->pdata->prox_th_low_cal;
  955. }
  956. taos_thresh_set(taos);
  957. taos->cal_result = 1;
  958. } else
  959. taos->cal_result = 2;
  960. }
  961. if (taos->offset_cal_high != false)
  962. set_prox_offset(taos, taos->offset_value);
  963. } else {
  964. /* tap reset button */
  965. pr_info("%s: reset\n", __func__);
  966. taos->threshold_high = taos->pdata->prox_thresh_hi;
  967. taos->threshold_low = taos->pdata->prox_thresh_low;
  968. taos_thresh_set(taos);
  969. taos->offset_value = taos->initial_offset;
  970. set_prox_offset(taos, taos->offset_value);
  971. taos->cal_result = 2;
  972. }
  973. old_fs = get_fs();
  974. set_fs(KERNEL_DS);
  975. offset_filp = filp_open(OFFSET_FILE_PATH,
  976. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
  977. if (IS_ERR(offset_filp)) {
  978. pr_err("%s: Can't open prox_offset file\n", __func__);
  979. set_fs(old_fs);
  980. err = PTR_ERR(offset_filp);
  981. return err;
  982. }
  983. err = offset_filp->f_op->write(offset_filp,
  984. (char *)&taos->offset_value, sizeof(u8), &offset_filp->f_pos);
  985. if (err != sizeof(u8)) {
  986. pr_err("%s: Can't write the offset data to file\n", __func__);
  987. err = -EIO;
  988. }
  989. filp_close(offset_filp, current->files);
  990. set_fs(old_fs);
  991. return err;
  992. }
  993. static ssize_t proximity_cal_store(struct device *dev,
  994. struct device_attribute *attr,
  995. const char *buf, size_t size)
  996. {
  997. bool do_calib;
  998. int err;
  999. if (sysfs_streq(buf, "1")) { /* calibrate cancelation value */
  1000. do_calib = true;
  1001. } else if (sysfs_streq(buf, "0")) { /* reset cancelation value */
  1002. do_calib = false;
  1003. } else {
  1004. pr_err("%s: invalid value %d\n", __func__, *buf);
  1005. return -EINVAL;
  1006. }
  1007. err = proximity_store_offset(dev, do_calib);
  1008. if (err < 0) {
  1009. pr_err("%s: proximity_store_offset() failed\n", __func__);
  1010. return err;
  1011. }
  1012. return size;
  1013. }
  1014. static ssize_t proximity_cal_show(struct device *dev,
  1015. struct device_attribute *attr, char *buf)
  1016. {
  1017. struct taos_data *taos = dev_get_drvdata(dev);
  1018. u8 p_offset = 0;
  1019. int ret = 0;
  1020. msleep(20);
  1021. ret = opt_i2c_read(taos, PRX_OFFSET, &p_offset);
  1022. if (ret < 0)
  1023. pr_err("%s: opt_i2c_read() failed\n", __func__);
  1024. return sprintf(buf, "%d,%d,%d\n",
  1025. p_offset, taos->threshold_high, taos->threshold_low);
  1026. }
  1027. static ssize_t prox_offset_pass_show(struct device *dev,
  1028. struct device_attribute *attr, char *buf)
  1029. {
  1030. struct taos_data *taos = dev_get_drvdata(dev);
  1031. return sprintf(buf, "%d\n", taos->cal_result);
  1032. }
  1033. static ssize_t proximity_avg_show(struct device *dev,
  1034. struct device_attribute *attr, char *buf)
  1035. {
  1036. struct taos_data *taos = dev_get_drvdata(dev);
  1037. return sprintf(buf, "%d,%d,%d\n",
  1038. taos->avg[0], taos->avg[1], taos->avg[2]);
  1039. }
  1040. static ssize_t proximity_avg_store(struct device *dev,
  1041. struct device_attribute *attr,
  1042. const char *buf, size_t size)
  1043. {
  1044. struct taos_data *taos = dev_get_drvdata(dev);
  1045. int new_value = 0;
  1046. if (sysfs_streq(buf, "1")) {
  1047. new_value = true;
  1048. } else if (sysfs_streq(buf, "0")) {
  1049. new_value = false;
  1050. } else {
  1051. pr_err("%s: invalid value %d\n", __func__, *buf);
  1052. return -EINVAL;
  1053. }
  1054. if (taos->prox_avg_enable == new_value)
  1055. taos_dbgmsg("same status\n");
  1056. else if (new_value == 1) {
  1057. taos_dbgmsg("starting poll timer, delay %lldns\n",
  1058. ktime_to_ns(taos->prox_polling_time));
  1059. hrtimer_start(&taos->prox_avg_timer,
  1060. taos->prox_polling_time, HRTIMER_MODE_REL);
  1061. taos->prox_avg_enable = 1;
  1062. } else {
  1063. taos_dbgmsg("cancelling prox avg poll timer\n");
  1064. hrtimer_cancel(&taos->prox_avg_timer);
  1065. cancel_work_sync(&taos->work_prox_avg);
  1066. taos->prox_avg_enable = 0;
  1067. }
  1068. return 1;
  1069. }
  1070. static ssize_t proximity_thresh_show(struct device *dev,
  1071. struct device_attribute *attr, char *buf)
  1072. {
  1073. struct taos_data *taos = dev_get_drvdata(dev);
  1074. int thresh_hi = 0;
  1075. msleep(20);
  1076. thresh_hi = i2c_smbus_read_word_data(taos->i2c_client,
  1077. (CMD_REG | PRX_MAXTHRESHLO));
  1078. pr_info("%s: THRESHOLD = %d\n", __func__, thresh_hi);
  1079. return sprintf(buf, "prox_threshold = %d\n", thresh_hi);
  1080. }
  1081. static ssize_t proximity_thresh_store(struct device *dev,
  1082. struct device_attribute *attr, const char *buf, size_t size)
  1083. {
  1084. struct taos_data *taos = dev_get_drvdata(dev);
  1085. int thresh_value = (u8)(taos->pdata->prox_thresh_hi);
  1086. int err = 0;
  1087. err = kstrtoint(buf, 10, &thresh_value);
  1088. if (err < 0)
  1089. pr_err("%s, kstrtoint failed.", __func__);
  1090. taos->threshold_high = thresh_value;
  1091. taos_thresh_set(taos);
  1092. msleep(20);
  1093. return size;
  1094. }
  1095. static ssize_t get_vendor_name(struct device *dev,
  1096. struct device_attribute *attr, char *buf)
  1097. {
  1098. return sprintf(buf, "%s\n", VENDOR_NAME);
  1099. }
  1100. static ssize_t get_chip_name(struct device *dev,
  1101. struct device_attribute *attr, char *buf)
  1102. {
  1103. return sprintf(buf, "%s\n", CHIP_NAME);
  1104. }
  1105. static DEVICE_ATTR(vendor, S_IRUGO, get_vendor_name, NULL);
  1106. static DEVICE_ATTR(name, S_IRUGO, get_chip_name, NULL);
  1107. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1108. static ssize_t lightsensor_file_state_show(struct device *dev,
  1109. struct device_attribute *attr, char *buf)
  1110. {
  1111. struct taos_data *taos = dev_get_drvdata(dev);
  1112. int adc = 0;
  1113. adc = lightsensor_get_adcvalue(taos);
  1114. return sprintf(buf, "%d\n", adc);
  1115. }
  1116. static DEVICE_ATTR(lux, S_IRUGO, lightsensor_file_state_show, NULL);
  1117. static ssize_t lightsensor_raw_data_show(struct device *dev,
  1118. struct device_attribute *attr, char *buf)
  1119. {
  1120. struct taos_data *taos = dev_get_drvdata(dev);
  1121. return sprintf(buf, "%d, %d\n", taos->cleardata, taos->irdata);
  1122. }
  1123. static struct device_attribute dev_attr_light_raw_data =
  1124. __ATTR(raw_data, S_IRUGO, lightsensor_raw_data_show, NULL);
  1125. static struct device_attribute dev_attr_light_enable =
  1126. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  1127. light_enable_show, light_enable_store);
  1128. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  1129. poll_delay_show, poll_delay_store);
  1130. static struct attribute *light_sysfs_attrs[] = {
  1131. &dev_attr_light_enable.attr,
  1132. &dev_attr_poll_delay.attr,
  1133. NULL
  1134. };
  1135. static struct attribute_group light_attribute_group = {
  1136. .attrs = light_sysfs_attrs,
  1137. };
  1138. static struct device_attribute *light_sensor_attrs[] = {
  1139. &dev_attr_lux,
  1140. &dev_attr_vendor,
  1141. &dev_attr_name,
  1142. &dev_attr_light_raw_data,
  1143. NULL
  1144. };
  1145. #endif
  1146. static struct device_attribute dev_attr_proximity_enable =
  1147. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  1148. proximity_enable_show, proximity_enable_store);
  1149. static struct attribute *proximity_sysfs_attrs[] = {
  1150. &dev_attr_proximity_enable.attr,
  1151. NULL
  1152. };
  1153. static struct attribute_group proximity_attribute_group = {
  1154. .attrs = proximity_sysfs_attrs,
  1155. };
  1156. static struct device_attribute dev_attr_proximity_raw_data =
  1157. __ATTR(raw_data, S_IRUGO, proximity_state_show, NULL);
  1158. static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, proximity_state_show, NULL);
  1159. static DEVICE_ATTR(prox_avg, S_IRUGO | S_IWUSR, proximity_avg_show,
  1160. proximity_avg_store);
  1161. static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR, proximity_cal_show,
  1162. proximity_cal_store);
  1163. static DEVICE_ATTR(prox_offset_pass, S_IRUGO | S_IWUSR,
  1164. prox_offset_pass_show, NULL);
  1165. static DEVICE_ATTR(prox_thresh, S_IRUGO | S_IWUSR, proximity_thresh_show,
  1166. proximity_thresh_store);
  1167. static struct device_attribute *prox_sensor_attrs[] = {
  1168. &dev_attr_state,
  1169. &dev_attr_proximity_raw_data,
  1170. &dev_attr_prox_avg,
  1171. &dev_attr_prox_cal,
  1172. &dev_attr_prox_offset_pass,
  1173. &dev_attr_prox_thresh,
  1174. &dev_attr_vendor,
  1175. &dev_attr_name,
  1176. NULL
  1177. };
  1178. #ifdef CONFIG_OPTICAL_TAOS_TMD2672X
  1179. static int proximity_get_channelvalue(struct taos_data *taos)
  1180. {
  1181. int status = 0, psat = 0;
  1182. int irdata = 0;
  1183. int cleardata = 0;
  1184. int ret = 0;
  1185. cleardata = i2c_smbus_read_word_data(taos->i2c_client,
  1186. (CMD_REG | ALS_CHAN0LO));
  1187. irdata = i2c_smbus_read_word_data(taos->i2c_client,
  1188. (CMD_REG | ALS_CHAN1LO));
  1189. taos->cleardata = cleardata;
  1190. taos->irdata = irdata;
  1191. ret = opt_i2c_read(taos, STATUS, &status);
  1192. if (ret < 0) {
  1193. pr_err("%s: opt_i2c_read() to STATUS regs failed\n", __func__);
  1194. return ret;
  1195. }
  1196. psat = (status & 0x40) >> 6;
  1197. return psat;
  1198. }
  1199. #endif
  1200. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1201. static int lightsensor_get_adcvalue(struct taos_data *taos)
  1202. {
  1203. int i = 0;
  1204. int j = 0;
  1205. unsigned int adc_total = 0;
  1206. int adc_avr_value;
  1207. unsigned int adc_index = 0;
  1208. unsigned int adc_max = 0;
  1209. unsigned int adc_min = 0;
  1210. int value = 0;
  1211. /* get ADC */
  1212. value = taos_get_lux(taos);
  1213. adc_index = (taos->adc_index_count++) % ADC_BUFFER_NUM;
  1214. /*ADC buffer initialize (light sensor off ---> light sensor on) */
  1215. if (!taos->adc_buf_initialized) {
  1216. taos->adc_buf_initialized = true;
  1217. for (j = 0; j < ADC_BUFFER_NUM; j++)
  1218. taos->adc_value_buf[j] = value;
  1219. } else
  1220. taos->adc_value_buf[adc_index] = value;
  1221. adc_max = taos->adc_value_buf[0];
  1222. adc_min = taos->adc_value_buf[0];
  1223. for (i = 0; i < ADC_BUFFER_NUM; i++) {
  1224. adc_total += taos->adc_value_buf[i];
  1225. if (adc_max < taos->adc_value_buf[i])
  1226. adc_max = taos->adc_value_buf[i];
  1227. if (adc_min > taos->adc_value_buf[i])
  1228. adc_min = taos->adc_value_buf[i];
  1229. }
  1230. adc_avr_value = (adc_total-(adc_max+adc_min))/(ADC_BUFFER_NUM - 2);
  1231. if (taos->adc_index_count == ADC_BUFFER_NUM-1)
  1232. taos->adc_index_count = 0;
  1233. return adc_avr_value;
  1234. }
  1235. static void taos_work_func_light(struct work_struct *work)
  1236. {
  1237. struct taos_data *taos = container_of(work, struct taos_data,
  1238. work_light);
  1239. int adc = taos_get_lux(taos);
  1240. if (adc < 0 && taos->err_cnt >= 2) {
  1241. taos->power_state &= ~LIGHT_ENABLED;
  1242. if (!taos->power_state) {
  1243. taos_chip_off(taos);
  1244. taos->pdata->power(false);
  1245. }
  1246. hrtimer_cancel(&taos->timer);
  1247. return;
  1248. }
  1249. input_report_rel(taos->light_input_dev, REL_MISC, adc+1);
  1250. input_sync(taos->light_input_dev);
  1251. }
  1252. #endif
  1253. static void taos_work_func_prox(struct work_struct *work)
  1254. {
  1255. struct taos_data *taos =
  1256. container_of(work, struct taos_data, work_prox);
  1257. u16 adc_data = 0;
  1258. u16 threshold_high;
  1259. u16 threshold_low;
  1260. u8 prox_int_thresh[4];
  1261. int i;
  1262. int proximity_value = 0;
  1263. #ifdef CONFIG_OPTICAL_TAOS_TMD2672X
  1264. int psat = 0;
  1265. #endif
  1266. if (!taos->chip_on_success) {
  1267. pr_err("[SENSOR] %s : chip_on failed!\n", __func__);
  1268. return ;
  1269. }
  1270. /* change Threshold */
  1271. #if defined(CONFIG_SEC_LOCALE_KOR_FRESCO)
  1272. for (i = 0; i < 3; i++) {
  1273. mutex_lock(&taos->prox_mutex);
  1274. adc_data = i2c_smbus_read_word_data(taos->i2c_client,
  1275. CMD_REG | PRX_LO);
  1276. mutex_unlock(&taos->prox_mutex);
  1277. if (adc_data < TAOS_PROX_MAX+1)
  1278. break;
  1279. pr_err("%s : adc value read fail %d times!(%d)\n",
  1280. __func__, i+1, adc_data);
  1281. msleep(20);
  1282. }
  1283. threshold_high = i2c_smbus_read_word_data(taos->i2c_client,
  1284. (CMD_REG | PRX_MAXTHRESHLO));
  1285. threshold_low = i2c_smbus_read_word_data(taos->i2c_client,
  1286. (CMD_REG | PRX_MINTHRESHLO));
  1287. pr_info("%s: adc = %d, hi = %d, %d, low = %d, %d\n", __func__, adc_data,
  1288. threshold_high, taos->threshold_high,
  1289. threshold_low, taos->threshold_low);
  1290. #else
  1291. mutex_lock(&taos->prox_mutex);
  1292. adc_data = i2c_smbus_read_word_data(taos->i2c_client, CMD_REG | PRX_LO);
  1293. mutex_unlock(&taos->prox_mutex);
  1294. threshold_high = i2c_smbus_read_word_data(taos->i2c_client,
  1295. (CMD_REG | PRX_MAXTHRESHLO));
  1296. threshold_low = i2c_smbus_read_word_data(taos->i2c_client,
  1297. (CMD_REG | PRX_MINTHRESHLO));
  1298. pr_info("%s: hi = %d, low = %d\n", __func__,
  1299. taos->threshold_high, taos->threshold_low);
  1300. #endif
  1301. /*
  1302. * protection code for abnormal proximity operation
  1303. * under the saturation condition
  1304. */
  1305. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1306. if (taos_get_lux(taos) >= 1500) {
  1307. proximity_value = 0;
  1308. input_report_abs(taos->proximity_input_dev,
  1309. ABS_DISTANCE, !proximity_value);
  1310. input_sync(taos->proximity_input_dev);
  1311. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1312. prox_int_thresh[0] = (0x0000) & 0xFF;
  1313. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  1314. prox_int_thresh[2] = (taos->threshold_high) & 0xFF;
  1315. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xFF;
  1316. for (i = 0; i < 4; i++)
  1317. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1318. &prox_int_thresh[i]);
  1319. } else if ((threshold_high == (taos->threshold_high)) &&
  1320. (adc_data >= (taos->threshold_high))) {
  1321. proximity_value = 1;
  1322. input_report_abs(taos->proximity_input_dev,
  1323. ABS_DISTANCE, !proximity_value);
  1324. input_sync(taos->proximity_input_dev);
  1325. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1326. prox_int_thresh[0] = (taos->threshold_low) & 0xFF;
  1327. prox_int_thresh[1] = (taos->threshold_low >> 8) & 0xFF;
  1328. prox_int_thresh[2] = (0xFFFF) & 0xFF;
  1329. prox_int_thresh[3] = (0xFFFF >> 8) & 0xFF;
  1330. for (i = 0; i < 4; i++)
  1331. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1332. &prox_int_thresh[i]);
  1333. } else if ((threshold_high == (0xFFFF)) &&
  1334. (adc_data <= (taos->threshold_low))) {
  1335. proximity_value = 0;
  1336. input_report_abs(taos->proximity_input_dev,
  1337. ABS_DISTANCE, !proximity_value);
  1338. input_sync(taos->proximity_input_dev);
  1339. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1340. prox_int_thresh[0] = (0x0000) & 0xFF;
  1341. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  1342. prox_int_thresh[2] = (taos->threshold_high) & 0xFF;
  1343. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xFF;
  1344. for (i = 0; i < 4; i++)
  1345. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1346. &prox_int_thresh[i]);
  1347. } else {
  1348. pr_err("%s: Error Case!adc=[%X], th_high=[%d], th_min=[%d]\n",
  1349. __func__, adc_data, threshold_high, threshold_low);
  1350. }
  1351. #else
  1352. psat = proximity_get_channelvalue(taos);
  1353. if (taos->cleardata >= 9000 || taos->irdata >= 9000 || psat) {
  1354. proximity_value = 0;
  1355. input_report_abs(taos->proximity_input_dev,
  1356. ABS_DISTANCE, !proximity_value);
  1357. input_sync(taos->proximity_input_dev);
  1358. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1359. prox_int_thresh[0] = (0x0000) & 0xFF;
  1360. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  1361. prox_int_thresh[2] = (taos->threshold_high) & 0xFF;
  1362. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xFF;
  1363. for (i = 0; i < 4; i++)
  1364. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1365. &prox_int_thresh[i]);
  1366. } else if ((threshold_high == (taos->threshold_high)) &&
  1367. (adc_data >= (taos->threshold_high))) {
  1368. proximity_value = 1;
  1369. input_report_abs(taos->proximity_input_dev,
  1370. ABS_DISTANCE, !proximity_value);
  1371. input_sync(taos->proximity_input_dev);
  1372. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1373. prox_int_thresh[0] = (taos->threshold_low) & 0xFF;
  1374. prox_int_thresh[1] = (taos->threshold_low >> 8) & 0xFF;
  1375. prox_int_thresh[2] = (0xFFFF) & 0xFF;
  1376. prox_int_thresh[3] = (0xFFFF >> 8) & 0xFF;
  1377. for (i = 0; i < 4; i++)
  1378. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1379. &prox_int_thresh[i]);
  1380. } else if ((threshold_high == (0xFFFF)) &&
  1381. (adc_data <= (taos->threshold_low))) {
  1382. proximity_value = 0;
  1383. input_report_abs(taos->proximity_input_dev,
  1384. ABS_DISTANCE, !proximity_value);
  1385. input_sync(taos->proximity_input_dev);
  1386. pr_info("%s: prox value = %d\n", __func__, !proximity_value);
  1387. prox_int_thresh[0] = (0x0000) & 0xFF;
  1388. prox_int_thresh[1] = (0x0000 >> 8) & 0xFF;
  1389. prox_int_thresh[2] = (taos->threshold_high) & 0xFF;
  1390. prox_int_thresh[3] = (taos->threshold_high >> 8) & 0xFF;
  1391. for (i = 0; i < 4; i++)
  1392. opt_i2c_write(taos, (CMD_REG|(PRX_MINTHRESHLO + i)),
  1393. &prox_int_thresh[i]);
  1394. } else {
  1395. pr_err("%s: Error Case!adc=[%X], th_high=[%X], th_min=[%X]\n",
  1396. __func__, adc_data, threshold_high, threshold_low);
  1397. }
  1398. #endif
  1399. taos->proximity_value = proximity_value;
  1400. /* reset Interrupt pin */
  1401. /* to active Interrupt, TMD2771x Interuupt pin shoud be reset. */
  1402. i2c_smbus_write_byte(taos->i2c_client,
  1403. (CMD_REG|CMD_SPL_FN|CMD_PROXALS_INTCLR));
  1404. /* enable INT */
  1405. enable_irq(taos->irq);
  1406. }
  1407. static void taos_work_func_prox_avg(struct work_struct *work)
  1408. {
  1409. struct taos_data *taos = container_of(work, struct taos_data,
  1410. work_prox_avg);
  1411. u16 proximity_value = 0;
  1412. int min = 0, max = 0, avg = 0;
  1413. int i = 0;
  1414. for (i = 0; i < PROX_AVG_COUNT; i++) {
  1415. mutex_lock(&taos->prox_mutex);
  1416. proximity_value = i2c_smbus_read_word_data(taos->i2c_client,
  1417. CMD_REG | PRX_LO);
  1418. mutex_unlock(&taos->prox_mutex);
  1419. if (proximity_value > TAOS_PROX_MIN) {
  1420. if (proximity_value > TAOS_PROX_MAX)
  1421. proximity_value = TAOS_PROX_MAX;
  1422. avg += proximity_value;
  1423. if (!i)
  1424. min = proximity_value;
  1425. if (proximity_value < min)
  1426. min = proximity_value;
  1427. if (proximity_value > max)
  1428. max = proximity_value;
  1429. } else {
  1430. proximity_value = TAOS_PROX_MIN;
  1431. }
  1432. msleep(40);
  1433. }
  1434. avg /= i;
  1435. taos->avg[0] = min;
  1436. taos->avg[1] = avg;
  1437. taos->avg[2] = max;
  1438. }
  1439. /* This function is for light sensor. It operates every a few seconds.
  1440. * It asks for work to be done on a thread because i2c needs a thread
  1441. * context (slow and blocking) and then reschedules the timer to run again.
  1442. */
  1443. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1444. static enum hrtimer_restart taos_timer_func(struct hrtimer *timer)
  1445. {
  1446. struct taos_data *taos = container_of(timer, struct taos_data, timer);
  1447. queue_work(taos->wq, &taos->work_light);
  1448. hrtimer_forward_now(&taos->timer, taos->light_poll_delay);
  1449. return HRTIMER_RESTART;
  1450. }
  1451. #endif
  1452. static enum hrtimer_restart taos_prox_timer_func(struct hrtimer *timer)
  1453. {
  1454. struct taos_data *taos = container_of(timer, struct taos_data,
  1455. prox_avg_timer);
  1456. queue_work(taos->wq_avg, &taos->work_prox_avg);
  1457. hrtimer_forward_now(&taos->prox_avg_timer, taos->prox_polling_time);
  1458. return HRTIMER_RESTART;
  1459. }
  1460. /* interrupt happened due to transition/change of near/far proximity state */
  1461. irqreturn_t taos_irq_handler(int irq, void *data)
  1462. {
  1463. struct taos_data *ip = data;
  1464. pr_info("[SENSOR] taos interrupt handler is called\n");
  1465. wake_lock_timeout(&ip->prx_wake_lock, 3*HZ);
  1466. disable_irq_nosync(ip->irq);
  1467. queue_work(ip->wq, &ip->work_prox);
  1468. return IRQ_HANDLED;
  1469. }
  1470. static int taos_setup_irq(struct taos_data *taos)
  1471. {
  1472. int rc = -EIO;
  1473. struct taos_platform_data *pdata = taos->pdata;
  1474. int irq;
  1475. taos_dbgmsg("start\n");
  1476. rc = gpio_request(pdata->als_int, "gpio_proximity_int");
  1477. if (rc < 0) {
  1478. pr_err("%s: gpio %d request failed (%d)\n",
  1479. __func__, pdata->als_int, rc);
  1480. return rc;
  1481. }
  1482. rc = gpio_direction_input(pdata->als_int);
  1483. if (rc < 0) {
  1484. pr_err("%s: failed to set gpio %d as input (%d)\n",
  1485. __func__, pdata->als_int, rc);
  1486. goto err_gpio_direction_input;
  1487. }
  1488. irq = gpio_to_irq(pdata->als_int);
  1489. rc = request_threaded_irq(irq, NULL,
  1490. taos_irq_handler,
  1491. IRQF_TRIGGER_FALLING,
  1492. "proximity_int",
  1493. taos);
  1494. if (rc < 0) {
  1495. pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
  1496. __func__, irq,
  1497. pdata->als_int, rc);
  1498. goto err_request_irq;
  1499. }
  1500. /* start with interrupts disabled */
  1501. disable_irq(irq);
  1502. taos->irq = irq;
  1503. taos_dbgmsg("success\n");
  1504. goto done;
  1505. err_gpio_direction_input:
  1506. err_request_irq:
  1507. gpio_free(pdata->als_int);
  1508. done:
  1509. return rc;
  1510. }
  1511. static int taos_get_initial_offset(struct taos_data *taos)
  1512. {
  1513. int ret = 0;
  1514. u8 p_offset = 0;
  1515. taos_power_enable(1);
  1516. msleep(20);
  1517. ret = opt_i2c_read(taos, PRX_OFFSET, &p_offset);
  1518. if (ret < 0)
  1519. pr_err("%s: opt_i2c_read() failed\n", __func__);
  1520. else
  1521. pr_info("%s: initial offset = %d\n", __func__, p_offset);
  1522. taos_power_enable(0);
  1523. return p_offset;
  1524. }
  1525. #ifdef CONFIG_OF
  1526. /* device tree parsing function */
  1527. static int taos_parse_dt(struct device *dev,
  1528. struct taos_platform_data *pdata)
  1529. {
  1530. struct device_node *np = dev->of_node;
  1531. pdata->als_int = of_get_named_gpio_flags(np, "taos,irq_gpio",
  1532. 0, &pdata->als_int_flags);
  1533. pdata->en = of_get_named_gpio_flags(np, "taos,en",
  1534. 0, &pdata->ldo_gpio_flags);
  1535. pr_info("%s: en: %u\n", __func__, pdata->en);
  1536. /* Recommended */
  1537. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  1538. of_property_read_u32(np, "taos,prox_thresh_hi",
  1539. &pdata->prox_thresh_hi);
  1540. of_property_read_u32(np, "taos,prox_thresh_low",
  1541. &pdata->prox_thresh_low);
  1542. of_property_read_u32(np, "taos,prox_th_hi_cal",
  1543. &pdata->prox_th_hi_cal);
  1544. of_property_read_u32(np, "taos,prox_th_low_cal",
  1545. &pdata->prox_th_low_cal);
  1546. of_property_read_u32(np, "taos,als_time", &pdata->als_time);
  1547. of_property_read_u32(np, "taos,intr_filter", &pdata->intr_filter);
  1548. of_property_read_u32(np, "taos,prox_pulsecnt", &pdata->prox_pulsecnt);
  1549. of_property_read_u32(np, "taos,prox_gain", &pdata->prox_gain);
  1550. of_property_read_u32(np, "taos,coef_atime", &pdata->coef_atime);
  1551. of_property_read_u32(np, "taos,ga", &pdata->ga);
  1552. of_property_read_u32(np, "taos,coef_a", &pdata->coef_a);
  1553. of_property_read_u32(np, "taos,coef_b", &pdata->coef_b);
  1554. of_property_read_u32(np, "taos,coef_c", &pdata->coef_c);
  1555. of_property_read_u32(np, "taos,coef_d", &pdata->coef_d);
  1556. pr_info("%s: prox_thresh_hi: %u\n", __func__, pdata->prox_thresh_hi);
  1557. pr_info("%s: prox_thresh_low: %u\n", __func__, pdata->prox_thresh_low);
  1558. pr_info("%s: prox_th_hi_cal: %u\n", __func__, pdata->prox_th_hi_cal);
  1559. pr_info("%s: prox_th_low_cal: %u\n", __func__, pdata->prox_th_low_cal);
  1560. pr_info("%s: als_time: %u\n", __func__, pdata->als_time);
  1561. pr_info("%s: intr_filter: %u\n", __func__, pdata->intr_filter);
  1562. pr_info("%s: prox_pulsecnt: %u\n", __func__, pdata->prox_pulsecnt);
  1563. pr_info("%s: prox_gain: %u\n", __func__, pdata->prox_gain);
  1564. pr_info("%s: coef_atime: %u\n", __func__, pdata->coef_atime);
  1565. pr_info("%s: ga: %u\n", __func__, pdata->ga);
  1566. pr_info("%s: coef_a: %u\n", __func__, pdata->coef_a);
  1567. pr_info("%s: coef_b: %u\n", __func__, pdata->coef_b);
  1568. pr_info("%s: coef_c: %u\n", __func__, pdata->coef_c);
  1569. pr_info("%s: coef_d: %u\n", __func__, pdata->coef_d);
  1570. pdata->min_max = MIN;
  1571. pdata->max_data = true;
  1572. /* Deprecated */
  1573. #elif defined(CONFIG_SEC_LOCALE_KOR_FRESCO)
  1574. pdata->prox_thresh_hi = 420;
  1575. pdata->prox_thresh_low = 245;
  1576. pdata->prox_th_hi_cal = 470;
  1577. pdata->prox_th_low_cal = 380;
  1578. pdata->als_time = 0xED;
  1579. pdata->intr_filter = 0x22;
  1580. pdata->prox_pulsecnt = 0x10;
  1581. pdata->prox_gain = 0x28;
  1582. pdata->coef_atime = 50;
  1583. pdata->ga = 97;
  1584. pdata->coef_a = 1000;
  1585. pdata->coef_b = 1850;
  1586. pdata->coef_c = 510;
  1587. pdata->coef_d = 870;
  1588. pdata->min_max = MIN;
  1589. pdata->max_data = true;
  1590. #else
  1591. pdata->prox_thresh_hi = 180;
  1592. pdata->prox_thresh_low = 105;
  1593. pdata->prox_th_hi_cal = 470;
  1594. pdata->prox_th_low_cal = 380;
  1595. pdata->als_time = 0xED;
  1596. pdata->intr_filter = 0x33;
  1597. pdata->prox_pulsecnt = 0x08;
  1598. pdata->prox_gain = 0x28;
  1599. pdata->coef_atime = 50;
  1600. pdata->ga = 112;
  1601. pdata->coef_a = 1000;
  1602. pdata->coef_b = 1880;
  1603. pdata->coef_c = 460;
  1604. pdata->coef_d = 780;
  1605. pdata->min_max = MIN;
  1606. pdata->max_data = true;
  1607. #endif
  1608. return 0;
  1609. }
  1610. #else
  1611. static int taos_parse_dt(struct device *dev,
  1612. struct taos_platform_data)
  1613. {
  1614. return -ENODEV;
  1615. }
  1616. #endif
  1617. static int taos_i2c_probe(struct i2c_client *client,
  1618. const struct i2c_device_id *id)
  1619. {
  1620. int ret = -ENODEV;
  1621. u8 chipid = 0;
  1622. struct input_dev *input_dev;
  1623. struct taos_data *taos;
  1624. struct taos_platform_data *pdata = NULL;
  1625. int err;
  1626. pr_info("%s, is called\n", __func__);
  1627. taos_power_enable(1);
  1628. msleep(100);
  1629. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1630. pr_err("%s: i2c functionality check failed!\n", __func__);
  1631. goto exit;
  1632. }
  1633. taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
  1634. if (!taos) {
  1635. pr_err("%s: failed to alloc memory for module data\n",
  1636. __func__);
  1637. ret = -ENOMEM;
  1638. goto exit;
  1639. }
  1640. if (client->dev.of_node) {
  1641. pdata = devm_kzalloc(&client->dev,
  1642. sizeof(struct taos_platform_data), GFP_KERNEL);
  1643. if (!pdata) {
  1644. dev_err(&client->dev, "Failed to allocate memory\n");
  1645. if (taos)
  1646. kfree(taos);
  1647. return -ENOMEM;
  1648. }
  1649. err = taos_parse_dt(&client->dev, pdata);
  1650. if (err)
  1651. goto err_devicetree;
  1652. } else
  1653. pdata = client->dev.platform_data;
  1654. if (!pdata) {
  1655. pr_err("%s: missing pdata!\n", __func__);
  1656. if (taos)
  1657. kfree(taos);
  1658. return ret;
  1659. }
  1660. taos->offset_cal_high = false;
  1661. taos->pdata = pdata;
  1662. taos->i2c_client = client;
  1663. i2c_set_clientdata(client, taos);
  1664. taos_request_gpio(taos);
  1665. msleep(100);
  1666. chipid = i2c_smbus_read_byte_data(client, CMD_REG | CHIPID);
  1667. if (chipid != CHIP_ID) {
  1668. pr_err("%s: i2c read error [%x]\n", __func__, chipid);
  1669. goto err_chip_id_or_i2c_error;
  1670. }
  1671. taos->threshold_high = taos->pdata->prox_thresh_hi;
  1672. taos->threshold_low = taos->pdata->prox_thresh_low;
  1673. taos->initial_offset = taos_get_initial_offset(taos);
  1674. taos->offset_value = taos->initial_offset;
  1675. mutex_init(&taos->prox_mutex);
  1676. /* wake lock init */
  1677. wake_lock_init(&taos->prx_wake_lock, WAKE_LOCK_SUSPEND,
  1678. "prx_wake_lock");
  1679. mutex_init(&taos->power_lock);
  1680. /* allocate proximity input_device */
  1681. input_dev = input_allocate_device();
  1682. if (!input_dev) {
  1683. pr_err("%s: could not allocate input device\n", __func__);
  1684. goto err_input_device_proximity;
  1685. }
  1686. taos->proximity_input_dev = input_dev;
  1687. input_set_drvdata(input_dev, taos);
  1688. input_dev->name = "proximity_sensor";
  1689. input_set_capability(input_dev, EV_ABS, ABS_DISTANCE);
  1690. input_set_abs_params(input_dev, ABS_DISTANCE, 0, 1, 0, 0);
  1691. input_report_abs(taos->proximity_input_dev, ABS_DISTANCE, 1);
  1692. input_sync(taos->proximity_input_dev);
  1693. taos_dbgmsg("registering proximity input device\n");
  1694. ret = input_register_device(input_dev);
  1695. if (ret < 0) {
  1696. pr_err("%s: could not register proximity input device\n",
  1697. __func__);
  1698. input_free_device(input_dev);
  1699. goto err_input_device_proximity;
  1700. }
  1701. ret = sensors_create_symlink(&taos->proximity_input_dev->dev.kobj,
  1702. input_dev->name);
  1703. if (ret < 0) {
  1704. pr_err("%s: could not create proximity symlink\n", __func__);
  1705. goto err_create_symlink_proximity;
  1706. }
  1707. ret = sysfs_create_group(&input_dev->dev.kobj,
  1708. &proximity_attribute_group);
  1709. if (ret) {
  1710. pr_err("%s: could not create sysfs group\n", __func__);
  1711. goto err_sysfs_create_group_proximity;
  1712. }
  1713. /* allocate lightsensor-level input_device */
  1714. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1715. input_dev = input_allocate_device();
  1716. if (!input_dev) {
  1717. pr_err("%s: could not allocate input device\n", __func__);
  1718. ret = -ENOMEM;
  1719. goto err_input_device_light;
  1720. }
  1721. input_set_drvdata(input_dev, taos);
  1722. input_dev->name = "light_sensor";
  1723. input_set_capability(input_dev, EV_REL, REL_MISC);
  1724. taos_dbgmsg("registering lightsensor-level input device\n");
  1725. ret = input_register_device(input_dev);
  1726. if (ret < 0) {
  1727. pr_err("%s: could not register light input device\n", __func__);
  1728. input_free_device(input_dev);
  1729. goto err_input_device_light;
  1730. }
  1731. taos->light_input_dev = input_dev;
  1732. ret = sensors_create_symlink(&taos->light_input_dev->dev.kobj,
  1733. taos->light_input_dev->name);
  1734. if (ret < 0) {
  1735. pr_err("%s: could not create light symlink\n", __func__);
  1736. goto err_create_symlink_light;
  1737. }
  1738. ret = sysfs_create_group(&input_dev->dev.kobj,
  1739. &light_attribute_group);
  1740. if (ret) {
  1741. pr_err("%s: could not create sysfs group\n", __func__);
  1742. goto err_sysfs_create_group_light;
  1743. }
  1744. #endif
  1745. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1746. /* hrtimer settings. we poll for light values using a timer. */
  1747. hrtimer_init(&taos->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1748. taos->light_poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
  1749. taos->timer.function = taos_timer_func;
  1750. #endif
  1751. hrtimer_init(&taos->prox_avg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1752. taos->prox_polling_time = ns_to_ktime(2000 * NSEC_PER_MSEC);
  1753. taos->prox_avg_timer.function = taos_prox_timer_func;
  1754. /* the timer just fires off a work queue request. we need a thread
  1755. to read the i2c (can be slow and blocking). */
  1756. taos->wq = create_singlethread_workqueue("taos_wq");
  1757. if (!taos->wq) {
  1758. ret = -ENOMEM;
  1759. pr_err("%s: could not create workqueue\n", __func__);
  1760. goto err_create_workqueue;
  1761. }
  1762. taos->wq_avg = create_singlethread_workqueue("taos_wq_avg");
  1763. if (!taos->wq_avg) {
  1764. ret = -ENOMEM;
  1765. pr_err("%s: could not create workqueue\n", __func__);
  1766. goto err_create_avg_workqueue;
  1767. }
  1768. /* this is the thread function we run on the work queue */
  1769. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1770. INIT_WORK(&taos->work_light, taos_work_func_light);
  1771. #endif
  1772. INIT_WORK(&taos->work_prox, taos_work_func_prox);
  1773. INIT_WORK(&taos->work_prox_avg, taos_work_func_prox_avg);
  1774. taos->prox_avg_enable = 0;
  1775. ret = taos_setup_irq(taos);
  1776. if (ret) {
  1777. pr_err("%s: could not setup irq\n", __func__);
  1778. goto err_setup_irq;
  1779. }
  1780. /* set sysfs for proximity sensor and light sensor */
  1781. ret = sensors_register(taos->proximity_dev,
  1782. taos, prox_sensor_attrs, "proximity_sensor");
  1783. if (ret) {
  1784. pr_err("%s: cound not register proximity sensor device(%d).\n",
  1785. __func__, ret);
  1786. goto err_proximity_sensor_register_failed;
  1787. }
  1788. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1789. ret = sensors_register(taos->light_dev,
  1790. taos, light_sensor_attrs, "light_sensor");
  1791. if (ret) {
  1792. pr_err("%s: cound not register light sensor device(%d).\n",
  1793. __func__, ret);
  1794. goto err_light_sensor_register_failed;
  1795. }
  1796. #endif
  1797. pr_info("%s is done.", __func__);
  1798. return ret;
  1799. /* error, unwind it all */
  1800. err_devicetree:
  1801. pr_warn("\n error in device tree");
  1802. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1803. err_light_sensor_register_failed:
  1804. sensors_unregister(taos->proximity_dev, prox_sensor_attrs);
  1805. #endif
  1806. err_proximity_sensor_register_failed:
  1807. free_irq(taos->irq, 0);
  1808. gpio_free(taos->pdata->als_int);
  1809. err_setup_irq:
  1810. destroy_workqueue(taos->wq_avg);
  1811. err_create_avg_workqueue:
  1812. destroy_workqueue(taos->wq);
  1813. err_create_workqueue:
  1814. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1815. sysfs_remove_group(&taos->light_input_dev->dev.kobj,
  1816. &light_attribute_group);
  1817. err_sysfs_create_group_light:
  1818. sensors_remove_symlink(&taos->light_input_dev->dev.kobj,
  1819. taos->light_input_dev->name);
  1820. err_create_symlink_light:
  1821. input_unregister_device(taos->light_input_dev);
  1822. err_input_device_light:
  1823. #endif
  1824. sysfs_remove_group(&taos->proximity_input_dev->dev.kobj,
  1825. &proximity_attribute_group);
  1826. err_sysfs_create_group_proximity:
  1827. sensors_remove_symlink(&taos->proximity_input_dev->dev.kobj,
  1828. taos->proximity_input_dev->name);
  1829. err_create_symlink_proximity:
  1830. input_unregister_device(taos->proximity_input_dev);
  1831. err_input_device_proximity:
  1832. mutex_destroy(&taos->power_lock);
  1833. wake_lock_destroy(&taos->prx_wake_lock);
  1834. mutex_destroy(&taos->prox_mutex);
  1835. err_chip_id_or_i2c_error:
  1836. kfree(taos);
  1837. exit:
  1838. pr_err("%s failed. ret = %d\n", __func__, ret);
  1839. return ret;
  1840. }
  1841. static int taos_suspend(struct device *dev)
  1842. {
  1843. /* We disable power only if proximity is disabled. If proximity
  1844. is enabled, we leave power on because proximity is allowed
  1845. to wake up device. We remove power without changing
  1846. gp2a->power_state because we use that state in resume.
  1847. */
  1848. struct i2c_client *client = to_i2c_client(dev);
  1849. struct taos_data *taos = i2c_get_clientdata(client);
  1850. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1851. if (taos->power_state & LIGHT_ENABLED)
  1852. taos_light_disable(taos);
  1853. #endif
  1854. if (taos->power_state == LIGHT_ENABLED) {
  1855. taos_chip_off(taos);
  1856. taos_power_enable(0);
  1857. }
  1858. if (taos->power_state & PROXIMITY_ENABLED)
  1859. disable_irq(taos->irq);
  1860. return 0;
  1861. }
  1862. static int taos_resume(struct device *dev)
  1863. {
  1864. /* Turn power back on if we were before suspend. */
  1865. struct i2c_client *client = to_i2c_client(dev);
  1866. struct taos_data *taos = i2c_get_clientdata(client);
  1867. if (taos->power_state == LIGHT_ENABLED) {
  1868. taos_power_enable(1);
  1869. msleep(20);
  1870. taos->chip_on_success = (taos_chip_on(taos) >= 0) ?
  1871. true : false;
  1872. }
  1873. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1874. if (taos->power_state & LIGHT_ENABLED)
  1875. taos_light_enable(taos);
  1876. #endif
  1877. if (taos->power_state & PROXIMITY_ENABLED)
  1878. enable_irq(taos->irq);
  1879. return 0;
  1880. }
  1881. static int taos_i2c_remove(struct i2c_client *client)
  1882. {
  1883. struct taos_data *taos = i2c_get_clientdata(client);
  1884. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1885. sysfs_remove_group(&taos->light_input_dev->dev.kobj,
  1886. &light_attribute_group);
  1887. input_unregister_device(taos->light_input_dev);
  1888. #endif
  1889. sysfs_remove_group(&taos->proximity_input_dev->dev.kobj,
  1890. &proximity_attribute_group);
  1891. input_unregister_device(taos->proximity_input_dev);
  1892. free_irq(taos->irq, NULL);
  1893. gpio_free(taos->pdata->als_int);
  1894. if (taos->power_state) {
  1895. taos->power_state = 0;
  1896. #ifndef CONFIG_OPTICAL_TAOS_TMD2672X
  1897. if (taos->power_state & LIGHT_ENABLED)
  1898. taos_light_disable(taos);
  1899. #endif
  1900. taos_power_enable(0);
  1901. }
  1902. destroy_workqueue(taos->wq);
  1903. destroy_workqueue(taos->wq_avg);
  1904. mutex_destroy(&taos->power_lock);
  1905. wake_lock_destroy(&taos->prx_wake_lock);
  1906. mutex_destroy(&taos->prox_mutex);
  1907. kfree(taos);
  1908. return 0;
  1909. }
  1910. static const struct i2c_device_id taos_device_id[] = {
  1911. {"tmd27723", 0},
  1912. {}
  1913. };
  1914. MODULE_DEVICE_TABLE(i2c, taos_device_id);
  1915. static const struct dev_pm_ops taos_pm_ops = {
  1916. .suspend = taos_suspend,
  1917. .resume = taos_resume
  1918. };
  1919. #ifdef CONFIG_OF
  1920. static struct of_device_id tmd27723_match_table[] = {
  1921. { .compatible = "taos,tmd27723",},
  1922. {},
  1923. };
  1924. #else
  1925. #define tm2672_match_table NULL
  1926. #endif
  1927. static struct i2c_driver taos_i2c_driver = {
  1928. .driver = {
  1929. .name = "tmd27723",
  1930. .owner = THIS_MODULE,
  1931. .pm = &taos_pm_ops,
  1932. .of_match_table = tmd27723_match_table,
  1933. },
  1934. .probe = taos_i2c_probe,
  1935. .remove = taos_i2c_remove,
  1936. .id_table = taos_device_id,
  1937. };
  1938. static int __init taos_init(void)
  1939. {
  1940. return i2c_add_driver(&taos_i2c_driver);
  1941. }
  1942. static void __exit taos_exit(void)
  1943. {
  1944. i2c_del_driver(&taos_i2c_driver);
  1945. }
  1946. module_init(taos_init);
  1947. module_exit(taos_exit);
  1948. MODULE_AUTHOR("SAMSUNG");
  1949. MODULE_DESCRIPTION("Optical Sensor driver for taos");
  1950. MODULE_LICENSE("GPL");