sx9500.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * 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., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include <linux/input.h>
  23. #include <linux/gpio.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/fs.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/wakelock.h>
  28. #include <linux/regulator/consumer.h>
  29. #include "sx9500_reg.h"
  30. #include "sensors_core.h"
  31. #define VENDOR_NAME "SEMTECH"
  32. #define MODEL_NAME "SX9500"
  33. #define MODULE_NAME "grip_sensor"
  34. #define CALIBRATION_FILE_PATH "/efs/FactoryApp/grip_cal_data"
  35. #define I2C_M_WR 0 /* for i2c Write */
  36. #define I2c_M_RD 1 /* for i2c Read */
  37. #define IDLE 0
  38. #define ACTIVE 1
  39. #define CAL_RET_ERROR -1
  40. #define CAL_RET_NONE 0
  41. #define CAL_RET_EXIST 1
  42. #define CAL_RET_SUCCESS 2
  43. #define INIT_TOUCH_MODE 0
  44. #define NORMAL_TOUCH_MODE 1
  45. #define SX9500_MODE_SLEEP 0
  46. #define SX9500_MODE_NORMAL 1
  47. #ifdef CONFIG_MACH_MONDRIAN
  48. #define MAIN_SENSOR 3
  49. #define REF_SENSOR 2
  50. #define CSX_STATUS_REG SX9500_TCHCMPSTAT_TCHSTAT3_FLAG
  51. #else
  52. #define MAIN_SENSOR 0
  53. #define REF_SENSOR 1
  54. #define CSX_STATUS_REG SX9500_TCHCMPSTAT_TCHSTAT0_FLAG
  55. #endif
  56. #define LIMIT_PROXOFFSET 3880 /* 45 pF */
  57. #define LIMIT_PROXUSEFUL 10000
  58. #define STANDARD_CAP_MAIN 450000
  59. #define DEFAULT_INIT_TOUCH_THRESHOLD 2000
  60. #define DEFAULT_NORMAL_TOUCH_THRESHOLD 17
  61. #define TOUCH_CHECK_REF_AMB 0 /* 44523 */
  62. #define TOUCH_CHECK_SLOPE 0 /* 50 */
  63. #define TOUCH_CHECK_MAIN_AMB 0 /* 151282 */
  64. #define DEFENCE_CODE_FOR_DEVICE_DAMAGE
  65. /* CS0, CS1, CS2, CS3 */
  66. #define TOTAL_BOTTON_COUNT 1
  67. #define ENABLE_CSX ((1 << MAIN_SENSOR) | (1 << REF_SENSOR))
  68. #define IRQ_PROCESS_CONDITION (SX9500_IRQSTAT_TOUCH_FLAG \
  69. | SX9500_IRQSTAT_RELEASE_FLAG \
  70. | SX9500_IRQSTAT_COMPDONE_FLAG)
  71. struct sx9500_p {
  72. struct i2c_client *client;
  73. struct input_dev *input;
  74. struct device *factory_device;
  75. struct delayed_work init_work;
  76. struct delayed_work irq_work;
  77. struct wake_lock grip_wake_lock;
  78. struct mutex mode_mutex;
  79. bool calSuccessed;
  80. bool flagDataSkip;
  81. u8 touchTh;
  82. int initTh;
  83. int calData[3];
  84. int touchMode;
  85. int irq;
  86. int gpioNirq;
  87. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  88. int gpio_adjdet;
  89. int irq_adjdet;
  90. int grip_state;
  91. int enable_adjdet;
  92. #endif
  93. int state[TOTAL_BOTTON_COUNT];
  94. #if defined(CONFIG_SEC_MILLET_PROJECT) \
  95. || defined(CONFIG_SEC_MATISSE_PROJECT) \
  96. || defined(CONFIG_SEC_BERLUTI_PROJECT) \
  97. || defined(CONFIG_SEC_DEGAS_PROJECT) \
  98. || defined(CONFIG_SEC_T8_PROJECT) \
  99. || defined(CONFIG_SEC_T10_PROJECT)
  100. struct regulator *L19;
  101. struct regulator *lvs1_1p8;
  102. #endif
  103. #if defined(CONFIG_SENSORS_SX9500_REGULATOR_ONOFF)
  104. struct regulator *vdd;
  105. struct regulator *ldo;
  106. #endif
  107. atomic_t enable;
  108. };
  109. #if defined(CONFIG_MACH_CHAGALL)
  110. #ifndef CONFIG_MACH_CHAGALL_KDI
  111. #define SX9500_NORMAL_TOUCH_CABLE_THRESHOLD 22
  112. int msm8974_get_cable_type(void);
  113. #endif
  114. #endif
  115. static int sx9500_get_nirq_state(struct sx9500_p *data)
  116. {
  117. return gpio_get_value_cansleep(data->gpioNirq);
  118. }
  119. static int sx9500_i2c_write(struct sx9500_p *data, u8 reg_addr, u8 buf)
  120. {
  121. int ret;
  122. struct i2c_msg msg;
  123. unsigned char w_buf[2];
  124. w_buf[0] = reg_addr;
  125. w_buf[1] = buf;
  126. msg.addr = data->client->addr;
  127. msg.flags = I2C_M_WR;
  128. msg.len = 2;
  129. msg.buf = (char *)w_buf;
  130. ret = i2c_transfer(data->client->adapter, &msg, 1);
  131. if (ret < 0)
  132. pr_err("[SX9500]: %s - i2c write error %d\n", __func__, ret);
  133. return ret;
  134. }
  135. static int sx9500_i2c_read(struct sx9500_p *data, u8 reg_addr, u8 *buf)
  136. {
  137. int ret;
  138. struct i2c_msg msg[2];
  139. msg[0].addr = data->client->addr;
  140. msg[0].flags = I2C_M_WR;
  141. msg[0].len = 1;
  142. msg[0].buf = &reg_addr;
  143. msg[1].addr = data->client->addr;
  144. msg[1].flags = I2C_M_RD;
  145. msg[1].len = 1;
  146. msg[1].buf = buf;
  147. ret = i2c_transfer(data->client->adapter, msg, 2);
  148. if (ret < 0)
  149. pr_err("[SX9500]: %s - i2c read error %d\n", __func__, ret);
  150. return ret;
  151. }
  152. static u8 sx9500_read_irqstate(struct sx9500_p *data)
  153. {
  154. u8 val = 0;
  155. u8 ret;
  156. if (sx9500_i2c_read(data, SX9500_IRQSTAT_REG, &val) >= 0) {
  157. ret = val & 0x00FF;
  158. return ret;
  159. }
  160. return 0;
  161. }
  162. static void sx9500_initialize_register(struct sx9500_p *data)
  163. {
  164. u8 val = 0;
  165. int idx;
  166. for (idx = 0; idx < (sizeof(setup_reg) >> 1); idx++) {
  167. sx9500_i2c_write(data, setup_reg[idx].reg, setup_reg[idx].val);
  168. pr_info("[SX9500]: %s - Write Reg: 0x%x Value: 0x%x\n",
  169. __func__, setup_reg[idx].reg, setup_reg[idx].val);
  170. sx9500_i2c_read(data, setup_reg[idx].reg, &val);
  171. pr_info("[SX9500]: %s - Read Reg: 0x%x Value: 0x%x\n\n",
  172. __func__, setup_reg[idx].reg, val);
  173. }
  174. }
  175. static void sx9500_initialize_chip(struct sx9500_p *data)
  176. {
  177. int cnt = 0;
  178. while ((sx9500_get_nirq_state(data) == 0) && (cnt++ < 10)) {
  179. sx9500_read_irqstate(data);
  180. msleep(20);
  181. }
  182. if (cnt >= 10)
  183. pr_err("[SX9500]: %s - s/w reset fail(%d)\n", __func__, cnt);
  184. sx9500_initialize_register(data);
  185. }
  186. static int sx9500_set_offset_calibration(struct sx9500_p *data)
  187. {
  188. int ret = 0;
  189. ret = sx9500_i2c_write(data, SX9500_IRQSTAT_REG, 0xFF);
  190. return ret;
  191. }
  192. static void send_event(struct sx9500_p *data, int cnt, u8 state)
  193. {
  194. u8 buf;
  195. #if defined(CONFIG_MACH_CHAGALL)
  196. #if defined(CONFIG_MACH_CHAGALL_KDI)
  197. buf = data->touchTh;
  198. #else
  199. if (msm8974_get_cable_type() > 1) {
  200. buf = SX9500_NORMAL_TOUCH_CABLE_THRESHOLD;
  201. pr_info("[SX9500]: %s - cable connected %d\n",
  202. __func__, msm8974_get_cable_type());
  203. } else {
  204. buf = data->touchTh;
  205. }
  206. #endif
  207. #else
  208. buf = data->touchTh;
  209. #endif
  210. if (state == ACTIVE) {
  211. data->state[cnt] = ACTIVE;
  212. sx9500_i2c_write(data, SX9500_CPS_CTRL6_REG, buf);
  213. pr_info("[SX9500]: %s - %d button touched\n", __func__, cnt);
  214. } else {
  215. data->touchMode = NORMAL_TOUCH_MODE;
  216. data->state[cnt] = IDLE;
  217. sx9500_i2c_write(data, SX9500_CPS_CTRL6_REG, buf);
  218. pr_info("[SX9500]: %s - %d button released\n", __func__, cnt);
  219. }
  220. if (data->flagDataSkip == true)
  221. return;
  222. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  223. if (data->enable_adjdet == 1
  224. &&
  225. gpio_get_value_cansleep(data->gpio_adjdet) == 1) {
  226. pr_info("[SX9500]: %s : adj detect cable connected," \
  227. " skip grip sensor\n", __func__);
  228. return;
  229. }
  230. #endif
  231. switch (cnt) {
  232. case 0:
  233. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  234. data->grip_state = state;
  235. #endif
  236. if (state == ACTIVE)
  237. input_report_rel(data->input, REL_MISC, 1);
  238. else
  239. input_report_rel(data->input, REL_MISC, 2);
  240. break;
  241. case 1:
  242. case 2:
  243. case 3:
  244. pr_info("[SX9500]: %s - There is no defined event for" \
  245. " button %d.\n", __func__, cnt);
  246. break;
  247. default:
  248. break;
  249. }
  250. input_sync(data->input);
  251. }
  252. static void sx9500_display_data_reg(struct sx9500_p *data)
  253. {
  254. u8 val, reg;
  255. int i;
  256. for (i = 0; i < TOTAL_BOTTON_COUNT; i++) {
  257. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, i);
  258. pr_info("[SX9500]: ############# %d button #############\n", i);
  259. for (reg = SX9500_REGUSEMSB;
  260. reg <= SX9500_REGOFFSETLSB; reg++) {
  261. sx9500_i2c_read(data, reg, &val);
  262. pr_info("[SX9500]: %s - Register(0x%2x) data(0x%2x)\n",
  263. __func__, reg, val);
  264. }
  265. }
  266. }
  267. static s32 sx9500_get_init_threshold(struct sx9500_p *data)
  268. {
  269. s32 threshold;
  270. /* Because the STANDARD_CAP_MAIN was 300,000 in the previous patch,
  271. * the exception code is added. It will be removed later */
  272. if (data->calData[0] == 0)
  273. threshold = STANDARD_CAP_MAIN + data->initTh;
  274. else if (data->calData[0] > 100000)
  275. threshold = data->initTh + data->calData[0];
  276. else
  277. threshold = 300000 + data->initTh + data->calData[0];
  278. return threshold;
  279. }
  280. static s32 sx9500_get_capMain(struct sx9500_p *data)
  281. {
  282. u8 msByte = 0;
  283. u8 lsByte = 0;
  284. u16 offset = 0;
  285. s32 capMain = 0, useful = 0;
  286. /* Calculate out the Main Cap information */
  287. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, MAIN_SENSOR);
  288. sx9500_i2c_read(data, SX9500_REGUSEMSB, &msByte);
  289. sx9500_i2c_read(data, SX9500_REGUSELSB, &lsByte);
  290. useful = (s32)msByte;
  291. useful = (useful << 8) | ((s32)lsByte);
  292. if (useful > 32767)
  293. useful -= 65536;
  294. sx9500_i2c_read(data, SX9500_REGOFFSETMSB, &msByte);
  295. sx9500_i2c_read(data, SX9500_REGOFFSETLSB, &lsByte);
  296. offset = (u16)msByte;
  297. offset = (offset << 8) | ((u16)lsByte);
  298. msByte = (u8)(offset >> 6);
  299. lsByte = (u8)(offset - (((u16)msByte) << 6));
  300. capMain = 2 * (((s32)msByte * 3600) + ((s32)lsByte * 225)) +
  301. (((s32)useful * 50000) / (8 * 65536));
  302. /* Calculate out the Reference Cap information */
  303. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, REF_SENSOR);
  304. sx9500_i2c_read(data, SX9500_REGUSEMSB, &msByte);
  305. sx9500_i2c_read(data, SX9500_REGUSELSB, &lsByte);
  306. #if 0 /* Calculate out the difference between the two */
  307. s32 capRef = 0;
  308. capRef = (s32)msByte;
  309. capRef = (capRef << 8) | ((s32)lsByte);
  310. if (capRef > 32767)
  311. capRef -= 65536;
  312. sx9500_i2c_read(data, SX9500_REGOFFSETMSB, &msByte);
  313. sx9500_i2c_read(data, SX9500_REGOFFSETLSB, &lsByte);
  314. offset = (u16)msByte;
  315. offset = (offset << 8) | ((u16)lsByte);
  316. msByte = (u8)(offset >> 6);
  317. lsByte = (u8)(offset - (((u16)msByte) << 6));
  318. capRef = 2 * (((s32)msByte * 3600) + ((s32)lsByte * 225)) +
  319. (((s32)capRef * 50000) / (8 * 65536));
  320. capRef = (capRef - TOUCH_CHECK_REF_AMB) *
  321. TOUCH_CHECK_SLOPE + TOUCH_CHECK_MAIN_AMB;
  322. capMain = capMain - capRef;
  323. #endif
  324. pr_info("[SX9500]: %s - CapsMain: %ld, useful: %ld, Offset: %u\n",
  325. __func__, (long int)capMain, (long int)useful, offset);
  326. return capMain;
  327. }
  328. static void sx9500_touchCheckWithRefSensor(struct sx9500_p *data)
  329. {
  330. s32 capMain, threshold;
  331. int cnt = 0;
  332. threshold = sx9500_get_init_threshold(data);
  333. capMain = sx9500_get_capMain(data);
  334. if (data->state[cnt] == IDLE) {
  335. if (capMain >= threshold)
  336. send_event(data, cnt, ACTIVE);
  337. else
  338. send_event(data, cnt, IDLE);
  339. } else {
  340. if (capMain < threshold)
  341. send_event(data, cnt, IDLE);
  342. else
  343. send_event(data, cnt, ACTIVE);
  344. }
  345. }
  346. static int sx9500_save_caldata(struct sx9500_p *data)
  347. {
  348. struct file *cal_filp = NULL;
  349. mm_segment_t old_fs;
  350. int ret = 0;
  351. old_fs = get_fs();
  352. set_fs(KERNEL_DS);
  353. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  354. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC,
  355. S_IRUGO | S_IWUSR | S_IWGRP);
  356. if (IS_ERR(cal_filp)) {
  357. pr_err("[SX9500]: %s - Can't open calibration file\n",
  358. __func__);
  359. set_fs(old_fs);
  360. ret = PTR_ERR(cal_filp);
  361. return ret;
  362. }
  363. ret = cal_filp->f_op->write(cal_filp, (char *)data->calData,
  364. sizeof(int) * 3, &cal_filp->f_pos);
  365. if (ret != (sizeof(int) * 3)) {
  366. pr_err("[SX9500]: %s - Can't write the cal data to file\n",
  367. __func__);
  368. ret = -EIO;
  369. }
  370. filp_close(cal_filp, current->files);
  371. set_fs(old_fs);
  372. return ret;
  373. }
  374. static void sx9500_open_caldata(struct sx9500_p *data)
  375. {
  376. struct file *cal_filp = NULL;
  377. mm_segment_t old_fs;
  378. int ret;
  379. old_fs = get_fs();
  380. set_fs(KERNEL_DS);
  381. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY,
  382. S_IRUGO | S_IWUSR | S_IWGRP);
  383. if (IS_ERR(cal_filp)) {
  384. ret = PTR_ERR(cal_filp);
  385. if (ret != -ENOENT)
  386. pr_err("[SX9500]: %s - Can't open calibration file.\n",
  387. __func__);
  388. else {
  389. pr_info("[SX9500]: %s - There is no calibration file\n",
  390. __func__);
  391. /* calibration status init */
  392. memset(data->calData, 0, sizeof(int) * 3);
  393. }
  394. set_fs(old_fs);
  395. return;
  396. }
  397. ret = cal_filp->f_op->read(cal_filp, (char *)data->calData,
  398. sizeof(int) * 3, &cal_filp->f_pos);
  399. if (ret != (sizeof(int) * 3))
  400. pr_err("[SX9500]: %s - Can't read the cal data from file\n",
  401. __func__);
  402. filp_close(cal_filp, current->files);
  403. set_fs(old_fs);
  404. pr_info("[SX9500]: %s - (%d, %d, %d)\n", __func__,
  405. data->calData[0], data->calData[1], data->calData[2]);
  406. }
  407. static int sx9500_set_mode(struct sx9500_p *data, unsigned char mode)
  408. {
  409. int ret = -EINVAL;
  410. mutex_lock(&data->mode_mutex);
  411. if (mode == SX9500_MODE_SLEEP) {
  412. ret = sx9500_i2c_write(data, SX9500_CPS_CTRL0_REG, 0x20);
  413. disable_irq(data->irq);
  414. disable_irq_wake(data->irq);
  415. } else if (mode == SX9500_MODE_NORMAL) {
  416. ret = sx9500_i2c_write(data, SX9500_CPS_CTRL0_REG,
  417. 0x20 | ENABLE_CSX);
  418. msleep(20);
  419. sx9500_set_offset_calibration(data);
  420. msleep(400);
  421. sx9500_touchCheckWithRefSensor(data);
  422. enable_irq(data->irq);
  423. enable_irq_wake(data->irq);
  424. }
  425. /* make sure no interrupts are pending since enabling irq
  426. * will only work on next falling edge */
  427. sx9500_read_irqstate(data);
  428. pr_info("[SX9500]: %s - change the mode : %u\n", __func__, mode);
  429. mutex_unlock(&data->mode_mutex);
  430. return ret;
  431. }
  432. static int sx9500_get_useful(struct sx9500_p *data)
  433. {
  434. u8 msByte = 0;
  435. u8 lsByte = 0;
  436. s16 fullByte = 0;
  437. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, MAIN_SENSOR);
  438. sx9500_i2c_read(data, SX9500_REGUSEMSB, &msByte);
  439. sx9500_i2c_read(data, SX9500_REGUSELSB, &lsByte);
  440. fullByte = (s16)((msByte << 8) | lsByte);
  441. pr_info("[SX9500]: %s - PROXIUSEFUL = %d\n", __func__, fullByte);
  442. return (int)fullByte;
  443. }
  444. static int sx9500_get_offset(struct sx9500_p *data)
  445. {
  446. u8 msByte = 0;
  447. u8 lsByte = 0;
  448. u16 fullByte = 0;
  449. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, MAIN_SENSOR);
  450. sx9500_i2c_read(data, SX9500_REGOFFSETMSB, &msByte);
  451. sx9500_i2c_read(data, SX9500_REGOFFSETLSB, &lsByte);
  452. fullByte = (u16)((msByte << 8) | lsByte);
  453. pr_info("[SX9500]: %s - PROXIOFFSET = %u\n", __func__, fullByte);
  454. return (int)fullByte;
  455. }
  456. static int sx9500_do_calibrate(struct sx9500_p *data, bool do_calib)
  457. {
  458. int ret = 0;
  459. s32 capMain;
  460. if (do_calib == false) {
  461. pr_info("[SX9500]: %s - Erase!\n", __func__);
  462. goto cal_erase;
  463. }
  464. if (atomic_read(&data->enable) == OFF)
  465. sx9500_set_mode(data, SX9500_MODE_NORMAL);
  466. data->calData[2] = sx9500_get_offset(data);
  467. if ((data->calData[2] >= LIMIT_PROXOFFSET)
  468. || (data->calData[2] == 0)) {
  469. pr_err("[SX9500]: %s - offset fail(%d)\n", __func__,
  470. data->calData[2]);
  471. goto cal_fail;
  472. }
  473. data->calData[1] = sx9500_get_useful(data);
  474. if (data->calData[1] >= LIMIT_PROXUSEFUL) {
  475. pr_err("[SX9500]: %s - useful warning(%d)\n", __func__,
  476. data->calData[1]);
  477. }
  478. capMain = sx9500_get_capMain(data);
  479. data->calData[0] = capMain;
  480. if (atomic_read(&data->enable) == OFF)
  481. sx9500_set_mode(data, SX9500_MODE_SLEEP);
  482. goto exit;
  483. cal_fail:
  484. if (atomic_read(&data->enable) == OFF)
  485. sx9500_set_mode(data, SX9500_MODE_SLEEP);
  486. ret = -1;
  487. cal_erase:
  488. memset(data->calData, 0, sizeof(int) * 3);
  489. exit:
  490. pr_info("[SX9500]: %s - (%d, %d, %d)\n", __func__,
  491. data->calData[0], data->calData[1], data->calData[2]);
  492. return ret;
  493. }
  494. static void sx9500_set_enable(struct sx9500_p *data, int enable)
  495. {
  496. int pre_enable = atomic_read(&data->enable);
  497. if (enable) {
  498. if (pre_enable == OFF) {
  499. data->touchMode = INIT_TOUCH_MODE;
  500. data->calSuccessed = false;
  501. sx9500_open_caldata(data);
  502. sx9500_set_mode(data, SX9500_MODE_NORMAL);
  503. atomic_set(&data->enable, ON);
  504. }
  505. } else {
  506. if (pre_enable == ON) {
  507. sx9500_set_mode(data, SX9500_MODE_SLEEP);
  508. atomic_set(&data->enable, OFF);
  509. }
  510. }
  511. }
  512. static ssize_t sx9500_get_offset_calibration_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. u8 val = 0;
  516. struct sx9500_p *data = dev_get_drvdata(dev);
  517. sx9500_i2c_read(data, SX9500_IRQSTAT_REG, &val);
  518. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  519. }
  520. static ssize_t sx9500_set_offset_calibration_store(struct device *dev,
  521. struct device_attribute *attr, const char *buf, size_t count)
  522. {
  523. unsigned long val;
  524. struct sx9500_p *data = dev_get_drvdata(dev);
  525. if (strict_strtoul(buf, 10, &val)) {
  526. pr_err("[SX9500]: %s - Invalid Argument\n", __func__);
  527. return -EINVAL;
  528. }
  529. if (val)
  530. sx9500_set_offset_calibration(data);
  531. return count;
  532. }
  533. static ssize_t sx9500_register_write_store(struct device *dev,
  534. struct device_attribute *attr, const char *buf, size_t count)
  535. {
  536. int regist = 0, val = 0;
  537. struct sx9500_p *data = dev_get_drvdata(dev);
  538. if (sscanf(buf, "%d,%d", &regist, &val) != 2) {
  539. pr_err("[SX9500]: %s - The number of data are wrong\n",
  540. __func__);
  541. return -EINVAL;
  542. }
  543. sx9500_i2c_write(data, (unsigned char)regist, (unsigned char)val);
  544. pr_info("[SX9500]: %s - Register(0x%2x) data(0x%2x)\n",
  545. __func__, regist, val);
  546. return count;
  547. }
  548. static ssize_t sx9500_register_read_store(struct device *dev,
  549. struct device_attribute *attr, const char *buf, size_t count)
  550. {
  551. int regist = 0;
  552. unsigned char val = 0;
  553. struct sx9500_p *data = dev_get_drvdata(dev);
  554. if (sscanf(buf, "%d", &regist) != 1) {
  555. pr_err("[SX9500]: %s - The number of data are wrong\n",
  556. __func__);
  557. return -EINVAL;
  558. }
  559. sx9500_i2c_read(data, (unsigned char)regist, &val);
  560. pr_info("[SX9500]: %s - Register(0x%2x) data(0x%2x)\n",
  561. __func__, regist, val);
  562. return count;
  563. }
  564. static ssize_t sx9500_read_data_show(struct device *dev,
  565. struct device_attribute *attr, char *buf)
  566. {
  567. struct sx9500_p *data = dev_get_drvdata(dev);
  568. sx9500_display_data_reg(data);
  569. return snprintf(buf, PAGE_SIZE, "%d\n", 0);
  570. }
  571. static ssize_t sx9500_sw_reset_show(struct device *dev,
  572. struct device_attribute *attr, char *buf)
  573. {
  574. int ret = 0;
  575. struct sx9500_p *data = dev_get_drvdata(dev);
  576. if (atomic_read(&data->enable) == ON)
  577. sx9500_set_mode(data, SX9500_MODE_SLEEP);
  578. ret = sx9500_i2c_write(data, SX9500_SOFTRESET_REG, SX9500_SOFTRESET);
  579. msleep(300);
  580. sx9500_initialize_chip(data);
  581. if (atomic_read(&data->enable) == ON)
  582. sx9500_set_mode(data, SX9500_MODE_NORMAL);
  583. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  584. }
  585. static ssize_t sx9500_vendor_show(struct device *dev,
  586. struct device_attribute *attr, char *buf)
  587. {
  588. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  589. }
  590. static ssize_t sx9500_name_show(struct device *dev,
  591. struct device_attribute *attr, char *buf)
  592. {
  593. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  594. }
  595. static ssize_t sx9500_touch_mode_show(struct device *dev,
  596. struct device_attribute *attr, char *buf)
  597. {
  598. struct sx9500_p *data = dev_get_drvdata(dev);
  599. return snprintf(buf, PAGE_SIZE, "%d\n", data->touchMode);
  600. }
  601. static ssize_t sx9500_raw_data_show(struct device *dev,
  602. struct device_attribute *attr, char *buf)
  603. {
  604. u8 msb, lsb;
  605. s16 useful;
  606. u16 offset;
  607. s32 capMain;
  608. struct sx9500_p *data = dev_get_drvdata(dev);
  609. if (atomic_read(&data->enable) == OFF)
  610. pr_err("[SX9500]: %s - SX9500 was not enabled\n", __func__);
  611. capMain = sx9500_get_capMain(data);
  612. sx9500_i2c_write(data, SX9500_REGSENSORSELECT, MAIN_SENSOR);
  613. sx9500_i2c_read(data, SX9500_REGUSEMSB, &msb);
  614. sx9500_i2c_read(data, SX9500_REGUSELSB, &lsb);
  615. useful = (s16)((msb << 8) | lsb);
  616. sx9500_i2c_read(data, SX9500_REGOFFSETMSB, &msb);
  617. sx9500_i2c_read(data, SX9500_REGOFFSETLSB, &lsb);
  618. offset = (u16)((msb << 8) | lsb);
  619. return snprintf(buf, PAGE_SIZE, "%d,%d,%u\n", capMain, useful, offset);
  620. }
  621. static ssize_t sx9500_threshold_show(struct device *dev,
  622. struct device_attribute *attr, char *buf)
  623. {
  624. struct sx9500_p *data = dev_get_drvdata(dev);
  625. /* It's for init touch */
  626. return snprintf(buf, PAGE_SIZE, "%d\n",
  627. sx9500_get_init_threshold(data));
  628. }
  629. static ssize_t sx9500_threshold_store(struct device *dev,
  630. struct device_attribute *attr, const char *buf, size_t count)
  631. {
  632. unsigned long val;
  633. struct sx9500_p *data = dev_get_drvdata(dev);
  634. /* It's for normal touch */
  635. if (strict_strtoul(buf, 10, &val)) {
  636. pr_err("[SX9500]: %s - Invalid Argument\n", __func__);
  637. return -EINVAL;
  638. }
  639. pr_info("[SX9500]: %s - normal threshold %lu\n", __func__, val);
  640. data->touchTh = (u8)val;
  641. return count;
  642. }
  643. static ssize_t sx9500_onoff_show(struct device *dev,
  644. struct device_attribute *attr, char *buf)
  645. {
  646. struct sx9500_p *data = dev_get_drvdata(dev);
  647. return snprintf(buf, PAGE_SIZE, "%u\n", !data->flagDataSkip);
  648. }
  649. static ssize_t sx9500_onoff_store(struct device *dev,
  650. struct device_attribute *attr, const char *buf, size_t count)
  651. {
  652. u8 val;
  653. int ret;
  654. struct sx9500_p *data = dev_get_drvdata(dev);
  655. ret = kstrtou8(buf, 2, &val);
  656. if (ret) {
  657. pr_err("[SX9500]: %s - Invalid Argument\n", __func__);
  658. return ret;
  659. }
  660. if (val == 0)
  661. data->flagDataSkip = true;
  662. else
  663. data->flagDataSkip = false;
  664. pr_info("[SX9500]: %s -%u\n", __func__, val);
  665. return count;
  666. }
  667. static ssize_t sx9500_calibration_show(struct device *dev,
  668. struct device_attribute *attr, char *buf)
  669. {
  670. int ret;
  671. struct sx9500_p *data = dev_get_drvdata(dev);
  672. if ((data->calSuccessed == false) && (data->calData[0] == 0))
  673. ret = CAL_RET_NONE;
  674. else if ((data->calSuccessed == false) && (data->calData[0] != 0))
  675. ret = CAL_RET_EXIST;
  676. else if ((data->calSuccessed == true) && (data->calData[0] != 0))
  677. ret = CAL_RET_SUCCESS;
  678. else
  679. ret = CAL_RET_ERROR;
  680. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", ret,
  681. data->calData[1], data->calData[2]);
  682. }
  683. static ssize_t sx9500_calibration_store(struct device *dev,
  684. struct device_attribute *attr, const char *buf, size_t count)
  685. {
  686. bool do_calib;
  687. int ret;
  688. struct sx9500_p *data = dev_get_drvdata(dev);
  689. if (sysfs_streq(buf, "1"))
  690. do_calib = true;
  691. else if (sysfs_streq(buf, "0"))
  692. do_calib = false;
  693. else {
  694. pr_info("[SX9500]: %s - invalid value %d\n", __func__, *buf);
  695. return -EINVAL;
  696. }
  697. ret = sx9500_do_calibrate(data, do_calib);
  698. if (ret < 0) {
  699. pr_err("[SX9500]: %s - sx9500_do_calibrate fail(%d)\n",
  700. __func__, ret);
  701. goto exit;
  702. }
  703. ret = sx9500_save_caldata(data);
  704. if (ret < 0) {
  705. pr_err("[SX9500]: %s - sx9500_save_caldata fail(%d)\n",
  706. __func__, ret);
  707. memset(data->calData, 0, sizeof(int) * 3);
  708. goto exit;
  709. }
  710. pr_info("[SX9500]: %s - %u success!\n", __func__, do_calib);
  711. exit:
  712. if ((data->calData[0] != 0) && (ret >= 0))
  713. data->calSuccessed = true;
  714. else
  715. data->calSuccessed = false;
  716. return count;
  717. }
  718. static DEVICE_ATTR(menual_calibrate, S_IRUGO | S_IWUSR | S_IWGRP,
  719. sx9500_get_offset_calibration_show,
  720. sx9500_set_offset_calibration_store);
  721. static DEVICE_ATTR(register_write, S_IRUGO | S_IWUSR | S_IWGRP,
  722. NULL, sx9500_register_write_store);
  723. static DEVICE_ATTR(register_read, S_IRUGO | S_IWUSR | S_IWGRP,
  724. NULL, sx9500_register_read_store);
  725. static DEVICE_ATTR(readback, S_IRUGO, sx9500_read_data_show, NULL);
  726. static DEVICE_ATTR(reset, S_IRUGO, sx9500_sw_reset_show, NULL);
  727. static DEVICE_ATTR(name, S_IRUGO, sx9500_name_show, NULL);
  728. static DEVICE_ATTR(vendor, S_IRUGO, sx9500_vendor_show, NULL);
  729. static DEVICE_ATTR(mode, S_IRUGO, sx9500_touch_mode_show, NULL);
  730. static DEVICE_ATTR(raw_data, S_IRUGO, sx9500_raw_data_show, NULL);
  731. static DEVICE_ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  732. sx9500_calibration_show, sx9500_calibration_store);
  733. static DEVICE_ATTR(onoff, S_IRUGO | S_IWUSR | S_IWGRP,
  734. sx9500_onoff_show, sx9500_onoff_store);
  735. static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR | S_IWGRP,
  736. sx9500_threshold_show, sx9500_threshold_store);
  737. static struct device_attribute *sensor_attrs[] = {
  738. &dev_attr_menual_calibrate,
  739. &dev_attr_register_write,
  740. &dev_attr_register_read,
  741. &dev_attr_readback,
  742. &dev_attr_reset,
  743. &dev_attr_name,
  744. &dev_attr_vendor,
  745. &dev_attr_mode,
  746. &dev_attr_raw_data,
  747. &dev_attr_threshold,
  748. &dev_attr_onoff,
  749. &dev_attr_calibration,
  750. NULL,
  751. };
  752. /*****************************************************************************/
  753. static ssize_t sx9500_enable_store(struct device *dev,
  754. struct device_attribute *attr, const char *buf, size_t size)
  755. {
  756. u8 enable;
  757. int ret;
  758. struct sx9500_p *data = dev_get_drvdata(dev);
  759. ret = kstrtou8(buf, 2, &enable);
  760. if (ret) {
  761. pr_err("[SX9500]: %s - Invalid Argument\n", __func__);
  762. return ret;
  763. }
  764. pr_info("[SX9500]: %s - new_value = %u\n", __func__, enable);
  765. if ((enable == 0) || (enable == 1))
  766. sx9500_set_enable(data, (int)enable);
  767. return size;
  768. }
  769. static ssize_t sx9500_enable_show(struct device *dev,
  770. struct device_attribute *attr, char *buf)
  771. {
  772. struct sx9500_p *data = dev_get_drvdata(dev);
  773. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
  774. }
  775. static ssize_t sx9500_flush_store(struct device *dev,
  776. struct device_attribute *attr, const char *buf, size_t size)
  777. {
  778. u8 enable;
  779. int ret;
  780. struct sx9500_p *data = dev_get_drvdata(dev);
  781. ret = kstrtou8(buf, 2, &enable);
  782. if (ret) {
  783. pr_err("[SX9500]: %s - Invalid Argument\n", __func__);
  784. return ret;
  785. }
  786. if (enable == 1) {
  787. mutex_lock(&data->mode_mutex);
  788. input_report_rel(data->input, REL_MAX, 1);
  789. input_sync(data->input);
  790. mutex_unlock(&data->mode_mutex);
  791. }
  792. return size;
  793. }
  794. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  795. sx9500_enable_show, sx9500_enable_store);
  796. static DEVICE_ATTR(flush, S_IWUSR | S_IWGRP,
  797. NULL, sx9500_flush_store);
  798. static struct attribute *sx9500_attributes[] = {
  799. &dev_attr_enable.attr,
  800. &dev_attr_flush.attr,
  801. NULL
  802. };
  803. static struct attribute_group sx9500_attribute_group = {
  804. .attrs = sx9500_attributes
  805. };
  806. static void sx9500_touch_process(struct sx9500_p *data)
  807. {
  808. u8 status = 0;
  809. int cnt;
  810. s32 capMain, threshold;
  811. threshold = sx9500_get_init_threshold(data);
  812. capMain = sx9500_get_capMain(data);
  813. sx9500_i2c_read(data, SX9500_TCHCMPSTAT_REG, &status);
  814. for (cnt = 0; cnt < TOTAL_BOTTON_COUNT; cnt++) {
  815. if (data->state[cnt] == IDLE) {
  816. if (status & (CSX_STATUS_REG << cnt)) {
  817. #ifdef DEFENCE_CODE_FOR_DEVICE_DAMAGE
  818. if ((data->calData[0] - 5000) > capMain) {
  819. sx9500_set_offset_calibration(data);
  820. mdelay(400);
  821. sx9500_touchCheckWithRefSensor(data);
  822. pr_err("[SX9500]: %s - Defence code for"
  823. " device damage\n", __func__);
  824. return;
  825. }
  826. #endif
  827. send_event(data, cnt, ACTIVE);
  828. } else {
  829. pr_info("[SX9500]: %s - %d already released.\n",
  830. __func__, cnt);
  831. }
  832. } else { /* User released button */
  833. if (!(status & (CSX_STATUS_REG << cnt))) {
  834. if ((data->touchMode == INIT_TOUCH_MODE)
  835. && (capMain >= threshold))
  836. pr_info("[SX9500]: %s - IDLE SKIP\n",
  837. __func__);
  838. else
  839. send_event(data, cnt, IDLE);
  840. } else {
  841. pr_info("[SX9500]: %s - %d still touched\n",
  842. __func__, cnt);
  843. }
  844. }
  845. }
  846. }
  847. static void sx9500_process_interrupt(struct sx9500_p *data)
  848. {
  849. u8 status = 0;
  850. /* since we are not in an interrupt don't need to disable irq. */
  851. status = sx9500_read_irqstate(data);
  852. if (status & IRQ_PROCESS_CONDITION)
  853. sx9500_touch_process(data);
  854. }
  855. static void sx9500_init_work_func(struct work_struct *work)
  856. {
  857. struct sx9500_p *data = container_of((struct delayed_work *)work,
  858. struct sx9500_p, init_work);
  859. sx9500_initialize_chip(data);
  860. /* make sure no interrupts are pending since enabling irq
  861. * will only work on next falling edge */
  862. sx9500_read_irqstate(data);
  863. }
  864. static void sx9500_irq_work_func(struct work_struct *work)
  865. {
  866. struct sx9500_p *data = container_of((struct delayed_work *)work,
  867. struct sx9500_p, irq_work);
  868. sx9500_process_interrupt(data);
  869. }
  870. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  871. static irqreturn_t sx9500_adjdet_interrupt_thread(int irq, void *pdata)
  872. {
  873. struct sx9500_p *data = pdata;
  874. if (gpio_get_value_cansleep(data->gpio_adjdet) == 0)
  875. pr_info("[SX9500]: %s : adj detect cable disconnect!" \
  876. " grip sensor enable\n", __func__);
  877. else {
  878. pr_info("[SX9500]: %s : adj detect cable connect!" \
  879. " grip sensordisable\n", __func__);
  880. if (data->grip_state == ACTIVE) {
  881. pr_info("[SX9500]: %s : Send FAR(IDLE)\n", __func__);
  882. input_report_rel(data->input, REL_MISC, 2);
  883. input_sync(data->input);
  884. data->grip_state = IDLE;
  885. }
  886. }
  887. return IRQ_HANDLED;
  888. }
  889. #endif
  890. static irqreturn_t sx9500_interrupt_thread(int irq, void *pdata)
  891. {
  892. struct sx9500_p *data = pdata;
  893. if (sx9500_get_nirq_state(data) == 1) {
  894. pr_err("[SX9500]: %s - nirq read high\n", __func__);
  895. } else {
  896. wake_lock_timeout(&data->grip_wake_lock, 3 * HZ);
  897. schedule_delayed_work(&data->irq_work, msecs_to_jiffies(100));
  898. }
  899. return IRQ_HANDLED;
  900. }
  901. static int sx9500_input_init(struct sx9500_p *data)
  902. {
  903. int ret = 0;
  904. struct input_dev *dev = NULL;
  905. /* Create the input device */
  906. dev = input_allocate_device();
  907. if (!dev)
  908. return -ENOMEM;
  909. dev->name = MODULE_NAME;
  910. dev->id.bustype = BUS_I2C;
  911. input_set_capability(dev, EV_REL, REL_MISC);
  912. input_set_capability(dev, EV_REL, REL_MAX);
  913. input_set_drvdata(dev, data);
  914. ret = input_register_device(dev);
  915. if (ret < 0) {
  916. input_free_device(dev);
  917. return ret;
  918. }
  919. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  920. if (ret < 0) {
  921. input_unregister_device(dev);
  922. return ret;
  923. }
  924. ret = sysfs_create_group(&dev->dev.kobj, &sx9500_attribute_group);
  925. if (ret < 0) {
  926. sensors_remove_symlink(&data->input->dev.kobj,
  927. data->input->name);
  928. input_unregister_device(dev);
  929. return ret;
  930. }
  931. /* save the input pointer and finish initialization */
  932. data->input = dev;
  933. return 0;
  934. }
  935. static int sx9500_setup_pin(struct sx9500_p *data)
  936. {
  937. int ret;
  938. ret = gpio_request(data->gpioNirq, "SX9500_nIRQ");
  939. if (ret < 0) {
  940. pr_err("[SX9500]: %s - gpio %d request failed (%d)\n",
  941. __func__, data->gpioNirq, ret);
  942. return ret;
  943. }
  944. ret = gpio_direction_input(data->gpioNirq);
  945. if (ret < 0) {
  946. pr_err("[SX9500]: %s - failed to set gpio %d as input (%d)\n",
  947. __func__, data->gpioNirq, ret);
  948. gpio_free(data->gpioNirq);
  949. return ret;
  950. }
  951. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  952. if (data->enable_adjdet == 1) {
  953. ret = gpio_request(data->gpio_adjdet, "SX9500_ADJDETIRQ");
  954. if (ret < 0) {
  955. pr_err("[SX9500]: %s - gpio %d request failed (%d)\n",
  956. __func__, data->gpio_adjdet, ret);
  957. return ret;
  958. }
  959. ret = gpio_direction_input(data->gpio_adjdet);
  960. if (ret < 0) {
  961. pr_err("[SX9500]: %s - failed to set" \
  962. " gpio %d as input (%d)\n",
  963. __func__, data->gpio_adjdet, ret);
  964. gpio_free(data->gpio_adjdet);
  965. return ret;
  966. }
  967. }
  968. #endif
  969. return 0;
  970. }
  971. static void sx9500_initialize_variable(struct sx9500_p *data)
  972. {
  973. int cnt;
  974. for (cnt = 0; cnt < TOTAL_BOTTON_COUNT; cnt++)
  975. data->state[cnt] = IDLE;
  976. data->touchMode = INIT_TOUCH_MODE;
  977. data->flagDataSkip = false;
  978. data->calSuccessed = false;
  979. memset(data->calData, 0, sizeof(int) * 3);
  980. atomic_set(&data->enable, OFF);
  981. data->initTh = (int)CONFIG_SENSORS_SX9500_INIT_TOUCH_THRESHOLD;
  982. pr_info("[SX9500]: %s - Init Touch Threshold : %d\n",
  983. __func__, data->initTh);
  984. data->touchTh = (u8)CONFIG_SENSORS_SX9500_NORMAL_TOUCH_THRESHOLD;
  985. pr_info("[SX9500]: %s - Normal Touch Threshold : %u\n",
  986. __func__, data->touchTh);
  987. }
  988. static int sx9500_parse_dt(struct sx9500_p *data, struct device *dev)
  989. {
  990. struct device_node *dNode = dev->of_node;
  991. enum of_gpio_flags flags;
  992. if (dNode == NULL)
  993. return -ENODEV;
  994. data->gpioNirq = of_get_named_gpio_flags(dNode,
  995. "sx9500-i2c,nirq-gpio", 0, &flags);
  996. if (data->gpioNirq < 0) {
  997. pr_err("[SX9500]: %s - get gpioNirq error\n", __func__);
  998. return -ENODEV;
  999. }
  1000. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  1001. data->gpio_adjdet = of_get_named_gpio_flags(dNode,
  1002. "sx9500-i2c,adjdet-gpio", 0, &flags);
  1003. if (data->gpio_adjdet < 0) {
  1004. pr_err("[SX9500]: %s - get gpio_adjdet error\n", __func__);
  1005. data->enable_adjdet = 0;
  1006. } else {
  1007. pr_info("[SX9500]: %s - get gpio_adjdet success\n", __func__);
  1008. data->enable_adjdet = 1;
  1009. }
  1010. #endif
  1011. return 0;
  1012. }
  1013. #if defined(CONFIG_SENSORS_SX9500_REGULATOR_ONOFF)
  1014. int sx9500_regulator_on(struct sx9500_p *data, bool onoff)
  1015. {
  1016. int ret = -1;
  1017. if (!data->vdd) {
  1018. data->vdd = devm_regulator_get(&data->client->dev, "vdd");
  1019. if (!data->vdd) {
  1020. pr_err("%s: regulator pointer null vdd, rc=%d\n",
  1021. __func__, ret);
  1022. return ret;
  1023. }
  1024. ret = regulator_set_voltage(data->vdd, 2850000, 2850000);
  1025. if (ret) {
  1026. pr_err("%s: set voltage failed on vdd, rc=%d\n",
  1027. __func__, ret);
  1028. return ret;
  1029. }
  1030. }
  1031. if (!data->ldo) {
  1032. data->ldo = devm_regulator_get(&data->client->dev, "vdd_ldo");
  1033. if (!data->ldo) {
  1034. pr_err("%s: devm_regulator_get for vdd_ldo failed\n",
  1035. __func__);
  1036. return 0;
  1037. }
  1038. }
  1039. if (onoff) {
  1040. ret = regulator_enable(data->vdd);
  1041. if (ret) {
  1042. pr_err("%s: Failed to enable regulator vdd.\n",
  1043. __func__);
  1044. return ret;
  1045. }
  1046. ret = regulator_enable(data->ldo);
  1047. if (ret) {
  1048. pr_err("%s: Failed to enable regulator ldo.\n",
  1049. __func__);
  1050. return ret;
  1051. }
  1052. } else {
  1053. ret = regulator_disable(data->vdd);
  1054. if (ret) {
  1055. pr_err("%s: Failed to disable regulator vdd.\n",
  1056. __func__);
  1057. return ret;
  1058. }
  1059. ret = regulator_disable(data->ldo);
  1060. if (ret) {
  1061. pr_err("%s: Failed to disable regulator ldo.\n",
  1062. __func__);
  1063. return ret;
  1064. }
  1065. }
  1066. /*Delay added for wakeup of chip, before i2c-transactions */
  1067. msleep(30);
  1068. return 0;
  1069. }
  1070. #endif
  1071. #if defined(CONFIG_SEC_MILLET_PROJECT) \
  1072. || defined(CONFIG_SEC_MATISSE_PROJECT) \
  1073. || defined(CONFIG_SEC_BERLUTI_PROJECT) \
  1074. || defined(CONFIG_SEC_DEGAS_PROJECT) \
  1075. || defined(CONFIG_SEC_T8_PROJECT) \
  1076. || defined(CONFIG_SEC_T10_PROJECT)
  1077. int sx9500_power_on(struct sx9500_p *data, bool onoff)
  1078. {
  1079. int ret = -1;
  1080. #if defined(CONFIG_SEC_MILLET_PROJECT) \
  1081. || defined(CONFIG_SEC_BERLUTI_PROJECT) \
  1082. || defined(CONFIG_SEC_DEGAS_PROJECT) \
  1083. || defined(CONFIG_SEC_T8_PROJECT)
  1084. if (!data->L19) {
  1085. data->L19 = regulator_get(&data->client->dev, "8226_l19");
  1086. if (!data->L19) {
  1087. pr_err("%s: regulator pointer null L19, rc=%d\n",
  1088. __func__, ret);
  1089. return ret;
  1090. }
  1091. ret = regulator_set_voltage(data->L19, 2850000, 2850000);
  1092. if (ret) {
  1093. pr_err("%s: set voltage failed on L19, rc=%d\n",
  1094. __func__, ret);
  1095. return ret;
  1096. }
  1097. }
  1098. #endif
  1099. #if defined(CONFIG_SEC_MATISSE_PROJECT) || defined(CONFIG_SEC_T10_PROJECT)
  1100. if (!data->L19) {
  1101. data->L19 = regulator_get(&data->client->dev, "8226_l15");
  1102. if (!data->L19) {
  1103. pr_err("%s: regulator pointer null L19, rc=%d\n",
  1104. __func__, ret);
  1105. return ret;
  1106. }
  1107. ret = regulator_set_voltage(data->L19, 2800000, 2800000);
  1108. if (ret) {
  1109. pr_err("%s: set voltage failed on L19, rc=%d\n",
  1110. __func__, ret);
  1111. return ret;
  1112. }
  1113. }
  1114. #endif
  1115. if (!data->lvs1_1p8) {
  1116. data->lvs1_1p8 = regulator_get(&data->client->dev, "8226_lvs1");
  1117. if (!data->lvs1_1p8) {
  1118. pr_err("%s: regulator_get for 8226_lvs1 failed\n",
  1119. __func__);
  1120. return 0;
  1121. }
  1122. }
  1123. if (onoff) {
  1124. ret = regulator_enable(data->L19);
  1125. if (ret) {
  1126. pr_err("%s: Failed to enable regulator l19.\n",
  1127. __func__);
  1128. return ret;
  1129. }
  1130. ret = regulator_enable(data->lvs1_1p8);
  1131. if (ret) {
  1132. pr_err("%s: Failed to enable regulator lvs1_1p8.\n",
  1133. __func__);
  1134. return ret;
  1135. }
  1136. } else {
  1137. ret = regulator_disable(data->L19);
  1138. if (ret) {
  1139. pr_err("%s: Failed to disable regulatorl19.\n",
  1140. __func__);
  1141. return ret;
  1142. }
  1143. ret = regulator_disable(data->lvs1_1p8);
  1144. if (ret) {
  1145. pr_err("%s: Failed to disable regulator lvs1_1p8.\n",
  1146. __func__);
  1147. return ret;
  1148. }
  1149. }
  1150. /*Delay added for wakeup of chip, before i2c-transactions */
  1151. msleep(30);
  1152. return 0;
  1153. }
  1154. #endif
  1155. static int sx9500_probe(struct i2c_client *client,
  1156. const struct i2c_device_id *id)
  1157. {
  1158. int ret = -ENODEV;
  1159. struct sx9500_p *data = NULL;
  1160. pr_info("[SX9500]: %s - Probe Start!\n", __func__);
  1161. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1162. pr_err("[SX9500]: %s - i2c_check_functionality error\n",
  1163. __func__);
  1164. goto exit;
  1165. }
  1166. /* create memory for main struct */
  1167. data = kzalloc(sizeof(struct sx9500_p), GFP_KERNEL);
  1168. if (data == NULL) {
  1169. pr_err("[SX9500]: %s - kzalloc error\n", __func__);
  1170. ret = -ENOMEM;
  1171. goto exit_kzalloc;
  1172. }
  1173. ret = sx9500_parse_dt(data, &client->dev);
  1174. if (ret < 0) {
  1175. pr_err("[SX9500]: %s - of_node error\n", __func__);
  1176. ret = -ENODEV;
  1177. goto exit_of_node;
  1178. }
  1179. ret = sx9500_setup_pin(data);
  1180. if (ret) {
  1181. pr_err("[SX9500]: %s - could not setup pin\n", __func__);
  1182. goto exit_setup_pin;
  1183. }
  1184. i2c_set_clientdata(client, data);
  1185. data->client = client;
  1186. #if defined(CONFIG_SENSORS_SX9500_REGULATOR_ONOFF)
  1187. sx9500_regulator_on(data,1);
  1188. #endif
  1189. #if defined(CONFIG_SEC_MILLET_PROJECT) \
  1190. || defined(CONFIG_SEC_MATISSE_PROJECT) \
  1191. || defined(CONFIG_SEC_BERLUTI_PROJECT) \
  1192. || defined(CONFIG_SEC_DEGAS_PROJECT) \
  1193. || defined(CONFIG_SEC_T8_PROJECT) \
  1194. || defined(CONFIG_SEC_T10_PROJECT)
  1195. sx9500_power_on(data, 1);
  1196. #endif
  1197. /* read chip id */
  1198. ret = sx9500_i2c_write(data, SX9500_SOFTRESET_REG, SX9500_SOFTRESET);
  1199. if (ret < 0) {
  1200. pr_err("[SX9500]: %s - chip reset failed %d\n", __func__, ret);
  1201. goto exit_chip_reset;
  1202. }
  1203. ret = sx9500_input_init(data);
  1204. if (ret < 0)
  1205. goto exit_input_init;
  1206. wake_lock_init(&data->grip_wake_lock,
  1207. WAKE_LOCK_SUSPEND, "grip_wake_lock");
  1208. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  1209. sx9500_initialize_variable(data);
  1210. INIT_DELAYED_WORK(&data->init_work, sx9500_init_work_func);
  1211. INIT_DELAYED_WORK(&data->irq_work, sx9500_irq_work_func);
  1212. mutex_init(&data->mode_mutex);
  1213. data->irq = gpio_to_irq(data->gpioNirq);
  1214. /* initailize interrupt reporting */
  1215. ret = request_threaded_irq(data->irq, NULL, sx9500_interrupt_thread,
  1216. IRQF_TRIGGER_FALLING , "sx9500_irq", data);
  1217. if (ret < 0) {
  1218. pr_err("[SX9500]: %s - failed to set request_threaded_irq %d" \
  1219. " as returning (%d)\n", __func__, data->irq, ret);
  1220. goto exit_request_threaded_irq;
  1221. }
  1222. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  1223. if (data->enable_adjdet == 1) {
  1224. data->irq_adjdet = gpio_to_irq(data->gpio_adjdet);
  1225. /* initailize interrupt reporting */
  1226. ret = request_threaded_irq(data->irq_adjdet, NULL,
  1227. sx9500_adjdet_interrupt_thread,
  1228. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  1229. "sx9500_adjdet_irq", data);
  1230. if (ret < 0) {
  1231. pr_err("[SX9500]: %s - failed to set" \
  1232. " request_threaded_adjdet_irq %d" \
  1233. " as returning (%d)\n",
  1234. __func__, data->irq_adjdet, ret);
  1235. goto exit_request_threaded_adjdet_irq;
  1236. }
  1237. data->grip_state = IDLE;
  1238. }
  1239. #endif
  1240. disable_irq(data->irq);
  1241. schedule_delayed_work(&data->init_work, msecs_to_jiffies(300));
  1242. pr_info("[SX9500]: %s - Probe done!\n", __func__);
  1243. return 0;
  1244. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  1245. exit_request_threaded_adjdet_irq:
  1246. free_irq(data->irq, data);
  1247. #endif
  1248. exit_request_threaded_irq:
  1249. mutex_destroy(&data->mode_mutex);
  1250. sensors_unregister(data->factory_device, sensor_attrs);
  1251. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1252. wake_lock_destroy(&data->grip_wake_lock);
  1253. sysfs_remove_group(&data->input->dev.kobj, &sx9500_attribute_group);
  1254. input_unregister_device(data->input);
  1255. exit_input_init:
  1256. exit_chip_reset:
  1257. gpio_free(data->gpioNirq);
  1258. exit_setup_pin:
  1259. exit_of_node:
  1260. kfree(data);
  1261. exit_kzalloc:
  1262. exit:
  1263. pr_err("[SX9500]: %s - Probe fail!\n", __func__);
  1264. return ret;
  1265. }
  1266. static int __devexit sx9500_remove(struct i2c_client *client)
  1267. {
  1268. struct sx9500_p *data = (struct sx9500_p *)i2c_get_clientdata(client);
  1269. if (atomic_read(&data->enable) == ON)
  1270. sx9500_set_mode(data, SX9500_MODE_SLEEP);
  1271. cancel_delayed_work_sync(&data->init_work);
  1272. cancel_delayed_work_sync(&data->irq_work);
  1273. free_irq(data->irq, data);
  1274. #ifdef CONFIG_SENSORS_GRIP_ADJDET
  1275. if (data->enable_adjdet == 1)
  1276. free_irq(data->irq_adjdet, data);
  1277. #endif
  1278. gpio_free(data->gpioNirq);
  1279. wake_lock_destroy(&data->grip_wake_lock);
  1280. sensors_unregister(data->factory_device, sensor_attrs);
  1281. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  1282. sysfs_remove_group(&data->input->dev.kobj, &sx9500_attribute_group);
  1283. input_unregister_device(data->input);
  1284. mutex_destroy(&data->mode_mutex);
  1285. kfree(data);
  1286. return 0;
  1287. }
  1288. static int sx9500_suspend(struct device *dev)
  1289. {
  1290. struct sx9500_p *data = dev_get_drvdata(dev);
  1291. if (atomic_read(&data->enable) == ON)
  1292. pr_info("[SX9500]: %s\n", __func__);
  1293. return 0;
  1294. }
  1295. static int sx9500_resume(struct device *dev)
  1296. {
  1297. struct sx9500_p *data = dev_get_drvdata(dev);
  1298. if (atomic_read(&data->enable) == ON)
  1299. pr_info("[SX9500]: %s\n", __func__);
  1300. return 0;
  1301. }
  1302. static struct of_device_id sx9500_match_table[] = {
  1303. { .compatible = "sx9500-i2c",},
  1304. {},
  1305. };
  1306. static const struct i2c_device_id sx9500_id[] = {
  1307. { "sx9500_match_table", 0 },
  1308. { }
  1309. };
  1310. static const struct dev_pm_ops sx9500_pm_ops = {
  1311. .suspend = sx9500_suspend,
  1312. .resume = sx9500_resume,
  1313. };
  1314. static struct i2c_driver sx9500_driver = {
  1315. .driver = {
  1316. .name = MODEL_NAME,
  1317. .owner = THIS_MODULE,
  1318. .of_match_table = sx9500_match_table,
  1319. .pm = &sx9500_pm_ops
  1320. },
  1321. .probe = sx9500_probe,
  1322. .remove = __devexit_p(sx9500_remove),
  1323. .id_table = sx9500_id,
  1324. };
  1325. static int __init sx9500_init(void)
  1326. {
  1327. return i2c_add_driver(&sx9500_driver);
  1328. }
  1329. static void __exit sx9500_exit(void)
  1330. {
  1331. i2c_del_driver(&sx9500_driver);
  1332. }
  1333. module_init(sx9500_init);
  1334. module_exit(sx9500_exit);
  1335. MODULE_DESCRIPTION("Semtech Corp. SX9500 Capacitive Touch Controller Driver");
  1336. MODULE_AUTHOR("Samsung Electronics");
  1337. MODULE_LICENSE("GPL");