upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / misc / gp2a_light.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/delay.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/input.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/types.h>
28 #include <linux/platform_device.h>
29 #include <linux/input/gp2a.h>
30 #include <linux/slab.h>
31 #include <linux/regulator/consumer.h>
32
33 /* for debugging */
34 #undef DEBUG
35
36 /*********** for debug ***************************/
37 #if 1
38 #define gprintk(fmt, x...) printk(KERN_INFO "%s(%d): " fmt\
39 , __func__ , __LINE__, ## x)
40 #else
41 #define gprintk(x...) do { } while (0)
42 #endif
43 /***********************************************/
44
45 #define SENSOR_NAME                     "light_gp2a"
46 #define SENSOR_MAX_DELAY        (2000)  /* 2000 ms */
47 #define LIGHT_BUFFER_NUM        5
48
49 struct sensor_data {
50         struct mutex mutex;
51         struct delayed_work work;
52         struct input_dev *input_dev;
53         struct workqueue_struct *wq;
54         int enabled;
55         int delay;
56         int light_buffer;
57         int light_count;
58 };
59
60 /*TODO Need to change depends on the lightsensor table*/
61 static const int adc_table[4] = {
62         15,                     /*15 lux match adc value */
63         140,                    /*150 lux match adc value */
64         1490,                   /*1500 lux match adc value */
65         15000,                  /*15000 lux match adc value */
66 };
67
68 static bool first_value = true;
69 u8 lightsensor_mode;            /* 0 = low, 1 = high */
70
71 static int lightsensor_get_adc(void);
72 static int lightsensor_onoff(u8 onoff);
73
74 static ssize_t
75 light_curr_adc_show(struct device *dev, struct device_attribute *attr, char *buf)
76 {
77         int adc = 0;
78
79         adc = lightsensor_get_adcvalue();
80
81         return sprintf(buf, "%d\n", adc);
82 }
83
84 static ssize_t
85 light_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
86 {
87         struct input_dev *input_data = to_input_dev(dev);
88         struct sensor_data *data = input_get_drvdata(input_data);
89
90         return sprintf(buf, "%d\n", data->delay);
91 }
92
93 static ssize_t
94 light_delay_store(struct device *dev, struct device_attribute *attr,
95                   const char *buf, size_t count)
96 {
97         struct input_dev *input_data = to_input_dev(dev);
98         struct sensor_data *data = input_get_drvdata(input_data);
99         long tempv;
100         int delay;
101         int ret;
102
103         ret = strict_strtol(buf, 10, &tempv);
104
105         if ((ret != 0) || (tempv <= 0))
106                 return count;
107
108         delay = tempv/1000000; /* ns to msec */
109
110         pr_info("%s, new_delay = %d(ms), old_delay = %d(ms)",
111                         __func__, delay, data->delay);
112
113         if (SENSOR_MAX_DELAY < delay)
114                 delay = SENSOR_MAX_DELAY;
115
116         data->delay = delay;
117
118         mutex_lock(&data->mutex);
119
120         if (data->enabled) {
121                 cancel_delayed_work_sync(&data->work);
122                 queue_delayed_work(data->wq, &data->work,
123                                 msecs_to_jiffies(delay));
124         }
125
126         mutex_unlock(&data->mutex);
127
128         return count;
129 }
130
131 static ssize_t
132 light_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134         struct input_dev *input_data = to_input_dev(dev);
135         struct sensor_data *data = input_get_drvdata(input_data);
136         int enabled;
137
138         enabled = data->enabled;
139
140         return sprintf(buf, "%d\n", enabled);
141 }
142
143 static ssize_t
144 light_enable_store(struct device *dev, struct device_attribute *attr,
145                    const char *buf, size_t count)
146 {
147         struct input_dev *input_data = to_input_dev(dev);
148         struct sensor_data *data = input_get_drvdata(input_data);
149         long value;
150         int ret;
151
152         ret = strict_strtol(buf, 10, &value);
153
154         if ((ret != 0) || (value != 0 && value != 1))
155                 return count;
156
157         mutex_lock(&data->mutex);
158
159         if (data->enabled && !value) {
160                 cancel_delayed_work_sync(&data->work);
161                 gprintk("timer canceled.\n");
162                 lightsensor_onoff(0);
163                 data->enabled = value;
164         }
165         if (!data->enabled && value) {
166                 lightsensor_onoff(1);
167                 data->enabled = value;
168                 first_value = true;
169                 queue_delayed_work(data->wq, &data->work, 0);
170                 gprintk("timer started.\n");
171         }
172
173         mutex_unlock(&data->mutex);
174
175         return count;
176 }
177
178 static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR, light_delay_show, light_delay_store);
179 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, light_enable_show, light_enable_store);
180 static DEVICE_ATTR(curr_adc, S_IRUGO, light_curr_adc_show, NULL);
181
182 static struct attribute *lightsensor_attributes[] = {
183         &dev_attr_poll_delay.attr,
184         &dev_attr_enable.attr,
185         &dev_attr_curr_adc.attr,
186         NULL
187 };
188
189 static struct attribute_group lightsensor_attribute_group = {
190         .attrs = lightsensor_attributes
191 };
192
193 int lightsensor_get_adc(void)
194 {
195         unsigned char get_data[4] = { 0, };
196         int D0_raw_data;
197         int D1_raw_data;
198         int D0_data;
199         int D1_data;
200         int lx = 0;
201         u8 value;
202         int light_alpha;
203         int light_beta;
204         static int lx_prev;
205         int ret = 0;
206         int d0_boundary = 93;
207
208         ret = opt_i2c_read(DATA0_LSB, get_data, sizeof(get_data));
209         if (ret < 0)
210                 return lx_prev;
211         D0_raw_data = (get_data[1] << 8) | get_data[0]; /* clear */
212         D1_raw_data = (get_data[3] << 8) | get_data[2]; /* IR */
213
214         if (lightsensor_mode) { /* HIGH_MODE */
215                 if (100 * D1_raw_data <= 32 * D0_raw_data) {
216                         light_alpha = 800;
217                         light_beta = 0;
218                 } else if (100 * D1_raw_data <= 67 * D0_raw_data) {
219                         light_alpha = 2015;
220                         light_beta = 2925;
221                 } else if (100 * D1_raw_data <= d0_boundary * D0_raw_data) {
222                         light_alpha = 56;
223                         light_beta = 12;
224                 } else {
225                         light_alpha = 0;
226                         light_beta = 0;
227                 }
228         } else {                /* LOW_MODE */
229                 if (100 * D1_raw_data <= 32 * D0_raw_data) {
230                         light_alpha = 800;
231                         light_beta = 0;
232                 } else if (100 * D1_raw_data <= 67 * D0_raw_data) {
233                         light_alpha = 2015;
234                         light_beta = 2925;
235                 } else if (100 * D1_raw_data <= d0_boundary * D0_raw_data) {
236                         light_alpha = 547;
237                         light_beta = 599;
238                 } else {
239                         light_alpha = 0;
240                         light_beta = 0;
241                 }
242         }
243
244         if (lightsensor_mode) { /* HIGH_MODE */
245                 D0_data = D0_raw_data * 16;
246                 D1_data = D1_raw_data * 16;
247         } else {                /* LOW_MODE */
248                 D0_data = D0_raw_data;
249                 D1_data = D1_raw_data;
250         }
251
252         if ((D0_data == 0 || D1_data == 0)\
253                 && (D0_data < 300 && D1_data < 300)) {
254                 lx = 0;
255 #ifdef DEBUG
256                 gprintk("lx is 0 : D0=%d, D1=%d\n", D0_raw_data, D1_raw_data);
257 #endif
258         } else if (lightsensor_mode == 0
259                    && (D0_raw_data >= 16000 || D1_raw_data >= 16000)
260                    && (D0_raw_data <= 16383 && D1_raw_data <= 16383)) {
261 #ifdef DEBUG
262                 gprintk("need to changed HIGH_MODE D0=%d, D1=%d\n",
263                         D0_raw_data, D1_raw_data);
264 #endif
265                 lx = lx_prev;
266         } else if ((100 * D1_data > d0_boundary * D0_data)
267                    || (100 * D1_data < 15 * D0_data)) {
268                 lx = lx_prev;
269 #ifdef DEBUG
270 gprintk("Data range over so ues prev_lx value=%d D0=%d, D1=%d mode=%d\n",\
271 lx, D0_data, D1_data, lightsensor_mode);
272 #endif
273                 return lx;
274         } else {
275                 lx = (int)((light_alpha / 10 * D0_data * 33)
276                                 - (light_beta / 10 * D1_data * 33)) / 1000;
277 #ifdef DEBUG
278                 gprintk("D0=%d, D1=%d, lx=%d mode=%d a=%d, b=%d prev_lx=%d\n",
279                         D0_raw_data, D1_raw_data, lx, lightsensor_mode,
280                         light_alpha, light_beta, lx_prev);
281 #endif
282         }
283
284         lx_prev = lx;
285
286         if (lightsensor_mode) { /* HIGH MODE */
287                 if (D0_raw_data < 1000) {
288                         printk(KERN_INFO
289                                "%s: change to LOW_MODE detection=%d\n",
290                                __func__, proximity_sensor_detection);
291                         lightsensor_mode = 0;   /* change to LOW MODE */
292
293                         value = 0x0C;
294                         opt_i2c_write(COMMAND1, &value);
295
296                         if (proximity_sensor_detection)
297                                 value = 0x23;
298                         else
299                                 value = 0x63;
300                         opt_i2c_write(COMMAND2, &value);
301
302                         if (proximity_enable)
303                                 value = 0xCC;
304                         else
305                                 value = 0xDC;
306                         opt_i2c_write(COMMAND1, &value);
307                 }
308         } else { /* LOW MODE*/
309                 if (D0_raw_data > 16000 || D1_raw_data > 16000) {
310                         printk(KERN_INFO "%s: change to HIGH_MODE detection=%d\n",
311                                __func__, proximity_sensor_detection);
312                         lightsensor_mode = 1; /* change to HIGH MODE */
313
314                         value = 0x0C;
315                         opt_i2c_write(COMMAND1, &value);
316
317                         if (proximity_sensor_detection)
318                                 value = 0x27;
319                         else
320                                 value = 0x67;
321                         opt_i2c_write(COMMAND2, &value);
322
323                         if (proximity_enable)
324                                 value = 0xCC;
325                         else
326                                 value = 0xDC;
327                         opt_i2c_write(COMMAND1, &value);
328                 }
329         }
330
331         return lx;
332 }
333
334 int lightsensor_get_adcvalue(void)
335 {
336         int i, j, value, adc_avr_value;
337         unsigned int adc_total = 0, adc_max, adc_min, adc_index;
338         static unsigned int adc_index_count;
339         static int adc_value_buf[ADC_BUFFER_NUM] = { 0, };
340
341         value = lightsensor_get_adc();
342
343         adc_index = (adc_index_count++) % ADC_BUFFER_NUM;
344
345         /*ADC buffer initialize (light sensor off ---> light sensor on) */
346         if (first_value == true) {
347                 for (j = 0; j < ADC_BUFFER_NUM; j++)
348                         adc_value_buf[j] = value;
349                 first_value = false;
350         } else {
351                 adc_value_buf[adc_index] = value;
352         }
353
354         adc_max = adc_value_buf[0];
355         adc_min = adc_value_buf[0];
356
357         for (i = 0; i < ADC_BUFFER_NUM; i++) {
358                 adc_total += adc_value_buf[i];
359
360                 if (adc_max < adc_value_buf[i])
361                         adc_max = adc_value_buf[i];
362
363                 if (adc_min > adc_value_buf[i])
364                         adc_min = adc_value_buf[i];
365         }
366         adc_avr_value =
367             (adc_total - (adc_max + adc_min)) / (ADC_BUFFER_NUM - 2);
368
369         if (adc_index_count == ADC_BUFFER_NUM - 1)
370                 adc_index_count = 0;
371
372         return adc_avr_value;
373 }
374
375 static int lightsensor_onoff(u8 onoff)
376 {
377         u8 value;
378
379 #ifdef DEBUG
380         printk(KERN_INFO "%s : light_sensor onoff = %d\n", __func__, onoff);
381         printk(KERN_INFO "%s : proximity_enable onoff = %d\n", __func__,
382                proximity_enable);
383 #endif
384
385         if (onoff) {
386                 /*in calling, must turn on proximity sensor */
387                 if (proximity_enable == 0) {
388                         value = 0x01;
389                         opt_i2c_write(COMMAND4, &value);
390
391                         value = 0x63;
392                         opt_i2c_write(COMMAND2, &value);
393                         /*OP3 : 1(operating mode) OP2 :1
394                            (coutinuous operating mode)
395                            OP1 : 01(ALS mode) TYPE=0(auto) */
396                         value = 0xD0;
397                         opt_i2c_write(COMMAND1, &value);
398                         /* other setting have defualt value. */
399                 }
400         } else {
401                 /*in calling, must turn on proximity sensor */
402                 if (proximity_enable == 0) {
403                         value = 0x00; /*shutdown mode */
404                         opt_i2c_write((u8) (COMMAND1), &value);
405                 }
406         }
407
408         return 0;
409 }
410
411 static void gp2a_work_func_light(struct work_struct *work)
412 {
413         struct sensor_data *data = container_of((struct delayed_work *)work,
414                                                 struct sensor_data, work);
415         int i;
416         int adc = 0;
417
418         adc = lightsensor_get_adcvalue();
419
420         for (i = 0; ARRAY_SIZE(adc_table); i++)
421                 if (adc <= adc_table[i])
422                         break;
423
424         if (data->light_buffer == i) {
425                 if (data->light_count++ == LIGHT_BUFFER_NUM) {
426                         input_report_rel(data->input_dev, REL_MISC, adc);
427                         input_sync(data->input_dev);
428                         data->light_count = 0;
429                 }
430         } else {
431                 data->light_buffer = i;
432                 data->light_count = 0;
433         }
434
435         if (data->enabled)
436                 queue_delayed_work(data->wq, &data->work,
437                                    msecs_to_jiffies(data->delay));
438 }
439
440 static int lightsensor_probe(struct platform_device *pdev)
441 {
442         struct sensor_data *data = NULL;
443         int rt;
444         unsigned char get_data = 0;
445
446         /* Check I2C communication */
447         rt = opt_i2c_read(DATA0_LSB, &get_data, sizeof(get_data));
448
449         if (rt < 0) {
450                 pr_err("%s failed : threre is no such device.\n", __func__);
451                 return rt;
452         }
453
454         pr_info("%s : probe start!\n", __func__);
455
456         data = kzalloc(sizeof(struct sensor_data), GFP_KERNEL);
457         if (!data) {
458                 pr_err("%s failed to alloc memory.\n", __func__);
459                 return -ENOMEM;
460         }
461         data->delay = SENSOR_MAX_DELAY;
462
463         data->input_dev = input_allocate_device();
464         if (!data->input_dev) {
465                 pr_err("%s: could not allocate input device\n", __func__);
466                 rt = -ENOMEM;
467                 goto err_input_allocate_device_light;
468         }
469
470         input_set_capability(data->input_dev, EV_REL, REL_MISC);
471         data->input_dev->name = SENSOR_NAME;
472
473         rt = input_register_device(data->input_dev);
474         if (rt) {
475                 pr_err("%s: could not register input device\n", __func__);
476                 input_free_device(data->input_dev);
477                 goto err_input_allocate_device_light;
478         }
479         input_set_drvdata(data->input_dev, data);
480
481         rt = sysfs_create_group(&data->input_dev->dev.kobj,
482                                 &lightsensor_attribute_group);
483         if (rt) {
484                 pr_err("%s: could not create sysfs group\n", __func__);
485                 goto err_sysfs_create_group_light;
486         }
487         mutex_init(&data->mutex);
488
489         data->wq = create_singlethread_workqueue("gp2a_wq");
490         if (!data->wq) {
491                 rt = -ENOMEM;
492                 pr_err("%s: could not create workqueue\n", __func__);
493                 goto err_create_workqueue;
494         }
495
496         /* this is the thread function we run on the work queue */
497         INIT_DELAYED_WORK(&data->work, gp2a_work_func_light);
498
499         /* set platdata */
500         platform_set_drvdata(pdev, data);
501
502         pr_info("%s : probe success!\n", __func__);
503
504         return 0;
505
506 /* error, unwind it all */
507 err_create_workqueue:
508         mutex_destroy(&data->mutex);
509         sysfs_remove_group(&data->input_dev->dev.kobj,
510                            &lightsensor_attribute_group);
511 err_sysfs_create_group_light:
512         input_unregister_device(data->input_dev);
513 err_input_allocate_device_light:
514         kfree(data);
515
516         return rt;
517 }
518
519 static int lightsensor_remove(struct platform_device *pdev)
520 {
521         struct sensor_data *data = platform_get_drvdata(pdev);
522
523         if (data != NULL) {
524                 sysfs_remove_group(&data->input_dev->dev.kobj,
525                                    &lightsensor_attribute_group);
526                 cancel_delayed_work_sync(&data->work);
527                 flush_workqueue(data->wq);
528                 destroy_workqueue(data->wq);
529                 input_unregister_device(data->input_dev);
530                 mutex_destroy(&data->mutex);
531                 kfree(data);
532         }
533
534         return 0;
535 }
536
537 static void lightsensor_shutdown(struct platform_device *pdev)
538 {
539         struct sensor_data *data = platform_get_drvdata(pdev);
540
541         if (data != NULL) {
542                 sysfs_remove_group(&data->input_dev->dev.kobj,
543                                    &lightsensor_attribute_group);
544                 cancel_delayed_work_sync(&data->work);
545                 flush_workqueue(data->wq);
546                 destroy_workqueue(data->wq);
547                 input_unregister_device(data->input_dev);
548                 kfree(data);
549         }
550 }
551
552 #ifdef CONFIG_PM
553
554 static int lightsensor_suspend(struct device *dev)
555 {
556         struct platform_device *pdev = to_platform_device(dev);
557         struct sensor_data *data = platform_get_drvdata(pdev);
558
559         int rt = 0;
560
561         mutex_lock(&data->mutex);
562
563         if (data->enabled) {
564                 rt = cancel_delayed_work_sync(&data->work);
565                 gprintk(": The timer is cancled.\n");
566         }
567
568         mutex_unlock(&data->mutex);
569
570         return rt;
571 }
572
573 static int lightsensor_resume(struct device *dev)
574 {
575         struct platform_device *pdev = to_platform_device(dev);
576         struct sensor_data *data = platform_get_drvdata(pdev);
577         int rt = 0;
578
579         data->light_count = 0;
580         data->light_buffer = 0;
581         first_value = true;
582
583         mutex_lock(&data->mutex);
584
585         if (data->enabled) {
586                 rt = queue_delayed_work(data->wq, &data->work, 0);
587                 gprintk(": The timer is started.\n");
588         }
589
590         mutex_unlock(&data->mutex);
591
592         return rt;
593 }
594
595 static int lightsensor_freeze(struct device *dev)
596 {
597         struct platform_device *pdev = to_platform_device(dev);
598         struct sensor_data *data = platform_get_drvdata(pdev);
599         int rt = 0;
600
601         if (data->enabled) {
602                 cancel_delayed_work_sync(&data->work);
603                 gprintk("timer canceled.\n");
604                 rt = lightsensor_onoff(0);
605         }
606
607         return rt;
608 }
609
610 static int lightsensor_restore(struct device *dev)
611 {
612         struct platform_device *pdev = to_platform_device(dev);
613         struct sensor_data *data = platform_get_drvdata(pdev);
614         int rt = 0;
615
616         if (data->enabled) {
617                 lightsensor_onoff(1);
618                 rt = queue_delayed_work(data->wq, &data->work, 0);
619                 gprintk("timer started.\n");
620         }
621
622         return rt;
623 }
624
625 static struct dev_pm_ops lightsensor_dev_pm_ops = {
626         .suspend        = lightsensor_suspend,
627         .resume         = lightsensor_resume,
628         .freeze         = lightsensor_freeze,
629         .restore        = lightsensor_restore,
630 };
631
632 #define LIGHTSENSOR_DEV_PM_OPS  (&lightsensor_dev_pm_ops)
633 #else
634 #define LIGHTSENSOR_DEV_PM_OPS  NULL
635 #endif /* CONFIG_PM */
636
637 /*
638  * Module init and exit
639  */
640 static struct platform_driver lightsensor_driver = {
641         .probe = lightsensor_probe,
642         .remove = lightsensor_remove,
643         .shutdown = lightsensor_shutdown,
644         .driver = {
645                    .name = SENSOR_NAME,
646                    .owner = THIS_MODULE,
647                    .pm = LIGHTSENSOR_DEV_PM_OPS,
648                    },
649 };
650
651 /* TODO     Need to check if we need this or not.*/
652 /*
653 #ifdef CONFIG_BATTERY_SEC
654 extern unsigned int is_lpcharging_state(void);
655 #endif
656 */
657
658 static int __init lightsensor_init(void)
659 {
660 /*
661 #ifdef CONFIG_BATTERY_SEC
662         if (is_lpcharging_state()) {
663                 pr_info("%s : LPM Charging Mode! return 0\n", __func__);
664                 return 0;
665         }
666 #endif
667 */
668
669         return platform_driver_register(&lightsensor_driver);
670 }
671
672 module_init(lightsensor_init);
673
674 static void __exit lightsensor_exit(void)
675 {
676         platform_driver_unregister(&lightsensor_driver);
677 }
678
679 module_exit(lightsensor_exit);
680
681 MODULE_AUTHOR("SAMSUNG");
682 MODULE_DESCRIPTION("Optical Sensor driver for GP2AP020A00F");
683 MODULE_LICENSE("GPL");