blob: d850a0e3536cc7f1666091ce22d2898b1afe7a53 [file] [log] [blame]
Joseph Hsiao091151d2013-06-17 11:37:05 +08001/* drivers/input/misc/cm36283.c - cm36283 optical sensors driver
2 *
3 * Copyright (C) 2012 Capella Microsystems Inc.
4 * Author: Frank Hsieh <pengyueh@gmail.com>
Oliver Wang8a1b8be2013-08-23 13:42:10 +08005 *
6 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
7 *
Joseph Hsiao091151d2013-06-17 11:37:05 +08008 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/delay.h>
20#include <linux/earlysuspend.h>
21#include <linux/i2c.h>
22#include <linux/input.h>
23#include <linux/interrupt.h>
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/irq.h>
28#include <linux/errno.h>
29#include <linux/err.h>
30#include <linux/gpio.h>
31#include <linux/miscdevice.h>
Joseph Hsiao091151d2013-06-17 11:37:05 +080032#include <linux/slab.h>
Oliver Wanga9be2d22013-08-22 18:08:22 +080033#include <linux/regulator/consumer.h>
Joseph Hsiao091151d2013-06-17 11:37:05 +080034#include <linux/wakelock.h>
35#include <linux/jiffies.h>
Oliver Wanga9be2d22013-08-22 18:08:22 +080036#include <linux/cm36283.h>
Oliver Wang8a1b8be2013-08-23 13:42:10 +080037#include <linux/of_gpio.h>
Oliver Wanga9be2d22013-08-22 18:08:22 +080038
39#include <asm/uaccess.h>
40#include <asm/mach-types.h>
41#include <asm/setup.h>
Joseph Hsiao091151d2013-06-17 11:37:05 +080042
43#define D(x...) pr_info(x)
44
45#define I2C_RETRY_COUNT 10
46
47#define NEAR_DELAY_TIME ((100 * HZ) / 1000)
48
49#define CONTROL_INT_ISR_REPORT 0x00
50#define CONTROL_ALS 0x01
51#define CONTROL_PS 0x02
52
Oliver Wanga9be2d22013-08-22 18:08:22 +080053/* POWER SUPPLY VOLTAGE RANGE */
54#define CM36283_VDD_MIN_UV 2700000
55#define CM36283_VDD_MAX_UV 3300000
56#define CM36283_VI2C_MIN_UV 1750000
57#define CM36283_VI2C_MAX_UV 1950000
58
Oliver Wanged314dd2013-08-23 16:34:37 +080059/* cm36283 polling rate in ms */
60#define CM36283_LS_MIN_POLL_DELAY 1
61#define CM36283_LS_MAX_POLL_DELAY 1000
62#define CM36283_LS_DEFAULT_POLL_DELAY 100
63
64#define CM36283_PS_MIN_POLL_DELAY 1
65#define CM36283_PS_MAX_POLL_DELAY 1000
66#define CM36283_PS_DEFAULT_POLL_DELAY 100
67
Joseph Hsiao091151d2013-06-17 11:37:05 +080068static int record_init_fail = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +080069
70static const int als_range[] = {
71 [CM36283_ALS_IT0] = 6554,
72 [CM36283_ALS_IT1] = 3277,
73 [CM36283_ALS_IT2] = 1638,
74 [CM36283_ALS_IT3] = 819,
75};
76
77static const int als_sense[] = {
78 [CM36283_ALS_IT0] = 10,
79 [CM36283_ALS_IT1] = 20,
80 [CM36283_ALS_IT2] = 40,
81 [CM36283_ALS_IT3] = 80,
82};
83
Joseph Hsiao091151d2013-06-17 11:37:05 +080084static void sensor_irq_do_work(struct work_struct *work);
85static DECLARE_WORK(sensor_irq_work, sensor_irq_do_work);
86
87struct cm36283_info {
88 struct class *cm36283_class;
89 struct device *ls_dev;
90 struct device *ps_dev;
91
92 struct input_dev *ls_input_dev;
93 struct input_dev *ps_input_dev;
94
95 struct early_suspend early_suspend;
96 struct i2c_client *i2c_client;
97 struct workqueue_struct *lp_wq;
98
99 int intr_pin;
100 int als_enable;
101 int ps_enable;
102 int ps_irq_flag;
103
104 uint16_t *adc_table;
105 uint16_t cali_table[10];
106 int irq;
107
108 int ls_calibrate;
109
110 int (*power)(int, uint8_t); /* power to the chip */
111
112 uint32_t als_kadc;
113 uint32_t als_gadc;
114 uint16_t golden_adc;
115
116 struct wake_lock ps_wake_lock;
117 int psensor_opened;
118 int lightsensor_opened;
119 uint8_t slave_addr;
120
121 uint8_t ps_close_thd_set;
122 uint8_t ps_away_thd_set;
123 int current_level;
124 uint16_t current_adc;
125
126 uint16_t ps_conf1_val;
127 uint16_t ps_conf3_val;
128
129 uint16_t ls_cmd;
130 uint8_t record_clear_int_fail;
Oliver Wanged314dd2013-08-23 16:34:37 +0800131 bool polling;
132 atomic_t ls_poll_delay;
133 atomic_t ps_poll_delay;
Oliver Wanga9be2d22013-08-22 18:08:22 +0800134 struct regulator *vdd;
135 struct regulator *vio;
Oliver Wanged314dd2013-08-23 16:34:37 +0800136 struct delayed_work ldwork;
137 struct delayed_work pdwork;
Joseph Hsiao091151d2013-06-17 11:37:05 +0800138};
139struct cm36283_info *lp_info;
140int fLevel=-1;
141static struct mutex als_enable_mutex, als_disable_mutex, als_get_adc_mutex;
142static struct mutex ps_enable_mutex, ps_disable_mutex, ps_get_adc_mutex;
143static struct mutex CM36283_control_mutex;
Oliver Wanged314dd2013-08-23 16:34:37 +0800144static struct mutex wq_lock;
Joseph Hsiao091151d2013-06-17 11:37:05 +0800145static int lightsensor_enable(struct cm36283_info *lpi);
146static int lightsensor_disable(struct cm36283_info *lpi);
147static int initial_cm36283(struct cm36283_info *lpi);
148static void psensor_initial_cmd(struct cm36283_info *lpi);
Oliver Wang0afd6a32013-08-26 14:23:51 +0800149static int cm36283_power_set(struct cm36283_info *info, bool on);
Joseph Hsiao091151d2013-06-17 11:37:05 +0800150
151int32_t als_kadc;
152
Oliver Wanged314dd2013-08-23 16:34:37 +0800153static int control_and_report(struct cm36283_info *lpi, uint8_t mode,
154 uint16_t param, int report);
Joseph Hsiao091151d2013-06-17 11:37:05 +0800155
156static int I2C_RxData(uint16_t slaveAddr, uint8_t cmd, uint8_t *rxData, int length)
157{
158 uint8_t loop_i;
159 int val;
160 struct cm36283_info *lpi = lp_info;
161 uint8_t subaddr[1];
162
163 struct i2c_msg msgs[] = {
164 {
165 .addr = slaveAddr,
166 .flags = 0,
167 .len = 1,
168 .buf = subaddr,
169 },
170 {
171 .addr = slaveAddr,
172 .flags = I2C_M_RD,
173 .len = length,
174 .buf = rxData,
175 },
176 };
177 subaddr[0] = cmd;
178
179 for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
180
181 if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 2) > 0)
182 break;
183
184 val = gpio_get_value(lpi->intr_pin);
185 /*check intr GPIO when i2c error*/
186 if (loop_i == 0 || loop_i == I2C_RETRY_COUNT -1)
187 D("[PS][CM36283 error] %s, i2c err, slaveAddr 0x%x ISR gpio %d = %d, record_init_fail %d \n",
188 __func__, slaveAddr, lpi->intr_pin, val, record_init_fail);
189
190 msleep(10);
191 }
192 if (loop_i >= I2C_RETRY_COUNT) {
193 printk(KERN_ERR "[PS_ERR][CM36283 error] %s retry over %d\n",
194 __func__, I2C_RETRY_COUNT);
195 return -EIO;
196 }
197
198 return 0;
199}
200
201static int I2C_TxData(uint16_t slaveAddr, uint8_t *txData, int length)
202{
203 uint8_t loop_i;
204 int val;
205 struct cm36283_info *lpi = lp_info;
206 struct i2c_msg msg[] = {
207 {
208 .addr = slaveAddr,
209 .flags = 0,
210 .len = length,
211 .buf = txData,
212 },
213 };
214
215 for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
216 if (i2c_transfer(lp_info->i2c_client->adapter, msg, 1) > 0)
217 break;
218
219 val = gpio_get_value(lpi->intr_pin);
220 /*check intr GPIO when i2c error*/
221 if (loop_i == 0 || loop_i == I2C_RETRY_COUNT -1)
222 D("[PS][CM36283 error] %s, i2c err, slaveAddr 0x%x, value 0x%x, ISR gpio%d = %d, record_init_fail %d\n",
223 __func__, slaveAddr, txData[0], lpi->intr_pin, val, record_init_fail);
224
225 msleep(10);
226 }
227
228 if (loop_i >= I2C_RETRY_COUNT) {
229 printk(KERN_ERR "[PS_ERR][CM36283 error] %s retry over %d\n",
230 __func__, I2C_RETRY_COUNT);
231 return -EIO;
232 }
233
234 return 0;
235}
236
237static int _cm36283_I2C_Read_Word(uint16_t slaveAddr, uint8_t cmd, uint16_t *pdata)
238{
239 uint8_t buffer[2];
240 int ret = 0;
241
242 if (pdata == NULL)
243 return -EFAULT;
244
245 ret = I2C_RxData(slaveAddr, cmd, buffer, 2);
246 if (ret < 0) {
247 pr_err(
248 "[PS_ERR][CM3218 error]%s: I2C_RxData fail [0x%x, 0x%x]\n",
249 __func__, slaveAddr, cmd);
250 return ret;
251 }
252
253 *pdata = (buffer[1]<<8)|buffer[0];
254#if 0
255 /* Debug use */
256 printk(KERN_DEBUG "[CM3218] %s: I2C_RxData[0x%x, 0x%x] = 0x%x\n",
257 __func__, slaveAddr, cmd, *pdata);
258#endif
259 return ret;
260}
261
262static int _cm36283_I2C_Write_Word(uint16_t SlaveAddress, uint8_t cmd, uint16_t data)
263{
264 char buffer[3];
265 int ret = 0;
266#if 0
267 /* Debug use */
268 printk(KERN_DEBUG
269 "[CM3218] %s: _cm36283_I2C_Write_Word[0x%x, 0x%x, 0x%x]\n",
270 __func__, SlaveAddress, cmd, data);
271#endif
272 buffer[0] = cmd;
273 buffer[1] = (uint8_t)(data&0xff);
274 buffer[2] = (uint8_t)((data&0xff00)>>8);
275
276 ret = I2C_TxData(SlaveAddress, buffer, 3);
277 if (ret < 0) {
278 pr_err("[PS_ERR][CM3218 error]%s: I2C_TxData fail\n", __func__);
279 return -EIO;
280 }
281
282 return ret;
283}
284
285static int get_ls_adc_value(uint16_t *als_step, bool resume)
286{
287 struct cm36283_info *lpi = lp_info;
288 uint32_t tmpResult;
289 int ret = 0;
290
291 if (als_step == NULL)
292 return -EFAULT;
293
294 /* Read ALS data: */
295 ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_DATA, als_step);
296 if (ret < 0) {
297 pr_err(
298 "[LS][CM3218 error]%s: _cm36283_I2C_Read_Word fail\n",
299 __func__);
300 return -EIO;
301 }
302
303 if (!lpi->ls_calibrate) {
304 tmpResult = (uint32_t)(*als_step) * lpi->als_gadc / lpi->als_kadc;
305 if (tmpResult > 0xFFFF)
306 *als_step = 0xFFFF;
307 else
308 *als_step = tmpResult;
309 }
310
311 D("[LS][CM3218] %s: raw adc = 0x%X, ls_calibrate = %d\n",
312 __func__, *als_step, lpi->ls_calibrate);
313
314 return ret;
315}
316
317static int set_lsensor_range(uint16_t low_thd, uint16_t high_thd)
318{
319 int ret = 0;
320 struct cm36283_info *lpi = lp_info;
321
322 _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDH, high_thd);
323 _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDL, low_thd);
324
325 return ret;
326}
327
328static int get_ps_adc_value(uint16_t *data)
329{
330 int ret = 0;
331 struct cm36283_info *lpi = lp_info;
332
333 if (data == NULL)
334 return -EFAULT;
335
336 ret = _cm36283_I2C_Read_Word(lpi->slave_addr, PS_DATA, data);
337
338 (*data) &= 0xFF;
339
340 if (ret < 0) {
341 pr_err(
342 "[PS][CM36283 error]%s: _cm36283_I2C_Read_Word fail\n",
343 __func__);
344 return -EIO;
345 } else {
346 pr_err(
347 "[PS][CM36283 OK]%s: _cm36283_I2C_Read_Word OK 0x%x\n",
348 __func__, *data);
349 }
350
351 return ret;
352}
353
354static uint16_t mid_value(uint16_t value[], uint8_t size)
355{
356 int i = 0, j = 0;
357 uint16_t temp = 0;
358
359 if (size < 3)
360 return 0;
361
362 for (i = 0; i < (size - 1); i++)
363 for (j = (i + 1); j < size; j++)
364 if (value[i] > value[j]) {
365 temp = value[i];
366 value[i] = value[j];
367 value[j] = temp;
368 }
369 return value[((size - 1) / 2)];
370}
371
372static int get_stable_ps_adc_value(uint16_t *ps_adc)
373{
374 uint16_t value[3] = {0, 0, 0}, mid_val = 0;
375 int ret = 0;
376 int i = 0;
377 int wait_count = 0;
378 struct cm36283_info *lpi = lp_info;
379
380 for (i = 0; i < 3; i++) {
381 /*wait interrupt GPIO high*/
382 while (gpio_get_value(lpi->intr_pin) == 0) {
383 msleep(10);
384 wait_count++;
385 if (wait_count > 12) {
386 pr_err("[PS_ERR][CM36283 error]%s: interrupt GPIO low,"
387 " get_ps_adc_value\n", __func__);
388 return -EIO;
389 }
390 }
391
392 ret = get_ps_adc_value(&value[i]);
393 if (ret < 0) {
394 pr_err("[PS_ERR][CM36283 error]%s: get_ps_adc_value\n",
395 __func__);
396 return -EIO;
397 }
398
399 if (wait_count < 60/10) {/*wait gpio less than 60ms*/
400 msleep(60 - (10*wait_count));
401 }
402 wait_count = 0;
403 }
404
405 /*D("Sta_ps: Before sort, value[0, 1, 2] = [0x%x, 0x%x, 0x%x]",
406 value[0], value[1], value[2]);*/
407 mid_val = mid_value(value, 3);
408 D("Sta_ps: After sort, value[0, 1, 2] = [0x%x, 0x%x, 0x%x]",
409 value[0], value[1], value[2]);
410 *ps_adc = (mid_val & 0xFF);
411
412 return 0;
413}
414
415static void sensor_irq_do_work(struct work_struct *work)
416{
417 struct cm36283_info *lpi = lp_info;
418 uint16_t intFlag;
Oliver Wanged314dd2013-08-23 16:34:37 +0800419 _cm36283_I2C_Read_Word(lpi->slave_addr, INT_FLAG, &intFlag);
420 control_and_report(lpi, CONTROL_INT_ISR_REPORT, intFlag, 1);
421
Joseph Hsiao091151d2013-06-17 11:37:05 +0800422 enable_irq(lpi->irq);
423}
424
Oliver Wanged314dd2013-08-23 16:34:37 +0800425static int get_als_range(void)
426{
427 uint16_t ls_conf;
428 int ret = 0;
429 int index = 0;
430 struct cm36283_info *lpi = lp_info;
431
432 ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_CONF, &ls_conf);
433 if (ret) {
434 dev_err(&lpi->i2c_client->dev, "read ALS_CONF from i2c error. %d\n",
435 ret);
436 return -EIO;
437 }
438
439 index = (ls_conf & 0xC0) >> 0x06;
440 return als_range[index];
441}
442
443static int get_als_sense(void)
444{
445 uint16_t ls_conf;
446 int ret = 0;
447 int index = 0;
448 struct cm36283_info *lpi = lp_info;
449
450 ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_CONF, &ls_conf);
451 if (ret) {
452 dev_err(&lpi->i2c_client->dev, "read ALS_CONF from i2c error. %d\n",
453 ret);
454 return -EIO;
455 }
456
457 index = (ls_conf & 0xC0) >> 0x06;
458 return als_sense[index];
459}
460
461static void psensor_delay_work_handler(struct work_struct *work)
462{
463 struct cm36283_info *lpi = lp_info;
464 uint16_t adc_value = 0;
465 int ret;
466
467 mutex_lock(&wq_lock);
468
469 ret = get_ps_adc_value(&adc_value);
470
471 mutex_unlock(&wq_lock);
472
473 if (ret >= 0) {
474 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE,
475 adc_value > lpi->ps_close_thd_set ? 0 : 1);
476 input_sync(lpi->ps_input_dev);
477 }
478 schedule_delayed_work(&lpi->pdwork,
479 msecs_to_jiffies(atomic_read(&lpi->ps_poll_delay)));
480}
481
482static void lsensor_delay_work_handler(struct work_struct *work)
483{
484 struct cm36283_info *lpi = lp_info;
485 uint16_t adc_value = 0;
486 int sense;
487
488 mutex_lock(&wq_lock);
489
490 get_ls_adc_value(&adc_value, 0);
491 sense = get_als_sense();
492
493 mutex_unlock(&wq_lock);
494
495 if (sense > 0) {
496 lpi->current_adc = adc_value;
497 input_report_abs(lpi->ls_input_dev, ABS_MISC, adc_value/sense);
498 input_sync(lpi->ls_input_dev);
499 }
500 schedule_delayed_work(&lpi->ldwork,
501 msecs_to_jiffies(atomic_read(&lpi->ls_poll_delay)));
502}
503
Joseph Hsiao091151d2013-06-17 11:37:05 +0800504static irqreturn_t cm36283_irq_handler(int irq, void *data)
505{
506 struct cm36283_info *lpi = data;
507
508 disable_irq_nosync(lpi->irq);
509 queue_work(lpi->lp_wq, &sensor_irq_work);
510
511 return IRQ_HANDLED;
512}
513
514static int als_power(int enable)
515{
516 struct cm36283_info *lpi = lp_info;
517
518 if (lpi->power)
519 lpi->power(LS_PWR_ON, 1);
520
521 return 0;
522}
523
524static void ls_initial_cmd(struct cm36283_info *lpi)
525{
526 /*must disable l-sensor interrupt befrore IST create*//*disable ALS func*/
527 lpi->ls_cmd &= CM36283_ALS_INT_MASK;
528 lpi->ls_cmd |= CM36283_ALS_SD;
529 _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
530}
531
532static void psensor_initial_cmd(struct cm36283_info *lpi)
533{
534 /*must disable p-sensor interrupt befrore IST create*//*disable ALS func*/
535 lpi->ps_conf1_val |= CM36283_PS_SD;
536 lpi->ps_conf1_val &= CM36283_PS_INT_MASK;
537 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val);
538 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val);
539 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_THD, (lpi->ps_close_thd_set <<8)| lpi->ps_away_thd_set);
540
541 D("[PS][CM36283] %s, finish\n", __func__);
542}
543
544static int psensor_enable(struct cm36283_info *lpi)
545{
546 int ret = -EIO;
Oliver Wanged314dd2013-08-23 16:34:37 +0800547 unsigned int delay;
Joseph Hsiao091151d2013-06-17 11:37:05 +0800548
549 mutex_lock(&ps_enable_mutex);
550 D("[PS][CM36283] %s\n", __func__);
551
Oliver Wanged314dd2013-08-23 16:34:37 +0800552 if (lpi->ps_enable) {
Joseph Hsiao091151d2013-06-17 11:37:05 +0800553 D("[PS][CM36283] %s: already enabled\n", __func__);
554 ret = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +0800555 } else {
556 ret = control_and_report(lpi, CONTROL_PS, 1, 0);
557 }
558
Joseph Hsiao091151d2013-06-17 11:37:05 +0800559 mutex_unlock(&ps_enable_mutex);
Oliver Wanged314dd2013-08-23 16:34:37 +0800560
561 delay = atomic_read(&lpi->ps_poll_delay);
562 if (lpi->polling)
563 schedule_delayed_work(&lpi->pdwork, msecs_to_jiffies(delay));
564
Joseph Hsiao091151d2013-06-17 11:37:05 +0800565 return ret;
566}
567
568static int psensor_disable(struct cm36283_info *lpi)
569{
570 int ret = -EIO;
Oliver Wanged314dd2013-08-23 16:34:37 +0800571
572 if (lpi->polling)
573 cancel_delayed_work_sync(&lpi->pdwork);
574
Joseph Hsiao091151d2013-06-17 11:37:05 +0800575 mutex_lock(&ps_disable_mutex);
576 D("[PS][CM36283] %s\n", __func__);
577
Oliver Wanged314dd2013-08-23 16:34:37 +0800578 if (lpi->ps_enable == 0) {
Joseph Hsiao091151d2013-06-17 11:37:05 +0800579 D("[PS][CM36283] %s: already disabled\n", __func__);
580 ret = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +0800581 } else {
582 ret = control_and_report(lpi, CONTROL_PS, 0, 0);
583 }
584
Joseph Hsiao091151d2013-06-17 11:37:05 +0800585 mutex_unlock(&ps_disable_mutex);
586 return ret;
587}
588
589static int psensor_open(struct inode *inode, struct file *file)
590{
591 struct cm36283_info *lpi = lp_info;
592
593 D("[PS][CM36283] %s\n", __func__);
594
595 if (lpi->psensor_opened)
596 return -EBUSY;
597
598 lpi->psensor_opened = 1;
599
600 return 0;
601}
602
603static int psensor_release(struct inode *inode, struct file *file)
604{
605 struct cm36283_info *lpi = lp_info;
606
607 D("[PS][CM36283] %s\n", __func__);
608
609 lpi->psensor_opened = 0;
610
611 return psensor_disable(lpi);
612 //return 0;
613}
614
615static long psensor_ioctl(struct file *file, unsigned int cmd,
616 unsigned long arg)
617{
618 int val;
619 struct cm36283_info *lpi = lp_info;
620
621 D("[PS][CM36283] %s cmd %d\n", __func__, _IOC_NR(cmd));
622
623 switch (cmd) {
624 case CAPELLA_CM3602_IOCTL_ENABLE:
625 if (get_user(val, (unsigned long __user *)arg))
626 return -EFAULT;
627 if (val)
628 return psensor_enable(lpi);
629 else
630 return psensor_disable(lpi);
631 break;
632 case CAPELLA_CM3602_IOCTL_GET_ENABLED:
633 return put_user(lpi->ps_enable, (unsigned long __user *)arg);
634 break;
635 default:
636 pr_err("[PS][CM36283 error]%s: invalid cmd %d\n",
637 __func__, _IOC_NR(cmd));
638 return -EINVAL;
639 }
640}
641
642static const struct file_operations psensor_fops = {
643 .owner = THIS_MODULE,
644 .open = psensor_open,
645 .release = psensor_release,
646 .unlocked_ioctl = psensor_ioctl
647};
648
649struct miscdevice psensor_misc = {
650 .minor = MISC_DYNAMIC_MINOR,
651 .name = "proximity",
652 .fops = &psensor_fops
653};
654
655void lightsensor_set_kvalue(struct cm36283_info *lpi)
656{
657 if (!lpi) {
658 pr_err("[LS][CM36283 error]%s: ls_info is empty\n", __func__);
659 return;
660 }
661
662 D("[LS][CM36283] %s: ALS calibrated als_kadc=0x%x\n",
663 __func__, als_kadc);
664
665 if (als_kadc >> 16 == ALS_CALIBRATED)
666 lpi->als_kadc = als_kadc & 0xFFFF;
667 else {
668 lpi->als_kadc = 0;
669 D("[LS][CM36283] %s: no ALS calibrated\n", __func__);
670 }
671
672 if (lpi->als_kadc && lpi->golden_adc > 0) {
673 lpi->als_kadc = (lpi->als_kadc > 0 && lpi->als_kadc < 0x1000) ?
674 lpi->als_kadc : lpi->golden_adc;
675 lpi->als_gadc = lpi->golden_adc;
676 } else {
677 lpi->als_kadc = 1;
678 lpi->als_gadc = 1;
679 }
680 D("[LS][CM36283] %s: als_kadc=0x%x, als_gadc=0x%x\n",
681 __func__, lpi->als_kadc, lpi->als_gadc);
682}
683
684
685static int lightsensor_update_table(struct cm36283_info *lpi)
686{
687 uint32_t tmpData[10];
688 int i;
689 for (i = 0; i < 10; i++) {
690 tmpData[i] = (uint32_t)(*(lpi->adc_table + i))
691 * lpi->als_kadc / lpi->als_gadc ;
692 if( tmpData[i] <= 0xFFFF ){
693 lpi->cali_table[i] = (uint16_t) tmpData[i];
694 } else {
695 lpi->cali_table[i] = 0xFFFF;
696 }
697 D("[LS][CM36283] %s: Calibrated adc_table: data[%d], %x\n",
698 __func__, i, lpi->cali_table[i]);
699 }
700
701 return 0;
702}
703
704
705static int lightsensor_enable(struct cm36283_info *lpi)
706{
707 int ret = -EIO;
Oliver Wanged314dd2013-08-23 16:34:37 +0800708 unsigned int delay;
Joseph Hsiao091151d2013-06-17 11:37:05 +0800709
710 mutex_lock(&als_enable_mutex);
711 D("[LS][CM36283] %s\n", __func__);
712
713 if (lpi->als_enable) {
714 D("[LS][CM36283] %s: already enabled\n", __func__);
715 ret = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +0800716 } else {
717 ret = control_and_report(lpi, CONTROL_ALS, 1, 0);
718 }
Joseph Hsiao091151d2013-06-17 11:37:05 +0800719
720 mutex_unlock(&als_enable_mutex);
Oliver Wanged314dd2013-08-23 16:34:37 +0800721
722 delay = atomic_read(&lpi->ls_poll_delay);
723 if (lpi->polling)
724 schedule_delayed_work(&lpi->ldwork,
725 msecs_to_jiffies(delay));
726
Joseph Hsiao091151d2013-06-17 11:37:05 +0800727 return ret;
728}
729
730static int lightsensor_disable(struct cm36283_info *lpi)
731{
732 int ret = -EIO;
733 mutex_lock(&als_disable_mutex);
734 D("[LS][CM36283] %s\n", __func__);
735
Oliver Wanged314dd2013-08-23 16:34:37 +0800736 if (lpi->polling)
737 cancel_delayed_work_sync(&lpi->ldwork);
738
Joseph Hsiao091151d2013-06-17 11:37:05 +0800739 if ( lpi->als_enable == 0 ) {
740 D("[LS][CM36283] %s: already disabled\n", __func__);
741 ret = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +0800742 } else {
743 ret = control_and_report(lpi, CONTROL_ALS, 0, 0);
744 }
Joseph Hsiao091151d2013-06-17 11:37:05 +0800745
746 mutex_unlock(&als_disable_mutex);
747 return ret;
748}
749
750static int lightsensor_open(struct inode *inode, struct file *file)
751{
752 struct cm36283_info *lpi = lp_info;
753 int rc = 0;
754
755 D("[LS][CM36283] %s\n", __func__);
756 if (lpi->lightsensor_opened) {
757 pr_err("[LS][CM36283 error]%s: already opened\n", __func__);
758 rc = -EBUSY;
759 }
760 lpi->lightsensor_opened = 1;
761 return rc;
762}
763
764static int lightsensor_release(struct inode *inode, struct file *file)
765{
766 struct cm36283_info *lpi = lp_info;
767
768 D("[LS][CM36283] %s\n", __func__);
769 lpi->lightsensor_opened = 0;
770 return 0;
771}
772
773static long lightsensor_ioctl(struct file *file, unsigned int cmd,
774 unsigned long arg)
775{
776 int rc, val;
777 struct cm36283_info *lpi = lp_info;
778
779 /*D("[CM36283] %s cmd %d\n", __func__, _IOC_NR(cmd));*/
780
781 switch (cmd) {
782 case LIGHTSENSOR_IOCTL_ENABLE:
783 if (get_user(val, (unsigned long __user *)arg)) {
784 rc = -EFAULT;
785 break;
786 }
787 D("[LS][CM36283] %s LIGHTSENSOR_IOCTL_ENABLE, value = %d\n",
788 __func__, val);
789 rc = val ? lightsensor_enable(lpi) : lightsensor_disable(lpi);
790 break;
791 case LIGHTSENSOR_IOCTL_GET_ENABLED:
792 val = lpi->als_enable;
793 D("[LS][CM36283] %s LIGHTSENSOR_IOCTL_GET_ENABLED, enabled %d\n",
794 __func__, val);
795 rc = put_user(val, (unsigned long __user *)arg);
796 break;
797 default:
798 pr_err("[LS][CM36283 error]%s: invalid cmd %d\n",
799 __func__, _IOC_NR(cmd));
800 rc = -EINVAL;
801 }
802
803 return rc;
804}
805
806static const struct file_operations lightsensor_fops = {
807 .owner = THIS_MODULE,
808 .open = lightsensor_open,
809 .release = lightsensor_release,
810 .unlocked_ioctl = lightsensor_ioctl
811};
812
813static struct miscdevice lightsensor_misc = {
814 .minor = MISC_DYNAMIC_MINOR,
815 .name = "lightsensor",
816 .fops = &lightsensor_fops
817};
818
819
820static ssize_t ps_adc_show(struct device *dev,
821 struct device_attribute *attr, char *buf)
822{
823
824 uint16_t value;
825 int ret;
826 struct cm36283_info *lpi = lp_info;
Oliver Wanged314dd2013-08-23 16:34:37 +0800827 int intr_val = -1;
Joseph Hsiao091151d2013-06-17 11:37:05 +0800828
829 get_ps_adc_value(&value);
Oliver Wanged314dd2013-08-23 16:34:37 +0800830 if (gpio_is_valid(lpi->intr_pin))
831 intr_val = gpio_get_value(lpi->intr_pin);
Joseph Hsiao091151d2013-06-17 11:37:05 +0800832
Oliver Wanged314dd2013-08-23 16:34:37 +0800833 ret = snprintf(buf, PAGE_SIZE, "ADC[0x%04X], ENABLE=%d intr_pin=%d\n",
834 value, lpi->ps_enable, intr_val);
Joseph Hsiao091151d2013-06-17 11:37:05 +0800835
836 return ret;
837}
838
839static ssize_t ps_enable_store(struct device *dev,
840 struct device_attribute *attr,
841 const char *buf, size_t count)
842{
843 int ps_en;
844 struct cm36283_info *lpi = lp_info;
845
846 ps_en = -1;
847 sscanf(buf, "%d", &ps_en);
848
849 if (ps_en != 0 && ps_en != 1
850 && ps_en != 10 && ps_en != 13 && ps_en != 16)
851 return -EINVAL;
852
853 if (ps_en) {
854 D("[PS][CM36283] %s: ps_en=%d\n",
855 __func__, ps_en);
856 psensor_enable(lpi);
857 } else
858 psensor_disable(lpi);
859
860 D("[PS][CM36283] %s\n", __func__);
861
862 return count;
863}
864
865static DEVICE_ATTR(ps_adc, 0664, ps_adc_show, ps_enable_store);
866
867unsigned PS_cmd_test_value;
868static ssize_t ps_parameters_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
870{
871 int ret;
872 struct cm36283_info *lpi = lp_info;
873
874 ret = sprintf(buf, "PS_close_thd_set = 0x%x, PS_away_thd_set = 0x%x, PS_cmd_cmd:value = 0x%x\n",
875 lpi->ps_close_thd_set, lpi->ps_away_thd_set, PS_cmd_test_value);
876
877 return ret;
878}
879
880static ssize_t ps_parameters_store(struct device *dev,
881 struct device_attribute *attr,
882 const char *buf, size_t count)
883{
884
885 struct cm36283_info *lpi = lp_info;
886 char *token[10];
887 int i;
888
889 printk(KERN_INFO "[PS][CM36283] %s\n", buf);
890 for (i = 0; i < 3; i++)
891 token[i] = strsep((char **)&buf, " ");
892
893 lpi->ps_close_thd_set = simple_strtoul(token[0], NULL, 16);
894 lpi->ps_away_thd_set = simple_strtoul(token[1], NULL, 16);
895 PS_cmd_test_value = simple_strtoul(token[2], NULL, 16);
896 printk(KERN_INFO
897 "[PS][CM36283]Set PS_close_thd_set = 0x%x, PS_away_thd_set = 0x%x, PS_cmd_cmd:value = 0x%x\n",
898 lpi->ps_close_thd_set, lpi->ps_away_thd_set, PS_cmd_test_value);
899
900 D("[PS][CM36283] %s\n", __func__);
901
902 return count;
903}
904
905static DEVICE_ATTR(ps_parameters, 0664,
906 ps_parameters_show, ps_parameters_store);
907
908
909static ssize_t ps_conf_show(struct device *dev,
910 struct device_attribute *attr, char *buf)
911{
912 struct cm36283_info *lpi = lp_info;
913 return sprintf(buf, "PS_CONF1 = 0x%x, PS_CONF3 = 0x%x\n", lpi->ps_conf1_val, lpi->ps_conf3_val);
914}
915static ssize_t ps_conf_store(struct device *dev,
916 struct device_attribute *attr,
917 const char *buf, size_t count)
918{
919 int code1, code2;
920 struct cm36283_info *lpi = lp_info;
921
922 sscanf(buf, "0x%x 0x%x", &code1, &code2);
923
924 D("[PS]%s: store value PS conf1 reg = 0x%x PS conf3 reg = 0x%x\n", __func__, code1, code2);
925
926 lpi->ps_conf1_val = code1;
927 lpi->ps_conf3_val = code2;
928
929 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val );
930 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val );
931
932 return count;
933}
934static DEVICE_ATTR(ps_conf, 0664, ps_conf_show, ps_conf_store);
935
936static ssize_t ps_thd_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
938{
939 int ret;
940 struct cm36283_info *lpi = lp_info;
941 ret = sprintf(buf, "%s ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n", __func__, lpi->ps_close_thd_set, lpi->ps_away_thd_set);
942 return ret;
943}
944static ssize_t ps_thd_store(struct device *dev,
945 struct device_attribute *attr,
946 const char *buf, size_t count)
947{
948 int code;
949 struct cm36283_info *lpi = lp_info;
950
951 sscanf(buf, "0x%x", &code);
952
953 D("[PS]%s: store value = 0x%x\n", __func__, code);
954
955 lpi->ps_away_thd_set = code &0xFF;
956 lpi->ps_close_thd_set = (code &0xFF00)>>8;
957
958 D("[PS]%s: ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n", __func__, lpi->ps_close_thd_set, lpi->ps_away_thd_set);
959
960 return count;
961}
962static DEVICE_ATTR(ps_thd, 0664, ps_thd_show, ps_thd_store);
963
964static ssize_t ps_hw_show(struct device *dev,
965 struct device_attribute *attr, char *buf)
966{
967 int ret = 0;
968 struct cm36283_info *lpi = lp_info;
969
970 ret = sprintf(buf, "PS1: reg = 0x%x, PS3: reg = 0x%x, ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n",
971 lpi->ps_conf1_val, lpi->ps_conf3_val, lpi->ps_close_thd_set, lpi->ps_away_thd_set);
972
973 return ret;
974}
975static ssize_t ps_hw_store(struct device *dev,
976 struct device_attribute *attr,
977 const char *buf, size_t count)
978{
979 int code;
980// struct cm36283_info *lpi = lp_info;
981
982 sscanf(buf, "0x%x", &code);
983
984 D("[PS]%s: store value = 0x%x\n", __func__, code);
985
986 return count;
987}
988static DEVICE_ATTR(ps_hw, 0664, ps_hw_show, ps_hw_store);
989
990static ssize_t ls_adc_show(struct device *dev,
991 struct device_attribute *attr, char *buf)
992{
993 int ret;
994 struct cm36283_info *lpi = lp_info;
995
996 D("[LS][CM36283] %s: ADC = 0x%04X, Level = %d \n",
997 __func__, lpi->current_adc, lpi->current_level);
998 ret = sprintf(buf, "ADC[0x%04X] => level %d\n",
999 lpi->current_adc, lpi->current_level);
1000
1001 return ret;
1002}
1003
1004static DEVICE_ATTR(ls_adc, 0664, ls_adc_show, NULL);
1005
1006static ssize_t ls_enable_show(struct device *dev,
1007 struct device_attribute *attr, char *buf)
1008{
1009
1010 int ret = 0;
1011 struct cm36283_info *lpi = lp_info;
1012
1013 ret = sprintf(buf, "Light sensor Auto Enable = %d\n",
1014 lpi->als_enable);
1015
1016 return ret;
1017}
1018
1019static ssize_t ls_enable_store(struct device *dev,
1020 struct device_attribute *attr,
1021 const char *buf, size_t count)
1022{
1023 int ret = 0;
1024 int ls_auto;
1025 struct cm36283_info *lpi = lp_info;
1026
1027 ls_auto = -1;
1028 sscanf(buf, "%d", &ls_auto);
1029
1030 if (ls_auto != 0 && ls_auto != 1 && ls_auto != 147)
1031 return -EINVAL;
1032
1033 if (ls_auto) {
1034 lpi->ls_calibrate = (ls_auto == 147) ? 1 : 0;
1035 ret = lightsensor_enable(lpi);
1036 } else {
1037 lpi->ls_calibrate = 0;
1038 ret = lightsensor_disable(lpi);
1039 }
1040
1041 D("[LS][CM36283] %s: lpi->als_enable = %d, lpi->ls_calibrate = %d, ls_auto=%d\n",
1042 __func__, lpi->als_enable, lpi->ls_calibrate, ls_auto);
1043
1044 if (ret < 0)
1045 pr_err(
1046 "[LS][CM36283 error]%s: set auto light sensor fail\n",
1047 __func__);
1048
1049 return count;
1050}
1051
1052static DEVICE_ATTR(ls_auto, 0664,
1053 ls_enable_show, ls_enable_store);
1054
1055static ssize_t ls_kadc_show(struct device *dev,
1056 struct device_attribute *attr, char *buf)
1057{
1058 struct cm36283_info *lpi = lp_info;
1059 int ret;
1060
1061 ret = sprintf(buf, "kadc = 0x%x",
1062 lpi->als_kadc);
1063
1064 return ret;
1065}
1066
1067static ssize_t ls_kadc_store(struct device *dev,
1068 struct device_attribute *attr,
1069 const char *buf, size_t count)
1070{
1071 struct cm36283_info *lpi = lp_info;
1072 int kadc_temp = 0;
1073
1074 sscanf(buf, "%d", &kadc_temp);
1075
1076 mutex_lock(&als_get_adc_mutex);
1077 if(kadc_temp != 0) {
1078 lpi->als_kadc = kadc_temp;
1079 if( lpi->als_gadc != 0){
1080 if (lightsensor_update_table(lpi) < 0)
1081 printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n", __func__);
1082 } else {
1083 printk(KERN_INFO "[LS]%s: als_gadc =0x%x wait to be set\n",
1084 __func__, lpi->als_gadc);
1085 }
1086 } else {
1087 printk(KERN_INFO "[LS]%s: als_kadc can't be set to zero\n",
1088 __func__);
1089 }
1090
1091 mutex_unlock(&als_get_adc_mutex);
1092 return count;
1093}
1094
1095static DEVICE_ATTR(ls_kadc, 0664, ls_kadc_show, ls_kadc_store);
1096
1097static ssize_t ls_gadc_show(struct device *dev,
1098 struct device_attribute *attr, char *buf)
1099{
1100 struct cm36283_info *lpi = lp_info;
1101 int ret;
1102
1103 ret = sprintf(buf, "gadc = 0x%x\n", lpi->als_gadc);
1104
1105 return ret;
1106}
1107
1108static ssize_t ls_gadc_store(struct device *dev,
1109 struct device_attribute *attr,
1110 const char *buf, size_t count)
1111{
1112 struct cm36283_info *lpi = lp_info;
1113 int gadc_temp = 0;
1114
1115 sscanf(buf, "%d", &gadc_temp);
1116
1117 mutex_lock(&als_get_adc_mutex);
1118 if(gadc_temp != 0) {
1119 lpi->als_gadc = gadc_temp;
1120 if( lpi->als_kadc != 0){
1121 if (lightsensor_update_table(lpi) < 0)
1122 printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n", __func__);
1123 } else {
1124 printk(KERN_INFO "[LS]%s: als_kadc =0x%x wait to be set\n",
1125 __func__, lpi->als_kadc);
1126 }
1127 } else {
1128 printk(KERN_INFO "[LS]%s: als_gadc can't be set to zero\n",
1129 __func__);
1130 }
1131 mutex_unlock(&als_get_adc_mutex);
1132 return count;
1133}
1134
1135static DEVICE_ATTR(ls_gadc, 0664, ls_gadc_show, ls_gadc_store);
1136
1137static ssize_t ls_adc_table_show(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1139{
1140 unsigned length = 0;
1141 int i;
1142
1143 for (i = 0; i < 10; i++) {
1144 length += sprintf(buf + length,
1145 "[CM36283]Get adc_table[%d] = 0x%x ; %d, Get cali_table[%d] = 0x%x ; %d, \n",
1146 i, *(lp_info->adc_table + i),
1147 *(lp_info->adc_table + i),
1148 i, *(lp_info->cali_table + i),
1149 *(lp_info->cali_table + i));
1150 }
1151 return length;
1152}
1153
1154static ssize_t ls_adc_table_store(struct device *dev,
1155 struct device_attribute *attr,
1156 const char *buf, size_t count)
1157{
1158
1159 struct cm36283_info *lpi = lp_info;
1160 char *token[10];
1161 uint16_t tempdata[10];
1162 int i;
1163
1164 printk(KERN_INFO "[LS][CM36283]%s\n", buf);
1165 for (i = 0; i < 10; i++) {
1166 token[i] = strsep((char **)&buf, " ");
1167 tempdata[i] = simple_strtoul(token[i], NULL, 16);
1168 if (tempdata[i] < 1 || tempdata[i] > 0xffff) {
1169 printk(KERN_ERR
1170 "[LS][CM36283 error] adc_table[%d] = 0x%x Err\n",
1171 i, tempdata[i]);
1172 return count;
1173 }
1174 }
1175 mutex_lock(&als_get_adc_mutex);
1176 for (i = 0; i < 10; i++) {
1177 lpi->adc_table[i] = tempdata[i];
1178 printk(KERN_INFO
1179 "[LS][CM36283]Set lpi->adc_table[%d] = 0x%x\n",
1180 i, *(lp_info->adc_table + i));
1181 }
1182 if (lightsensor_update_table(lpi) < 0)
1183 printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n",
1184 __func__);
1185 mutex_unlock(&als_get_adc_mutex);
1186 D("[LS][CM36283] %s\n", __func__);
1187
1188 return count;
1189}
1190
1191static DEVICE_ATTR(ls_adc_table, 0664,
1192 ls_adc_table_show, ls_adc_table_store);
1193
1194static ssize_t ls_conf_show(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1196{
1197 struct cm36283_info *lpi = lp_info;
1198 return sprintf(buf, "ALS_CONF = %x\n", lpi->ls_cmd);
1199}
1200static ssize_t ls_conf_store(struct device *dev,
1201 struct device_attribute *attr,
1202 const char *buf, size_t count)
1203{
1204 struct cm36283_info *lpi = lp_info;
1205 int value = 0;
1206 sscanf(buf, "0x%x", &value);
1207
1208 lpi->ls_cmd = value;
1209 printk(KERN_INFO "[LS]set ALS_CONF = %x\n", lpi->ls_cmd);
1210
1211 _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
1212 return count;
1213}
1214static DEVICE_ATTR(ls_conf, 0664, ls_conf_show, ls_conf_store);
1215
Oliver Wanged314dd2013-08-23 16:34:37 +08001216static ssize_t ls_poll_delay_show(struct device *dev,
1217 struct device_attribute *attr, char *buf)
1218{
1219 struct cm36283_info *lpi = lp_info;
1220 return snprintf(buf, PAGE_SIZE, "%d\n",
1221 atomic_read(&lpi->ls_poll_delay));
1222}
1223
1224static ssize_t ls_poll_delay_store(struct device *dev,
1225 struct device_attribute *attr, const char *buf, size_t count)
1226{
1227 struct cm36283_info *lpi = lp_info;
1228 unsigned long interval_ms;
1229
1230 if (kstrtoul(buf, 10, &interval_ms))
1231 return -EINVAL;
1232
1233 if ((interval_ms < CM36283_LS_MIN_POLL_DELAY) ||
1234 (interval_ms > CM36283_LS_MAX_POLL_DELAY))
1235 return -EINVAL;
1236
1237 atomic_set(&lpi->ls_poll_delay, (unsigned int) interval_ms);
1238 return count;
1239}
1240
1241static DEVICE_ATTR(ls_poll_delay, 0664, ls_poll_delay_show,
1242 ls_poll_delay_store);
1243
1244static ssize_t ps_poll_delay_show(struct device *dev,
1245 struct device_attribute *attr, char *buf)
1246{
1247 struct cm36283_info *lpi = lp_info;
1248 return snprintf(buf, PAGE_SIZE, "%d\n",
1249 atomic_read(&lpi->ps_poll_delay));
1250}
1251
1252static ssize_t ps_poll_delay_store(struct device *dev,
1253 struct device_attribute *attr, const char *buf, size_t count)
1254{
1255 struct cm36283_info *lpi = lp_info;
1256 unsigned long interval_ms;
1257
1258 if (kstrtoul(buf, 10, &interval_ms))
1259 return -EINVAL;
1260
1261 if ((interval_ms < CM36283_PS_MIN_POLL_DELAY) ||
1262 (interval_ms > CM36283_PS_MAX_POLL_DELAY))
1263 return -EINVAL;
1264
1265 atomic_set(&lpi->ps_poll_delay, (unsigned int) interval_ms);
1266 return count;
1267}
1268
1269static DEVICE_ATTR(ps_poll_delay, 0664, ps_poll_delay_show,
1270 ps_poll_delay_store);
1271
Joseph Hsiao091151d2013-06-17 11:37:05 +08001272static ssize_t ls_fLevel_show(struct device *dev,
1273 struct device_attribute *attr, char *buf)
1274{
1275 return sprintf(buf, "fLevel = %d\n", fLevel);
1276}
1277static ssize_t ls_fLevel_store(struct device *dev,
1278 struct device_attribute *attr,
1279 const char *buf, size_t count)
1280{
1281 struct cm36283_info *lpi = lp_info;
1282 int value=0;
1283 sscanf(buf, "%d", &value);
1284 (value>=0)?(value=min(value,10)):(value=max(value,-1));
1285 fLevel=value;
1286 input_report_abs(lpi->ls_input_dev, ABS_MISC, fLevel);
1287 input_sync(lpi->ls_input_dev);
1288 printk(KERN_INFO "[LS]set fLevel = %d\n", fLevel);
1289
1290 msleep(1000);
1291 fLevel=-1;
1292 return count;
1293}
1294static DEVICE_ATTR(ls_flevel, 0664, ls_fLevel_show, ls_fLevel_store);
1295
1296static int lightsensor_setup(struct cm36283_info *lpi)
1297{
1298 int ret;
Oliver Wanged314dd2013-08-23 16:34:37 +08001299 int range;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001300
1301 lpi->ls_input_dev = input_allocate_device();
1302 if (!lpi->ls_input_dev) {
1303 pr_err(
1304 "[LS][CM36283 error]%s: could not allocate ls input device\n",
1305 __func__);
1306 return -ENOMEM;
1307 }
1308 lpi->ls_input_dev->name = "cm36283-ls";
1309 set_bit(EV_ABS, lpi->ls_input_dev->evbit);
Oliver Wanged314dd2013-08-23 16:34:37 +08001310
1311 range = get_als_range();
1312 input_set_abs_params(lpi->ls_input_dev, ABS_MISC, 0, range, 0, 0);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001313
1314 ret = input_register_device(lpi->ls_input_dev);
1315 if (ret < 0) {
1316 pr_err("[LS][CM36283 error]%s: can not register ls input device\n",
1317 __func__);
1318 goto err_free_ls_input_device;
1319 }
1320
1321 ret = misc_register(&lightsensor_misc);
1322 if (ret < 0) {
1323 pr_err("[LS][CM36283 error]%s: can not register ls misc device\n",
1324 __func__);
1325 goto err_unregister_ls_input_device;
1326 }
1327
1328 return ret;
1329
1330err_unregister_ls_input_device:
1331 input_unregister_device(lpi->ls_input_dev);
1332err_free_ls_input_device:
1333 input_free_device(lpi->ls_input_dev);
1334 return ret;
1335}
1336
1337static int psensor_setup(struct cm36283_info *lpi)
1338{
1339 int ret;
1340
1341 lpi->ps_input_dev = input_allocate_device();
1342 if (!lpi->ps_input_dev) {
1343 pr_err(
1344 "[PS][CM36283 error]%s: could not allocate ps input device\n",
1345 __func__);
1346 return -ENOMEM;
1347 }
1348 lpi->ps_input_dev->name = "cm36283-ps";
1349 set_bit(EV_ABS, lpi->ps_input_dev->evbit);
1350 input_set_abs_params(lpi->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
1351
1352 ret = input_register_device(lpi->ps_input_dev);
1353 if (ret < 0) {
1354 pr_err(
1355 "[PS][CM36283 error]%s: could not register ps input device\n",
1356 __func__);
1357 goto err_free_ps_input_device;
1358 }
1359
1360 ret = misc_register(&psensor_misc);
1361 if (ret < 0) {
1362 pr_err(
1363 "[PS][CM36283 error]%s: could not register ps misc device\n",
1364 __func__);
1365 goto err_unregister_ps_input_device;
1366 }
1367
1368 return ret;
1369
1370err_unregister_ps_input_device:
1371 input_unregister_device(lpi->ps_input_dev);
1372err_free_ps_input_device:
1373 input_free_device(lpi->ps_input_dev);
1374 return ret;
1375}
1376
1377
1378static int initial_cm36283(struct cm36283_info *lpi)
1379{
1380 int val, ret;
1381 uint16_t idReg;
1382
1383 val = gpio_get_value(lpi->intr_pin);
1384 D("[PS][CM36283] %s, INTERRUPT GPIO val = %d\n", __func__, val);
1385
1386 ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ID_REG, &idReg);
Oliver Wanged314dd2013-08-23 16:34:37 +08001387
1388 return ret;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001389}
1390
1391static int cm36283_setup(struct cm36283_info *lpi)
1392{
1393 int ret = 0;
1394
1395 als_power(1);
1396 msleep(5);
1397 ret = gpio_request(lpi->intr_pin, "gpio_cm36283_intr");
1398 if (ret < 0) {
1399 pr_err("[PS][CM36283 error]%s: gpio %d request failed (%d)\n",
1400 __func__, lpi->intr_pin, ret);
1401 return ret;
1402 }
1403
1404 ret = gpio_direction_input(lpi->intr_pin);
1405 if (ret < 0) {
1406 pr_err(
1407 "[PS][CM36283 error]%s: fail to set gpio %d as input (%d)\n",
1408 __func__, lpi->intr_pin, ret);
1409 goto fail_free_intr_pin;
1410 }
1411
1412
1413 ret = initial_cm36283(lpi);
1414 if (ret < 0) {
1415 pr_err(
1416 "[PS_ERR][CM36283 error]%s: fail to initial cm36283 (%d)\n",
1417 __func__, ret);
1418 goto fail_free_intr_pin;
1419 }
1420
1421 /*Default disable P sensor and L sensor*/
Oliver Wanged314dd2013-08-23 16:34:37 +08001422 ls_initial_cmd(lpi);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001423 psensor_initial_cmd(lpi);
1424
Oliver Wanged314dd2013-08-23 16:34:37 +08001425 if (!lpi->polling)
1426 ret = request_any_context_irq(lpi->irq,
1427 cm36283_irq_handler,
1428 IRQF_TRIGGER_LOW,
1429 "cm36283",
1430 lpi);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001431 if (ret < 0) {
1432 pr_err(
1433 "[PS][CM36283 error]%s: req_irq(%d) fail for gpio %d (%d)\n",
1434 __func__, lpi->irq,
1435 lpi->intr_pin, ret);
1436 goto fail_free_intr_pin;
1437 }
1438
1439 return ret;
1440
1441fail_free_intr_pin:
1442 gpio_free(lpi->intr_pin);
1443 return ret;
1444}
1445
1446#ifdef CONFIG_HAS_EARLYSUSPEND
1447static void cm36283_early_suspend(struct early_suspend *h)
1448{
1449 struct cm36283_info *lpi = lp_info;
1450
1451 D("[LS][CM36283] %s\n", __func__);
1452
1453 if (lpi->als_enable)
1454 lightsensor_disable(lpi);
1455
1456}
1457
1458static void cm36283_late_resume(struct early_suspend *h)
1459{
1460 struct cm36283_info *lpi = lp_info;
1461
1462 D("[LS][CM36283] %s\n", __func__);
1463
1464 if (!lpi->als_enable)
1465 lightsensor_enable(lpi);
1466}
1467#endif
1468
Oliver Wang8a1b8be2013-08-23 13:42:10 +08001469static int cm36283_parse_dt(struct device *dev,
1470 struct cm36283_platform_data *pdata)
1471{
1472 struct device_node *np = dev->of_node;
1473 u32 levels[CM36283_LEVELS_SIZE], i;
1474 u32 temp_val;
1475 int rc;
1476
1477 rc = of_get_named_gpio_flags(np, "capella,interrupt-gpio",
1478 0, NULL);
1479 if (rc < 0) {
1480 dev_err(dev, "Unable to read interrupt pin number\n");
1481 return rc;
1482 } else {
1483 pdata->intr = rc;
1484 }
1485
1486 rc = of_property_read_u32_array(np, "capella,levels", levels,
1487 CM36283_LEVELS_SIZE);
1488 if (rc) {
1489 dev_err(dev, "Unable to read levels data\n");
1490 return rc;
1491 } else {
1492 for (i = 0; i < CM36283_LEVELS_SIZE; i++)
1493 pdata->levels[i] = levels[i];
1494 }
1495
1496 rc = of_property_read_u32(np, "capella,ps_close_thd_set", &temp_val);
1497 if (rc) {
1498 dev_err(dev, "Unable to read ps_close_thd_set\n");
1499 return rc;
1500 } else {
1501 pdata->ps_close_thd_set = (u8)temp_val;
1502 }
1503
1504 rc = of_property_read_u32(np, "capella,ps_away_thd_set", &temp_val);
1505 if (rc) {
1506 dev_err(dev, "Unable to read ps_away_thd_set\n");
1507 return rc;
1508 } else {
1509 pdata->ps_away_thd_set = (u8)temp_val;
1510 }
1511
1512 rc = of_property_read_u32(np, "capella,ls_cmd", &temp_val);
1513 if (rc) {
1514 dev_err(dev, "Unable to read ls_cmd\n");
1515 return rc;
1516 } else {
1517 pdata->ls_cmd = (u16)temp_val;
1518 }
1519
1520 rc = of_property_read_u32(np, "capella,ps_conf1_val", &temp_val);
1521 if (rc) {
1522 dev_err(dev, "Unable to read ps_conf1_val\n");
1523 return rc;
1524 } else {
1525 pdata->ps_conf1_val = (u16)temp_val;
1526 }
1527
1528 rc = of_property_read_u32(np, "capella,ps_conf3_val", &temp_val);
1529 if (rc) {
1530 dev_err(dev, "Unable to read ps_conf3_val\n");
1531 return rc;
1532 } else {
1533 pdata->ps_conf3_val = (u16)temp_val;
1534 }
1535
1536 pdata->polling = of_property_read_bool(np, "capella,use-polling");
1537
1538 return 0;
1539}
1540
Joseph Hsiao091151d2013-06-17 11:37:05 +08001541static int cm36283_probe(struct i2c_client *client,
1542 const struct i2c_device_id *id)
1543{
1544 int ret = 0;
1545 struct cm36283_info *lpi;
1546 struct cm36283_platform_data *pdata;
1547
1548 D("[PS][CM36283] %s\n", __func__);
1549
1550
1551 lpi = kzalloc(sizeof(struct cm36283_info), GFP_KERNEL);
1552 if (!lpi)
1553 return -ENOMEM;
1554
1555 /*D("[CM36283] %s: client->irq = %d\n", __func__, client->irq);*/
1556
1557 lpi->i2c_client = client;
Oliver Wang8a1b8be2013-08-23 13:42:10 +08001558
1559 if (client->dev.of_node) {
1560 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1561 if (!pdata) {
1562 dev_err(&client->dev, "Failed to allocate memory for pdata\n");
1563 ret = -ENOMEM;
1564 goto err_platform_data_null;
1565 }
1566
1567 ret = cm36283_parse_dt(&client->dev, pdata);
1568 pdata->slave_addr = client->addr;
1569 if (ret) {
1570 dev_err(&client->dev, "Failed to get pdata from device tree\n");
1571 goto err_parse_dt;
1572 }
1573 } else {
1574 pdata = client->dev.platform_data;
1575 if (!pdata) {
1576 dev_err(&client->dev, "%s: Assign platform_data error!!\n",
1577 __func__);
1578 ret = -EBUSY;
1579 goto err_platform_data_null;
1580 }
Joseph Hsiao091151d2013-06-17 11:37:05 +08001581 }
1582
1583 lpi->irq = client->irq;
1584
1585 i2c_set_clientdata(client, lpi);
1586
1587 lpi->intr_pin = pdata->intr;
1588 lpi->adc_table = pdata->levels;
1589 lpi->power = pdata->power;
1590
1591 lpi->slave_addr = pdata->slave_addr;
1592
1593 lpi->ps_away_thd_set = pdata->ps_away_thd_set;
1594 lpi->ps_close_thd_set = pdata->ps_close_thd_set;
1595 lpi->ps_conf1_val = pdata->ps_conf1_val;
1596 lpi->ps_conf3_val = pdata->ps_conf3_val;
Oliver Wanged314dd2013-08-23 16:34:37 +08001597 lpi->polling = pdata->polling;
1598 atomic_set(&lpi->ls_poll_delay,
1599 (unsigned int) CM36283_LS_DEFAULT_POLL_DELAY);
1600 atomic_set(&lpi->ps_poll_delay,
1601 (unsigned int) CM36283_PS_DEFAULT_POLL_DELAY);
1602
Joseph Hsiao091151d2013-06-17 11:37:05 +08001603
1604 lpi->ls_cmd = pdata->ls_cmd;
1605
1606 lpi->record_clear_int_fail=0;
1607
1608 D("[PS][CM36283] %s: ls_cmd 0x%x\n",
1609 __func__, lpi->ls_cmd);
1610
1611 if (pdata->ls_cmd == 0) {
1612 lpi->ls_cmd = CM36283_ALS_IT_160ms | CM36283_ALS_GAIN_2;
1613 }
1614
1615 lp_info = lpi;
1616
1617 mutex_init(&CM36283_control_mutex);
1618
1619 mutex_init(&als_enable_mutex);
1620 mutex_init(&als_disable_mutex);
1621 mutex_init(&als_get_adc_mutex);
1622
Joseph Hsiao091151d2013-06-17 11:37:05 +08001623
1624 mutex_init(&ps_enable_mutex);
1625 mutex_init(&ps_disable_mutex);
1626 mutex_init(&ps_get_adc_mutex);
1627
Joseph Hsiao091151d2013-06-17 11:37:05 +08001628
1629 //SET LUX STEP FACTOR HERE
1630 // if adc raw value one step = 5/100 = 1/20 = 0.05 lux
1631 // the following will set the factor 0.05 = 1/20
1632 // and lpi->golden_adc = 1;
1633 // set als_kadc = (ALS_CALIBRATED <<16) | 20;
1634
1635 als_kadc = (ALS_CALIBRATED <<16) | 20;
1636 lpi->golden_adc = 1;
1637
1638 //ls calibrate always set to 1
1639 lpi->ls_calibrate = 1;
1640
1641 lightsensor_set_kvalue(lpi);
1642 ret = lightsensor_update_table(lpi);
1643 if (ret < 0) {
1644 pr_err("[LS][CM36283 error]%s: update ls table fail\n",
1645 __func__);
1646 goto err_lightsensor_update_table;
1647 }
1648
1649 lpi->lp_wq = create_singlethread_workqueue("cm36283_wq");
1650 if (!lpi->lp_wq) {
1651 pr_err("[PS][CM36283 error]%s: can't create workqueue\n", __func__);
1652 ret = -ENOMEM;
1653 goto err_create_singlethread_workqueue;
1654 }
1655 wake_lock_init(&(lpi->ps_wake_lock), WAKE_LOCK_SUSPEND, "proximity");
1656
Oliver Wang0afd6a32013-08-26 14:23:51 +08001657 ret = cm36283_power_set(lpi, true);
1658 if (ret < 0) {
1659 dev_err(&client->dev, "%s:cm36283 power on error!\n", __func__);
1660 goto err_cm36283_power_on;
1661 }
1662
Joseph Hsiao091151d2013-06-17 11:37:05 +08001663 ret = cm36283_setup(lpi);
1664 if (ret < 0) {
1665 pr_err("[PS_ERR][CM36283 error]%s: cm36283_setup error!\n", __func__);
1666 goto err_cm36283_setup;
1667 }
Oliver Wang0afd6a32013-08-26 14:23:51 +08001668
1669 ret = lightsensor_setup(lpi);
1670 if (ret < 0) {
1671 pr_err("[LS][CM36283 error]%s: lightsensor_setup error!!\n",
1672 __func__);
1673 goto err_lightsensor_setup;
1674 }
1675
1676 ret = psensor_setup(lpi);
1677 if (ret < 0) {
1678 pr_err("[PS][CM36283 error]%s: psensor_setup error!!\n",
1679 __func__);
1680 goto err_psensor_setup;
1681 }
1682
Joseph Hsiao091151d2013-06-17 11:37:05 +08001683 lpi->cm36283_class = class_create(THIS_MODULE, "optical_sensors");
1684 if (IS_ERR(lpi->cm36283_class)) {
1685 ret = PTR_ERR(lpi->cm36283_class);
1686 lpi->cm36283_class = NULL;
1687 goto err_create_class;
1688 }
1689
1690 lpi->ls_dev = device_create(lpi->cm36283_class,
1691 NULL, 0, "%s", "lightsensor");
1692 if (unlikely(IS_ERR(lpi->ls_dev))) {
1693 ret = PTR_ERR(lpi->ls_dev);
1694 lpi->ls_dev = NULL;
1695 goto err_create_ls_device;
1696 }
1697
1698 /* register the attributes */
1699 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc);
1700 if (ret)
1701 goto err_create_ls_device_file;
1702
1703 /* register the attributes */
1704 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_auto);
1705 if (ret)
1706 goto err_create_ls_device_file;
1707
1708 /* register the attributes */
1709 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_kadc);
1710 if (ret)
1711 goto err_create_ls_device_file;
1712
1713 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_gadc);
1714 if (ret)
1715 goto err_create_ls_device_file;
1716
1717 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc_table);
1718 if (ret)
1719 goto err_create_ls_device_file;
1720
1721 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_conf);
1722 if (ret)
1723 goto err_create_ls_device_file;
1724
1725 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_flevel);
1726 if (ret)
1727 goto err_create_ls_device_file;
1728
Oliver Wanged314dd2013-08-23 16:34:37 +08001729 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_poll_delay);
1730 if (ret)
1731 goto err_create_ls_device_file;
1732
Joseph Hsiao091151d2013-06-17 11:37:05 +08001733 lpi->ps_dev = device_create(lpi->cm36283_class,
1734 NULL, 0, "%s", "proximity");
1735 if (unlikely(IS_ERR(lpi->ps_dev))) {
1736 ret = PTR_ERR(lpi->ps_dev);
1737 lpi->ps_dev = NULL;
Oliver Wang0afd6a32013-08-26 14:23:51 +08001738 goto err_create_ps_device;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001739 }
1740
1741 /* register the attributes */
1742 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_adc);
1743 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001744 goto err_create_ps_device_file;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001745
1746 ret = device_create_file(lpi->ps_dev,
1747 &dev_attr_ps_parameters);
1748 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001749 goto err_create_ps_device_file;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001750
1751 /* register the attributes */
1752 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_conf);
1753 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001754 goto err_create_ps_device_file;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001755
1756 /* register the attributes */
1757 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_thd);
1758 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001759 goto err_create_ps_device_file;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001760
1761 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_hw);
1762 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001763 goto err_create_ps_device_file;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001764
Oliver Wanged314dd2013-08-23 16:34:37 +08001765 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_poll_delay);
1766 if (ret)
Oliver Wang0afd6a32013-08-26 14:23:51 +08001767 goto err_create_ps_device_file;
Oliver Wanged314dd2013-08-23 16:34:37 +08001768
Joseph Hsiao091151d2013-06-17 11:37:05 +08001769#ifdef CONFIG_HAS_EARLYSUSPEND
1770 lpi->early_suspend.level =
1771 EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1772 lpi->early_suspend.suspend = cm36283_early_suspend;
1773 lpi->early_suspend.resume = cm36283_late_resume;
1774 register_early_suspend(&lpi->early_suspend);
1775#endif
1776
Oliver Wanged314dd2013-08-23 16:34:37 +08001777 mutex_init(&wq_lock);
1778 INIT_DELAYED_WORK(&lpi->ldwork, lsensor_delay_work_handler);
1779 INIT_DELAYED_WORK(&lpi->pdwork, psensor_delay_work_handler);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001780 D("[PS][CM36283] %s: Probe success!\n", __func__);
1781
1782 return ret;
1783
Oliver Wang0afd6a32013-08-26 14:23:51 +08001784err_create_ps_device_file:
Joseph Hsiao091151d2013-06-17 11:37:05 +08001785 device_unregister(lpi->ps_dev);
Oliver Wang0afd6a32013-08-26 14:23:51 +08001786err_create_ps_device:
Joseph Hsiao091151d2013-06-17 11:37:05 +08001787err_create_ls_device_file:
1788 device_unregister(lpi->ls_dev);
1789err_create_ls_device:
1790 class_destroy(lpi->cm36283_class);
1791err_create_class:
Oliver Wang0afd6a32013-08-26 14:23:51 +08001792 misc_deregister(&psensor_misc);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001793 input_unregister_device(lpi->ps_input_dev);
1794 input_free_device(lpi->ps_input_dev);
Oliver Wang0afd6a32013-08-26 14:23:51 +08001795err_psensor_setup:
1796 misc_deregister(&lightsensor_misc);
1797 input_unregister_device(lpi->ls_input_dev);
1798 input_free_device(lpi->ls_input_dev);
1799err_lightsensor_setup:
1800err_cm36283_setup:
1801 cm36283_power_set(lpi, false);
1802err_cm36283_power_on:
1803 wake_lock_destroy(&(lpi->ps_wake_lock));
1804 destroy_workqueue(lpi->lp_wq);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001805err_create_singlethread_workqueue:
1806err_lightsensor_update_table:
Joseph Hsiao091151d2013-06-17 11:37:05 +08001807 mutex_destroy(&CM36283_control_mutex);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001808 mutex_destroy(&als_enable_mutex);
1809 mutex_destroy(&als_disable_mutex);
1810 mutex_destroy(&als_get_adc_mutex);
Oliver Wang0afd6a32013-08-26 14:23:51 +08001811 mutex_destroy(&ps_enable_mutex);
1812 mutex_destroy(&ps_disable_mutex);
1813 mutex_destroy(&ps_get_adc_mutex);
Oliver Wang8a1b8be2013-08-23 13:42:10 +08001814err_parse_dt:
1815 if (client->dev.of_node && (pdata != NULL))
1816 devm_kfree(&client->dev, pdata);
Joseph Hsiao091151d2013-06-17 11:37:05 +08001817err_platform_data_null:
1818 kfree(lpi);
Oliver Wang0afd6a32013-08-26 14:23:51 +08001819 dev_err(&client->dev, "%s:error exit! ret = %d\n", __func__, ret);
1820
Joseph Hsiao091151d2013-06-17 11:37:05 +08001821 return ret;
1822}
Oliver Wanged314dd2013-08-23 16:34:37 +08001823
1824static int control_and_report(struct cm36283_info *lpi, uint8_t mode,
1825 uint16_t param, int report)
1826{
1827 int ret = 0;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001828 uint16_t adc_value = 0;
Oliver Wanged314dd2013-08-23 16:34:37 +08001829 uint16_t ps_data = 0;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001830 int level = 0, i, val;
1831
1832 mutex_lock(&CM36283_control_mutex);
1833
1834 if( mode == CONTROL_ALS ){
1835 if(param){
1836 lpi->ls_cmd &= CM36283_ALS_SD_MASK;
1837 } else {
1838 lpi->ls_cmd |= CM36283_ALS_SD;
1839 }
1840 _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
1841 lpi->als_enable=param;
1842 } else if( mode == CONTROL_PS ){
1843 if(param){
1844 lpi->ps_conf1_val &= CM36283_PS_SD_MASK;
1845 lpi->ps_conf1_val |= CM36283_PS_INT_IN_AND_OUT;
1846 } else {
1847 lpi->ps_conf1_val |= CM36283_PS_SD;
1848 lpi->ps_conf1_val &= CM36283_PS_INT_MASK;
1849 }
1850 _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val);
1851 lpi->ps_enable=param;
1852 }
1853 if((mode == CONTROL_ALS)||(mode == CONTROL_PS)){
1854 if( param==1 ){
1855 msleep(100);
1856 }
1857 }
1858
1859 if(lpi->als_enable){
1860 if( mode == CONTROL_ALS ||
1861 ( mode == CONTROL_INT_ISR_REPORT &&
1862 ((param&INT_FLAG_ALS_IF_L)||(param&INT_FLAG_ALS_IF_H)))){
1863
1864 lpi->ls_cmd &= CM36283_ALS_INT_MASK;
1865 ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
1866
1867 get_ls_adc_value(&adc_value, 0);
1868
1869 if( lpi->ls_calibrate ) {
1870 for (i = 0; i < 10; i++) {
1871 if (adc_value <= (*(lpi->cali_table + i))) {
1872 level = i;
1873 if (*(lpi->cali_table + i))
1874 break;
1875 }
1876 if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */
1877 level = i;
1878 break;
1879 }
1880 }
1881 } else {
1882 for (i = 0; i < 10; i++) {
1883 if (adc_value <= (*(lpi->adc_table + i))) {
1884 level = i;
1885 if (*(lpi->adc_table + i))
1886 break;
1887 }
1888 if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */
1889 level = i;
1890 break;
1891 }
1892 }
1893 }
Oliver Wanged314dd2013-08-23 16:34:37 +08001894 if (!lpi->polling) {
1895 ret = set_lsensor_range(((i == 0) ||
1896 (adc_value == 0)) ? 0 :
1897 *(lpi->cali_table + (i - 1)) + 1,
1898 *(lpi->cali_table + i));
1899
1900 lpi->ls_cmd |= CM36283_ALS_INT_EN;
1901 }
1902
1903 ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF,
1904 lpi->ls_cmd);
1905
1906 if (report) {
1907 lpi->current_level = level;
1908 lpi->current_adc = adc_value;
1909 input_report_abs(lpi->ls_input_dev, ABS_MISC, level);
1910 input_sync(lpi->ls_input_dev);
1911 }
Joseph Hsiao091151d2013-06-17 11:37:05 +08001912 }
1913 }
1914
1915#define PS_CLOSE 1
1916#define PS_AWAY (1<<1)
1917#define PS_CLOSE_AND_AWAY PS_CLOSE+PS_AWAY
Oliver Wanged314dd2013-08-23 16:34:37 +08001918 if (report && (lpi->ps_enable)) {
1919 int ps_status = 0;
1920 if (mode == CONTROL_PS)
1921 ps_status = PS_CLOSE_AND_AWAY;
1922 else if (mode == CONTROL_INT_ISR_REPORT) {
1923 if (param & INT_FLAG_PS_IF_CLOSE)
1924 ps_status |= PS_CLOSE;
1925 if (param & INT_FLAG_PS_IF_AWAY)
1926 ps_status |= PS_AWAY;
1927 }
Joseph Hsiao091151d2013-06-17 11:37:05 +08001928
Oliver Wanged314dd2013-08-23 16:34:37 +08001929 if (ps_status != 0) {
1930 switch (ps_status) {
1931 case PS_CLOSE_AND_AWAY:
1932 get_stable_ps_adc_value(&ps_data);
1933 val = (ps_data >= lpi->ps_close_thd_set)
1934 ? 0 : 1;
1935 break;
1936 case PS_AWAY:
1937 val = 1;
1938 break;
1939 case PS_CLOSE:
1940 val = 0;
1941 break;
1942 };
1943 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, val);
1944 input_sync(lpi->ps_input_dev);
1945 }
1946 }
Joseph Hsiao091151d2013-06-17 11:37:05 +08001947
Oliver Wanged314dd2013-08-23 16:34:37 +08001948 mutex_unlock(&CM36283_control_mutex);
1949 return ret;
Joseph Hsiao091151d2013-06-17 11:37:05 +08001950}
1951
Oliver Wanga9be2d22013-08-22 18:08:22 +08001952static int cm36283_power_set(struct cm36283_info *info, bool on)
1953{
1954 int rc;
1955
1956 if (on) {
1957 info->vdd = regulator_get(&info->i2c_client->dev, "vdd");
1958 if (IS_ERR(info->vdd)) {
1959 rc = PTR_ERR(info->vdd);
1960 dev_err(&info->i2c_client->dev,
1961 "Regulator get failed vdd rc=%d\n", rc);
1962 goto err_vdd_get;
1963 }
1964
1965 if (regulator_count_voltages(info->vdd) > 0) {
1966 rc = regulator_set_voltage(info->vdd,
1967 CM36283_VDD_MIN_UV, CM36283_VDD_MAX_UV);
1968 if (rc) {
1969 dev_err(&info->i2c_client->dev,
1970 "Regulator set failed vdd rc=%d\n", rc);
1971 goto err_vdd_set_vtg;
1972 }
1973 }
1974
1975 info->vio = regulator_get(&info->i2c_client->dev, "vio");
1976 if (IS_ERR(info->vio)) {
1977 rc = PTR_ERR(info->vio);
1978 dev_err(&info->i2c_client->dev,
1979 "Regulator get failed vio rc=%d\n", rc);
1980 goto err_vio_get;
1981 }
1982
1983 if (regulator_count_voltages(info->vio) > 0) {
1984 rc = regulator_set_voltage(info->vio,
1985 CM36283_VI2C_MIN_UV, CM36283_VI2C_MAX_UV);
1986 if (rc) {
1987 dev_err(&info->i2c_client->dev,
1988 "Regulator set failed vio rc=%d\n", rc);
1989 goto err_vio_set_vtg;
1990 }
1991 }
1992
1993 rc = regulator_enable(info->vdd);
1994 if (rc) {
1995 dev_err(&info->i2c_client->dev,
1996 "Regulator vdd enable failed rc=%d\n", rc);
1997 goto err_vdd_ena;
1998 }
1999
2000 rc = regulator_enable(info->vio);
2001 if (rc) {
2002 dev_err(&info->i2c_client->dev,
2003 "Regulator vio enable failed rc=%d\n", rc);
2004 goto err_vio_ena;
2005 }
2006
2007 } else {
2008 rc = regulator_disable(info->vdd);
2009 if (rc) {
2010 dev_err(&info->i2c_client->dev,
2011 "Regulator vdd disable failed rc=%d\n", rc);
2012 return rc;
2013 }
2014 if (regulator_count_voltages(info->vdd) > 0)
2015 regulator_set_voltage(info->vdd, 0, CM36283_VDD_MAX_UV);
2016
2017 regulator_put(info->vdd);
2018
2019 rc = regulator_disable(info->vio);
2020 if (rc) {
2021 dev_err(&info->i2c_client->dev,
2022 "Regulator vio disable failed rc=%d\n", rc);
2023 return rc;
2024 }
2025 if (regulator_count_voltages(info->vio) > 0)
2026 regulator_set_voltage(info->vio, 0,
2027 CM36283_VI2C_MAX_UV);
2028
2029 regulator_put(info->vio);
2030 }
2031
2032 return 0;
2033
2034err_vio_ena:
2035 regulator_disable(info->vdd);
2036err_vdd_ena:
2037 if (regulator_count_voltages(info->vio) > 0)
2038 regulator_set_voltage(info->vio, 0, CM36283_VI2C_MAX_UV);
2039err_vio_set_vtg:
2040 regulator_put(info->vio);
2041err_vio_get:
2042 if (regulator_count_voltages(info->vdd) > 0)
2043 regulator_set_voltage(info->vdd, 0, CM36283_VDD_MAX_UV);
2044err_vdd_set_vtg:
2045 regulator_put(info->vdd);
2046err_vdd_get:
2047 return rc;
2048}
2049
2050#ifdef CONFIG_PM_SLEEP
2051static int cm36283_suspend(struct device *dev)
2052{
2053 struct cm36283_info *lpi = lp_info;
2054
2055 if (lpi->als_enable) {
2056 lightsensor_disable(lpi);
2057 lpi->als_enable = 1;
2058 }
2059 cm36283_power_set(lpi, 0);
2060
2061 return 0;
2062}
2063
2064static int cm36283_resume(struct device *dev)
2065{
2066 struct cm36283_info *lpi = lp_info;
2067
2068 cm36283_power_set(lpi, 1);
2069
2070 if (lpi->als_enable) {
2071 cm36283_setup(lpi);
2072 lightsensor_setup(lpi);
2073 psensor_setup(lpi);
2074 lightsensor_enable(lpi);
2075 }
2076 return 0;
2077}
2078#endif
2079
2080static UNIVERSAL_DEV_PM_OPS(cm36283_pm, cm36283_suspend, cm36283_resume, NULL);
Joseph Hsiao091151d2013-06-17 11:37:05 +08002081
2082static const struct i2c_device_id cm36283_i2c_id[] = {
2083 {CM36283_I2C_NAME, 0},
2084 {}
2085};
2086
Oliver Wang8a1b8be2013-08-23 13:42:10 +08002087static struct of_device_id cm36283_match_table[] = {
2088 { .compatible = "capella,cm36283",},
2089 { },
2090};
2091
Joseph Hsiao091151d2013-06-17 11:37:05 +08002092static struct i2c_driver cm36283_driver = {
2093 .id_table = cm36283_i2c_id,
2094 .probe = cm36283_probe,
2095 .driver = {
2096 .name = CM36283_I2C_NAME,
2097 .owner = THIS_MODULE,
Oliver Wanga9be2d22013-08-22 18:08:22 +08002098 .pm = &cm36283_pm,
Oliver Wang8a1b8be2013-08-23 13:42:10 +08002099 .of_match_table = cm36283_match_table,
Joseph Hsiao091151d2013-06-17 11:37:05 +08002100 },
2101};
2102
2103static int __init cm36283_init(void)
2104{
2105 return i2c_add_driver(&cm36283_driver);
2106}
2107
2108static void __exit cm36283_exit(void)
2109{
2110 i2c_del_driver(&cm36283_driver);
2111}
2112
2113module_init(cm36283_init);
2114module_exit(cm36283_exit);
2115
2116MODULE_LICENSE("GPL");
2117MODULE_DESCRIPTION("CM36283 Driver");
2118MODULE_AUTHOR("Frank Hsieh <pengyueh@gmail.com>");