upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / misc / gp2a_proximity.c
1 /*
2  * Copyright (c) 2010 SAMSUNG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA  02110-1301, USA.
18  */
19
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/input.h>
33 #include <linux/workqueue.h>
34 #include <linux/uaccess.h>
35 #include <linux/input/gp2a.h>
36 #include <linux/slab.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/gpio.h>
39
40 /*********** for debug *******************************/
41 #ifdef DEBUG
42 #define gprintk(fmt, x...) printk(KERN_INFO "%s(%d): "\
43 fmt, __func__ , __LINE__, ## x)
44 #else
45 #define gprintk(x...) do { } while (0)
46 #endif
47 /**************************************************/
48 #if 0
49 #define SENSOR_DEFAULT_DELAY            (200)   /* 200 ms */
50 #define SENSOR_MAX_DELAY                (2000)  /* 2000 ms */
51 #define ABS_STATUS                      (ABS_BRAKE)
52 #define ABS_WAKE                        (ABS_MISC)
53 #define ABS_CONTROL_REPORT              (ABS_THROTTLE)
54 #endif
55 #define PROX_READ_NUM   10
56
57 /* global var */
58 static struct wake_lock prx_wake_lock;
59
60 static struct i2c_driver opt_i2c_driver;
61 static struct i2c_client *opt_i2c_client;
62
63 int proximity_enable;
64
65 struct gp2a_data {
66         struct input_dev *input_dev;
67         struct work_struct work;        /* for proximity sensor */
68         /*struct mutex data_mutex; */
69         struct gp2a_platform_data *pdata;
70
71         /*int wakeup; configure the button as a wake-up source */
72         int enabled;
73         /*int delay;*/
74         int prox_data;
75         int irq;
76         int average[PROX_READ_NUM];     /*for proximity adc average */
77 };
78
79 static struct gp2a_data *prox_data;
80
81 struct opt_state {
82         struct i2c_client *client;
83 };
84
85 struct opt_state *opt_state;
86
87 /* initial value for sensor register */
88 #define COL 8
89 static u8 gp2a_original_image[COL][2] = {
90         /*{Regster, Value} */
91         /*PRST :01(4 cycle at Detection/Non-detection),
92            ALSresolution :16bit, range *128   //0x1F -> 5F by sharp */
93         {0x01, 0x63},
94         /*ALC : 0, INTTYPE : 1, PS mode resolution : 12bit, range*1 */
95         {0x02, 0x72},
96         /*LED drive current 110mA, Detection/Non-detection judgment output */
97         {0x03, 0x3C},
98         /*      {0x04 , 0x00}, */
99         /*      {0x05 , 0x00}, */
100         /*      {0x06 , 0xFF}, */
101         /*      {0x07 , 0xFF}, */
102         {0x08, 0x07},           /*PS mode LTH(Loff):  (??mm) */
103         {0x09, 0x00},           /*PS mode LTH(Loff) : */
104         {0x0A, 0x08},           /*PS mode HTH(Lon) : (??mm) */
105         {0x0B, 0x00},           /* PS mode HTH(Lon) : */
106         /*{0x13 , 0x08}, by sharp for internal calculation (type:0)*/
107         /*alternating mode (PS+ALS),
108         TYPE=1(0:externel 1:auto calculated mode) //umfa.cal */
109         {0x00, 0xC0}
110 };
111
112 /*static int alt_int = 0;*/
113 extern u8 lightsensor_mode;     /* 0 = low, 1 = high */
114 extern unsigned int get_hw_rev(void);
115
116 static int proximity_onoff(u8 onoff);
117
118 /*TODO Need to add comments*/
119 /* Proximity Sysfs interface */
120 #if 0
121 static ssize_t
122 proximity_delay_show(struct device *dev,
123                      struct device_attribute *attr, char *buf)
124 {
125         struct input_dev *input_data = to_input_dev(dev);
126         struct gp2a_data *data = input_get_drvdata(input_data);
127         int delay;
128
129         delay = data->delay;
130
131         return sprintf(buf, "%d\n", delay);
132 }
133
134 static ssize_t
135 proximity_delay_store(struct device *dev,
136                       struct device_attribute *attr,
137                       const char *buf, size_t count)
138 {
139         struct input_dev *input_data = to_input_dev(dev);
140         struct gp2a_data *data = input_get_drvdata(input_data);
141         int delay = 0;
142         int err = 0;
143
144         err = kstrtoint(buf, 10, &delay);
145
146         if (err)
147                 printk(KERN_ERR "%s, kstrtoint failed.", __func__);
148
149         if (delay < 0)
150                 return count;
151
152         if (SENSOR_MAX_DELAY < delay)
153                 delay = SENSOR_MAX_DELAY;
154
155         data->delay = delay;
156
157         /*input_report_abs(input_data, ABS_CONTROL_REPORT
158            , (data->enabled<<16) | delay); */
159
160         return count;
161 }
162 #endif
163
164 static ssize_t
165 proximity_enable_show(struct device *dev,
166                       struct device_attribute *attr, char *buf)
167 {
168         struct input_dev *input_data = to_input_dev(dev);
169         struct gp2a_data *data = input_get_drvdata(input_data);
170         int enabled;
171
172         enabled = data->enabled;
173
174         return sprintf(buf, "%d\n", enabled);
175 }
176
177 static ssize_t
178 proximity_enable_store(struct device *dev,
179                        struct device_attribute *attr,
180                        const char *buf, size_t count)
181 {
182         struct input_dev *input_data = to_input_dev(dev);
183         struct gp2a_data *data = input_get_drvdata(input_data);
184         unsigned long value = 0;
185         char input;
186         int err = 0;
187         /*signed char get_buf[18]={0,};//test */
188
189         err = strict_strtoul(buf, 10, &value);
190
191         if (err)
192                 printk(KERN_ERR "%s, conversion %s to number.\n", __func__, buf);
193
194         if (value != 0 && value != 1)
195                 return count;
196
197         if (data->enabled && !value) { /* Proximity power off */
198                 disable_irq(data->irq);
199
200                 proximity_onoff(0);
201                 disable_irq_wake(data->irq);
202                 /*data->pdata->gp2a_led_on(false); */
203         }
204         if (!data->enabled && value) { /* proximity power on */
205                 /*data->pdata->gp2a_led_on(true); */
206                 /*msleep(1); */
207
208                 proximity_onoff(1);
209                 enable_irq_wake(data->irq);
210                 msleep(160);
211
212                 input = gpio_get_value(data->pdata->p_out);
213                 /* we reports it to SensorFW by opposition, 1 :proximity , 0 :away*/
214                 input_report_abs(data->input_dev, ABS_DISTANCE, !input);
215                 input_sync(data->input_dev);
216
217                 enable_irq(data->irq);
218         }
219         data->enabled = (int)value;
220         proximity_enable = (int)value;
221
222         /* input_report_abs(input_data, ABS_CONTROL_REPORT,
223         (value<<16) | data->delay); */
224
225         return count;
226 }
227
228 /*
229 static ssize_t
230 proximity_wake_store(struct device *dev,
231         struct device_attribute *attr,
232         const char *buf, size_t count)
233 {
234     struct input_dev *input_data = to_input_dev(dev);
235     static int cnt = 1;
236
237     input_report_abs(input_data, ABS_WAKE, cnt++);
238
239     return count;
240 }
241
242 static ssize_t
243 proximity_data_show(struct device *dev,
244         struct device_attribute *attr, char *buf)
245 {
246     struct input_dev *input_data = to_input_dev(dev);
247         struct gp2a_data *data = input_get_drvdata(input_data);
248     int x;
249
250         mutex_lock(&data->data_mutex);
251         x = data->prox_data;
252         mutex_unlock(&data->data_mutex);
253     return sprintf(buf, "%d\n", x);
254 }
255 */
256
257 static int count; /*count for proximity average */
258
259 static ssize_t proximity_state_show(struct device *dev,
260                                     struct device_attribute *attr, char *buf)
261 {
262
263         struct input_dev *input_data = to_input_dev(dev);
264         struct gp2a_data *data = input_get_drvdata(input_data);
265         int D2_data = 0;
266         unsigned char get_D2_data[2] = { 0, };  /*test */
267
268         msleep(20);
269         opt_i2c_read(0x10, get_D2_data, sizeof(get_D2_data));
270         D2_data = (get_D2_data[1] << 8) | get_D2_data[0];
271
272         data->average[count] = D2_data;
273         count++;
274         if (count == PROX_READ_NUM)
275                 count = 0;
276
277         return sprintf(buf, "%d\n", D2_data);
278 }
279
280 static ssize_t proximity_avg_show(struct device *dev,
281                                   struct device_attribute *attr, char *buf)
282 {
283         struct input_dev *input_data = to_input_dev(dev);
284         struct gp2a_data *data = input_get_drvdata(input_data);
285
286         int min = 0, max = 0, avg = 0;
287         int i;
288         int proximity_value = 0;
289
290         for (i = 0; i < PROX_READ_NUM; i++) {
291                 proximity_value = data->average[i];
292                 if (proximity_value > 0) {
293
294                         avg += proximity_value;
295
296                         if (!i)
297                                 min = proximity_value;
298                         else if (proximity_value < min)
299                                 min = proximity_value;
300
301                         if (proximity_value > max)
302                                 max = proximity_value;
303                 }
304         }
305         avg /= i;
306
307         return sprintf(buf, "%d, %d, %d\n", min, avg, max);
308 }
309
310 static ssize_t proximity_avg_store(struct device *dev,
311                                    struct device_attribute *attr,
312                                    const char *buf, size_t size)
313 {
314         return proximity_enable_store(dev, attr, buf, size);
315 }
316
317 static ssize_t proximity_thresh_show(struct device *dev,
318                                     struct device_attribute *attr, char *buf)
319 {
320         int i;
321         int threshold = 0;
322
323         for (i = 0; i < COL; i++) {
324                 if (gp2a_original_image[i][0] == 0x08)
325                         /*PS mode LTH(Loff) */
326                         threshold = gp2a_original_image[i][1];
327                 else if (gp2a_original_image[i][0] == 0x09)
328                         /*PS mode LTH(Loff) */
329                         threshold |= gp2a_original_image[i][1]<<8;
330         }
331
332         return sprintf(buf, "prox_threshold = %d\n", threshold);
333 }
334
335 static ssize_t proximity_thresh_store(struct device *dev,
336                                    struct device_attribute *attr,
337                                    const char *buf, size_t size)
338 {
339         unsigned long thresh_value = 0x07;
340         int err = 0;
341         int i;
342
343         err = strict_strtoul(buf, 10, &thresh_value);
344
345         if (err)
346                 printk(KERN_ERR "%s, conversion %s to number.\n",
347                         __func__, buf);
348
349         for (i = 0; i < COL; i++) {
350                 if (gp2a_original_image[i][0] == 0x08)
351                         /*PS mode LTH(Loff) */
352                         gp2a_original_image[i][1] =
353                                 thresh_value & 0x00FF;
354                 else if (gp2a_original_image[i][0] == 0x09)
355                         /*PS mode LTH(Loff) */
356                         gp2a_original_image[i][1] =
357                                 (thresh_value & 0xFF00) >> 8;
358                 else if (gp2a_original_image[i][0] == 0x0A)
359                         /*PS mode HTH(Lon) */
360                         gp2a_original_image[i][1] =
361                                 (thresh_value+1) & 0x00FF;
362                 else if (gp2a_original_image[i][0] == 0x0B)
363                         /* PS mode HTH(Lon) */
364                         gp2a_original_image[i][1] =
365                                 ((thresh_value+1) & 0xFF00) >> 8;
366         }
367
368         for (i = 0; i < COL; i++) {
369                 if (gp2a_original_image[i][0] == 0x08 ||
370                         gp2a_original_image[i][0] == 0x09 ||
371                         gp2a_original_image[i][0] == 0x0A ||
372                         gp2a_original_image[i][0] == 0x0B)
373                         err = opt_i2c_write(gp2a_original_image[i][0],
374                                 &gp2a_original_image[i][1]);
375
376                 if (err < 0)
377                         printk(KERN_ERR "%s : setting error i = %d, err=%d\n",
378                                  __func__, i, err);
379         }
380
381         return size;
382 }
383
384
385 #if 0
386 static DEVICE_ATTR(poll_delay, 664,
387                    proximity_delay_show, proximity_delay_store);
388 #endif
389 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, proximity_enable_show, proximity_enable_store);
390 static DEVICE_ATTR(proximity_avg, S_IRUGO | S_IWUSR, proximity_avg_show, proximity_avg_store);
391 static DEVICE_ATTR(state, S_IRUGO, proximity_state_show, NULL);
392 static DEVICE_ATTR(prox_thresh, S_IRUGO | S_IWUSR,
393                         proximity_thresh_show, proximity_thresh_store);
394
395 /* TODO Need to check if those are used or not.*/
396 /*
397 static DEVICE_ATTR(wake, S_IWUSR|S_IWGRP,
398 NULL, proximity_wake_store);
399 static DEVICE_ATTR(data, S_IRUGO, proximity_data_show, NULL);
400 */
401
402 static struct attribute *proximity_attributes[] = {
403         /*&dev_attr_poll_delay.attr,*/
404         &dev_attr_enable.attr,
405         &dev_attr_state.attr,
406         &dev_attr_prox_thresh.attr,
407         /*
408            &dev_attr_prox_avg.attr,
409            &dev_attr_wake.attr,
410            &dev_attr_data.attr,
411          */
412         NULL
413 };
414
415 static struct attribute_group proximity_attribute_group = {
416         .attrs = proximity_attributes
417 };
418
419 irqreturn_t gp2a_irq_handler(int irq, void *dev_id)
420 {
421         schedule_work(&prox_data->work);
422
423         printk(KERN_INFO "[PROXIMITY] IRQ_HANDLED.\n");
424         return IRQ_HANDLED;
425 }
426
427 /***************************************************
428  *
429  *      function        : gp2a_setup_irq
430  *      description : This function sets up the interrupt.
431  *
432  */
433
434 static int gp2a_setup_irq(struct gp2a_data *gp2a)
435 {
436         int rc = -EIO;
437         struct gp2a_platform_data *pdata = gp2a->pdata;
438         int irq;
439
440         pr_err("%s, start\n", __func__);
441
442         rc = gpio_request(pdata->p_out, "gpio_proximity_out");
443         if (rc < 0) {
444                 pr_err("%s: gpio %d request failed (%d)\n",
445                        __func__, pdata->p_out, rc);
446                 return rc;
447         }
448
449         rc = gpio_direction_input(pdata->p_out);
450         if (rc < 0) {
451                 pr_err("%s: failed to set gpio %d as input (%d)\n",
452                        __func__, pdata->p_out, rc);
453                 goto err_gpio_direction_input;
454         }
455
456         irq = gpio_to_irq(pdata->p_out);
457         rc = request_threaded_irq(irq, NULL,
458                                   gp2a_irq_handler,
459                                   IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
460                                   "proximity_int", gp2a);
461         if (rc < 0) {
462                 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
463                        __func__, irq, pdata->p_out, rc);
464                 goto err_request_irq;
465         }
466
467         /* start with interrupts disabled */
468         disable_irq(irq);
469         gp2a->irq = irq;
470
471         pr_err("%s, success\n", __func__);
472
473         goto done;
474
475 err_request_irq:
476 err_gpio_direction_input:
477         gpio_free(pdata->p_out);
478 done:
479         return rc;
480 }
481
482 /***************************************************
483  *
484  *  function    : gp2a_work_func_prox
485  *  description : This function is for proximity sensor work function.
486  *         when INT signal is occured , it gets value the interrupt pin.
487  */
488
489 char proximity_sensor_detection;
490
491 static void gp2a_work_func_prox(struct work_struct *work)
492 {
493         struct gp2a_data *gp2a = container_of((struct work_struct *)work,
494                                               struct gp2a_data, work);
495
496         unsigned char value;
497         char result;
498         int ret;
499
500         /* 0 : proximity, 1 : away */
501         result = gpio_get_value(gp2a->pdata->p_out);
502         proximity_sensor_detection = !result;
503
504         input_report_abs(gp2a->input_dev, ABS_DISTANCE, result);
505         gprintk("%s Proximity values = %d\n", __func__, result);
506         input_sync(gp2a->input_dev);
507
508         disable_irq(gp2a->irq);
509
510         value = 0x0C;
511         ret = opt_i2c_write(COMMAND1, &value); /*Software reset */
512
513         if (result == 0) { /* detection = Falling Edge */
514                 if (lightsensor_mode == 0) /* Low mode */
515                         value = 0x23;
516                 else /* High mode */
517                         value = 0x27;
518                 ret = opt_i2c_write(COMMAND2, &value);
519         } else { /* none Detection */
520                 if (lightsensor_mode == 0) /* Low mode */
521                         value = 0x63;
522                 else /* High mode */
523                         value = 0x67;
524                 ret = opt_i2c_write(COMMAND2, &value);
525         }
526
527         enable_irq(gp2a->irq);
528
529         value = 0xCC;
530         ret = opt_i2c_write(COMMAND1, &value);
531
532         gp2a->prox_data = result;
533         gprintk("proximity = %d, lightsensor_mode=%d\n",
534                 result, lightsensor_mode);
535 }
536
537 static int opt_i2c_init(void)
538 {
539         if (i2c_add_driver(&opt_i2c_driver)) {
540                 printk(KERN_ERR "i2c_add_driver failed\n");
541                 return -ENODEV;
542         }
543         return 0;
544 }
545
546 int opt_i2c_read(u8 reg, unsigned char *rbuf, int len)
547 {
548         int ret = -1;
549         struct i2c_msg msg;
550
551         if ((opt_i2c_client == NULL) || (!opt_i2c_client->adapter)) {
552                 printk(KERN_ERR "%s %d (opt_i2c_client == NULL)\n",
553                        __func__, __LINE__);
554                 return -ENODEV;
555         }
556
557         msg.addr = opt_i2c_client->addr;
558         msg.flags = I2C_M_WR;
559         msg.len = 1;
560         msg.buf = &reg;
561
562         ret = i2c_transfer(opt_i2c_client->adapter, &msg, 1);
563
564         if (ret >= 0) {
565                 msg.flags = I2c_M_RD;
566                 msg.len = len;
567                 msg.buf = rbuf;
568                 ret = i2c_transfer(opt_i2c_client->adapter, &msg, 1);
569         }
570
571         if (ret < 0)
572                 gprintk("i2c transfer error ret=%d\n", ret);
573
574         /*
575            for (i=0;i<len;i++)
576            {
577            printk(KERN_INFO "%s : 0x%x, 0x%x\n", __func__,reg++,rbuf[i]);
578            }
579          */
580         return ret;
581 }
582
583 int opt_i2c_write(u8 reg, u8 *val)
584 {
585         int err = 0;
586         struct i2c_msg msg[1];
587         unsigned char data[2];
588         int retry = 3;
589
590         if ((opt_i2c_client == NULL) || (!opt_i2c_client->adapter))
591                 return -ENODEV;
592
593         while (retry--) {
594                 data[0] = reg;
595                 data[1] = *val;
596
597                 msg->addr = opt_i2c_client->addr;
598                 msg->flags = I2C_M_WR;
599                 msg->len = 2;
600                 msg->buf = data;
601
602                 err = i2c_transfer(opt_i2c_client->adapter, msg, 1);
603                 /*gprintk("0x%x, 0x%x\n", reg, *val); */
604
605                 if (err >= 0)
606                         return 0;
607         }
608         gprintk(" i2c transfer error(%d)\n", err);
609         return err;
610 }
611
612 static int proximity_input_init(struct gp2a_data *data)
613 {
614         struct input_dev *dev;
615         int err = 0;
616
617         printk(KERN_INFO "%s, %d start\n", __func__, __LINE__);
618
619         dev = input_allocate_device();
620         if (!dev) {
621                 printk(KERN_ERR "%s, error\n", __func__);
622                 return -ENOMEM;
623         }
624
625         input_set_capability(dev, EV_ABS, ABS_DISTANCE);
626         input_set_abs_params(dev, ABS_DISTANCE, 0, 0xFF, 0, 0);
627
628     #if 0
629         input_set_capability(dev, EV_ABS, ABS_STATUS);
630         input_set_capability(dev, EV_ABS, ABS_WAKE);
631         input_set_capability(dev, EV_ABS, ABS_CONTROL_REPORT);
632         #endif
633
634         dev->name = "proximity_sensor";
635         input_set_drvdata(dev, data);
636
637         err = input_register_device(dev);
638         if (err < 0) {
639                 input_free_device(dev);
640                 return err;
641         }
642         data->input_dev = dev;
643
644         printk(KERN_INFO "%s, success\n", __func__);
645         return 0;
646 }
647
648 static int gp2a_opt_probe(struct platform_device *pdev)
649 {
650         struct gp2a_data *gp2a;
651         struct gp2a_platform_data *pdata = pdev->dev.platform_data;
652         u8 value = 0;
653         int ret = 0;
654         int err = 0;
655         /* int wakeup = 0; */
656
657         printk(KERN_INFO "%s : probe start!\n", __func__);
658
659         if (!pdata) {
660                 pr_err("%s: missing pdata!\n", __func__);
661                 return ret;
662         }
663
664         if (pdata->gp2a_get_low_threshold) {
665                 int i;
666                 for (i = 0; i < COL; i++) {
667                         if (gp2a_original_image[i][0] == 0x08)
668                                 /*PS mode LTH(Loff) */
669                                 gp2a_original_image[i][1] =
670                                         pdata->gp2a_get_low_threshold()
671                                         & 0x00FF;
672                         else if (gp2a_original_image[i][0] == 0x09)
673                                 /*PS mode LTH(Loff) */
674                                 gp2a_original_image[i][1] =
675                                         (pdata->gp2a_get_low_threshold()
676                                         & 0xFF00) >> 8;
677                         else if (gp2a_original_image[i][0] == 0x0A)
678                                 /*PS mode HTH(Lon) */
679                                 gp2a_original_image[i][1] =
680                                         (pdata->gp2a_get_low_threshold()+1)
681                                         & 0x00FF;
682                         else if (gp2a_original_image[i][0] == 0x0B)
683                                 /* PS mode HTH(Lon) */
684                                 gp2a_original_image[i][1] =
685                                         ((pdata->gp2a_get_low_threshold()+1)
686                                         & 0xFF00) >> 8;
687                 }
688         }
689
690         if (!pdata->gp2a_led_on) {
691                 pr_err("%s: incomplete pdata!\n", __func__);
692                 return ret;
693         }
694         /* gp2a power on */
695         pdata->gp2a_led_on(true);
696
697         /* allocate driver_data */
698         gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
699         if (!gp2a) {
700                 pr_err("kzalloc error\n");
701                 return -ENOMEM;
702
703         }
704
705         gp2a->enabled = 0;
706         gp2a->pdata = pdata;
707         /* gp2a->delay = SENSOR_DEFAULT_DELAY; */
708         prox_data = gp2a;
709
710         /*mutex_init(&gp2a->data_mutex); */
711
712         INIT_WORK(&gp2a->work, gp2a_work_func_prox);
713
714         err = proximity_input_init(gp2a);
715         if (err < 0)
716                 goto error_setup_reg;
717
718         err = sysfs_create_group(&gp2a->input_dev->dev.kobj,
719                                  &proximity_attribute_group);
720         if (err < 0)
721                 goto err_sysfs_create_group_proximity;
722
723         /* set platdata */
724         platform_set_drvdata(pdev, gp2a);
725
726         /* init i2c */
727         opt_i2c_init();
728
729         if (opt_i2c_client == NULL) {
730                 pr_err("opt_probe failed : i2c_client is NULL\n");
731                 return -ENODEV;
732         } else
733                 printk(KERN_INFO "opt_i2c_client : (0x%p), address = %x\n",
734                        opt_i2c_client, opt_i2c_client->addr);
735
736         /* GP2A Regs INIT SETTINGS  and Check I2C communication */
737
738         value = 0x00;
739         opt_i2c_write((u8) (COMMAND1), &value); /* shutdown mode op[3]=0 */
740
741         /* Setup irq */
742         ret = gp2a_setup_irq(gp2a);
743         if (ret) {
744                 pr_err("%s: could not setup irq\n", __func__);
745                 goto err_setup_irq;
746         }
747
748         device_init_wakeup(&pdev->dev, 1/*wakeup*/);
749
750         printk(KERN_INFO "%s : probe success!\n", __func__);
751
752         return 0;
753
754 err_setup_irq:
755         sysfs_remove_group(&gp2a->input_dev->dev.kobj,
756                            &proximity_attribute_group);
757 err_sysfs_create_group_proximity:
758         input_unregister_device(gp2a->input_dev);
759         input_free_device(gp2a->input_dev);
760 error_setup_reg:
761         kfree(gp2a);
762         return err;
763 }
764
765 static int gp2a_opt_remove(struct platform_device *pdev)
766 {
767         struct gp2a_data *gp2a = platform_get_drvdata(pdev);
768
769         if (gp2a->input_dev != NULL) {
770                 sysfs_remove_group(&gp2a->input_dev->dev.kobj,
771                                    &proximity_attribute_group);
772                 input_unregister_device(gp2a->input_dev);
773                 if (gp2a->input_dev != NULL)
774                         kfree(gp2a->input_dev);
775         }
776
777         /*mutex_destroy(gp2a->data_mutex); */
778         device_init_wakeup(&pdev->dev, 0);
779
780         kfree(gp2a);
781
782         return 0;
783 }
784
785 #ifdef CONFIG_PM
786
787 static int gp2a_opt_suspend(struct device *dev)
788 {
789         struct gp2a_data *gp2a = dev_get_drvdata(dev);
790
791         gprintk("\n");
792
793         if (gp2a->enabled) {
794                 if (device_may_wakeup(dev))
795                         enable_irq_wake(gp2a->irq);
796         }
797
798         return 0;
799 }
800
801 static int gp2a_opt_resume(struct device *dev)
802 {
803         struct gp2a_data *gp2a = dev_get_drvdata(dev);
804
805         gprintk("\n");
806
807         if (gp2a->enabled) {
808                 if (device_may_wakeup(dev))
809                         disable_irq_wake(gp2a->irq);
810         }
811
812         return 0;
813 }
814
815 static int gp2a_opt_freeze(struct device *dev)
816 {
817         struct gp2a_data *gp2a = dev_get_drvdata(dev);
818
819         if (gp2a->enabled) {
820                 disable_irq_wake(gp2a->irq);
821                 disable_irq_nosync(gp2a->irq);
822                 proximity_onoff(0);
823         }
824
825         return 0;
826 }
827 static int gp2a_opt_restore(struct device *dev)
828 {
829         struct gp2a_data *gp2a = dev_get_drvdata(dev);
830
831         if (gp2a->enabled) {
832                 proximity_onoff(1);
833                 enable_irq(gp2a->irq);
834         }
835
836         return 0;
837 }
838
839 static struct dev_pm_ops gp2a_opt_dev_pm_ops = {
840         .suspend        = gp2a_opt_suspend,
841         .resume         = gp2a_opt_resume,
842         .freeze         = gp2a_opt_freeze,
843         .restore        = gp2a_opt_restore,
844 };
845
846 #define GP2A_OPT_DEV_PM_OPS     (&gp2a_opt_dev_pm_ops)
847 #else
848 #define GP2A_OPT_DEV_PM_OPS     NULL
849 #endif /* CONFIG_PM */
850
851 static int proximity_onoff(u8 onoff)
852 {
853         u8 value;
854         int i;
855         /* unsigned char get_data[1]; */
856         int err = 0;
857
858         printk(KERN_INFO "%s : proximity turn on/off = %d\n", __func__, onoff);
859
860         /* already on light sensor, so must simultaneously
861         turn on light sensor and proximity sensor */
862         if (onoff) {
863                 /*opt_i2c_read(COMMAND1, get_data, sizeof(get_data)); */
864                 /*if (get_data == 0xC1)
865                    return 0; */
866                 for (i = 0; i < COL; i++) {
867                         err =
868                             opt_i2c_write(gp2a_original_image[i][0],
869                                           &gp2a_original_image[i][1]);
870                         if (err < 0)
871                                 printk(KERN_ERR "%s : turnning on error i = %d, err=%d\n",
872                                      __func__, i, err);
873                         lightsensor_mode = 0;
874                 }
875         } else { /* light sensor turn on and proximity turn off */
876                 /*opt_i2c_read(COMMAND1, get_data, sizeof(get_data)); */
877                 /*if (get_data == 0xD1)
878                    return 0; */
879
880                 if (lightsensor_mode)
881                         value = 0x67; /*resolution :16bit, range: *8(HIGH) */
882                 else
883                         value = 0x63; /* resolution :16bit, range: *128(LOW) */
884                 opt_i2c_write(COMMAND2, &value);
885                 /* OP3 : 1(operating mode)
886                 OP2 :1(coutinuous operating mode) OP1 : 01(ALS mode) */
887                 value = 0xD0;
888                 opt_i2c_write(COMMAND1, &value);
889         }
890
891         return 0;
892 }
893
894 static int opt_i2c_remove(struct i2c_client *client)
895 {
896         struct opt_state *data = i2c_get_clientdata(client);
897
898         kfree(data);
899         opt_i2c_client = NULL;
900
901         return 0;
902 }
903
904 static int opt_i2c_probe(struct i2c_client *client,
905                          const struct i2c_device_id *id)
906 {
907         struct opt_state *opt;
908
909         printk(KERN_INFO "%s, %d : start!!!\n", __func__, __LINE__);
910
911         opt = kzalloc(sizeof(struct opt_state), GFP_KERNEL);
912         if (opt == NULL) {
913                 printk(KERN_ERR "failed to allocate memory\n");
914                 pr_err("kzalloc error\n");
915                 pr_err("%s, %d : error!!!\n", __func__, __LINE__);
916                 return -ENOMEM;
917         }
918
919         if (client == NULL)
920                 printk(KERN_ERR "GP2A i2c client is NULL !!!\n");
921         opt->client = client;
922         i2c_set_clientdata(client, opt);
923
924         /* rest of the initialisation goes here. */
925
926         pr_err("GP2A opt i2c attach success!!!\n");
927
928         opt_i2c_client = client;
929         pr_err("%s, %d : end!!!\n", __func__, __LINE__);
930
931         return 0;
932 }
933
934 static const struct i2c_device_id opt_device_id[] = {
935         {"gp2a", 0},
936         {}
937 };
938
939 MODULE_DEVICE_TABLE(i2c, opt_device_id);
940
941 static struct i2c_driver opt_i2c_driver = {
942         .driver = {
943                    .name = "gp2a",
944                    .owner = THIS_MODULE,
945                    },
946         .probe = opt_i2c_probe,
947         .remove = opt_i2c_remove,
948         .id_table = opt_device_id,
949 };
950
951 static struct platform_driver gp2a_opt_driver = {
952         .probe = gp2a_opt_probe,
953         .remove = gp2a_opt_remove,
954         .driver = {
955                    .name = "gp2a-opt",
956                    .owner = THIS_MODULE,
957                    .pm = GP2A_OPT_DEV_PM_OPS,
958                    },
959 };
960
961 #ifdef CONFIG_BATTERY_SEC
962 extern unsigned int is_lpcharging_state(void);
963 #endif
964
965 static int __init gp2a_opt_init(void)
966 {
967         int ret;
968
969 #ifdef CONFIG_BATTERY_SEC
970         if (is_lpcharging_state()) {
971                 pr_info("%s : LPM Charging Mode! return 0\n", __func__);
972                 return 0;
973         }
974 #endif
975
976         ret = platform_driver_register(&gp2a_opt_driver);
977         return ret;
978 }
979
980 static void __exit gp2a_opt_exit(void)
981 {
982         platform_driver_unregister(&gp2a_opt_driver);
983 }
984
985 module_init(gp2a_opt_init);
986 module_exit(gp2a_opt_exit);
987
988 MODULE_AUTHOR("SAMSUNG");
989 MODULE_DESCRIPTION("Optical Sensor driver for GP2AP020A00F");
990 MODULE_LICENSE("GPL");