gp2ap030.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /*
  2. * Copyright (c) 2012 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/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/i2c.h>
  23. #include <linux/fs.h>
  24. #include <linux/errno.h>
  25. #include <linux/device.h>
  26. #include <linux/delay.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/leds.h>
  30. #include <linux/gpio.h>
  31. #include <mach/hardware.h>
  32. #include <linux/wakelock.h>
  33. #include <linux/input.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/platform_data/gp2ap030.h>
  37. #include <linux/slab.h>
  38. #include <linux/regulator/consumer.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/i2c.h>
  42. #include <linux/fs.h>
  43. #define THR_REG_LSB(data, reg) \
  44. { \
  45. reg = (u8)data & 0xff; \
  46. }
  47. #define THR_REG_MSB(data, reg) \
  48. { \
  49. reg = (u8)data >> 8; \
  50. }
  51. #define CAL_PATH "/efs/prox_cal_data"
  52. #ifdef CONFIG_SENSORS
  53. #include "sensors_core.h"
  54. #define GP2A_VENDOR "SHARP"
  55. #define GP2A_CHIP_ID "GP2AP030"
  56. #define PROX_READ_NUM 10
  57. #define GP2A_PROX_MAX 1023
  58. #define GP2A_PROX_MIN 0
  59. #endif
  60. #define MIN_DELAY 10
  61. #define MAX_DELAY 200
  62. #define SENSOR_ENABLE 1
  63. #define SENSOR_DISABLE 0
  64. #define LUX_MAX_VALUE 65535
  65. #define RAWDATA_THRESHOLD 16000
  66. static bool bShutdown;
  67. struct gp2a_data {
  68. struct i2c_client *client;
  69. struct input_dev *light_input_dev;
  70. struct delayed_work light_work;
  71. struct mutex data_mutex;
  72. struct mutex light_mutex;
  73. u8 light_enabled;
  74. u8 prox_enabled;
  75. u8 proximity_detection;
  76. u8 lightsensor_mode;
  77. int light_delay;
  78. int lux;
  79. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  80. struct input_dev *prox_input_dev;
  81. struct wake_lock prx_wake_lock;
  82. struct work_struct proximity_work;
  83. int irq;
  84. int gpio;
  85. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  86. int vled_gpio;
  87. #endif
  88. int con_gpio;
  89. int offset_value;
  90. int cal_result;
  91. uint16_t threshold_high;
  92. bool offset_cal_high;
  93. bool prox_is_calibrated;
  94. #endif
  95. #ifdef CONFIG_SENSORS
  96. struct device *light_sensor_device;
  97. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  98. struct device *prox_sensor_device;
  99. struct delayed_work prox_avg_work;
  100. int prox_delay;
  101. int avg[3];
  102. #endif
  103. #endif
  104. };
  105. /* initial value for sensor register */
  106. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  107. extern unsigned int system_rev;
  108. #define COL 8
  109. static u8 gp2a_reg[COL][2] = {
  110. /* {Regster, Value} */
  111. {0x01, 0x63}, /*PRST :01(4 cycle at Detection/Non-detection), ALSresolution :16bit, range *128 //0x1F -> 5F by sharp */
  112. /*{0x02, 0x1A}, *//*ALC : 0, INTTYPE : 1, PS mode resolution : 12bit, range*1 */
  113. {0x02, 0x5A}, /*ALC : 0, INTTYPE : 1, PS mode resolution : 12bit, range*1 */
  114. {0x03, 0x3C}, /*LED drive current 110mA, Detection/Non-detection judgment output */
  115. {0x08, 0x08}, /*PS mode LTH(Loff): (??mm) */
  116. {0x09, 0x00}, /*PS mode LTH(Loff) : */
  117. {0x0A, 0x0A}, /*PS mode HTH(Lon) : (??mm) */
  118. {0x0B, 0x00}, /*PS mode HTH(Lon) : */
  119. {0x00, 0xC0} /*alternating mode (PS+ALS), TYPE=1 (0:externel 1:auto calculated mode)*/
  120. };
  121. #endif
  122. static int gp2a_i2c_read(struct gp2a_data *gp2a,
  123. u8 reg, unsigned char *rbuf, int len)
  124. {
  125. int ret = -1;
  126. struct i2c_msg msg[2];
  127. struct i2c_client *client = gp2a->client;
  128. if (unlikely((client == NULL) || (!client->adapter)))
  129. return -ENODEV;
  130. msg[0].addr = client->addr;
  131. msg[0].flags = I2C_M_WR;
  132. msg[0].len = 1;
  133. msg[0].buf = &reg;
  134. msg[1].addr = client->addr;
  135. msg[1].flags = I2c_M_RD;
  136. msg[1].len = len;
  137. msg[1].buf = rbuf;
  138. ret = i2c_transfer(client->adapter, &msg[0], 2);
  139. if (unlikely(ret < 0))
  140. pr_err("i2c transfer error ret=%d\n", ret);
  141. return ret;
  142. }
  143. static int gp2a_i2c_write(struct gp2a_data *gp2a,
  144. u8 reg, u8 *val)
  145. {
  146. int err = 0;
  147. struct i2c_msg msg[1];
  148. unsigned char data[2];
  149. int retry = 2;
  150. struct i2c_client *client = gp2a->client;
  151. if (unlikely((client == NULL) || (!client->adapter)))
  152. return -ENODEV;
  153. do {
  154. data[0] = reg;
  155. data[1] = *val;
  156. msg->addr = client->addr;
  157. msg->flags = I2C_M_WR;
  158. msg->len = 2;
  159. msg->buf = data;
  160. err = i2c_transfer(client->adapter, msg, 1);
  161. if (err >= 0)
  162. return 0;
  163. } while (--retry > 0);
  164. pr_err(" i2c transfer error(%d)\n", err);
  165. return err;
  166. }
  167. static int gp2a_regulator_onoff(struct device *dev, bool onoff)
  168. {
  169. struct regulator *gp2a_vcc, *gp2a_lvs1;
  170. char *gp2a_vcc_reg, *gp2a_lvs1_reg;
  171. #if defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  172. struct regulator *gp2a_2p8;
  173. char *gp2a_2p8_reg;
  174. #endif
  175. #if !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  176. gp2a_vcc_reg = "gp2a030a-vcc";
  177. gp2a_lvs1_reg = "gp2a030a-lvs1";
  178. #else
  179. gp2a_vcc_reg = "8226_l19";
  180. gp2a_lvs1_reg = "8226_lvs1";
  181. gp2a_2p8_reg = "8226_l15";
  182. #endif
  183. gp2a_vcc = devm_regulator_get(dev, gp2a_vcc_reg);
  184. if (IS_ERR(gp2a_vcc)) {
  185. pr_err("%s: cannot get gp2a_vcc\n", __func__);
  186. return -ENOMEM;
  187. }
  188. gp2a_lvs1 = devm_regulator_get(dev, gp2a_lvs1_reg);
  189. if (IS_ERR(gp2a_lvs1)) {
  190. pr_err("%s: cannot get gp2a_vcc\n", __func__);
  191. devm_regulator_put(gp2a_vcc);
  192. return -ENOMEM;
  193. }
  194. #if defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  195. gp2a_2p8 = devm_regulator_get(dev, gp2a_2p8_reg);
  196. if (IS_ERR(gp2a_2p8)) {
  197. pr_err("%s: cannot get gp2a_2p8\n", __func__);
  198. devm_regulator_put(gp2a_lvs1);
  199. devm_regulator_put(gp2a_vcc);
  200. return -ENOMEM;
  201. }
  202. #endif
  203. if (onoff) {
  204. regulator_enable(gp2a_vcc);
  205. msleep(5);
  206. regulator_enable(gp2a_lvs1);
  207. #if defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  208. msleep(5);
  209. regulator_enable(gp2a_2p8);
  210. #endif
  211. } else {
  212. #if defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  213. regulator_disable(gp2a_2p8);
  214. msleep(5);
  215. #endif
  216. regulator_disable(gp2a_lvs1);
  217. msleep(5);
  218. regulator_disable(gp2a_vcc);
  219. }
  220. devm_regulator_put(gp2a_vcc);
  221. devm_regulator_put(gp2a_lvs1);
  222. #if defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  223. devm_regulator_put(gp2a_2p8);
  224. #endif
  225. msleep(10);
  226. return 0;
  227. }
  228. static int lightsensor_onoff(u8 onoff, struct gp2a_data *data)
  229. {
  230. u8 value;
  231. pr_info("%s : light_sensor onoff = %d\n", __func__, onoff);
  232. if (onoff) {
  233. /*in calling, must turn on proximity sensor */
  234. if (data->prox_enabled == 0) {
  235. value = 0x01;
  236. gp2a_i2c_write(data, COMMAND4, &value);
  237. value = 0x63;
  238. gp2a_i2c_write(data, COMMAND2, &value);
  239. /*OP3 : 1(operating mode)
  240. OP2 : 1(coutinuous operating mode)
  241. OP1 : 01(ALS mode) TYPE=0(auto)*/
  242. value = 0xD0;
  243. gp2a_i2c_write(data, COMMAND1, &value);
  244. /* other setting have defualt value. */
  245. }
  246. msleep(3);
  247. } else {
  248. /*in calling, must turn on proximity sensor */
  249. if (data->prox_enabled == 0) {
  250. value = 0x00; /*shutdown mode */
  251. gp2a_i2c_write(data, COMMAND1, &value);
  252. }
  253. }
  254. return 0;
  255. }
  256. int gp2a_get_lux(struct gp2a_data *data)
  257. {
  258. unsigned char get_data[4] = { 0, };
  259. int d0_raw_data;
  260. int d1_raw_data;
  261. int d0_data;
  262. int d1_data;
  263. int lx = 0;
  264. u8 value;
  265. int light_alpha = 0;
  266. int light_beta = 0;
  267. static int lx_prev;
  268. int ret ;
  269. mutex_lock(&data->data_mutex);
  270. ret = gp2a_i2c_read(data, DATA0_LSB, get_data, sizeof(get_data));
  271. mutex_unlock(&data->data_mutex);
  272. if (ret < 0)
  273. return lx_prev;
  274. d0_raw_data = (get_data[1] << 8) | get_data[0]; /* clear */
  275. d1_raw_data = (get_data[3] << 8) | get_data[2]; /* IR */
  276. if (100 * d1_raw_data <= 55 * d0_raw_data) {
  277. light_alpha = 746;
  278. light_beta = 0;
  279. } else if (100 * d1_raw_data <= 75 * d0_raw_data) {
  280. light_alpha = 2535;
  281. light_beta = 3252;
  282. } else if (100 * d1_raw_data <= 91 * d0_raw_data) {
  283. if (d0_raw_data < 240) {
  284. light_alpha = 274;
  285. light_beta = 298;
  286. } else if (d0_raw_data < 400) {
  287. light_alpha = 531;
  288. light_beta = 577;
  289. } else if (d0_raw_data < 1600) {
  290. light_alpha = 295;
  291. light_beta = 320;
  292. } else if (d0_raw_data < 2800) {
  293. light_alpha = 338;
  294. light_beta = 368;
  295. } else {
  296. light_alpha = 516;
  297. light_beta = 562;
  298. }
  299. } else {
  300. light_alpha = 0;
  301. light_beta = 0;
  302. }
  303. if (data->lightsensor_mode) { /* HIGH_MODE */
  304. d0_data = d0_raw_data * 16;
  305. d1_data = d1_raw_data * 16;
  306. } else { /* LOW_MODE */
  307. d0_data = d0_raw_data;
  308. d1_data = d1_raw_data;
  309. }
  310. if (d0_data < 2) {
  311. lx = 0;
  312. } else if (data->lightsensor_mode == 0
  313. && (d0_raw_data >= RAWDATA_THRESHOLD || d1_raw_data >= RAWDATA_THRESHOLD)) {
  314. lx = LUX_MAX_VALUE;
  315. } else if (100 * d1_data > 95 * d0_data) {
  316. if ((d0_raw_data >= RAWDATA_THRESHOLD) || (d1_raw_data >= RAWDATA_THRESHOLD))
  317. lx_prev = LUX_MAX_VALUE;
  318. lx = lx_prev;
  319. return lx;
  320. } else {
  321. lx = (int)((light_alpha * d0_data)
  322. - (light_beta * d1_data)) * 33 / 10000;
  323. }
  324. if (lx >= LUX_MAX_VALUE)
  325. lx = LUX_MAX_VALUE;
  326. lx_prev = lx;
  327. if (data->lightsensor_mode) { /* HIGH MODE */
  328. if (d0_raw_data < 1000) {
  329. pr_info("%s: change to LOW_MODE detection=%d\n",
  330. __func__, data->proximity_detection);
  331. data->lightsensor_mode = 0; /* change to LOW MODE */
  332. value = 0x0C;
  333. gp2a_i2c_write(data, COMMAND1, &value);
  334. if (data->proximity_detection)
  335. value = 0x23;
  336. else
  337. value = 0x63;
  338. gp2a_i2c_write(data, COMMAND2, &value);
  339. if (data->prox_enabled)
  340. value = 0xCC;
  341. else
  342. value = 0xDC;
  343. gp2a_i2c_write(data, COMMAND1, &value);
  344. }
  345. } else { /* LOW MODE */
  346. if (d0_raw_data > RAWDATA_THRESHOLD || d1_raw_data > RAWDATA_THRESHOLD) {
  347. pr_info("%s: change to HIGH_MODE detection=%d\n",
  348. __func__, data->proximity_detection);
  349. /* change to HIGH MODE */
  350. data->lightsensor_mode = 1;
  351. value = 0x0C;
  352. gp2a_i2c_write(data, COMMAND1, &value);
  353. if (data->proximity_detection)
  354. value = 0x27;
  355. else
  356. value = 0x67;
  357. gp2a_i2c_write(data, COMMAND2, &value);
  358. if (data->prox_enabled)
  359. value = 0xCC;
  360. else
  361. value = 0xDC;
  362. gp2a_i2c_write(data, COMMAND1, &value);
  363. }
  364. }
  365. return lx;
  366. }
  367. static void gp2a_work_func_light(struct work_struct *work)
  368. {
  369. struct gp2a_data *data = container_of((struct delayed_work *)work,
  370. struct gp2a_data, light_work);
  371. static int count;
  372. data->lux = gp2a_get_lux(data);
  373. /* detecting 0 after 3.6sec, set the register again. */
  374. if (data->lux == 0) {
  375. count++;
  376. if (count * data->light_delay > 3600) {
  377. lightsensor_onoff(1, data);
  378. count = 0;
  379. pr_info("%s: register reset\n", __func__);
  380. }
  381. } else
  382. count = 0;
  383. input_report_rel(data->light_input_dev, REL_MISC, data->lux + 1);
  384. input_sync(data->light_input_dev);
  385. if (data->light_enabled)
  386. schedule_delayed_work(&data->light_work,
  387. msecs_to_jiffies(data->light_delay));
  388. }
  389. static ssize_t
  390. gp2a_light_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
  391. {
  392. struct gp2a_data *data = dev_get_drvdata(dev);
  393. return snprintf(buf, PAGE_SIZE, "%d\n", data->light_delay);
  394. }
  395. static ssize_t
  396. gp2a_light_delay_store(struct device *dev, struct device_attribute *attr,
  397. const char *buf, size_t count)
  398. {
  399. struct gp2a_data *data = dev_get_drvdata(dev);
  400. int delay;
  401. int err = 0;
  402. if (bShutdown == true){
  403. pr_err("%s already shutdown.", __func__);
  404. goto done;
  405. }
  406. err = kstrtoint(buf, 10, &delay);
  407. if (err) {
  408. pr_err("%s kstrtoint failed.", __func__);
  409. goto done;
  410. }
  411. if (delay < 0)
  412. goto done;
  413. delay = delay / 1000000; /* ns to msec */
  414. if (delay > MAX_DELAY)
  415. delay = MAX_DELAY;
  416. if (delay < MIN_DELAY)
  417. delay = MIN_DELAY;
  418. pr_info("%s new_delay = %d, old_delay = %d",
  419. __func__, delay, data->light_delay);
  420. data->light_delay = delay;
  421. mutex_lock(&data->light_mutex);
  422. if (data->light_enabled) {
  423. cancel_delayed_work_sync(&data->light_work);
  424. schedule_delayed_work(&data->light_work,
  425. msecs_to_jiffies(delay));
  426. }
  427. mutex_unlock(&data->light_mutex);
  428. done:
  429. return count;
  430. }
  431. static ssize_t
  432. gp2a_light_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  433. {
  434. struct gp2a_data *data = dev_get_drvdata(dev);
  435. return snprintf(buf, PAGE_SIZE, "%d\n", data->light_enabled);
  436. }
  437. static ssize_t
  438. gp2a_light_enable_store(struct device *dev, struct device_attribute *attr,
  439. const char *buf, size_t count)
  440. {
  441. struct gp2a_data *data = dev_get_drvdata(dev);
  442. int value;
  443. int err = 0;
  444. if (bShutdown == true){
  445. pr_err("%s already shutdown.", __func__);
  446. goto done;
  447. }
  448. err = kstrtoint(buf, 10, &value);
  449. if (err) {
  450. pr_err("%s kstrtoint failed.", __func__);
  451. goto done;
  452. }
  453. pr_info("%s %d value = %d\n", __func__, __LINE__, value);
  454. if (value != SENSOR_DISABLE && value != SENSOR_ENABLE)
  455. goto done;
  456. if (value) {
  457. if (data->light_enabled == SENSOR_DISABLE) {
  458. if(data->prox_enabled == SENSOR_DISABLE )
  459. {
  460. err = gp2a_regulator_onoff(dev, true);
  461. if (err) {
  462. pr_err("%s, Power Up Failed\n", __func__);
  463. return err;
  464. }
  465. }
  466. lightsensor_onoff(1, data);
  467. schedule_delayed_work(&data->light_work, msecs_to_jiffies(100));
  468. data->light_enabled = SENSOR_ENABLE;
  469. } else {
  470. pr_err("%s already enabled\n", __func__);
  471. }
  472. } else {
  473. if (data->light_enabled == SENSOR_ENABLE) {
  474. if(data->prox_enabled == SENSOR_DISABLE )
  475. {
  476. err = gp2a_regulator_onoff(dev, false);
  477. if (err) {
  478. pr_err("%s, Power Down Failed\n", __func__);
  479. return err;
  480. }
  481. }
  482. lightsensor_onoff(0, data);
  483. schedule_delayed_work(&data->light_work,
  484. msecs_to_jiffies(data->light_delay));
  485. data->light_enabled = SENSOR_DISABLE;
  486. } else {
  487. pr_err("%s already disabled\n", __func__);
  488. }
  489. }
  490. done:
  491. return count;
  492. }
  493. static ssize_t
  494. gp2a_light_flush_store(struct device *dev, struct device_attribute *attr,
  495. const char *buf, size_t count)
  496. {
  497. struct gp2a_data *data = dev_get_drvdata(dev);
  498. int value;
  499. int err = 0;
  500. err = kstrtoint(buf, 10, &value);
  501. if (err) {
  502. pr_err("%s kstrtoint failed.", __func__);
  503. goto done;
  504. }
  505. pr_info("%s %d value = %d\n", __func__, __LINE__, value);
  506. if (value) {
  507. mutex_lock(&data->light_mutex);
  508. input_report_rel(data->light_input_dev, REL_MAX, 1);
  509. input_sync(data->light_input_dev);
  510. mutex_unlock(&data->light_mutex);
  511. }
  512. done:
  513. return count;
  514. }
  515. static struct device_attribute dev_attr_light_enable =
  516. __ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP,
  517. gp2a_light_enable_show, gp2a_light_enable_store);
  518. static struct device_attribute dev_attr_light_delay =
  519. __ATTR(delay, S_IRUGO|S_IWUSR|S_IWGRP,
  520. gp2a_light_delay_show, gp2a_light_delay_store);
  521. static struct device_attribute dev_attr_light_flush =
  522. __ATTR(flush, S_IWUSR|S_IWGRP,
  523. NULL, gp2a_light_flush_store);
  524. static struct attribute *gp2a_light_attributes[] = {
  525. &dev_attr_light_enable.attr,
  526. &dev_attr_light_delay.attr,
  527. &dev_attr_light_flush.attr,
  528. NULL
  529. };
  530. static struct attribute_group gp2a_light_attribute_group = {
  531. .attrs = gp2a_light_attributes,
  532. };
  533. #ifdef CONFIG_SENSORS
  534. static ssize_t gp2a_light_raw_data_show(struct device *dev,
  535. struct device_attribute *attr,
  536. char *buf)
  537. {
  538. struct gp2a_data *data = dev_get_drvdata(dev);
  539. unsigned char get_data[4] = { 0, };
  540. int d0_raw_data;
  541. int d1_raw_data;
  542. int ret = 0;
  543. if (bShutdown == true){
  544. pr_err("%s bShutdown true.", __func__);
  545. goto done;
  546. }
  547. mutex_lock(&data->data_mutex);
  548. ret = gp2a_i2c_read(data, DATA0_LSB, get_data, sizeof(get_data));
  549. mutex_unlock(&data->data_mutex);
  550. if (ret < 0)
  551. pr_err("%s i2c err: %d\n", __func__, ret) ;
  552. d0_raw_data = (get_data[1] << 8) | get_data[0]; /* clear */
  553. d1_raw_data = (get_data[3] << 8) | get_data[2]; /* IR */
  554. done:
  555. return snprintf(buf, PAGE_SIZE, "%d,%d\n", d0_raw_data, d1_raw_data);
  556. }
  557. static ssize_t gp2a_vendor_show(struct device *dev,
  558. struct device_attribute *attr, char *buf)
  559. {
  560. return snprintf(buf, PAGE_SIZE, "%s\n", GP2A_VENDOR);
  561. }
  562. static ssize_t gp2a_name_show(struct device *dev,
  563. struct device_attribute *attr, char *buf)
  564. {
  565. return snprintf(buf, PAGE_SIZE, "%s\n", GP2A_CHIP_ID);
  566. }
  567. static struct device_attribute dev_attr_light_raw_data =
  568. __ATTR(raw_data, S_IRUSR | S_IRGRP,
  569. gp2a_light_raw_data_show, NULL);
  570. static struct device_attribute dev_attr_light_lux =
  571. __ATTR(lux, S_IRUSR | S_IRGRP,
  572. gp2a_light_raw_data_show, NULL);
  573. static struct device_attribute dev_attr_light_vendor =
  574. __ATTR(vendor, S_IRUSR | S_IRGRP,
  575. gp2a_vendor_show, NULL);
  576. static struct device_attribute dev_attr_light_name =
  577. __ATTR(name, S_IRUSR | S_IRGRP,
  578. gp2a_name_show, NULL);
  579. static struct device_attribute *light_sensor_attrs[] = {
  580. &dev_attr_light_raw_data,
  581. &dev_attr_light_lux,
  582. &dev_attr_light_vendor,
  583. &dev_attr_light_name,
  584. NULL,
  585. };
  586. #endif
  587. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  588. static int gp2a_prox_open_calibration(struct gp2a_data *data)
  589. {
  590. struct file *cal_filp = NULL;
  591. int err = 0;
  592. mm_segment_t old_fs;
  593. if (data->prox_is_calibrated == true)
  594. return 1;
  595. old_fs = get_fs();
  596. set_fs(KERNEL_DS);
  597. cal_filp = filp_open(CAL_PATH,
  598. O_RDONLY, S_IRUGO | S_IWUSR | S_IWGRP);
  599. if (IS_ERR(cal_filp)) {
  600. pr_err("%s Can't open calibration file\n", __func__);
  601. set_fs(old_fs);
  602. err = PTR_ERR(cal_filp);
  603. goto done;
  604. }
  605. err = cal_filp->f_op->read(cal_filp,
  606. (char *)&data->offset_value,
  607. sizeof(int), &cal_filp->f_pos);
  608. if (err != sizeof(int)) {
  609. pr_err("%s Can't read the cal data from file\n",
  610. __func__);
  611. err = -EIO;
  612. }
  613. pr_info("%s (%d)\n", __func__, data->offset_value);
  614. filp_close(cal_filp, current->files);
  615. data->prox_is_calibrated = true;
  616. done:
  617. set_fs(old_fs);
  618. return err;
  619. }
  620. static int gp2a_prox_onoff(u8 onoff, struct gp2a_data *data)
  621. {
  622. u8 value;
  623. pr_info("%s : proximity turn on/off = %d\n", __func__, onoff);
  624. /* already on light sensor, so must simultaneously
  625. turn on light sensor and proximity sensor */
  626. if (onoff) {
  627. int i;
  628. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  629. gpio_set_value(data->vled_gpio, 1);
  630. #endif
  631. for (i = 0; i < COL; i++) {
  632. int err = gp2a_i2c_write(data, gp2a_reg[i][0],
  633. &gp2a_reg[i][1]);
  634. if (err < 0)
  635. pr_err("%s : turnning on error i = %d, err=%d\n",
  636. __func__, i, err);
  637. data->lightsensor_mode = 0;
  638. }
  639. } else {
  640. if (data->light_enabled) {
  641. if (data->lightsensor_mode)
  642. value = 0x67; /*resolution :16bit, range: *8(HIGH) */
  643. else
  644. value = 0x63; /* resolution :16bit, range: *128(LOW) */
  645. gp2a_i2c_write(data, COMMAND2, &value);
  646. /* OP3 : 1(operating mode)
  647. OP2 : 1(coutinuous operating mode) OP1 : 01(ALS mode) */
  648. value = 0xD0;
  649. gp2a_i2c_write(data, COMMAND1, &value);
  650. } else {
  651. value = 0x00; /*shutdown mode */
  652. gp2a_i2c_write(data, (u8) (COMMAND1), &value);
  653. }
  654. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  655. gpio_set_value(data->vled_gpio, 0);
  656. #endif
  657. }
  658. return 0;
  659. }
  660. static ssize_t
  661. gp2a_prox_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  662. {
  663. struct gp2a_data *data = dev_get_drvdata(dev);
  664. return snprintf(buf, PAGE_SIZE, "%d\n", data->light_enabled);
  665. }
  666. static ssize_t
  667. gp2a_prox_enable_store(struct device *dev, struct device_attribute *attr,
  668. const char *buf, size_t count)
  669. {
  670. struct gp2a_data *data = dev_get_drvdata(dev);
  671. int value;
  672. int err = 0;
  673. if (bShutdown == true) {
  674. pr_err("%s already shutdown.", __func__);
  675. goto done;
  676. }
  677. err = kstrtoint(buf, 10, &value);
  678. if (err) {
  679. pr_err("%s kstrtoint failed.", __func__);
  680. goto done;
  681. }
  682. pr_info("%s %d value = %d\n", __func__, __LINE__, value);
  683. if (value != SENSOR_DISABLE && value != SENSOR_ENABLE)
  684. goto done;
  685. if (value) {
  686. if (data->prox_enabled == SENSOR_DISABLE) {
  687. uint16_t thrd = 0;
  688. u8 reg;
  689. if(data->light_enabled == SENSOR_DISABLE )
  690. {
  691. err = gp2a_regulator_onoff(dev, true);
  692. if (err) {
  693. pr_err("%s, Power Up Failed\n", __func__);
  694. return err;
  695. }
  696. }
  697. gp2a_prox_onoff(1, data);
  698. err = gp2a_prox_open_calibration(data);
  699. if (err < 0 && err != -ENOENT)
  700. pr_err("%s gp2a_prox_open_offset() failed\n",
  701. __func__);
  702. else {
  703. thrd = gp2a_reg[3][1]+(data->offset_value);
  704. THR_REG_LSB(thrd, reg);
  705. gp2a_i2c_write(data, gp2a_reg[3][0], &reg);
  706. THR_REG_MSB(thrd, reg);
  707. gp2a_i2c_write(data, gp2a_reg[4][0], &reg);
  708. thrd = gp2a_reg[5][1]+(data->offset_value);
  709. THR_REG_LSB(thrd, reg);
  710. gp2a_i2c_write(data, gp2a_reg[5][0], &reg);
  711. THR_REG_MSB(thrd, reg);
  712. gp2a_i2c_write(data, gp2a_reg[6][0], &reg);
  713. }
  714. enable_irq_wake(data->irq);
  715. enable_irq(data->irq);
  716. input_report_abs(data->prox_input_dev, ABS_DISTANCE, 1);
  717. input_sync(data->prox_input_dev);
  718. data->prox_enabled = SENSOR_ENABLE;
  719. } else {
  720. pr_err("%s already enabled\n", __func__);
  721. }
  722. } else {
  723. if (data->prox_enabled == SENSOR_ENABLE) {
  724. if(data->light_enabled == SENSOR_DISABLE )
  725. {
  726. err = gp2a_regulator_onoff(dev, false);
  727. if (err) {
  728. pr_err("%s, Power Down Failed\n", __func__);
  729. return err;
  730. }
  731. }
  732. disable_irq(data->irq);
  733. disable_irq_wake(data->irq);
  734. gp2a_prox_onoff(0, data);
  735. data->prox_enabled = SENSOR_DISABLE;
  736. } else {
  737. pr_err("%s already disabled\n", __func__);
  738. }
  739. }
  740. done:
  741. return count;
  742. }
  743. static ssize_t
  744. gp2a_prox_flush_store(struct device *dev, struct device_attribute *attr,
  745. const char *buf, size_t count)
  746. {
  747. struct gp2a_data *data = dev_get_drvdata(dev);
  748. int value;
  749. int err = 0;
  750. err = kstrtoint(buf, 10, &value);
  751. if (err) {
  752. pr_err("%s kstrtoint failed.", __func__);
  753. goto done;
  754. }
  755. pr_info("%s %d value = %d\n", __func__, __LINE__, value);
  756. if (err) {
  757. pr_err("%s kstrtoint failed.", __func__);
  758. goto done;
  759. }
  760. pr_info("%s %d value = %d\n", __func__, __LINE__, value);
  761. if (value) {
  762. mutex_lock(&data->data_mutex);
  763. input_report_abs(data->prox_input_dev, ABS_MAX, 1);
  764. input_sync(data->prox_input_dev);
  765. mutex_unlock(&data->data_mutex);
  766. }
  767. done:
  768. return count;
  769. }
  770. static struct device_attribute dev_attr_prox_enable =
  771. __ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP,
  772. gp2a_prox_enable_show, gp2a_prox_enable_store);
  773. static struct device_attribute dev_attr_prox_flush =
  774. __ATTR(flush, S_IWUSR|S_IWGRP,
  775. NULL, gp2a_prox_flush_store);
  776. static struct attribute *gp2a_prox_attributes[] = {
  777. &dev_attr_prox_enable.attr,
  778. &dev_attr_prox_flush.attr,
  779. NULL
  780. };
  781. static struct attribute_group gp2a_prox_attribute_group = {
  782. .attrs = gp2a_prox_attributes,
  783. };
  784. #ifdef CONFIG_SENSORS
  785. static int gp2a_prox_adc_read(struct gp2a_data *data)
  786. {
  787. int sum[OFFSET_ARRAY_LENGTH];
  788. int i = OFFSET_ARRAY_LENGTH-1;
  789. int avg = 0;
  790. int min = 0;
  791. int max = 0;
  792. int total = 0;
  793. if (bShutdown == true){
  794. pr_err("%s bShutdown true.", __func__);
  795. goto done;
  796. }
  797. mutex_lock(&data->data_mutex);
  798. do {
  799. unsigned char get_D2_data[2] = {0,};
  800. int D2_data;
  801. msleep(50);
  802. gp2a_i2c_read(data, DATA2_LSB, get_D2_data,
  803. sizeof(get_D2_data));
  804. D2_data = (get_D2_data[1] << 8) | get_D2_data[0];
  805. sum[i] = D2_data;
  806. if (i == OFFSET_ARRAY_LENGTH - 1) {
  807. min = sum[i];
  808. max = sum[i];
  809. } else {
  810. if (sum[i] < min)
  811. min = sum[i];
  812. else if (sum[i] > max)
  813. max = sum[i];
  814. }
  815. total += sum[i];
  816. } while (i--);
  817. mutex_unlock(&data->data_mutex);
  818. total -= (min + max);
  819. avg = (int)(total / (OFFSET_ARRAY_LENGTH - 2));
  820. pr_info("%s offset = %d\n", __func__, avg);
  821. done:
  822. return avg;
  823. }
  824. static int gp2a_prox_do_calibrate(struct gp2a_data *data,
  825. bool do_calib, bool thresh_set)
  826. {
  827. struct file *cal_filp;
  828. int err;
  829. int xtalk_avg = 0;
  830. int offset_change = 0;
  831. uint16_t thrd = 0;
  832. u8 reg;
  833. mm_segment_t old_fs;
  834. if (do_calib) {
  835. if (thresh_set) {
  836. /* for gp2a_prox_thresh_store */
  837. data->offset_value =
  838. data->threshold_high -
  839. (gp2a_reg[6][1] << 8 | gp2a_reg[5][1]);
  840. } else {
  841. /* tap offset button */
  842. /* get offset value */
  843. xtalk_avg = gp2a_prox_adc_read(data);
  844. offset_change =
  845. (gp2a_reg[6][1] << 8 | gp2a_reg[5][1])
  846. - DEFAULT_HI_THR;
  847. if (xtalk_avg < offset_change) {
  848. /* do not need calibration */
  849. data->cal_result = 0;
  850. err = 0;
  851. goto no_cal;
  852. }
  853. data->offset_value = xtalk_avg - offset_change;
  854. }
  855. /* update threshold */
  856. thrd = (gp2a_reg[4][1] << 8 | gp2a_reg[3][1])
  857. + (data->offset_value);
  858. THR_REG_LSB(thrd, reg);
  859. gp2a_i2c_write(data, gp2a_reg[3][0], &reg);
  860. THR_REG_MSB(thrd, reg);
  861. gp2a_i2c_write(data, gp2a_reg[4][0], &reg);
  862. thrd = (gp2a_reg[4][1] << 8 | gp2a_reg[5][1])
  863. +(data->offset_value);
  864. THR_REG_LSB(thrd, reg);
  865. gp2a_i2c_write(data, gp2a_reg[5][0], &reg);
  866. THR_REG_MSB(thrd, reg);
  867. gp2a_i2c_write(data, gp2a_reg[6][0], &reg);
  868. /* calibration result */
  869. if (!thresh_set)
  870. data->cal_result = 1;
  871. } else {
  872. /* tap reset button */
  873. data->offset_value = 0;
  874. /* update threshold */
  875. gp2a_i2c_write(data, gp2a_reg[3][0], &gp2a_reg[3][1]);
  876. gp2a_i2c_write(data, gp2a_reg[4][0], &gp2a_reg[4][1]);
  877. gp2a_i2c_write(data, gp2a_reg[5][0], &gp2a_reg[5][1]);
  878. gp2a_i2c_write(data, gp2a_reg[6][0], &gp2a_reg[6][1]);
  879. /* calibration result */
  880. data->cal_result = 2;
  881. }
  882. old_fs = get_fs();
  883. set_fs(KERNEL_DS);
  884. cal_filp = filp_open(CAL_PATH,
  885. O_CREAT | O_TRUNC | O_WRONLY,
  886. S_IRUGO | S_IWUSR | S_IWGRP);
  887. if (IS_ERR(cal_filp)) {
  888. pr_err("%s Can't open calibration file\n", __func__);
  889. set_fs(old_fs);
  890. err = PTR_ERR(cal_filp);
  891. goto done;
  892. }
  893. err = cal_filp->f_op->write(cal_filp,
  894. (char *)&data->offset_value, sizeof(int),
  895. &cal_filp->f_pos);
  896. if (err != sizeof(int)) {
  897. pr_err("%s Can't write the cal data to file\n",
  898. __func__);
  899. err = -EIO;
  900. }
  901. filp_close(cal_filp, current->files);
  902. done:
  903. set_fs(old_fs);
  904. no_cal:
  905. return err;
  906. }
  907. static ssize_t gp2a_prox_cal_show(struct device *dev,
  908. struct device_attribute *attr, char *buf)
  909. {
  910. struct gp2a_data *data = dev_get_drvdata(dev);
  911. int thresh_hi, thresh_low;
  912. unsigned char get_D2_data[4];
  913. if (bShutdown == true){
  914. pr_err("%s bShutdown true.", __func__);
  915. goto done;
  916. }
  917. msleep(20);
  918. gp2a_i2c_read(data, PS_LT_LSB, get_D2_data,
  919. sizeof(get_D2_data));
  920. thresh_hi = (get_D2_data[3] << 8) | get_D2_data[2];
  921. thresh_low = (get_D2_data[1] << 8) | get_D2_data[0];
  922. data->threshold_high = thresh_hi;
  923. done:
  924. return sprintf(buf, "%d,%d,%d\n",
  925. data->offset_value, thresh_hi, thresh_low);
  926. }
  927. static ssize_t gp2a_prox_cal_store(struct device *dev,
  928. struct device_attribute *attr,
  929. const char *buf, size_t size)
  930. {
  931. struct gp2a_data *data = dev_get_drvdata(dev);
  932. bool do_calib;
  933. int err;
  934. if (bShutdown == true){
  935. pr_err("%s bShutdown true.", __func__);
  936. goto done;
  937. }
  938. if (sysfs_streq(buf, "1")) { /* calibrate cancelation value */
  939. do_calib = true;
  940. } else if (sysfs_streq(buf, "0")) { /* reset cancelation value */
  941. do_calib = false;
  942. } else {
  943. pr_err("%s invalid value %d\n", __func__, *buf);
  944. err = -EINVAL;
  945. goto done;
  946. }
  947. err = gp2a_prox_do_calibrate(data, do_calib, false);
  948. if (err < 0) {
  949. pr_err("%s gp2a_prox_store_offset() failed\n",
  950. __func__);
  951. goto done;
  952. } else
  953. err = size;
  954. done:
  955. return err;
  956. }
  957. static void gp2a_work_avg_prox(struct work_struct *work)
  958. {
  959. struct gp2a_data *data = container_of((struct delayed_work *)work,
  960. struct gp2a_data, prox_avg_work);
  961. int min = 0, max = 0, avg = 0;
  962. int i = 0;
  963. unsigned char raw_data[2] = { 0, };
  964. for (i = 0; i < PROX_READ_NUM; i++) {
  965. int gp2a_prox_value;
  966. mutex_lock(&data->data_mutex);
  967. gp2a_i2c_read(data, 0x10, raw_data, sizeof(raw_data));
  968. mutex_unlock(&data->data_mutex);
  969. gp2a_prox_value = (raw_data[1] << 8) | raw_data[0];
  970. if (gp2a_prox_value > GP2A_PROX_MAX)
  971. gp2a_prox_value = GP2A_PROX_MAX;
  972. if (gp2a_prox_value > GP2A_PROX_MIN) {
  973. avg += gp2a_prox_value;
  974. if (!i)
  975. min = gp2a_prox_value;
  976. else if (gp2a_prox_value < min)
  977. min = gp2a_prox_value;
  978. if (gp2a_prox_value > max)
  979. max = gp2a_prox_value;
  980. } else {
  981. gp2a_prox_value = GP2A_PROX_MIN;
  982. }
  983. msleep(40);
  984. }
  985. avg /= i;
  986. data->avg[0] = min;
  987. data->avg[1] = avg;
  988. data->avg[2] = max;
  989. if (data->prox_enabled)
  990. schedule_delayed_work(&data->prox_avg_work,
  991. msecs_to_jiffies(data->prox_delay));
  992. }
  993. static ssize_t gp2a_pxor_prox_avg_show(struct device *dev,
  994. struct device_attribute *attr,
  995. char *buf)
  996. {
  997. struct gp2a_data *data = dev_get_drvdata(dev);
  998. if (bShutdown == true){
  999. pr_err("%s bShutdown true.", __func__);
  1000. return snprintf(buf, PAGE_SIZE, "0,0,0\n");
  1001. }
  1002. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", data->avg[0],
  1003. data->avg[1], data->avg[2]);
  1004. }
  1005. static ssize_t gp2a_pxor_prox_avg_store(struct device *dev,
  1006. struct device_attribute *attr,
  1007. const char *buf, size_t size)
  1008. {
  1009. struct gp2a_data *data = dev_get_drvdata(dev);
  1010. int enable = 0;
  1011. int err = 0;
  1012. if (bShutdown == true){
  1013. pr_err("%s bShutdown true.", __func__);
  1014. goto done;
  1015. }
  1016. err = kstrtoint(buf, 10, &enable);
  1017. if (err < 0) {
  1018. pr_err("%s, kstrtoint failed.", __func__);
  1019. } else {
  1020. pr_info("%s, %d\n", __func__, enable);
  1021. if (enable)
  1022. schedule_delayed_work(&data->prox_avg_work,
  1023. msecs_to_jiffies(data->prox_delay));
  1024. else
  1025. cancel_delayed_work_sync(&data->prox_avg_work);
  1026. }
  1027. done:
  1028. return size;
  1029. }
  1030. static ssize_t gp2a_prox_raw_data_show(struct device *dev,
  1031. struct device_attribute *attr,
  1032. char *buf)
  1033. {
  1034. struct gp2a_data *data = dev_get_drvdata(dev);
  1035. int d2_data = 0;
  1036. unsigned char raw_data[2] = { 0, };
  1037. if (bShutdown == true){
  1038. pr_err("%s bShutdown true.", __func__);
  1039. goto done;
  1040. }
  1041. mutex_lock(&data->data_mutex);
  1042. gp2a_i2c_read(data, 0x10, raw_data, sizeof(raw_data));
  1043. mutex_unlock(&data->data_mutex);
  1044. d2_data = (raw_data[1] << 8) | raw_data[0];
  1045. done:
  1046. return snprintf(buf, PAGE_SIZE, "%d\n", d2_data);
  1047. }
  1048. static int gp2a_prox_manual_offset(struct gp2a_data *data, u8 change_on)
  1049. {
  1050. struct file *cal_filp;
  1051. int err;
  1052. int16_t thrd;
  1053. u8 reg;
  1054. mm_segment_t old_fs;
  1055. data->offset_value = change_on;
  1056. /* update threshold */
  1057. thrd = gp2a_reg[3][1]+(data->offset_value);
  1058. THR_REG_LSB(thrd, reg);
  1059. gp2a_i2c_write(data, gp2a_reg[3][0], &reg);
  1060. THR_REG_MSB(thrd, reg);
  1061. gp2a_i2c_write(data, gp2a_reg[4][0], &reg);
  1062. thrd = gp2a_reg[5][1]+(data->offset_value);
  1063. THR_REG_LSB(thrd, reg);
  1064. gp2a_i2c_write(data, gp2a_reg[5][0], &reg);
  1065. THR_REG_MSB(thrd, reg);
  1066. gp2a_i2c_write(data, gp2a_reg[6][0], &reg);
  1067. /* calibration result */
  1068. data->cal_result = 1;
  1069. old_fs = get_fs();
  1070. set_fs(KERNEL_DS);
  1071. cal_filp = filp_open(CAL_PATH,
  1072. O_CREAT | O_TRUNC | O_WRONLY,
  1073. S_IRUGO | S_IWUSR | S_IWGRP);
  1074. if (IS_ERR(cal_filp)) {
  1075. pr_err("%s Can't open calibration file\n", __func__);
  1076. set_fs(old_fs);
  1077. err = PTR_ERR(cal_filp);
  1078. goto done;
  1079. }
  1080. err = cal_filp->f_op->write(cal_filp,
  1081. (char *)&data->offset_value, sizeof(int),
  1082. &cal_filp->f_pos);
  1083. if (err != sizeof(int)) {
  1084. pr_err("%s Can't write the cal data to file\n",
  1085. __func__);
  1086. err = -EIO;
  1087. }
  1088. filp_close(cal_filp, current->files);
  1089. done:
  1090. set_fs(old_fs);
  1091. return err;
  1092. }
  1093. static ssize_t gp2a_prox_cal2_store(struct device *dev,
  1094. struct device_attribute *attr,
  1095. const char *buf, size_t size)
  1096. {
  1097. struct gp2a_data *data = dev_get_drvdata(dev);
  1098. u8 change_on;
  1099. int err;
  1100. if (bShutdown == true){
  1101. pr_err("%s bShutdown true.", __func__);
  1102. goto done;
  1103. }
  1104. if (sysfs_streq(buf, "1")) /* change hi threshold by -2 */
  1105. change_on = -2;
  1106. else if (sysfs_streq(buf, "2")) /*change hi threshold by +4 */
  1107. change_on = 4;
  1108. else if (sysfs_streq(buf, "3")) /*change hi threshold by +8 */
  1109. change_on = 8;
  1110. else {
  1111. pr_err("%s invalid value %d\n", __func__, *buf);
  1112. goto done;
  1113. }
  1114. err = gp2a_prox_manual_offset(data, change_on);
  1115. if (err < 0) {
  1116. pr_err("%s gp2a_prox_store_offset() failed\n",
  1117. __func__);
  1118. goto done;
  1119. }
  1120. done:
  1121. return size;
  1122. }
  1123. static ssize_t gp2a_prox_thresh_show(struct device *dev,
  1124. struct device_attribute *attr, char *buf)
  1125. {
  1126. struct gp2a_data *data = dev_get_drvdata(dev);
  1127. int thresh_hi = 0, thresh_low = 0;
  1128. unsigned char get_D2_data[4];
  1129. if (bShutdown == true){
  1130. pr_err("%s bShutdown true.", __func__);
  1131. goto done;
  1132. }
  1133. msleep(20);
  1134. gp2a_i2c_read(data, PS_LT_LSB, get_D2_data,
  1135. sizeof(get_D2_data));
  1136. thresh_hi = (get_D2_data[3] << 8) | get_D2_data[2];
  1137. thresh_low = (get_D2_data[1] << 8) | get_D2_data[0];
  1138. pr_info("%s THRESHOLD = %d\n", __func__, thresh_hi);
  1139. done:
  1140. return sprintf(buf, "%d,%d\n", thresh_hi, thresh_low);
  1141. }
  1142. static ssize_t gp2a_prox_thresh_store(struct device *dev,
  1143. struct device_attribute *attr,
  1144. const char *buf, size_t size)
  1145. {
  1146. struct gp2a_data *data = dev_get_drvdata(dev);
  1147. long thresh_value = 0;
  1148. int err = 0;
  1149. if (bShutdown == true){
  1150. pr_err("%s bShutdown true.", __func__);
  1151. goto done;
  1152. }
  1153. err = strict_strtol(buf, 10, &thresh_value);
  1154. if (unlikely(err < 0)) {
  1155. pr_err("%s kstrtoint failed.", __func__);
  1156. goto done;
  1157. }
  1158. data->threshold_high = (uint16_t)thresh_value;
  1159. err = gp2a_prox_do_calibrate(data, true, true);
  1160. if (err < 0) {
  1161. pr_err("%s thresh_store failed\n", __func__);
  1162. goto done;
  1163. }
  1164. msleep(20);
  1165. done:
  1166. return size;
  1167. }
  1168. static ssize_t prox_offset_pass_show(struct device *dev,
  1169. struct device_attribute *attr, char *buf)
  1170. {
  1171. struct gp2a_data *data = dev_get_drvdata(dev);
  1172. if (bShutdown == true){
  1173. pr_err("%s bShutdown true.", __func__);
  1174. return sprintf(buf, "0\n");
  1175. }
  1176. return sprintf(buf, "%d\n", data->cal_result);
  1177. }
  1178. static struct device_attribute dev_attr_gp2a_prox_sensor_prox_cal2 =
  1179. __ATTR(prox_cal2, S_IRUGO | S_IWUSR | S_IWGRP,
  1180. NULL, gp2a_prox_cal2_store);
  1181. static struct device_attribute dev_attr_gp2a_prox_thresh =
  1182. __ATTR(prox_thresh, S_IRUGO | S_IWUSR | S_IWGRP,
  1183. gp2a_prox_thresh_show, gp2a_prox_thresh_store);
  1184. static struct device_attribute dev_attr_gp2a_prox_offset_pass =
  1185. __ATTR(prox_offset_pass, S_IRUGO | S_IWUSR | S_IWGRP,
  1186. prox_offset_pass_show, NULL);
  1187. static struct device_attribute dev_attr_prox_cal =
  1188. __ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
  1189. gp2a_prox_cal_show, gp2a_prox_cal_store);
  1190. static struct device_attribute dev_attr_prox_prox_avg =
  1191. __ATTR(prox_avg, S_IRUGO | S_IWUSR | S_IWGRP,
  1192. gp2a_pxor_prox_avg_show, gp2a_pxor_prox_avg_store);
  1193. static struct device_attribute dev_attr_prox_raw_data =
  1194. __ATTR(raw_data, S_IRUSR | S_IRGRP,
  1195. gp2a_prox_raw_data_show, NULL);
  1196. static struct device_attribute dev_attr_prox_state =
  1197. __ATTR(state, S_IRUSR | S_IRGRP,
  1198. gp2a_prox_raw_data_show, NULL);
  1199. static struct device_attribute dev_attr_prox_vendor =
  1200. __ATTR(vendor, S_IRUSR | S_IRGRP,
  1201. gp2a_vendor_show, NULL);
  1202. static struct device_attribute dev_attr_prox_name =
  1203. __ATTR(name, S_IRUSR | S_IRGRP,
  1204. gp2a_name_show, NULL);
  1205. static struct device_attribute *prox_sensor_attrs[] = {
  1206. &dev_attr_prox_state,
  1207. &dev_attr_prox_prox_avg,
  1208. &dev_attr_prox_raw_data,
  1209. &dev_attr_prox_vendor,
  1210. &dev_attr_prox_name,
  1211. &dev_attr_prox_cal,
  1212. &dev_attr_gp2a_prox_thresh,
  1213. &dev_attr_gp2a_prox_offset_pass,
  1214. &dev_attr_gp2a_prox_sensor_prox_cal2,
  1215. NULL,
  1216. };
  1217. #endif
  1218. static void gp2a_work_func_prox(struct work_struct *work)
  1219. {
  1220. struct gp2a_data *data = container_of((struct work_struct *)work,
  1221. struct gp2a_data, proximity_work);
  1222. unsigned char value;
  1223. int ret;
  1224. ret = gp2a_i2c_read(data, COMMAND1, &value, sizeof(value));
  1225. if (ret < 0) {
  1226. pr_info("%s, read data error\n", __func__);
  1227. } else {
  1228. pr_info("%s, read data %d, %d\n", __func__, value & 0x08, !(value & 0x08));
  1229. data->proximity_detection = !(value & 0x08);
  1230. }
  1231. if (!(value & 0x08)) {
  1232. if (data->lightsensor_mode == 0)
  1233. value = 0x63;
  1234. else
  1235. value = 0x67;
  1236. gp2a_i2c_write(data, COMMAND2, &value);
  1237. } else {
  1238. if (data->lightsensor_mode == 0)
  1239. value = 0x23;
  1240. else
  1241. value = 0x27;
  1242. gp2a_i2c_write(data, COMMAND2, &value);
  1243. }
  1244. value = 0xCC;
  1245. gp2a_i2c_write(data, COMMAND1, &value);
  1246. ret = gp2a_i2c_read(data, COMMAND1, &value, sizeof(value));
  1247. if (ret < 0)
  1248. pr_info("%s, read data error\n", __func__);
  1249. pr_info("%s, detection=%d, mode=%d, rev=%d\n", __func__,
  1250. data->proximity_detection, data->lightsensor_mode, system_rev);
  1251. if (system_rev < 12) {
  1252. input_report_abs(data->prox_input_dev, ABS_DISTANCE, data->proximity_detection);
  1253. } else {
  1254. if (!gpio_get_value(data->con_gpio)) {
  1255. input_report_abs(data->prox_input_dev, ABS_DISTANCE, data->proximity_detection);
  1256. } else {
  1257. if (!data->proximity_detection) {
  1258. pr_err("%s, Conducntion is Connect\n", __func__);
  1259. input_report_abs(data->prox_input_dev, ABS_DISTANCE, 1);
  1260. } else {
  1261. input_report_abs(data->prox_input_dev, ABS_DISTANCE, data->proximity_detection);
  1262. }
  1263. }
  1264. }
  1265. input_sync(data->prox_input_dev);
  1266. }
  1267. static irqreturn_t gp2a_irq_handler(int irq, void *dev_id)
  1268. {
  1269. struct gp2a_data *gp2a = dev_id;
  1270. wake_lock_timeout(&gp2a->prx_wake_lock, 3 * HZ);
  1271. schedule_work(&gp2a->proximity_work);
  1272. pr_info("%s IRQ_HANDLED.\n", __func__);
  1273. return IRQ_HANDLED;
  1274. }
  1275. static int gp2a_setup_irq(struct gp2a_data *data)
  1276. {
  1277. int rc;
  1278. pr_err("%s, start\n", __func__);
  1279. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1280. rc = gpio_request(data->vled_gpio, "gpio_vled_en");
  1281. if (unlikely(rc < 0)) {
  1282. pr_err("%s: gpio %d request failed (%d)\n",
  1283. __func__, data->vled_gpio, rc);
  1284. goto done;
  1285. }
  1286. rc = gpio_direction_output(data->vled_gpio, 0);
  1287. if (unlikely(rc < 0)) {
  1288. pr_err("%s: failed to set gpio %d as input (%d)\n",
  1289. __func__, data->vled_gpio, rc);
  1290. goto err_gpio_direction_output;
  1291. }
  1292. #endif
  1293. rc = gpio_request(data->gpio, "gpio_gp2a_prox_out");
  1294. if (unlikely(rc < 0)) {
  1295. pr_err("%s: gpio %d request failed (%d)\n",
  1296. __func__, data->gpio, rc);
  1297. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1298. goto err_gpio_direction_output;
  1299. #else
  1300. goto done;
  1301. #endif
  1302. }
  1303. rc = gpio_direction_input(data->gpio);
  1304. if (unlikely(rc < 0)) {
  1305. pr_err("%s: failed to set gpio %d as input (%d)\n",
  1306. __func__, data->gpio, rc);
  1307. goto err_gpio_direction_input;
  1308. }
  1309. data->irq = gpio_to_irq(data->gpio);
  1310. rc = request_threaded_irq(data->irq, NULL,
  1311. gp2a_irq_handler,
  1312. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1313. "gp2a_prox_int", data);
  1314. if (unlikely(rc < 0)) {
  1315. pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
  1316. __func__, data->irq, data->gpio, rc);
  1317. goto err_request_irq;
  1318. }
  1319. disable_irq(data->irq);
  1320. pr_info("%s, success\n", __func__);
  1321. goto done;
  1322. err_request_irq:
  1323. err_gpio_direction_input:
  1324. gpio_free(data->gpio);
  1325. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1326. err_gpio_direction_output:
  1327. gpio_free(data->vled_gpio);
  1328. #endif
  1329. done:
  1330. return rc;
  1331. }
  1332. static int gp2a_parse_dt(struct gp2a_data *data, struct device *dev)
  1333. {
  1334. struct device_node *this_node= dev->of_node;
  1335. enum of_gpio_flags flags;
  1336. int rc;
  1337. if (this_node == NULL)
  1338. return -ENODEV;
  1339. data->gpio = of_get_named_gpio_flags(this_node,
  1340. "gp2a030a,irq_gpio", 0, &flags);
  1341. if (data->gpio < 0) {
  1342. pr_err("%s : get irq_gpio(%d) error\n", __func__, data->gpio);
  1343. return -ENODEV;
  1344. }
  1345. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1346. data->vled_gpio = of_get_named_gpio_flags(this_node,
  1347. "gp2a030a,vled_gpio", 0, &flags);
  1348. if (data->vled_gpio < 0) {
  1349. pr_err("%s : get irq_gpio(%d) error\n", __func__, data->vled_gpio);
  1350. return -ENODEV;
  1351. }
  1352. #endif
  1353. if (system_rev >= 12) {
  1354. data->con_gpio = of_get_named_gpio_flags(this_node,
  1355. "gp2a030a,con_gpio", 0, &flags);
  1356. if (data->con_gpio < 0) {
  1357. pr_err("%s : get con_gpio(%d) error\n", __func__, data->con_gpio);
  1358. return -ENODEV;
  1359. }
  1360. rc = gpio_request(data->con_gpio, "con_gpio");
  1361. if (unlikely(rc < 0)) {
  1362. pr_err("%s: gpio %d request failed (%d)\n",
  1363. __func__, data->con_gpio, rc);
  1364. goto done;
  1365. }
  1366. rc = gpio_direction_input(data->con_gpio);
  1367. if (unlikely(rc < 0)) {
  1368. pr_err("%s: failed to set gpio %d as input (%d)\n",
  1369. __func__, data->con_gpio, rc);
  1370. goto err_con_gpio_direction_input;
  1371. }
  1372. }
  1373. return 0;
  1374. err_con_gpio_direction_input:
  1375. gpio_free(data->con_gpio);
  1376. done:
  1377. return rc;
  1378. }
  1379. #endif
  1380. static int gp2a_probe(struct i2c_client *client,
  1381. const struct i2c_device_id *id)
  1382. {
  1383. int err = 0;
  1384. struct gp2a_data *data;
  1385. u8 value;
  1386. pr_info("%s, is called\n", __func__);
  1387. if (client == NULL) {
  1388. pr_err("%s, client doesn't exist\n", __func__);
  1389. err = -ENOMEM;
  1390. return err;
  1391. }
  1392. err = gp2a_regulator_onoff(&client->dev, true);
  1393. if (err) {
  1394. pr_err("%s, Power Up Failed\n", __func__);
  1395. return err;
  1396. }
  1397. data = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
  1398. if (!data) {
  1399. pr_err("%s, kzalloc error\n", __func__);
  1400. err = -ENOMEM;
  1401. return err;
  1402. }
  1403. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1404. err = gp2a_parse_dt(data, &client->dev);
  1405. if (err) {
  1406. pr_err("%s, get gpio is failed\n", __func__);
  1407. goto gp2a_parse_dt_err;
  1408. }
  1409. #endif
  1410. data->client = client;
  1411. data->light_delay = MAX_DELAY;
  1412. bShutdown = false;
  1413. i2c_set_clientdata(client, data);
  1414. value = 0x00;
  1415. err = gp2a_i2c_write(data, (u8) (COMMAND1), &value);
  1416. if (err < 0) {
  1417. pr_err("%s failed : threre is no such device.\n", __func__);
  1418. goto i2c_fail_err;
  1419. }
  1420. data->light_input_dev = input_allocate_device();
  1421. if (!data->light_input_dev) {
  1422. pr_err("%s input_allocate_device error\n", __func__);
  1423. err = -ENOMEM;
  1424. goto input_allocate_light_device_err;
  1425. }
  1426. data->light_input_dev->name = "light_sensor";
  1427. input_set_capability(data->light_input_dev, EV_REL, REL_MAX);
  1428. input_set_capability(data->light_input_dev, EV_REL, REL_MISC);
  1429. input_set_drvdata(data->light_input_dev, data);
  1430. err = input_register_device(data->light_input_dev);
  1431. if (err < 0) {
  1432. pr_err("%s input_register_device light error\n", __func__);
  1433. goto input_register_device_err;
  1434. }
  1435. #if defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  1436. err = sensors_create_symlink(&data->light_input_dev->dev.kobj, data->light_input_dev->name);
  1437. if (err < 0) {
  1438. pr_err("%s sensors_create_symlink light error\n", __func__);
  1439. goto sensors_create_symlink_err;
  1440. }
  1441. #endif
  1442. err = sysfs_create_group(&data->light_input_dev->dev.kobj,
  1443. &gp2a_light_attribute_group);
  1444. if (err) {
  1445. pr_err("%s sysfs_create_group light error\n", __func__);
  1446. goto sysfs_create_group_light_err;
  1447. }
  1448. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1449. data->prox_is_calibrated = false;
  1450. err = gp2a_setup_irq(data);
  1451. if (err) {
  1452. pr_err("%s: could not setup irq\n", __func__);
  1453. goto err_setup_irq;
  1454. }
  1455. data->prox_input_dev = input_allocate_device();
  1456. if (!data->prox_input_dev) {
  1457. pr_err("%s input_allocate_device error\n", __func__);
  1458. err = -ENOMEM;
  1459. goto input_allocate_prox_device_err;
  1460. }
  1461. data->prox_input_dev->name = "proximity_sensor";
  1462. input_set_capability(data->prox_input_dev, EV_ABS, ABS_DISTANCE);
  1463. input_set_capability(data->prox_input_dev, EV_ABS, ABS_MAX);
  1464. input_set_abs_params(data->prox_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
  1465. input_set_abs_params(data->prox_input_dev, ABS_MAX, 0, 1, 0, 0);
  1466. input_set_drvdata(data->prox_input_dev, data);
  1467. err = input_register_device(data->prox_input_dev);
  1468. if (err < 0) {
  1469. pr_err("%s input_register_device prox error\n", __func__);
  1470. goto input_register_prox_device_err;
  1471. }
  1472. #if defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  1473. err = sensors_create_symlink(&data->prox_input_dev->dev.kobj, data->prox_input_dev->name);
  1474. if (err < 0) {
  1475. pr_err("%s sensors_create_symlink light error\n", __func__);
  1476. goto sensors_create_symlink_light_err;
  1477. }
  1478. #endif
  1479. err = sysfs_create_group(&data->prox_input_dev->dev.kobj,
  1480. &gp2a_prox_attribute_group);
  1481. if (err) {
  1482. pr_err("%s sysfs_create_group prox error\n", __func__);
  1483. goto sysfs_create_group_prox_err;
  1484. }
  1485. #endif
  1486. #ifdef CONFIG_SENSORS
  1487. err = sensors_register(data->light_sensor_device,
  1488. data, light_sensor_attrs, "light_sensor");
  1489. if (err) {
  1490. pr_err("%s: cound not register prox sensor device(%d).\n",
  1491. __func__, err);
  1492. goto sensors_register_light_err;
  1493. }
  1494. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1495. err = sensors_register(data->prox_sensor_device,
  1496. data, prox_sensor_attrs, "proximity_sensor");
  1497. if (err) {
  1498. pr_err("%s: cound not register prox sensor device(%d).\n",
  1499. __func__, err);
  1500. goto sensors_register_prox_err;
  1501. }
  1502. #endif
  1503. #endif
  1504. mutex_init(&data->light_mutex);
  1505. mutex_init(&data->data_mutex);
  1506. INIT_DELAYED_WORK(&data->light_work, gp2a_work_func_light);
  1507. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1508. wake_lock_init(&data->prx_wake_lock,
  1509. WAKE_LOCK_SUSPEND, "prx_wake_lock");
  1510. INIT_WORK(&data->proximity_work, gp2a_work_func_prox);
  1511. #ifdef CONFIG_SENSORS
  1512. INIT_DELAYED_WORK(&data->prox_avg_work, gp2a_work_avg_prox);
  1513. #endif
  1514. #endif
  1515. err = gp2a_regulator_onoff(&client->dev, false);
  1516. if (err) {
  1517. pr_err("%s, Power Down Failed\n", __func__);
  1518. return err;
  1519. }
  1520. goto done;
  1521. mutex_destroy(&data->light_mutex);
  1522. mutex_destroy(&data->data_mutex);
  1523. #ifdef CONFIG_SENSORS
  1524. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1525. sensors_unregister(data->prox_sensor_device, prox_sensor_attrs);
  1526. sensors_register_prox_err:
  1527. #endif
  1528. sensors_unregister(data->light_sensor_device, light_sensor_attrs);
  1529. sensors_register_light_err:
  1530. #endif
  1531. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1532. sysfs_remove_group(&data->prox_input_dev->dev.kobj,
  1533. &gp2a_prox_attribute_group);
  1534. sysfs_create_group_prox_err:
  1535. #if defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  1536. sensors_remove_symlink(&data->prox_input_dev->dev.kobj,
  1537. data->prox_input_dev->name);
  1538. sensors_create_symlink_err:
  1539. #endif
  1540. input_unregister_device(data->prox_input_dev);
  1541. input_register_prox_device_err:
  1542. input_free_device(data->prox_input_dev);
  1543. input_allocate_prox_device_err:
  1544. gpio_free(data->gpio);
  1545. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1546. gpio_free(data->vled_gpio);
  1547. #endif
  1548. err_setup_irq:
  1549. #endif
  1550. sysfs_remove_group(&data->light_input_dev->dev.kobj,
  1551. &gp2a_light_attribute_group);
  1552. sysfs_create_group_light_err:
  1553. #if defined(CONFIG_SEC_BERLUTI_PROJECT) || defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE)
  1554. sensors_remove_symlink(&data->light_input_dev->dev.kobj,
  1555. data->light_input_dev->name);
  1556. sensors_create_symlink_light_err:
  1557. #endif
  1558. input_unregister_device(data->light_input_dev);
  1559. input_register_device_err:
  1560. input_free_device(data->light_input_dev);
  1561. input_allocate_light_device_err:
  1562. i2c_fail_err:
  1563. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1564. gp2a_parse_dt_err:
  1565. #endif
  1566. kfree(data);
  1567. gp2a_regulator_onoff(&client->dev, false);
  1568. bShutdown = true;
  1569. done:
  1570. return err;
  1571. }
  1572. static void gp2a_shutdown(struct i2c_client *client)
  1573. {
  1574. struct gp2a_data *data = i2c_get_clientdata(client);
  1575. pr_info("%s, is called\n", __func__);
  1576. bShutdown = true;
  1577. if (data->light_enabled) {
  1578. cancel_delayed_work_sync(&data->light_work);
  1579. lightsensor_onoff(0, data);
  1580. input_report_rel(data->light_input_dev, REL_MISC, data->lux + 1);
  1581. input_sync(data->light_input_dev);
  1582. }
  1583. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1584. if (data->prox_enabled) {
  1585. disable_irq(data->irq);
  1586. disable_irq_wake(data->irq);
  1587. gp2a_prox_onoff(0, data);
  1588. wake_unlock(&data->prx_wake_lock);
  1589. }
  1590. wake_lock_destroy(&data->prx_wake_lock);
  1591. gpio_free(data->gpio);
  1592. if (system_rev >= 12) {
  1593. gpio_free(data->con_gpio);
  1594. }
  1595. #if !defined(CONFIG_SEC_BERLUTI_PROJECT) && !defined(CONFIG_MACH_MS01_EUR_3G) && !defined(CONFIG_MACH_MS01_EUR_LTE)
  1596. gpio_free(data->vled_gpio);
  1597. #endif
  1598. #endif
  1599. mutex_destroy(&data->light_mutex);
  1600. mutex_destroy(&data->data_mutex);
  1601. gp2a_regulator_onoff(&client->dev, false);
  1602. }
  1603. static int gp2a_suspend(struct device *dev)
  1604. {
  1605. struct i2c_client *client = to_i2c_client(dev);
  1606. struct gp2a_data *data = i2c_get_clientdata(client);
  1607. int err=0;
  1608. if (data->light_enabled) {
  1609. cancel_delayed_work_sync(&data->light_work);
  1610. lightsensor_onoff(0, data);
  1611. if(data->prox_enabled == SENSOR_DISABLE)
  1612. {
  1613. err = gp2a_regulator_onoff(&client->dev, false);
  1614. if (err) {
  1615. pr_err("%s, Power Down Failed\n", __func__);
  1616. return err;
  1617. }
  1618. }
  1619. }
  1620. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1621. if (data->prox_enabled) {
  1622. disable_irq(data->irq);
  1623. /*enable_irq_wake(gp2a->irq); already wake irq enabled */
  1624. } else {
  1625. // Chip is Off and Power is also Off.
  1626. }
  1627. #endif
  1628. pr_info("%s, is called\n", __func__);
  1629. return 0;
  1630. }
  1631. static int gp2a_resume(struct device *dev)
  1632. {
  1633. struct i2c_client *client = to_i2c_client(dev);
  1634. struct gp2a_data *data = i2c_get_clientdata(client);
  1635. int err=0;
  1636. if (data->light_enabled) {
  1637. lightsensor_onoff(1, data);
  1638. schedule_delayed_work(&data->light_work,
  1639. msecs_to_jiffies(data->light_delay));
  1640. if(data->prox_enabled == SENSOR_DISABLE)
  1641. {
  1642. err = gp2a_regulator_onoff(&client->dev, true);
  1643. if (err) {
  1644. pr_err("%s, Power Up Failed\n", __func__);
  1645. return err;
  1646. }
  1647. }
  1648. }
  1649. #ifdef CONFIG_SENSORS_GP2A030A_PROX
  1650. if (data->prox_enabled) {
  1651. enable_irq(data->irq);
  1652. /*enable_irq_wake(gp2a->irq); already wake irq enabled */
  1653. } else {
  1654. }
  1655. #endif
  1656. pr_info("%s, is called\n", __func__);
  1657. return 0;
  1658. }
  1659. static const u16 normal_i2c[] = { I2C_CLIENT_END };
  1660. static struct of_device_id gp2a_match_table[] = {
  1661. { .compatible = "gp2a030a",},
  1662. {},
  1663. };
  1664. static const struct i2c_device_id gp2a_device_id[] = {
  1665. {"gp2a030a", 0},
  1666. {}
  1667. };
  1668. MODULE_DEVICE_TABLE(i2c, gp2a_device_id);
  1669. static const struct dev_pm_ops gp2a_pm_ops = {
  1670. .suspend = gp2a_suspend,
  1671. .resume = gp2a_resume,
  1672. };
  1673. static struct i2c_driver gp2a_driver = {
  1674. .driver = {
  1675. .name = "gp2a030a",
  1676. .owner = THIS_MODULE,
  1677. .pm = &gp2a_pm_ops,
  1678. .of_match_table = gp2a_match_table,
  1679. },
  1680. .probe = gp2a_probe,
  1681. .shutdown = gp2a_shutdown,
  1682. .id_table = gp2a_device_id,
  1683. .address_list = normal_i2c,
  1684. };
  1685. module_i2c_driver(gp2a_driver);
  1686. MODULE_AUTHOR("SAMSUNG");
  1687. MODULE_DESCRIPTION("Optical Sensor driver for GP2AP030A00F");
  1688. MODULE_LICENSE("GPL");