/drivers/input/misc/ct406.c
C | 1707 lines | 1383 code | 275 blank | 49 comment | 242 complexity | 8792f9b9f31b15bffb4049c02b20a41e MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
1/* 2 * Copyright (C) 2011-2012 Motorola Mobility, Inc. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 16 * 02111-1307, USA 17 */ 18 19#include <linux/ct406.h> 20 21#include <linux/delay.h> 22#include <linux/earlysuspend.h> 23#include <linux/i2c.h> 24#include <linux/input.h> 25#include <linux/interrupt.h> 26#include <linux/irq.h> 27#include <linux/miscdevice.h> 28#include <linux/mutex.h> 29#include <linux/platform_device.h> 30#include <linux/regulator/consumer.h> 31#include <linux/slab.h> 32#include <linux/suspend.h> 33#include <linux/types.h> 34#include <linux/uaccess.h> 35#include <linux/workqueue.h> 36 37#define CT406_I2C_RETRIES 2 38#define CT406_I2C_RETRY_DELAY 10 39 40#define CT406_COMMAND_SELECT 0x80 41#define CT406_COMMAND_AUTO_INCREMENT 0x20 42#define CT406_COMMAND_SPECIAL_FUNCTION 0x60 43#define CT406_COMMAND_PROX_INT_CLEAR 0x05 44#define CT406_COMMAND_ALS_INT_CLEAR 0x06 45 46#define CT406_ENABLE 0x00 47#define CT406_ENABLE_PIEN (1<<5) 48#define CT406_ENABLE_AIEN (1<<4) 49#define CT406_ENABLE_WEN (1<<3) 50#define CT406_ENABLE_PEN (1<<2) 51#define CT406_ENABLE_AEN (1<<1) 52#define CT406_ENABLE_PON (1<<0) 53 54#define CT406_ATIME 0x01 55#define CT406_ATIME_SATURATED 0xFF 56#define CT406_ATIME_NOT_SATURATED 0xEE 57 58#define CT406_PTIME 0x02 59 60#define CT406_WTIME 0x03 61#define CT406_WTIME_ALS_OFF 0xEE 62#define CT406_WTIME_ALS_ON 0xFF 63#define CT406_WTIME_SATURATED 0xFF 64 65#define CT406_AILTL 0x04 66#define CT406_AILTH 0x05 67#define CT406_AIHTL 0x06 68#define CT406_AIHTH 0x07 69#define CT406_PILTL 0x08 70#define CT406_PILTH 0x09 71#define CT406_PIHTL 0x0A 72#define CT406_PIHTH 0x0B 73 74#define CT406_PERS 0x0C 75#define CT406_PERS_PPERS_MASK 0xF0 76#define CT406_PERS_APERS_MASK 0x0F 77#define CT406_PERS_PPERS 0x10 78#define CT406_PERS_APERS_SATURATED 0x03 79 80#define CT406_CONFIG 0x0D 81#define CT406_CONFIG_AGL (1<<2) 82 83#define CT406_PPCOUNT 0x0E 84 85#define CT406_CONTROL 0x0F 86#define CT406_CONTROL_PDIODE_CH0 0x10 87#define CT406_CONTROL_PDIODE_CH1 0x20 88#define CT406_CONTROL_PGAIN_1X 0x00 89#define CT406_CONTROL_PGAIN_2X 0x04 90#define CT406_CONTROL_PGAIN_4X 0x08 91#define CT406_CONTROL_PGAIN_8X 0x0C 92#define CT406_CONTROL_AGAIN_1X 0x00 93#define CT406_CONTROL_AGAIN_8X 0x01 94#define CT406_CONTROL_AGAIN_16X 0x02 95#define CT406_CONTROL_AGAIN_120X 0x03 96 97#define CT406_REV_ID 0x11 98 99#define CT406_ID 0x12 100 101#define CT406_STATUS 0x13 102#define CT406_STATUS_PINT (1<<5) 103#define CT406_STATUS_AINT (1<<4) 104 105#define CT406_C0DATA 0x14 106#define CT406_C0DATAH 0x15 107#define CT406_C1DATA 0x16 108#define CT406_C1DATAH 0x17 109#define CT406_PDATA 0x18 110#define CT406_PDATAH 0x19 111#define CT406_POFFSET 0x1E 112 113#define CT406_C0DATA_MAX 0xFFFF 114#define CT405_PDATA_MAX 0x03FF 115#define CT406_PDATA_MAX 0x07FF 116 117#define CT406_PROXIMITY_NEAR 30 /* 30 mm */ 118#define CT406_PROXIMITY_FAR 1000 /* 1 meter */ 119 120#define CT406_ALS_LOW_TO_HIGH_THRESHOLD 200 /* 200 lux */ 121#define CT406_ALS_HIGH_TO_LOW_THRESHOLD 100 /* 100 lux */ 122 123#define CT40X_REV_ID_CT405 0x02 124#define CT40X_REV_ID_CT406a 0x03 125#define CT40X_REV_ID_CT406b 0x04 126 127enum ct406_prox_mode { 128 CT406_PROX_MODE_SATURATED, 129 CT406_PROX_MODE_UNCOVERED, 130 CT406_PROX_MODE_COVERED, 131}; 132 133enum ct406_als_mode { 134 CT406_ALS_MODE_SUNLIGHT, 135 CT406_ALS_MODE_LOW_LUX, 136 CT406_ALS_MODE_HIGH_LUX, 137}; 138 139enum ct40x_hardware_type { 140 CT405_HW_TYPE, 141 CT406_HW_TYPE, 142}; 143 144struct ct406_data { 145 struct input_dev *dev; 146 struct i2c_client *client; 147 struct regulator *regulator; 148 struct work_struct work; 149 struct workqueue_struct *workqueue; 150 struct ct406_platform_data *pdata; 151 struct miscdevice miscdevice; 152 struct notifier_block pm_notifier; 153 struct mutex mutex; 154 /* state flags */ 155 unsigned int suspended; 156 unsigned int regs_initialized; 157 unsigned int oscillator_enabled; 158 unsigned int prox_requested; 159 unsigned int prox_enabled; 160 enum ct406_prox_mode prox_mode; 161 unsigned int als_requested; 162 unsigned int als_enabled; 163 unsigned int als_apers; 164 unsigned int als_first_report; 165 enum ct406_als_mode als_mode; 166 unsigned int wait_enabled; 167 /* numeric values */ 168 unsigned int prox_noise_floor; 169 unsigned int prox_low_threshold; 170 unsigned int prox_high_threshold; 171 unsigned int als_low_threshold; 172 u16 prox_saturation_threshold; 173 u16 prox_covered_offset; 174 u16 prox_uncovered_offset; 175 u16 prox_recalibrate_offset; 176 u8 prox_pulse_count; 177 u8 prox_offset; 178 u16 pdata_max; 179 enum ct40x_hardware_type hw_type; 180#ifdef CONFIG_HAS_EARLYSUSPEND 181 struct early_suspend ct406_early_suspend; 182#endif 183}; 184 185static struct ct406_data *ct406_misc_data; 186 187static struct ct406_reg { 188 const char *name; 189 u8 reg; 190} ct406_regs[] = { 191 { "ENABLE", CT406_ENABLE }, 192 { "ATIME", CT406_ATIME }, 193 { "PTIME", CT406_PTIME }, 194 { "WTIME", CT406_WTIME }, 195 { "AILTL", CT406_AILTL }, 196 { "AILTH", CT406_AILTH }, 197 { "AIHTL", CT406_AIHTL }, 198 { "AIHTH", CT406_AIHTH }, 199 { "PILTL", CT406_PILTL }, 200 { "PILTH", CT406_PILTH }, 201 { "PIHTL", CT406_PIHTL }, 202 { "PIHTH", CT406_PIHTH }, 203 { "PERS", CT406_PERS }, 204 { "CONFIG", CT406_CONFIG }, 205 { "PPCOUNT", CT406_PPCOUNT }, 206 { "CONTROL", CT406_CONTROL }, 207 { "ID", CT406_ID }, 208 { "STATUS", CT406_STATUS }, 209 { "C0DATA", CT406_C0DATA }, 210 { "C0DATAH", CT406_C0DATAH }, 211 { "C1DATA", CT406_C1DATA }, 212 { "C1DATAH", CT406_C1DATAH }, 213 { "PDATA", CT406_PDATA }, 214 { "PDATAH", CT406_PDATAH }, 215 { "POFFSET", CT406_POFFSET }, 216}; 217 218#define CT406_DBG_INPUT 0x00000001 219#define CT406_DBG_POWER_ON_OFF 0x00000002 220#define CT406_DBG_ENABLE_DISABLE 0x00000004 221#define CT406_DBG_IOCTL 0x00000008 222#define CT406_DBG_SUSPEND_RESUME 0x00000010 223static u32 ct406_debug = 0x00000000; 224module_param_named(debug_mask, ct406_debug, uint, 0644); 225 226static int ct406_i2c_read(struct ct406_data *ct, u8 *buf, int len) 227{ 228 int err; 229 int tries = 0; 230 struct i2c_msg msgs[] = { 231 { 232 .addr = ct->client->addr, 233 .flags = ct->client->flags & I2C_M_TEN, 234 .len = 1, 235 .buf = buf, 236 }, 237 { 238 .addr = ct->client->addr, 239 .flags = (ct->client->flags & I2C_M_TEN) | I2C_M_RD, 240 .len = len, 241 .buf = buf, 242 }, 243 }; 244 245 buf[0] |= CT406_COMMAND_SELECT; 246 do { 247 err = i2c_transfer(ct->client->adapter, msgs, 2); 248 if (err != 2) 249 msleep_interruptible(CT406_I2C_RETRY_DELAY); 250 } while ((err != 2) && (++tries < CT406_I2C_RETRIES)); 251 252 if (err != 2) { 253 pr_err("%s: read transfer error.\n", __func__); 254 dev_err(&ct->client->dev, "read transfer error\n"); 255 err = -EIO; 256 } else { 257 err = 0; 258 } 259 260 return err; 261} 262 263static int ct406_i2c_write(struct ct406_data *ct, u8 *buf, int len) 264{ 265 int err; 266 int tries = 0; 267 struct i2c_msg msgs[] = { 268 { 269 .addr = ct->client->addr, 270 .flags = ct->client->flags & I2C_M_TEN, 271 .len = len + 1, 272 .buf = buf, 273 }, 274 }; 275 276 buf[0] |= CT406_COMMAND_SELECT; 277 do { 278 err = i2c_transfer(ct->client->adapter, msgs, 1); 279 if (err != 1) 280 msleep_interruptible(CT406_I2C_RETRY_DELAY); 281 } while ((err != 1) && (++tries < CT406_I2C_RETRIES)); 282 283 if (err != 1) { 284 pr_err("%s: write transfer error.\n", __func__); 285 dev_err(&ct->client->dev, "write transfer error\n"); 286 err = -EIO; 287 } else { 288 err = 0; 289 } 290 291 return err; 292} 293 294static int ct406_write_enable(struct ct406_data *ct) 295{ 296 int error = 0; 297 u8 reg_data[2] = {0x00, 0x00}; 298 reg_data[0] = CT406_ENABLE; 299 if (ct->oscillator_enabled || ct->als_enabled || ct->prox_enabled) { 300 reg_data[1] |= CT406_ENABLE_PON; 301 if (!ct->oscillator_enabled) { 302 error = ct406_i2c_write(ct, reg_data, 1); 303 if (error < 0) 304 return error; 305 msleep(3); 306 ct->oscillator_enabled = 1; 307 } 308 if (ct->als_enabled) 309 reg_data[1] |= CT406_ENABLE_AEN | CT406_ENABLE_AIEN; 310 if (ct->prox_enabled) 311 reg_data[1] |= CT406_ENABLE_PEN | CT406_ENABLE_PIEN; 312 if (ct->wait_enabled) 313 reg_data[1] |= CT406_ENABLE_WEN; 314 } 315 if (ct406_debug & CT406_DBG_ENABLE_DISABLE) 316 pr_info("%s: writing ENABLE=0x%02x\n", __func__, reg_data[1]); 317 318 return ct406_i2c_write(ct, reg_data, 1); 319} 320 321static int ct406_set_als_enable(struct ct406_data *ct, 322 unsigned int enable) 323{ 324 int error = 0; 325 if (ct->als_enabled != enable) { 326 ct->als_enabled = enable; 327 if (ct->regs_initialized) 328 error = ct406_write_enable(ct); 329 } 330 return error; 331} 332 333static int ct406_set_prox_enable(struct ct406_data *ct, 334 unsigned int enable) 335{ 336 int error = 0; 337 if (ct->prox_enabled != enable) { 338 ct->prox_enabled = enable; 339 if (ct->regs_initialized) 340 error = ct406_write_enable(ct); 341 } 342 return error; 343} 344 345static int ct406_set_wait_enable(struct ct406_data *ct, 346 unsigned int enable) 347{ 348 int error = 0; 349 if (ct->wait_enabled != enable) { 350 ct->wait_enabled = enable; 351 if (ct->regs_initialized) 352 error = ct406_write_enable(ct); 353 } 354 return error; 355} 356 357static int ct406_clear_als_flag(struct ct406_data *ct) 358{ 359 u8 reg_data[1] = {0}; 360 reg_data[0] = CT406_COMMAND_SPECIAL_FUNCTION 361 | CT406_COMMAND_ALS_INT_CLEAR; 362 return ct406_i2c_write(ct, reg_data, 0); 363} 364 365static int ct406_clear_prox_flag(struct ct406_data *ct) 366{ 367 u8 reg_data[1] = {0}; 368 reg_data[0] = CT406_COMMAND_SPECIAL_FUNCTION 369 | CT406_COMMAND_PROX_INT_CLEAR; 370 return ct406_i2c_write(ct, reg_data, 0); 371} 372 373static int ct406_init_registers(struct ct406_data *ct) 374{ 375 int error = 0; 376 u8 reg_data[3] = {0}; 377 378 /* write ALS integration time = ~49 ms */ 379 /* write prox integration time = ~3 ms */ 380 reg_data[0] = (CT406_ATIME | CT406_COMMAND_AUTO_INCREMENT); 381 reg_data[1] = CT406_ATIME_NOT_SATURATED; 382 if (ct->hw_type == CT405_HW_TYPE) 383 reg_data[2] = 0xFF; /* 2.73 ms */ 384 else 385 reg_data[2] = 0xFE; /* 5.46 ms */ 386 error = ct406_i2c_write(ct, reg_data, 2); 387 if (error < 0) 388 return error; 389 390 /* write IR LED pulse count */ 391 reg_data[0] = (CT406_PPCOUNT | CT406_COMMAND_AUTO_INCREMENT); 392 reg_data[1] = ct->prox_pulse_count; 393 error = ct406_i2c_write(ct, reg_data, 1); 394 if (error < 0) 395 return error; 396 397 /* write proximity diode = ch1, proximity gain = 1/2, ALS gain = 1 */ 398 reg_data[0] = (CT406_CONTROL | CT406_COMMAND_AUTO_INCREMENT); 399 if (ct->hw_type == CT405_HW_TYPE) 400 reg_data[1] = CT406_CONTROL_PDIODE_CH1 401 | CT406_CONTROL_PGAIN_2X | CT406_CONTROL_AGAIN_1X; 402 else 403 reg_data[1] = CT406_CONTROL_PDIODE_CH1 404 | CT406_CONTROL_PGAIN_1X | CT406_CONTROL_AGAIN_1X; 405 error = ct406_i2c_write(ct, reg_data, 1); 406 if (error < 0) 407 return error; 408 409 /* write proximity offset */ 410 reg_data[0] = (CT406_POFFSET | CT406_COMMAND_AUTO_INCREMENT); 411 reg_data[1] = ct->prox_offset; 412 error = ct406_i2c_write(ct, reg_data, 1); 413 if (error < 0) 414 return error; 415 416 return 0; 417} 418 419static void ct406_write_als_thresholds(struct ct406_data *ct) 420{ 421 u8 reg_data[5] = {0}; 422 unsigned int ailt = ct->als_low_threshold; 423 unsigned int aiht = CT406_C0DATA_MAX; 424 int error; 425 426 reg_data[0] = (CT406_AILTL | CT406_COMMAND_AUTO_INCREMENT); 427 reg_data[1] = (ailt & 0xFF); 428 reg_data[2] = ((ailt >> 8) & 0xFF); 429 reg_data[3] = (aiht & 0xFF); 430 reg_data[4] = ((aiht >> 8) & 0xFF); 431 432 error = ct406_i2c_write(ct, reg_data, 4); 433 if (error < 0) 434 pr_err("%s: Error writing new ALS thresholds: %d\n", 435 __func__, error); 436} 437 438static void ct406_als_mode_sunlight(struct ct406_data *ct) 439{ 440 int error; 441 u8 reg_data[2] = {0}; 442 443 /* set AGL to reduce ALS gain by 1/6 */ 444 reg_data[0] = CT406_CONFIG; 445 reg_data[1] = CT406_CONFIG_AGL; 446 error = ct406_i2c_write(ct, reg_data, 1); 447 if (error < 0) { 448 pr_err("%s: error writing CONFIG: %d\n", __func__, error); 449 return; 450 } 451 452 /* write ALS gain = 1 */ 453 reg_data[0] = CT406_CONTROL; 454 if (ct->hw_type == CT405_HW_TYPE) 455 reg_data[1] = CT406_CONTROL_PDIODE_CH1 456 | CT406_CONTROL_PGAIN_2X | CT406_CONTROL_AGAIN_1X; 457 else 458 reg_data[1] = CT406_CONTROL_PDIODE_CH1 459 | CT406_CONTROL_PGAIN_1X | CT406_CONTROL_AGAIN_1X; 460 error = ct406_i2c_write(ct, reg_data, 1); 461 if (error < 0) { 462 pr_err("%s: error writing ALS gain: %d\n", __func__, error); 463 return; 464 } 465 466 /* write ALS integration time = ~3 ms */ 467 reg_data[0] = CT406_ATIME; 468 reg_data[1] = CT406_ATIME_SATURATED; 469 error = ct406_i2c_write(ct, reg_data, 1); 470 if (error < 0) { 471 pr_err("%s: error writing ATIME: %d\n", __func__, error); 472 return; 473 } 474 475 ct->als_mode = CT406_ALS_MODE_SUNLIGHT; 476 ct->als_low_threshold = ct->prox_saturation_threshold; 477 ct406_write_als_thresholds(ct); 478} 479 480static void ct406_als_mode_low_lux(struct ct406_data *ct) 481{ 482 int error; 483 u8 reg_data[2] = {0}; 484 485 /* clear AGL for regular ALS gain behavior */ 486 reg_data[0] = CT406_CONFIG; 487 reg_data[1] = 0; 488 error = ct406_i2c_write(ct, reg_data, 1); 489 if (error < 0) { 490 pr_err("%s: error writing CONFIG: %d\n", __func__, error); 491 return; 492 } 493 494 /* write ALS gain = 8 */ 495 reg_data[0] = CT406_CONTROL; 496 if (ct->hw_type == CT405_HW_TYPE) 497 reg_data[1] = CT406_CONTROL_PDIODE_CH1 498 | CT406_CONTROL_PGAIN_2X | CT406_CONTROL_AGAIN_8X; 499 else 500 reg_data[1] = CT406_CONTROL_PDIODE_CH1 501 | CT406_CONTROL_PGAIN_1X | CT406_CONTROL_AGAIN_8X; 502 error = ct406_i2c_write(ct, reg_data, 1); 503 if (error < 0) { 504 pr_err("%s: error writing ALS gain: %d\n", __func__, error); 505 return; 506 } 507 508 /* write ALS integration time = ~49 ms */ 509 reg_data[0] = CT406_ATIME; 510 reg_data[1] = CT406_ATIME_NOT_SATURATED; 511 error = ct406_i2c_write(ct, reg_data, 1); 512 if (error < 0) { 513 pr_err("%s: error writing ATIME: %d\n", __func__, error); 514 return; 515 } 516 517 ct->als_mode = CT406_ALS_MODE_LOW_LUX; 518 ct->als_low_threshold = CT406_C0DATA_MAX - 1; 519 ct406_write_als_thresholds(ct); 520} 521 522static void ct406_als_mode_high_lux(struct ct406_data *ct) 523{ 524 int error; 525 u8 reg_data[2] = {0}; 526 527 /* clear AGL for regular ALS gain behavior */ 528 reg_data[0] = CT406_CONFIG; 529 reg_data[1] = 0; 530 error = ct406_i2c_write(ct, reg_data, 1); 531 if (error < 0) { 532 pr_err("%s: error writing CONFIG: %d\n", __func__, error); 533 return; 534 } 535 536 /* write ALS gain = 1 */ 537 reg_data[0] = CT406_CONTROL; 538 if (ct->hw_type == CT405_HW_TYPE) 539 reg_data[1] = CT406_CONTROL_PDIODE_CH1 540 | CT406_CONTROL_PGAIN_2X | CT406_CONTROL_AGAIN_1X; 541 else 542 reg_data[1] = CT406_CONTROL_PDIODE_CH1 543 | CT406_CONTROL_PGAIN_1X | CT406_CONTROL_AGAIN_1X; 544 error = ct406_i2c_write(ct, reg_data, 1); 545 if (error < 0) { 546 pr_err("%s: error writing ALS gain: %d\n", __func__, error); 547 return; 548 } 549 550 /* write ALS integration time = ~49 ms */ 551 reg_data[0] = CT406_ATIME; 552 reg_data[1] = CT406_ATIME_NOT_SATURATED; 553 error = ct406_i2c_write(ct, reg_data, 1); 554 if (error < 0) { 555 pr_err("%s: error writing ATIME: %d\n", __func__, error); 556 return; 557 } 558 559 ct->als_mode = CT406_ALS_MODE_HIGH_LUX; 560 ct->als_low_threshold = CT406_C0DATA_MAX - 1; 561 ct406_write_als_thresholds(ct); 562} 563 564static void ct406_write_prox_thresholds(struct ct406_data *ct) 565{ 566 u8 reg_data[5] = {0}; 567 unsigned int pilt = ct->prox_low_threshold; 568 unsigned int piht = ct->prox_high_threshold; 569 int error; 570 571 reg_data[0] = (CT406_PILTL | CT406_COMMAND_AUTO_INCREMENT); 572 reg_data[1] = (pilt & 0xFF); 573 reg_data[2] = ((pilt >> 8) & 0xFF); 574 reg_data[3] = (piht & 0xFF); 575 reg_data[4] = ((piht >> 8) & 0xFF); 576 577 error = ct406_i2c_write(ct, reg_data, 4); 578 if (error < 0) 579 pr_err("%s: Error writing new prox thresholds: %d\n", 580 __func__, error); 581} 582 583static void ct406_prox_mode_saturated(struct ct406_data *ct) 584{ 585 ct->prox_mode = CT406_PROX_MODE_SATURATED; 586 pr_info("%s: Prox mode saturated\n", __func__); 587} 588 589static void ct406_prox_mode_uncovered(struct ct406_data *ct) 590{ 591 unsigned int noise_floor = ct->prox_noise_floor; 592 unsigned int pilt = noise_floor - ct->prox_recalibrate_offset; 593 unsigned int piht = noise_floor + ct->prox_covered_offset; 594 595 if (pilt > ct->pdata_max) 596 pilt = 0; 597 if (piht > ct->pdata_max) 598 piht = ct->pdata_max; 599 600 ct->prox_mode = CT406_PROX_MODE_UNCOVERED; 601 ct->prox_low_threshold = pilt; 602 ct->prox_high_threshold = piht; 603 ct406_write_prox_thresholds(ct); 604 pr_info("%s: Prox mode uncovered\n", __func__); 605} 606 607static void ct406_prox_mode_covered(struct ct406_data *ct) 608{ 609 unsigned int noise_floor = ct->prox_noise_floor; 610 unsigned int pilt = noise_floor + ct->prox_uncovered_offset; 611 unsigned int piht = ct->pdata_max; 612 613 if (pilt > ct->pdata_max) 614 pilt = ct->pdata_max; 615 616 ct->prox_mode = CT406_PROX_MODE_COVERED; 617 ct->prox_low_threshold = pilt; 618 ct->prox_high_threshold = piht; 619 ct406_write_prox_thresholds(ct); 620 pr_info("%s: Prox mode covered\n", __func__); 621} 622 623static void ct406_device_power_off(struct ct406_data *ct) 624{ 625 int error; 626 627 if (ct406_debug & CT406_DBG_POWER_ON_OFF) 628 pr_info("%s: initialized=%d\n", __func__, ct->regs_initialized); 629 630 if (ct->regs_initialized) { 631 disable_irq_nosync(ct->client->irq); 632 ct->oscillator_enabled = 0; 633 error = ct406_write_enable(ct); 634 if (error) { 635 pr_err("%s: ct406_disable failed: %d\n", 636 __func__, error); 637 } 638 ct->regs_initialized = 0; 639 } 640 641 if (ct->regulator) { 642 error = regulator_disable(ct->regulator); 643 if (error) { 644 pr_err("%s: regulator_disable failed: %d\n", 645 __func__, error); 646 } 647 } 648 649} 650 651static int ct406_device_power_on(struct ct406_data *ct) 652{ 653 int error; 654 655 if (ct406_debug & CT406_DBG_POWER_ON_OFF) 656 pr_info("%s: initialized=%d\n", __func__, ct->regs_initialized); 657 658 if (ct->regulator) { 659 error = regulator_enable(ct->regulator); 660 if (error) { 661 pr_err("%s: regulator_enable failed: %d\n", 662 __func__, error); 663 return error; 664 } 665 } 666 return 0; 667} 668 669static int ct406_device_init(struct ct406_data *ct) 670{ 671 int error; 672 673 if (!ct->regs_initialized) { 674 error = ct406_init_registers(ct); 675 if (error < 0) { 676 pr_err("%s: init_registers failed: %d\n", 677 __func__, error); 678 if (ct->regulator) 679 regulator_disable(ct->regulator); 680 return error; 681 } 682 ct->oscillator_enabled = 0; 683 ct->prox_enabled = 0; 684 ct->prox_mode = CT406_PROX_MODE_SATURATED; 685 ct->als_enabled = 0; 686 ct->als_mode = CT406_ALS_MODE_LOW_LUX; 687 688 enable_irq(ct->client->irq); 689 690 ct->regs_initialized = 1; 691 } 692 693 return 0; 694} 695 696static void ct406_check_als_range(struct ct406_data *ct, unsigned int lux) 697{ 698 if (ct->als_mode == CT406_ALS_MODE_LOW_LUX) { 699 if (lux >= CT406_ALS_LOW_TO_HIGH_THRESHOLD) 700 ct406_als_mode_high_lux(ct); 701 } else if (ct->als_mode == CT406_ALS_MODE_HIGH_LUX) { 702 if (lux < CT406_ALS_HIGH_TO_LOW_THRESHOLD) 703 ct406_als_mode_low_lux(ct); 704 } 705} 706 707static int ct406_enable_als(struct ct406_data *ct) 708{ 709 int error; 710 u8 reg_data[5] = {0}; 711 712 if (!ct->suspended && ct->als_mode != CT406_ALS_MODE_SUNLIGHT) { 713 error = ct406_set_als_enable(ct, 0); 714 if (error) { 715 pr_err("%s: Unable to turn off ALS: %d\n", 716 __func__, error); 717 return error; 718 } 719 720 ct406_als_mode_low_lux(ct); 721 722 /* write wait time = ALS on value */ 723 reg_data[0] = CT406_WTIME; 724 reg_data[1] = CT406_WTIME_ALS_ON; 725 error = ct406_i2c_write(ct, reg_data, 1); 726 if (error < 0) { 727 pr_err("%s: Error %d\n", __func__, error); 728 return error; 729 } 730 error = ct406_set_wait_enable(ct, 0); 731 if (error) { 732 pr_err("%s: Unable to set wait enable: %d\n", 733 __func__, error); 734 return error; 735 } 736 737 /* write ALS interrupt persistence */ 738 reg_data[0] = CT406_PERS; 739 reg_data[1] = CT406_PERS_PPERS; 740 error = ct406_i2c_write(ct, reg_data, 1); 741 if (error < 0) { 742 pr_err("%s: Error %d\n", __func__, error); 743 return error; 744 } 745 ct->als_first_report = 0; 746 747 ct406_clear_als_flag(ct); 748 749 error = ct406_set_als_enable(ct, 1); 750 if (error) { 751 pr_err("%s: Unable to turn on ALS: %d\n", 752 __func__, error); 753 return error; 754 } 755 } 756 757 return 0; 758} 759 760static int ct406_disable_als(struct ct406_data *ct) 761{ 762 int error; 763 u8 reg_data[2] = {0}; 764 765 if (ct->als_enabled && ct->als_mode != CT406_ALS_MODE_SUNLIGHT) { 766 ct406_set_als_enable(ct, 0); 767 768 /* write wait time = ALS off value */ 769 reg_data[0] = CT406_WTIME; 770 reg_data[1] = CT406_WTIME_ALS_OFF; 771 error = ct406_i2c_write(ct, reg_data, 1); 772 if (error < 0) { 773 pr_err("%s: Error %d\n", __func__, error); 774 return error; 775 } 776 error = ct406_set_wait_enable(ct, 1); 777 if (error) { 778 pr_err("%s: Unable to set wait enable: %d\n", 779 __func__, error); 780 return error; 781 } 782 783 ct406_clear_als_flag(ct); 784 } 785 786 return 0; 787} 788 789static void ct406_measure_noise_floor(struct ct406_data *ct) 790{ 791 int error = -EINVAL; 792 unsigned int num_samples = ct->pdata->prox_samples_for_noise_floor; 793 unsigned int i, sum = 0, avg = 0; 794 unsigned int max = ct->pdata_max - 1 - ct->prox_covered_offset; 795 u8 reg_data[2] = {0}; 796 797 /* disable ALS temporarily */ 798 error = ct406_set_als_enable(ct, 0); 799 if (error < 0) 800 pr_err("%s: error disabling ALS: %d\n", 801 __func__, error); 802 803 /* clear AGL for regular ALS gain behavior */ 804 reg_data[0] = CT406_CONFIG; 805 reg_data[1] = 0; 806 error = ct406_i2c_write(ct, reg_data, 1); 807 if (error < 0) 808 pr_err("%s: error writing CONFIG: %d\n", 809 __func__, error); 810 811 /* write wait time = ALS off value */ 812 reg_data[0] = CT406_WTIME; 813 reg_data[1] = CT406_WTIME_ALS_OFF; 814 error = ct406_i2c_write(ct, reg_data, 1); 815 if (error < 0) 816 pr_err("%s: Error writing WTIME: %d\n", 817 __func__, error); 818 error = ct406_set_wait_enable(ct, 1); 819 if (error) { 820 pr_err("%s: Unable to set wait enable: %d\n", 821 __func__, error); 822 } 823 824 for (i = 0; i < num_samples; i++) { 825 /* enable prox sensor and wait */ 826 error = ct406_set_prox_enable(ct, 1); 827 if (error) { 828 pr_err("%s: Error enabling proximity sensor: %d\n", 829 __func__, error); 830 break; 831 } 832 msleep(5); 833 834 reg_data[0] = (CT406_PDATA | CT406_COMMAND_AUTO_INCREMENT); 835 error = ct406_i2c_read(ct, reg_data, 2); 836 if (error) { 837 pr_err("%s: Error reading prox data: %d\n", 838 __func__, error); 839 break; 840 } 841 sum += (reg_data[1] << 8) | reg_data[0]; 842 843 /* disable prox sensor */ 844 error = ct406_set_prox_enable(ct, 0); 845 if (error) { 846 pr_err("%s: Error disabling proximity sensor: %d\n", 847 __func__, error); 848 break; 849 } 850 } 851 852 if (!error) 853 avg = sum / num_samples; 854 855 if (avg < max) 856 ct->prox_noise_floor = avg; 857 else 858 ct->prox_noise_floor = max; 859 860 if (ct406_debug & CT406_DBG_ENABLE_DISABLE) 861 pr_info("%s: Noise floor is 0x%x\n", __func__, 862 ct->prox_noise_floor); 863 864 ct406_prox_mode_uncovered(ct); 865 866 error = ct406_set_prox_enable(ct, 1); 867 if (error) 868 pr_err("%s: Error enabling proximity sensor: %d\n", 869 __func__, error); 870 871 /* re-enable ALS if necessary */ 872 ct406_als_mode_low_lux(ct); 873 if (ct->als_requested && !ct->suspended) 874 ct406_enable_als(ct); 875} 876 877static int ct406_check_saturation(struct ct406_data *ct) 878{ 879 int error = 0; 880 u8 reg_data[2] = {0}; 881 unsigned int c0data; 882 883 /* disable prox */ 884 ct406_set_prox_enable(ct, 0); 885 ct->prox_low_threshold = 0; 886 ct->prox_high_threshold = ct->pdata_max; 887 ct406_write_prox_thresholds(ct); 888 889 ct406_als_mode_sunlight(ct); 890 ct406_set_als_enable(ct, 1); 891 892 /* write ALS interrupt persistence = saturated value */ 893 reg_data[0] = CT406_PERS; 894 reg_data[1] = CT406_PERS_PPERS | CT406_PERS_APERS_SATURATED; 895 error = ct406_i2c_write(ct, reg_data, 1); 896 if (error < 0) 897 return 0; 898 899 /* write wait time = saturated value */ 900 reg_data[0] = CT406_WTIME; 901 reg_data[1] = CT406_WTIME_SATURATED; 902 error = ct406_i2c_write(ct, reg_data, 1); 903 if (error < 0) 904 return 0; 905 error = ct406_set_wait_enable(ct, 1); 906 if (error) { 907 pr_err("%s: Unable to set wait enable: %d\n", 908 __func__, error); 909 return error; 910 } 911 912 msleep(5); 913 914 /* read C0DATA */ 915 reg_data[0] = (CT406_C0DATA | CT406_COMMAND_AUTO_INCREMENT); 916 error = ct406_i2c_read(ct, reg_data, 2); 917 if (error < 0) 918 return 0; 919 c0data = (reg_data[1] << 8) | reg_data[0]; 920 921 return (c0data > ct->als_low_threshold); 922} 923 924static int ct406_enable_prox(struct ct406_data *ct) 925{ 926 int error; 927 928 if (ct->suspended) { 929 if (ct406_debug & CT406_DBG_ENABLE_DISABLE) 930 pr_info("%s: Powering on\n", __func__); 931 error = ct406_device_power_on(ct); 932 if (error) 933 return error; 934 error = ct406_device_init(ct); 935 if (error) 936 return error; 937 } 938 939 if (ct406_check_saturation(ct)) 940 ct406_prox_mode_saturated(ct); 941 else 942 ct406_measure_noise_floor(ct); 943 944 return 0; 945} 946 947static int ct406_disable_prox(struct ct406_data *ct) 948{ 949 if (ct->prox_enabled) { 950 ct406_set_prox_enable(ct, 0); 951 ct406_clear_prox_flag(ct); 952 ct406_prox_mode_saturated(ct); 953 954 if (ct->als_requested) { 955 if (ct->als_mode == CT406_ALS_MODE_SUNLIGHT) { 956 ct406_als_mode_low_lux(ct); 957 if (!ct->suspended) 958 ct406_enable_als(ct); 959 } 960 } 961 } 962 963 if (ct->suspended && ct->regs_initialized) { 964 if (ct406_debug & CT406_DBG_ENABLE_DISABLE) 965 pr_info("%s: Powering off\n", __func__); 966 ct406_device_power_off(ct); 967 } 968 969 return 0; 970} 971 972 973static void ct406_report_prox(struct ct406_data *ct) 974{ 975 int error = 0; 976 u8 reg_data[2] = {0}; 977 unsigned int pdata = 0; 978 979 reg_data[0] = (CT406_PDATA | CT406_COMMAND_AUTO_INCREMENT); 980 error = ct406_i2c_read(ct, reg_data, 2); 981 if (error < 0) 982 return; 983 984 pdata = (reg_data[1] << 8) | reg_data[0]; 985 if (ct406_debug & CT406_DBG_INPUT) 986 pr_info("%s: PDATA = %d\n", __func__, pdata); 987 988 switch (ct->prox_mode) { 989 case CT406_PROX_MODE_SATURATED: 990 pr_warn("%s: prox interrupted in saturated mode!\n", __func__); 991 break; 992 case CT406_PROX_MODE_UNCOVERED: 993 if (pdata < ct->prox_low_threshold) 994 ct406_enable_prox(ct); 995 if (pdata > ct->prox_high_threshold) { 996 input_event(ct->dev, EV_MSC, MSC_RAW, 997 CT406_PROXIMITY_NEAR); 998 input_sync(ct->dev); 999 ct406_prox_mode_covered(ct); 1000 } 1001 break; 1002 case CT406_PROX_MODE_COVERED: 1003 if (pdata < ct->prox_low_threshold) { 1004 input_event(ct->dev, EV_MSC, MSC_RAW, 1005 CT406_PROXIMITY_FAR); 1006 input_sync(ct->dev); 1007 ct406_prox_mode_uncovered(ct); 1008 } 1009 break; 1010 default: 1011 pr_err("%s: prox mode is %d!\n", __func__, ct->prox_mode); 1012 } 1013 1014 ct406_clear_prox_flag(ct); 1015} 1016 1017static void ct406_report_als(struct ct406_data *ct) 1018{ 1019 int error; 1020 u8 reg_data[4] = {0}; 1021 unsigned int c0data; 1022 unsigned int c1data; 1023 unsigned int ratio; 1024 unsigned int lux = 0; 1025 1026 reg_data[0] = (CT406_C0DATA | CT406_COMMAND_AUTO_INCREMENT); 1027 error = ct406_i2c_read(ct, reg_data, 4); 1028 if (error < 0) 1029 return; 1030 1031 c0data = (reg_data[1] << 8) | reg_data[0]; 1032 c1data = (reg_data[3] << 8) | reg_data[2]; 1033 if (ct406_debug & CT406_DBG_INPUT) 1034 pr_info("%s: C0DATA = %d, C1DATA = %d\n", 1035 __func__, c0data, c1data); 1036 1037 /* calculate lux using piecewise function from TAOS */ 1038 if (c0data == 0) 1039 c0data = 1; 1040 ratio = ((100 * c1data) + c0data - 1) / c0data; 1041 switch (ct->als_mode) { 1042 case CT406_ALS_MODE_SUNLIGHT: 1043 if (c0data == 0x0400 || c1data == 0x0400) 1044 lux = 0xFFFF; 1045 else if (ratio <= 51) 1046 lux = (1041*c0data - 1963*c1data); 1047 else if (ratio <= 58) 1048 lux = (342*c0data - 587*c1data); 1049 else 1050 lux = 0; 1051 break; 1052 case CT406_ALS_MODE_LOW_LUX: 1053 if (c0data == 0x4800 || c1data == 0x4800) 1054 lux = CT406_ALS_LOW_TO_HIGH_THRESHOLD; 1055 else if (ratio <= 51) 1056 lux = (121*c0data - 227*c1data) / 100; 1057 else if (ratio <= 58) 1058 lux = (40*c0data - 68*c1data) / 100; 1059 else 1060 lux = 0; 1061 break; 1062 case CT406_ALS_MODE_HIGH_LUX: 1063 if (c0data == 0x4800 || c1data == 0x4800) 1064 lux = 0xFFFF; 1065 else if (ratio <= 51) 1066 lux = (964*c0data - 1818*c1data) / 100; 1067 else if (ratio <= 58) 1068 lux = (317*c0data - 544*c1data) / 100; 1069 else 1070 lux = 0; 1071 break; 1072 default: 1073 pr_err("%s: ALS mode is %d!\n", __func__, ct->als_mode); 1074 } 1075 1076 /* input.c filters consecutive LED_MISC values <=1. */ 1077 lux = (lux >= 2) ? (lux * 2) : 2; 1078 1079 if( lux > 0xFFFF ) 1080 lux = 0xFFFF; 1081 1082 input_event(ct->dev, EV_LED, LED_MISC, lux); 1083 input_sync(ct->dev); 1084 1085 if (ct->als_first_report == 0) { 1086 /* write ALS interrupt persistence */ 1087 reg_data[0] = CT406_PERS; 1088 reg_data[1] = CT406_PERS_PPERS | ct->als_apers; 1089 error = ct406_i2c_write(ct, reg_data, 1); 1090 if (error < 0) { 1091 pr_err("%s: Error %d\n", __func__, error); 1092 } 1093 ct->als_first_report = 1; 1094 } 1095 1096 if (ct->als_mode != CT406_ALS_MODE_SUNLIGHT) 1097 ct406_check_als_range(ct, lux); 1098 1099 ct406_clear_als_flag(ct); 1100 1101 if (ct->als_mode == CT406_ALS_MODE_SUNLIGHT) 1102 ct406_enable_prox(ct); /* re-check saturation */ 1103} 1104 1105static int ct406_misc_open(struct inode *inode, struct file *file) 1106{ 1107 int err; 1108 1109 err = nonseekable_open(inode, file); 1110 if (err < 0) 1111 return err; 1112 1113 file->private_data = ct406_misc_data; 1114 1115 return 0; 1116} 1117 1118static int ct406_als_enable_param; 1119 1120static int ct406_set_als_enable_param(const char *char_value, 1121 struct kernel_param *kp) 1122{ 1123 unsigned long int_value; 1124 int ret; 1125 1126 if (!char_value) 1127 return -EINVAL; 1128 1129 if (!ct406_misc_data) 1130 return -EINVAL; 1131 1132 ret = strict_strtoul(char_value, (unsigned int)0, &int_value); 1133 if (ret) 1134 return ret; 1135 1136 *((int *)kp->arg) = int_value; 1137 1138 mutex_lock(&ct406_misc_data->mutex); 1139 1140 ct406_misc_data->als_requested = (int_value != 0); 1141 if (ct406_debug & CT406_DBG_INPUT) 1142 pr_info("%s: als enable = %d\n", __func__, 1143 ct406_misc_data->als_requested); 1144 if (ct406_misc_data->als_requested) 1145 ret = ct406_enable_als(ct406_misc_data); 1146 else 1147 ret = ct406_disable_als(ct406_misc_data); 1148 1149 mutex_unlock(&ct406_misc_data->mutex); 1150 1151 return ret; 1152} 1153 1154static int ct406_get_als_enable_param(char *buffer, struct kernel_param *kp) 1155{ 1156 int num_chars; 1157 1158 if (!buffer) 1159 return -EINVAL; 1160 1161 if (!ct406_misc_data) { 1162 scnprintf(buffer, PAGE_SIZE, "0\n"); 1163 return 1; 1164 } 1165 1166 num_chars = scnprintf(buffer, 2, "%d\n", 1167 ct406_misc_data->als_requested); 1168 1169 return num_chars; 1170} 1171 1172module_param_call(als_enable, ct406_set_als_enable_param, ct406_get_als_enable_param, 1173 &ct406_als_enable_param, 0644); 1174MODULE_PARM_DESC(als_enable, "Enable/disable the ALS."); 1175 1176static int ct406_prox_enable_param; 1177 1178static int ct406_set_prox_enable_param(const char *char_value, 1179 struct kernel_param *kp) 1180{ 1181 unsigned long int_value; 1182 int ret; 1183 1184 if (!char_value) 1185 return -EINVAL; 1186 1187 if (!ct406_misc_data) 1188 return -EINVAL; 1189 1190 ret = strict_strtoul(char_value, (unsigned int)0, &int_value); 1191 if (ret) 1192 return ret; 1193 1194 *((int *)kp->arg) = int_value; 1195 1196 mutex_lock(&ct406_misc_data->mutex); 1197 1198 ct406_misc_data->prox_requested = (int_value != 0); 1199 if (ct406_debug & CT406_DBG_INPUT) 1200 pr_info("%s: prox enable = %d\n", __func__, 1201 ct406_misc_data->prox_requested); 1202 if (ct406_misc_data->prox_requested) 1203 ret = ct406_enable_prox(ct406_misc_data); 1204 else 1205 ret = ct406_disable_prox(ct406_misc_data); 1206 1207 mutex_unlock(&ct406_misc_data->mutex); 1208 1209 return ret; 1210} 1211 1212static int ct406_get_prox_enable_param(char *buffer, struct kernel_param *kp) 1213{ 1214 int num_chars; 1215 1216 if (!buffer) 1217 return -EINVAL; 1218 1219 if (!ct406_misc_data) { 1220 scnprintf(buffer, PAGE_SIZE, "0\n"); 1221 return 1; 1222 } 1223 1224 num_chars = scnprintf(buffer, 2, "%d\n", 1225 ct406_misc_data->prox_requested); 1226 1227 return num_chars; 1228} 1229 1230module_param_call(prox_enable, ct406_set_prox_enable_param, ct406_get_prox_enable_param, 1231 &ct406_prox_enable_param, 0644); 1232MODULE_PARM_DESC(prox_enable, "Enable/disable the Prox."); 1233 1234static int ct406_als_delay_param; 1235 1236static int ct406_set_als_delay_param(const char *char_value, 1237 struct kernel_param *kp) 1238{ 1239 unsigned long int_value; 1240 int ret; 1241 1242 if (!char_value) 1243 return -EINVAL; 1244 1245 if (!ct406_misc_data) 1246 return -EINVAL; 1247 1248 ret = strict_strtoul(char_value, (unsigned int)0, &int_value); 1249 if (ret) 1250 return ret; 1251 1252 *((int *)kp->arg) = int_value; 1253 1254 mutex_lock(&ct406_misc_data->mutex); 1255 1256 ct406_als_delay_param = int_value; 1257 if (ct406_debug & CT406_DBG_INPUT) 1258 pr_info("%s: delay = %d\n", __func__, ct406_als_delay_param); 1259 1260 if (ct406_als_delay_param > 982) 1261 ct406_misc_data->als_apers = 0x07; 1262 else if (ct406_als_delay_param > 737) 1263 ct406_misc_data->als_apers = 0x06; 1264 else if (ct406_als_delay_param > 491) 1265 ct406_misc_data->als_apers = 0x05; 1266 else if (ct406_als_delay_param > 245) 1267 ct406_misc_data->als_apers = 0x04; 1268 else if (ct406_als_delay_param > 147) 1269 ct406_misc_data->als_apers = 0x03; 1270 else if (ct406_als_delay_param > 98) 1271 ct406_misc_data->als_apers = 0x02; 1272 else 1273 ct406_misc_data->als_apers = 0x01; 1274 1275 if (ct406_misc_data->als_enabled 1276 && (ct406_misc_data->als_mode != CT406_ALS_MODE_SUNLIGHT)) { 1277 ct406_disable_als(ct406_misc_data); 1278 ct406_enable_als(ct406_misc_data); 1279 } 1280 1281 mutex_unlock(&ct406_misc_data->mutex); 1282 1283 return ret; 1284} 1285 1286static int ct406_get_als_delay_param(char *buffer, struct kernel_param *kp) 1287{ 1288 int num_chars; 1289 1290 if (!buffer) 1291 return -EINVAL; 1292 1293 if (!ct406_misc_data) { 1294 scnprintf(buffer, PAGE_SIZE, "0\n"); 1295 return 1; 1296 } 1297 1298 num_chars = scnprintf(buffer, 2, "%d\n", ct406_als_delay_param); 1299 1300 return num_chars; 1301} 1302 1303module_param_call(als_delay, ct406_set_als_delay_param, ct406_get_als_delay_param, 1304 &ct406_als_delay_param, 0644); 1305MODULE_PARM_DESC(als_delay, "Set ALS delay."); 1306 1307static const struct file_operations ct406_misc_fops = { 1308 .owner = THIS_MODULE, 1309 .open = ct406_misc_open, 1310}; 1311 1312static ssize_t ct406_registers_show(struct device *dev, 1313 struct device_attribute *attr, 1314 char *buf) 1315{ 1316 struct i2c_client *client = container_of(dev, struct i2c_client, dev); 1317 struct ct406_data *ct = i2c_get_clientdata(client); 1318 int error = 0; 1319 unsigned int i, n, reg_count; 1320 u8 reg_data[1] = {0}; 1321 1322 reg_count = sizeof(ct406_regs) / sizeof(ct406_regs[0]); 1323 mutex_lock(&ct->mutex); 1324 for (i = 0, n = 0; i < reg_count; i++) { 1325 reg_data[0] = ct406_regs[i].reg; 1326 error = ct406_i2c_read(ct, reg_data, 1); 1327 n += scnprintf(buf + n, PAGE_SIZE - n, 1328 "%-20s = 0x%02X\n", 1329 ct406_regs[i].name, 1330 reg_data[0]); 1331 } 1332 mutex_unlock(&ct->mutex); 1333 1334 return n; 1335} 1336 1337static ssize_t ct406_registers_store(struct device *dev, 1338 struct device_attribute *attr, 1339 const char *buf, 1340 size_t count) 1341{ 1342 struct i2c_client *client = container_of(dev, struct i2c_client, dev); 1343 struct ct406_data *ct = i2c_get_clientdata(client); 1344 unsigned int i, reg_count; 1345 unsigned int value; 1346 u8 reg_data[2] = {0}; 1347 int error; 1348 char name[30]; 1349 1350 if (count >= 30) { 1351 pr_err("%s:input too long\n", __func__); 1352 return -EMSGSIZE; 1353 } 1354 1355 if (sscanf(buf, "%30s %x", name, &value) != 2) { 1356 pr_err("%s:unable to parse input\n", __func__); 1357 return -EINVAL; 1358 } 1359 1360 reg_count = sizeof(ct406_regs) / sizeof(ct406_regs[0]); 1361 for (i = 0; i < reg_count; i++) { 1362 if (!strcmp(name, ct406_regs[i].name)) { 1363 mutex_lock(&ct->mutex); 1364 error = ct406_i2c_write(ct, reg_data, 1); 1365 mutex_unlock(&ct->mutex); 1366 if (error) { 1367 pr_err("%s:Failed to write register %s\n", 1368 __func__, name); 1369 return error; 1370 } 1371 return count; 1372 } 1373 } 1374 1375 pr_err("%s:no such register %s\n", __func__, name); 1376 return -EINVAL; 1377} 1378 1379static DEVICE_ATTR(registers, 0644, ct406_registers_show, 1380 ct406_registers_store); 1381 1382static irqreturn_t ct406_irq_handler(int irq, void *dev) 1383{ 1384 struct ct406_data *ct = dev; 1385 1386 disable_irq_nosync(ct->client->irq); 1387 queue_work(ct->workqueue, &ct->work); 1388 1389 return IRQ_HANDLED; 1390} 1391 1392static void ct406_work_func_locked(struct ct406_data *ct) 1393{ 1394 int error; 1395 u8 reg_data[1] = {0}; 1396 reg_data[0] = CT406_STATUS; 1397 error = ct406_i2c_read(ct, reg_data, 1); 1398 if (error < 0) { 1399 pr_err("%s: Unable to read interrupt register: %d\n", 1400 __func__, error); 1401 return; 1402 } 1403 1404 if (ct->als_enabled && (reg_data[0] & CT406_STATUS_AINT)) 1405 ct406_report_als(ct); 1406 1407 if (ct->prox_enabled && (reg_data[0] & CT406_STATUS_PINT)) 1408 ct406_report_prox(ct); 1409} 1410 1411static void ct406_work_func(struct work_struct *work) 1412{ 1413 struct ct406_data *ct = 1414 container_of(work, struct ct406_data, work); 1415 1416 mutex_lock(&ct->mutex); 1417 if (ct->regs_initialized) 1418 ct406_work_func_locked(ct); 1419 mutex_unlock(&ct->mutex); 1420 1421 enable_irq(ct->client->irq); 1422} 1423 1424#ifdef CONFIG_HAS_EARLYSUSPEND 1425static void ct406_suspend(struct early_suspend *handler) 1426{ 1427 if (ct406_debug & CT406_DBG_SUSPEND_RESUME) 1428 pr_info("%s\n", __func__); 1429 1430 mutex_lock(&ct406_misc_data->mutex); 1431 1432 ct406_disable_als(ct406_misc_data); 1433 1434 if (!ct406_misc_data->prox_requested) 1435 ct406_device_power_off(ct406_misc_data); 1436 1437 ct406_misc_data->suspended = 1; 1438 1439 mutex_unlock(&ct406_misc_data->mutex); 1440} 1441 1442static void ct406_resume(struct early_suspend *handler) 1443{ 1444 if (ct406_debug & CT406_DBG_SUSPEND_RESUME) 1445 pr_info("%s\n", __func__); 1446 1447 mutex_lock(&ct406_misc_data->mutex); 1448 1449 ct406_device_power_on(ct406_misc_data); 1450 ct406_device_init(ct406_misc_data); 1451 1452 ct406_misc_data->suspended = 0; 1453 1454 if (ct406_misc_data->als_requested) 1455 ct406_enable_als(ct406_misc_data); 1456 1457 mutex_unlock(&ct406_misc_data->mutex); 1458} 1459#endif /* CONFIG_HAS_EARLYSUSPEND */ 1460 1461static int ct406_probe(struct i2c_client *client, 1462 const struct i2c_device_id *id) 1463{ 1464 struct ct406_platform_data *pdata = client->dev.platform_data; 1465 struct ct406_data *ct; 1466 u8 reg_data[1] = {0}; 1467 int error = 0; 1468 1469 if (pdata == NULL) { 1470 pr_err("%s: platform data required\n", __func__); 1471 return -ENODEV; 1472 } 1473 1474 client->irq = pdata->irq; 1475 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1476 pr_err("%s:I2C_FUNC_I2C not supported\n", __func__); 1477 return -ENODEV; 1478 } 1479 1480 ct = kzalloc(sizeof(struct ct406_data), GFP_KERNEL); 1481 if (ct == NULL) { 1482 error = -ENOMEM; 1483 goto error_alloc_data_failed; 1484 } 1485 1486 ct->client = client; 1487 ct->pdata = pdata; 1488 1489 if (ct->pdata->regulator_name[0] != '\0') { 1490 ct->regulator = regulator_get(NULL, 1491 ct->pdata->regulator_name); 1492 if (IS_ERR(ct->regulator)) { 1493 pr_err("%s: cannot acquire regulator [%s]\n", 1494 __func__, ct->pdata->regulator_name); 1495 error = PTR_ERR(ct->regulator); 1496 goto error_regulator_get_failed; 1497 } 1498 } 1499 1500 ct->dev = input_allocate_device(); 1501 if (!ct->dev) { 1502 error = -ENOMEM; 1503 pr_err("%s: input device allocate failed: %d\n", __func__, 1504 error); 1505 goto error_input_allocate_failed; 1506 } 1507 1508 ct->dev->name = "light-prox"; 1509 input_set_capability(ct->dev, EV_LED, LED_MISC); 1510 input_set_capability(ct->dev, EV_MSC, MSC_RAW); 1511 1512 ct406_misc_data = ct; 1513 ct->miscdevice.minor = MISC_DYNAMIC_MINOR; 1514 ct->miscdevice.name = LD_CT406_NAME; 1515 ct->miscdevice.fops = &ct406_misc_fops; 1516 error = misc_register(&ct->miscdevice); 1517 if (error < 0) { 1518 pr_err("%s: misc_register failed\n", __func__); 1519 goto error_misc_register_failed; 1520 } 1521 1522 ct->regs_initialized = 0; 1523 ct->suspended = 0; 1524 1525 ct->oscillator_enabled = 0; 1526 ct->prox_requested = 0; 1527 ct->prox_enabled = 0; 1528 ct->prox_mode = CT406_PROX_MODE_SATURATED; 1529 ct->als_requested = 0; 1530 ct->als_enabled = 0; 1531 ct->als_apers = 0x4; 1532 ct->als_first_report = 0; 1533 ct->als_mode = CT406_ALS_MODE_LOW_LUX; 1534 1535 ct->workqueue = create_singlethread_workqueue("als_wq"); 1536 if (!ct->workqueue) { 1537 pr_err("%s: Cannot create work queue\n", __func__); 1538 error = -ENOMEM; 1539 goto error_create_wq_failed; 1540 } 1541 1542 INIT_WORK(&ct->work, ct406_work_func); 1543 1544 error = request_irq(client->irq, ct406_irq_handler, 1545 IRQF_TRIGGER_LOW, LD_CT406_NAME, ct); 1546 if (error != 0) { 1547 pr_err("%s: irq request failed: %d\n", __func__, error); 1548 error = -ENODEV; 1549 goto error_req_irq_failed; 1550 } 1551 enable_irq_wake(client->irq); 1552 disable_irq(client->irq); 1553 1554 i2c_set_clientdata(client, ct); 1555 1556 mutex_init(&ct->mutex); 1557 1558 error = input_register_device(ct->dev); 1559 if (error) { 1560 pr_err("%s: input device register failed:%d\n", __func__, 1561 error); 1562 goto error_input_register_failed; 1563 } 1564 1565 error = device_create_file(&client->dev, &dev_attr_registers); 1566 if (error < 0) { 1567 pr_err("%s:File device creation failed: %d\n", __func__, error); 1568 error = -ENODEV; 1569 goto error_create_registers_file_failed; 1570 } 1571 1572 error = ct406_device_power_on(ct); 1573 if (error) { 1574 pr_err("%s:power_on failed: %d\n", __func__, error); 1575 goto error_power_on_failed; 1576 } 1577 1578 reg_data[0] = CT406_REV_ID; 1579 error = ct406_i2c_read(ct, reg_data, 1); 1580 if (error < 0) { 1581 pr_err("%s: revision read failed: %d\n", __func__, error); 1582 goto error_revision_read_failed; 1583 } 1584 if (reg_data[0] == CT40X_REV_ID_CT405) { 1585 pr_info("%s: CT405 part detected\n", __func__); 1586 ct->prox_saturation_threshold 1587 = ct->pdata->ct405_prox_saturation_threshold; 1588 ct->prox_covered_offset = ct->pdata->ct405_prox_covered_offset; 1589 ct->prox_uncovered_offset 1590 = ct->pdata->ct405_prox_uncovered_offset; 1591 ct->prox_recalibrate_offset 1592 = ct->pdata->ct405_prox_recalibrate_offset; 1593 ct->prox_pulse_count = ct->pdata->ct405_prox_pulse_count; 1594 ct->prox_offset = ct->pdata->ct405_prox_offset; 1595 ct->pdata_max = CT405_PDATA_MAX; 1596 ct->hw_type = CT405_HW_TYPE; 1597 } else { 1598 pr_info("%s: CT406 part detected\n", __func__); 1599 ct->prox_saturation_threshold 1600 = ct->pdata->ct406_prox_saturation_threshold; 1601 ct->prox_covered_offset = ct->pdata->ct406_prox_covered_offset; 1602 ct->prox_uncovered_offset 1603 = ct->pdata->ct406_prox_uncovered_offset; 1604 ct->prox_recalibrate_offset 1605 = ct->pdata->ct406_prox_recalibrate_offset; 1606 ct->prox_pulse_count = ct->pdata->ct406_prox_pulse_count; 1607 ct->prox_offset = ct->pdata->ct406_prox_offset; 1608 ct->pdata_max = CT406_PDATA_MAX; 1609 ct->hw_type = CT406_HW_TYPE; 1610 } 1611 1612 error = ct406_device_init(ct); 1613 if (error) { 1614 pr_err("%s:device init failed: %d\n", __func__, error); 1615 goto error_revision_read_failed; 1616 } 1617 1618#ifdef CONFIG_HAS_EARLYSUSPEND 1619 ct->ct406_early_suspend.suspend = ct406_suspend; 1620 ct->ct406_early_suspend.resume = ct406_resume; 1621 register_early_suspend(&ct->ct406_early_suspend); 1622#endif 1623 1624 return 0; 1625 1626error_revision_read_failed: 1627 ct406_device_power_off(ct); 1628error_power_on_failed: 1629 device_remove_file(&client->dev, &dev_attr_registers); 1630error_create_registers_file_failed: 1631 input_unregister_device(ct->dev); 1632 ct->dev = NULL; 1633error_input_register_failed: 1634 mutex_destroy(&ct->mutex); 1635 i2c_set_clientdata(client, NULL); 1636 free_irq(ct->client->irq, ct); 1637error_req_irq_failed: 1638 destroy_workqueue(ct->workqueue); 1639error_create_wq_failed: 1640 misc_deregister(&ct->miscdevice); 1641error_misc_register_failed: 1642 ct406_misc_data = NULL; 1643 input_free_device(ct->dev); 1644error_input_allocate_failed: 1645 regulator_put(ct->regulator); 1646error_regulator_get_failed: 1647 kfree(ct); 1648error_alloc_data_failed: 1649 return error; 1650} 1651 1652static int ct406_remove(struct i2c_client *client) 1653{ 1654 struct ct406_data *ct = i2c_get_clientdata(client); 1655 1656 device_remove_file(&client->dev, &dev_attr_registers); 1657 1658 ct406_device_power_off(ct); 1659 1660 input_unregister_device(ct->dev); 1661 1662 mutex_destroy(&ct->mutex); 1663 i2c_set_clientdata(client, NULL); 1664 free_irq(ct->client->irq, ct); 1665 1666 destroy_workqueue(ct->workqueue); 1667 1668 misc_deregister(&ct->miscdevice); 1669 1670 regulator_put(ct->regulator); 1671 1672 kfree(ct); 1673 1674 return 0; 1675} 1676 1677static const struct i2c_device_id ct406_id[] = { 1678 {LD_CT406_NAME, 0}, 1679 {} 1680}; 1681 1682static struct i2c_driver ct406_i2c_driver = { 1683 .probe = ct406_probe, 1684 .remove = ct406_remove, 1685 .id_table = ct406_id, 1686 .driver = { 1687 .name = LD_CT406_NAME, 1688 .owner = THIS_MODULE, 1689 }, 1690}; 1691 1692static int __init ct406_init(void) 1693{ 1694 return i2c_add_driver(&ct406_i2c_driver); 1695} 1696 1697static void __exit ct406_exit(void) 1698{ 1699 i2c_del_driver(&ct406_i2c_driver); 1700} 1701 1702module_init(ct406_init); 1703module_exit(ct406_exit); 1704 1705MODULE_DESCRIPTION("ALS and Proximity driver for CT406"); 1706MODULE_AUTHOR("Motorola Mobility"); 1707MODULE_LICENSE("GPL");