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