| Flemmard | 4d9dbc7 | 2014-01-29 10:06:14 +0100 | [diff] [blame] | 1 | /* drivers/i2c/chips/isl29029.c - isl29029 optical sensors driver |
| 2 | * |
| 3 | * Copyright (C) 2010 HTC, Inc. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/gpio.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/lightsensor.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/mach-types.h> |
| 31 | #include <linux/isl29029.h> |
| 32 | #include <linux/pl_sensor.h> |
| 33 | #include <linux/capella_cm3602.h> |
| 34 | #include <asm/setup.h> |
| 35 | #include <linux/wakelock.h> |
| 36 | #include <linux/rtc.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <mach/board.h> |
| 39 | |
| 40 | #define DPS(x...) printk(KERN_DEBUG "[PS][ISL29029] " x) |
| 41 | #define DLS(x...) printk(KERN_DEBUG "[LS][ISL29029] " x) |
| 42 | #define IPS(x...) printk(KERN_INFO "[PS][ISL29029] " x) |
| 43 | #define ILS(x...) printk(KERN_INFO "[LS][ISL29029] " x) |
| 44 | #define EPS(x...) printk(KERN_ERR "[PS][ISL29029 ERROR] " x) |
| 45 | #define ELS(x...) printk(KERN_ERR "[LS][ISL29029 ERROR] " x) |
| 46 | |
| 47 | #define I2C_RETRY_COUNT 10 |
| 48 | |
| 49 | #define INTR_DEFAULT 0x04 |
| 50 | #define CONFIG_DEFAULT 0x52 |
| 51 | |
| 52 | #define INTR_MASK 0x77 |
| 53 | #define CONFIG_MASK 0x7B |
| 54 | |
| 55 | extern unsigned int als_kadc; |
| 56 | static void sensor_irq_do_work(struct work_struct *work); |
| 57 | static DECLARE_WORK(sensor_irq_work, sensor_irq_do_work); |
| 58 | |
| 59 | struct isl29029_info { |
| 60 | struct class *isl29029_class; |
| 61 | struct device *ls_dev; |
| 62 | struct device *ps_dev; |
| 63 | |
| 64 | struct input_dev *ls_input_dev; |
| 65 | struct input_dev *ps_input_dev; |
| 66 | |
| 67 | struct i2c_client *i2c_client; |
| 68 | struct workqueue_struct *lp_wq; |
| 69 | |
| 70 | int intr_pin; |
| 71 | |
| 72 | int als_enable; |
| 73 | int ls_enable_flag; |
| 74 | |
| 75 | int ps_enable; |
| 76 | uint8_t ps_lt; |
| 77 | uint8_t ps_ht; |
| 78 | uint8_t ps_B_val; |
| 79 | uint8_t ps_C_val; |
| 80 | uint8_t ps_A_val; |
| 81 | uint8_t ps_X_val; |
| 82 | int led; |
| 83 | uint8_t default_ps_lt; |
| 84 | uint8_t default_ps_ht; |
| 85 | |
| 86 | uint16_t *adc_table; |
| 87 | uint16_t cali_table[10]; |
| 88 | int irq; |
| 89 | |
| 90 | int ls_calibrate; |
| 91 | |
| 92 | int (*power)(int, uint8_t); /* power to the chip */ |
| 93 | int (*calibrate_func)(int, int, int, int, int*, int*); |
| 94 | uint32_t als_kadc; |
| 95 | uint32_t als_gadc; |
| 96 | uint16_t golden_adc; |
| 97 | |
| 98 | struct wake_lock ps_wake_lock; |
| 99 | int psensor_opened; |
| 100 | int lightsensor_opened; |
| 101 | uint8_t default_config_reg; |
| 102 | }; |
| 103 | |
| 104 | static struct isl29029_info *lp_info; |
| 105 | |
| 106 | /*static uint32_t als_kadc;*/ |
| 107 | static uint32_t ps_threshold; |
| 108 | |
| 109 | static int I2C_RxData(char *rxData, int length) |
| 110 | { |
| 111 | uint8_t loop_i; |
| 112 | struct i2c_msg msgs[] = { |
| 113 | { |
| 114 | .addr = lp_info->i2c_client->addr, |
| 115 | .flags = 0, |
| 116 | .len = 1, |
| 117 | .buf = rxData, |
| 118 | }, |
| 119 | { |
| 120 | .addr = lp_info->i2c_client->addr, |
| 121 | .flags = I2C_M_RD, |
| 122 | .len = length, |
| 123 | .buf = rxData, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) { |
| 128 | if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 2) > 0) |
| 129 | break; |
| 130 | |
| 131 | mdelay(10); |
| 132 | } |
| 133 | |
| 134 | if (loop_i >= I2C_RETRY_COUNT) { |
| 135 | EPS("%s retry over %d\n", |
| 136 | __func__, I2C_RETRY_COUNT); |
| 137 | return -EIO; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int I2C_TxData(char *txData, int length) |
| 144 | { |
| 145 | uint8_t loop_i; |
| 146 | struct i2c_msg msg[] = { |
| 147 | { |
| 148 | .addr = lp_info->i2c_client->addr, |
| 149 | .flags = 0, |
| 150 | .len = length, |
| 151 | .buf = txData, |
| 152 | }, |
| 153 | }; |
| 154 | |
| 155 | for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) { |
| 156 | if (i2c_transfer(lp_info->i2c_client->adapter, msg, 1) > 0) |
| 157 | break; |
| 158 | |
| 159 | mdelay(10); |
| 160 | } |
| 161 | |
| 162 | if (loop_i >= I2C_RETRY_COUNT) { |
| 163 | EPS("%s retry over %d\n", |
| 164 | __func__, I2C_RETRY_COUNT); |
| 165 | return -EIO; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static uint16_t get_ls_adc_value(uint16_t *raw_adc_value) |
| 172 | { |
| 173 | uint16_t value, tmp_value; |
| 174 | struct isl29029_info *lpi = lp_info; |
| 175 | char buffer[3] = {0}; |
| 176 | int ret = 0; |
| 177 | |
| 178 | buffer[0] = ISL29029_LS_DATA1; |
| 179 | ret = I2C_RxData(buffer, 2); |
| 180 | if (ret < 0) { |
| 181 | ELS("%s: I2C_RxData fail\n", __func__); |
| 182 | return ret; |
| 183 | } |
| 184 | value = buffer[0]; |
| 185 | tmp_value = buffer[1]; |
| 186 | |
| 187 | /*DLS("%s: value = 0x%03X, tmp_value = 0x%03X\n", |
| 188 | __func__, value, tmp_value);*/ |
| 189 | |
| 190 | value = value | tmp_value << 8; |
| 191 | *raw_adc_value = value; |
| 192 | |
| 193 | if (value > 0xFFF) { |
| 194 | printk(KERN_WARNING "%s: get wrong value: 0x%X\n", |
| 195 | __func__, value); |
| 196 | return -1; |
| 197 | } else { |
| 198 | if (!lpi->ls_calibrate) { |
| 199 | value = value * lpi->als_gadc / lpi->als_kadc; |
| 200 | if (value > 0xFFF) |
| 201 | value = 0xFFF; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return value & 0x0FFF; |
| 206 | } |
| 207 | |
| 208 | static uint16_t get_ps_adc_value(void) |
| 209 | { |
| 210 | uint16_t value; |
| 211 | char buffer[2]; |
| 212 | int ret = 0; |
| 213 | |
| 214 | buffer[0] = ISL29029_PROX_DATA; |
| 215 | ret = I2C_RxData(buffer, 1); |
| 216 | if (ret < 0) { |
| 217 | EPS("%s: I2C_RxData fail\n", __func__); |
| 218 | return 0; |
| 219 | } |
| 220 | value = buffer[0]; |
| 221 | |
| 222 | return value & 0xFF; |
| 223 | } |
| 224 | |
| 225 | static int _isl29029_set_reg_bit(struct i2c_client *client, u8 set, |
| 226 | u8 cmd, u8 data) |
| 227 | { |
| 228 | char buffer[2]; |
| 229 | u8 value; |
| 230 | int ret = 0; |
| 231 | |
| 232 | buffer[0] = cmd; |
| 233 | ret = I2C_RxData(buffer, 1); |
| 234 | if (ret < 0) { |
| 235 | EPS("%s: I2C_RxData fail\n", __func__); |
| 236 | return ret; |
| 237 | } |
| 238 | value = buffer[0]; |
| 239 | /*DPS("Andy, %s: I2C_RxData[0x%x] = 0x%x\n", |
| 240 | __func__, cmd, value);*/ |
| 241 | |
| 242 | if (set) |
| 243 | value |= data; |
| 244 | else |
| 245 | value &= ~data; |
| 246 | |
| 247 | buffer[0] = cmd; |
| 248 | buffer[1] = value; |
| 249 | ret = I2C_TxData(buffer, 2); |
| 250 | if (ret < 0) { |
| 251 | EPS("%s: I2C_TxData fail\n", __func__); |
| 252 | return -EIO; |
| 253 | } |
| 254 | |
| 255 | /* Debug use */ |
| 256 | /*value = i2c_smbus_read_byte_data(client, cmd); |
| 257 | DPS("Andy, %s: After set reg bit[0x%x] = 0x%x\n", |
| 258 | __func__, cmd, value);*/ |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static int set_lsensor_range(uint16_t lt, uint16_t ht) |
| 264 | { |
| 265 | uint16_t value = 0; |
| 266 | int ret; |
| 267 | char buffer[4]; |
| 268 | |
| 269 | value = (lt >> 8) & 0x0f; |
| 270 | value |= ht << 4; |
| 271 | value &= 0xff; |
| 272 | |
| 273 | buffer[0] = ISL29029_LS_TH1; |
| 274 | buffer[1] = lt & 0xFF; |
| 275 | buffer[2] = value; |
| 276 | buffer[3] = (ht >> 4) & 0xFF; |
| 277 | ret = I2C_TxData(buffer, 4); |
| 278 | if (ret < 0) { |
| 279 | ELS("%s : I2C_TxData fail\n", __func__); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | value = i2c_smbus_read_byte_data(lpi->i2c_client, ISL29029_LS_TH1); |
| 285 | DLS("TH1--------->0x%03X\n", value); |
| 286 | value = i2c_smbus_read_byte_data(lpi->i2c_client, ISL29029_LS_TH2); |
| 287 | DLS("TH2--------->0x%03X\n", value); |
| 288 | value = i2c_smbus_read_byte_data(lpi->i2c_client, ISL29029_LS_TH3); |
| 289 | DLS("TH3--------->0x%03X\n", value); |
| 290 | */ |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static int set_psensor_range(u8 lt, u8 ht) |
| 295 | { |
| 296 | int ret; |
| 297 | char buffer[3]; |
| 298 | struct isl29029_info *lpi = lp_info; |
| 299 | |
| 300 | if (!lpi) { |
| 301 | EPS("%s: lpi_info is empty\n", __func__); |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | /*DPS("%s: PS ps_threshold = 0x%x\n", |
| 306 | __func__, ps_threshold);*/ |
| 307 | |
| 308 | if (ps_threshold >> 16 == PS_CALIBRATED) { |
| 309 | lt = (ps_threshold >> 8) & 0xFF; |
| 310 | ht = ps_threshold & 0xFF; |
| 311 | } |
| 312 | |
| 313 | IPS("%s: Setting psensor range (0x%X, 0x%X)\n", |
| 314 | __func__, lt, ht); |
| 315 | |
| 316 | buffer[0] = ISL29029_PROX_LT; |
| 317 | buffer[1] = lt; |
| 318 | buffer[2] = ht; |
| 319 | ret = I2C_TxData(buffer, 3); |
| 320 | if (ret < 0) { |
| 321 | EPS("%s : I2C_TxData fail\n", __func__); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static void set_psensor_th(u8 lt, u8 ht) |
| 329 | { |
| 330 | int ret = 0; |
| 331 | |
| 332 | ps_threshold = (PS_CALIBRATED << 16) | (lt << 8) | ht; |
| 333 | /*DPS("set_ps_th: ps_threshold = 0x%08X\n", |
| 334 | ps_threshold);*/ |
| 335 | |
| 336 | ret = set_psensor_range(lt, ht); |
| 337 | if (ret < 0) |
| 338 | EPS("%s: set_psensor_range fail\n", __func__); |
| 339 | } |
| 340 | |
| 341 | static void lightsensor_real_disable(struct isl29029_info *lpi) |
| 342 | { |
| 343 | int ret; |
| 344 | |
| 345 | if (lpi->als_enable) { |
| 346 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 347 | ISL29029_CONFIGURE, ISL29029_ALS_EN); |
| 348 | if (ret < 0) |
| 349 | ELS("%s: disable auto light sensor fail\n", |
| 350 | __func__); |
| 351 | else |
| 352 | lpi->als_enable = 0; |
| 353 | |
| 354 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 355 | ISL29029_INTERRUPT, ISL29029_INT_ALS_FLAG); |
| 356 | if (ret < 0) |
| 357 | ELS("%s: clear lsensor intr flag fail\n", __func__); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void report_psensor_input_event(struct isl29029_info *lpi, |
| 362 | uint16_t ps_adc) |
| 363 | { |
| 364 | int val, ret; |
| 365 | |
| 366 | if (ps_adc > lpi->ps_ht) |
| 367 | val = 0; |
| 368 | else if (ps_adc <= lpi->ps_ht) |
| 369 | val = 1; |
| 370 | else |
| 371 | IPS("%s: Proximity adc value not as expected!\n", __func__); |
| 372 | |
| 373 | IPS("proximity %s\n", val ? "FAR" : "NEAR"); |
| 374 | |
| 375 | /* 0 is close, 1 is far */ |
| 376 | input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, val); |
| 377 | input_sync(lpi->ps_input_dev); |
| 378 | |
| 379 | blocking_notifier_call_chain(&psensor_notifier_list, val+2, NULL); |
| 380 | |
| 381 | if (val == 1) |
| 382 | set_psensor_th(0x0, lpi->ps_ht); |
| 383 | else |
| 384 | set_psensor_th(lpi->ps_lt, 0xFF); |
| 385 | |
| 386 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 387 | ISL29029_INTERRUPT, ISL29029_INT_PROX_FLAG); |
| 388 | if (ret < 0) |
| 389 | EPS("%s: clear psensor intr flag fail\n", __func__); |
| 390 | |
| 391 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 392 | ISL29029_INTERRUPT, ISL29029_INT_ALS_FLAG); |
| 393 | if (ret < 0) |
| 394 | EPS("%s: clear lsensor intr flag fail\n", __func__); |
| 395 | } |
| 396 | |
| 397 | static void report_lsensor_input_event(struct isl29029_info *lpi) |
| 398 | { |
| 399 | uint16_t adc_value, raw_adc_value; |
| 400 | int level = 0, i, ret; |
| 401 | |
| 402 | adc_value = get_ls_adc_value(&raw_adc_value); |
| 403 | |
| 404 | for (i = 0; i < 10; i++) { |
| 405 | if (adc_value <= (*(lpi->adc_table + i))) { |
| 406 | level = i; |
| 407 | if (*(lpi->adc_table + i)) |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (i == 0 && (adc_value >= (*(lpi->cali_table + i)))) { |
| 413 | ret = set_lsensor_range((i == 0) ? 0 : |
| 414 | *(lpi->cali_table + (i - 1)) + 1, |
| 415 | raw_adc_value + 1); |
| 416 | if (ret < 0) |
| 417 | ELS("%s: set_lsensor_range fail\n", __func__); |
| 418 | |
| 419 | ILS("ALS_ADC = 0x%03X, Level = %d, l_thd equal 0, h_thd(raw_adc_value+1) = 0x%x \n", |
| 420 | adc_value, level, raw_adc_value + 1); |
| 421 | } else if (i < 10) { |
| 422 | ret = set_lsensor_range((i == 0) ? 0 : |
| 423 | *(lpi->cali_table + (i - 1)) + 1, |
| 424 | *(lpi->cali_table + i)); |
| 425 | if (ret < 0) |
| 426 | ELS("%s: set_lsensor_range fail\n", __func__); |
| 427 | |
| 428 | if (i == 0) |
| 429 | ILS("ALS_ADC = 0x%03X, Level = %d, l_thd equal 0, h_thd = 0x%x \n", |
| 430 | adc_value, level, *(lpi->cali_table + i)); |
| 431 | else |
| 432 | ILS("ALS_ADC = 0x%03X, Level = %d, l_thd equal = 0x%x, h_thd = 0x%x \n", |
| 433 | adc_value, level, *(lpi->cali_table + (i - 1)) + 1, *(lpi->cali_table + i)); |
| 434 | } else |
| 435 | ILS("%s: i = %d\n", __func__, i); |
| 436 | |
| 437 | /*DLS("%s: RAW ADC = 0x%03X\n", __func__, raw_adc_value);*/ |
| 438 | input_report_abs(lpi->ls_input_dev, ABS_MISC, level); |
| 439 | input_sync(lpi->ls_input_dev); |
| 440 | |
| 441 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 442 | ISL29029_INTERRUPT, ISL29029_INT_ALS_FLAG); |
| 443 | if (ret < 0) |
| 444 | ELS("%s: clear lsensor intr flag fail\n", __func__); |
| 445 | |
| 446 | } |
| 447 | |
| 448 | static int lightsensor_real_enable(struct isl29029_info *lpi) |
| 449 | { |
| 450 | int ret = -1; |
| 451 | |
| 452 | /*DLS("%s\n", __func__);*/ |
| 453 | |
| 454 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 1, |
| 455 | ISL29029_CONFIGURE, ISL29029_ALS_EN); |
| 456 | if (ret < 0) |
| 457 | ELS("%s: _isl29029_set_reg_bit error\n", __func__); |
| 458 | else { |
| 459 | lpi->als_enable = 1; |
| 460 | input_report_abs(lpi->ls_input_dev, ABS_MISC, -1); |
| 461 | input_sync(lpi->ls_input_dev); |
| 462 | |
| 463 | report_lsensor_input_event(lpi); |
| 464 | } |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | static void judge_and_enable_lightsensor(struct isl29029_info *lpi) |
| 470 | { |
| 471 | int ret; |
| 472 | |
| 473 | if (lpi->ls_enable_flag) { |
| 474 | ret = lightsensor_real_enable(lpi); |
| 475 | if (ret < 0) |
| 476 | ELS("%s: lightsensor_real_enable: not enabled!\n", |
| 477 | __func__); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | static void check_and_recover(struct isl29029_info *lpi) |
| 482 | { |
| 483 | uint8_t intrrupt, reg_config; |
| 484 | uint8_t def_intrrupt, def_reg_config; |
| 485 | char buffer[2]; |
| 486 | int ret = 0; |
| 487 | uint8_t orig_enabled; |
| 488 | |
| 489 | buffer[0] = ISL29029_INTERRUPT; |
| 490 | ret = I2C_RxData(buffer, 1); |
| 491 | if (ret < 0) { |
| 492 | EPS("%s: I2C_RxData fail (ISL29029_INTERRUPT)\n", |
| 493 | __func__); |
| 494 | return; |
| 495 | } |
| 496 | intrrupt = buffer[0]; |
| 497 | |
| 498 | buffer[0] = ISL29029_CONFIGURE; |
| 499 | ret = I2C_RxData(buffer, 1); |
| 500 | if (ret < 0) { |
| 501 | EPS("%s: I2C_RxData fail (ISL29029_CONFIGURE)\n", |
| 502 | __func__); |
| 503 | return; |
| 504 | } |
| 505 | reg_config = buffer[0]; |
| 506 | |
| 507 | def_intrrupt = (intrrupt & INTR_MASK); |
| 508 | def_reg_config = (reg_config & CONFIG_MASK); |
| 509 | orig_enabled = ((reg_config & ISL29029_PROX_EN) | |
| 510 | (reg_config & ISL29029_ALS_EN)); |
| 511 | |
| 512 | if (def_intrrupt != INTR_DEFAULT) { |
| 513 | buffer[0] = ISL29029_INTERRUPT; |
| 514 | buffer[1] = INTR_DEFAULT; |
| 515 | ret = I2C_TxData(buffer, 2); |
| 516 | if (ret < 0) |
| 517 | EPS("%s : Set LPS INTR fail\n", __func__); |
| 518 | } |
| 519 | |
| 520 | if (def_reg_config != lpi->default_config_reg) { |
| 521 | buffer[0] = ISL29029_CONFIGURE; |
| 522 | buffer[1] = (lpi->default_config_reg | orig_enabled); |
| 523 | ret = I2C_TxData(buffer, 2); |
| 524 | if (ret < 0) |
| 525 | EPS("%s : Set LPS Configuration fail\n", |
| 526 | __func__); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static void sensor_irq_do_work(struct work_struct *work) |
| 531 | { |
| 532 | uint8_t intrrupt, reg_config; |
| 533 | struct isl29029_info *lpi = lp_info; |
| 534 | char buffer[2]; |
| 535 | int ret = 0; |
| 536 | int value1 = -1; |
| 537 | static int count; |
| 538 | |
| 539 | uint16_t ps_adc = 0; |
| 540 | |
| 541 | wake_lock_timeout(&(lpi->ps_wake_lock), 3*HZ); |
| 542 | |
| 543 | value1 = gpio_get_value(lpi->intr_pin); |
| 544 | /*DPS("%s: lpi->intr_pin = %d\n", __func__, value1);*/ |
| 545 | |
| 546 | buffer[0] = ISL29029_INTERRUPT; |
| 547 | ret = I2C_RxData(buffer, 1); |
| 548 | if (ret < 0) { |
| 549 | EPS("%s: I2C_RxData %d fail (ISL29029_INTERRUPT)\n", |
| 550 | __func__, count); |
| 551 | |
| 552 | if (count < 5) |
| 553 | count++; |
| 554 | else { |
| 555 | count = 0; |
| 556 | input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, 1); |
| 557 | input_sync(lpi->ps_input_dev); |
| 558 | blocking_notifier_call_chain(&psensor_notifier_list, 3, NULL); |
| 559 | } |
| 560 | |
| 561 | enable_irq(lpi->irq); |
| 562 | return; |
| 563 | } |
| 564 | intrrupt = buffer[0]; |
| 565 | |
| 566 | buffer[0] = ISL29029_CONFIGURE; |
| 567 | ret = I2C_RxData(buffer, 1); |
| 568 | if (ret < 0) { |
| 569 | EPS("%s: I2C_RxData %d fail (ISL29029_CONFIGURE)\n", |
| 570 | __func__, count); |
| 571 | |
| 572 | if (count < 5) |
| 573 | count++; |
| 574 | else { |
| 575 | count = 0; |
| 576 | input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, 1); |
| 577 | input_sync(lpi->ps_input_dev); |
| 578 | blocking_notifier_call_chain(&psensor_notifier_list, 3, NULL); |
| 579 | } |
| 580 | |
| 581 | enable_irq(lpi->irq); |
| 582 | return; |
| 583 | } |
| 584 | reg_config = buffer[0]; |
| 585 | IPS("isl_irq: CONFIG = 0x%x, INT_PIN = %d, INT = 0x%x\n", |
| 586 | reg_config, value1, intrrupt); |
| 587 | |
| 588 | if (intrrupt & ISL29029_INT_PROX_FLAG) { |
| 589 | |
| 590 | ps_adc = get_ps_adc_value(); |
| 591 | DPS("isl_irq: ps_adc = 0x%02X, ps_lt = 0x%02X, ps_ht =" |
| 592 | " 0x%02X\n", ps_adc, lpi->ps_lt, lpi->ps_ht); |
| 593 | |
| 594 | report_psensor_input_event(lpi, ps_adc); |
| 595 | } |
| 596 | |
| 597 | if (intrrupt & ISL29029_INT_ALS_FLAG) |
| 598 | report_lsensor_input_event(lpi); |
| 599 | |
| 600 | check_and_recover(lpi); |
| 601 | |
| 602 | enable_irq(lpi->irq); |
| 603 | |
| 604 | count = 0; |
| 605 | } |
| 606 | |
| 607 | static void info_do_work(struct work_struct *w) |
| 608 | { |
| 609 | struct isl29029_info *lpi = lp_info; |
| 610 | uint8_t intrrupt, reg_config; |
| 611 | int ret, i = 0, level = 0; |
| 612 | int value1; |
| 613 | uint16_t value, TH1_value, TH2_value, TH3_value; |
| 614 | uint16_t PROX_LT, PROX_HT; |
| 615 | char buffer[4] = ""; |
| 616 | uint16_t adc_value, raw_adc_value; |
| 617 | uint16_t value_of_test1, value_of_test2; |
| 618 | |
| 619 | buffer[0] = ISL29029_TEST1; |
| 620 | ret = I2C_RxData(buffer, 2); |
| 621 | if (ret < 0) |
| 622 | EPS("%s: I2C_RxData fail (ISL29029_TEST1)\n", |
| 623 | __func__); |
| 624 | value_of_test1 = buffer[0]; |
| 625 | value_of_test2 = buffer[1]; |
| 626 | |
| 627 | DPS("\n"); |
| 628 | DPS("%s: TEST1 = 0x%02x, TEST2 = 0x%02x\n", |
| 629 | __func__, value_of_test1, value_of_test2); |
| 630 | |
| 631 | value1 = gpio_get_value(lpi->intr_pin); |
| 632 | DPS("%s: intr_pin = %d, value of intr_pin = %d\n", |
| 633 | __func__, lpi->intr_pin, value1); |
| 634 | |
| 635 | value = get_ps_adc_value(); |
| 636 | |
| 637 | DPS("%s: PS_ADC[0x%03X], ENABLE = %d\n", |
| 638 | __func__, value, lpi->ps_enable); |
| 639 | |
| 640 | adc_value = get_ls_adc_value(&raw_adc_value); |
| 641 | |
| 642 | for (i = 0; i < 10; i++) { |
| 643 | if (adc_value <= (*(lpi->adc_table + i))) { |
| 644 | level = i; |
| 645 | if (*(lpi->adc_table + i)) |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | DLS("%s: LS_ADC = 0x%03X, Level = %d \n", __func__, adc_value, level); |
| 651 | DLS("%s: LS_RAW ADC = 0x%03X\n", __func__, raw_adc_value); |
| 652 | |
| 653 | buffer[0] = ISL29029_LS_TH1; |
| 654 | ret = I2C_RxData(buffer, 3); |
| 655 | if (ret < 0) { |
| 656 | ELS("%s: I2C_RxData fail (ISL29029_LS_TH1)\n", |
| 657 | __func__); |
| 658 | return; |
| 659 | } |
| 660 | TH1_value = buffer[0]; |
| 661 | TH2_value = buffer[1]; |
| 662 | TH3_value = buffer[2]; |
| 663 | |
| 664 | DLS("%s: LS_LOW_TH--------->0x%01X%02X\n", __func__, |
| 665 | (TH2_value & 0xF), TH1_value); |
| 666 | DLS("%s: LS_HIGH_TH--------->0x%02X%01X\n", __func__, |
| 667 | TH3_value, ((TH2_value >> 4) & 0xF)); |
| 668 | |
| 669 | buffer[0] = ISL29029_PROX_LT; |
| 670 | ret = I2C_RxData(buffer, 2); |
| 671 | if (ret < 0) { |
| 672 | EPS("%s: I2C_RxData fail (ISL29029_PROX_LT)\n", |
| 673 | __func__); |
| 674 | return; |
| 675 | } |
| 676 | PROX_LT = buffer[0]; |
| 677 | PROX_HT = buffer[1]; |
| 678 | |
| 679 | DPS("%s: PROX_LT--------->0x%02X\n", __func__, PROX_LT); |
| 680 | DPS("%s: PROX_HT--------->0x%02X\n", __func__, PROX_HT); |
| 681 | |
| 682 | buffer[0] = ISL29029_INTERRUPT; |
| 683 | ret = I2C_RxData(buffer, 1); |
| 684 | if (ret < 0) { |
| 685 | EPS("%s: I2C_RxData fail (ISL29029_INTERRUPT)\n", |
| 686 | __func__); |
| 687 | return; |
| 688 | } |
| 689 | intrrupt = buffer[0]; |
| 690 | |
| 691 | buffer[0] = ISL29029_CONFIGURE; |
| 692 | ret = I2C_RxData(buffer, 1); |
| 693 | if (ret < 0) { |
| 694 | EPS("%s: I2C_RxData fail (ISL29029_CONFIGURE)\n", |
| 695 | __func__); |
| 696 | return; |
| 697 | } |
| 698 | reg_config = buffer[0]; |
| 699 | |
| 700 | DPS("%s: reg_config = 0x%x\n", __func__, reg_config); |
| 701 | DPS("%s: intrrupt = 0x%x\n", __func__, intrrupt); |
| 702 | } |
| 703 | |
| 704 | static irqreturn_t isl29029_irq_handler(int irq, void *data) |
| 705 | { |
| 706 | struct isl29029_info *lpi = data; |
| 707 | |
| 708 | /*int value1; |
| 709 | value1 = gpio_get_value(lpi->intr_pin); |
| 710 | DPS("\n%s: intr_pin = %d, value of intr_pin = %d\n", |
| 711 | __func__, lpi->intr_pin, value1);*/ |
| 712 | if (lpi->ps_enable == 1) |
| 713 | IPS("%s\n", __func__); |
| 714 | |
| 715 | disable_irq_nosync(lpi->irq); |
| 716 | queue_work_on(0, lpi->lp_wq, &sensor_irq_work); |
| 717 | |
| 718 | return IRQ_HANDLED; |
| 719 | } |
| 720 | |
| 721 | static int als_power(int enable) |
| 722 | { |
| 723 | struct isl29029_info *lpi = lp_info; |
| 724 | |
| 725 | if (lpi->power) |
| 726 | lpi->power(LS_PWR_ON, 1); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int psensor_enable(struct isl29029_info *lpi) |
| 732 | { |
| 733 | int ret; |
| 734 | uint16_t ps_adc = 0; |
| 735 | |
| 736 | IPS("%s\n", __func__); |
| 737 | if (lpi->ps_enable) { |
| 738 | DPS("%s: already enabled\n", __func__); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | blocking_notifier_call_chain(&psensor_notifier_list, 1, NULL); |
| 743 | |
| 744 | /* dummy report */ |
| 745 | input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, -1); |
| 746 | input_sync(lpi->ps_input_dev); |
| 747 | |
| 748 | if (lpi->power) |
| 749 | lpi->power(PS_PWR_ON, 1); |
| 750 | |
| 751 | ps_adc = get_ps_adc_value(); |
| 752 | IPS("isl_irq: ps_adc = 0x%02X, ps_lt = 0x%02X, ps_ht = 0x%02X\n", |
| 753 | ps_adc, lpi->ps_lt, lpi->ps_ht); |
| 754 | |
| 755 | wake_lock_timeout(&(lpi->ps_wake_lock), 2*HZ); |
| 756 | report_psensor_input_event(lpi, ps_adc); |
| 757 | |
| 758 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 1, |
| 759 | ISL29029_CONFIGURE, ISL29029_PROX_EN); |
| 760 | if (ret < 0) { |
| 761 | EPS("%s: enable psensor fail\n", __func__); |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | lpi->ps_enable = 1; |
| 766 | |
| 767 | ret = irq_set_irq_wake(lpi->irq, 1); |
| 768 | if (ret < 0) { |
| 769 | EPS("%s: failed to disable irq %d as a wake interrupt\n", |
| 770 | __func__, lpi->irq); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | static int psensor_disable(struct isl29029_info *lpi) |
| 778 | { |
| 779 | int ret = -EIO; |
| 780 | |
| 781 | judge_and_enable_lightsensor(lpi); |
| 782 | |
| 783 | IPS("%s\n", __func__); |
| 784 | if (!lpi->ps_enable) { |
| 785 | DPS("%s: already disabled\n", __func__); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | ret = irq_set_irq_wake(lpi->irq, 0); |
| 790 | if (ret < 0) { |
| 791 | EPS("%s: failed to disable irq %d as a wake interrupt\n", |
| 792 | __func__, lpi->irq); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 797 | ISL29029_CONFIGURE, ISL29029_PROX_EN); |
| 798 | if (ret < 0) { |
| 799 | EPS("%s: disable psensor fail\n", __func__); |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | blocking_notifier_call_chain(&psensor_notifier_list, 0, NULL); |
| 804 | |
| 805 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 0, |
| 806 | ISL29029_INTERRUPT, ISL29029_INT_PROX_FLAG); |
| 807 | if (ret < 0) { |
| 808 | EPS("%s: clear proximity INT flag fail\n", __func__); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | if (lpi->power) |
| 813 | lpi->power(PS_PWR_ON, 0); |
| 814 | |
| 815 | lpi->ps_enable = 0; |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | static int psensor_open(struct inode *inode, struct file *file) |
| 821 | { |
| 822 | struct isl29029_info *lpi = lp_info; |
| 823 | |
| 824 | DPS("%s\n", __func__); |
| 825 | |
| 826 | if (lpi->psensor_opened) |
| 827 | return -EBUSY; |
| 828 | |
| 829 | lpi->psensor_opened = 1; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int psensor_release(struct inode *inode, struct file *file) |
| 835 | { |
| 836 | struct isl29029_info *lpi = lp_info; |
| 837 | |
| 838 | DPS("%s\n", __func__); |
| 839 | |
| 840 | lpi->psensor_opened = 0; |
| 841 | |
| 842 | return psensor_disable(lpi); |
| 843 | } |
| 844 | |
| 845 | static long psensor_ioctl(struct file *file, unsigned int cmd, |
| 846 | unsigned long arg) |
| 847 | { |
| 848 | int val; |
| 849 | struct isl29029_info *lpi = lp_info; |
| 850 | |
| 851 | DPS("%s cmd %d\n", __func__, _IOC_NR(cmd)); |
| 852 | |
| 853 | switch (cmd) { |
| 854 | case CAPELLA_CM3602_IOCTL_ENABLE: |
| 855 | if (get_user(val, (unsigned long __user *)arg)) |
| 856 | return -EFAULT; |
| 857 | if (val) |
| 858 | return psensor_enable(lpi); |
| 859 | else |
| 860 | return psensor_disable(lpi); |
| 861 | break; |
| 862 | case CAPELLA_CM3602_IOCTL_GET_ENABLED: |
| 863 | return put_user(lpi->ps_enable, (unsigned long __user *)arg); |
| 864 | break; |
| 865 | default: |
| 866 | IPS("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd)); |
| 867 | return -EINVAL; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | static const struct file_operations psensor_fops = { |
| 872 | .owner = THIS_MODULE, |
| 873 | .open = psensor_open, |
| 874 | .release = psensor_release, |
| 875 | .unlocked_ioctl = psensor_ioctl |
| 876 | }; |
| 877 | |
| 878 | static struct miscdevice psensor_misc = { |
| 879 | .minor = MISC_DYNAMIC_MINOR, |
| 880 | .name = "cm3602", |
| 881 | .fops = &psensor_fops |
| 882 | }; |
| 883 | |
| 884 | static void lightsensor_set_kvalue(struct isl29029_info *lpi) |
| 885 | { |
| 886 | if (!lpi) { |
| 887 | ELS("%s: ls_info is empty\n", __func__); |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | ILS("%s: ALS calibrated als_kadc=0x%x\n", |
| 892 | __func__, als_kadc); |
| 893 | |
| 894 | if (als_kadc >> 16 == ALS_CALIBRATED) |
| 895 | lpi->als_kadc = als_kadc & 0xFFFF; |
| 896 | else { |
| 897 | lpi->als_kadc = 0; |
| 898 | ILS("%s: no ALS calibrated\n", __func__); |
| 899 | } |
| 900 | |
| 901 | if (lpi->als_kadc && lpi->golden_adc > 0) { |
| 902 | lpi->als_kadc = (lpi->als_kadc > 0 && lpi->als_kadc < 0x1000) ? |
| 903 | lpi->als_kadc : lpi->golden_adc; |
| 904 | lpi->als_gadc = lpi->golden_adc; |
| 905 | } else { |
| 906 | lpi->als_kadc = 1; |
| 907 | lpi->als_gadc = 1; |
| 908 | } |
| 909 | ILS("%s: als_kadc=0x%x, als_gadc=0x%x\n", |
| 910 | __func__, lpi->als_kadc, lpi->als_gadc); |
| 911 | } |
| 912 | |
| 913 | static void psensor_set_kvalue(struct isl29029_info *lpi) |
| 914 | { |
| 915 | int thl_value, thh_value; |
| 916 | |
| 917 | IPS("%s: PS calibrated ps_kparam1 = 0x%x, ps_kparam2 = 0x%x\n", |
| 918 | __func__, ps_kparam1, ps_kparam2); |
| 919 | |
| 920 | if (ps_kparam1 >> 16 == PS_CALIBRATED) { |
| 921 | |
| 922 | lpi->ps_B_val = (ps_kparam1 >> 8) & 0xFF; |
| 923 | lpi->ps_C_val = ps_kparam1 & 0xFF; |
| 924 | lpi->ps_A_val = (ps_kparam2 >> 24) & 0xFF; |
| 925 | lpi->ps_X_val = (ps_kparam2 >> 16) & 0xFF; |
| 926 | thl_value = (ps_kparam2 >> 8) & 0xFF; |
| 927 | thh_value = ps_kparam2 & 0xFF; |
| 928 | IPS("%s: PS calibrated ps_B_val = 0x%x, ps_C_val = 0x%x" |
| 929 | ", ps_A_val = 0x%x, ps_X_val = 0x%x, ps_lt = 0x%x" |
| 930 | ", ps_ht = 0x%x\n", |
| 931 | __func__, lpi->ps_B_val, lpi->ps_C_val, |
| 932 | lpi->ps_A_val, lpi->ps_X_val, thl_value, |
| 933 | thh_value); |
| 934 | |
| 935 | if ((thl_value != 0) && (thh_value != 0) && (thh_value > thl_value)) { |
| 936 | lpi->ps_lt = thl_value; |
| 937 | lpi->ps_ht = thh_value; |
| 938 | IPS("%s: PS calibrated ps_lt = 0x%x" |
| 939 | ", ps_ht = 0x%x\n", __func__, lpi->ps_lt, lpi->ps_ht); |
| 940 | } else{ |
| 941 | EPS("%s: PS no calibrated, default ps_lt = 0x%x" |
| 942 | ", ps_ht = 0x%x\n", __func__, lpi->ps_lt, lpi->ps_ht); |
| 943 | if (lpi->calibrate_func != NULL) { |
| 944 | if (lpi->ps_B_val != 0 && lpi->ps_C_val != 0) { |
| 945 | lpi->calibrate_func(lpi->ps_B_val, lpi->ps_C_val, lpi->ps_A_val, |
| 946 | lpi->ps_X_val, &thl_value, &thh_value); |
| 947 | IPS("%s: PS recaculate ps_lt = 0x%x" |
| 948 | ", ps_ht = 0x%x\n", __func__, thl_value, thh_value); |
| 949 | } |
| 950 | } |
| 951 | if ((thl_value != 0) && (thh_value != 0) && (thh_value > thl_value)) { |
| 952 | lpi->ps_lt = thl_value; |
| 953 | lpi->ps_ht = thh_value; |
| 954 | } |
| 955 | } |
| 956 | ps_threshold = (PS_CALIBRATED << 16) | |
| 957 | ((lpi->ps_lt << 8) | lpi->ps_ht); |
| 958 | |
| 959 | set_psensor_range(lpi->ps_lt, lpi->ps_ht); |
| 960 | } else |
| 961 | DPS("%s: Proximity not calibrated\n", __func__); |
| 962 | } |
| 963 | |
| 964 | static int lightsensor_update_table(struct isl29029_info *lpi) |
| 965 | { |
| 966 | uint16_t data[10]; |
| 967 | int i; |
| 968 | for (i = 0; i < 10; i++) { |
| 969 | if (*(lpi->adc_table + i) < 0xFFF) { |
| 970 | data[i] = *(lpi->adc_table + i) |
| 971 | * lpi->als_kadc / lpi->als_gadc; |
| 972 | } else { |
| 973 | data[i] = *(lpi->adc_table + i); |
| 974 | } |
| 975 | ILS("%s: Calibrated adc_table: data[%d] = %x\n", |
| 976 | __func__, i, data[i]); |
| 977 | } |
| 978 | memcpy(lpi->cali_table, data, 20); |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int lightsensor_enable(struct isl29029_info *lpi) |
| 983 | { |
| 984 | int ret; |
| 985 | |
| 986 | ILS("%s\n", __func__); |
| 987 | |
| 988 | lpi->ls_enable_flag = 1; |
| 989 | |
| 990 | if (!lpi->als_enable) { |
| 991 | ret = _isl29029_set_reg_bit(lpi->i2c_client, 1, |
| 992 | ISL29029_CONFIGURE, ISL29029_ALS_EN); |
| 993 | if (ret < 0) |
| 994 | ELS("%s: set auto light sensor fail\n", __func__); |
| 995 | else { |
| 996 | lpi->als_enable = 1; |
| 997 | /* report an invalid value first to ensure we |
| 998 | * trigger an event when adc_level is zero. |
| 999 | */ |
| 1000 | input_report_abs(lpi->ls_input_dev, ABS_MISC, -1); |
| 1001 | input_sync(lpi->ls_input_dev); |
| 1002 | |
| 1003 | report_lsensor_input_event(lpi); |
| 1004 | } |
| 1005 | } |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | static int lightsensor_disable(struct isl29029_info *lpi) |
| 1010 | { |
| 1011 | ILS("%s\n", __func__); |
| 1012 | |
| 1013 | lpi->ls_enable_flag = 0; |
| 1014 | |
| 1015 | lightsensor_real_disable(lpi); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static int lightsensor_open(struct inode *inode, struct file *file) |
| 1021 | { |
| 1022 | struct isl29029_info *lpi = lp_info; |
| 1023 | int rc = 0; |
| 1024 | |
| 1025 | DLS("%s\n", __func__); |
| 1026 | if (lpi->lightsensor_opened) { |
| 1027 | DLS("%s: already opened\n", __func__); |
| 1028 | rc = -EBUSY; |
| 1029 | } |
| 1030 | lpi->lightsensor_opened = 1; |
| 1031 | return rc; |
| 1032 | } |
| 1033 | |
| 1034 | static int lightsensor_release(struct inode *inode, struct file *file) |
| 1035 | { |
| 1036 | struct isl29029_info *lpi = lp_info; |
| 1037 | |
| 1038 | DLS("%s\n", __func__); |
| 1039 | lpi->lightsensor_opened = 0; |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | static long lightsensor_ioctl(struct file *file, unsigned int cmd, |
| 1044 | unsigned long arg) |
| 1045 | { |
| 1046 | int rc, val; |
| 1047 | struct isl29029_info *lpi = lp_info; |
| 1048 | |
| 1049 | DLS("%s cmd %d\n", __func__, _IOC_NR(cmd)); |
| 1050 | |
| 1051 | switch (cmd) { |
| 1052 | case LIGHTSENSOR_IOCTL_ENABLE: |
| 1053 | if (get_user(val, (unsigned long __user *)arg)) { |
| 1054 | rc = -EFAULT; |
| 1055 | break; |
| 1056 | } |
| 1057 | /*DLS("%s value = %d\n", __func__, val);*/ |
| 1058 | rc = val ? lightsensor_enable(lpi) : lightsensor_disable(lpi); |
| 1059 | break; |
| 1060 | case LIGHTSENSOR_IOCTL_GET_ENABLED: |
| 1061 | val = lpi->ls_enable_flag; |
| 1062 | /*DLS("%s enabled %d\n", __func__, val);*/ |
| 1063 | rc = put_user(val, (unsigned long __user *)arg); |
| 1064 | break; |
| 1065 | default: |
| 1066 | ILS("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd)); |
| 1067 | rc = -EINVAL; |
| 1068 | } |
| 1069 | |
| 1070 | return rc; |
| 1071 | } |
| 1072 | |
| 1073 | static const struct file_operations lightsensor_fops = { |
| 1074 | .owner = THIS_MODULE, |
| 1075 | .open = lightsensor_open, |
| 1076 | .release = lightsensor_release, |
| 1077 | .unlocked_ioctl = lightsensor_ioctl |
| 1078 | }; |
| 1079 | |
| 1080 | static struct miscdevice lightsensor_misc = { |
| 1081 | .minor = MISC_DYNAMIC_MINOR, |
| 1082 | .name = "lightsensor", |
| 1083 | .fops = &lightsensor_fops |
| 1084 | }; |
| 1085 | |
| 1086 | static ssize_t ps_adc_show(struct device *dev, |
| 1087 | struct device_attribute *attr, char *buf) |
| 1088 | { |
| 1089 | |
| 1090 | uint16_t value; |
| 1091 | int ret; |
| 1092 | struct isl29029_info *lpi = lp_info; |
| 1093 | |
| 1094 | int value1; |
| 1095 | |
| 1096 | info_do_work(NULL); |
| 1097 | |
| 1098 | value1 = gpio_get_value(lpi->intr_pin); |
| 1099 | |
| 1100 | value = get_ps_adc_value(); |
| 1101 | |
| 1102 | ret = sprintf(buf, "ADC[0x%03X], ENABLE = %d, intr_pin = %d\n" |
| 1103 | , value, lpi->ps_enable, value1); |
| 1104 | |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static ssize_t ps_enable_store(struct device *dev, |
| 1109 | struct device_attribute *attr, |
| 1110 | const char *buf, size_t count) |
| 1111 | { |
| 1112 | int ps_en; |
| 1113 | struct isl29029_info *lpi = lp_info; |
| 1114 | |
| 1115 | ps_en = -1; |
| 1116 | sscanf(buf, "%d", &ps_en); |
| 1117 | |
| 1118 | if (ps_en != 0 && ps_en != 1) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | if (ps_en) |
| 1122 | psensor_enable(lpi); |
| 1123 | else |
| 1124 | psensor_disable(lpi); |
| 1125 | |
| 1126 | return count; |
| 1127 | } |
| 1128 | |
| 1129 | static DEVICE_ATTR(ps_adc, 0664, ps_adc_show, ps_enable_store); |
| 1130 | |
| 1131 | static ssize_t ps_kadc_show(struct device *dev, |
| 1132 | struct device_attribute *attr, char *buf) |
| 1133 | { |
| 1134 | int ret = 0; |
| 1135 | struct isl29029_info *lpi = lp_info; |
| 1136 | |
| 1137 | if ((ps_kparam1 >> 16) == PS_CALIBRATED) { |
| 1138 | ret = sprintf(buf, "P-sensor calibrated, " |
| 1139 | "(B_value, C_value, A_value, X_value," |
| 1140 | " THL, THH) = (0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n", |
| 1141 | lpi->ps_B_val, lpi->ps_C_val, lpi->ps_A_val, |
| 1142 | lpi->ps_X_val, lpi->ps_lt, lpi->ps_ht); |
| 1143 | } else { |
| 1144 | ret = sprintf(buf, "P-sensor NOT calibrated, " |
| 1145 | "(B_value, C_value, A_value, X_value," |
| 1146 | " THL, THH) = (0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n", |
| 1147 | lpi->ps_B_val, lpi->ps_C_val, lpi->ps_A_val, |
| 1148 | lpi->ps_X_val, lpi->ps_lt, lpi->ps_ht); |
| 1149 | } |
| 1150 | |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
| 1154 | static ssize_t ps_kadc_store(struct device *dev, |
| 1155 | struct device_attribute *attr, |
| 1156 | const char *buf, size_t count) |
| 1157 | { |
| 1158 | uint8_t lt, ht; |
| 1159 | int param1, param2; |
| 1160 | int ret; |
| 1161 | struct isl29029_info *lpi = lp_info; |
| 1162 | |
| 1163 | sscanf(buf, "0x%x 0x%x", ¶m1, ¶m2); |
| 1164 | |
| 1165 | DPS("%s: store value = 0x%X, 0x%X\n", __func__, param1, param2); |
| 1166 | |
| 1167 | lt = (param2 >> 8) & 0xFF; |
| 1168 | ht = param2 & 0xFF; |
| 1169 | |
| 1170 | ps_threshold = (PS_CALIBRATED << 16) | (param2 & 0xFFFF); |
| 1171 | |
| 1172 | DPS("%s: lt = 0x%X, ht = 0x%X\n", __func__, lt, ht); |
| 1173 | DPS("%s: ps_threshold = 0x%X\n", __func__, ps_threshold); |
| 1174 | |
| 1175 | if (lt < ht) { |
| 1176 | |
| 1177 | lpi->ps_B_val = (param1 >> 8) & 0xFF; |
| 1178 | lpi->ps_C_val = param1 & 0xFF; |
| 1179 | lpi->ps_A_val = (param2 >> 24) & 0xFF; |
| 1180 | lpi->ps_X_val = (param2 >> 16) & 0xFF; |
| 1181 | lpi->ps_lt = (param2 >> 8) & 0xFF; |
| 1182 | lpi->ps_ht = param2 & 0xFF; |
| 1183 | |
| 1184 | ret = set_psensor_range(lt, ht); |
| 1185 | if (ret < 0) { |
| 1186 | EPS("%s : write ISL29029_PS_LT_HT fail\n", |
| 1187 | __func__); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | lpi->ps_lt = lt; |
| 1191 | lpi->ps_ht = ht; |
| 1192 | } else |
| 1193 | DPS("%s: Setting PS calibration ERROR!\n", __func__); |
| 1194 | |
| 1195 | return count; |
| 1196 | } |
| 1197 | |
| 1198 | static DEVICE_ATTR(ps_kadc, 0664, ps_kadc_show, ps_kadc_store); |
| 1199 | |
| 1200 | static ssize_t ps_led_show(struct device *dev, |
| 1201 | struct device_attribute *attr, char *buf) |
| 1202 | { |
| 1203 | char buffer[2]; |
| 1204 | uint8_t value; |
| 1205 | int ret = 0; |
| 1206 | struct isl29029_info *lpi = lp_info; |
| 1207 | |
| 1208 | buffer[0] = ISL29029_CONFIGURE; |
| 1209 | ret = I2C_RxData(buffer, 1); |
| 1210 | if (ret < 0) { |
| 1211 | EPS("%s: I2C_RxData fail\n", __func__); |
| 1212 | return ret; |
| 1213 | } |
| 1214 | value = buffer[0]; |
| 1215 | DPS("%s: Configure reg = 0x%x\n", __func__, value); |
| 1216 | |
| 1217 | lpi->led = (value & 0x08) >> 3; |
| 1218 | DPS("%s: lpi->led = 0x%x\n", __func__, lpi->led); |
| 1219 | |
| 1220 | ret = sprintf(buf, "Current led setting: %s\n", |
| 1221 | (lpi->led == 0) ? "100mA" : "200mA"); |
| 1222 | |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | static ssize_t ps_led_store(struct device *dev, |
| 1227 | struct device_attribute *attr, |
| 1228 | const char *buf, size_t count) |
| 1229 | { |
| 1230 | int param; |
| 1231 | int ret; |
| 1232 | struct isl29029_info *lpi = lp_info; |
| 1233 | |
| 1234 | sscanf(buf, "%d", ¶m); |
| 1235 | |
| 1236 | DPS("%s: store value = %d\n", __func__, param); |
| 1237 | |
| 1238 | if (param == 0 || param == 1) { |
| 1239 | ret = _isl29029_set_reg_bit(lpi->i2c_client, |
| 1240 | param, ISL29029_CONFIGURE, ISL29029_PROX_DR); |
| 1241 | if (ret < 0) { |
| 1242 | EPS("%s : Set LED fail\n", __func__); |
| 1243 | return -1; |
| 1244 | } |
| 1245 | lpi->led = param; |
| 1246 | } else |
| 1247 | DPS("%s: Setting PS LED ERROR, please use 0 or 1!\n", __func__); |
| 1248 | |
| 1249 | return count; |
| 1250 | } |
| 1251 | |
| 1252 | static DEVICE_ATTR(ps_led, 0664, ps_led_show, ps_led_store); |
| 1253 | |
| 1254 | static ssize_t ps_test_mode_show(struct device *dev, |
| 1255 | struct device_attribute *attr, char *buf) |
| 1256 | { |
| 1257 | char buffer[2] = ""; |
| 1258 | uint8_t test1 = 0, test2 = 0, is_test_mode = 0; |
| 1259 | int ret = 0; |
| 1260 | |
| 1261 | buffer[0] = ISL29029_TEST1; |
| 1262 | ret = I2C_RxData(buffer, 1); |
| 1263 | if (ret < 0) { |
| 1264 | EPS("%s: I2C_RxData fail, Test1\n", __func__); |
| 1265 | return ret; |
| 1266 | } |
| 1267 | test1 = buffer[0]; |
| 1268 | |
| 1269 | buffer[0] = ISL29029_TEST2; |
| 1270 | ret = I2C_RxData(buffer, 1); |
| 1271 | if (ret < 0) { |
| 1272 | EPS("%s: I2C_RxData fail, Test2\n", __func__); |
| 1273 | return ret; |
| 1274 | } |
| 1275 | test2 = buffer[0]; |
| 1276 | |
| 1277 | if ((test1 == 0) && (test2 == 0)) |
| 1278 | is_test_mode = 0; |
| 1279 | else |
| 1280 | is_test_mode = 1; |
| 1281 | |
| 1282 | ret = sprintf(buf, "Test mode = %d, test1 = 0x%x, test2 = 0x%x\n", |
| 1283 | is_test_mode, test1, test2); |
| 1284 | |
| 1285 | return ret; |
| 1286 | } |
| 1287 | |
| 1288 | static ssize_t ps_test_mode_store(struct device *dev, |
| 1289 | struct device_attribute *attr, |
| 1290 | const char *buf, size_t count) |
| 1291 | { |
| 1292 | char buffer[2] = ""; |
| 1293 | int test1 = 0, test2 = 0; |
| 1294 | int value = 0; |
| 1295 | int ret; |
| 1296 | |
| 1297 | sscanf(buf, "0x%02x 0x%02x", &test1, &test2); |
| 1298 | |
| 1299 | DPS("%s: store value = %d\n", __func__, value); |
| 1300 | |
| 1301 | buffer[0] = ISL29029_TEST1; |
| 1302 | buffer[1] = (test1 & 0xFF); |
| 1303 | ret = I2C_TxData(buffer, 2); |
| 1304 | if (ret < 0) |
| 1305 | EPS("%s : failed to set ISL29029_TEST1\n", |
| 1306 | __func__); |
| 1307 | |
| 1308 | buffer[0] = ISL29029_TEST2; |
| 1309 | buffer[1] = (test2 & 0xFF); |
| 1310 | ret = I2C_TxData(buffer, 2); |
| 1311 | if (ret < 0) |
| 1312 | EPS("%s : failed to set ISL29029_TEST2\n", |
| 1313 | __func__); |
| 1314 | |
| 1315 | return count; |
| 1316 | } |
| 1317 | |
| 1318 | static DEVICE_ATTR(ps_test_mode, 0664, ps_test_mode_show, ps_test_mode_store); |
| 1319 | |
| 1320 | static ssize_t ls_adc_show(struct device *dev, |
| 1321 | struct device_attribute *attr, char *buf) |
| 1322 | { |
| 1323 | uint16_t value, raw_adc_value; |
| 1324 | int ret, i, level = -1; |
| 1325 | struct isl29029_info *lpi = lp_info; |
| 1326 | |
| 1327 | value = get_ls_adc_value(&raw_adc_value); |
| 1328 | |
| 1329 | for (i = 0; i < 10; i++) { |
| 1330 | if (value <= (*(lpi->adc_table + i))) { |
| 1331 | level = i; |
| 1332 | if (*(lpi->adc_table + i)) |
| 1333 | break; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | ret = sprintf(buf, "ADC[0x%03X] => level %d\n", value, level); |
| 1338 | |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static DEVICE_ATTR(ls_adc, 0664, ls_adc_show, NULL); |
| 1343 | |
| 1344 | static ssize_t ls_enable_show(struct device *dev, |
| 1345 | struct device_attribute *attr, char *buf) |
| 1346 | { |
| 1347 | uint8_t value; |
| 1348 | int ret; |
| 1349 | /*struct isl29029_info *lpi = lp_info;*/ |
| 1350 | char buffer[2]; |
| 1351 | |
| 1352 | buffer[0] = ISL29029_CONFIGURE; |
| 1353 | ret = I2C_RxData(buffer, 1); |
| 1354 | if (ret < 0) { |
| 1355 | ELS("%s: I2C_RxData fail\n", __func__); |
| 1356 | return ret; |
| 1357 | } |
| 1358 | value = buffer[0]; |
| 1359 | |
| 1360 | ret = sprintf(buf, "Light sensor Auto Enable = %d\n", |
| 1361 | (value & 0x04) ? 1 : 0); |
| 1362 | |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | static ssize_t ls_enable_store(struct device *dev, |
| 1367 | struct device_attribute *attr, |
| 1368 | const char *buf, size_t count) |
| 1369 | { |
| 1370 | |
| 1371 | int ls_auto; |
| 1372 | int want_enable; |
| 1373 | int ret; |
| 1374 | struct isl29029_info *lpi = lp_info; |
| 1375 | |
| 1376 | ls_auto = -1; |
| 1377 | sscanf(buf, "%d", &ls_auto); |
| 1378 | |
| 1379 | if (ls_auto != 0 && ls_auto != 1 && ls_auto != 147) |
| 1380 | return -EINVAL; |
| 1381 | |
| 1382 | if (ls_auto) { |
| 1383 | lpi->ls_calibrate = (ls_auto == 147) ? 1 : 0; |
| 1384 | want_enable = 1; |
| 1385 | } else { |
| 1386 | lpi->ls_calibrate = 0; |
| 1387 | want_enable = 0; |
| 1388 | } |
| 1389 | |
| 1390 | DLS("%s: want_enable = %d!\n", __func__, want_enable); |
| 1391 | |
| 1392 | ret = _isl29029_set_reg_bit(lpi->i2c_client, |
| 1393 | ((want_enable) ? 1 : 0), |
| 1394 | ISL29029_CONFIGURE, ISL29029_ALS_EN); |
| 1395 | if (ret < 0) |
| 1396 | ELS("%s: ls enable fail\n", __func__); |
| 1397 | else |
| 1398 | lpi->als_enable = (want_enable) ? 1 : 0; |
| 1399 | |
| 1400 | return count; |
| 1401 | } |
| 1402 | |
| 1403 | static DEVICE_ATTR(ls_auto, 0664, |
| 1404 | ls_enable_show, ls_enable_store); |
| 1405 | |
| 1406 | static ssize_t ls_kadc_show(struct device *dev, |
| 1407 | struct device_attribute *attr, char *buf) |
| 1408 | { |
| 1409 | struct isl29029_info *lpi = lp_info; |
| 1410 | int ret; |
| 1411 | |
| 1412 | ret = sprintf(buf, "kadc = 0x%x, gadc = 0x%x, kadc while this boot" |
| 1413 | " = 0x%x\n", |
| 1414 | lpi->als_kadc, lpi->als_gadc, als_kadc); |
| 1415 | |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
| 1419 | static ssize_t ls_kadc_store(struct device *dev, |
| 1420 | struct device_attribute *attr, |
| 1421 | const char *buf, size_t count) |
| 1422 | { |
| 1423 | struct isl29029_info *lpi = lp_info; |
| 1424 | int kadc_temp = 0; |
| 1425 | |
| 1426 | sscanf(buf, "%d", &kadc_temp); |
| 1427 | if (kadc_temp <= 0 || lpi->golden_adc <= 0) { |
| 1428 | ELS("%s: kadc_temp=0x%x, als_gadc=0x%x\n", |
| 1429 | __func__, kadc_temp, lpi->golden_adc); |
| 1430 | return -EINVAL; |
| 1431 | } |
| 1432 | |
| 1433 | lpi->als_kadc = kadc_temp; |
| 1434 | lpi->als_gadc = lpi->golden_adc; |
| 1435 | ILS("%s: als_kadc=0x%x, als_gadc=0x%x\n", |
| 1436 | __func__, lpi->als_kadc, lpi->als_gadc); |
| 1437 | |
| 1438 | if (lightsensor_update_table(lpi) < 0) |
| 1439 | ELS("%s: update ls table fail\n", __func__); |
| 1440 | |
| 1441 | return count; |
| 1442 | } |
| 1443 | |
| 1444 | static DEVICE_ATTR(ls_kadc, 0664, ls_kadc_show, ls_kadc_store); |
| 1445 | |
| 1446 | static int lightsensor_setup(struct isl29029_info *lpi) |
| 1447 | { |
| 1448 | int ret; |
| 1449 | |
| 1450 | lpi->ls_input_dev = input_allocate_device(); |
| 1451 | if (!lpi->ls_input_dev) { |
| 1452 | ELS("%s: could not allocate ls input device\n", __func__); |
| 1453 | return -ENOMEM; |
| 1454 | } |
| 1455 | lpi->ls_input_dev->name = "lightsensor-level"; |
| 1456 | set_bit(EV_ABS, lpi->ls_input_dev->evbit); |
| 1457 | input_set_abs_params(lpi->ls_input_dev, ABS_MISC, 0, 9, 0, 0); |
| 1458 | |
| 1459 | ret = input_register_device(lpi->ls_input_dev); |
| 1460 | if (ret < 0) { |
| 1461 | ELS("%s: can not register ls input device\n", |
| 1462 | __func__); |
| 1463 | goto err_free_ls_input_device; |
| 1464 | } |
| 1465 | |
| 1466 | ret = misc_register(&lightsensor_misc); |
| 1467 | if (ret < 0) { |
| 1468 | ELS("%s: can not register ls misc device\n", |
| 1469 | __func__); |
| 1470 | goto err_unregister_ls_input_device; |
| 1471 | } |
| 1472 | |
| 1473 | return ret; |
| 1474 | |
| 1475 | err_unregister_ls_input_device: |
| 1476 | input_unregister_device(lpi->ls_input_dev); |
| 1477 | err_free_ls_input_device: |
| 1478 | input_free_device(lpi->ls_input_dev); |
| 1479 | return ret; |
| 1480 | } |
| 1481 | |
| 1482 | static int psensor_setup(struct isl29029_info *lpi) |
| 1483 | { |
| 1484 | int ret; |
| 1485 | |
| 1486 | lpi->ps_input_dev = input_allocate_device(); |
| 1487 | if (!lpi->ps_input_dev) { |
| 1488 | EPS("%s: could not allocate ps input device\n", __func__); |
| 1489 | return -ENOMEM; |
| 1490 | } |
| 1491 | lpi->ps_input_dev->name = "proximity"; |
| 1492 | set_bit(EV_ABS, lpi->ps_input_dev->evbit); |
| 1493 | input_set_abs_params(lpi->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0); |
| 1494 | |
| 1495 | ret = input_register_device(lpi->ps_input_dev); |
| 1496 | if (ret < 0) { |
| 1497 | EPS("%s: could not register ps input device\n", __func__); |
| 1498 | goto err_free_ps_input_device; |
| 1499 | } |
| 1500 | |
| 1501 | ret = misc_register(&psensor_misc); |
| 1502 | if (ret < 0) { |
| 1503 | EPS("%s: could not register ps misc device\n", __func__); |
| 1504 | goto err_unregister_ps_input_device; |
| 1505 | } |
| 1506 | |
| 1507 | return ret; |
| 1508 | |
| 1509 | err_unregister_ps_input_device: |
| 1510 | input_unregister_device(lpi->ps_input_dev); |
| 1511 | err_free_ps_input_device: |
| 1512 | input_free_device(lpi->ps_input_dev); |
| 1513 | return ret; |
| 1514 | } |
| 1515 | |
| 1516 | static int isl29029_power_up_seq(void) |
| 1517 | { |
| 1518 | int ret = 0; |
| 1519 | char buffer[2]; |
| 1520 | |
| 1521 | buffer[0] = ISL29029_CONFIGURE; |
| 1522 | buffer[1] = 0x0; |
| 1523 | ret = I2C_TxData(buffer, 2); |
| 1524 | if (ret < 0) |
| 1525 | EPS("%s : failed to set ISL29029_CONFIGURE\n", |
| 1526 | __func__); |
| 1527 | |
| 1528 | buffer[0] = ISL29029_TEST2; |
| 1529 | buffer[1] = 0x29; |
| 1530 | ret = I2C_TxData(buffer, 2); |
| 1531 | if (ret < 0) |
| 1532 | EPS("%s : failed to set ISL29029_TEST2[1]\n", |
| 1533 | __func__); |
| 1534 | |
| 1535 | buffer[0] = ISL29029_TEST1; |
| 1536 | buffer[1] = 0x0; |
| 1537 | ret = I2C_TxData(buffer, 2); |
| 1538 | if (ret < 0) |
| 1539 | EPS("%s : failed to set ISL29029_TEST1\n", |
| 1540 | __func__); |
| 1541 | |
| 1542 | buffer[0] = ISL29029_TEST2; |
| 1543 | buffer[1] = 0x0; |
| 1544 | ret = I2C_TxData(buffer, 2); |
| 1545 | if (ret < 0) |
| 1546 | EPS("%s : failed to set ISL29029_TEST2[2]\n", |
| 1547 | __func__); |
| 1548 | |
| 1549 | msleep(1); |
| 1550 | |
| 1551 | return ret; |
| 1552 | } |
| 1553 | |
| 1554 | static int isl29029_setup(struct isl29029_info *lpi) |
| 1555 | { |
| 1556 | int ret = 0; |
| 1557 | char buffer[2]; |
| 1558 | |
| 1559 | ret = gpio_request(lpi->intr_pin, "gpio_isl29029_intr"); |
| 1560 | if (ret < 0) { |
| 1561 | EPS("%s: gpio %d request failed (%d)\n", |
| 1562 | __func__, lpi->intr_pin, ret); |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | ret = gpio_direction_input(lpi->intr_pin); |
| 1567 | if (ret < 0) { |
| 1568 | EPS("%s: failed to set gpio %d as input (%d)\n", |
| 1569 | __func__, lpi->intr_pin, ret); |
| 1570 | goto fail_free_intr_pin; |
| 1571 | } |
| 1572 | |
| 1573 | ret = request_any_context_irq(lpi->irq, |
| 1574 | isl29029_irq_handler, |
| 1575 | IRQF_TRIGGER_LOW, |
| 1576 | "isl29029", |
| 1577 | lpi); |
| 1578 | if (ret < 0) { |
| 1579 | EPS("%s: request_irq(%d) failed for gpio %d (%d)\n", |
| 1580 | __func__, lpi->irq, |
| 1581 | lpi->intr_pin, ret); |
| 1582 | goto fail_free_intr_pin; |
| 1583 | } |
| 1584 | |
| 1585 | ret = isl29029_power_up_seq(); |
| 1586 | if (ret < 0) { |
| 1587 | EPS("%s: isl29018_power_up_seq() fail!!\n", __func__); |
| 1588 | goto err_configure; |
| 1589 | } |
| 1590 | |
| 1591 | buffer[0] = ISL29029_CONFIGURE; |
| 1592 | buffer[1] = lpi->default_config_reg; |
| 1593 | ret = I2C_TxData(buffer, 2); |
| 1594 | if (ret < 0) { |
| 1595 | EPS("%s : failed to set LPS Configuration fail\n", |
| 1596 | __func__); |
| 1597 | goto err_configure; |
| 1598 | } |
| 1599 | lpi->led = 0; |
| 1600 | |
| 1601 | ret = set_lsensor_range(0x0, 0x0); |
| 1602 | if (ret < 0) { |
| 1603 | ELS("%s : write ISL29029_LS_TH123 fail\n", |
| 1604 | __func__); |
| 1605 | goto err_set_lsensor_range; |
| 1606 | } |
| 1607 | |
| 1608 | lpi->ps_lt = lpi->default_ps_lt; |
| 1609 | lpi->ps_ht = lpi->default_ps_ht; |
| 1610 | ret = set_psensor_range(lpi->ps_lt, lpi->ps_ht); |
| 1611 | if (ret < 0) { |
| 1612 | EPS("%s : write ISL29029_PS_LT_HT fail\n", |
| 1613 | __func__); |
| 1614 | goto err_set_psensor_range; |
| 1615 | } |
| 1616 | |
| 1617 | buffer[0] = ISL29029_INTERRUPT; |
| 1618 | buffer[1] = INTR_DEFAULT; |
| 1619 | ret = I2C_TxData(buffer, 2); |
| 1620 | if (ret < 0) { |
| 1621 | EPS("%s: set ALS PRST fail\n", __func__); |
| 1622 | goto err_set_als_prst; |
| 1623 | } |
| 1624 | |
| 1625 | return ret; |
| 1626 | |
| 1627 | err_set_als_prst: |
| 1628 | err_set_psensor_range: |
| 1629 | err_set_lsensor_range: |
| 1630 | err_configure: |
| 1631 | free_irq(lpi->irq, lpi); |
| 1632 | fail_free_intr_pin: |
| 1633 | gpio_free(lpi->intr_pin); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
| 1637 | static int isl29029_suspend(struct i2c_client *client, pm_message_t mesg) |
| 1638 | { |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static int isl29029_resume(struct i2c_client *client) |
| 1643 | { |
| 1644 | struct isl29029_info *lpi = lp_info; |
| 1645 | char buffer[2]; |
| 1646 | uint8_t reg_config = 0; |
| 1647 | int ret = 0; |
| 1648 | |
| 1649 | buffer[0] = ISL29029_CONFIGURE; |
| 1650 | ret = I2C_RxData(buffer, 1); |
| 1651 | if (ret < 0) |
| 1652 | EPS("%s: I2C_RxData fail (ISL29029_CONFIGURE)\n", |
| 1653 | __func__); |
| 1654 | reg_config = buffer[0]; |
| 1655 | |
| 1656 | if (((reg_config & ISL29029_PROX_EN) == 0) && |
| 1657 | ((reg_config & ISL29029_ALS_EN) == 0) && |
| 1658 | (ret == 0)) { |
| 1659 | ret = isl29029_power_up_seq(); |
| 1660 | if (ret < 0) |
| 1661 | EPS("%s: isl29018_power_up_seq() fail!!\n", |
| 1662 | __func__); |
| 1663 | |
| 1664 | buffer[0] = ISL29029_CONFIGURE; |
| 1665 | buffer[1] = lpi->default_config_reg; |
| 1666 | ret = I2C_TxData(buffer, 2); |
| 1667 | if (ret < 0) |
| 1668 | EPS("%s : failed to set " |
| 1669 | "ISL29029_CONFIGURE2\n", __func__); |
| 1670 | |
| 1671 | buffer[0] = ISL29029_INTERRUPT; |
| 1672 | buffer[1] = INTR_DEFAULT; |
| 1673 | ret = I2C_TxData(buffer, 2); |
| 1674 | if (ret < 0) |
| 1675 | EPS("%s : Set LPS INTR2 fail\n", __func__); |
| 1676 | |
| 1677 | ret = set_lsensor_range(0x0, 0x0); |
| 1678 | if (ret < 0) |
| 1679 | ELS("%s : write ISL29029_LS_TH123 fail\n", |
| 1680 | __func__); |
| 1681 | |
| 1682 | DPS("isl_resume: Re-initialize isl29029, ret = %d\n", ret); |
| 1683 | } |
| 1684 | |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | static int isl29029_probe(struct i2c_client *client, |
| 1689 | const struct i2c_device_id *id) |
| 1690 | { |
| 1691 | int ret = 0; |
| 1692 | char buffer[2]; |
| 1693 | struct isl29029_info *lpi; |
| 1694 | struct isl29029_platform_data *pdata; |
| 1695 | |
| 1696 | IPS("%s\n", __func__); |
| 1697 | |
| 1698 | lpi = kzalloc(sizeof(struct isl29029_info), GFP_KERNEL); |
| 1699 | if (!lpi) |
| 1700 | return -ENOMEM; |
| 1701 | |
| 1702 | /*DPS("%s: client->irq = %d\n", __func__, client->irq);*/ |
| 1703 | |
| 1704 | lpi->i2c_client = client; |
| 1705 | pdata = client->dev.platform_data; |
| 1706 | if (!pdata) { |
| 1707 | EPS("%s: Assign platform_data error!!\n", __func__); |
| 1708 | ret = -EBUSY; |
| 1709 | goto err_platform_data_null; |
| 1710 | } |
| 1711 | |
| 1712 | lpi->irq = client->irq; |
| 1713 | |
| 1714 | i2c_set_clientdata(client, lpi); |
| 1715 | lpi->intr_pin = pdata->intr; |
| 1716 | lpi->adc_table = pdata->levels; |
| 1717 | lpi->golden_adc = pdata->golden_adc; |
| 1718 | lpi->power = pdata->power; |
| 1719 | lpi->default_ps_lt = pdata->lt; |
| 1720 | lpi->default_ps_ht = pdata->ht; |
| 1721 | lpi->calibrate_func = pdata->calibrate_func; |
| 1722 | if (pdata->default_config_reg != 0) |
| 1723 | lpi->default_config_reg = pdata->default_config_reg; |
| 1724 | else |
| 1725 | lpi->default_config_reg = CONFIG_DEFAULT; |
| 1726 | lp_info = lpi; |
| 1727 | |
| 1728 | /*DLS("Andy: lpi->default_config_reg = 0x%x\n", |
| 1729 | lpi->default_config_reg);*/ |
| 1730 | |
| 1731 | als_power(1); |
| 1732 | |
| 1733 | buffer[0] = ISL29029_CHIPID; |
| 1734 | ret = I2C_RxData(buffer, 1); |
| 1735 | if (ret < 0) { |
| 1736 | EPS("%s: Read Chip ID fail\n", __func__); |
| 1737 | goto err_platform_data_null; |
| 1738 | } |
| 1739 | |
| 1740 | if (buffer[0] != 0xA7) { |
| 1741 | EPS("%s: Error Chip ID or i2c error\n", __func__); |
| 1742 | goto err_platform_data_null; |
| 1743 | } |
| 1744 | |
| 1745 | ret = lightsensor_setup(lpi); |
| 1746 | if (ret < 0) { |
| 1747 | ELS("%s: lightsensor_setup error!!\n", __func__); |
| 1748 | goto err_lightsensor_setup; |
| 1749 | } |
| 1750 | |
| 1751 | ret = psensor_setup(lpi); |
| 1752 | if (ret < 0) { |
| 1753 | EPS("%s: psensor_setup error!!\n", __func__); |
| 1754 | goto err_psensor_setup; |
| 1755 | } |
| 1756 | |
| 1757 | lightsensor_set_kvalue(lpi); |
| 1758 | |
| 1759 | ret = lightsensor_update_table(lpi); |
| 1760 | if (ret < 0) { |
| 1761 | ELS("%s: update ls table fail\n", |
| 1762 | __func__); |
| 1763 | goto err_lightsensor_update_table; |
| 1764 | } |
| 1765 | |
| 1766 | lpi->lp_wq = create_singlethread_workqueue("isl29029_wq"); |
| 1767 | if (!lpi->lp_wq) { |
| 1768 | EPS("%s: can't create workqueue\n", __func__); |
| 1769 | ret = -ENOMEM; |
| 1770 | goto err_create_singlethread_workqueue; |
| 1771 | } |
| 1772 | |
| 1773 | wake_lock_init(&(lpi->ps_wake_lock), WAKE_LOCK_SUSPEND, "proximity"); |
| 1774 | |
| 1775 | ret = isl29029_setup(lpi); |
| 1776 | if (ret < 0) { |
| 1777 | EPS("%s: isl29029_setup error!\n", __func__); |
| 1778 | goto err_isl29029_setup; |
| 1779 | } |
| 1780 | |
| 1781 | psensor_set_kvalue(lpi); |
| 1782 | |
| 1783 | lpi->isl29029_class = class_create(THIS_MODULE, "optical_sensors"); |
| 1784 | if (IS_ERR(lpi->isl29029_class)) { |
| 1785 | ret = PTR_ERR(lpi->isl29029_class); |
| 1786 | lpi->isl29029_class = NULL; |
| 1787 | goto err_create_class; |
| 1788 | } |
| 1789 | |
| 1790 | lpi->ls_dev = device_create(lpi->isl29029_class, |
| 1791 | NULL, 0, "%s", "lightsensor"); |
| 1792 | if (unlikely(IS_ERR(lpi->ls_dev))) { |
| 1793 | ret = PTR_ERR(lpi->ls_dev); |
| 1794 | lpi->ls_dev = NULL; |
| 1795 | goto err_create_ls_device; |
| 1796 | } |
| 1797 | |
| 1798 | /* register the attributes */ |
| 1799 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc); |
| 1800 | if (ret) |
| 1801 | goto err_create_ls_device_file; |
| 1802 | |
| 1803 | /* register the attributes */ |
| 1804 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_auto); |
| 1805 | if (ret) |
| 1806 | goto err_create_ls_device_file; |
| 1807 | |
| 1808 | /* register the attributes */ |
| 1809 | ret = device_create_file(lpi->ls_dev, &dev_attr_ls_kadc); |
| 1810 | if (ret) |
| 1811 | goto err_create_ls_device_file; |
| 1812 | |
| 1813 | lpi->ps_dev = device_create(lpi->isl29029_class, |
| 1814 | NULL, 0, "%s", "proximity"); |
| 1815 | if (unlikely(IS_ERR(lpi->ps_dev))) { |
| 1816 | ret = PTR_ERR(lpi->ps_dev); |
| 1817 | lpi->ps_dev = NULL; |
| 1818 | goto err_create_ls_device_file; |
| 1819 | } |
| 1820 | |
| 1821 | /* register the attributes */ |
| 1822 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_adc); |
| 1823 | if (ret) |
| 1824 | goto err_create_ps_device; |
| 1825 | |
| 1826 | /* register the attributes */ |
| 1827 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_kadc); |
| 1828 | if (ret) |
| 1829 | goto err_create_ps_device; |
| 1830 | |
| 1831 | /* register the attributes */ |
| 1832 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_led); |
| 1833 | if (ret) |
| 1834 | goto err_create_ps_device; |
| 1835 | |
| 1836 | /* register the attributes */ |
| 1837 | ret = device_create_file(lpi->ps_dev, &dev_attr_ps_test_mode); |
| 1838 | if (ret) |
| 1839 | goto err_create_ps_device; |
| 1840 | |
| 1841 | IPS("%s: Probe success! [CONFIGURE from board]\n", __func__); |
| 1842 | return ret; |
| 1843 | |
| 1844 | err_create_ps_device: |
| 1845 | device_unregister(lpi->ps_dev); |
| 1846 | err_create_ls_device_file: |
| 1847 | device_unregister(lpi->ls_dev); |
| 1848 | err_create_ls_device: |
| 1849 | class_destroy(lpi->isl29029_class); |
| 1850 | err_create_class: |
| 1851 | err_isl29029_setup: |
| 1852 | destroy_workqueue(lpi->lp_wq); |
| 1853 | err_create_singlethread_workqueue: |
| 1854 | err_lightsensor_update_table: |
| 1855 | err_psensor_setup: |
| 1856 | err_lightsensor_setup: |
| 1857 | err_platform_data_null: |
| 1858 | kfree(lpi); |
| 1859 | return ret; |
| 1860 | } |
| 1861 | |
| 1862 | static const struct i2c_device_id isl29029_i2c_id[] = { |
| 1863 | {ISL29029_I2C_NAME, 0}, |
| 1864 | {} |
| 1865 | }; |
| 1866 | |
| 1867 | static struct i2c_driver isl29029_driver = { |
| 1868 | .id_table = isl29029_i2c_id, |
| 1869 | .probe = isl29029_probe, |
| 1870 | .suspend = isl29029_suspend, |
| 1871 | .resume = isl29029_resume, |
| 1872 | .driver = { |
| 1873 | .name = ISL29029_I2C_NAME, |
| 1874 | .owner = THIS_MODULE, |
| 1875 | }, |
| 1876 | }; |
| 1877 | |
| 1878 | static int __init isl29029_init(void) |
| 1879 | { |
| 1880 | return i2c_add_driver(&isl29029_driver); |
| 1881 | } |
| 1882 | |
| 1883 | static void __exit isl29029_exit(void) |
| 1884 | { |
| 1885 | i2c_del_driver(&isl29029_driver); |
| 1886 | } |
| 1887 | |
| 1888 | module_init(isl29029_init); |
| 1889 | module_exit(isl29029_exit); |
| 1890 | |
| 1891 | MODULE_DESCRIPTION("ISL29029 Driver"); |
| 1892 | MODULE_LICENSE("GPL"); |