cyttsp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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->dev, ts->xfer_buf, command,
  79. length, buf);
  80. if (!error)
  81. return 0;
  82. msleep(CY_DELAY_DFLT);
  83. }
  84. return -EIO;
  85. }
  86. static int ttsp_write_block_data(struct cyttsp *ts, u8 command,
  87. u8 length, void *buf)
  88. {
  89. int error;
  90. int tries;
  91. for (tries = 0; tries < CY_NUM_RETRY; tries++) {
  92. error = ts->bus_ops->write(ts->dev, ts->xfer_buf, command,
  93. length, buf);
  94. if (!error)
  95. return 0;
  96. msleep(CY_DELAY_DFLT);
  97. }
  98. return -EIO;
  99. }
  100. static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
  101. {
  102. return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd);
  103. }
  104. static int cyttsp_handshake(struct cyttsp *ts)
  105. {
  106. if (ts->pdata->use_hndshk)
  107. return ttsp_send_command(ts,
  108. ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
  109. return 0;
  110. }
  111. static int cyttsp_load_bl_regs(struct cyttsp *ts)
  112. {
  113. memset(&ts->bl_data, 0, sizeof(ts->bl_data));
  114. ts->bl_data.bl_status = 0x10;
  115. return ttsp_read_block_data(ts, CY_REG_BASE,
  116. sizeof(ts->bl_data), &ts->bl_data);
  117. }
  118. static int cyttsp_exit_bl_mode(struct cyttsp *ts)
  119. {
  120. int error;
  121. u8 bl_cmd[sizeof(bl_command)];
  122. memcpy(bl_cmd, bl_command, sizeof(bl_command));
  123. if (ts->pdata->bl_keys)
  124. memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
  125. ts->pdata->bl_keys, CY_NUM_BL_KEYS);
  126. error = ttsp_write_block_data(ts, CY_REG_BASE,
  127. sizeof(bl_cmd), bl_cmd);
  128. if (error)
  129. return error;
  130. /* wait for TTSP Device to complete the operation */
  131. msleep(CY_DELAY_DFLT);
  132. error = cyttsp_load_bl_regs(ts);
  133. if (error)
  134. return error;
  135. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
  136. return -EIO;
  137. return 0;
  138. }
  139. static int cyttsp_set_operational_mode(struct cyttsp *ts)
  140. {
  141. int error;
  142. error = ttsp_send_command(ts, CY_OPERATE_MODE);
  143. if (error)
  144. return error;
  145. /* wait for TTSP Device to complete switch to Operational mode */
  146. error = ttsp_read_block_data(ts, CY_REG_BASE,
  147. sizeof(ts->xy_data), &ts->xy_data);
  148. if (error)
  149. return error;
  150. error = cyttsp_handshake(ts);
  151. if (error)
  152. return error;
  153. return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
  154. }
  155. static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
  156. {
  157. int error;
  158. memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
  159. /* switch to sysinfo mode */
  160. error = ttsp_send_command(ts, CY_SYSINFO_MODE);
  161. if (error)
  162. return error;
  163. /* read sysinfo registers */
  164. msleep(CY_DELAY_DFLT);
  165. error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
  166. &ts->sysinfo_data);
  167. if (error)
  168. return error;
  169. error = cyttsp_handshake(ts);
  170. if (error)
  171. return error;
  172. if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
  173. return -EIO;
  174. return 0;
  175. }
  176. static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
  177. {
  178. int retval = 0;
  179. if (ts->pdata->act_intrvl != CY_ACT_INTRVL_DFLT ||
  180. ts->pdata->tch_tmout != CY_TCH_TMOUT_DFLT ||
  181. ts->pdata->lp_intrvl != CY_LP_INTRVL_DFLT) {
  182. u8 intrvl_ray[] = {
  183. ts->pdata->act_intrvl,
  184. ts->pdata->tch_tmout,
  185. ts->pdata->lp_intrvl
  186. };
  187. /* set intrvl registers */
  188. retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
  189. sizeof(intrvl_ray), intrvl_ray);
  190. msleep(CY_DELAY_DFLT);
  191. }
  192. return retval;
  193. }
  194. static int cyttsp_soft_reset(struct cyttsp *ts)
  195. {
  196. unsigned long timeout;
  197. int retval;
  198. /* wait for interrupt to set ready completion */
  199. reinit_completion(&ts->bl_ready);
  200. ts->state = CY_BL_STATE;
  201. enable_irq(ts->irq);
  202. retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
  203. if (retval)
  204. goto out;
  205. timeout = wait_for_completion_timeout(&ts->bl_ready,
  206. msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX));
  207. retval = timeout ? 0 : -EIO;
  208. out:
  209. ts->state = CY_IDLE_STATE;
  210. disable_irq(ts->irq);
  211. return retval;
  212. }
  213. static int cyttsp_act_dist_setup(struct cyttsp *ts)
  214. {
  215. u8 act_dist_setup = ts->pdata->act_dist;
  216. /* Init gesture; active distance setup */
  217. return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
  218. sizeof(act_dist_setup), &act_dist_setup);
  219. }
  220. static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
  221. {
  222. ids[0] = xy_data->touch12_id >> 4;
  223. ids[1] = xy_data->touch12_id & 0xF;
  224. ids[2] = xy_data->touch34_id >> 4;
  225. ids[3] = xy_data->touch34_id & 0xF;
  226. }
  227. static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
  228. int idx)
  229. {
  230. switch (idx) {
  231. case 0:
  232. return &xy_data->tch1;
  233. case 1:
  234. return &xy_data->tch2;
  235. case 2:
  236. return &xy_data->tch3;
  237. case 3:
  238. return &xy_data->tch4;
  239. default:
  240. return NULL;
  241. }
  242. }
  243. static void cyttsp_report_tchdata(struct cyttsp *ts)
  244. {
  245. struct cyttsp_xydata *xy_data = &ts->xy_data;
  246. struct input_dev *input = ts->input;
  247. int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
  248. const struct cyttsp_tch *tch;
  249. int ids[CY_MAX_ID];
  250. int i;
  251. DECLARE_BITMAP(used, CY_MAX_ID);
  252. if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
  253. /* terminate all active tracks */
  254. num_tch = 0;
  255. dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
  256. } else if (num_tch > CY_MAX_FINGER) {
  257. /* terminate all active tracks */
  258. num_tch = 0;
  259. dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
  260. } else if (IS_BAD_PKT(xy_data->tt_mode)) {
  261. /* terminate all active tracks */
  262. num_tch = 0;
  263. dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
  264. }
  265. cyttsp_extract_track_ids(xy_data, ids);
  266. bitmap_zero(used, CY_MAX_ID);
  267. for (i = 0; i < num_tch; i++) {
  268. tch = cyttsp_get_tch(xy_data, i);
  269. input_mt_slot(input, ids[i]);
  270. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  271. input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
  272. input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
  273. input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
  274. __set_bit(ids[i], used);
  275. }
  276. for (i = 0; i < CY_MAX_ID; i++) {
  277. if (test_bit(i, used))
  278. continue;
  279. input_mt_slot(input, i);
  280. input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
  281. }
  282. input_sync(input);
  283. }
  284. static irqreturn_t cyttsp_irq(int irq, void *handle)
  285. {
  286. struct cyttsp *ts = handle;
  287. int error;
  288. if (unlikely(ts->state == CY_BL_STATE)) {
  289. complete(&ts->bl_ready);
  290. goto out;
  291. }
  292. /* Get touch data from CYTTSP device */
  293. error = ttsp_read_block_data(ts, CY_REG_BASE,
  294. sizeof(struct cyttsp_xydata), &ts->xy_data);
  295. if (error)
  296. goto out;
  297. /* provide flow control handshake */
  298. error = cyttsp_handshake(ts);
  299. if (error)
  300. goto out;
  301. if (unlikely(ts->state == CY_IDLE_STATE))
  302. goto out;
  303. if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
  304. /*
  305. * TTSP device has reset back to bootloader mode.
  306. * Restore to operational mode.
  307. */
  308. error = cyttsp_exit_bl_mode(ts);
  309. if (error) {
  310. dev_err(ts->dev,
  311. "Could not return to operational mode, err: %d\n",
  312. error);
  313. ts->state = CY_IDLE_STATE;
  314. }
  315. } else {
  316. cyttsp_report_tchdata(ts);
  317. }
  318. out:
  319. return IRQ_HANDLED;
  320. }
  321. static int cyttsp_power_on(struct cyttsp *ts)
  322. {
  323. int error;
  324. error = cyttsp_soft_reset(ts);
  325. if (error)
  326. return error;
  327. error = cyttsp_load_bl_regs(ts);
  328. if (error)
  329. return error;
  330. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
  331. IS_VALID_APP(ts->bl_data.bl_status)) {
  332. error = cyttsp_exit_bl_mode(ts);
  333. if (error)
  334. return error;
  335. }
  336. if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
  337. IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
  338. return -ENODEV;
  339. }
  340. error = cyttsp_set_sysinfo_mode(ts);
  341. if (error)
  342. return error;
  343. error = cyttsp_set_sysinfo_regs(ts);
  344. if (error)
  345. return error;
  346. error = cyttsp_set_operational_mode(ts);
  347. if (error)
  348. return error;
  349. /* init active distance */
  350. error = cyttsp_act_dist_setup(ts);
  351. if (error)
  352. return error;
  353. ts->state = CY_ACTIVE_STATE;
  354. return 0;
  355. }
  356. static int cyttsp_enable(struct cyttsp *ts)
  357. {
  358. int error;
  359. /*
  360. * The device firmware can wake on an I2C or SPI memory slave
  361. * address match. So just reading a register is sufficient to
  362. * wake up the device. The first read attempt will fail but it
  363. * will wake it up making the second read attempt successful.
  364. */
  365. error = ttsp_read_block_data(ts, CY_REG_BASE,
  366. sizeof(ts->xy_data), &ts->xy_data);
  367. if (error)
  368. return error;
  369. if (GET_HSTMODE(ts->xy_data.hst_mode))
  370. return -EIO;
  371. enable_irq(ts->irq);
  372. return 0;
  373. }
  374. static int cyttsp_disable(struct cyttsp *ts)
  375. {
  376. int error;
  377. error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
  378. if (error)
  379. return error;
  380. disable_irq(ts->irq);
  381. return 0;
  382. }
  383. static int __maybe_unused cyttsp_suspend(struct device *dev)
  384. {
  385. struct cyttsp *ts = dev_get_drvdata(dev);
  386. int retval = 0;
  387. mutex_lock(&ts->input->mutex);
  388. if (ts->input->users) {
  389. retval = cyttsp_disable(ts);
  390. if (retval == 0)
  391. ts->suspended = true;
  392. }
  393. mutex_unlock(&ts->input->mutex);
  394. return retval;
  395. }
  396. static int __maybe_unused cyttsp_resume(struct device *dev)
  397. {
  398. struct cyttsp *ts = dev_get_drvdata(dev);
  399. mutex_lock(&ts->input->mutex);
  400. if (ts->input->users)
  401. cyttsp_enable(ts);
  402. ts->suspended = false;
  403. mutex_unlock(&ts->input->mutex);
  404. return 0;
  405. }
  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_get_platdata(dev);
  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_get_platdata(dev);
  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");