cyttsp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Core Source for:
  3. * Cypress TrueTouch(TM) Standard Product (TTSP) touchscreen drivers.
  4. * For use with Cypress Txx3xx parts.
  5. * Supported parts include:
  6. * CY8CTST341
  7. * CY8CTMA340
  8. *
  9. * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
  10. * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2, and only version 2, as published by the
  15. * Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. *
  26. * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com>
  27. *
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/input.h>
  31. #include <linux/input/mt.h>
  32. #include <linux/gpio.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/slab.h>
  35. #include "cyttsp_core.h"
  36. /* Bootloader number of command keys */
  37. #define CY_NUM_BL_KEYS 8
  38. /* helpers */
  39. #define GET_NUM_TOUCHES(x) ((x) & 0x0F)
  40. #define IS_LARGE_AREA(x) (((x) & 0x10) >> 4)
  41. #define IS_BAD_PKT(x) ((x) & 0x20)
  42. #define IS_VALID_APP(x) ((x) & 0x01)
  43. #define IS_OPERATIONAL_ERR(x) ((x) & 0x3F)
  44. #define GET_HSTMODE(reg) (((reg) & 0x70) >> 4)
  45. #define GET_BOOTLOADERMODE(reg) (((reg) & 0x10) >> 4)
  46. #define CY_REG_BASE 0x00
  47. #define CY_REG_ACT_DIST 0x1E
  48. #define CY_REG_ACT_INTRVL 0x1D
  49. #define CY_REG_TCH_TMOUT (CY_REG_ACT_INTRVL + 1)
  50. #define CY_REG_LP_INTRVL (CY_REG_TCH_TMOUT + 1)
  51. #define CY_MAXZ 255
  52. #define CY_DELAY_DFLT 20 /* ms */
  53. #define CY_DELAY_MAX 500
  54. #define CY_ACT_DIST_DFLT 0xF8
  55. #define CY_HNDSHK_BIT 0x80
  56. /* device mode bits */
  57. #define CY_OPERATE_MODE 0x00
  58. #define CY_SYSINFO_MODE 0x10
  59. /* power mode select bits */
  60. #define CY_SOFT_RESET_MODE 0x01 /* return to Bootloader mode */
  61. #define CY_DEEP_SLEEP_MODE 0x02
  62. #define CY_LOW_POWER_MODE 0x04
  63. /* Slots management */
  64. #define CY_MAX_FINGER 4
  65. #define CY_MAX_ID 16
  66. static const u8 bl_command[] = {
  67. 0x00, /* file offset */
  68. 0xFF, /* command */
  69. 0xA5, /* exit bootloader command */
  70. 0, 1, 2, 3, 4, 5, 6, 7 /* default keys */
  71. };
  72. static int ttsp_read_block_data(struct cyttsp *ts, u8 command,
  73. u8 length, void *buf)
  74. {
  75. int error;
  76. int tries;
  77. for (tries = 0; tries < CY_NUM_RETRY; tries++) {
  78. error = ts->bus_ops->read(ts, command, length, buf);
  79. if (!error)
  80. return 0;
  81. msleep(CY_DELAY_DFLT);
  82. }
  83. return -EIO;
  84. }
  85. static int ttsp_write_block_data(struct cyttsp *ts, u8 command,
  86. u8 length, void *buf)
  87. {
  88. int error;
  89. int tries;
  90. for (tries = 0; tries < CY_NUM_RETRY; tries++) {
  91. error = ts->bus_ops->write(ts, command, length, buf);
  92. if (!error)
  93. return 0;
  94. msleep(CY_DELAY_DFLT);
  95. }
  96. return -EIO;
  97. }
  98. static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
  99. {
  100. return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd);
  101. }
  102. static int cyttsp_handshake(struct cyttsp *ts)
  103. {
  104. if (ts->pdata->use_hndshk)
  105. return ttsp_send_command(ts,
  106. ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
  107. return 0;
  108. }
  109. static int cyttsp_load_bl_regs(struct cyttsp *ts)
  110. {
  111. memset(&ts->bl_data, 0, sizeof(ts->bl_data));
  112. ts->bl_data.bl_status = 0x10;
  113. return ttsp_read_block_data(ts, CY_REG_BASE,
  114. sizeof(ts->bl_data), &ts->bl_data);
  115. }
  116. static int cyttsp_exit_bl_mode(struct cyttsp *ts)
  117. {
  118. int error;
  119. u8 bl_cmd[sizeof(bl_command)];
  120. memcpy(bl_cmd, bl_command, sizeof(bl_command));
  121. if (ts->pdata->bl_keys)
  122. memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
  123. ts->pdata->bl_keys, CY_NUM_BL_KEYS);
  124. error = ttsp_write_block_data(ts, CY_REG_BASE,
  125. sizeof(bl_cmd), bl_cmd);
  126. if (error)
  127. return error;
  128. /* wait for TTSP Device to complete the operation */
  129. msleep(CY_DELAY_DFLT);
  130. error = cyttsp_load_bl_regs(ts);
  131. if (error)
  132. return error;
  133. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
  134. return -EIO;
  135. return 0;
  136. }
  137. static int cyttsp_set_operational_mode(struct cyttsp *ts)
  138. {
  139. int error;
  140. error = ttsp_send_command(ts, CY_OPERATE_MODE);
  141. if (error)
  142. return error;
  143. /* wait for TTSP Device to complete switch to Operational mode */
  144. error = ttsp_read_block_data(ts, CY_REG_BASE,
  145. sizeof(ts->xy_data), &ts->xy_data);
  146. if (error)
  147. return error;
  148. error = cyttsp_handshake(ts);
  149. if (error)
  150. return error;
  151. return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
  152. }
  153. static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
  154. {
  155. int error;
  156. memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
  157. /* switch to sysinfo mode */
  158. error = ttsp_send_command(ts, CY_SYSINFO_MODE);
  159. if (error)
  160. return error;
  161. /* read sysinfo registers */
  162. msleep(CY_DELAY_DFLT);
  163. error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
  164. &ts->sysinfo_data);
  165. if (error)
  166. return error;
  167. error = cyttsp_handshake(ts);
  168. if (error)
  169. return error;
  170. if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
  171. return -EIO;
  172. return 0;
  173. }
  174. static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
  175. {
  176. int retval = 0;
  177. if (ts->pdata->act_intrvl != CY_ACT_INTRVL_DFLT ||
  178. ts->pdata->tch_tmout != CY_TCH_TMOUT_DFLT ||
  179. ts->pdata->lp_intrvl != CY_LP_INTRVL_DFLT) {
  180. u8 intrvl_ray[] = {
  181. ts->pdata->act_intrvl,
  182. ts->pdata->tch_tmout,
  183. ts->pdata->lp_intrvl
  184. };
  185. /* set intrvl registers */
  186. retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
  187. sizeof(intrvl_ray), intrvl_ray);
  188. msleep(CY_DELAY_DFLT);
  189. }
  190. return retval;
  191. }
  192. static int cyttsp_soft_reset(struct cyttsp *ts)
  193. {
  194. unsigned long timeout;
  195. int retval;
  196. /* wait for interrupt to set ready completion */
  197. INIT_COMPLETION(ts->bl_ready);
  198. ts->state = CY_BL_STATE;
  199. enable_irq(ts->irq);
  200. retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
  201. if (retval)
  202. goto out;
  203. timeout = wait_for_completion_timeout(&ts->bl_ready,
  204. msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX));
  205. retval = timeout ? 0 : -EIO;
  206. out:
  207. ts->state = CY_IDLE_STATE;
  208. disable_irq(ts->irq);
  209. return retval;
  210. }
  211. static int cyttsp_act_dist_setup(struct cyttsp *ts)
  212. {
  213. u8 act_dist_setup = ts->pdata->act_dist;
  214. /* Init gesture; active distance setup */
  215. return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
  216. sizeof(act_dist_setup), &act_dist_setup);
  217. }
  218. static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
  219. {
  220. ids[0] = xy_data->touch12_id >> 4;
  221. ids[1] = xy_data->touch12_id & 0xF;
  222. ids[2] = xy_data->touch34_id >> 4;
  223. ids[3] = xy_data->touch34_id & 0xF;
  224. }
  225. static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
  226. int idx)
  227. {
  228. switch (idx) {
  229. case 0:
  230. return &xy_data->tch1;
  231. case 1:
  232. return &xy_data->tch2;
  233. case 2:
  234. return &xy_data->tch3;
  235. case 3:
  236. return &xy_data->tch4;
  237. default:
  238. return NULL;
  239. }
  240. }
  241. static void cyttsp_report_tchdata(struct cyttsp *ts)
  242. {
  243. struct cyttsp_xydata *xy_data = &ts->xy_data;
  244. struct input_dev *input = ts->input;
  245. int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
  246. const struct cyttsp_tch *tch;
  247. int ids[CY_MAX_ID];
  248. int i;
  249. DECLARE_BITMAP(used, CY_MAX_ID);
  250. if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
  251. /* terminate all active tracks */
  252. num_tch = 0;
  253. dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
  254. } else if (num_tch > CY_MAX_FINGER) {
  255. /* terminate all active tracks */
  256. num_tch = 0;
  257. dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
  258. } else if (IS_BAD_PKT(xy_data->tt_mode)) {
  259. /* terminate all active tracks */
  260. num_tch = 0;
  261. dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
  262. }
  263. cyttsp_extract_track_ids(xy_data, ids);
  264. bitmap_zero(used, CY_MAX_ID);
  265. for (i = 0; i < num_tch; i++) {
  266. tch = cyttsp_get_tch(xy_data, i);
  267. input_mt_slot(input, ids[i]);
  268. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  269. input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
  270. input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
  271. input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
  272. __set_bit(ids[i], used);
  273. }
  274. for (i = 0; i < CY_MAX_ID; i++) {
  275. if (test_bit(i, used))
  276. continue;
  277. input_mt_slot(input, i);
  278. input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
  279. }
  280. input_sync(input);
  281. }
  282. static irqreturn_t cyttsp_irq(int irq, void *handle)
  283. {
  284. struct cyttsp *ts = handle;
  285. int error;
  286. if (unlikely(ts->state == CY_BL_STATE)) {
  287. complete(&ts->bl_ready);
  288. goto out;
  289. }
  290. /* Get touch data from CYTTSP device */
  291. error = ttsp_read_block_data(ts, CY_REG_BASE,
  292. sizeof(struct cyttsp_xydata), &ts->xy_data);
  293. if (error)
  294. goto out;
  295. /* provide flow control handshake */
  296. error = cyttsp_handshake(ts);
  297. if (error)
  298. goto out;
  299. if (unlikely(ts->state == CY_IDLE_STATE))
  300. goto out;
  301. if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
  302. /*
  303. * TTSP device has reset back to bootloader mode.
  304. * Restore to operational mode.
  305. */
  306. error = cyttsp_exit_bl_mode(ts);
  307. if (error) {
  308. dev_err(ts->dev,
  309. "Could not return to operational mode, err: %d\n",
  310. error);
  311. ts->state = CY_IDLE_STATE;
  312. }
  313. } else {
  314. cyttsp_report_tchdata(ts);
  315. }
  316. out:
  317. return IRQ_HANDLED;
  318. }
  319. static int cyttsp_power_on(struct cyttsp *ts)
  320. {
  321. int error;
  322. error = cyttsp_soft_reset(ts);
  323. if (error)
  324. return error;
  325. error = cyttsp_load_bl_regs(ts);
  326. if (error)
  327. return error;
  328. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
  329. IS_VALID_APP(ts->bl_data.bl_status)) {
  330. error = cyttsp_exit_bl_mode(ts);
  331. if (error)
  332. return error;
  333. }
  334. if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
  335. IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
  336. return -ENODEV;
  337. }
  338. error = cyttsp_set_sysinfo_mode(ts);
  339. if (error)
  340. return error;
  341. error = cyttsp_set_sysinfo_regs(ts);
  342. if (error)
  343. return error;
  344. error = cyttsp_set_operational_mode(ts);
  345. if (error)
  346. return error;
  347. /* init active distance */
  348. error = cyttsp_act_dist_setup(ts);
  349. if (error)
  350. return error;
  351. ts->state = CY_ACTIVE_STATE;
  352. return 0;
  353. }
  354. static int cyttsp_enable(struct cyttsp *ts)
  355. {
  356. int error;
  357. /*
  358. * The device firmware can wake on an I2C or SPI memory slave
  359. * address match. So just reading a register is sufficient to
  360. * wake up the device. The first read attempt will fail but it
  361. * will wake it up making the second read attempt successful.
  362. */
  363. error = ttsp_read_block_data(ts, CY_REG_BASE,
  364. sizeof(ts->xy_data), &ts->xy_data);
  365. if (error)
  366. return error;
  367. if (GET_HSTMODE(ts->xy_data.hst_mode))
  368. return -EIO;
  369. enable_irq(ts->irq);
  370. return 0;
  371. }
  372. static int cyttsp_disable(struct cyttsp *ts)
  373. {
  374. int error;
  375. error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
  376. if (error)
  377. return error;
  378. disable_irq(ts->irq);
  379. return 0;
  380. }
  381. #ifdef CONFIG_PM_SLEEP
  382. static int cyttsp_suspend(struct device *dev)
  383. {
  384. struct cyttsp *ts = dev_get_drvdata(dev);
  385. int retval = 0;
  386. mutex_lock(&ts->input->mutex);
  387. if (ts->input->users) {
  388. retval = cyttsp_disable(ts);
  389. if (retval == 0)
  390. ts->suspended = true;
  391. }
  392. mutex_unlock(&ts->input->mutex);
  393. return retval;
  394. }
  395. static int cyttsp_resume(struct device *dev)
  396. {
  397. struct cyttsp *ts = dev_get_drvdata(dev);
  398. mutex_lock(&ts->input->mutex);
  399. if (ts->input->users)
  400. cyttsp_enable(ts);
  401. ts->suspended = false;
  402. mutex_unlock(&ts->input->mutex);
  403. return 0;
  404. }
  405. #endif
  406. SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume);
  407. EXPORT_SYMBOL_GPL(cyttsp_pm_ops);
  408. static int cyttsp_open(struct input_dev *dev)
  409. {
  410. struct cyttsp *ts = input_get_drvdata(dev);
  411. int retval = 0;
  412. if (!ts->suspended)
  413. retval = cyttsp_enable(ts);
  414. return retval;
  415. }
  416. static void cyttsp_close(struct input_dev *dev)
  417. {
  418. struct cyttsp *ts = input_get_drvdata(dev);
  419. if (!ts->suspended)
  420. cyttsp_disable(ts);
  421. }
  422. struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
  423. struct device *dev, int irq, size_t xfer_buf_size)
  424. {
  425. const struct cyttsp_platform_data *pdata = dev->platform_data;
  426. struct cyttsp *ts;
  427. struct input_dev *input_dev;
  428. int error;
  429. if (!pdata || !pdata->name || irq <= 0) {
  430. error = -EINVAL;
  431. goto err_out;
  432. }
  433. ts = kzalloc(sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
  434. input_dev = input_allocate_device();
  435. if (!ts || !input_dev) {
  436. error = -ENOMEM;
  437. goto err_free_mem;
  438. }
  439. ts->dev = dev;
  440. ts->input = input_dev;
  441. ts->pdata = dev->platform_data;
  442. ts->bus_ops = bus_ops;
  443. ts->irq = irq;
  444. init_completion(&ts->bl_ready);
  445. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
  446. if (pdata->init) {
  447. error = pdata->init();
  448. if (error) {
  449. dev_err(ts->dev, "platform init failed, err: %d\n",
  450. error);
  451. goto err_free_mem;
  452. }
  453. }
  454. input_dev->name = pdata->name;
  455. input_dev->phys = ts->phys;
  456. input_dev->id.bustype = bus_ops->bustype;
  457. input_dev->dev.parent = ts->dev;
  458. input_dev->open = cyttsp_open;
  459. input_dev->close = cyttsp_close;
  460. input_set_drvdata(input_dev, ts);
  461. __set_bit(EV_ABS, input_dev->evbit);
  462. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  463. 0, pdata->maxx, 0, 0);
  464. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  465. 0, pdata->maxy, 0, 0);
  466. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  467. 0, CY_MAXZ, 0, 0);
  468. input_mt_init_slots(input_dev, CY_MAX_ID, 0);
  469. error = request_threaded_irq(ts->irq, NULL, cyttsp_irq,
  470. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  471. pdata->name, ts);
  472. if (error) {
  473. dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
  474. ts->irq, error);
  475. goto err_platform_exit;
  476. }
  477. disable_irq(ts->irq);
  478. error = cyttsp_power_on(ts);
  479. if (error)
  480. goto err_free_irq;
  481. error = input_register_device(input_dev);
  482. if (error) {
  483. dev_err(ts->dev, "failed to register input device: %d\n",
  484. error);
  485. goto err_free_irq;
  486. }
  487. return ts;
  488. err_free_irq:
  489. free_irq(ts->irq, ts);
  490. err_platform_exit:
  491. if (pdata->exit)
  492. pdata->exit();
  493. err_free_mem:
  494. input_free_device(input_dev);
  495. kfree(ts);
  496. err_out:
  497. return ERR_PTR(error);
  498. }
  499. EXPORT_SYMBOL_GPL(cyttsp_probe);
  500. void cyttsp_remove(struct cyttsp *ts)
  501. {
  502. free_irq(ts->irq, ts);
  503. input_unregister_device(ts->input);
  504. if (ts->pdata->exit)
  505. ts->pdata->exit();
  506. kfree(ts);
  507. }
  508. EXPORT_SYMBOL_GPL(cyttsp_remove);
  509. MODULE_LICENSE("GPL");
  510. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
  511. MODULE_AUTHOR("Cypress");