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