gp2a002.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (C) 2010 Samsung Electronics. All rights reserved.
  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. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/i2c.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/device.h>
  24. #include <linux/delay.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/leds.h>
  28. #include <linux/gpio.h>
  29. #include <linux/wakelock.h>
  30. #include <linux/slab.h>
  31. #include <linux/input.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/gp2a002.h>
  35. #include <linux/module.h>
  36. #include <linux/sensors_core.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_gpio.h>
  40. #define REGS_PROX 0x0 /* Read Only */
  41. #define REGS_GAIN 0x1 /* Write Only */
  42. #define REGS_HYS 0x2 /* Write Only */
  43. #define REGS_CYCLE 0x3 /* Write Only */
  44. #define REGS_OPMOD 0x4 /* Write Only */
  45. #define REGS_CON 0x6 /* Write Only */
  46. #if defined(CONFIG_MACH_AFYONLTE_TMO) || defined(CONFIG_MACH_AFYONLTE_MTR)
  47. #define PROX_NONDETECT 0x40
  48. #define PROX_DETECT 0x20
  49. #else
  50. #define PROX_NONDETECT 0x2F
  51. #define PROX_DETECT 0x0F
  52. #endif
  53. #define PROX_NONDETECT_MODE1 0x43
  54. #define PROX_DETECT_MODE1 0x28
  55. #define PROX_NONDETECT_MODE2 0x48
  56. #define PROX_DETECT_MODE2 0x42
  57. #define OFFSET_FILE_PATH "/efs/prox_cal"
  58. #define PROXIMITY 1
  59. #define CHIP_DEV_NAME "GP2AP002"
  60. #define CHIP_DEV_VENDOR "SHARP"
  61. struct gp2a_data {
  62. struct input_dev *input;
  63. struct device *dev;
  64. struct gp2a_platform_data *pdata;
  65. struct i2c_client *i2c_client;
  66. struct mutex power_lock;
  67. struct wake_lock prx_wake_lock;
  68. struct workqueue_struct *wq;
  69. struct work_struct work_prox;
  70. int irq;
  71. int power_state;
  72. char val_state;
  73. char cal_mode;
  74. u8 detect;
  75. u8 nondetect;
  76. };
  77. int gp2a_i2c_read(struct gp2a_data *gp2a, u8 reg, u8 *val)
  78. {
  79. int err = 0;
  80. unsigned char data[2] = {reg, 0};
  81. int retry = 10;
  82. struct i2c_msg msg[2] = {};
  83. struct i2c_client *client = gp2a->i2c_client;
  84. if ((client == NULL) || (!client->adapter))
  85. return -ENODEV;
  86. msg[0].addr = client->addr;
  87. msg[0].flags = 0;
  88. msg[0].len = 1;
  89. msg[0].buf = data;
  90. msg[1].addr = client->addr;
  91. msg[1].flags = 1;
  92. msg[1].len = 2;
  93. msg[1].buf = data;
  94. while (retry--) {
  95. data[0] = reg;
  96. err = i2c_transfer(client->adapter, msg, 2);
  97. if (err >= 0) {
  98. *val = data[1];
  99. return 0;
  100. }
  101. }
  102. pr_err("%s : i2c transfer error ret = %d\n", __func__, err);
  103. return err;
  104. }
  105. int gp2a_i2c_write(struct gp2a_data *gp2a, u8 reg, u8 val)
  106. {
  107. int err = -1;
  108. struct i2c_msg msg[1];
  109. unsigned char data[2];
  110. int retry = 10;
  111. struct i2c_client *client = gp2a->i2c_client;
  112. if ((client == NULL) || (!client->adapter))
  113. return -ENODEV;
  114. while (retry--) {
  115. data[0] = reg;
  116. data[1] = val;
  117. msg->addr = client->addr;
  118. msg->flags = 0;
  119. msg->len = 2;
  120. msg->buf = data;
  121. err = i2c_transfer(client->adapter, msg, 1);
  122. if (err >= 0)
  123. return 0;
  124. }
  125. pr_err("%s : i2c transfer error ret= %d\n", __func__, err);
  126. return err;
  127. }
  128. #ifdef CONFIG_SENSORS_POWER_EN
  129. static int gp2a_leda_onoff(struct gp2a_data *gp2a, int power)
  130. {
  131. int ret;
  132. if (power)
  133. ret = gpio_direction_output(gp2a->pdata->power_en, 1);
  134. else
  135. ret = gpio_direction_output(gp2a->pdata->power_en, 0);
  136. if (ret < 0)
  137. pr_err("%s, error for direction\n", __func__);
  138. return 0;
  139. }
  140. #endif
  141. static int gp2a_power_onoff(struct gp2a_data *gp2a, int power)
  142. {
  143. u8 value;
  144. pr_info("%s,status(%d)\n", __func__, power);
  145. if (power) {
  146. #ifdef CONFIG_SENSORS_POWER_EN
  147. gp2a_leda_onoff(gp2a, power);
  148. #endif
  149. value = 0x18;
  150. gp2a_i2c_write(gp2a, REGS_CON, value);
  151. value = 0x08;
  152. gp2a_i2c_write(gp2a, REGS_GAIN, value);
  153. value = gp2a->nondetect;
  154. gp2a_i2c_write(gp2a, REGS_HYS, value);
  155. value = 0x04;
  156. gp2a_i2c_write(gp2a, REGS_CYCLE, value);
  157. value = 0x03;
  158. gp2a_i2c_write(gp2a, REGS_OPMOD, value);
  159. enable_irq_wake(gp2a->irq);
  160. enable_irq(gp2a->irq);
  161. value = 0x00;
  162. gp2a_i2c_write(gp2a, REGS_CON, value);
  163. } else {
  164. #ifdef CONFIG_SENSORS_POWER_EN
  165. gp2a_leda_onoff(gp2a, power);
  166. #endif
  167. disable_irq_wake(gp2a->irq);
  168. disable_irq(gp2a->irq);
  169. value = 0x02;
  170. gp2a_i2c_write(gp2a, REGS_OPMOD, value);
  171. }
  172. return 0;
  173. }
  174. static ssize_t adc_read(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  178. return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->val_state);
  179. }
  180. static ssize_t state_read(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  184. return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->val_state);
  185. }
  186. static ssize_t name_read(struct device *dev,
  187. struct device_attribute *attr, char *buf)
  188. {
  189. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_NAME);
  190. }
  191. static ssize_t vendor_read(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_VENDOR);
  195. }
  196. static int gp2a_cal_mode_read_file(struct gp2a_data *gp2a)
  197. {
  198. int err = 0;
  199. mm_segment_t old_fs;
  200. struct file *cal_mode_filp = NULL;
  201. old_fs = get_fs();
  202. set_fs(KERNEL_DS);
  203. cal_mode_filp = filp_open(OFFSET_FILE_PATH, O_RDONLY, 0666);
  204. if (IS_ERR(cal_mode_filp)) {
  205. err = PTR_ERR(cal_mode_filp);
  206. if (err != -ENOENT)
  207. pr_err("%s,Can't open cal_mode file\n", __func__);
  208. set_fs(old_fs);
  209. return err;
  210. }
  211. err = cal_mode_filp->f_op->read(cal_mode_filp,
  212. (char *)&gp2a->cal_mode,
  213. sizeof(u8), &cal_mode_filp->f_pos);
  214. if (err != sizeof(u8)) {
  215. pr_err("%s,Can't read the cal_mode from file\n",
  216. __func__);
  217. filp_close(cal_mode_filp, current->files);
  218. set_fs(old_fs);
  219. return -EIO;
  220. }
  221. filp_close(cal_mode_filp, current->files);
  222. set_fs(old_fs);
  223. return err;
  224. }
  225. static int gp2a_cal_mode_save_file(char mode)
  226. {
  227. struct file *cal_mode_filp = NULL;
  228. int err = 0;
  229. mm_segment_t old_fs;
  230. old_fs = get_fs();
  231. set_fs(KERNEL_DS);
  232. cal_mode_filp = filp_open(OFFSET_FILE_PATH,
  233. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  234. if (IS_ERR(cal_mode_filp)) {
  235. pr_err("%s,Can't open cal_mode file\n",
  236. __func__);
  237. set_fs(old_fs);
  238. err = PTR_ERR(cal_mode_filp);
  239. pr_err("%s,err = %d\n",
  240. __func__, err);
  241. return err;
  242. }
  243. err = cal_mode_filp->f_op->write(cal_mode_filp,
  244. (char *)&mode, sizeof(u8), &cal_mode_filp->f_pos);
  245. if (err != sizeof(u8)) {
  246. pr_err("%s,Can't read the cal_mode from file\n", __func__);
  247. err = -EIO;
  248. }
  249. filp_close(cal_mode_filp, current->files);
  250. set_fs(old_fs);
  251. return err;
  252. }
  253. static ssize_t prox_cal_read(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  257. return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->cal_mode);
  258. }
  259. static ssize_t prox_cal_write(struct device *dev,
  260. struct device_attribute *attr, const char *buf, size_t size)
  261. {
  262. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  263. int err;
  264. if (sysfs_streq(buf, "1")) {
  265. gp2a->cal_mode = 1;
  266. gp2a->nondetect = PROX_NONDETECT_MODE1;
  267. gp2a->detect = PROX_DETECT_MODE1;
  268. } else if (sysfs_streq(buf, "2")) {
  269. gp2a->cal_mode = 2;
  270. gp2a->nondetect = PROX_NONDETECT_MODE2;
  271. gp2a->detect = PROX_DETECT_MODE2;
  272. } else if (sysfs_streq(buf, "0")) {
  273. gp2a->cal_mode = 0;
  274. gp2a->nondetect = PROX_NONDETECT;
  275. gp2a->detect = PROX_DETECT;
  276. } else {
  277. pr_err("%s,invalid value %d\n", __func__, *buf);
  278. return -EINVAL;
  279. }
  280. if (gp2a->power_state == 1) {
  281. gp2a_power_onoff(gp2a, 0);
  282. msleep(5);
  283. gp2a_power_onoff(gp2a, 1);
  284. }
  285. err = gp2a_cal_mode_save_file(gp2a->cal_mode);
  286. if (err < 0) {
  287. pr_err("%s,prox_cal_write() failed\n", __func__);
  288. return err;
  289. }
  290. return size;
  291. }
  292. static DEVICE_ATTR(adc, 0440, adc_read, NULL);
  293. static DEVICE_ATTR(state, 0440, state_read, NULL);
  294. static DEVICE_ATTR(name, 0440, name_read, NULL);
  295. static DEVICE_ATTR(vendor, 0440, vendor_read, NULL);
  296. static DEVICE_ATTR(prox_cal, 0664, prox_cal_read, prox_cal_write);
  297. static struct device_attribute *proxi_attrs[] = {
  298. &dev_attr_adc,
  299. &dev_attr_state,
  300. &dev_attr_name,
  301. &dev_attr_vendor,
  302. &dev_attr_prox_cal,
  303. NULL,
  304. };
  305. #ifdef CONFIG_SENSORS_POWERCONTROL
  306. static int gp2a_regulator_onoff(struct device *dev, bool onoff)
  307. {
  308. struct regulator *gp2a_vio;
  309. #ifdef CONFIG_SENSORS_GP2A_VDDCONTROL
  310. struct regulator *gp2a_vdd;
  311. struct regulator *gp2a_vdd2;
  312. int ret = -1;
  313. #endif
  314. pr_info("%s %s\n", __func__, (onoff) ? "on" : "off");
  315. #ifdef CONFIG_SENSORS_GP2A_VDDCONTROL
  316. gp2a_vdd = devm_regulator_get(dev, "gp2a-vdd");
  317. if (IS_ERR(gp2a_vdd)) {
  318. pr_err("[SENSOR]: %s - cannot get gp2a_vdd\n", __func__);
  319. return -ENOMEM;
  320. }
  321. ret = regulator_set_voltage(gp2a_vdd, 3000000, 3000000);
  322. if (ret) {
  323. pr_err("%s: set voltage failed on gp2a_vdd, rc=%d\n",
  324. __func__, ret);
  325. return ret;
  326. }
  327. gp2a_vdd2 = devm_regulator_get(dev, "gp2a-vdd2");
  328. if (IS_ERR(gp2a_vdd2)) {
  329. pr_err("[SENSOR]: %s - cannot get gp2a_vdd2\n", __func__);
  330. return -ENOMEM;
  331. }
  332. ret = regulator_set_voltage(gp2a_vdd2, 3000000, 3000000);
  333. if (ret) {
  334. pr_err("%s: set voltage failed on gp2a_vdd2, rc=%d\n",
  335. __func__, ret);
  336. return ret;
  337. }
  338. #endif
  339. gp2a_vio = devm_regulator_get(dev, "gp2a-vio");
  340. if (IS_ERR(gp2a_vio)) {
  341. pr_err("%s: cannot get gp2a-vio\n", __func__);
  342. return -ENOMEM;
  343. }
  344. if (onoff) {
  345. #ifdef CONFIG_SENSORS_GP2A_VDDCONTROL
  346. regulator_enable(gp2a_vdd);
  347. regulator_enable(gp2a_vdd2);
  348. #endif
  349. regulator_enable(gp2a_vio);
  350. } else {
  351. #ifdef CONFIG_SENSORS_GP2A_VDDCONTROL
  352. regulator_disable(gp2a_vdd);
  353. regulator_disable(gp2a_vdd2);
  354. #endif
  355. regulator_disable(gp2a_vio);
  356. }
  357. #ifdef CONFIG_SENSORS_GP2A_VDDCONTROL
  358. devm_regulator_put(gp2a_vdd);
  359. devm_regulator_put(gp2a_vdd2);
  360. #endif
  361. devm_regulator_put(gp2a_vio);
  362. msleep(10);
  363. return 0;
  364. }
  365. #endif
  366. static ssize_t proximity_enable_show(struct device *dev,
  367. struct device_attribute *attr, char *buf)
  368. {
  369. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  370. return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->power_state);
  371. }
  372. static ssize_t proximity_enable_store(struct device *dev,
  373. struct device_attribute *attr,
  374. const char *buf, size_t size)
  375. {
  376. struct gp2a_data *gp2a = dev_get_drvdata(dev);
  377. int value = 0;
  378. int err = 0;
  379. err = kstrtoint(buf, 10, &value);
  380. if (err) {
  381. pr_err("%s,kstrtoint failed.", __func__);
  382. goto done;
  383. }
  384. if (value != 0 && value != 1) {
  385. pr_err("%s,wrong value(%d)\n", __func__, value);
  386. goto done;
  387. }
  388. mutex_lock(&gp2a->power_lock);
  389. if (gp2a->power_state != value) {
  390. pr_info("%s,enable(%d)\n", __func__, value);
  391. if (value) {
  392. err = gp2a_cal_mode_read_file(gp2a);
  393. if (err < 0 && err != -ENOENT)
  394. pr_err("%s,cal_mode file read fail\n", __func__);
  395. pr_info("%s,mode(%d)\n", __func__, gp2a->cal_mode);
  396. if (gp2a->cal_mode == 2) {
  397. gp2a->nondetect = PROX_NONDETECT_MODE2;
  398. gp2a->detect = PROX_DETECT_MODE2;
  399. } else if (gp2a->cal_mode == 1) {
  400. gp2a->nondetect = PROX_NONDETECT_MODE1;
  401. gp2a->detect = PROX_DETECT_MODE1;
  402. } else {
  403. gp2a->nondetect = PROX_NONDETECT;
  404. gp2a->detect = PROX_DETECT;
  405. }
  406. #ifdef CONFIG_SENSORS_POWERCONTROL
  407. gp2a_regulator_onoff(&gp2a->i2c_client->dev, true);
  408. #endif
  409. gp2a_power_onoff(gp2a, 1);
  410. gp2a->power_state = value;
  411. gp2a->val_state = value;
  412. input_report_abs(gp2a->input, ABS_DISTANCE, gp2a->val_state);
  413. input_sync(gp2a->input);
  414. } else {
  415. #ifdef CONFIG_SENSORS_POWERCONTROL
  416. gp2a_regulator_onoff(&gp2a->i2c_client->dev, false);
  417. #endif
  418. gp2a_power_onoff(gp2a, 0);
  419. gp2a->power_state = value;
  420. }
  421. } else {
  422. pr_err("%s,wrong cmd for enable\n", __func__);
  423. }
  424. mutex_unlock(&gp2a->power_lock);
  425. done:
  426. return size;
  427. }
  428. static struct device_attribute dev_attr_proximity_enable =
  429. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  430. proximity_enable_show, proximity_enable_store);
  431. static struct attribute *proximity_sysfs_attrs[] = {
  432. &dev_attr_proximity_enable.attr,
  433. NULL
  434. };
  435. static struct attribute_group proximity_attribute_group = {
  436. .attrs = proximity_sysfs_attrs,
  437. };
  438. static void gp2a_prox_work_func(struct work_struct *work)
  439. {
  440. struct gp2a_data *gp2a = container_of(work,
  441. struct gp2a_data, work_prox);
  442. u8 vo, value;
  443. gp2a_i2c_read(gp2a, REGS_PROX, &vo);
  444. vo = 0x01 & vo;
  445. value = 0x18;
  446. gp2a_i2c_write(gp2a, REGS_CON, value);
  447. if (!vo) {
  448. gp2a->val_state = 0x01;
  449. value = gp2a->nondetect;
  450. } else {
  451. gp2a->val_state = 0x00;
  452. value = gp2a->detect;
  453. }
  454. gp2a_i2c_write(gp2a, REGS_HYS, value);
  455. pr_info("%s,%d\n", __func__, gp2a->val_state);
  456. input_report_abs(gp2a->input, ABS_DISTANCE, gp2a->val_state);
  457. input_sync(gp2a->input);
  458. msleep(5);
  459. value = 0x00;
  460. gp2a_i2c_write(gp2a, REGS_CON, value);
  461. }
  462. irqreturn_t gp2a_irq_handler(int irq, void *data)
  463. {
  464. struct gp2a_data *gp2a = data;
  465. pr_info("%s,%d\n", __func__, irq);
  466. schedule_work((struct work_struct *)&gp2a->work_prox);
  467. wake_lock_timeout(&gp2a->prx_wake_lock, 3*HZ);
  468. return IRQ_HANDLED;
  469. }
  470. static int gp2a_setup_irq(struct gp2a_data *gp2a)
  471. {
  472. int rc;
  473. struct gp2a_platform_data *pdata = gp2a->pdata;
  474. int irq = -1;
  475. u8 value;
  476. rc = gpio_request(pdata->p_out, "gpio_proximity_out");
  477. if (rc < 0) {
  478. pr_err("%s,gpio %d request failed (%d)\n",
  479. __func__, pdata->p_out, rc);
  480. return rc;
  481. }
  482. rc = gpio_direction_input(pdata->p_out);
  483. if (rc < 0) {
  484. pr_err("%s,failed gpio %d as input (%d)\n",
  485. __func__, pdata->p_out, rc);
  486. goto err_gpio_direction_input;
  487. }
  488. value = 0x18;
  489. gp2a_i2c_write(gp2a, REGS_CON, value);
  490. irq = gpio_to_irq(pdata->p_out);
  491. rc = request_irq(irq, gp2a_irq_handler, IRQF_TRIGGER_FALLING,
  492. "proximity_int", gp2a);
  493. if (rc < 0) {
  494. pr_err("%s,request_irq(%d) failed for gpio %d (%d)\n",
  495. __func__, irq,
  496. pdata->p_out, rc);
  497. goto err_request_irq;
  498. } else{
  499. pr_info("%s,request_irq(%d) success for gpio %d\n",
  500. __func__, irq, pdata->p_out);
  501. }
  502. disable_irq(irq);
  503. gp2a->irq = irq;
  504. value = 0x02;
  505. gp2a_i2c_write(gp2a, REGS_OPMOD, value);
  506. goto done;
  507. err_request_irq:
  508. err_gpio_direction_input:
  509. gpio_free(pdata->p_out);
  510. done:
  511. return rc;
  512. }
  513. #ifdef CONFIG_SENSORS_POWER_EN
  514. static int gp2a_request_gpio(struct gp2a_platform_data *pdata)
  515. {
  516. int ret;
  517. ret = gpio_request(pdata->power_en, "prox_en");
  518. if(ret) {
  519. pr_err("%s: gpio request fail\n",__func__);
  520. return ret;
  521. }
  522. ret = gpio_direction_output(pdata->power_en, 0);
  523. if (ret) {
  524. pr_err("%s: unable to set_direction [%d]\n",__func__, pdata->power_en);
  525. return ret;
  526. }
  527. return 0;
  528. }
  529. #endif
  530. static int gp2a_parse_dt(struct device *dev, struct gp2a_platform_data *pdata)
  531. {
  532. struct device_node *np = dev->of_node;
  533. enum of_gpio_flags flags;
  534. pdata->p_out = of_get_named_gpio_flags(np, "gp2a-i2c,irq-gpio",
  535. 0, &flags);
  536. if (pdata->p_out < 0) {
  537. pr_err("%s : get irq_gpio(%d) error\n", __func__, pdata->p_out);
  538. return -ENODEV;
  539. }
  540. #ifdef CONFIG_SENSORS_POWER_EN
  541. pdata->power_en = of_get_named_gpio_flags(np, "gp2a-i2c,en-gpio",
  542. 0, &flags);
  543. if (pdata->power_en < 0) {
  544. pr_err("%s : get power_en(%d) error\n", __func__, pdata->power_en);
  545. return -ENODEV;
  546. }
  547. #endif
  548. return 0;
  549. }
  550. static int gp2a_i2c_probe(struct i2c_client *client,
  551. const struct i2c_device_id *id)
  552. {
  553. int ret = 0;
  554. struct input_dev *input_dev;
  555. struct gp2a_data *gp2a;
  556. struct gp2a_platform_data *pdata = client->dev.platform_data;
  557. pr_info("%s, start\n", __func__);
  558. #ifdef CONFIG_SENSORS_POWERCONTROL
  559. ret = gp2a_regulator_onoff(&client->dev, true);
  560. if (ret) {
  561. pr_err("%s, Power Up Failed\n", __func__);
  562. return ret;
  563. }
  564. #endif
  565. if(client->dev.of_node) {
  566. pdata = devm_kzalloc(&client->dev,
  567. sizeof(struct gp2a_platform_data), GFP_KERNEL);
  568. if (!pdata) {
  569. pr_err("%s,Failed to allocate memory\n", __func__);
  570. return -ENOMEM;
  571. }
  572. ret = gp2a_parse_dt(&client->dev, pdata);
  573. if (ret < 0)
  574. return ret;
  575. #ifdef CONFIG_SENSORS_POWER_EN
  576. ret = gp2a_request_gpio(pdata);
  577. if (ret < 0)
  578. return ret;
  579. #endif
  580. }
  581. if (!pdata) {
  582. pr_err("%s,missing pdata\n", __func__);
  583. return -ENOMEM;
  584. }
  585. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  586. pr_err("%s,i2c functionality failed\n", __func__);
  587. return -ENOMEM;
  588. }
  589. gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
  590. if (!gp2a) {
  591. pr_err("%s,failed memory alloc\n",
  592. __func__);
  593. return -ENOMEM;
  594. }
  595. gp2a->pdata = pdata;
  596. gp2a->i2c_client = client;
  597. i2c_set_clientdata(client, gp2a);
  598. wake_lock_init(&gp2a->prx_wake_lock, WAKE_LOCK_SUSPEND,
  599. "prx_wake_lock");
  600. mutex_init(&gp2a->power_lock);
  601. input_dev = input_allocate_device();
  602. if (!input_dev) {
  603. pr_err("%s,could not allocate input device\n", __func__);
  604. goto err_input_allocate_device_proximity;
  605. }
  606. gp2a->input = input_dev;
  607. input_dev->name = "proximity_sensor";
  608. input_set_capability(input_dev, EV_ABS, ABS_DISTANCE);
  609. input_set_abs_params(input_dev, ABS_DISTANCE, 0, 1, 0, 0);
  610. input_set_drvdata(input_dev, gp2a);
  611. ret = input_register_device(input_dev);
  612. if (ret < 0) {
  613. pr_err("%s,could not register input device\n",
  614. __func__);
  615. goto err_input_register_device_proximity;
  616. }
  617. ret = sensors_create_symlink(&input_dev->dev.kobj, input_dev->name);
  618. if (ret < 0) {
  619. input_unregister_device(input_dev);
  620. return ret;
  621. }
  622. ret = sysfs_create_group(&input_dev->dev.kobj,
  623. &proximity_attribute_group);
  624. if (ret) {
  625. pr_err("%s,create sysfs group error\n", __func__);
  626. goto err_sysfs_create_group_proximity;
  627. }
  628. INIT_WORK(&gp2a->work_prox, gp2a_prox_work_func);
  629. #ifdef CONFIG_SENSORS_POWER_EN
  630. gp2a_leda_onoff(gp2a, 1);
  631. #endif
  632. ret = gp2a_setup_irq(gp2a);
  633. if (ret) {
  634. pr_err("%s,could not setup irq\n", __func__);
  635. goto err_setup_irq;
  636. }
  637. #ifdef CONFIG_SENSORS_POWER_EN
  638. gp2a_leda_onoff(gp2a, 0);
  639. #endif
  640. ret = sensors_register(gp2a->dev, gp2a,
  641. proxi_attrs, "proximity_sensor");
  642. if (ret < 0) {
  643. pr_info("%s,could not sensors_register\n", __func__);
  644. goto exit_gp2a_sensors_register;
  645. }
  646. pr_info("%s,%d\n", __func__, __LINE__);
  647. goto done;
  648. exit_gp2a_sensors_register:
  649. free_irq(gp2a->irq, gp2a);
  650. gpio_free(gp2a->pdata->p_out);
  651. err_setup_irq:
  652. #ifdef CONFIG_SENSORS_POWER_EN
  653. gp2a_leda_onoff(gp2a, 0);
  654. #endif
  655. sysfs_remove_group(&gp2a->input->dev.kobj,
  656. &proximity_attribute_group);
  657. err_sysfs_create_group_proximity:
  658. input_unregister_device(gp2a->input);
  659. err_input_register_device_proximity:
  660. input_free_device(input_dev);
  661. err_input_allocate_device_proximity:
  662. mutex_destroy(&gp2a->power_lock);
  663. wake_lock_destroy(&gp2a->prx_wake_lock);
  664. kfree(gp2a);
  665. done:
  666. #ifdef CONFIG_SENSORS_POWERCONTROL
  667. gp2a_regulator_onoff(&client->dev, false);
  668. #endif
  669. return ret;
  670. }
  671. static void gp2a_i2c_shutdown(struct i2c_client *client)
  672. {
  673. struct gp2a_data *gp2a = i2c_get_clientdata(client);
  674. if (gp2a != NULL) {
  675. if (gp2a->power_state) {
  676. disable_irq_wake(gp2a->irq);
  677. disable_irq(gp2a->irq);
  678. msleep(5);
  679. }
  680. sysfs_remove_group(&gp2a->input->dev.kobj,
  681. &proximity_attribute_group);
  682. input_unregister_device(gp2a->input);
  683. free_irq(gp2a->irq, gp2a);
  684. gpio_free(gp2a->pdata->p_out);
  685. mutex_destroy(&gp2a->power_lock);
  686. wake_lock_destroy(&gp2a->prx_wake_lock);
  687. kfree(gp2a);
  688. }
  689. }
  690. static const struct i2c_device_id gp2a_device_id[] = {
  691. {"gp2a", 0},
  692. {}
  693. };
  694. MODULE_DEVICE_TABLE(i2c, gp2a_device_id);
  695. static struct of_device_id gp2a_i2c_match_table[] = {
  696. { .compatible = "gp2a-i2c",},
  697. {},
  698. };
  699. MODULE_DEVICE_TABLE(of, gp2a_i2c_match_table);
  700. static struct i2c_driver gp2a_i2c_driver = {
  701. .driver = {
  702. .name = "gp2a",
  703. .owner = THIS_MODULE,
  704. .of_match_table = gp2a_i2c_match_table,
  705. },
  706. .probe = gp2a_i2c_probe,
  707. .shutdown = gp2a_i2c_shutdown,
  708. .id_table = gp2a_device_id,
  709. };
  710. module_i2c_driver(gp2a_i2c_driver);
  711. MODULE_AUTHOR("mjchen@sta.samsung.com");
  712. MODULE_DESCRIPTION("Optical Sensor driver for gp2ap002");
  713. MODULE_LICENSE("GPL");