Joseph Hsiao | 091151d | 2013-06-17 11:37:05 +0800 | [diff] [blame^] | 1 | /* drivers/input/misc/cm36283.c - cm36283 optical sensors driver |
| 2 | * |
| 3 | * Copyright (C) 2012 Capella Microsystems Inc. |
| 4 | * Author: Frank Hsieh <pengyueh@gmail.com> |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/earlysuspend.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/gpio.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/lightsensor.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | #include <asm/mach-types.h> |
| 34 | #include <linux/cm36283.h> |
| 35 | #include <linux/capella_cm3602.h> |
| 36 | #include <asm/setup.h> |
| 37 | #include <linux/wakelock.h> |
| 38 | #include <linux/jiffies.h> |
| 39 | |
| 40 | #define D(x...) pr_info(x) |
| 41 | |
| 42 | #define I2C_RETRY_COUNT 10 |
| 43 | |
| 44 | #define NEAR_DELAY_TIME ((100 * HZ) / 1000) |
| 45 | |
| 46 | #define CONTROL_INT_ISR_REPORT 0x00 |
| 47 | #define CONTROL_ALS 0x01 |
| 48 | #define CONTROL_PS 0x02 |
| 49 | |
| 50 | static int record_init_fail = 0; |
| 51 | static void sensor_irq_do_work(struct work_struct *work); |
| 52 | static DECLARE_WORK(sensor_irq_work, sensor_irq_do_work); |
| 53 | |
| 54 | struct cm36283_info { |
| 55 | struct class *cm36283_class; |
| 56 | struct device *ls_dev; |
| 57 | struct device *ps_dev; |
| 58 | |
| 59 | struct input_dev *ls_input_dev; |
| 60 | struct input_dev *ps_input_dev; |
| 61 | |
| 62 | struct early_suspend early_suspend; |
| 63 | struct i2c_client *i2c_client; |
| 64 | struct workqueue_struct *lp_wq; |
| 65 | |
| 66 | int intr_pin; |
| 67 | int als_enable; |
| 68 | int ps_enable; |
| 69 | int ps_irq_flag; |
| 70 | |
| 71 | uint16_t *adc_table; |
| 72 | uint16_t cali_table[10]; |
| 73 | int irq; |
| 74 | |
| 75 | int ls_calibrate; |
| 76 | |
| 77 | int (*power)(int, uint8_t); /* power to the chip */ |
| 78 | |
| 79 | uint32_t als_kadc; |
| 80 | uint32_t als_gadc; |
| 81 | uint16_t golden_adc; |
| 82 | |
| 83 | struct wake_lock ps_wake_lock; |
| 84 | int psensor_opened; |
| 85 | int lightsensor_opened; |
| 86 | uint8_t slave_addr; |
| 87 | |
| 88 | uint8_t ps_close_thd_set; |
| 89 | uint8_t ps_away_thd_set; |
| 90 | int current_level; |
| 91 | uint16_t current_adc; |
| 92 | |
| 93 | uint16_t ps_conf1_val; |
| 94 | uint16_t ps_conf3_val; |
| 95 | |
| 96 | uint16_t ls_cmd; |
| 97 | uint8_t record_clear_int_fail; |
| 98 | }; |
| 99 | struct cm36283_info *lp_info; |
| 100 | int fLevel=-1; |
| 101 | static struct mutex als_enable_mutex, als_disable_mutex, als_get_adc_mutex; |
| 102 | static struct mutex ps_enable_mutex, ps_disable_mutex, ps_get_adc_mutex; |
| 103 | static struct mutex CM36283_control_mutex; |
| 104 | static int lightsensor_enable(struct cm36283_info *lpi); |
| 105 | static int lightsensor_disable(struct cm36283_info *lpi); |
| 106 | static int initial_cm36283(struct cm36283_info *lpi); |
| 107 | static void psensor_initial_cmd(struct cm36283_info *lpi); |
| 108 | |
| 109 | int32_t als_kadc; |
| 110 | |
| 111 | static int control_and_report(struct cm36283_info *lpi, uint8_t mode, uint16_t param); |
| 112 | |
| 113 | static int I2C_RxData(uint16_t slaveAddr, uint8_t cmd, uint8_t *rxData, int length) |
| 114 | { |
| 115 | uint8_t loop_i; |
| 116 | int val; |
| 117 | struct cm36283_info *lpi = lp_info; |
| 118 | uint8_t subaddr[1]; |
| 119 | |
| 120 | struct i2c_msg msgs[] = { |
| 121 | { |
| 122 | .addr = slaveAddr, |
| 123 | .flags = 0, |
| 124 | .len = 1, |
| 125 | .buf = subaddr, |
| 126 | }, |
| 127 | { |
| 128 | .addr = slaveAddr, |
| 129 | .flags = I2C_M_RD, |
| 130 | .len = length, |
| 131 | .buf = rxData, |
| 132 | }, |
| 133 | }; |
| 134 | subaddr[0] = cmd; |
| 135 | |
| 136 | for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) { |
| 137 | |
| 138 | if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 2) > 0) |
| 139 | break; |
| 140 | |
| 141 | val = gpio_get_value(lpi->intr_pin); |
| 142 | /*check intr GPIO when i2c error*/ |
| 143 | if (loop_i == 0 || loop_i == I2C_RETRY_COUNT -1) |
| 144 | D("[PS][CM36283 error] %s, i2c err, slaveAddr 0x%x ISR gpio %d = %d, record_init_fail %d \n", |
| 145 | __func__, slaveAddr, lpi->intr_pin, val, record_init_fail); |
| 146 | |
| 147 | msleep(10); |
| 148 | } |
| 149 | if (loop_i >= I2C_RETRY_COUNT) { |
| 150 | printk(KERN_ERR "[PS_ERR][CM36283 error] %s retry over %d\n", |
| 151 | __func__, I2C_RETRY_COUNT); |
| 152 | return -EIO; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int I2C_TxData(uint16_t slaveAddr, uint8_t *txData, int length) |
| 159 | { |
| 160 | uint8_t loop_i; |
| 161 | int val; |
| 162 | struct cm36283_info *lpi = lp_info; |
| 163 | struct i2c_msg msg[] = { |
| 164 | { |
| 165 | .addr = slaveAddr, |
| 166 | .flags = 0, |
| 167 | .len = length, |
| 168 | .buf = txData, |
| 169 | }, |
| 170 | }; |
| 171 | |
| 172 | for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) { |
| 173 | if (i2c_transfer(lp_info->i2c_client->adapter, msg, 1) > 0) |
| 174 | break; |
| 175 | |
| 176 | val = gpio_get_value(lpi->intr_pin); |
| 177 | /*check intr GPIO when i2c error*/ |
| 178 | if (loop_i == 0 || loop_i == I2C_RETRY_COUNT -1) |
| 179 | D("[PS][CM36283 error] %s, i2c err, slaveAddr 0x%x, value 0x%x, ISR gpio%d = %d, record_init_fail %d\n", |
| 180 | __func__, slaveAddr, txData[0], lpi->intr_pin, val, record_init_fail); |
| 181 | |
| 182 | msleep(10); |
| 183 | } |
| 184 | |
| 185 | if (loop_i >= I2C_RETRY_COUNT) { |
| 186 | printk(KERN_ERR "[PS_ERR][CM36283 error] %s retry over %d\n", |
| 187 | __func__, I2C_RETRY_COUNT); |
| 188 | return -EIO; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int _cm36283_I2C_Read_Word(uint16_t slaveAddr, uint8_t cmd, uint16_t *pdata) |
| 195 | { |
| 196 | uint8_t buffer[2]; |
| 197 | int ret = 0; |
| 198 | |
| 199 | if (pdata == NULL) |
| 200 | return -EFAULT; |
| 201 | |
| 202 | ret = I2C_RxData(slaveAddr, cmd, buffer, 2); |
| 203 | if (ret < 0) { |
| 204 | pr_err( |
| 205 | "[PS_ERR][CM3218 error]%s: I2C_RxData fail [0x%x, 0x%x]\n", |
| 206 | __func__, slaveAddr, cmd); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | *pdata = (buffer[1]<<8)|buffer[0]; |
| 211 | #if 0 |
| 212 | /* Debug use */ |
| 213 | printk(KERN_DEBUG "[CM3218] %s: I2C_RxData[0x%x, 0x%x] = 0x%x\n", |
| 214 | __func__, slaveAddr, cmd, *pdata); |
| 215 | #endif |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int _cm36283_I2C_Write_Word(uint16_t SlaveAddress, uint8_t cmd, uint16_t data) |
| 220 | { |
| 221 | char buffer[3]; |
| 222 | int ret = 0; |
| 223 | #if 0 |
| 224 | /* Debug use */ |
| 225 | printk(KERN_DEBUG |
| 226 | "[CM3218] %s: _cm36283_I2C_Write_Word[0x%x, 0x%x, 0x%x]\n", |
| 227 | __func__, SlaveAddress, cmd, data); |
| 228 | #endif |
| 229 | buffer[0] = cmd; |
| 230 | buffer[1] = (uint8_t)(data&0xff); |
| 231 | buffer[2] = (uint8_t)((data&0xff00)>>8); |
| 232 | |
| 233 | ret = I2C_TxData(SlaveAddress, buffer, 3); |
| 234 | if (ret < 0) { |
| 235 | pr_err("[PS_ERR][CM3218 error]%s: I2C_TxData fail\n", __func__); |
| 236 | return -EIO; |
| 237 | } |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static int get_ls_adc_value(uint16_t *als_step, bool resume) |
| 243 | { |
| 244 | struct cm36283_info *lpi = lp_info; |
| 245 | uint32_t tmpResult; |
| 246 | int ret = 0; |
| 247 | |
| 248 | if (als_step == NULL) |
| 249 | return -EFAULT; |
| 250 | |
| 251 | /* Read ALS data: */ |
| 252 | ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_DATA, als_step); |
| 253 | if (ret < 0) { |
| 254 | pr_err( |
| 255 | "[LS][CM3218 error]%s: _cm36283_I2C_Read_Word fail\n", |
| 256 | __func__); |
| 257 | return -EIO; |
| 258 | } |
| 259 | |
| 260 | if (!lpi->ls_calibrate) { |
| 261 | tmpResult = (uint32_t)(*als_step) * lpi->als_gadc / lpi->als_kadc; |
| 262 | if (tmpResult > 0xFFFF) |
| 263 | *als_step = 0xFFFF; |
| 264 | else |
| 265 | *als_step = tmpResult; |
| 266 | } |
| 267 | |
| 268 | D("[LS][CM3218] %s: raw adc = 0x%X, ls_calibrate = %d\n", |
| 269 | __func__, *als_step, lpi->ls_calibrate); |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static int set_lsensor_range(uint16_t low_thd, uint16_t high_thd) |
| 275 | { |
| 276 | int ret = 0; |
| 277 | struct cm36283_info *lpi = lp_info; |
| 278 | |
| 279 | _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDH, high_thd); |
| 280 | _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDL, low_thd); |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int get_ps_adc_value(uint16_t *data) |
| 286 | { |
| 287 | int ret = 0; |
| 288 | struct cm36283_info *lpi = lp_info; |
| 289 | |
| 290 | if (data == NULL) |
| 291 | return -EFAULT; |
| 292 | |
| 293 | ret = _cm36283_I2C_Read_Word(lpi->slave_addr, PS_DATA, data); |
| 294 | |
| 295 | (*data) &= 0xFF; |
| 296 | |
| 297 | if (ret < 0) { |
| 298 | pr_err( |
| 299 | "[PS][CM36283 error]%s: _cm36283_I2C_Read_Word fail\n", |
| 300 | __func__); |
| 301 | return -EIO; |
| 302 | } else { |
| 303 | pr_err( |
| 304 | "[PS][CM36283 OK]%s: _cm36283_I2C_Read_Word OK 0x%x\n", |
| 305 | __func__, *data); |
| 306 | } |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static uint16_t mid_value(uint16_t value[], uint8_t size) |
| 312 | { |
| 313 | int i = 0, j = 0; |
| 314 | uint16_t temp = 0; |
| 315 | |
| 316 | if (size < 3) |
| 317 | return 0; |
| 318 | |
| 319 | for (i = 0; i < (size - 1); i++) |
| 320 | for (j = (i + 1); j < size; j++) |
| 321 | if (value[i] > value[j]) { |
| 322 | temp = value[i]; |
| 323 | value[i] = value[j]; |
| 324 | value[j] = temp; |
| 325 | } |
| 326 | return value[((size - 1) / 2)]; |
| 327 | } |
| 328 | |
| 329 | static int get_stable_ps_adc_value(uint16_t *ps_adc) |
| 330 | { |
| 331 | uint16_t value[3] = {0, 0, 0}, mid_val = 0; |
| 332 | int ret = 0; |
| 333 | int i = 0; |
| 334 | int wait_count = 0; |
| 335 | struct cm36283_info *lpi = lp_info; |
| 336 | |
| 337 | for (i = 0; i < 3; i++) { |
| 338 | /*wait interrupt GPIO high*/ |
| 339 | while (gpio_get_value(lpi->intr_pin) == 0) { |
| 340 | msleep(10); |
| 341 | wait_count++; |
| 342 | if (wait_count > 12) { |
| 343 | pr_err("[PS_ERR][CM36283 error]%s: interrupt GPIO low," |
| 344 | " get_ps_adc_value\n", __func__); |
| 345 | return -EIO; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | ret = get_ps_adc_value(&value[i]); |
| 350 | if (ret < 0) { |
| 351 | pr_err("[PS_ERR][CM36283 error]%s: get_ps_adc_value\n", |
| 352 | __func__); |
| 353 | return -EIO; |
| 354 | } |
| 355 | |
| 356 | if (wait_count < 60/10) {/*wait gpio less than 60ms*/ |
| 357 | msleep(60 - (10*wait_count)); |
| 358 | } |
| 359 | wait_count = 0; |
| 360 | } |
| 361 | |
| 362 | /*D("Sta_ps: Before sort, value[0, 1, 2] = [0x%x, 0x%x, 0x%x]", |
| 363 | value[0], value[1], value[2]);*/ |
| 364 | mid_val = mid_value(value, 3); |
| 365 | D("Sta_ps: After sort, value[0, 1, 2] = [0x%x, 0x%x, 0x%x]", |
| 366 | value[0], value[1], value[2]); |
| 367 | *ps_adc = (mid_val & 0xFF); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void sensor_irq_do_work(struct work_struct *work) |
| 373 | { |
| 374 | struct cm36283_info *lpi = lp_info; |
| 375 | uint16_t intFlag; |
| 376 | _cm36283_I2C_Read_Word(lpi->slave_addr, INT_FLAG, &intFlag); |
| 377 | control_and_report(lpi, CONTROL_INT_ISR_REPORT, intFlag); |
| 378 | |
| 379 | enable_irq(lpi->irq); |
| 380 | } |
| 381 | |
| 382 | static irqreturn_t cm36283_irq_handler(int irq, void *data) |
| 383 | { |
| 384 | struct cm36283_info *lpi = data; |
| 385 | |
| 386 | disable_irq_nosync(lpi->irq); |
| 387 | queue_work(lpi->lp_wq, &sensor_irq_work); |
| 388 | |
| 389 | return IRQ_HANDLED; |
| 390 | } |
| 391 | |
| 392 | static int als_power(int enable) |
| 393 | { |
| 394 | struct cm36283_info *lpi = lp_info; |
| 395 | |
| 396 | if (lpi->power) |
| 397 | lpi->power(LS_PWR_ON, 1); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void ls_initial_cmd(struct cm36283_info *lpi) |
| 403 | { |
| 404 | /*must disable l-sensor interrupt befrore IST create*//*disable ALS func*/ |
| 405 | lpi->ls_cmd &= CM36283_ALS_INT_MASK; |
| 406 | lpi->ls_cmd |= CM36283_ALS_SD; |
| 407 | _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd); |
| 408 | } |
| 409 | |
| 410 | static void psensor_initial_cmd(struct cm36283_info *lpi) |
| 411 | { |
| 412 | /*must disable p-sensor interrupt befrore IST create*//*disable ALS func*/ |
| 413 | lpi->ps_conf1_val |= CM36283_PS_SD; |
| 414 | lpi->ps_conf1_val &= CM36283_PS_INT_MASK; |
| 415 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val); |
| 416 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val); |
| 417 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_THD, (lpi->ps_close_thd_set <<8)| lpi->ps_away_thd_set); |
| 418 | |
| 419 | D("[PS][CM36283] %s, finish\n", __func__); |
| 420 | } |
| 421 | |
| 422 | static int psensor_enable(struct cm36283_info *lpi) |
| 423 | { |
| 424 | int ret = -EIO; |
| 425 | |
| 426 | mutex_lock(&ps_enable_mutex); |
| 427 | D("[PS][CM36283] %s\n", __func__); |
| 428 | |
| 429 | if ( lpi->ps_enable ) { |
| 430 | D("[PS][CM36283] %s: already enabled\n", __func__); |
| 431 | ret = 0; |
| 432 | } else |
| 433 | ret = control_and_report(lpi, CONTROL_PS, 1); |
| 434 | |
| 435 | mutex_unlock(&ps_enable_mutex); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int psensor_disable(struct cm36283_info *lpi) |
| 440 | { |
| 441 | int ret = -EIO; |
| 442 | |
| 443 | mutex_lock(&ps_disable_mutex); |
| 444 | D("[PS][CM36283] %s\n", __func__); |
| 445 | |
| 446 | if ( lpi->ps_enable == 0 ) { |
| 447 | D("[PS][CM36283] %s: already disabled\n", __func__); |
| 448 | ret = 0; |
| 449 | } else |
| 450 | ret = control_and_report(lpi, CONTROL_PS,0); |
| 451 | |
| 452 | mutex_unlock(&ps_disable_mutex); |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | static int psensor_open(struct inode *inode, struct file *file) |
| 457 | { |
| 458 | struct cm36283_info *lpi = lp_info; |
| 459 | |
| 460 | D("[PS][CM36283] %s\n", __func__); |
| 461 | |
| 462 | if (lpi->psensor_opened) |
| 463 | return -EBUSY; |
| 464 | |
| 465 | lpi->psensor_opened = 1; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int psensor_release(struct inode *inode, struct file *file) |
| 471 | { |
| 472 | struct cm36283_info *lpi = lp_info; |
| 473 | |
| 474 | D("[PS][CM36283] %s\n", __func__); |
| 475 | |
| 476 | lpi->psensor_opened = 0; |
| 477 | |
| 478 | return psensor_disable(lpi); |
| 479 | //return 0; |
| 480 | } |
| 481 | |
| 482 | static long psensor_ioctl(struct file *file, unsigned int cmd, |
| 483 | unsigned long arg) |
| 484 | { |
| 485 | int val; |
| 486 | struct cm36283_info *lpi = lp_info; |
| 487 | |
| 488 | D("[PS][CM36283] %s cmd %d\n", __func__, _IOC_NR(cmd)); |
| 489 | |
| 490 | switch (cmd) { |
| 491 | case CAPELLA_CM3602_IOCTL_ENABLE: |
| 492 | if (get_user(val, (unsigned long __user *)arg)) |
| 493 | return -EFAULT; |
| 494 | if (val) |
| 495 | return psensor_enable(lpi); |
| 496 | else |
| 497 | return psensor_disable(lpi); |
| 498 | break; |
| 499 | case CAPELLA_CM3602_IOCTL_GET_ENABLED: |
| 500 | return put_user(lpi->ps_enable, (unsigned long __user *)arg); |
| 501 | break; |
| 502 | default: |
| 503 | pr_err("[PS][CM36283 error]%s: invalid cmd %d\n", |
| 504 | __func__, _IOC_NR(cmd)); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static const struct file_operations psensor_fops = { |
| 510 | .owner = THIS_MODULE, |
| 511 | .open = psensor_open, |
| 512 | .release = psensor_release, |
| 513 | .unlocked_ioctl = psensor_ioctl |
| 514 | }; |
| 515 | |
| 516 | struct miscdevice psensor_misc = { |
| 517 | .minor = MISC_DYNAMIC_MINOR, |
| 518 | .name = "proximity", |
| 519 | .fops = &psensor_fops |
| 520 | }; |
| 521 | |
| 522 | void lightsensor_set_kvalue(struct cm36283_info *lpi) |
| 523 | { |
| 524 | if (!lpi) { |
| 525 | pr_err("[LS][CM36283 error]%s: ls_info is empty\n", __func__); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | D("[LS][CM36283] %s: ALS calibrated als_kadc=0x%x\n", |
| 530 | __func__, als_kadc); |
| 531 | |
| 532 | if (als_kadc >> 16 == ALS_CALIBRATED) |
| 533 | lpi->als_kadc = als_kadc & 0xFFFF; |
| 534 | else { |
| 535 | lpi->als_kadc = 0; |
| 536 | D("[LS][CM36283] %s: no ALS calibrated\n", __func__); |
| 537 | } |
| 538 | |
| 539 | if (lpi->als_kadc && lpi->golden_adc > 0) { |
| 540 | lpi->als_kadc = (lpi->als_kadc > 0 && lpi->als_kadc < 0x1000) ? |
| 541 | lpi->als_kadc : lpi->golden_adc; |
| 542 | lpi->als_gadc = lpi->golden_adc; |
| 543 | } else { |
| 544 | lpi->als_kadc = 1; |
| 545 | lpi->als_gadc = 1; |
| 546 | } |
| 547 | D("[LS][CM36283] %s: als_kadc=0x%x, als_gadc=0x%x\n", |
| 548 | __func__, lpi->als_kadc, lpi->als_gadc); |
| 549 | } |
| 550 | |
| 551 | |
| 552 | static int lightsensor_update_table(struct cm36283_info *lpi) |
| 553 | { |
| 554 | uint32_t tmpData[10]; |
| 555 | int i; |
| 556 | for (i = 0; i < 10; i++) { |
| 557 | tmpData[i] = (uint32_t)(*(lpi->adc_table + i)) |
| 558 | * lpi->als_kadc / lpi->als_gadc ; |
| 559 | if( tmpData[i] <= 0xFFFF ){ |
| 560 | lpi->cali_table[i] = (uint16_t) tmpData[i]; |
| 561 | } else { |
| 562 | lpi->cali_table[i] = 0xFFFF; |
| 563 | } |
| 564 | D("[LS][CM36283] %s: Calibrated adc_table: data[%d], %x\n", |
| 565 | __func__, i, lpi->cali_table[i]); |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | static int lightsensor_enable(struct cm36283_info *lpi) |
| 573 | { |
| 574 | int ret = -EIO; |
| 575 | |
| 576 | mutex_lock(&als_enable_mutex); |
| 577 | D("[LS][CM36283] %s\n", __func__); |
| 578 | |
| 579 | if (lpi->als_enable) { |
| 580 | D("[LS][CM36283] %s: already enabled\n", __func__); |
| 581 | ret = 0; |
| 582 | } else |
| 583 | ret = control_and_report(lpi, CONTROL_ALS, 1); |
| 584 | |
| 585 | mutex_unlock(&als_enable_mutex); |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | static int lightsensor_disable(struct cm36283_info *lpi) |
| 590 | { |
| 591 | int ret = -EIO; |
| 592 | mutex_lock(&als_disable_mutex); |
| 593 | D("[LS][CM36283] %s\n", __func__); |
| 594 | |
| 595 | if ( lpi->als_enable == 0 ) { |
| 596 | D("[LS][CM36283] %s: already disabled\n", __func__); |
| 597 | ret = 0; |
| 598 | } else |
| 599 | ret = control_and_report(lpi, CONTROL_ALS, 0); |
| 600 | |
| 601 | mutex_unlock(&als_disable_mutex); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static int lightsensor_open(struct inode *inode, struct file *file) |
| 606 | { |
| 607 | struct cm36283_info *lpi = lp_info; |
| 608 | int rc = 0; |
| 609 | |
| 610 | D("[LS][CM36283] %s\n", __func__); |
| 611 | if (lpi->lightsensor_opened) { |
| 612 | pr_err("[LS][CM36283 error]%s: already opened\n", __func__); |
| 613 | rc = -EBUSY; |
| 614 | } |
| 615 | lpi->lightsensor_opened = 1; |
| 616 | return rc; |
| 617 | } |
| 618 | |
| 619 | static int lightsensor_release(struct inode *inode, struct file *file) |
| 620 | { |
| 621 | struct cm36283_info *lpi = lp_info; |
| 622 | |
| 623 | D("[LS][CM36283] %s\n", __func__); |
| 624 | lpi->lightsensor_opened = 0; |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static long lightsensor_ioctl(struct file *file, unsigned int cmd, |
| 629 | unsigned long arg) |
| 630 | { |
| 631 | int rc, val; |
| 632 | struct cm36283_info *lpi = lp_info; |
| 633 | |
| 634 | /*D("[CM36283] %s cmd %d\n", __func__, _IOC_NR(cmd));*/ |
| 635 | |
| 636 | switch (cmd) { |
| 637 | case LIGHTSENSOR_IOCTL_ENABLE: |
| 638 | if (get_user(val, (unsigned long __user *)arg)) { |
| 639 | rc = -EFAULT; |
| 640 | break; |
| 641 | } |
| 642 | D("[LS][CM36283] %s LIGHTSENSOR_IOCTL_ENABLE, value = %d\n", |
| 643 | __func__, val); |
| 644 | rc = val ? lightsensor_enable(lpi) : lightsensor_disable(lpi); |
| 645 | break; |
| 646 | case LIGHTSENSOR_IOCTL_GET_ENABLED: |
| 647 | val = lpi->als_enable; |
| 648 | D("[LS][CM36283] %s LIGHTSENSOR_IOCTL_GET_ENABLED, enabled %d\n", |
| 649 | __func__, val); |
| 650 | rc = put_user(val, (unsigned long __user *)arg); |
| 651 | break; |
| 652 | default: |
| 653 | pr_err("[LS][CM36283 error]%s: invalid cmd %d\n", |
| 654 | __func__, _IOC_NR(cmd)); |
| 655 | rc = -EINVAL; |
| 656 | } |
| 657 | |
| 658 | return rc; |
| 659 | } |
| 660 | |
| 661 | static const struct file_operations lightsensor_fops = { |
| 662 | .owner = THIS_MODULE, |
| 663 | .open = lightsensor_open, |
| 664 | .release = lightsensor_release, |
| 665 | .unlocked_ioctl = lightsensor_ioctl |
| 666 | }; |
| 667 | |
| 668 | static struct miscdevice lightsensor_misc = { |
| 669 | .minor = MISC_DYNAMIC_MINOR, |
| 670 | .name = "lightsensor", |
| 671 | .fops = &lightsensor_fops |
| 672 | }; |
| 673 | |
| 674 | |
| 675 | static ssize_t ps_adc_show(struct device *dev, |
| 676 | struct device_attribute *attr, char *buf) |
| 677 | { |
| 678 | |
| 679 | uint16_t value; |
| 680 | int ret; |
| 681 | struct cm36283_info *lpi = lp_info; |
| 682 | int intr_val; |
| 683 | |
| 684 | intr_val = gpio_get_value(lpi->intr_pin); |
| 685 | |
| 686 | get_ps_adc_value(&value); |
| 687 | |
| 688 | ret = sprintf(buf, "ADC[0x%04X], ENABLE = %d, intr_pin = %d\n", value, lpi->ps_enable, intr_val); |
| 689 | |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static ssize_t ps_enable_store(struct device *dev, |
| 694 | struct device_attribute *attr, |
| 695 | const char *buf, size_t count) |
| 696 | { |
| 697 | int ps_en; |
| 698 | struct cm36283_info *lpi = lp_info; |
| 699 | |
| 700 | ps_en = -1; |
| 701 | sscanf(buf, "%d", &ps_en); |
| 702 | |
| 703 | if (ps_en != 0 && ps_en != 1 |
| 704 | && ps_en != 10 && ps_en != 13 && ps_en != 16) |
| 705 | return -EINVAL; |
| 706 | |
| 707 | if (ps_en) { |
| 708 | D("[PS][CM36283] %s: ps_en=%d\n", |
| 709 | __func__, ps_en); |
| 710 | psensor_enable(lpi); |
| 711 | } else |
| 712 | psensor_disable(lpi); |
| 713 | |
| 714 | D("[PS][CM36283] %s\n", __func__); |
| 715 | |
| 716 | return count; |
| 717 | } |
| 718 | |
| 719 | static DEVICE_ATTR(ps_adc, 0664, ps_adc_show, ps_enable_store); |
| 720 | |
| 721 | unsigned PS_cmd_test_value; |
| 722 | static ssize_t ps_parameters_show(struct device *dev, |
| 723 | struct device_attribute *attr, char *buf) |
| 724 | { |
| 725 | int ret; |
| 726 | struct cm36283_info *lpi = lp_info; |
| 727 | |
| 728 | ret = sprintf(buf, "PS_close_thd_set = 0x%x, PS_away_thd_set = 0x%x, PS_cmd_cmd:value = 0x%x\n", |
| 729 | lpi->ps_close_thd_set, lpi->ps_away_thd_set, PS_cmd_test_value); |
| 730 | |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | static ssize_t ps_parameters_store(struct device *dev, |
| 735 | struct device_attribute *attr, |
| 736 | const char *buf, size_t count) |
| 737 | { |
| 738 | |
| 739 | struct cm36283_info *lpi = lp_info; |
| 740 | char *token[10]; |
| 741 | int i; |
| 742 | |
| 743 | printk(KERN_INFO "[PS][CM36283] %s\n", buf); |
| 744 | for (i = 0; i < 3; i++) |
| 745 | token[i] = strsep((char **)&buf, " "); |
| 746 | |
| 747 | lpi->ps_close_thd_set = simple_strtoul(token[0], NULL, 16); |
| 748 | lpi->ps_away_thd_set = simple_strtoul(token[1], NULL, 16); |
| 749 | PS_cmd_test_value = simple_strtoul(token[2], NULL, 16); |
| 750 | printk(KERN_INFO |
| 751 | "[PS][CM36283]Set PS_close_thd_set = 0x%x, PS_away_thd_set = 0x%x, PS_cmd_cmd:value = 0x%x\n", |
| 752 | lpi->ps_close_thd_set, lpi->ps_away_thd_set, PS_cmd_test_value); |
| 753 | |
| 754 | D("[PS][CM36283] %s\n", __func__); |
| 755 | |
| 756 | return count; |
| 757 | } |
| 758 | |
| 759 | static DEVICE_ATTR(ps_parameters, 0664, |
| 760 | ps_parameters_show, ps_parameters_store); |
| 761 | |
| 762 | |
| 763 | static ssize_t ps_conf_show(struct device *dev, |
| 764 | struct device_attribute *attr, char *buf) |
| 765 | { |
| 766 | struct cm36283_info *lpi = lp_info; |
| 767 | return sprintf(buf, "PS_CONF1 = 0x%x, PS_CONF3 = 0x%x\n", lpi->ps_conf1_val, lpi->ps_conf3_val); |
| 768 | } |
| 769 | static ssize_t ps_conf_store(struct device *dev, |
| 770 | struct device_attribute *attr, |
| 771 | const char *buf, size_t count) |
| 772 | { |
| 773 | int code1, code2; |
| 774 | struct cm36283_info *lpi = lp_info; |
| 775 | |
| 776 | sscanf(buf, "0x%x 0x%x", &code1, &code2); |
| 777 | |
| 778 | D("[PS]%s: store value PS conf1 reg = 0x%x PS conf3 reg = 0x%x\n", __func__, code1, code2); |
| 779 | |
| 780 | lpi->ps_conf1_val = code1; |
| 781 | lpi->ps_conf3_val = code2; |
| 782 | |
| 783 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val ); |
| 784 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val ); |
| 785 | |
| 786 | return count; |
| 787 | } |
| 788 | static DEVICE_ATTR(ps_conf, 0664, ps_conf_show, ps_conf_store); |
| 789 | |
| 790 | static ssize_t ps_thd_show(struct device *dev, |
| 791 | struct device_attribute *attr, char *buf) |
| 792 | { |
| 793 | int ret; |
| 794 | struct cm36283_info *lpi = lp_info; |
| 795 | 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); |
| 796 | return ret; |
| 797 | } |
| 798 | static ssize_t ps_thd_store(struct device *dev, |
| 799 | struct device_attribute *attr, |
| 800 | const char *buf, size_t count) |
| 801 | { |
| 802 | int code; |
| 803 | struct cm36283_info *lpi = lp_info; |
| 804 | |
| 805 | sscanf(buf, "0x%x", &code); |
| 806 | |
| 807 | D("[PS]%s: store value = 0x%x\n", __func__, code); |
| 808 | |
| 809 | lpi->ps_away_thd_set = code &0xFF; |
| 810 | lpi->ps_close_thd_set = (code &0xFF00)>>8; |
| 811 | |
| 812 | 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); |
| 813 | |
| 814 | return count; |
| 815 | } |
| 816 | static DEVICE_ATTR(ps_thd, 0664, ps_thd_show, ps_thd_store); |
| 817 | |
| 818 | static ssize_t ps_hw_show(struct device *dev, |
| 819 | struct device_attribute *attr, char *buf) |
| 820 | { |
| 821 | int ret = 0; |
| 822 | struct cm36283_info *lpi = lp_info; |
| 823 | |
| 824 | ret = sprintf(buf, "PS1: reg = 0x%x, PS3: reg = 0x%x, ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n", |
| 825 | lpi->ps_conf1_val, lpi->ps_conf3_val, lpi->ps_close_thd_set, lpi->ps_away_thd_set); |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | static ssize_t ps_hw_store(struct device *dev, |
| 830 | struct device_attribute *attr, |
| 831 | const char *buf, size_t count) |
| 832 | { |
| 833 | int code; |
| 834 | // struct cm36283_info *lpi = lp_info; |
| 835 | |
| 836 | sscanf(buf, "0x%x", &code); |
| 837 | |
| 838 | D("[PS]%s: store value = 0x%x\n", __func__, code); |
| 839 | |
| 840 | return count; |
| 841 | } |
| 842 | static DEVICE_ATTR(ps_hw, 0664, ps_hw_show, ps_hw_store); |
| 843 | |
| 844 | static ssize_t ls_adc_show(struct device *dev, |
| 845 | struct device_attribute *attr, char *buf) |
| 846 | { |
| 847 | int ret; |
| 848 | struct cm36283_info *lpi = lp_info; |
| 849 | |
| 850 | D("[LS][CM36283] %s: ADC = 0x%04X, Level = %d \n", |
| 851 | __func__, lpi->current_adc, lpi->current_level); |
| 852 | ret = sprintf(buf, "ADC[0x%04X] => level %d\n", |
| 853 | lpi->current_adc, lpi->current_level); |
| 854 | |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static DEVICE_ATTR(ls_adc, 0664, ls_adc_show, NULL); |
| 859 | |
| 860 | static ssize_t ls_enable_show(struct device *dev, |
| 861 | struct device_attribute *attr, char *buf) |
| 862 | { |
| 863 | |
| 864 | int ret = 0; |
| 865 | struct cm36283_info *lpi = lp_info; |
| 866 | |
| 867 | ret = sprintf(buf, "Light sensor Auto Enable = %d\n", |
| 868 | lpi->als_enable); |
| 869 | |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | static ssize_t ls_enable_store(struct device *dev, |
| 874 | struct device_attribute *attr, |
| 875 | const char *buf, size_t count) |
| 876 | { |
| 877 | int ret = 0; |
| 878 | int ls_auto; |
| 879 | struct cm36283_info *lpi = lp_info; |
| 880 | |
| 881 | ls_auto = -1; |
| 882 | sscanf(buf, "%d", &ls_auto); |
| 883 | |
| 884 | if (ls_auto != 0 && ls_auto != 1 && ls_auto != 147) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | if (ls_auto) { |
| 888 | lpi->ls_calibrate = (ls_auto == 147) ? 1 : 0; |
| 889 | ret = lightsensor_enable(lpi); |
| 890 | } else { |
| 891 | lpi->ls_calibrate = 0; |
| 892 | ret = lightsensor_disable(lpi); |
| 893 | } |
| 894 | |
| 895 | D("[LS][CM36283] %s: lpi->als_enable = %d, lpi->ls_calibrate = %d, ls_auto=%d\n", |
| 896 | __func__, lpi->als_enable, lpi->ls_calibrate, ls_auto); |
| 897 | |
| 898 | if (ret < 0) |
| 899 | pr_err( |
| 900 | "[LS][CM36283 error]%s: set auto light sensor fail\n", |
| 901 | __func__); |
| 902 | |
| 903 | return count; |
| 904 | } |
| 905 | |
| 906 | static DEVICE_ATTR(ls_auto, 0664, |
| 907 | ls_enable_show, ls_enable_store); |
| 908 | |
| 909 | static ssize_t ls_kadc_show(struct device *dev, |
| 910 | struct device_attribute *attr, char *buf) |
| 911 | { |
| 912 | struct cm36283_info *lpi = lp_info; |
| 913 | int ret; |
| 914 | |
| 915 | ret = sprintf(buf, "kadc = 0x%x", |
| 916 | lpi->als_kadc); |
| 917 | |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | static ssize_t ls_kadc_store(struct device *dev, |
| 922 | struct device_attribute *attr, |
| 923 | const char *buf, size_t count) |
| 924 | { |
| 925 | struct cm36283_info *lpi = lp_info; |
| 926 | int kadc_temp = 0; |
| 927 | |
| 928 | sscanf(buf, "%d", &kadc_temp); |
| 929 | |
| 930 | mutex_lock(&als_get_adc_mutex); |
| 931 | if(kadc_temp != 0) { |
| 932 | lpi->als_kadc = kadc_temp; |
| 933 | if( lpi->als_gadc != 0){ |
| 934 | if (lightsensor_update_table(lpi) < 0) |
| 935 | printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n", __func__); |
| 936 | } else { |
| 937 | printk(KERN_INFO "[LS]%s: als_gadc =0x%x wait to be set\n", |
| 938 | __func__, lpi->als_gadc); |
| 939 | } |
| 940 | } else { |
| 941 | printk(KERN_INFO "[LS]%s: als_kadc can't be set to zero\n", |
| 942 | __func__); |
| 943 | } |
| 944 | |
| 945 | mutex_unlock(&als_get_adc_mutex); |
| 946 | return count; |
| 947 | } |
| 948 | |
| 949 | static DEVICE_ATTR(ls_kadc, 0664, ls_kadc_show, ls_kadc_store); |
| 950 | |
| 951 | static ssize_t ls_gadc_show(struct device *dev, |
| 952 | struct device_attribute *attr, char *buf) |
| 953 | { |
| 954 | struct cm36283_info *lpi = lp_info; |
| 955 | int ret; |
| 956 | |
| 957 | ret = sprintf(buf, "gadc = 0x%x\n", lpi->als_gadc); |
| 958 | |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | static ssize_t ls_gadc_store(struct device *dev, |
| 963 | struct device_attribute *attr, |
| 964 | const char *buf, size_t count) |
| 965 | { |
| 966 | struct cm36283_info *lpi = lp_info; |
| 967 | int gadc_temp = 0; |
| 968 | |
| 969 | sscanf(buf, "%d", &gadc_temp); |
| 970 | |
| 971 | mutex_lock(&als_get_adc_mutex); |
| 972 | if(gadc_temp != 0) { |
| 973 | lpi->als_gadc = gadc_temp; |
| 974 | if( lpi->als_kadc != 0){ |
| 975 | if (lightsensor_update_table(lpi) < 0) |
| 976 | printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n", __func__); |
| 977 | } else { |
| 978 | printk(KERN_INFO "[LS]%s: als_kadc =0x%x wait to be set\n", |
| 979 | __func__, lpi->als_kadc); |
| 980 | } |
| 981 | } else { |
| 982 | printk(KERN_INFO "[LS]%s: als_gadc can't be set to zero\n", |
| 983 | __func__); |
| 984 | } |
| 985 | mutex_unlock(&als_get_adc_mutex); |
| 986 | return count; |
| 987 | } |
| 988 | |
| 989 | static DEVICE_ATTR(ls_gadc, 0664, ls_gadc_show, ls_gadc_store); |
| 990 | |
| 991 | static ssize_t ls_adc_table_show(struct device *dev, |
| 992 | struct device_attribute *attr, char *buf) |
| 993 | { |
| 994 | unsigned length = 0; |
| 995 | int i; |
| 996 | |
| 997 | for (i = 0; i < 10; i++) { |
| 998 | length += sprintf(buf + length, |
| 999 | "[CM36283]Get adc_table[%d] = 0x%x ; %d, Get cali_table[%d] = 0x%x ; %d, \n", |
| 1000 | i, *(lp_info->adc_table + i), |
| 1001 | *(lp_info->adc_table + i), |
| 1002 | i, *(lp_info->cali_table + i), |
| 1003 | *(lp_info->cali_table + i)); |
| 1004 | } |
| 1005 | return length; |
| 1006 | } |
| 1007 | |
| 1008 | static ssize_t ls_adc_table_store(struct device *dev, |
| 1009 | struct device_attribute *attr, |
| 1010 | const char *buf, size_t count) |
| 1011 | { |
| 1012 | |
| 1013 | struct cm36283_info *lpi = lp_info; |
| 1014 | char *token[10]; |
| 1015 | uint16_t tempdata[10]; |
| 1016 | int i; |
| 1017 | |
| 1018 | printk(KERN_INFO "[LS][CM36283]%s\n", buf); |
| 1019 | for (i = 0; i < 10; i++) { |
| 1020 | token[i] = strsep((char **)&buf, " "); |
| 1021 | tempdata[i] = simple_strtoul(token[i], NULL, 16); |
| 1022 | if (tempdata[i] < 1 || tempdata[i] > 0xffff) { |
| 1023 | printk(KERN_ERR |
| 1024 | "[LS][CM36283 error] adc_table[%d] = 0x%x Err\n", |
| 1025 | i, tempdata[i]); |
| 1026 | return count; |
| 1027 | } |
| 1028 | } |
| 1029 | mutex_lock(&als_get_adc_mutex); |
| 1030 | for (i = 0; i < 10; i++) { |
| 1031 | lpi->adc_table[i] = tempdata[i]; |
| 1032 | printk(KERN_INFO |
| 1033 | "[LS][CM36283]Set lpi->adc_table[%d] = 0x%x\n", |
| 1034 | i, *(lp_info->adc_table + i)); |
| 1035 | } |
| 1036 | if (lightsensor_update_table(lpi) < 0) |
| 1037 | printk(KERN_ERR "[LS][CM36283 error] %s: update ls table fail\n", |
| 1038 | __func__); |
| 1039 | mutex_unlock(&als_get_adc_mutex); |
| 1040 | D("[LS][CM36283] %s\n", __func__); |
| 1041 | |
| 1042 | return count; |
| 1043 | } |
| 1044 | |
| 1045 | static DEVICE_ATTR(ls_adc_table, 0664, |
| 1046 | ls_adc_table_show, ls_adc_table_store); |
| 1047 | |
| 1048 | static ssize_t ls_conf_show(struct device *dev, |
| 1049 | struct device_attribute *attr, char *buf) |
| 1050 | { |
| 1051 | struct cm36283_info *lpi = lp_info; |
| 1052 | return sprintf(buf, "ALS_CONF = %x\n", lpi->ls_cmd); |
| 1053 | } |
| 1054 | static ssize_t ls_conf_store(struct device *dev, |
| 1055 | struct device_attribute *attr, |
| 1056 | const char *buf, size_t count) |
| 1057 | { |
| 1058 | struct cm36283_info *lpi = lp_info; |
| 1059 | int value = 0; |
| 1060 | sscanf(buf, "0x%x", &value); |
| 1061 | |
| 1062 | lpi->ls_cmd = value; |
| 1063 | printk(KERN_INFO "[LS]set ALS_CONF = %x\n", lpi->ls_cmd); |
| 1064 | |
| 1065 | _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd); |
| 1066 | return count; |
| 1067 | } |
| 1068 | static DEVICE_ATTR(ls_conf, 0664, ls_conf_show, ls_conf_store); |
| 1069 | |
| 1070 | static ssize_t ls_fLevel_show(struct device *dev, |
| 1071 | struct device_attribute *attr, char *buf) |
| 1072 | { |
| 1073 | return sprintf(buf, "fLevel = %d\n", fLevel); |
| 1074 | } |
| 1075 | static ssize_t ls_fLevel_store(struct device *dev, |
| 1076 | struct device_attribute *attr, |
| 1077 | const char *buf, size_t count) |
| 1078 | { |
| 1079 | struct cm36283_info *lpi = lp_info; |
| 1080 | int value=0; |
| 1081 | sscanf(buf, "%d", &value); |
| 1082 | (value>=0)?(value=min(value,10)):(value=max(value,-1)); |
| 1083 | fLevel=value; |
| 1084 | input_report_abs(lpi->ls_input_dev, ABS_MISC, fLevel); |
| 1085 | input_sync(lpi->ls_input_dev); |
| 1086 | printk(KERN_INFO "[LS]set fLevel = %d\n", fLevel); |
| 1087 | |
| 1088 | msleep(1000); |
| 1089 | fLevel=-1; |
| 1090 | return count; |
| 1091 | } |
| 1092 | static DEVICE_ATTR(ls_flevel, 0664, ls_fLevel_show, ls_fLevel_store); |
| 1093 | |
| 1094 | static int lightsensor_setup(struct cm36283_info *lpi) |
| 1095 | { |
| 1096 | int ret; |
| 1097 | |
| 1098 | lpi->ls_input_dev = input_allocate_device(); |
| 1099 | if (!lpi->ls_input_dev) { |
| 1100 | pr_err( |
| 1101 | "[LS][CM36283 error]%s: could not allocate ls input device\n", |
| 1102 | __func__); |
| 1103 | return -ENOMEM; |
| 1104 | } |
| 1105 | lpi->ls_input_dev->name = "cm36283-ls"; |
| 1106 | set_bit(EV_ABS, lpi->ls_input_dev->evbit); |
| 1107 | input_set_abs_params(lpi->ls_input_dev, ABS_MISC, 0, 9, 0, 0); |
| 1108 | |
| 1109 | ret = input_register_device(lpi->ls_input_dev); |
| 1110 | if (ret < 0) { |
| 1111 | pr_err("[LS][CM36283 error]%s: can not register ls input device\n", |
| 1112 | __func__); |
| 1113 | goto err_free_ls_input_device; |
| 1114 | } |
| 1115 | |
| 1116 | ret = misc_register(&lightsensor_misc); |
| 1117 | if (ret < 0) { |
| 1118 | pr_err("[LS][CM36283 error]%s: can not register ls misc device\n", |
| 1119 | __func__); |
| 1120 | goto err_unregister_ls_input_device; |
| 1121 | } |
| 1122 | |
| 1123 | return ret; |
| 1124 | |
| 1125 | err_unregister_ls_input_device: |
| 1126 | input_unregister_device(lpi->ls_input_dev); |
| 1127 | err_free_ls_input_device: |
| 1128 | input_free_device(lpi->ls_input_dev); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | static int psensor_setup(struct cm36283_info *lpi) |
| 1133 | { |
| 1134 | int ret; |
| 1135 | |
| 1136 | lpi->ps_input_dev = input_allocate_device(); |
| 1137 | if (!lpi->ps_input_dev) { |
| 1138 | pr_err( |
| 1139 | "[PS][CM36283 error]%s: could not allocate ps input device\n", |
| 1140 | __func__); |
| 1141 | return -ENOMEM; |
| 1142 | } |
| 1143 | lpi->ps_input_dev->name = "cm36283-ps"; |
| 1144 | set_bit(EV_ABS, lpi->ps_input_dev->evbit); |
| 1145 | input_set_abs_params(lpi->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0); |
| 1146 | |
| 1147 | ret = input_register_device(lpi->ps_input_dev); |
| 1148 | if (ret < 0) { |
| 1149 | pr_err( |
| 1150 | "[PS][CM36283 error]%s: could not register ps input device\n", |
| 1151 | __func__); |
| 1152 | goto err_free_ps_input_device; |
| 1153 | } |
| 1154 | |
| 1155 | ret = misc_register(&psensor_misc); |
| 1156 | if (ret < 0) { |
| 1157 | pr_err( |
| 1158 | "[PS][CM36283 error]%s: could not register ps misc device\n", |
| 1159 | __func__); |
| 1160 | goto err_unregister_ps_input_device; |
| 1161 | } |
| 1162 | |
| 1163 | return ret; |
| 1164 | |
| 1165 | err_unregister_ps_input_device: |
| 1166 | input_unregister_device(lpi->ps_input_dev); |
| 1167 | err_free_ps_input_device: |
| 1168 | input_free_device(lpi->ps_input_dev); |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | static int initial_cm36283(struct cm36283_info *lpi) |
| 1174 | { |
| 1175 | int val, ret; |
| 1176 | uint16_t idReg; |
| 1177 | |
| 1178 | val = gpio_get_value(lpi->intr_pin); |
| 1179 | D("[PS][CM36283] %s, INTERRUPT GPIO val = %d\n", __func__, val); |
| 1180 | |
| 1181 | ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ID_REG, &idReg); |
| 1182 | if ((ret < 0) || (idReg != 0xC082)) { |
| 1183 | if (record_init_fail == 0) |
| 1184 | record_init_fail = 1; |
| 1185 | return -ENOMEM;/*If devices without cm36283 chip and did not probe driver*/ |
| 1186 | } |
| 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | static int cm36283_setup(struct cm36283_info *lpi) |
| 1192 | { |
| 1193 | int ret = 0; |
| 1194 | |
| 1195 | als_power(1); |
| 1196 | msleep(5); |
| 1197 | ret = gpio_request(lpi->intr_pin, "gpio_cm36283_intr"); |
| 1198 | if (ret < 0) { |
| 1199 | pr_err("[PS][CM36283 error]%s: gpio %d request failed (%d)\n", |
| 1200 | __func__, lpi->intr_pin, ret); |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | ret = gpio_direction_input(lpi->intr_pin); |
| 1205 | if (ret < 0) { |
| 1206 | pr_err( |
| 1207 | "[PS][CM36283 error]%s: fail to set gpio %d as input (%d)\n", |
| 1208 | __func__, lpi->intr_pin, ret); |
| 1209 | goto fail_free_intr_pin; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | ret = initial_cm36283(lpi); |
| 1214 | if (ret < 0) { |
| 1215 | pr_err( |
| 1216 | "[PS_ERR][CM36283 error]%s: fail to initial cm36283 (%d)\n", |
| 1217 | __func__, ret); |
| 1218 | goto fail_free_intr_pin; |
| 1219 | } |
| 1220 | |
| 1221 | /*Default disable P sensor and L sensor*/ |
| 1222 | ls_initial_cmd(lpi); |
| 1223 | psensor_initial_cmd(lpi); |
| 1224 | |
| 1225 | ret = request_any_context_irq(lpi->irq, |
| 1226 | cm36283_irq_handler, |
| 1227 | IRQF_TRIGGER_LOW, |
| 1228 | "cm36283", |
| 1229 | lpi); |
| 1230 | if (ret < 0) { |
| 1231 | pr_err( |
| 1232 | "[PS][CM36283 error]%s: req_irq(%d) fail for gpio %d (%d)\n", |
| 1233 | __func__, lpi->irq, |
| 1234 | lpi->intr_pin, ret); |
| 1235 | goto fail_free_intr_pin; |
| 1236 | } |
| 1237 | |
| 1238 | return ret; |
| 1239 | |
| 1240 | fail_free_intr_pin: |
| 1241 | gpio_free(lpi->intr_pin); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1246 | static void cm36283_early_suspend(struct early_suspend *h) |
| 1247 | { |
| 1248 | struct cm36283_info *lpi = lp_info; |
| 1249 | |
| 1250 | D("[LS][CM36283] %s\n", __func__); |
| 1251 | |
| 1252 | if (lpi->als_enable) |
| 1253 | lightsensor_disable(lpi); |
| 1254 | |
| 1255 | } |
| 1256 | |
| 1257 | static void cm36283_late_resume(struct early_suspend *h) |
| 1258 | { |
| 1259 | struct cm36283_info *lpi = lp_info; |
| 1260 | |
| 1261 | D("[LS][CM36283] %s\n", __func__); |
| 1262 | |
| 1263 | if (!lpi->als_enable) |
| 1264 | lightsensor_enable(lpi); |
| 1265 | } |
| 1266 | #endif |
| 1267 | |
| 1268 | static int cm36283_probe(struct i2c_client *client, |
| 1269 | const struct i2c_device_id *id) |
| 1270 | { |
| 1271 | int ret = 0; |
| 1272 | struct cm36283_info *lpi; |
| 1273 | struct cm36283_platform_data *pdata; |
| 1274 | |
| 1275 | D("[PS][CM36283] %s\n", __func__); |
| 1276 | |
| 1277 | |
| 1278 | lpi = kzalloc(sizeof(struct cm36283_info), GFP_KERNEL); |
| 1279 | if (!lpi) |
| 1280 | return -ENOMEM; |
| 1281 | |
| 1282 | /*D("[CM36283] %s: client->irq = %d\n", __func__, client->irq);*/ |
| 1283 | |
| 1284 | lpi->i2c_client = client; |
| 1285 | pdata = client->dev.platform_data; |
| 1286 | if (!pdata) { |
| 1287 | pr_err("[PS][CM36283 error]%s: Assign platform_data error!!\n", |
| 1288 | __func__); |
| 1289 | ret = -EBUSY; |
| 1290 | goto err_platform_data_null; |
| 1291 | } |
| 1292 | |
| 1293 | lpi->irq = client->irq; |
| 1294 | |
| 1295 | i2c_set_clientdata(client, lpi); |
| 1296 | |
| 1297 | lpi->intr_pin = pdata->intr; |
| 1298 | lpi->adc_table = pdata->levels; |
| 1299 | lpi->power = pdata->power; |
| 1300 | |
| 1301 | lpi->slave_addr = pdata->slave_addr; |
| 1302 | |
| 1303 | lpi->ps_away_thd_set = pdata->ps_away_thd_set; |
| 1304 | lpi->ps_close_thd_set = pdata->ps_close_thd_set; |
| 1305 | lpi->ps_conf1_val = pdata->ps_conf1_val; |
| 1306 | lpi->ps_conf3_val = pdata->ps_conf3_val; |
| 1307 | |
| 1308 | lpi->ls_cmd = pdata->ls_cmd; |
| 1309 | |
| 1310 | lpi->record_clear_int_fail=0; |
| 1311 | |
| 1312 | D("[PS][CM36283] %s: ls_cmd 0x%x\n", |
| 1313 | __func__, lpi->ls_cmd); |
| 1314 | |
| 1315 | if (pdata->ls_cmd == 0) { |
| 1316 | lpi->ls_cmd = CM36283_ALS_IT_160ms | CM36283_ALS_GAIN_2; |
| 1317 | } |
| 1318 | |
| 1319 | lp_info = lpi; |
| 1320 | |
| 1321 | mutex_init(&CM36283_control_mutex); |
| 1322 | |
| 1323 | mutex_init(&als_enable_mutex); |
| 1324 | mutex_init(&als_disable_mutex); |
| 1325 | mutex_init(&als_get_adc_mutex); |
| 1326 | |
| 1327 | ret = lightsensor_setup(lpi); |
| 1328 | if (ret < 0) { |
| 1329 | pr_err("[LS][CM36283 error]%s: lightsensor_setup error!!\n", |
| 1330 | __func__); |
| 1331 | goto err_lightsensor_setup; |
| 1332 | } |
| 1333 | |
| 1334 | mutex_init(&ps_enable_mutex); |
| 1335 | mutex_init(&ps_disable_mutex); |
| 1336 | mutex_init(&ps_get_adc_mutex); |
| 1337 | |
| 1338 | ret = psensor_setup(lpi); |
| 1339 | if (ret < 0) { |
| 1340 | pr_err("[PS][CM36283 error]%s: psensor_setup error!!\n", |
| 1341 | __func__); |
| 1342 | goto err_psensor_setup; |
| 1343 | } |
| 1344 | |
| 1345 | //SET LUX STEP FACTOR HERE |
| 1346 | // if adc raw value one step = 5/100 = 1/20 = 0.05 lux |
| 1347 | // the following will set the factor 0.05 = 1/20 |
| 1348 | // and lpi->golden_adc = 1; |
| 1349 | // set als_kadc = (ALS_CALIBRATED <<16) | 20; |
| 1350 | |
| 1351 | als_kadc = (ALS_CALIBRATED <<16) | 20; |
| 1352 | lpi->golden_adc = 1; |
| 1353 | |
| 1354 | //ls calibrate always set to 1 |
| 1355 | lpi->ls_calibrate = 1; |
| 1356 | |
| 1357 | lightsensor_set_kvalue(lpi); |
| 1358 | ret = lightsensor_update_table(lpi); |
| 1359 | if (ret < 0) { |
| 1360 | pr_err("[LS][CM36283 error]%s: update ls table fail\n", |
| 1361 | __func__); |
| 1362 | goto err_lightsensor_update_table; |
| 1363 | } |
| 1364 | |
| 1365 | lpi->lp_wq = create_singlethread_workqueue("cm36283_wq"); |
| 1366 | if (!lpi->lp_wq) { |
| 1367 | pr_err("[PS][CM36283 error]%s: can't create workqueue\n", __func__); |
| 1368 | ret = -ENOMEM; |
| 1369 | goto err_create_singlethread_workqueue; |
| 1370 | } |
| 1371 | wake_lock_init(&(lpi->ps_wake_lock), WAKE_LOCK_SUSPEND, "proximity"); |
| 1372 | |
| 1373 | ret = cm36283_setup(lpi); |
| 1374 | if (ret < 0) { |
| 1375 | pr_err("[PS_ERR][CM36283 error]%s: cm36283_setup error!\n", __func__); |
| 1376 | goto err_cm36283_setup; |
| 1377 | } |
| 1378 | lpi->cm36283_class = class_create(THIS_MODULE, "optical_sensors"); |
| 1379 | if (IS_ERR(lpi->cm36283_class)) { |
| 1380 | ret = PTR_ERR(lpi->cm36283_class); |
| 1381 | lpi->cm36283_class = NULL; |
| 1382 | goto err_create_class; |
| 1383 | } |
| 1384 | |
| 1385 | lpi->ls_dev = device_create(lpi->cm36283_class, |
| 1386 | NULL, 0, "%s", "lightsensor"); |
| 1387 | if (unlikely(IS_ERR(lpi->ls_dev))) { |
| 1388 | ret = PTR_ERR(lpi->ls_dev); |
| 1389 | lpi->ls_dev = NULL; |
| 1390 | goto err_create_ls_device; |
| 1391 | } |
| 1392 | |
| 1393 | /* register the attributes */ |
| 1394 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc); |
| 1395 | if (ret) |
| 1396 | goto err_create_ls_device_file; |
| 1397 | |
| 1398 | /* register the attributes */ |
| 1399 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_auto); |
| 1400 | if (ret) |
| 1401 | goto err_create_ls_device_file; |
| 1402 | |
| 1403 | /* register the attributes */ |
| 1404 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_kadc); |
| 1405 | if (ret) |
| 1406 | goto err_create_ls_device_file; |
| 1407 | |
| 1408 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_gadc); |
| 1409 | if (ret) |
| 1410 | goto err_create_ls_device_file; |
| 1411 | |
| 1412 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc_table); |
| 1413 | if (ret) |
| 1414 | goto err_create_ls_device_file; |
| 1415 | |
| 1416 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_conf); |
| 1417 | if (ret) |
| 1418 | goto err_create_ls_device_file; |
| 1419 | |
| 1420 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_flevel); |
| 1421 | if (ret) |
| 1422 | goto err_create_ls_device_file; |
| 1423 | |
| 1424 | lpi->ps_dev = device_create(lpi->cm36283_class, |
| 1425 | NULL, 0, "%s", "proximity"); |
| 1426 | if (unlikely(IS_ERR(lpi->ps_dev))) { |
| 1427 | ret = PTR_ERR(lpi->ps_dev); |
| 1428 | lpi->ps_dev = NULL; |
| 1429 | goto err_create_ls_device_file; |
| 1430 | } |
| 1431 | |
| 1432 | /* register the attributes */ |
| 1433 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_adc); |
| 1434 | if (ret) |
| 1435 | goto err_create_ps_device; |
| 1436 | |
| 1437 | ret = device_create_file(lpi->ps_dev, |
| 1438 | &dev_attr_ps_parameters); |
| 1439 | if (ret) |
| 1440 | goto err_create_ps_device; |
| 1441 | |
| 1442 | /* register the attributes */ |
| 1443 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_conf); |
| 1444 | if (ret) |
| 1445 | goto err_create_ps_device; |
| 1446 | |
| 1447 | /* register the attributes */ |
| 1448 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_thd); |
| 1449 | if (ret) |
| 1450 | goto err_create_ps_device; |
| 1451 | |
| 1452 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_hw); |
| 1453 | if (ret) |
| 1454 | goto err_create_ps_device; |
| 1455 | |
| 1456 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1457 | lpi->early_suspend.level = |
| 1458 | EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; |
| 1459 | lpi->early_suspend.suspend = cm36283_early_suspend; |
| 1460 | lpi->early_suspend.resume = cm36283_late_resume; |
| 1461 | register_early_suspend(&lpi->early_suspend); |
| 1462 | #endif |
| 1463 | |
| 1464 | D("[PS][CM36283] %s: Probe success!\n", __func__); |
| 1465 | |
| 1466 | return ret; |
| 1467 | |
| 1468 | err_create_ps_device: |
| 1469 | device_unregister(lpi->ps_dev); |
| 1470 | err_create_ls_device_file: |
| 1471 | device_unregister(lpi->ls_dev); |
| 1472 | err_create_ls_device: |
| 1473 | class_destroy(lpi->cm36283_class); |
| 1474 | err_create_class: |
| 1475 | err_cm36283_setup: |
| 1476 | destroy_workqueue(lpi->lp_wq); |
| 1477 | wake_lock_destroy(&(lpi->ps_wake_lock)); |
| 1478 | |
| 1479 | input_unregister_device(lpi->ls_input_dev); |
| 1480 | input_free_device(lpi->ls_input_dev); |
| 1481 | input_unregister_device(lpi->ps_input_dev); |
| 1482 | input_free_device(lpi->ps_input_dev); |
| 1483 | err_create_singlethread_workqueue: |
| 1484 | err_lightsensor_update_table: |
| 1485 | misc_deregister(&psensor_misc); |
| 1486 | err_psensor_setup: |
| 1487 | mutex_destroy(&CM36283_control_mutex); |
| 1488 | mutex_destroy(&ps_enable_mutex); |
| 1489 | mutex_destroy(&ps_disable_mutex); |
| 1490 | mutex_destroy(&ps_get_adc_mutex); |
| 1491 | misc_deregister(&lightsensor_misc); |
| 1492 | err_lightsensor_setup: |
| 1493 | mutex_destroy(&als_enable_mutex); |
| 1494 | mutex_destroy(&als_disable_mutex); |
| 1495 | mutex_destroy(&als_get_adc_mutex); |
| 1496 | err_platform_data_null: |
| 1497 | kfree(lpi); |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
| 1501 | static int control_and_report( struct cm36283_info *lpi, uint8_t mode, uint16_t param ) { |
| 1502 | int ret=0; |
| 1503 | uint16_t adc_value = 0; |
| 1504 | uint16_t ps_data = 0; |
| 1505 | int level = 0, i, val; |
| 1506 | |
| 1507 | mutex_lock(&CM36283_control_mutex); |
| 1508 | |
| 1509 | if( mode == CONTROL_ALS ){ |
| 1510 | if(param){ |
| 1511 | lpi->ls_cmd &= CM36283_ALS_SD_MASK; |
| 1512 | } else { |
| 1513 | lpi->ls_cmd |= CM36283_ALS_SD; |
| 1514 | } |
| 1515 | _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd); |
| 1516 | lpi->als_enable=param; |
| 1517 | } else if( mode == CONTROL_PS ){ |
| 1518 | if(param){ |
| 1519 | lpi->ps_conf1_val &= CM36283_PS_SD_MASK; |
| 1520 | lpi->ps_conf1_val |= CM36283_PS_INT_IN_AND_OUT; |
| 1521 | } else { |
| 1522 | lpi->ps_conf1_val |= CM36283_PS_SD; |
| 1523 | lpi->ps_conf1_val &= CM36283_PS_INT_MASK; |
| 1524 | } |
| 1525 | _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val); |
| 1526 | lpi->ps_enable=param; |
| 1527 | } |
| 1528 | if((mode == CONTROL_ALS)||(mode == CONTROL_PS)){ |
| 1529 | if( param==1 ){ |
| 1530 | msleep(100); |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | if(lpi->als_enable){ |
| 1535 | if( mode == CONTROL_ALS || |
| 1536 | ( mode == CONTROL_INT_ISR_REPORT && |
| 1537 | ((param&INT_FLAG_ALS_IF_L)||(param&INT_FLAG_ALS_IF_H)))){ |
| 1538 | |
| 1539 | lpi->ls_cmd &= CM36283_ALS_INT_MASK; |
| 1540 | ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd); |
| 1541 | |
| 1542 | get_ls_adc_value(&adc_value, 0); |
| 1543 | |
| 1544 | if( lpi->ls_calibrate ) { |
| 1545 | for (i = 0; i < 10; i++) { |
| 1546 | if (adc_value <= (*(lpi->cali_table + i))) { |
| 1547 | level = i; |
| 1548 | if (*(lpi->cali_table + i)) |
| 1549 | break; |
| 1550 | } |
| 1551 | if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */ |
| 1552 | level = i; |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | } else { |
| 1557 | for (i = 0; i < 10; i++) { |
| 1558 | if (adc_value <= (*(lpi->adc_table + i))) { |
| 1559 | level = i; |
| 1560 | if (*(lpi->adc_table + i)) |
| 1561 | break; |
| 1562 | } |
| 1563 | if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */ |
| 1564 | level = i; |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | ret = set_lsensor_range(((i == 0) || (adc_value == 0)) ? 0 : |
| 1571 | *(lpi->cali_table + (i - 1)) + 1, |
| 1572 | *(lpi->cali_table + i)); |
| 1573 | |
| 1574 | lpi->ls_cmd |= CM36283_ALS_INT_EN; |
| 1575 | |
| 1576 | ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd); |
| 1577 | |
| 1578 | if ((i == 0) || (adc_value == 0)) |
| 1579 | D("[LS][CM3628] %s: ADC=0x%03X, Level=%d, l_thd equal 0, h_thd = 0x%x \n", |
| 1580 | __func__, adc_value, level, *(lpi->cali_table + i)); |
| 1581 | else |
| 1582 | D("[LS][CM3628] %s: ADC=0x%03X, Level=%d, l_thd = 0x%x, h_thd = 0x%x \n", |
| 1583 | __func__, adc_value, level, *(lpi->cali_table + (i - 1)) + 1, *(lpi->cali_table + i)); |
| 1584 | lpi->current_level = level; |
| 1585 | lpi->current_adc = adc_value; |
| 1586 | input_report_abs(lpi->ls_input_dev, ABS_MISC, level); |
| 1587 | input_sync(lpi->ls_input_dev); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | #define PS_CLOSE 1 |
| 1592 | #define PS_AWAY (1<<1) |
| 1593 | #define PS_CLOSE_AND_AWAY PS_CLOSE+PS_AWAY |
| 1594 | |
| 1595 | if(lpi->ps_enable){ |
| 1596 | int ps_status = 0; |
| 1597 | if( mode == CONTROL_PS ) |
| 1598 | ps_status = PS_CLOSE_AND_AWAY; |
| 1599 | else if(mode == CONTROL_INT_ISR_REPORT ){ |
| 1600 | if ( param & INT_FLAG_PS_IF_CLOSE ) |
| 1601 | ps_status |= PS_CLOSE; |
| 1602 | if ( param & INT_FLAG_PS_IF_AWAY ) |
| 1603 | ps_status |= PS_AWAY; |
| 1604 | } |
| 1605 | |
| 1606 | if (ps_status!=0){ |
| 1607 | switch(ps_status){ |
| 1608 | case PS_CLOSE_AND_AWAY: |
| 1609 | get_stable_ps_adc_value(&ps_data); |
| 1610 | val = (ps_data >= lpi->ps_close_thd_set) ? 0 : 1; |
| 1611 | break; |
| 1612 | case PS_AWAY: |
| 1613 | val = 1; |
| 1614 | break; |
| 1615 | case PS_CLOSE: |
| 1616 | val = 0; |
| 1617 | break; |
| 1618 | }; |
| 1619 | input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, val); |
| 1620 | input_sync(lpi->ps_input_dev); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | mutex_unlock(&CM36283_control_mutex); |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | static const struct i2c_device_id cm36283_i2c_id[] = { |
| 1630 | {CM36283_I2C_NAME, 0}, |
| 1631 | {} |
| 1632 | }; |
| 1633 | |
| 1634 | static struct i2c_driver cm36283_driver = { |
| 1635 | .id_table = cm36283_i2c_id, |
| 1636 | .probe = cm36283_probe, |
| 1637 | .driver = { |
| 1638 | .name = CM36283_I2C_NAME, |
| 1639 | .owner = THIS_MODULE, |
| 1640 | }, |
| 1641 | }; |
| 1642 | |
| 1643 | static int __init cm36283_init(void) |
| 1644 | { |
| 1645 | return i2c_add_driver(&cm36283_driver); |
| 1646 | } |
| 1647 | |
| 1648 | static void __exit cm36283_exit(void) |
| 1649 | { |
| 1650 | i2c_del_driver(&cm36283_driver); |
| 1651 | } |
| 1652 | |
| 1653 | module_init(cm36283_init); |
| 1654 | module_exit(cm36283_exit); |
| 1655 | |
| 1656 | MODULE_LICENSE("GPL"); |
| 1657 | MODULE_DESCRIPTION("CM36283 Driver"); |
| 1658 | MODULE_AUTHOR("Frank Hsieh <pengyueh@gmail.com>"); |