drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / sensors / gp2a002.c
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
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/workqueue.h>
33 #include <linux/uaccess.h>
34 #include <linux/module.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/of_device.h>
37 #include <linux/of_gpio.h>
38 #include <linux/input.h>
39 #include <linux/iio/iio.h>
40 #include <linux/iio/sysfs.h>
41 #include <linux/iio/events.h>
42 #include <linux/iio/buffer.h>
43
44 #include "sensors_core.h"
45 #include <linux/sensor/gp2a002.h>
46
47 #define REGS_PROX               0x0 /* Read  Only */
48 #define REGS_GAIN               0x1 /* Write Only */
49 #define REGS_HYS                0x2 /* Write Only */
50 #define REGS_CYCLE              0x3 /* Write Only */
51 #define REGS_OPMOD              0x4 /* Write Only */
52 #define REGS_CON                0x6 /* Write Only */
53
54 #if defined(CONFIG_MACH_COREPRIMEVE3G)
55 #define PROX_NONDETECT          0x2F
56 #define PROX_DETECT             0x0D
57 #else
58 #define PROX_NONDETECT          0x2F
59 #define PROX_DETECT             0x0F
60 #endif
61 #define PROX_NONDETECT_MODE1    0x43
62 #define PROX_DETECT_MODE1       0x28
63 #define PROX_NONDETECT_MODE2    0x48
64 #define PROX_DETECT_MODE2       0x42
65 #define OFFSET_FILE_PATH        "/efs/FactoryApp/prox_cal"
66
67 #define CHIP_DEV_NAME           "GP2AP002"
68 #define CHIP_DEV_VENDOR         "SHARP"
69
70 struct gp2a_data {
71 #if defined(CONFIG_SENSORS_IIO)
72         struct iio_dev *indio_dev;
73 #else
74         struct input_dev *input;
75 #endif
76         struct device *dev;
77         struct gp2a_platform_data *pdata;
78         struct i2c_client *i2c_client;
79         struct mutex power_lock;
80         struct wake_lock prx_wake_lock;
81         struct workqueue_struct *wq;
82         struct work_struct work_prox;
83
84         int irq;
85         int power_state;
86         char val_state;
87         char cal_mode;
88
89         u8 detect;
90         u8 nondetect;
91 #if defined(CONFIG_SENSORS_IIO)
92         u64 timestamp;
93 #endif
94 };
95
96 enum {
97         OFF = 0,
98         ON,
99 };
100
101 #if defined(CONFIG_SENSORS_IIO)
102 #define IIO_BUFFER_1_BYTES      9 /* data 1 bytes + timestamp 8 bytes */
103
104 static const struct iio_chan_spec gp2a_channels[] = {
105         {
106                 .type = IIO_PROXIMITY,
107                 .channel = -1,
108                 .scan_index = 0,
109                 .scan_type = IIO_ST('s', IIO_BUFFER_1_BYTES * 8,
110                         IIO_BUFFER_1_BYTES * 8, 0)
111         }
112 };
113 #endif
114
115 int gp2a_i2c_read(struct gp2a_data *gp2a, u8 reg, u8 *val)
116 {
117         int err = 0;
118         unsigned char data[2] = {reg, 0};
119         int retry = 10;
120         struct i2c_msg msg[2] = {};
121         struct i2c_client *client = gp2a->i2c_client;
122
123         if ((client == NULL) || (!client->adapter))
124                 return -ENODEV;
125
126         msg[0].addr = client->addr;
127         msg[0].flags = 0;
128         msg[0].len = 1;
129         msg[0].buf = data;
130
131         msg[1].addr = client->addr;
132         msg[1].flags = 1;
133         msg[1].len = 2;
134         msg[1].buf = data;
135
136         while (retry--) {
137                 data[0] = reg;
138
139                 err = i2c_transfer(client->adapter, msg, 2);
140
141                 if (err >= 0) {
142                         *val = data[1];
143                         return 0;
144                 }
145         }
146
147         pr_err("[SENSOR] %s, i2c transfer error ret = %d\n", __func__, err);
148
149         return err;
150 }
151
152 int gp2a_i2c_write(struct gp2a_data *gp2a, u8 reg, u8 val)
153 {
154         int err = -1;
155         struct i2c_msg msg[1];
156         unsigned char data[2];
157         int retry = 10;
158         struct i2c_client *client = gp2a->i2c_client;
159
160         if ((client == NULL) || (!client->adapter))
161                 return -ENODEV;
162
163         while (retry--) {
164                 data[0] = reg;
165                 data[1] = val;
166
167                 msg->addr = client->addr;
168                 msg->flags = 0;
169                 msg->len = 2;
170                 msg->buf = data;
171
172                 err = i2c_transfer(client->adapter, msg, 1);
173
174                 if (err >= 0)
175                         return 0;
176         }
177
178         pr_err("[SENSOR] %s, i2c transfer error ret= %d\n", __func__, err);
179
180         return err;
181 }
182
183 static int gp2a_leda_onoff(struct gp2a_data *gp2a, int power)
184 {
185         int ret;
186
187 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
188         if (power)
189                 ret = gpio_direction_output(gp2a->pdata->power_en, 1);
190         else
191                 ret = gpio_direction_output(gp2a->pdata->power_en, 0);
192
193         if (ret < 0)
194                 pr_err("[SENSOR] %s, error for direction\n", __func__);
195
196 #else
197
198         struct regulator *gp2a_leda;
199
200         pr_info("[SENSOR] %s %s\n", __func__, (power) ? "on" : "off");
201
202         gp2a_leda = devm_regulator_get(&gp2a->i2c_client->dev, "gp2a-leda");
203         if (IS_ERR(gp2a_leda)) {
204                 pr_err("[SENSOR]: %s - cannot get gp2a_leda\n", __func__);
205                 return -ENOMEM;
206         }
207
208         if (power) {
209                 ret = regulator_enable(gp2a_leda);
210                 if (ret) {
211                         pr_err("[SENSOR] %s: enable gp2a_leda failed (%d)\n",
212                                 __func__, ret);
213                         return ret;
214                 }
215         } else {
216                 ret = regulator_disable(gp2a_leda);
217                 if (ret) {
218                         pr_err("[SENSOR] %s: disable gp2a_leda failed (%d)\n",
219                                 __func__, ret);
220                         return ret;
221                 }
222         }
223
224         devm_regulator_put(gp2a_leda);
225         msleep(20);
226 #endif
227
228         return 0;
229 }
230 static int gp2a_power_onoff(struct gp2a_data *gp2a, int power)
231 {
232         u8 value;
233         pr_info("[SENOSR] %s, status(%d)\n", __func__, power);
234
235         if (power) {
236                 gp2a_leda_onoff(gp2a, power);
237                 value = 0x18;
238                 gp2a_i2c_write(gp2a, REGS_CON, value);
239                 value = 0x08;
240                 gp2a_i2c_write(gp2a, REGS_GAIN, value);
241                 value = gp2a->nondetect;
242                 gp2a_i2c_write(gp2a, REGS_HYS, value);
243                 value = 0x04;
244                 gp2a_i2c_write(gp2a, REGS_CYCLE, value);
245                 value = 0x03;
246                 gp2a_i2c_write(gp2a, REGS_OPMOD, value);
247
248                 enable_irq_wake(gp2a->irq);
249                 enable_irq(gp2a->irq);
250
251                 value = 0x00;
252                 gp2a_i2c_write(gp2a, REGS_CON, value);
253         } else {
254                 disable_irq_wake(gp2a->irq);
255                 disable_irq(gp2a->irq);
256
257                 value = 0x02;
258                 gp2a_i2c_write(gp2a, REGS_OPMOD, value);
259                 gp2a_leda_onoff(gp2a, power);
260         }
261         return 0;
262 }
263
264 static ssize_t adc_read(struct device *dev,
265         struct device_attribute *attr, char *buf)
266 {
267         struct gp2a_data *gp2a = dev_get_drvdata(dev);
268
269         return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->val_state);
270 }
271
272 static ssize_t state_read(struct device *dev,
273         struct device_attribute *attr, char *buf)
274 {
275         struct gp2a_data *gp2a = dev_get_drvdata(dev);
276
277         return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->val_state);
278 }
279
280 static ssize_t name_read(struct device *dev,
281         struct device_attribute *attr, char *buf)
282 {
283         return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_NAME);
284 }
285
286 static ssize_t vendor_read(struct device *dev,
287         struct device_attribute *attr, char *buf)
288 {
289         return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_VENDOR);
290 }
291
292 static int gp2a_cal_mode_read_file(struct gp2a_data *gp2a)
293 {
294         int err;
295         mm_segment_t old_fs;
296         struct file *cal_mode_filp = NULL;
297
298         old_fs = get_fs();
299         set_fs(KERNEL_DS);
300
301         cal_mode_filp = filp_open(OFFSET_FILE_PATH, O_RDONLY, 0666);
302         if (IS_ERR(cal_mode_filp)) {
303                 err = PTR_ERR(cal_mode_filp);
304                 if (err != -ENOENT)
305                         pr_err("[SENOSR] %s, Can't open cal_mode file\n",
306                                 __func__);
307                 set_fs(old_fs);
308                 return err;
309         }
310
311         err = cal_mode_filp->f_op->read(cal_mode_filp,
312                 (char *)&gp2a->cal_mode,
313                 sizeof(u8), &cal_mode_filp->f_pos);
314         if (err != sizeof(u8)) {
315                 pr_err("[SENOSR] %s, Can't read the cal_mode from file\n",
316                         __func__);
317                 filp_close(cal_mode_filp, current->files);
318                 set_fs(old_fs);
319                 return -EIO;
320         }
321
322         filp_close(cal_mode_filp, current->files);
323         set_fs(old_fs);
324
325         return err;
326 }
327
328 static int gp2a_cal_mode_save_file(char mode)
329 {
330         struct file *cal_mode_filp = NULL;
331         int err = 0;
332         mm_segment_t old_fs;
333
334         old_fs = get_fs();
335         set_fs(KERNEL_DS);
336
337         cal_mode_filp = filp_open(OFFSET_FILE_PATH,
338                 O_CREAT | O_TRUNC | O_WRONLY, 0666);
339         if (IS_ERR(cal_mode_filp)) {
340                 pr_err("[SENOSR] %s, Can't open cal_mode file\n",
341                         __func__);
342                 set_fs(old_fs);
343                 err = PTR_ERR(cal_mode_filp);
344                 pr_err("[SENOSR] %s, err = %d\n", __func__, err);
345                 return err;
346         }
347
348         err = cal_mode_filp->f_op->write(cal_mode_filp,
349                 (char *)&mode, sizeof(u8), &cal_mode_filp->f_pos);
350         if (err != sizeof(u8)) {
351                 pr_err("[SENOSR] %s, Can't read the cal_mode from file\n",
352                         __func__);
353                 err = -EIO;
354         }
355
356         filp_close(cal_mode_filp, current->files);
357         set_fs(old_fs);
358
359         return err;
360 }
361
362 static ssize_t prox_cal_read(struct device *dev,
363         struct device_attribute *attr, char *buf)
364 {
365         struct gp2a_data *gp2a = dev_get_drvdata(dev);
366
367         return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->cal_mode);
368 }
369
370 static ssize_t prox_cal_write(struct device *dev,
371         struct device_attribute *attr, const char *buf, size_t size)
372 {
373         struct gp2a_data *gp2a = dev_get_drvdata(dev);
374         int err;
375
376         if (sysfs_streq(buf, "1")) {
377                 gp2a->cal_mode = 1;
378                 gp2a->nondetect = PROX_NONDETECT_MODE1;
379                 gp2a->detect = PROX_DETECT_MODE1;
380         } else if (sysfs_streq(buf, "2")) {
381                 gp2a->cal_mode = 2;
382                 gp2a->nondetect = PROX_NONDETECT_MODE2;
383                 gp2a->detect = PROX_DETECT_MODE2;
384         } else if (sysfs_streq(buf, "0")) {
385                 gp2a->cal_mode = 0;
386                 gp2a->nondetect = PROX_NONDETECT;
387                 gp2a->detect = PROX_DETECT;
388         } else {
389                 pr_err("[SENOSR] %s, invalid value %d\n", __func__, *buf);
390                 return -EINVAL;
391         }
392
393         if (gp2a->power_state == 1) {
394                 gp2a_power_onoff(gp2a, OFF);
395                 msleep(20);
396                 gp2a_power_onoff(gp2a, ON);
397         }
398
399         err = gp2a_cal_mode_save_file(gp2a->cal_mode);
400         if (err < 0) {
401                 pr_err("[SENOSR] %s, prox_cal_write() failed\n", __func__);
402                 return err;
403         }
404
405         return size;
406 }
407
408 static DEVICE_ATTR(adc, 0440, adc_read, NULL);
409 static DEVICE_ATTR(state, 0440, state_read, NULL);
410 static DEVICE_ATTR(name, 0440, name_read, NULL);
411 static DEVICE_ATTR(vendor, 0440, vendor_read, NULL);
412 static DEVICE_ATTR(prox_cal, 0664, prox_cal_read, prox_cal_write);
413
414 static struct device_attribute *proxi_attrs[] = {
415         &dev_attr_adc,
416         &dev_attr_state,
417         &dev_attr_name,
418         &dev_attr_vendor,
419         &dev_attr_prox_cal,
420         NULL,
421 };
422
423 #if defined(CONFIG_SENSORS_GP2A_HAS_REGULATOR)
424 static int gp2a_regulator_onoff(struct device *dev, bool onoff)
425 {
426         struct regulator *gp2a_vdd;
427         int ret;
428
429         pr_info("[SENOSR] %s %s\n", __func__, (onoff) ? "on" : "off");
430
431         gp2a_vdd = devm_regulator_get(dev, "gp2a-vdd");
432         if (IS_ERR(gp2a_vdd)) {
433                 pr_err("[SENSOR]: %s - cannot get gp2a_vdd\n", __func__);
434                 return -ENOMEM;
435         }
436
437         regulator_set_voltage(gp2a_vdd, 2850000, 2850000);
438
439         if (onoff) {
440                 ret = regulator_enable(gp2a_vdd);
441                 if (ret) {
442                         pr_err("[SENOSR] %s: enable vdd failed (%d)\n",
443                                 __func__, ret);
444                         return ret;
445                 }
446         } else {
447                 ret = regulator_disable(gp2a_vdd);
448                 if (ret) {
449                         pr_err("[SENOSR] %s: disable vdd failed (%d)\n",
450                                 __func__, ret);
451                         return ret;
452                 }
453         }
454
455         devm_regulator_put(gp2a_vdd);
456         msleep(20);
457
458         return 0;
459 }
460 #endif
461
462 static ssize_t proximity_enable_show(struct device *dev,
463         struct device_attribute *attr, char *buf)
464 {
465 #if defined(CONFIG_SENSORS_IIO)
466         struct gp2a_data *gp2a = iio_priv(dev_get_drvdata(dev));
467 #else
468         struct gp2a_data *gp2a = dev_get_drvdata(dev);
469 #endif
470
471         return snprintf(buf, PAGE_SIZE, "%d\n", gp2a->power_state);
472 }
473
474 static ssize_t proximity_enable_store(struct device *dev,
475         struct device_attribute *attr, const char *buf, size_t size)
476 {
477 #if defined(CONFIG_SENSORS_IIO)
478         struct gp2a_data *gp2a = iio_priv(dev_get_drvdata(dev));
479 #else
480         struct gp2a_data *gp2a = dev_get_drvdata(dev);
481 #endif
482         int value = 0;
483         int err = 0;
484
485         err = kstrtoint(buf, 10, &value);
486         if (err) {
487                 pr_err("[SENOSR] %s, kstrtoint failed.", __func__);
488                 return -EINVAL;
489         }
490         if (value != 0 && value != 1) {
491                 pr_err("[SENOSR] %s, wrong value(%d)\n", __func__, value);
492                 return -EINVAL;
493         }
494
495         mutex_lock(&gp2a->power_lock);
496
497         if (gp2a->power_state != value) {
498                 pr_info("[SENOSR] %s, enable(%d)\n", __func__, value);
499                 if (value) {
500                         err = gp2a_cal_mode_read_file(gp2a);
501                         if (err < 0 && err != -ENOENT)
502                                 pr_err("[SENOSR] %s, cal_mode read fail\n",
503                                         __func__);
504
505                         pr_info("[SENOSR] %s, mode(%d)\n", __func__,
506                                 gp2a->cal_mode);
507                         if (gp2a->cal_mode == 2) {
508                                 gp2a->nondetect = PROX_NONDETECT_MODE2;
509                                 gp2a->detect = PROX_DETECT_MODE2;
510                         } else if (gp2a->cal_mode == 1) {
511                                 gp2a->nondetect = PROX_NONDETECT_MODE1;
512                                 gp2a->detect = PROX_DETECT_MODE1;
513                         } else {
514                                 gp2a->nondetect = PROX_NONDETECT;
515                                 gp2a->detect = PROX_DETECT;
516                         }
517
518 #if defined(CONFIG_SENSORS_GP2A_HAS_REGULATOR)
519                         gp2a_regulator_onoff(&gp2a->i2c_client->dev, ON);
520 #endif
521                         gp2a_power_onoff(gp2a, ON);
522                         gp2a->power_state = value;
523
524                         gp2a->val_state = value;
525 #if !defined(CONFIG_SENSORS_IIO)
526                         input_report_abs(gp2a->input, ABS_DISTANCE,
527                                 gp2a->val_state);
528                         input_sync(gp2a->input);
529 #endif
530                 } else {
531                         gp2a_power_onoff(gp2a, OFF);
532 #if defined(CONFIG_SENSORS_GP2A_HAS_REGULATOR)
533                         gp2a_regulator_onoff(&gp2a->i2c_client->dev, OFF);
534 #endif
535                         gp2a->power_state = value;
536                 }
537         } else {
538                 pr_err("[SENOSR] %s, wrong cmd for enable\n", __func__);
539         }
540
541         mutex_unlock(&gp2a->power_lock);
542         return size;
543 }
544
545 #if defined(CONFIG_SENSORS_IIO)
546 static IIO_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
547         proximity_enable_show, proximity_enable_store, 0);
548
549 static struct attribute *proximity_sysfs_attrs[] = {
550         &iio_dev_attr_enable.dev_attr.attr,
551         NULL
552 };
553 #else
554
555 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
556         proximity_enable_show, proximity_enable_store);
557
558 static struct attribute *proximity_sysfs_attrs[] = {
559         &dev_attr_enable.attr,
560         NULL
561 };
562 #endif
563
564 static struct attribute_group proximity_attribute_group = {
565         .attrs = proximity_sysfs_attrs,
566 };
567
568 #if defined(CONFIG_SENSORS_IIO)
569 static int gp2a_read_raw(struct iio_dev *indio_dev,
570         struct iio_chan_spec const *chan,
571         int *val, int *val2, long mask)
572 {
573         struct gp2a_data *data = iio_priv(indio_dev);
574         int ret = -EINVAL;
575
576         switch (chan->type) {
577         case IIO_PROXIMITY:
578                 *val = data->val_state;
579                 break;
580         default:
581                 pr_err("[SENSOR] %s, invalied channel\n", __func__);
582                 return ret;
583         }
584
585         return IIO_VAL_INT;
586 }
587
588 static const struct iio_info gp2a_info = {
589         .attrs = &proximity_attribute_group,
590         .driver_module = THIS_MODULE,
591         .read_raw = gp2a_read_raw,
592 };
593 #endif
594
595 static void gp2a_prox_work_func(struct work_struct *work)
596 {
597         struct gp2a_data *gp2a = container_of(work,
598                 struct gp2a_data, work_prox);
599 #if defined(CONFIG_SENSORS_IIO)
600         struct iio_dev *indio_dev = iio_priv_to_dev(gp2a);
601         struct timespec ts;
602         u8 buf[IIO_BUFFER_1_BYTES];
603 #endif
604         u8 vo, value;
605
606         gp2a_i2c_read(gp2a, REGS_PROX, &vo);
607         vo = 0x01 & vo;
608
609         value = 0x18;
610         gp2a_i2c_write(gp2a, REGS_CON, value);
611
612         if (!vo) {
613                 gp2a->val_state = 0x01;
614                 value = gp2a->nondetect;
615         } else {
616                 gp2a->val_state = 0x00;
617                 value = gp2a->detect;
618         }
619         gp2a_i2c_write(gp2a, REGS_HYS, value);
620
621         pr_info("[SENSOR] %s, %d\n", __func__, gp2a->val_state);
622
623 #if defined(CONFIG_SENSORS_IIO)
624         ts = ktime_to_timespec(ktime_get_boottime());
625         gp2a->timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
626
627         memcpy(buf, &gp2a->val_state, sizeof(gp2a->val_state));
628         memcpy(buf + 1, &gp2a->timestamp, sizeof(gp2a->timestamp));
629
630         iio_push_to_buffers(indio_dev, buf);
631 #else
632         input_report_abs(gp2a->input, ABS_DISTANCE, gp2a->val_state);
633         input_sync(gp2a->input);
634 #endif
635         msleep(20);
636
637         value = 0x00;
638         gp2a_i2c_write(gp2a, REGS_CON, value);
639 }
640
641 irqreturn_t gp2a_irq_handler(int irq, void *data)
642 {
643         struct gp2a_data *gp2a = data;
644         pr_info("[SENSRO] %s, %d\n", __func__, irq);
645
646         schedule_work((struct work_struct *)&gp2a->work_prox);
647         wake_lock_timeout(&gp2a->prx_wake_lock, 3*HZ);
648
649         return IRQ_HANDLED;
650 }
651
652 static int gp2a_setup_irq(struct gp2a_data *gp2a)
653 {
654         struct gp2a_platform_data *pdata = gp2a->pdata;
655         int ret;
656         u8 value;
657
658         ret = gpio_request(pdata->p_out, "gpio_proximity_out");
659         if (ret < 0) {
660                 pr_err("[SENSOR] %s, gpio %d request failed (%d)\n",
661                         __func__, pdata->p_out, ret);
662                 return ret;
663         }
664
665         ret = gpio_direction_input(pdata->p_out);
666         if (ret < 0) {
667                 pr_err("[SENSOR] %s, failed gpio %d as input (%d)\n",
668                         __func__, pdata->p_out, ret);
669                 goto err_gpio_direction_input;
670         }
671
672         value = 0x18;
673         gp2a_i2c_write(gp2a, REGS_CON, value);
674
675         gp2a->irq = gpio_to_irq(pdata->p_out);
676         ret = request_irq(gp2a->irq, gp2a_irq_handler,
677                 IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
678                 "proximity_int", gp2a);
679         if (ret < 0) {
680                 pr_err("[SENSOR] %s, request_irq(%d) failed for gpio %d (%d)\n",
681                         __func__, gp2a->irq, pdata->p_out, ret);
682                 goto err_request_irq;
683         }
684
685         pr_info("[SENSOR] %s, request_irq(%d) success for gpio %d\n",
686                 __func__, gp2a->irq, pdata->p_out);
687
688         disable_irq(gp2a->irq);
689
690         value = 0x02;
691         gp2a_i2c_write(gp2a, REGS_OPMOD, value);
692
693         goto done;
694
695 err_request_irq:
696 err_gpio_direction_input:
697         gpio_free(pdata->p_out);
698 done:
699         return ret;
700 }
701
702 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
703 static int gp2a_request_gpio(struct gp2a_platform_data *pdata)
704 {
705         int ret;
706
707         ret = gpio_request(pdata->power_en, "prox_en");
708         if (ret) {
709                 pr_err("[SENSOR] %s: gpio request fail\n", __func__);
710                 return ret;
711         }
712
713         ret = gpio_direction_output(pdata->power_en, 0);
714         if (ret) {
715                 pr_err("[SENSOR] %s: unable to set_direction [%d]\n",
716                         __func__, pdata->power_en);
717                 return ret;
718         }
719         return 0;
720 }
721 #endif
722
723
724 static int gp2a_parse_dt(struct device *dev, struct gp2a_platform_data *pdata)
725 {
726         struct device_node *np = dev->of_node;
727         enum of_gpio_flags flags;
728
729         if (pdata == NULL)
730                 return -ENODEV;
731
732         pdata->p_out = of_get_named_gpio_flags(np, "gp2a-i2c,irq-gpio", 0,
733                 &flags);
734         if (pdata->p_out < 0) {
735                 pr_err("[SENSOR] %s : get irq_gpio(%d) error\n", __func__,
736                         pdata->p_out);
737                 return -ENODEV;
738         }
739
740 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
741         pdata->power_en = of_get_named_gpio_flags(np, "gp2a-i2c,en-gpio", 0,
742                 &flags);
743         if (pdata->power_en < 0) {
744                 pr_err("[SENSOR] %s : get power_en(%d) error\n", __func__,
745                         pdata->power_en);
746                 return -ENODEV;
747         }
748 #endif
749
750         return 0;
751 }
752
753
754 static int gp2a_i2c_probe(struct i2c_client *client,
755         const struct i2c_device_id *id)
756 {
757         struct gp2a_data *gp2a;
758         struct gp2a_platform_data *pdata = client->dev.platform_data;
759 #if defined(CONFIG_SENSORS_IIO)
760         struct iio_dev *indio_dev;
761 #endif
762         int ret;
763
764         pr_info("[SENSOR] %s, start\n", __func__);
765
766         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
767                 pr_err("[SENSOR] %s, i2c functionality failed\n", __func__);
768                 return -ENOMEM;
769         }
770
771 #if defined(CONFIG_SENSORS_GP2A_HAS_REGULATOR)
772         ret = gp2a_regulator_onoff(&client->dev, ON);
773         if (ret) {
774                 pr_err("[SENSOR] %s, Power Up Failed\n", __func__);
775                 goto err_regulator_onoff;
776         }
777 #endif
778
779         if (client->dev.of_node) {
780                 pdata = devm_kzalloc(&client->dev,
781                         sizeof(struct gp2a_platform_data), GFP_KERNEL);
782                 ret = gp2a_parse_dt(&client->dev, pdata);
783                 if (ret < 0)
784                         goto err_parse_dt;
785         }
786
787 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
788         ret = gp2a_request_gpio(pdata);
789         if (ret < 0)
790                 goto err_request_gpio;
791 #endif
792
793 #if defined(CONFIG_SENSORS_IIO)
794         /* Configure IIO device */
795         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*gp2a));
796         if (!indio_dev) {
797                 pr_err("[SENSOR] %s, iio_device_alloc failed\n", __func__);
798                 ret = -ENOMEM;
799                 goto err_mem_alloc;
800         }
801
802         gp2a = iio_priv(indio_dev);
803         dev_set_name(&indio_dev->dev, "proximity_sensor");
804         i2c_set_clientdata(client, indio_dev);
805
806         /* Init IIO device */
807         indio_dev->name = client->name;
808         indio_dev->channels = gp2a_channels;
809         indio_dev->num_channels = ARRAY_SIZE(gp2a_channels);
810         indio_dev->dev.parent = &client->dev;
811         indio_dev->modes = INDIO_DIRECT_MODE;
812         indio_dev->info = &gp2a_info;
813
814         ret = sensors_iio_configure_buffer(indio_dev);
815         if (ret) {
816                 pr_err("[SENSOR] %s, configure ring buffer failed\n",
817                         __func__);
818                 goto err_iio_configure_buffer_failed;
819         }
820
821         ret = iio_device_register(indio_dev);
822         if (ret) {
823                 pr_err("[SENSOR] %s: iio_device_register failed(%d)\n",
824                         __func__, ret);
825                 goto err_iio_register_failed;
826         }
827
828         gp2a->pdata = pdata;
829         gp2a->i2c_client = client;
830
831 #else   /* !defined(CONFIG_SENSORS_IIO)*/
832
833         /* Allocate memory for driver data */
834         gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
835         if (!gp2a) {
836                 pr_err("[SENSOR] %s, failed memory alloc\n", __func__);
837                 ret = -ENOMEM;
838                 goto err_mem_alloc;
839         }
840         i2c_set_clientdata(client, gp2a);
841
842         gp2a->pdata = pdata;
843         gp2a->i2c_client = client;
844
845         gp2a->input = input_allocate_device();
846         if (!gp2a->input) {
847                 pr_err("[SENSOR] %s, could not allocate input device\n",
848                         __func__);
849                 goto err_input_allocate_device_proximity;
850         }
851
852         input_set_drvdata(gp2a->input, gp2a);
853         gp2a->input->name = "proximity_sensor";
854         input_set_capability(gp2a->input, EV_ABS, ABS_DISTANCE);
855         input_set_abs_params(gp2a->input, ABS_DISTANCE, 0, 1, 0, 0);
856
857         ret = input_register_device(gp2a->input);
858         if (ret < 0) {
859                 pr_err("[SENSOR] %s, could not register input device\n",
860                         __func__);
861                 goto err_input_register_device_proximity;
862         }
863         ret = sensors_create_symlink(&gp2a->input->dev.kobj,
864                 gp2a->input->name);
865         if (ret < 0) {
866                 pr_err("[SENSOR] %s, create sysfs symlink error\n", __func__);
867                 goto err_sysfs_create_symlink_proximity;
868         }
869
870         ret = sysfs_create_group(&gp2a->input->dev.kobj,
871                 &proximity_attribute_group);
872         if (ret) {
873                 pr_err("[SENSOR] %s, create sysfs group error\n", __func__);
874                 goto err_sysfs_create_group_proximity;
875         }
876 #endif
877
878         wake_lock_init(&gp2a->prx_wake_lock, WAKE_LOCK_SUSPEND,
879                 "prx_wake_lock");
880         mutex_init(&gp2a->power_lock);
881
882         INIT_WORK(&gp2a->work_prox, gp2a_prox_work_func);
883
884         gp2a_leda_onoff(gp2a, ON);
885         ret = gp2a_setup_irq(gp2a);
886         if (ret) {
887                 pr_err("[SENSOR] %s, could not setup irq\n", __func__);
888                 goto err_setup_irq;
889         }
890
891         gp2a_leda_onoff(gp2a, OFF);
892
893         ret = sensors_register(gp2a->dev, gp2a, proxi_attrs,
894                 "proximity_sensor");
895         if (ret < 0) {
896                 pr_info("[SENSOR] %s, could not sensors_register\n",
897                         __func__);
898                 goto err_sensors_register;
899         }
900
901         pr_info("[SENSOR] %s done\n", __func__);
902         return 0;
903
904 err_sensors_register:
905         free_irq(gp2a->irq, gp2a);
906         gpio_free(gp2a->pdata->p_out);
907 err_setup_irq:
908         gp2a_leda_onoff(gp2a, OFF);
909         mutex_destroy(&gp2a->power_lock);
910         wake_lock_destroy(&gp2a->prx_wake_lock);
911 #if defined(CONFIG_SENSORS_IIO)
912         iio_device_unregister(indio_dev);
913 err_iio_register_failed:
914         sensors_iio_unconfigure_buffer(indio_dev);
915 err_iio_configure_buffer_failed:
916 #else
917         sysfs_remove_group(&gp2a->input->dev.kobj,
918                 &proximity_attribute_group);
919 err_sysfs_create_group_proximity:
920         sensors_remove_symlink(&gp2a->input->dev.kobj,
921                 gp2a->input->name);
922 err_sysfs_create_symlink_proximity:
923         input_unregister_device(gp2a->input);
924 err_input_register_device_proximity:
925         input_free_device(gp2a->input);
926 err_input_allocate_device_proximity:
927         kfree(gp2a);
928 #endif
929 err_mem_alloc:
930 err_request_gpio:
931 err_parse_dt:
932 #if defined(CONFIG_SENSORS_GP2A_HAS_REGULATOR)
933         gp2a_regulator_onoff(&client->dev, OFF);
934 err_regulator_onoff:
935 #endif
936         pr_err("[SENSOR] %s is FAILED (%d)\n", __func__, ret);
937         return ret;
938 }
939
940 static const struct i2c_device_id gp2a_device_id[] = {
941         {"gp2a", 0},
942         {}
943 };
944 MODULE_DEVICE_TABLE(i2c, gp2a_device_id);
945
946 static struct of_device_id gp2a_i2c_match_table[] = {
947         { .compatible = "gp2a-i2c",},
948         {},
949 };
950
951 MODULE_DEVICE_TABLE(of, gp2a_i2c_match_table);
952
953 static struct i2c_driver gp2a_i2c_driver = {
954         .driver = {
955                 .name = "gp2a",
956                 .owner = THIS_MODULE,
957                 .of_match_table = gp2a_i2c_match_table,
958
959         },
960         .probe          = gp2a_i2c_probe,
961         .id_table       = gp2a_device_id,
962 };
963
964 module_i2c_driver(gp2a_i2c_driver);
965
966 MODULE_AUTHOR("mjchen@sta.samsung.com");
967 MODULE_DESCRIPTION("Optical Sensor driver for gp2ap002");
968 MODULE_LICENSE("GPL");