elan_i2c_core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * Elan I2C/SMBus Touchpad driver
  3. *
  4. * Copyright (c) 2013 ELAN Microelectronics Corp.
  5. *
  6. * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
  7. * Version: 1.5.9
  8. *
  9. * Based on cyapa driver:
  10. * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
  11. * copyright (c) 2011-2012 Google, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2 as published
  15. * by the Free Software Foundation.
  16. *
  17. * Trademarks are the property of their respective owners.
  18. */
  19. #include <linux/acpi.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/firmware.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/input/mt.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/input.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/completion.h>
  35. #include <linux/of.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <asm/unaligned.h>
  38. #include "elan_i2c.h"
  39. #define DRIVER_NAME "elan_i2c"
  40. #define ELAN_DRIVER_VERSION "1.5.9"
  41. #define ETP_MAX_PRESSURE 255
  42. #define ETP_FWIDTH_REDUCE 90
  43. #define ETP_FINGER_WIDTH 15
  44. #define ETP_RETRY_COUNT 3
  45. #define ETP_MAX_FINGERS 5
  46. #define ETP_FINGER_DATA_LEN 5
  47. #define ETP_REPORT_ID 0x5D
  48. #define ETP_REPORT_ID_OFFSET 2
  49. #define ETP_TOUCH_INFO_OFFSET 3
  50. #define ETP_FINGER_DATA_OFFSET 4
  51. #define ETP_HOVER_INFO_OFFSET 30
  52. #define ETP_MAX_REPORT_LEN 34
  53. /* The main device structure */
  54. struct elan_tp_data {
  55. struct i2c_client *client;
  56. struct input_dev *input;
  57. struct regulator *vcc;
  58. const struct elan_transport_ops *ops;
  59. /* for fw update */
  60. struct completion fw_completion;
  61. bool in_fw_update;
  62. struct mutex sysfs_mutex;
  63. unsigned int max_x;
  64. unsigned int max_y;
  65. unsigned int width_x;
  66. unsigned int width_y;
  67. unsigned int x_res;
  68. unsigned int y_res;
  69. u8 product_id;
  70. u8 fw_version;
  71. u8 sm_version;
  72. u8 iap_version;
  73. u16 fw_checksum;
  74. int pressure_adjustment;
  75. u8 mode;
  76. u8 ic_type;
  77. u16 fw_vaildpage_count;
  78. u16 fw_signature_address;
  79. bool irq_wake;
  80. u8 min_baseline;
  81. u8 max_baseline;
  82. bool baseline_ready;
  83. };
  84. static int elan_get_fwinfo(u8 ic_type, u16 *vaildpage_count,
  85. u16 *signature_address)
  86. {
  87. switch(ic_type) {
  88. case 0x09:
  89. *vaildpage_count = 768;
  90. break;
  91. case 0x0D:
  92. *vaildpage_count = 896;
  93. break;
  94. default:
  95. /* unknown ic type clear value */
  96. *vaildpage_count = 0;
  97. *signature_address = 0;
  98. return -ENXIO;
  99. }
  100. *signature_address =
  101. (*vaildpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
  102. return 0;
  103. }
  104. static int elan_enable_power(struct elan_tp_data *data)
  105. {
  106. int repeat = ETP_RETRY_COUNT;
  107. int error;
  108. error = regulator_enable(data->vcc);
  109. if (error) {
  110. dev_err(&data->client->dev,
  111. "failed to enable regulator: %d\n", error);
  112. return error;
  113. }
  114. do {
  115. error = data->ops->power_control(data->client, true);
  116. if (error >= 0)
  117. return 0;
  118. msleep(30);
  119. } while (--repeat > 0);
  120. dev_err(&data->client->dev, "failed to enable power: %d\n", error);
  121. return error;
  122. }
  123. static int elan_disable_power(struct elan_tp_data *data)
  124. {
  125. int repeat = ETP_RETRY_COUNT;
  126. int error;
  127. do {
  128. error = data->ops->power_control(data->client, false);
  129. if (!error) {
  130. error = regulator_disable(data->vcc);
  131. if (error) {
  132. dev_err(&data->client->dev,
  133. "failed to disable regulator: %d\n",
  134. error);
  135. /* Attempt to power the chip back up */
  136. data->ops->power_control(data->client, true);
  137. break;
  138. }
  139. return 0;
  140. }
  141. msleep(30);
  142. } while (--repeat > 0);
  143. dev_err(&data->client->dev, "failed to disable power: %d\n", error);
  144. return error;
  145. }
  146. static int elan_sleep(struct elan_tp_data *data)
  147. {
  148. int repeat = ETP_RETRY_COUNT;
  149. int error;
  150. do {
  151. error = data->ops->sleep_control(data->client, true);
  152. if (!error)
  153. return 0;
  154. msleep(30);
  155. } while (--repeat > 0);
  156. return error;
  157. }
  158. static int __elan_initialize(struct elan_tp_data *data)
  159. {
  160. struct i2c_client *client = data->client;
  161. int error;
  162. error = data->ops->initialize(client);
  163. if (error) {
  164. dev_err(&client->dev, "device initialize failed: %d\n", error);
  165. return error;
  166. }
  167. data->mode |= ETP_ENABLE_ABS;
  168. error = data->ops->set_mode(client, data->mode);
  169. if (error) {
  170. dev_err(&client->dev,
  171. "failed to switch to absolute mode: %d\n", error);
  172. return error;
  173. }
  174. error = data->ops->sleep_control(client, false);
  175. if (error) {
  176. dev_err(&client->dev,
  177. "failed to wake device up: %d\n", error);
  178. return error;
  179. }
  180. return 0;
  181. }
  182. static int elan_initialize(struct elan_tp_data *data)
  183. {
  184. int repeat = ETP_RETRY_COUNT;
  185. int error;
  186. do {
  187. error = __elan_initialize(data);
  188. if (!error)
  189. return 0;
  190. msleep(30);
  191. } while (--repeat > 0);
  192. return error;
  193. }
  194. static int elan_query_device_info(struct elan_tp_data *data)
  195. {
  196. int error;
  197. error = data->ops->get_product_id(data->client, &data->product_id);
  198. if (error)
  199. return error;
  200. error = data->ops->get_version(data->client, false, &data->fw_version);
  201. if (error)
  202. return error;
  203. error = data->ops->get_checksum(data->client, false,
  204. &data->fw_checksum);
  205. if (error)
  206. return error;
  207. error = data->ops->get_sm_version(data->client, &data->ic_type,
  208. &data->sm_version);
  209. if (error)
  210. return error;
  211. error = data->ops->get_version(data->client, true, &data->iap_version);
  212. if (error)
  213. return error;
  214. error = data->ops->get_pressure_adjustment(data->client,
  215. &data->pressure_adjustment);
  216. if (error)
  217. return error;
  218. error = elan_get_fwinfo(data->ic_type, &data->fw_vaildpage_count,
  219. &data->fw_signature_address);
  220. if (error) {
  221. dev_err(&data->client->dev,
  222. "unknown ic type %d\n", data->ic_type);
  223. return error;
  224. }
  225. return 0;
  226. }
  227. static unsigned int elan_convert_resolution(u8 val)
  228. {
  229. /*
  230. * (value from firmware) * 10 + 790 = dpi
  231. *
  232. * We also have to convert dpi to dots/mm (*10/254 to avoid floating
  233. * point).
  234. */
  235. return ((int)(char)val * 10 + 790) * 10 / 254;
  236. }
  237. static int elan_query_device_parameters(struct elan_tp_data *data)
  238. {
  239. unsigned int x_traces, y_traces;
  240. u8 hw_x_res, hw_y_res;
  241. int error;
  242. error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
  243. if (error)
  244. return error;
  245. error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
  246. if (error)
  247. return error;
  248. data->width_x = data->max_x / x_traces;
  249. data->width_y = data->max_y / y_traces;
  250. error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
  251. if (error)
  252. return error;
  253. data->x_res = elan_convert_resolution(hw_x_res);
  254. data->y_res = elan_convert_resolution(hw_y_res);
  255. return 0;
  256. }
  257. /*
  258. **********************************************************
  259. * IAP firmware updater related routines
  260. **********************************************************
  261. */
  262. static int elan_write_fw_block(struct elan_tp_data *data,
  263. const u8 *page, u16 checksum, int idx)
  264. {
  265. int retry = ETP_RETRY_COUNT;
  266. int error;
  267. do {
  268. error = data->ops->write_fw_block(data->client,
  269. page, checksum, idx);
  270. if (!error)
  271. return 0;
  272. dev_dbg(&data->client->dev,
  273. "IAP retrying page %d (error: %d)\n", idx, error);
  274. } while (--retry > 0);
  275. return error;
  276. }
  277. static int __elan_update_firmware(struct elan_tp_data *data,
  278. const struct firmware *fw)
  279. {
  280. struct i2c_client *client = data->client;
  281. struct device *dev = &client->dev;
  282. int i, j;
  283. int error;
  284. u16 iap_start_addr;
  285. u16 boot_page_count;
  286. u16 sw_checksum = 0, fw_checksum = 0;
  287. error = data->ops->prepare_fw_update(client);
  288. if (error)
  289. return error;
  290. iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
  291. boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
  292. for (i = boot_page_count; i < data->fw_vaildpage_count; i++) {
  293. u16 checksum = 0;
  294. const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
  295. for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
  296. checksum += ((page[j + 1] << 8) | page[j]);
  297. error = elan_write_fw_block(data, page, checksum, i);
  298. if (error) {
  299. dev_err(dev, "write page %d fail: %d\n", i, error);
  300. return error;
  301. }
  302. sw_checksum += checksum;
  303. }
  304. /* Wait WDT reset and power on reset */
  305. msleep(600);
  306. error = data->ops->finish_fw_update(client, &data->fw_completion);
  307. if (error)
  308. return error;
  309. error = data->ops->get_checksum(client, true, &fw_checksum);
  310. if (error)
  311. return error;
  312. if (sw_checksum != fw_checksum) {
  313. dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
  314. sw_checksum, fw_checksum);
  315. return -EIO;
  316. }
  317. return 0;
  318. }
  319. static int elan_update_firmware(struct elan_tp_data *data,
  320. const struct firmware *fw)
  321. {
  322. struct i2c_client *client = data->client;
  323. int retval;
  324. dev_dbg(&client->dev, "Starting firmware update....\n");
  325. disable_irq(client->irq);
  326. data->in_fw_update = true;
  327. retval = __elan_update_firmware(data, fw);
  328. if (retval) {
  329. dev_err(&client->dev, "firmware update failed: %d\n", retval);
  330. data->ops->iap_reset(client);
  331. } else {
  332. /* Reinitialize TP after fw is updated */
  333. elan_initialize(data);
  334. elan_query_device_info(data);
  335. }
  336. data->in_fw_update = false;
  337. enable_irq(client->irq);
  338. return retval;
  339. }
  340. /*
  341. *******************************************************************
  342. * SYSFS attributes
  343. *******************************************************************
  344. */
  345. static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
  346. struct device_attribute *attr,
  347. char *buf)
  348. {
  349. struct i2c_client *client = to_i2c_client(dev);
  350. struct elan_tp_data *data = i2c_get_clientdata(client);
  351. return sprintf(buf, "0x%04x\n", data->fw_checksum);
  352. }
  353. static ssize_t elan_sysfs_read_product_id(struct device *dev,
  354. struct device_attribute *attr,
  355. char *buf)
  356. {
  357. struct i2c_client *client = to_i2c_client(dev);
  358. struct elan_tp_data *data = i2c_get_clientdata(client);
  359. return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
  360. data->product_id);
  361. }
  362. static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
  363. struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct i2c_client *client = to_i2c_client(dev);
  367. struct elan_tp_data *data = i2c_get_clientdata(client);
  368. return sprintf(buf, "%d.0\n", data->fw_version);
  369. }
  370. static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
  371. struct device_attribute *attr,
  372. char *buf)
  373. {
  374. struct i2c_client *client = to_i2c_client(dev);
  375. struct elan_tp_data *data = i2c_get_clientdata(client);
  376. return sprintf(buf, "%d.0\n", data->sm_version);
  377. }
  378. static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
  379. struct device_attribute *attr,
  380. char *buf)
  381. {
  382. struct i2c_client *client = to_i2c_client(dev);
  383. struct elan_tp_data *data = i2c_get_clientdata(client);
  384. return sprintf(buf, "%d.0\n", data->iap_version);
  385. }
  386. static ssize_t elan_sysfs_update_fw(struct device *dev,
  387. struct device_attribute *attr,
  388. const char *buf, size_t count)
  389. {
  390. struct elan_tp_data *data = dev_get_drvdata(dev);
  391. const struct firmware *fw;
  392. char *fw_name;
  393. int error;
  394. const u8 *fw_signature;
  395. static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
  396. /* Look for a firmware with the product id appended. */
  397. fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
  398. if (!fw_name) {
  399. dev_err(dev, "failed to allocate memory for firmware name\n");
  400. return -ENOMEM;
  401. }
  402. dev_info(dev, "requesting fw '%s'\n", fw_name);
  403. error = reject_firmware(&fw, fw_name, dev);
  404. kfree(fw_name);
  405. if (error) {
  406. dev_err(dev, "failed to request firmware: %d\n", error);
  407. return error;
  408. }
  409. /* Firmware file must match signature data */
  410. fw_signature = &fw->data[data->fw_signature_address];
  411. if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
  412. dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
  413. (int)sizeof(signature), signature,
  414. (int)sizeof(signature), fw_signature);
  415. error = -EBADF;
  416. goto out_release_fw;
  417. }
  418. error = mutex_lock_interruptible(&data->sysfs_mutex);
  419. if (error)
  420. goto out_release_fw;
  421. error = elan_update_firmware(data, fw);
  422. mutex_unlock(&data->sysfs_mutex);
  423. out_release_fw:
  424. release_firmware(fw);
  425. return error ?: count;
  426. }
  427. static ssize_t calibrate_store(struct device *dev,
  428. struct device_attribute *attr,
  429. const char *buf, size_t count)
  430. {
  431. struct i2c_client *client = to_i2c_client(dev);
  432. struct elan_tp_data *data = i2c_get_clientdata(client);
  433. int tries = 20;
  434. int retval;
  435. int error;
  436. u8 val[3];
  437. retval = mutex_lock_interruptible(&data->sysfs_mutex);
  438. if (retval)
  439. return retval;
  440. disable_irq(client->irq);
  441. data->mode |= ETP_ENABLE_CALIBRATE;
  442. retval = data->ops->set_mode(client, data->mode);
  443. if (retval) {
  444. dev_err(dev, "failed to enable calibration mode: %d\n",
  445. retval);
  446. goto out;
  447. }
  448. retval = data->ops->calibrate(client);
  449. if (retval) {
  450. dev_err(dev, "failed to start calibration: %d\n",
  451. retval);
  452. goto out_disable_calibrate;
  453. }
  454. val[0] = 0xff;
  455. do {
  456. /* Wait 250ms before checking if calibration has completed. */
  457. msleep(250);
  458. retval = data->ops->calibrate_result(client, val);
  459. if (retval)
  460. dev_err(dev, "failed to check calibration result: %d\n",
  461. retval);
  462. else if (val[0] == 0)
  463. break; /* calibration done */
  464. } while (--tries);
  465. if (tries == 0) {
  466. dev_err(dev, "failed to calibrate. Timeout.\n");
  467. retval = -ETIMEDOUT;
  468. }
  469. out_disable_calibrate:
  470. data->mode &= ~ETP_ENABLE_CALIBRATE;
  471. error = data->ops->set_mode(data->client, data->mode);
  472. if (error) {
  473. dev_err(dev, "failed to disable calibration mode: %d\n",
  474. error);
  475. if (!retval)
  476. retval = error;
  477. }
  478. out:
  479. enable_irq(client->irq);
  480. mutex_unlock(&data->sysfs_mutex);
  481. return retval ?: count;
  482. }
  483. static ssize_t elan_sysfs_read_mode(struct device *dev,
  484. struct device_attribute *attr,
  485. char *buf)
  486. {
  487. struct i2c_client *client = to_i2c_client(dev);
  488. struct elan_tp_data *data = i2c_get_clientdata(client);
  489. int error;
  490. enum tp_mode mode;
  491. error = mutex_lock_interruptible(&data->sysfs_mutex);
  492. if (error)
  493. return error;
  494. error = data->ops->iap_get_mode(data->client, &mode);
  495. mutex_unlock(&data->sysfs_mutex);
  496. if (error)
  497. return error;
  498. return sprintf(buf, "%d\n", (int)mode);
  499. }
  500. static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
  501. static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
  502. static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
  503. static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
  504. static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
  505. static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
  506. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
  507. static DEVICE_ATTR_WO(calibrate);
  508. static struct attribute *elan_sysfs_entries[] = {
  509. &dev_attr_product_id.attr,
  510. &dev_attr_firmware_version.attr,
  511. &dev_attr_sample_version.attr,
  512. &dev_attr_iap_version.attr,
  513. &dev_attr_fw_checksum.attr,
  514. &dev_attr_calibrate.attr,
  515. &dev_attr_mode.attr,
  516. &dev_attr_update_fw.attr,
  517. NULL,
  518. };
  519. static const struct attribute_group elan_sysfs_group = {
  520. .attrs = elan_sysfs_entries,
  521. };
  522. static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
  523. const char *buf, size_t count)
  524. {
  525. struct i2c_client *client = to_i2c_client(dev);
  526. struct elan_tp_data *data = i2c_get_clientdata(client);
  527. int error;
  528. int retval;
  529. retval = mutex_lock_interruptible(&data->sysfs_mutex);
  530. if (retval)
  531. return retval;
  532. disable_irq(client->irq);
  533. data->baseline_ready = false;
  534. data->mode |= ETP_ENABLE_CALIBRATE;
  535. retval = data->ops->set_mode(data->client, data->mode);
  536. if (retval) {
  537. dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
  538. retval);
  539. goto out;
  540. }
  541. msleep(250);
  542. retval = data->ops->get_baseline_data(data->client, true,
  543. &data->max_baseline);
  544. if (retval) {
  545. dev_err(dev, "Failed to read max baseline form device: %d\n",
  546. retval);
  547. goto out_disable_calibrate;
  548. }
  549. retval = data->ops->get_baseline_data(data->client, false,
  550. &data->min_baseline);
  551. if (retval) {
  552. dev_err(dev, "Failed to read min baseline form device: %d\n",
  553. retval);
  554. goto out_disable_calibrate;
  555. }
  556. data->baseline_ready = true;
  557. out_disable_calibrate:
  558. data->mode &= ~ETP_ENABLE_CALIBRATE;
  559. error = data->ops->set_mode(data->client, data->mode);
  560. if (error) {
  561. dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
  562. error);
  563. if (!retval)
  564. retval = error;
  565. }
  566. out:
  567. enable_irq(client->irq);
  568. mutex_unlock(&data->sysfs_mutex);
  569. return retval ?: count;
  570. }
  571. static ssize_t min_show(struct device *dev,
  572. struct device_attribute *attr, char *buf)
  573. {
  574. struct i2c_client *client = to_i2c_client(dev);
  575. struct elan_tp_data *data = i2c_get_clientdata(client);
  576. int retval;
  577. retval = mutex_lock_interruptible(&data->sysfs_mutex);
  578. if (retval)
  579. return retval;
  580. if (!data->baseline_ready) {
  581. retval = -ENODATA;
  582. goto out;
  583. }
  584. retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
  585. out:
  586. mutex_unlock(&data->sysfs_mutex);
  587. return retval;
  588. }
  589. static ssize_t max_show(struct device *dev,
  590. struct device_attribute *attr, char *buf)
  591. {
  592. struct i2c_client *client = to_i2c_client(dev);
  593. struct elan_tp_data *data = i2c_get_clientdata(client);
  594. int retval;
  595. retval = mutex_lock_interruptible(&data->sysfs_mutex);
  596. if (retval)
  597. return retval;
  598. if (!data->baseline_ready) {
  599. retval = -ENODATA;
  600. goto out;
  601. }
  602. retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
  603. out:
  604. mutex_unlock(&data->sysfs_mutex);
  605. return retval;
  606. }
  607. static DEVICE_ATTR_WO(acquire);
  608. static DEVICE_ATTR_RO(min);
  609. static DEVICE_ATTR_RO(max);
  610. static struct attribute *elan_baseline_sysfs_entries[] = {
  611. &dev_attr_acquire.attr,
  612. &dev_attr_min.attr,
  613. &dev_attr_max.attr,
  614. NULL,
  615. };
  616. static const struct attribute_group elan_baseline_sysfs_group = {
  617. .name = "baseline",
  618. .attrs = elan_baseline_sysfs_entries,
  619. };
  620. static const struct attribute_group *elan_sysfs_groups[] = {
  621. &elan_sysfs_group,
  622. &elan_baseline_sysfs_group,
  623. NULL
  624. };
  625. /*
  626. ******************************************************************
  627. * Elan isr functions
  628. ******************************************************************
  629. */
  630. static void elan_report_contact(struct elan_tp_data *data,
  631. int contact_num, bool contact_valid,
  632. bool hover_event, u8 *finger_data)
  633. {
  634. struct input_dev *input = data->input;
  635. unsigned int pos_x, pos_y;
  636. unsigned int pressure, mk_x, mk_y;
  637. unsigned int area_x, area_y, major, minor;
  638. unsigned int scaled_pressure;
  639. if (contact_valid) {
  640. pos_x = ((finger_data[0] & 0xf0) << 4) |
  641. finger_data[1];
  642. pos_y = ((finger_data[0] & 0x0f) << 8) |
  643. finger_data[2];
  644. mk_x = (finger_data[3] & 0x0f);
  645. mk_y = (finger_data[3] >> 4);
  646. pressure = finger_data[4];
  647. if (pos_x > data->max_x || pos_y > data->max_y) {
  648. dev_dbg(input->dev.parent,
  649. "[%d] x=%d y=%d over max (%d, %d)",
  650. contact_num, pos_x, pos_y,
  651. data->max_x, data->max_y);
  652. return;
  653. }
  654. /*
  655. * To avoid treating large finger as palm, let's reduce the
  656. * width x and y per trace.
  657. */
  658. area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
  659. area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
  660. major = max(area_x, area_y);
  661. minor = min(area_x, area_y);
  662. scaled_pressure = pressure + data->pressure_adjustment;
  663. if (scaled_pressure > ETP_MAX_PRESSURE)
  664. scaled_pressure = ETP_MAX_PRESSURE;
  665. input_mt_slot(input, contact_num);
  666. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  667. input_report_abs(input, ABS_MT_POSITION_X, pos_x);
  668. input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
  669. input_report_abs(input, ABS_MT_DISTANCE, hover_event);
  670. input_report_abs(input, ABS_MT_PRESSURE,
  671. hover_event ? 0 : scaled_pressure);
  672. input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
  673. input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
  674. input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
  675. } else {
  676. input_mt_slot(input, contact_num);
  677. input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
  678. }
  679. }
  680. static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
  681. {
  682. struct input_dev *input = data->input;
  683. u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
  684. int i;
  685. u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
  686. u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
  687. bool contact_valid, hover_event;
  688. hover_event = hover_info & 0x40;
  689. for (i = 0; i < ETP_MAX_FINGERS; i++) {
  690. contact_valid = tp_info & (1U << (3 + i));
  691. elan_report_contact(data, i, contact_valid, hover_event,
  692. finger_data);
  693. if (contact_valid)
  694. finger_data += ETP_FINGER_DATA_LEN;
  695. }
  696. input_report_key(input, BTN_LEFT, tp_info & 0x01);
  697. input_mt_report_pointer_emulation(input, true);
  698. input_sync(input);
  699. }
  700. static irqreturn_t elan_isr(int irq, void *dev_id)
  701. {
  702. struct elan_tp_data *data = dev_id;
  703. struct device *dev = &data->client->dev;
  704. int error;
  705. u8 report[ETP_MAX_REPORT_LEN];
  706. /*
  707. * When device is connected to i2c bus, when all IAP page writes
  708. * complete, the driver will receive interrupt and must read
  709. * 0000 to confirm that IAP is finished.
  710. */
  711. if (data->in_fw_update) {
  712. complete(&data->fw_completion);
  713. goto out;
  714. }
  715. error = data->ops->get_report(data->client, report);
  716. if (error)
  717. goto out;
  718. if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
  719. dev_err(dev, "invalid report id data (%x)\n",
  720. report[ETP_REPORT_ID_OFFSET]);
  721. else
  722. elan_report_absolute(data, report);
  723. out:
  724. return IRQ_HANDLED;
  725. }
  726. /*
  727. ******************************************************************
  728. * Elan initialization functions
  729. ******************************************************************
  730. */
  731. static int elan_setup_input_device(struct elan_tp_data *data)
  732. {
  733. struct device *dev = &data->client->dev;
  734. struct input_dev *input;
  735. unsigned int max_width = max(data->width_x, data->width_y);
  736. unsigned int min_width = min(data->width_x, data->width_y);
  737. int error;
  738. input = devm_input_allocate_device(dev);
  739. if (!input)
  740. return -ENOMEM;
  741. input->name = "Elan Touchpad";
  742. input->id.bustype = BUS_I2C;
  743. input_set_drvdata(input, data);
  744. error = input_mt_init_slots(input, ETP_MAX_FINGERS,
  745. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
  746. if (error) {
  747. dev_err(dev, "failed to initialize MT slots: %d\n", error);
  748. return error;
  749. }
  750. __set_bit(EV_ABS, input->evbit);
  751. __set_bit(INPUT_PROP_POINTER, input->propbit);
  752. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  753. __set_bit(BTN_LEFT, input->keybit);
  754. /* Set up ST parameters */
  755. input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
  756. input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
  757. input_abs_set_res(input, ABS_X, data->x_res);
  758. input_abs_set_res(input, ABS_Y, data->y_res);
  759. input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
  760. input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
  761. /* And MT parameters */
  762. input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
  763. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
  764. input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
  765. input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
  766. input_set_abs_params(input, ABS_MT_PRESSURE, 0,
  767. ETP_MAX_PRESSURE, 0, 0);
  768. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
  769. ETP_FINGER_WIDTH * max_width, 0, 0);
  770. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
  771. ETP_FINGER_WIDTH * min_width, 0, 0);
  772. input_set_abs_params(input, ABS_MT_DISTANCE, 0, 1, 0, 0);
  773. data->input = input;
  774. return 0;
  775. }
  776. static void elan_disable_regulator(void *_data)
  777. {
  778. struct elan_tp_data *data = _data;
  779. regulator_disable(data->vcc);
  780. }
  781. static void elan_remove_sysfs_groups(void *_data)
  782. {
  783. struct elan_tp_data *data = _data;
  784. sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
  785. }
  786. static int elan_probe(struct i2c_client *client,
  787. const struct i2c_device_id *dev_id)
  788. {
  789. const struct elan_transport_ops *transport_ops;
  790. struct device *dev = &client->dev;
  791. struct elan_tp_data *data;
  792. unsigned long irqflags;
  793. int error;
  794. if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
  795. i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  796. transport_ops = &elan_i2c_ops;
  797. } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
  798. i2c_check_functionality(client->adapter,
  799. I2C_FUNC_SMBUS_BYTE_DATA |
  800. I2C_FUNC_SMBUS_BLOCK_DATA |
  801. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  802. transport_ops = &elan_smbus_ops;
  803. } else {
  804. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  805. return -EIO;
  806. }
  807. data = devm_kzalloc(&client->dev, sizeof(struct elan_tp_data),
  808. GFP_KERNEL);
  809. if (!data)
  810. return -ENOMEM;
  811. i2c_set_clientdata(client, data);
  812. data->ops = transport_ops;
  813. data->client = client;
  814. init_completion(&data->fw_completion);
  815. mutex_init(&data->sysfs_mutex);
  816. data->vcc = devm_regulator_get(&client->dev, "vcc");
  817. if (IS_ERR(data->vcc)) {
  818. error = PTR_ERR(data->vcc);
  819. if (error != -EPROBE_DEFER)
  820. dev_err(&client->dev,
  821. "Failed to get 'vcc' regulator: %d\n",
  822. error);
  823. return error;
  824. }
  825. error = regulator_enable(data->vcc);
  826. if (error) {
  827. dev_err(&client->dev,
  828. "Failed to enable regulator: %d\n", error);
  829. return error;
  830. }
  831. error = devm_add_action(&client->dev,
  832. elan_disable_regulator, data);
  833. if (error) {
  834. regulator_disable(data->vcc);
  835. dev_err(&client->dev,
  836. "Failed to add disable regulator action: %d\n",
  837. error);
  838. return error;
  839. }
  840. /* Initialize the touchpad. */
  841. error = elan_initialize(data);
  842. if (error)
  843. return error;
  844. error = elan_query_device_info(data);
  845. if (error)
  846. return error;
  847. error = elan_query_device_parameters(data);
  848. if (error)
  849. return error;
  850. dev_dbg(&client->dev,
  851. "Elan Touchpad Information:\n"
  852. " Module product ID: 0x%04x\n"
  853. " Firmware Version: 0x%04x\n"
  854. " Sample Version: 0x%04x\n"
  855. " IAP Version: 0x%04x\n"
  856. " Max ABS X,Y: %d,%d\n"
  857. " Width X,Y: %d,%d\n"
  858. " Resolution X,Y: %d,%d (dots/mm)\n",
  859. data->product_id,
  860. data->fw_version,
  861. data->sm_version,
  862. data->iap_version,
  863. data->max_x, data->max_y,
  864. data->width_x, data->width_y,
  865. data->x_res, data->y_res);
  866. /* Set up input device properties based on queried parameters. */
  867. error = elan_setup_input_device(data);
  868. if (error)
  869. return error;
  870. /*
  871. * Systems using device tree should set up interrupt via DTS,
  872. * the rest will use the default falling edge interrupts.
  873. */
  874. irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
  875. error = devm_request_threaded_irq(&client->dev, client->irq,
  876. NULL, elan_isr,
  877. irqflags | IRQF_ONESHOT,
  878. client->name, data);
  879. if (error) {
  880. dev_err(&client->dev, "cannot register irq=%d\n", client->irq);
  881. return error;
  882. }
  883. error = sysfs_create_groups(&client->dev.kobj, elan_sysfs_groups);
  884. if (error) {
  885. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  886. error);
  887. return error;
  888. }
  889. error = devm_add_action(&client->dev,
  890. elan_remove_sysfs_groups, data);
  891. if (error) {
  892. elan_remove_sysfs_groups(data);
  893. dev_err(&client->dev,
  894. "Failed to add sysfs cleanup action: %d\n",
  895. error);
  896. return error;
  897. }
  898. error = input_register_device(data->input);
  899. if (error) {
  900. dev_err(&client->dev, "failed to register input device: %d\n",
  901. error);
  902. return error;
  903. }
  904. /*
  905. * Systems using device tree should set up wakeup via DTS,
  906. * the rest will configure device as wakeup source by default.
  907. */
  908. if (!client->dev.of_node)
  909. device_init_wakeup(&client->dev, true);
  910. return 0;
  911. }
  912. static int __maybe_unused elan_suspend(struct device *dev)
  913. {
  914. struct i2c_client *client = to_i2c_client(dev);
  915. struct elan_tp_data *data = i2c_get_clientdata(client);
  916. int ret;
  917. /*
  918. * We are taking the mutex to make sure sysfs operations are
  919. * complete before we attempt to bring the device into low[er]
  920. * power mode.
  921. */
  922. ret = mutex_lock_interruptible(&data->sysfs_mutex);
  923. if (ret)
  924. return ret;
  925. disable_irq(client->irq);
  926. if (device_may_wakeup(dev)) {
  927. ret = elan_sleep(data);
  928. /* Enable wake from IRQ */
  929. data->irq_wake = (enable_irq_wake(client->irq) == 0);
  930. } else {
  931. ret = elan_disable_power(data);
  932. }
  933. mutex_unlock(&data->sysfs_mutex);
  934. return ret;
  935. }
  936. static int __maybe_unused elan_resume(struct device *dev)
  937. {
  938. struct i2c_client *client = to_i2c_client(dev);
  939. struct elan_tp_data *data = i2c_get_clientdata(client);
  940. int error;
  941. if (device_may_wakeup(dev) && data->irq_wake) {
  942. disable_irq_wake(client->irq);
  943. data->irq_wake = false;
  944. }
  945. error = elan_enable_power(data);
  946. if (error) {
  947. dev_err(dev, "power up when resuming failed: %d\n", error);
  948. goto err;
  949. }
  950. error = elan_initialize(data);
  951. if (error)
  952. dev_err(dev, "initialize when resuming failed: %d\n", error);
  953. err:
  954. enable_irq(data->client->irq);
  955. return error;
  956. }
  957. static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
  958. static const struct i2c_device_id elan_id[] = {
  959. { DRIVER_NAME, 0 },
  960. { },
  961. };
  962. MODULE_DEVICE_TABLE(i2c, elan_id);
  963. #ifdef CONFIG_ACPI
  964. static const struct acpi_device_id elan_acpi_id[] = {
  965. { "ELAN0000", 0 },
  966. { }
  967. };
  968. MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
  969. #endif
  970. #ifdef CONFIG_OF
  971. static const struct of_device_id elan_of_match[] = {
  972. { .compatible = "elan,ekth3000" },
  973. { /* sentinel */ }
  974. };
  975. MODULE_DEVICE_TABLE(of, elan_of_match);
  976. #endif
  977. static struct i2c_driver elan_driver = {
  978. .driver = {
  979. .name = DRIVER_NAME,
  980. .owner = THIS_MODULE,
  981. .pm = &elan_pm_ops,
  982. .acpi_match_table = ACPI_PTR(elan_acpi_id),
  983. .of_match_table = of_match_ptr(elan_of_match),
  984. },
  985. .probe = elan_probe,
  986. .id_table = elan_id,
  987. };
  988. module_i2c_driver(elan_driver);
  989. MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
  990. MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
  991. MODULE_LICENSE("GPL");
  992. MODULE_VERSION(ELAN_DRIVER_VERSION);