cyttsp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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_load_bl_regs(struct cyttsp *ts)
  103. {
  104. memset(&ts->bl_data, 0, sizeof(ts->bl_data));
  105. ts->bl_data.bl_status = 0x10;
  106. return ttsp_read_block_data(ts, CY_REG_BASE,
  107. sizeof(ts->bl_data), &ts->bl_data);
  108. }
  109. static int cyttsp_exit_bl_mode(struct cyttsp *ts)
  110. {
  111. int error;
  112. u8 bl_cmd[sizeof(bl_command)];
  113. memcpy(bl_cmd, bl_command, sizeof(bl_command));
  114. if (ts->pdata->bl_keys)
  115. memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
  116. ts->pdata->bl_keys, CY_NUM_BL_KEYS);
  117. error = ttsp_write_block_data(ts, CY_REG_BASE,
  118. sizeof(bl_cmd), bl_cmd);
  119. if (error)
  120. return error;
  121. /* wait for TTSP Device to complete the operation */
  122. msleep(CY_DELAY_DFLT);
  123. error = cyttsp_load_bl_regs(ts);
  124. if (error)
  125. return error;
  126. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
  127. return -EIO;
  128. return 0;
  129. }
  130. static int cyttsp_set_operational_mode(struct cyttsp *ts)
  131. {
  132. int error;
  133. error = ttsp_send_command(ts, CY_OPERATE_MODE);
  134. if (error)
  135. return error;
  136. /* wait for TTSP Device to complete switch to Operational mode */
  137. error = ttsp_read_block_data(ts, CY_REG_BASE,
  138. sizeof(ts->xy_data), &ts->xy_data);
  139. if (error)
  140. return error;
  141. return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
  142. }
  143. static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
  144. {
  145. int error;
  146. memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
  147. /* switch to sysinfo mode */
  148. error = ttsp_send_command(ts, CY_SYSINFO_MODE);
  149. if (error)
  150. return error;
  151. /* read sysinfo registers */
  152. msleep(CY_DELAY_DFLT);
  153. error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
  154. &ts->sysinfo_data);
  155. if (error)
  156. return error;
  157. if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
  158. return -EIO;
  159. return 0;
  160. }
  161. static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
  162. {
  163. int retval = 0;
  164. if (ts->pdata->act_intrvl != CY_ACT_INTRVL_DFLT ||
  165. ts->pdata->tch_tmout != CY_TCH_TMOUT_DFLT ||
  166. ts->pdata->lp_intrvl != CY_LP_INTRVL_DFLT) {
  167. u8 intrvl_ray[] = {
  168. ts->pdata->act_intrvl,
  169. ts->pdata->tch_tmout,
  170. ts->pdata->lp_intrvl
  171. };
  172. /* set intrvl registers */
  173. retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
  174. sizeof(intrvl_ray), intrvl_ray);
  175. msleep(CY_DELAY_DFLT);
  176. }
  177. return retval;
  178. }
  179. static int cyttsp_soft_reset(struct cyttsp *ts)
  180. {
  181. unsigned long timeout;
  182. int retval;
  183. /* wait for interrupt to set ready completion */
  184. INIT_COMPLETION(ts->bl_ready);
  185. ts->state = CY_BL_STATE;
  186. enable_irq(ts->irq);
  187. retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
  188. if (retval)
  189. goto out;
  190. timeout = wait_for_completion_timeout(&ts->bl_ready,
  191. msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX));
  192. retval = timeout ? 0 : -EIO;
  193. out:
  194. ts->state = CY_IDLE_STATE;
  195. disable_irq(ts->irq);
  196. return retval;
  197. }
  198. static int cyttsp_act_dist_setup(struct cyttsp *ts)
  199. {
  200. u8 act_dist_setup = ts->pdata->act_dist;
  201. /* Init gesture; active distance setup */
  202. return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
  203. sizeof(act_dist_setup), &act_dist_setup);
  204. }
  205. static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
  206. {
  207. ids[0] = xy_data->touch12_id >> 4;
  208. ids[1] = xy_data->touch12_id & 0xF;
  209. ids[2] = xy_data->touch34_id >> 4;
  210. ids[3] = xy_data->touch34_id & 0xF;
  211. }
  212. static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
  213. int idx)
  214. {
  215. switch (idx) {
  216. case 0:
  217. return &xy_data->tch1;
  218. case 1:
  219. return &xy_data->tch2;
  220. case 2:
  221. return &xy_data->tch3;
  222. case 3:
  223. return &xy_data->tch4;
  224. default:
  225. return NULL;
  226. }
  227. }
  228. static void cyttsp_report_tchdata(struct cyttsp *ts)
  229. {
  230. struct cyttsp_xydata *xy_data = &ts->xy_data;
  231. struct input_dev *input = ts->input;
  232. int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
  233. const struct cyttsp_tch *tch;
  234. int ids[CY_MAX_ID];
  235. int i;
  236. DECLARE_BITMAP(used, CY_MAX_ID);
  237. if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
  238. /* terminate all active tracks */
  239. num_tch = 0;
  240. dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
  241. } else if (num_tch > CY_MAX_FINGER) {
  242. /* terminate all active tracks */
  243. num_tch = 0;
  244. dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
  245. } else if (IS_BAD_PKT(xy_data->tt_mode)) {
  246. /* terminate all active tracks */
  247. num_tch = 0;
  248. dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
  249. }
  250. cyttsp_extract_track_ids(xy_data, ids);
  251. bitmap_zero(used, CY_MAX_ID);
  252. for (i = 0; i < num_tch; i++) {
  253. tch = cyttsp_get_tch(xy_data, i);
  254. input_mt_slot(input, ids[i]);
  255. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  256. input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
  257. input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
  258. input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
  259. __set_bit(ids[i], used);
  260. }
  261. for (i = 0; i < CY_MAX_ID; i++) {
  262. if (test_bit(i, used))
  263. continue;
  264. input_mt_slot(input, i);
  265. input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
  266. }
  267. input_sync(input);
  268. }
  269. static irqreturn_t cyttsp_irq(int irq, void *handle)
  270. {
  271. struct cyttsp *ts = handle;
  272. int error;
  273. if (unlikely(ts->state == CY_BL_STATE)) {
  274. complete(&ts->bl_ready);
  275. goto out;
  276. }
  277. /* Get touch data from CYTTSP device */
  278. error = ttsp_read_block_data(ts, CY_REG_BASE,
  279. sizeof(struct cyttsp_xydata), &ts->xy_data);
  280. if (error)
  281. goto out;
  282. /* provide flow control handshake */
  283. if (ts->pdata->use_hndshk) {
  284. error = ttsp_send_command(ts,
  285. ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
  286. if (error)
  287. goto out;
  288. }
  289. if (unlikely(ts->state == CY_IDLE_STATE))
  290. goto out;
  291. if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
  292. /*
  293. * TTSP device has reset back to bootloader mode.
  294. * Restore to operational mode.
  295. */
  296. error = cyttsp_exit_bl_mode(ts);
  297. if (error) {
  298. dev_err(ts->dev,
  299. "Could not return to operational mode, err: %d\n",
  300. error);
  301. ts->state = CY_IDLE_STATE;
  302. }
  303. } else {
  304. cyttsp_report_tchdata(ts);
  305. }
  306. out:
  307. return IRQ_HANDLED;
  308. }
  309. static int cyttsp_power_on(struct cyttsp *ts)
  310. {
  311. int error;
  312. error = cyttsp_soft_reset(ts);
  313. if (error)
  314. return error;
  315. error = cyttsp_load_bl_regs(ts);
  316. if (error)
  317. return error;
  318. if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
  319. IS_VALID_APP(ts->bl_data.bl_status)) {
  320. error = cyttsp_exit_bl_mode(ts);
  321. if (error)
  322. return error;
  323. }
  324. if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
  325. IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
  326. return -ENODEV;
  327. }
  328. error = cyttsp_set_sysinfo_mode(ts);
  329. if (error)
  330. return error;
  331. error = cyttsp_set_sysinfo_regs(ts);
  332. if (error)
  333. return error;
  334. error = cyttsp_set_operational_mode(ts);
  335. if (error)
  336. return error;
  337. /* init active distance */
  338. error = cyttsp_act_dist_setup(ts);
  339. if (error)
  340. return error;
  341. ts->state = CY_ACTIVE_STATE;
  342. return 0;
  343. }
  344. static int cyttsp_enable(struct cyttsp *ts)
  345. {
  346. int error;
  347. /*
  348. * The device firmware can wake on an I2C or SPI memory slave
  349. * address match. So just reading a register is sufficient to
  350. * wake up the device. The first read attempt will fail but it
  351. * will wake it up making the second read attempt successful.
  352. */
  353. error = ttsp_read_block_data(ts, CY_REG_BASE,
  354. sizeof(ts->xy_data), &ts->xy_data);
  355. if (error)
  356. return error;
  357. if (GET_HSTMODE(ts->xy_data.hst_mode))
  358. return -EIO;
  359. enable_irq(ts->irq);
  360. return 0;
  361. }
  362. static int cyttsp_disable(struct cyttsp *ts)
  363. {
  364. int error;
  365. error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
  366. if (error)
  367. return error;
  368. disable_irq(ts->irq);
  369. return 0;
  370. }
  371. #ifdef CONFIG_PM_SLEEP
  372. static int cyttsp_suspend(struct device *dev)
  373. {
  374. struct cyttsp *ts = dev_get_drvdata(dev);
  375. int retval = 0;
  376. mutex_lock(&ts->input->mutex);
  377. if (ts->input->users) {
  378. retval = cyttsp_disable(ts);
  379. if (retval == 0)
  380. ts->suspended = true;
  381. }
  382. mutex_unlock(&ts->input->mutex);
  383. return retval;
  384. }
  385. static int cyttsp_resume(struct device *dev)
  386. {
  387. struct cyttsp *ts = dev_get_drvdata(dev);
  388. mutex_lock(&ts->input->mutex);
  389. if (ts->input->users)
  390. cyttsp_enable(ts);
  391. ts->suspended = false;
  392. mutex_unlock(&ts->input->mutex);
  393. return 0;
  394. }
  395. #endif
  396. SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume);
  397. EXPORT_SYMBOL_GPL(cyttsp_pm_ops);
  398. static int cyttsp_open(struct input_dev *dev)
  399. {
  400. struct cyttsp *ts = input_get_drvdata(dev);
  401. int retval = 0;
  402. if (!ts->suspended)
  403. retval = cyttsp_enable(ts);
  404. return retval;
  405. }
  406. static void cyttsp_close(struct input_dev *dev)
  407. {
  408. struct cyttsp *ts = input_get_drvdata(dev);
  409. if (!ts->suspended)
  410. cyttsp_disable(ts);
  411. }
  412. struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
  413. struct device *dev, int irq, size_t xfer_buf_size)
  414. {
  415. const struct cyttsp_platform_data *pdata = dev->platform_data;
  416. struct cyttsp *ts;
  417. struct input_dev *input_dev;
  418. int error;
  419. if (!pdata || !pdata->name || irq <= 0) {
  420. error = -EINVAL;
  421. goto err_out;
  422. }
  423. ts = kzalloc(sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
  424. input_dev = input_allocate_device();
  425. if (!ts || !input_dev) {
  426. error = -ENOMEM;
  427. goto err_free_mem;
  428. }
  429. ts->dev = dev;
  430. ts->input = input_dev;
  431. ts->pdata = dev->platform_data;
  432. ts->bus_ops = bus_ops;
  433. ts->irq = irq;
  434. init_completion(&ts->bl_ready);
  435. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
  436. if (pdata->init) {
  437. error = pdata->init();
  438. if (error) {
  439. dev_err(ts->dev, "platform init failed, err: %d\n",
  440. error);
  441. goto err_free_mem;
  442. }
  443. }
  444. input_dev->name = pdata->name;
  445. input_dev->phys = ts->phys;
  446. input_dev->id.bustype = bus_ops->bustype;
  447. input_dev->dev.parent = ts->dev;
  448. input_dev->open = cyttsp_open;
  449. input_dev->close = cyttsp_close;
  450. input_set_drvdata(input_dev, ts);
  451. __set_bit(EV_ABS, input_dev->evbit);
  452. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  453. 0, pdata->maxx, 0, 0);
  454. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  455. 0, pdata->maxy, 0, 0);
  456. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  457. 0, CY_MAXZ, 0, 0);
  458. input_mt_init_slots(input_dev, CY_MAX_ID);
  459. error = request_threaded_irq(ts->irq, NULL, cyttsp_irq,
  460. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  461. pdata->name, ts);
  462. if (error) {
  463. dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
  464. ts->irq, error);
  465. goto err_platform_exit;
  466. }
  467. disable_irq(ts->irq);
  468. error = cyttsp_power_on(ts);
  469. if (error)
  470. goto err_free_irq;
  471. error = input_register_device(input_dev);
  472. if (error) {
  473. dev_err(ts->dev, "failed to register input device: %d\n",
  474. error);
  475. goto err_free_irq;
  476. }
  477. return ts;
  478. err_free_irq:
  479. free_irq(ts->irq, ts);
  480. err_platform_exit:
  481. if (pdata->exit)
  482. pdata->exit();
  483. err_free_mem:
  484. input_free_device(input_dev);
  485. kfree(ts);
  486. err_out:
  487. return ERR_PTR(error);
  488. }
  489. EXPORT_SYMBOL_GPL(cyttsp_probe);
  490. void cyttsp_remove(struct cyttsp *ts)
  491. {
  492. free_irq(ts->irq, ts);
  493. input_unregister_device(ts->input);
  494. if (ts->pdata->exit)
  495. ts->pdata->exit();
  496. kfree(ts);
  497. }
  498. EXPORT_SYMBOL_GPL(cyttsp_remove);
  499. MODULE_LICENSE("GPL");
  500. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
  501. MODULE_AUTHOR("Cypress");