Merge tag 'v4.4.3'
[firefly-linux-kernel-4.4.55.git] / drivers / input / misc / isl29028.c
1 /* drivers/input/misc/isl29028.c
2  *
3  * Copyright (C) 2010 ROCK-CHIPS, Inc.
4  * Author: eric <linjh@rock-chips.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/i2c.h>
20 #include <linux/irq.h>
21 #include <linux/gpio.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/fs.h>
25 #include <linux/uaccess.h>
26 #include <linux/miscdevice.h>
27 #include <linux/circ_buf.h>
28 #include <linux/interrupt.h>
29 #include "isl29028.h"
30 #include <linux/slab.h>
31
32 #ifdef CONFIG_HAS_EARLYSUSPEND
33 #include <linux/earlysuspend.h>
34 #endif
35
36 #include <linux/delay.h>
37 #include <linux/wait.h>
38
39 #define ISL_DBG
40 #undef ISL_DBG
41
42 #ifdef ISL_DBG
43  #define D(x...) printk(x)
44 #else
45  #define D(x...)
46 #endif
47
48 #define ISL_REG_CONFIG    (0x01)
49 #define PROX_EN           (1 << 7)
50 #define PROX_SLP(x)       (x << 4)
51 #define PROX_DR_110       (0 << 3)
52 #define PROX_DR_220       (1 << 3)
53 #define ALS_EN            (1 << 2)
54 #define ALS_RANGE_H       (1 << 1)
55 #define ALS_RANGE_L       (0 << 1)
56 #define ALS_MODE          (1 << 0)
57
58 #define ISL_REG_INT   (0x02)
59 #define PROX_FLAG_MASK ~(1 << 7)
60 #define LS_FLAG_MASK ~(1 << 3)
61 #define PROX_PRST(x)  (x << 5)
62 #define ALS_PRST(x)   (x << 1)
63 #define INT_AND       (1 << 0)
64 #define INT_OR        (0 << 0)
65
66 #define ISL_REG_PROX_LT       (0x03)
67 #define ISL_REG_PROX_HT       (0x04)
68 #define PROX_LT               (0x90)
69 #define PROX_HT               (0xA0)
70
71 #define ISL_REG_PROX_DATA     (0x08)
72 #define ISL_REG_ALSIR_LDATA       (0x09)
73 #define ISL_REG_ALSIR_HDATA       (0x0a)
74
75
76 #define ISL_REG_ALSIR_TH1       (0x05)
77 #define ISL_REG_ALSIR_TH2       (0x06)
78 #define ISL_REG_ALSIR_TH3       (0x07)
79
80 struct isl29028_data {
81         struct input_dev         *psensor_input_dev;
82         struct input_dev         *lsensor_input_dev;
83         struct i2c_client        *client;
84         struct delayed_work      p_work; //for psensor
85         struct delayed_work     l_work; //for light sensor
86         struct mutex lock;
87         int enabled;
88         int irq;
89 };
90
91 #ifdef CONFIG_HAS_EARLYSUSPEND
92 static struct early_suspend isl29028_early_suspend;
93 #endif
94
95 static irqreturn_t isl29028_psensor_irq_handler(int irq, void *data);
96
97
98 int g_lightlevel = 8;
99
100 static const int luxValues[8] = {
101                 10, 160, 225, 320,
102                 640, 1280, 2600, 4095
103 };
104
105
106 static int isl29028_read_reg(struct i2c_client *client, char reg, char *value)
107 {
108         int ret = 0;
109         struct i2c_msg msg[2];
110         struct i2c_adapter *adap = client->adapter;
111
112         msg[0].addr  = client->addr;
113         msg[0].flags = client->flags;
114         msg[0].len = 1;
115         msg[0].buf = (char *)&reg;
116         msg[0].scl_rate = 400 * 1000;
117
118         msg[1].addr  = client->addr;
119         msg[1].flags = client->flags | I2C_M_RD;
120         msg[1].len = 1;
121         msg[1].buf = (char *)value;
122         msg[1].scl_rate = 400 * 1000;
123
124         if ((ret = i2c_transfer(adap, (struct i2c_msg *)&msg, 2)) < 2) {
125                 D("%s: read isl29028 register  %#x failure\n", __FUNCTION__, reg);
126                 return -EIO;
127         }
128
129         return 1;
130 }
131
132 static int isl29028_write_reg(struct i2c_client *client, char reg, char value)
133 {
134         int ret = 0;
135         char buf[2];
136         struct i2c_msg msg;
137         struct i2c_adapter *adap = client->adapter;
138
139         buf[0] = reg;
140         buf[1] = value;
141
142         msg.addr  = client->addr;
143         msg.flags = client->flags;
144         msg.len = 2;
145         msg.buf = (char *)&buf;
146         msg.scl_rate = 400 * 1000;
147
148
149         if ((ret = i2c_transfer(adap, (struct i2c_msg *)&msg, 1)) < 1) {
150                 D("%s: read isl29028 register  %#x failure\n", __FUNCTION__, reg);
151                 return -EIO;
152         }
153
154         return 1;
155 }
156
157 static void isl29028_psensor_work_handler(struct work_struct *work)
158 {
159 #if 1
160         struct isl29028_data *isl = (struct isl29028_data *)container_of(work, struct isl29028_data, p_work.work);
161         int rc;
162
163         if (gpio_get_value(isl->client->irq)) {
164                 D("line %d, input_report_abs 0 \n", __LINE__);
165                 input_report_abs(isl->psensor_input_dev, ABS_DISTANCE, 1);
166                 input_sync(isl->psensor_input_dev);
167                 free_irq(isl->irq, (void *)isl);
168                 rc = request_irq(isl->irq, isl29028_psensor_irq_handler,
169                                                 IRQ_TYPE_EDGE_FALLING, isl->client->name, (void *)isl);
170                 if (rc < 0) {
171                         dev_err(&(isl->client->dev),"request_irq failed for gpio %d (%d)\n", isl->client->irq, rc);
172                 }
173         }
174         else {
175                 D("line %d, input_report_abs 0 \n", __LINE__);
176                 input_report_abs(isl->psensor_input_dev, ABS_DISTANCE, 0);
177                 input_sync(isl->psensor_input_dev);
178                 free_irq(isl->irq, (void *)isl);
179                 rc = request_irq(isl->irq, isl29028_psensor_irq_handler,
180                                                 IRQ_TYPE_EDGE_RISING, isl->client->name, (void *)isl);
181                 if (rc < 0) {
182                         dev_err(&(isl->client->dev),"request_irq failed for gpio %d (%d)\n", isl->client->irq, rc);
183                 }
184         }
185
186         return ;
187 #else
188         struct isl29028_data *isl = (struct isl29028_data *)container_of(work, struct isl29028_data, p_work.work);
189         char reg, value, int_flag;
190
191         mutex_lock(&isl->lock);
192         reg = ISL_REG_INT;
193         isl29028_read_reg(isl->client, reg, (char *)&int_flag);
194
195         if (!(int_flag >> 7)) {
196                 D("line %d: input_report_abs 1 \n", __LINE__);
197                 input_report_abs(isl->psensor_input_dev, ABS_DISTANCE, 1);
198                 input_sync(isl->psensor_input_dev);
199         }
200
201         if (int_flag & 0x08) {
202                 D("line %d; light sensor interrupt\n", __LINE__);
203                 isl29028_write_reg(isl->client, reg, int_flag & 0xf7);          
204         }
205
206         reg = ISL_REG_PROX_DATA;
207         isl29028_read_reg(isl->client, reg, (char *)&value);
208         //D("%s: int is %#x\n", __FUNCTION__, int_flag);
209         D("%s: prox_int is %d\n", __FUNCTION__, (int_flag >> 7 ));
210         D("%s: prox_data is %#x\n", __FUNCTION__, value);
211         mutex_unlock(&isl->lock);
212
213         enable_irq(isl->irq);
214
215 #endif
216 }
217
218 static irqreturn_t isl29028_psensor_irq_handler(int irq, void *data)
219 {
220 #if 1
221         struct isl29028_data *isl = (struct isl29028_data *)data;
222         //disable_irq_nosync(isl->irq);
223         schedule_delayed_work(&isl->p_work, msecs_to_jiffies(420));
224         return IRQ_HANDLED;
225 #else
226         struct isl29028_data *isl = (struct isl29028_data *)data;
227         D("line %d, input_report_abs 0 \n", __LINE__);
228         input_report_abs(isl->psensor_input_dev, ABS_DISTANCE, 0);
229         input_sync(isl->psensor_input_dev);
230
231         disable_irq_nosync(isl->irq);
232         schedule_delayed_work(&isl->p_work, msecs_to_jiffies(420));
233
234         return IRQ_HANDLED;
235 #endif
236 }
237
238 static int isl29028_psensor_enable(struct i2c_client *client)
239 {
240         char reg, value, int_flag;
241         int ret;
242         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
243
244         printk("line %d: enter func %s\n", __LINE__, __FUNCTION__);
245
246         mutex_lock(&isl->lock);
247         reg = ISL_REG_CONFIG;
248         ret = isl29028_read_reg(client, reg, &value);
249         value |= PROX_EN;
250         ret = isl29028_write_reg(client, reg, value);
251 #ifdef ISL_DBG
252         ret = isl29028_read_reg(client, reg, &value);
253         D("%s: configure reg value %#x ...\n", __FUNCTION__, value);    
254 #endif
255
256         reg = ISL_REG_INT;
257         isl29028_read_reg(isl->client, reg, (char *)&int_flag);
258         if (!(int_flag >> 7)) {
259                 printk("line %d: input_report_abs 1 \n", __LINE__);
260                 input_report_abs(isl->psensor_input_dev, ABS_DISTANCE, 1);
261                 input_sync(isl->psensor_input_dev);
262         }
263         mutex_unlock(&isl->lock);
264
265         //enable_irq(isl->irq);
266
267         return ret;
268 }
269
270 static int isl29028_psensor_disable(struct i2c_client *client)
271 {
272         char ret, reg, reg2, value, value2;
273         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
274         printk("line %d: enter func %s\n", __LINE__, __FUNCTION__);
275
276         //disable_irq_nosync(isl->irq);
277
278         mutex_lock(&isl->lock);
279
280         reg = ISL_REG_CONFIG;
281         ret = isl29028_read_reg(client, reg, &value);
282         value &= ~PROX_EN;
283         ret = isl29028_write_reg(client, reg, value);
284
285         reg2 = ISL_REG_INT;
286         ret = isl29028_read_reg(client, reg2, &value2);
287         value2 &= PROX_FLAG_MASK;
288         ret = isl29028_write_reg(client, reg2, value2);
289
290 #ifdef ISL_DBG
291         ret = isl29028_read_reg(client, reg, &value);
292         ret = isl29028_read_reg(client, reg2, &value2);
293         D("%s: configure reg value %#x ...\n", __FUNCTION__, value);    
294         D("%s: interrupt reg value %#x ...\n", __FUNCTION__, value2);   
295 #endif
296         mutex_unlock(&isl->lock);
297
298         //disable_irq(isl->irq);
299         cancel_delayed_work_sync(&isl->p_work);
300         //enable_irq(isl->irq);
301
302         return ret;
303 }
304
305 static int isl29028_psensor_open(struct inode *inode, struct file *file);
306 static int isl29028_psensor_release(struct inode *inode, struct file *file);
307 static long isl29028_psensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
308
309 static int misc_ps_opened = 0;
310 static struct file_operations isl29028_psensor_fops = {
311         .owner = THIS_MODULE,
312         .open = isl29028_psensor_open,
313         .release = isl29028_psensor_release,
314         .unlocked_ioctl = isl29028_psensor_ioctl
315 };
316
317 static struct miscdevice isl29028_psensor_misc = {
318         .minor = MISC_DYNAMIC_MINOR,
319         .name = "psensor",
320         .fops = &isl29028_psensor_fops
321 };
322
323 static int isl29028_psensor_open(struct inode *inode, struct file *file)
324 {
325 //      struct i2c_client *client = 
326 //                     container_of (isl29028_psensor_misc.parent, struct i2c_client, dev);
327         D("%s\n", __func__);
328         if (misc_ps_opened)
329                 return -EBUSY;
330         misc_ps_opened = 1;
331         return 0;
332         //return isl29028_psensor_enable(client);
333 }
334
335 static int isl29028_psensor_release(struct inode *inode, struct file *file)
336 {       
337 //      struct i2c_client *client = 
338 //                     container_of (isl29028_psensor_misc.parent, struct i2c_client, dev);
339         D("%s\n", __func__);
340         misc_ps_opened = 0;
341         return 0;
342         //return isl29028_psensor_disable(client);
343 }
344
345 static long isl29028_psensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
346 {
347         char reg, val, enabled;
348         struct i2c_client *client = 
349                        container_of (isl29028_psensor_misc.parent, struct i2c_client, dev);
350         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
351
352         D("%s cmd %d\n", __func__, _IOC_NR(cmd));
353         switch (cmd) {
354         case PSENSOR_IOCTL_ENABLE:
355                 if (get_user(val, (unsigned long __user *)arg))
356                         return -EFAULT;
357                 if (val)
358                         return isl29028_psensor_enable(client);
359                 else
360                         return isl29028_psensor_disable(client);
361                 break;
362         case PSENSOR_IOCTL_GET_ENABLED:
363                 mutex_lock(&isl->lock);
364                 reg = ISL_REG_CONFIG;
365                 isl29028_read_reg(client, reg, &val);
366                 enabled = (val & (1 << 7)) ? 1 : 0;
367                 mutex_unlock(&isl->lock);
368                 return put_user(enabled, (unsigned long __user *)arg);
369                 break;
370         default:
371                 pr_err("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd));
372                 return -EINVAL;
373         }
374 }
375
376 static int register_psensor_device(struct i2c_client *client, struct isl29028_data *isl)
377 {
378         struct input_dev *input_dev;
379         int rc;
380
381         D("%s: allocating input device\n", __func__);
382         input_dev = input_allocate_device();
383         if (!input_dev) {
384                 dev_err(&client->dev,"%s: could not allocate input device for psensor\n", __FUNCTION__);
385                 rc = -ENOMEM;
386                 goto done;
387         }
388         
389         isl->psensor_input_dev = input_dev;
390         input_set_drvdata(input_dev, isl);
391
392         input_dev->name = "proximity";
393
394         set_bit(EV_ABS, input_dev->evbit);
395         input_set_abs_params(input_dev, ABS_DISTANCE, 0, 1, 0, 0);
396
397         D("%s: registering input device\n", __func__);
398         rc = input_register_device(input_dev);
399         if (rc < 0) {
400                 pr_err("%s: could not register input device for psensor\n", __func__);
401                 goto err_free_input_device;
402         }
403
404         D("%s: registering misc device for psensor\n", __func__);
405         rc = misc_register(&isl29028_psensor_misc);
406         if (rc < 0) {
407                 pr_err("%s: could not register misc device\n", __func__);
408                 goto err_unregister_input_device;
409         }
410         
411         isl29028_psensor_misc.parent = &client->dev;
412         //misc_deregister(&isl29028_psensor_misc);
413         
414         INIT_DELAYED_WORK(&isl->p_work, isl29028_psensor_work_handler);
415
416         rc = gpio_request(client->irq, "isl29028 irq");
417         if (rc) {
418                 pr_err("%s: request gpio %d failed \n", __func__, client->irq);
419                 goto err_unregister_misc;
420         }
421         rc = gpio_direction_input(client->irq);
422         if (rc) {
423                 pr_err("%s: failed set gpio input\n", __FUNCTION__);
424         }
425
426         gpio_pull_updown(client->irq, GPIOPullUp);
427         isl->irq = gpio_to_irq(client->irq);
428         //mdelay(1);
429         rc = request_irq(isl->irq, isl29028_psensor_irq_handler,
430                                         IRQ_TYPE_EDGE_FALLING, client->name, (void *)isl);
431         if (rc < 0) {
432                 dev_err(&client->dev,"request_irq failed for gpio %d (%d)\n", client->irq, rc);
433                 goto err_free_gpio;
434         }
435
436         //disable_irq_nosync(isl->irq);
437         
438         return 0;
439
440 err_free_gpio:
441         gpio_free(client->irq);
442 err_unregister_misc:
443         misc_deregister(&isl29028_psensor_misc);
444 err_unregister_input_device:
445         input_unregister_device(input_dev);
446 err_free_input_device:
447         input_free_device(input_dev);
448 done:
449         return rc;
450 }
451
452 static void unregister_psensor_device(struct i2c_client *client, struct isl29028_data *isl)
453 {
454         misc_deregister(&isl29028_psensor_misc);
455         input_unregister_device(isl->psensor_input_dev);
456         input_free_device(isl->psensor_input_dev);
457 }
458
459 #define LSENSOR_POLL_PROMESHUTOK   1000
460
461 static int isl29028_lsensor_enable(struct i2c_client *client)
462 {
463         char reg, value;
464         int ret;
465         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
466
467         mutex_lock(&isl->lock);
468
469         reg = ISL_REG_CONFIG;
470         ret = isl29028_read_reg(client, reg, &value);
471         value |= ALS_EN;
472         ret = isl29028_write_reg(client, reg, value);
473         
474 #ifdef ISL_DBG
475         ret = isl29028_read_reg(client, reg, &value);
476         D("%s: configure reg value %#x ...\n", __FUNCTION__, value);    
477 #endif
478
479         mutex_unlock(&isl->lock);
480
481         schedule_delayed_work(&(isl->l_work), msecs_to_jiffies(LSENSOR_POLL_PROMESHUTOK));
482
483         return ret;
484 }
485
486 static int isl29028_lsensor_disable(struct i2c_client *client)
487 {
488         char ret, reg, reg2, value, value2;
489         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
490
491         cancel_delayed_work_sync(&(isl->l_work));
492
493         mutex_lock(&isl->lock);
494
495         reg = ISL_REG_CONFIG;
496         ret = isl29028_read_reg(client, reg, &value);
497         value &= ~ALS_EN;
498         ret = isl29028_write_reg(client, reg, value);
499
500         reg2 = ISL_REG_INT;
501         ret = isl29028_read_reg(client, reg2, &value2);
502         value2 &= LS_FLAG_MASK;
503         ret = isl29028_write_reg(client, reg2, value2);
504
505 #ifdef ISL_DBG
506         ret = isl29028_read_reg(client, reg, &value);
507         ret = isl29028_read_reg(client, reg2, &value2);
508         D("%s: configure reg value %#x ...\n", __FUNCTION__, value);    
509         D("%s: interrupt reg value %#x ...\n", __FUNCTION__, value2);   
510 #endif
511
512         mutex_unlock(&isl->lock);
513
514         return ret;
515 }
516
517 static int luxValue_to_level(int value)
518 {
519         int i;
520         if (value >= luxValues[7])
521                 return 7;
522         if (value <= luxValues[0])
523                 return 0;
524         for (i=0;i<7;i++)
525                 if (value>=luxValues[i] && value<luxValues[i+1])
526                         return i;
527         return -1;
528 }
529
530 static void isl29028_lsensor_work_handler(struct work_struct *work)
531 {
532         struct isl29028_data *isl = (struct isl29028_data *)container_of(work, struct isl29028_data, l_work.work);
533         char reg, l_value, h_value;
534         unsigned int als_value;
535         int level;
536
537         mutex_lock(&isl->lock);
538         reg = ISL_REG_ALSIR_LDATA;
539         isl29028_read_reg(isl->client, reg, (char *)&l_value);
540
541         reg = ISL_REG_ALSIR_HDATA;
542         isl29028_read_reg(isl->client, reg, (char *)&h_value);
543
544         mutex_unlock(&isl->lock);
545
546         als_value = h_value;
547         als_value = (als_value << 8) | l_value;
548
549 #ifdef ISL_DBG 
550         D("%s: ls_data is %#x\n", __FUNCTION__, als_value);
551 #endif
552
553         level = luxValue_to_level(als_value);
554
555 #ifdef ISL_DBG 
556         D("%s: ls_level is %d\n", __FUNCTION__, level);
557 #endif
558
559         if (level != g_lightlevel) {
560                 g_lightlevel = level;
561                 input_report_abs(isl->lsensor_input_dev, ABS_MISC, level);
562                 input_sync(isl->lsensor_input_dev);
563         }
564         schedule_delayed_work(&(isl->l_work), msecs_to_jiffies(LSENSOR_POLL_PROMESHUTOK));
565 }
566
567 static int isl29028_lsensor_open(struct inode *inode, struct file *file);
568 static int isl29028_lsensor_release(struct inode *inode, struct file *file);
569 static long isl29028_lsensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
570
571 static int misc_ls_opened = 0;
572 static struct file_operations isl29028_lsensor_fops = {
573         .owner = THIS_MODULE,
574         .open = isl29028_lsensor_open,
575         .release = isl29028_lsensor_release,
576         .unlocked_ioctl = isl29028_lsensor_ioctl
577 };
578
579 static struct miscdevice isl29028_lsensor_misc = {
580         .minor = MISC_DYNAMIC_MINOR,
581         .name = "lightsensor",
582         .fops = &isl29028_lsensor_fops
583 };
584
585 static int isl29028_lsensor_open(struct inode *inode, struct file *file)
586 {
587 //      struct i2c_client *client = 
588 //                     container_of (isl29028_lsensor_misc.parent, struct i2c_client, dev);
589         D("%s\n", __func__);
590         if (misc_ls_opened)
591                 return -EBUSY;
592         misc_ls_opened = 1;
593         return 0;
594         //return isl29028_lsensor_enable(client);
595 }
596
597 static int isl29028_lsensor_release(struct inode *inode, struct file *file)
598 {
599         
600 //      struct i2c_client *client = 
601 //                     container_of (isl29028_lsensor_misc.parent, struct i2c_client, dev);
602         D("%s\n", __func__);
603         misc_ls_opened = 0;
604         return 0;
605         //return isl29028_lsensor_disable(client);
606 }
607
608 static long isl29028_lsensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
609 {
610         char reg, val, enabled;
611         struct i2c_client *client = 
612                        container_of (isl29028_lsensor_misc.parent, struct i2c_client, dev);
613         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
614
615         D("%s cmd %d\n", __func__, _IOC_NR(cmd));
616         switch (cmd) {
617         case LIGHTSENSOR_IOCTL_ENABLE:
618                 if (get_user(val, (unsigned long __user *)arg))
619                         return -EFAULT;
620                 if (val)
621                         return isl29028_lsensor_enable(client);
622                 else
623                         return isl29028_lsensor_disable(client);
624                 break;
625         case LIGHTSENSOR_IOCTL_GET_ENABLED:
626                 mutex_lock(&isl->lock);
627                 reg = ISL_REG_CONFIG;
628                 isl29028_read_reg(client, reg, &val);
629                 mutex_unlock(&isl->lock);
630                 enabled = (val & (1 << 2)) ? 1 : 0;
631                 return put_user(enabled, (unsigned long __user *)arg);
632                 break;
633         default:
634                 pr_err("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd));
635                 return -EINVAL;
636         }
637 }
638
639 static int register_lsensor_device(struct i2c_client *client, struct isl29028_data *isl)
640 {
641         struct input_dev *input_dev;
642         int rc;
643
644         D("%s: allocating input device\n", __func__);
645         input_dev = input_allocate_device();
646         if (!input_dev) {
647                 dev_err(&client->dev,"%s: could not allocate input device forlsensor\n", __FUNCTION__);
648                 rc = -ENOMEM;
649                 goto done;
650         }
651         
652         isl->lsensor_input_dev = input_dev;
653         input_set_drvdata(input_dev, isl);
654
655         input_dev->name = "lightsensor-level";
656
657         set_bit(EV_ABS, input_dev->evbit);
658         input_set_abs_params(input_dev, ABS_MISC, 0, 8, 0, 0);
659
660         D("%s: registering input device\n", __func__);
661         rc = input_register_device(input_dev);
662         if (rc < 0) {
663                 pr_err("%s: could not register input device for lsensor\n", __func__);
664                 goto err_free_input_device;
665         }
666
667         D("%s: registering misc device for lsensor\n", __func__);
668         rc = misc_register(&isl29028_lsensor_misc);
669         if (rc < 0) {
670                 pr_err("%s: could not register misc device\n", __func__);
671                 goto err_unregister_input_device;
672         }
673         
674         isl29028_lsensor_misc.parent = &client->dev;
675
676         INIT_DELAYED_WORK(&isl->l_work, isl29028_lsensor_work_handler);
677         
678         return 0;
679
680 err_unregister_input_device:
681         input_unregister_device(input_dev);
682 err_free_input_device:
683         input_free_device(input_dev);
684 done:
685         return rc;
686 }
687
688 static void unregister_lsensor_device(struct i2c_client *client, struct isl29028_data *isl)
689 {
690         misc_deregister(&isl29028_lsensor_misc);
691         input_unregister_device(isl->lsensor_input_dev);
692         input_free_device(isl->lsensor_input_dev);
693 }
694
695 static int isl29028_config(struct i2c_client *client)
696 {
697         int ret;
698         char value, buf[2];
699         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
700
701         D("%s: init isl29028 all register\n", __func__);
702
703         mutex_lock(&isl->lock);
704
705         /*********************** power on **************************/
706         buf[0] = 0x0e;
707         buf[1] = 0x00; 
708         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
709                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
710         }
711         
712         buf[0] = 0x0f;
713         buf[1] = 0x00; 
714         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
715                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
716         }
717
718         buf[0] = ISL_REG_CONFIG;
719         buf[1] = 0x00; 
720         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
721                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
722         }
723
724         if (ret < 2) {
725                 mutex_unlock(&isl->lock);
726                 return -1;
727         }
728         mdelay(2);
729         
730     /***********************config**************************/
731
732         buf[0] = ISL_REG_CONFIG;
733         buf[1] = /*PROX_EN | */PROX_SLP(4) | PROX_DR_220 | ALS_RANGE_H /*| ALS_EN */; 
734         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
735                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
736         }
737 #ifdef ISL_DBG
738         isl29028_read_reg(client, 0x01, &value);
739         printk("%s: config isl29028 CONFIGURE(0x01) reg %#x \n", __FUNCTION__, value);
740 #endif
741
742         buf[0] = ISL_REG_INT;
743         buf[1] = PROX_PRST(1) | ALS_PRST(3); 
744         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
745                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
746         }
747 #ifdef ISL_DBG
748         isl29028_read_reg(client, 0x02, &value);
749         printk("%s: config isl29028 INTERRUPT(0x02) reg %#x \n", __FUNCTION__, value);
750 #endif
751
752         buf[0] = ISL_REG_PROX_LT;
753         buf[1] = PROX_LT; 
754         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
755                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
756         }
757 #ifdef ISL_DBG
758         isl29028_read_reg(client, 0x03, &value);
759         printk("%s: config isl29028 PROX_LT(0x03) reg %#x \n", __FUNCTION__, value);
760 #endif
761
762         buf[0] = ISL_REG_PROX_HT;
763         buf[1] = PROX_HT; 
764         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
765                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
766         }
767 #ifdef ISL_DBG
768         isl29028_read_reg(client, 0x04, &value);
769         printk("%s: config isl29028 PROX_HT(0x04) reg %#x \n", __FUNCTION__, value);
770 #endif
771
772         buf[0] = ISL_REG_ALSIR_TH1;
773         buf[1] = 0x0;
774         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
775                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
776         }
777
778         buf[0] = ISL_REG_ALSIR_TH2;
779         buf[1] = 0xF0;
780         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
781                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
782         }
783         
784         buf[0] = ISL_REG_ALSIR_TH3;
785         buf[1] = 0xFF;
786         if ((ret = i2c_master_send(client, buf, 2)) < 2) {
787                 printk("%s: config isl29028 register %#x err %d\n", __FUNCTION__, buf[0], ret);
788         }
789         
790         mutex_unlock(&isl->lock);
791
792         return 0;
793 }
794
795 #ifdef CONFIG_HAS_EARLYSUSPEND
796 static void isl29028_suspend(struct early_suspend *h)
797 {
798         struct i2c_client *client = container_of(isl29028_psensor_misc.parent, struct i2c_client, dev);
799         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
800         
801         D("isl29028 early suspend ========================= \n");
802         if (misc_ps_opened)
803                 enable_irq_wake(isl->irq);
804 //              isl29028_psensor_disable(client);       
805         if (misc_ls_opened)
806                 isl29028_lsensor_disable(client);       
807 }
808
809 static void isl29028_resume(struct early_suspend *h)
810 {
811         struct i2c_client *client = container_of(isl29028_psensor_misc.parent, struct i2c_client, dev);
812         struct isl29028_data *isl = (struct isl29028_data *)i2c_get_clientdata(client);
813         
814     D("isl29028 early resume ======================== \n"); 
815         if (misc_ps_opened)
816                 disable_irq_wake(isl->irq);
817 //              isl29028_psensor_enable(client);        
818         if (misc_ls_opened)
819                 isl29028_lsensor_enable(client);        
820 }
821 #else
822 #define isl29028_suspend NULL
823 #define isl29028_resume NULL
824 #endif
825
826 static int isl29028_probe(struct i2c_client *client, const struct i2c_device_id *id)
827 {
828         int rc = -EIO;
829         struct isl29028_data *isl;
830
831         D("\n%s: isl29028 i2c client probe\n\n", __func__);
832
833         isl = kzalloc(sizeof(struct isl29028_data), GFP_KERNEL);
834         if (isl == NULL) {
835                 rc = -ENOMEM;
836                 dev_err(&client->dev, "failed to allocate driver data\n");
837                 goto done;
838         }
839
840         isl->client = client;
841         i2c_set_clientdata(client, isl);
842
843         
844         mutex_init(&isl->lock);
845
846         rc = register_psensor_device(client, isl);
847         if (rc) {
848                 dev_err(&client->dev, "failed to register_psensor_device\n");
849                 goto err_free_mem;
850         }
851         
852         rc = register_lsensor_device(client, isl);
853         if (rc) {
854                 dev_err(&client->dev, "failed to register_lsensor_device\n");
855                 goto unregister_device1;
856         }
857
858         rc = isl29028_config(client);
859         if (rc) {
860                 dev_err(&client->dev, "failed to isl29028_config\n");
861                 goto unregister_device2;
862         }
863
864 #ifdef CONFIG_HAS_EARLYSUSPEND
865         isl29028_early_suspend.suspend = isl29028_suspend;
866         isl29028_early_suspend.resume  = isl29028_resume;
867         isl29028_early_suspend.level   = 0x02;
868         register_early_suspend(&isl29028_early_suspend);
869 #endif
870
871         //isl29028_psensor_enable(client);
872         //isl29028_lsensor_enable(client);
873
874         return 0;
875
876 unregister_device2:
877         unregister_lsensor_device(client, isl);
878 unregister_device1:
879         unregister_psensor_device(client, isl);
880 err_free_mem:
881         kfree(isl);
882 done:
883         return rc;
884 }
885
886 static int isl29028_remove(struct i2c_client *client)
887 {
888         struct isl29028_data *isl29028 = i2c_get_clientdata(client);
889         
890         unregister_lsensor_device(client, isl29028);
891         unregister_psensor_device(client, isl29028);
892     kfree(isl29028); 
893 #ifdef CONFIG_HAS_EARLYSUSPEND
894     unregister_early_suspend(&isl29028_early_suspend);
895 #endif      
896         return 0;
897 }
898
899 static const struct i2c_device_id isl29028_id[] = {
900                 {"isl29028", 0},
901                 { }
902 };
903
904 static struct i2c_driver isl29028_driver = {
905         .driver = {
906                 .name = "isl29028",
907         },
908         .probe    = isl29028_probe,
909         .remove   = isl29028_remove,
910         .id_table = isl29028_id,
911
912 };
913
914 static int __init isl29028_init(void)
915 {
916
917         return i2c_add_driver(&isl29028_driver);
918 }
919
920 static void __exit isl29028_exit(void)
921 {
922         return i2c_del_driver(&isl29028_driver);
923 }
924
925 module_init(isl29028_init);
926 module_exit(isl29028_exit);
927