PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/input/misc/ct406.c

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