1. /* linux/drivers/input/touchscreen/cy8ctma300_touch.c
  2. *
  3. * Copyright (C) 2009 Sony Ericsson Mobile Communications, INC
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * <Driver name>
  15. * cy8ctma300 touchscreen driver
  16. *
  17. * <Supported firmware version>
  18. * TBD
  19. *
  20. * <Supported FPGA version>
  21. * TBD
  22. */
  23.  
  24.  
  25. #undef DEBUG
  26. #define DUMP_BUF_SHORT
  27.  
  28. #undef MEASURE_PERFORMANCE
  29.  
  30. #define MODULE_VER "0.9b-mt"
  31.  
  32. #include <linux/init.h>
  33. #include <linux/err.h>
  34. #include <linux/delay.h>
  35. #include <linux/input.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/slab.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/spi/cypress_touch.h>
  40. #include <linux/syscalls.h>
  41. #include <linux/cdev.h>
  42. #include <linux/device.h>
  43. #include <mach/gpio.h>
  44. #include <asm/irq.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/mutex.h>
  47.  
  48. #ifdef MEASURE_PERFORMANCE
  49. #include <linux/jiffies.h>
  50. #endif
  51.  
  52. #ifdef CONFIG_EARLYSUSPEND
  53. #include <linux/earlysuspend.h>
  54. #endif
  55.  
  56. #ifdef CONFIG_ARM
  57. #include <asm/mach-types.h>
  58. #endif
  59.  
  60. /* FIXME- Use firmware-loader facility */
  61. #include "cypress-firmware-0087.h"
  62.  
  63. /*--------------------------------------------------------------------------*/
  64.  
  65. /* Operating Mode Register */
  66. #define TP_REG_HST_MODE 0x00
  67.  
  68. #define TP_REG_TT_MODE 0x01
  69. #define TP_REG_TT_STAT 0x02
  70.  
  71. #define TP_REG_PNT1XH 0x03
  72. #define TP_REG_PNT1XL 0x04
  73. #define TP_REG_PNT1YH 0x05
  74. #define TP_REG_PNT1YL 0x06
  75. #define TP_REG_PNT1Z 0x07
  76.  
  77. #define TP_REG_PNT12_ID 0x08
  78.  
  79. #define TP_REG_PNT2XH 0x09
  80. #define TP_REG_PNT2XL 0x0A
  81. #define TP_REG_PNT2YH 0x0B
  82. #define TP_REG_PNT2YL 0x0C
  83. #define TP_REG_PNT2Z 0x0D
  84.  
  85. #define TP_REG_PNT3XH 0x10
  86. #define TP_REG_PNT3XL 0x11
  87. #define TP_REG_PNT3YH 0x12
  88. #define TP_REG_PNT3YL 0x13
  89. #define TP_REG_PNT3Z 0x14
  90.  
  91. #define TP_REG_PNT34_ID 0x15
  92.  
  93. #define TP_REG_PNT4XH 0x16
  94. #define TP_REG_PNT4XL 0x17
  95. #define TP_REG_PNT4YH 0x18
  96. #define TP_REG_PNT4YL 0x19
  97. #define TP_REG_PNT4Z 0x1A
  98.  
  99. #define TP_REG_GEST_CNT 0x0E
  100. #define TP_REG_GEST_ID 0x0F
  101. #define TP_REG_GEST_SET 0x1E
  102.  
  103. /* Gesture active distance. BEWARE: this is more than just for gestures */
  104. #define TP_GEST_ACTD 0x02
  105.  
  106. /* BIST Mode Register Set */
  107. #define TP_BISTR_BIST_STAT 0x01
  108. #define TP_BISTR_BIST_NUM 0x02
  109. /* ... */
  110. #define TP_BISTR_UID_0 0x07
  111. #define TP_BISTR_UID_1 0x08
  112. #define TP_BISTR_UID_2 0x09
  113. /* you get the idea ... */
  114. #define TP_BISTR_BL_VERH 0x0F
  115. #define TP_BISTR_BL_VERL 0x10
  116.  
  117. #define TP_BISTR_TTS_VERH 0x11
  118. #define TP_BISTR_TTS_VERL 0x12
  119.  
  120. #define TP_BISTR_AP_IDH 0x13
  121. #define TP_BISTR_AP_IDL 0x14
  122.  
  123. #define TP_BISTR_AP_VERH 0x15
  124. #define TP_BISTR_AP_VERL 0x16
  125.  
  126. #define TP_REG_FW 0xFF
  127.  
  128. /* more of these later if/as I need 'em */
  129.  
  130. #define TP_TM_BIT_BIST (1 << 4)
  131. #define TP_TM_BIT_SRESET (1 << 0)
  132. #define TP_TM_BIT_DEEPSLEEP (1 << 1)
  133.  
  134. #define TP_TT_BIT_DINVAL (1 << 5)
  135. #define TP_TT_BIT_FACE (1 << 4)
  136. #define TP_TT_BIT_TOUCH_MASK (0xF)
  137.  
  138. #define TP_BL_BIT_BLRDY (1 << 0)
  139. #define TP_BL_BIT_BM (1 << 1)
  140. #define TP_BL_BIT_ERR_MASK (~(TP_BL_BIT_BLRDY | TP_BL_BIT_BM))
  141.  
  142. /* if set alone in TP_REG_FW we're in application mode OK
  143. in later (> 0.65) firmwares. Sanity check */
  144. #define TP_AP_BIT_APPMODE (1 << 2)
  145.  
  146. #define FIRMWARE_BLOCK_SIZE (128)
  147. #define FIRMWARE_USER_BLOCKS (237)
  148. #define FIRMWARE_TOTAL_BLOCKS (256)
  149.  
  150. #define TOUCH_DATA_BYTES (TP_REG_PNT4Z)
  151.  
  152. #define BL_CMD_BL_ENTER (0x38)
  153. #define BL_CMD_WR_BLOCK (0x39)
  154. #define BL_CMD_BL_EXIT (0x3B)
  155. /* Big-Endian representation */
  156. #define BL_CMD_EXECUTE (0x53CA)
  157.  
  158. /* buffer valid bits, firmware 87+ 107+ */
  159. #define TP_SPI_BUFFER_VALID_MASK 0xC0 /* mask bits 6, 7 in reg 2 */
  160. #define TP_SPI_BUFFER_VALID 0x40 /* bit 6 set = accept data */
  161. #define TP_SPI_BUFFER_VALID_VER 87 /* FW version 87 and above */
  162.  
  163. #ifdef DEBUG
  164. #define DEBUG_PRINTK(format, args...) \
  165. { if (dump_noisy) \
  166. printk(format , ## args); }
  167. #else
  168. #define DEBUG_PRINTK(format, args...)
  169. #endif
  170.  
  171. #define ENABLE_IRQ(this) { if (!this->irq_suspend_enabled++) \
  172. enable_irq(this->pdata->irq); }
  173.  
  174. #define DISABLE_IRQ(this) { if (this->irq_suspend_enabled) {\
  175. disable_irq_nosync(this->pdata->irq); \
  176. this->irq_suspend_enabled = 0; } }
  177.  
  178. static int force_update;
  179. static int shit_firmware;
  180. module_param(force_update, int, 0);
  181. MODULE_PARM_DESC(force_update, "Force flashing of driver firmware");
  182.  
  183. /*--------------------------------------------------------------------------*/
  184.  
  185. struct cy8ctma300_touch {
  186. struct input_dev *input;
  187. spinlock_t lock;
  188. struct mutex mutex;
  189. struct spi_device *spi;
  190. struct work_struct fwupd_work;
  191. struct spi_message async_spi_message;
  192. struct spi_transfer async_spi_transfer;
  193. u8 async_read_buf[TOUCH_DATA_BYTES];
  194. struct cdev device_cdev;
  195. int device_major;
  196. struct class *device_class;
  197. int first_irq;
  198. #ifdef CONFIG_EARLYSUSPEND
  199. struct early_suspend early_suspend;
  200. #endif
  201. int irq_suspend_enabled;
  202. int init_complete;
  203. int has_been_initialized;
  204. struct cypress_touch_platform_data *pdata;
  205. #ifdef MEASURE_PERFORMANCE
  206. unsigned long measure_start;
  207. int measure_count;
  208. int measure_dupes;
  209. u16 dupe_x;
  210. u16 dupe_y;
  211. u8 dupe_touch;
  212. #endif
  213. u8 suspend;
  214. int use_spi_buffer_valid;
  215. };
  216.  
  217. struct fw_packet {
  218. u8 bl_keys[8];
  219. u8 bl_cmd;
  220. u16 bl_blockno;
  221. u8 bl_data[FIRMWARE_BLOCK_SIZE];
  222. u8 bl_data_csum;
  223. u8 bl_cmd_csum;
  224. u16 bl_execute;
  225. } __attribute__((packed));
  226.  
  227. /* send packet to get touch data from device */
  228. static u8 const cy8ctma300_send_msg[TOUCH_DATA_BYTES + 2] = { 0, 0, };
  229.  
  230. static u8 const bootloader_keys[] = { 0, 1, 2, 3, 4, 5, 6, 7, };
  231.  
  232. static int dump_noisy = 1;
  233.  
  234. static int cy8ctma300_deferred_init(struct cy8ctma300_touch *this);
  235. static int reset_device(struct cy8ctma300_touch *this);
  236. /*--------------------------------------------------------------------------*/
  237.  
  238. static void dump_buf(u8 *buf, unsigned len)
  239. {
  240. #ifdef DEBUG
  241. int i;
  242.  
  243. if (!len || !buf || !dump_noisy)
  244. return;
  245.  
  246. #ifdef DUMP_BUF_SHORT
  247. if (len > 12)
  248. len = 12;
  249. #endif
  250. printk(KERN_INFO "CY8CTMA300_TOUCH: dump_buf (%03d): ", len);
  251. for (i = 0; i < len; i++)
  252. printk("0x%02X ", buf[i]);
  253. printk("\n");
  254. #endif
  255. };
  256.  
  257. static int reg_write(struct spi_device *spi, u8 addr, u8* buf, size_t len)
  258. {
  259. int err = 0;
  260. u8 *write_buf;
  261.  
  262. DEBUG_PRINTK(KERN_DEBUG
  263. "CY8CTMA300_TOUCH: reg_write(0x%02X, %d)\n", addr, len);
  264.  
  265. if (!buf || !len)
  266. return -EINVAL;
  267.  
  268. write_buf = kzalloc(sizeof(u8) * (len + 2), GFP_KERNEL);
  269. if (!write_buf)
  270. return -ENOMEM;
  271.  
  272. dump_buf(buf, len);
  273.  
  274. write_buf[0] = 0x01;
  275. write_buf[1] = addr;
  276.  
  277. memcpy(&write_buf[2], buf, len);
  278.  
  279. err = spi_write(spi, write_buf, len + 2);
  280. msleep(10);
  281.  
  282. kfree(write_buf);
  283.  
  284. if (err)
  285. printk(KERN_ERR "CY8CTMA300_TOUCH: reg_write error %d\n", err);
  286.  
  287. return err;
  288. };
  289.  
  290. static inline int reg_write_byte(struct spi_device *spi, u8 addr, u8 data)
  291. {
  292. return reg_write(spi, addr, &data, 1);
  293. };
  294.  
  295. static int reg_read(struct spi_device *spi, u8 addr, u8* buf, size_t len)
  296. {
  297. int err = 0;
  298. u8 read_req[2];
  299. u8 *read_buf;
  300. size_t newlen;
  301.  
  302. DEBUG_PRINTK(KERN_DEBUG
  303. "CY8CTMA300_TOUCH: reg_read (0x%02X, %d)\n", addr, len);
  304.  
  305. if (!buf || !len)
  306. return -EINVAL;
  307.  
  308. /* re-buffer reads to ensure even-byte transfers */
  309. read_buf = kzalloc(sizeof(u8) * (newlen = len & 1 ? len + 1 : len),
  310. GFP_KERNEL);
  311. if (!read_buf)
  312. return -ENOMEM;
  313.  
  314. read_req[0] = 0x00;
  315. read_req[1] = addr;
  316.  
  317. err = spi_write(spi, read_req, 2);
  318. if (err) {
  319. printk(KERN_ERR
  320. "CY8CTMA300_TOUCH: reg_read() write err %d\n", err);
  321. goto out;
  322. };
  323. msleep(10);
  324.  
  325. err = spi_read(spi, read_buf, newlen);
  326. if (err) {
  327. printk(KERN_ERR
  328. "CY8CTMA300_TOUCH: reg_read() read err %d\n", err);
  329. } else {
  330. memcpy(buf, read_buf, len);
  331. dump_buf(buf, len);
  332. };
  333.  
  334. out:
  335. kfree(read_buf);
  336. return err;
  337. };
  338.  
  339. static int reg_read_intcause_async(struct spi_device *spi, void* complete)
  340. {
  341. struct cy8ctma300_touch *this = dev_get_drvdata(&spi->dev);
  342.  
  343. DEBUG_PRINTK(KERN_DEBUG
  344. "CY8CTMA300_TOUCH: reg_read_intcause_async()\n");
  345.  
  346. this->async_spi_transfer.tx_buf = cy8ctma300_send_msg;
  347. this->async_spi_transfer.rx_buf = this->async_read_buf;
  348. this->async_spi_transfer.len = TOUCH_DATA_BYTES;
  349.  
  350. spi_message_init(&this->async_spi_message);
  351. spi_message_add_tail(&this->async_spi_transfer,
  352. &this->async_spi_message);
  353. this->async_spi_message.complete = complete;
  354. this->async_spi_message.context = (void *)this;
  355.  
  356. return spi_async(spi, &this->async_spi_message);
  357. };
  358.  
  359. static int bl_flash_mode(struct spi_device *spi)
  360. {
  361. int err;
  362. u8 status;
  363.  
  364. mdelay(100);
  365.  
  366. err = reg_read(spi, TP_REG_FW, &status, 1);
  367. if (err)
  368. return err;
  369.  
  370. if (status & TP_BL_BIT_ERR_MASK)
  371. return status;
  372.  
  373. return ((status == (TP_BL_BIT_BM | TP_BL_BIT_BLRDY)) ? 0 : 1);
  374. };
  375.  
  376. static void do_checksum(struct fw_packet *pk)
  377. {
  378. u8 *fw_hdr;
  379.  
  380. pk->bl_data_csum = 0;
  381. pk->bl_cmd_csum = TP_REG_FW; /* calc includes the FW addr byte */
  382.  
  383. for (fw_hdr = &pk->bl_keys[0]; fw_hdr < &pk->bl_data[0]; fw_hdr++)
  384. pk->bl_cmd_csum += *fw_hdr;
  385. for (; fw_hdr < &pk->bl_data_csum; fw_hdr++)
  386. pk->bl_data_csum += *fw_hdr;
  387.  
  388. /* checksum data val, then the csum byte itself */
  389. pk->bl_cmd_csum += (pk->bl_data_csum * 2);
  390. };
  391.  
  392. static int write_firmware_block(struct spi_device *spi, u8 *block, u16 blockno)
  393. {
  394. struct fw_packet pk;
  395. int err = 0;
  396.  
  397. /* once bitten, twice shy */
  398. if (!blockno)
  399. return -EROFS;
  400.  
  401. if (blockno >= FIRMWARE_TOTAL_BLOCKS)
  402. return -ERANGE;
  403.  
  404. memcpy(&pk.bl_keys, bootloader_keys, sizeof(pk.bl_keys));
  405. pk.bl_cmd = BL_CMD_WR_BLOCK;
  406. pk.bl_blockno = (blockno << 8) | (blockno >> 8);
  407. memcpy(&pk.bl_data, block, FIRMWARE_BLOCK_SIZE);
  408. do_checksum(&pk);
  409. pk.bl_execute = BL_CMD_EXECUTE;
  410.  
  411. err = reg_write(spi, TP_REG_FW, (u8 *)&pk, sizeof(struct fw_packet));
  412. if (err)
  413. return err;
  414.  
  415. err = bl_flash_mode(spi);
  416. if (err < 0)
  417. return err;
  418.  
  419. if (err)
  420. return -EPROTO;
  421.  
  422. return err;
  423. };
  424.  
  425. /* sanity check on stored firmware file. Not necessary now but will be for
  426. when the file is loaded the right way */
  427. static int verify_fw_checksum(void)
  428. {
  429. int i;
  430. u16 mastercsum = 0;
  431. u8 *p;
  432.  
  433. /* calculate CSUM from 2nd block thru # of user blocks */
  434. p = &cypress_firmware[FIRMWARE_BLOCK_SIZE];
  435. i = FIRMWARE_USER_BLOCKS * FIRMWARE_BLOCK_SIZE;
  436.  
  437. while (i--)
  438. mastercsum += *p++;
  439.  
  440. p = &cypress_firmware[sizeof(cypress_firmware) - 2];
  441. if (mastercsum != (u16)((*p << 8) | *(p + 1))) {
  442. printk(KERN_ERR "CY8CTMA300_TOUCH: firmware file checksum error\n");
  443. return 1;
  444. };
  445.  
  446. return 0;
  447. };
  448.  
  449. static int send_bootloader_cmd(struct spi_device *spi, u8 cmd)
  450. {
  451. struct fw_packet packet;
  452. int err;
  453.  
  454. memcpy(&packet.bl_keys, bootloader_keys, sizeof(bootloader_keys));
  455. packet.bl_cmd = cmd;
  456. err = reg_write(spi, TP_REG_FW, (u8 *) &packet,
  457. sizeof(bootloader_keys) + 1);
  458. if (err)
  459. printk(KERN_ERR
  460. "CY8CTMA300_TOUCH: send_bootloader_cmd write: %d\n",
  461. err);
  462.  
  463. return err;
  464. };
  465.  
  466. static int perform_reset(struct spi_device *spi)
  467. {
  468. int i = 5000000;
  469. int loops = 0, cur_val, new_val = 0xFF;
  470. struct cypress_touch_platform_data *pdata = spi->dev.platform_data;
  471.  
  472. gpio_set_value(pdata->gpio_reset_pin, 0);
  473. udelay(100);
  474. gpio_set_value(pdata->gpio_reset_pin, 1);
  475.  
  476. cur_val = gpio_get_value(pdata->gpio_irq_pin);
  477. do {
  478. while (i-- && (cur_val ==
  479. (new_val = gpio_get_value(pdata->gpio_irq_pin))))
  480. ;
  481.  
  482. DEBUG_PRINTK(KERN_INFO
  483. "CY8CTMA300_TOUCH: loop %d val %d remain %d\n",
  484. loops, cur_val, i);
  485. cur_val = new_val;
  486.  
  487. /* successful reset is 2 successful pulses before timeout */
  488. if (++loops == 4) {
  489. mdelay(100);
  490. /* some old firmware ACKs with IRQ before
  491. really ready */
  492. if (shit_firmware)
  493. mdelay(2000);
  494. return 0;
  495. };
  496. } while (i > 0);
  497.  
  498. /* loops == 2 means bootloader failed (usually checksum) and we're in
  499. BL mode (ret 1), application won't start.
  500. Anything else is a bad TP (ret -1) */
  501. return (loops == 2) ? 1 : -1;
  502.  
  503. };
  504.  
  505. static int update_firmware(struct spi_device *spi)
  506. {
  507. u16 i;
  508. int err = 0;
  509. int tries = 0;
  510.  
  511. if (verify_fw_checksum())
  512. return -EBADF;
  513.  
  514. retry:
  515. /* try doing application mode first */
  516. reg_write_byte(spi, TP_REG_HST_MODE, 0);
  517. mdelay(100);
  518.  
  519. /* try entering bootloader mode */
  520. err = send_bootloader_cmd(spi, BL_CMD_BL_ENTER);
  521. if (err)
  522. return err;
  523.  
  524. /* check status */
  525. err = bl_flash_mode(spi);
  526. if (err) {
  527. if (err < 0) {
  528. printk(KERN_ERR
  529. "CY8CTMA300_TOUCH: "
  530. "bootloader enter read: %d\n",
  531. err);
  532. return err;
  533. } else {
  534. printk(KERN_ERR
  535. "CY8CTMA300_TOUCH: "
  536. "bootloader enter status: %d\n",
  537. err);
  538. if (tries++ < 2) {
  539. printk(KERN_ERR "CY8CTMA300_TOUCH: Retrying\n");
  540. perform_reset(spi);
  541. goto retry;
  542. }
  543. return -EPROTO;
  544. };
  545. };
  546.  
  547. printk(KERN_INFO "CY8CTMA300_TOUCH: loading firmware: please wait\n");
  548. dump_noisy = 0;
  549. /* try programming the device */
  550. for (i = 1; i < FIRMWARE_TOTAL_BLOCKS; i++) {
  551.  
  552. /* if we've programmed all the Application blocks,
  553. program the last block */
  554. if (i > FIRMWARE_USER_BLOCKS)
  555. i = FIRMWARE_TOTAL_BLOCKS - 1;
  556.  
  557. err = write_firmware_block(spi,
  558. &cypress_firmware[i * FIRMWARE_BLOCK_SIZE], i);
  559. if (err) {
  560. printk(KERN_ERR
  561. "CY8CTMA300_TOUCH: "
  562. "firmware write error %d\n", err);
  563. return err;
  564. };
  565.  
  566. };
  567. dump_noisy = 1;
  568.  
  569. /* Exiting BL mode no longer needed per Cypress;
  570. it performs the same as the reset below
  571. err = send_bootloader_cmd(spi, BL_CMD_BL_EXIT);
  572. */
  573.  
  574. if (!err) {
  575. printk(KERN_INFO "CY8CTMA300_TOUCH: loading firmware: done\n");
  576. err = perform_reset(spi);
  577. }
  578.  
  579. return err;
  580. };
  581.  
  582. static int query_chip(struct spi_device *spi)
  583. {
  584. u8 j[16];
  585. u16 blver, appver;
  586. struct cypress_touch_platform_data *pdata = spi->dev.platform_data;
  587. struct cy8ctma300_touch *this = dev_get_drvdata(&spi->dev);
  588.  
  589. reg_write_byte(spi, TP_REG_HST_MODE, TP_TM_BIT_BIST);
  590. mdelay(100);
  591. reg_read(spi, TP_REG_HST_MODE, j, 1);
  592. if (j[0] != TP_TM_BIT_BIST)
  593. return -EIO;
  594.  
  595. reg_read(spi, TP_BISTR_BL_VERH, j, 2);
  596. printk(KERN_INFO
  597. "CY8CTMA300_TOUCH: Bootloader FW Ver %d.%d\n", j[0], j[1]);
  598. blver = j[0] << 8 | j[1];
  599. reg_read(spi, TP_BISTR_AP_VERH, j, 2);
  600. printk(KERN_INFO
  601. "CY8CTMA300_TOUCH: Application FW Ver %d.%d\n", j[0], j[1]);
  602. appver = j[0] << 8 | j[1];
  603. reg_read(spi, TP_BISTR_UID_0, j, 2);
  604. printk(KERN_INFO
  605. "CY8CTMA300_TOUCH: Silicon Revision %c\n", ((j[0] == 0x05)
  606. && (j[1] == 0x80)) ? 'D' : 'E');
  607.  
  608. /* when firmware is 87 or above, use SPI buffer valid check */
  609. this->use_spi_buffer_valid = (TP_SPI_BUFFER_VALID_VER <= appver);
  610.  
  611. if (pdata->no_fw_update == 0) {
  612. if (appver < 58) {
  613. printk(KERN_INFO "CY8CTMA300_TOUCH: old FW version, applying reset workarounds\n");
  614. shit_firmware++;
  615. };
  616.  
  617. DEBUG_PRINTK(KERN_INFO
  618. "CY8CTMA300_TOUCH: about to compare FW versions\n");
  619. if ((blver == 1) && (appver != FW_VERSION_MINOR)) {
  620. force_update = 1;
  621. return 1;
  622. } else if (appver == FW_VERSION_MINOR) {
  623. printk(KERN_ERR "CY8CTMA300_TOUCH: firmware versions match.\n");
  624. };
  625. } else {
  626. printk(KERN_INFO "CY8CTMA300_TOUCH: no FW update allowed.\n");
  627. };
  628.  
  629. DEBUG_PRINTK(KERN_INFO
  630. "CY8CTMA300_TOUCH: about to re-enter normal mode\n");
  631. /* now put chip back into Normal mode */
  632. reg_write_byte(spi, TP_REG_HST_MODE, 0);
  633. mdelay(100);
  634. reg_read(spi, TP_REG_HST_MODE, j, 1);
  635. if (j[0] != TP_REG_HST_MODE)
  636. return -EIO;
  637.  
  638. return 0;
  639. }
  640. static void cy8ctma300_fwupd_work(struct work_struct *work)
  641. {
  642. struct cy8ctma300_touch *this =
  643. container_of(work, struct cy8ctma300_touch, fwupd_work);
  644. struct spi_device *spi = this->spi;
  645. int err;
  646. u8 j[16];
  647.  
  648. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: cy8ctma300_fwupd_work()\n");
  649.  
  650. mdelay(100);
  651. err = update_firmware(spi);
  652. if (err) {
  653. printk(KERN_ERR
  654. "CY8CTMA300_TOUCH: "
  655. "firmware update failed %d, not registering device\n",
  656. err);
  657. return;
  658. };
  659.  
  660. /* try to put device into BIST mode */
  661. /* have to reset FW status byte first,
  662. as current firmware locks otherwise */
  663. reg_read(spi, TP_REG_FW, j, 1);
  664. printk(KERN_INFO "CY8CTMA300_TOUCH: TP_REG_FW 0x%02X\n", j[0]);
  665.  
  666. err = query_chip(spi);
  667. if (!err) {
  668.  
  669. if (j[0] == TP_AP_BIT_APPMODE) {
  670. DEBUG_PRINTK(KERN_INFO
  671. "CY8CTMA300_TOUCH: have sane firmware rev now");
  672. if (shit_firmware) {
  673. DEBUG_PRINTK(", clearing bad-FW flag");
  674. shit_firmware = 0;
  675. };
  676.  
  677. DEBUG_PRINTK("\n");
  678. };
  679.  
  680. cy8ctma300_deferred_init(this);
  681. } else {
  682. printk(KERN_ERR
  683. "CY8CTMA300_TOUCH: cy8ctma300_fwupd_work():"
  684. " chip query failed %d\n", err);
  685. this->init_complete++;
  686. };
  687. };
  688.  
  689. static int reset_device(struct cy8ctma300_touch *this)
  690. {
  691. struct spi_device *spi = this->spi;
  692. int reset_retries = 2;
  693. int err = 0;
  694.  
  695. u8 j[16];
  696.  
  697. reset_chip:
  698. if ((perform_reset(spi) < 0) || !reset_retries--)
  699. return -ENOMEDIUM;
  700.  
  701. /* check FW status and attempt to flash chip if necessary */
  702. reg_read(spi, TP_REG_FW, j, 1);
  703. printk(KERN_INFO "CY8CTMA300_TOUCH: TP_REG_FW 0x%02X\n", j[0]);
  704.  
  705. /* check for the case of older firmware that's not quite ready yet */
  706. if (j[0] == 0xFF) {
  707. shit_firmware++;
  708. goto reset_chip;
  709. };
  710.  
  711. if ((force_update-- > 0) || (j[0] & (TP_BL_BIT_BLRDY | TP_BL_BIT_BM)))
  712. return 1;
  713.  
  714. err = query_chip(spi);
  715. if (err < 0) {
  716. shit_firmware++;
  717. goto reset_chip;
  718. };
  719.  
  720. return err;
  721. };
  722.  
  723. static void cy8ctma300_send_input(struct cy8ctma300_touch *this, u8 down,
  724. u16 x, u16 y, u16 id)
  725. {
  726. DEBUG_PRINTK(KERN_INFO
  727. "CY8CTMA300_TOUCH: down %u x %u y %u id %u\n", down, x, y, id);
  728.  
  729. if (down != 1)
  730. input_mt_sync(this->input);
  731.  
  732. input_report_abs(this->input, ABS_MT_POSITION_X, x);
  733. input_report_abs(this->input, ABS_MT_POSITION_Y, y);
  734. input_report_abs(this->input, ABS_MT_TOUCH_MAJOR, 1);
  735. input_report_abs(this->input, ABS_MT_TOUCH_MINOR, 1);
  736. input_report_abs(this->input, ABS_MT_TRACKING_ID, id - 1);
  737.  
  738. };
  739.  
  740. static void cy8ctma300_touch_isr(void *context)
  741. {
  742. struct cy8ctma300_touch *this = (struct cy8ctma300_touch *)context;
  743. struct cypress_touch_platform_data
  744. *pdata = this->spi->dev.platform_data;
  745. u8 read_buf[TOUCH_DATA_BYTES];
  746. u16 xp = 0, yp = 0, id = 0;
  747. u8 down;
  748.  
  749. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: cy8ctma300_touch_isr()\n");
  750. mutex_lock(&this->mutex);
  751. if (this->suspend) {
  752. mutex_unlock(&this->mutex);
  753. return;
  754. }
  755.  
  756. memcpy(read_buf, this->async_read_buf, TOUCH_DATA_BYTES);
  757. dump_buf(read_buf, TOUCH_DATA_BYTES);
  758.  
  759. if (read_buf[TP_REG_TT_MODE] & TP_TT_BIT_DINVAL) {
  760. printk(KERN_INFO "CY8CTMA300_TOUCH: invalid data?\n");
  761. goto out;
  762. };
  763.  
  764. /*
  765. * For SSD FW-87+ and DSD-FW 107+, check bitamsk in 3rd byte
  766. * for 01 in bits [7:6] to accept the data
  767. * i.e. if (BYTE[2] & 0xC0 == 0x40) accept data, else reject
  768. */
  769.  
  770. if (this->use_spi_buffer_valid) {
  771. if ((read_buf[TP_REG_TT_STAT] & TP_SPI_BUFFER_VALID_MASK) !=
  772. TP_SPI_BUFFER_VALID) {
  773. dev_info(&this->spi->dev,
  774. "CY8CTMA300_TOUCH: rejected data.\n");
  775. goto out;
  776. }
  777. }
  778.  
  779. down = read_buf[TP_REG_TT_STAT] & TP_TT_BIT_TOUCH_MASK;
  780. xp = read_buf[TP_REG_PNT1XL] | (read_buf[TP_REG_PNT1XH] << 8);
  781. yp = read_buf[TP_REG_PNT1YL] | (read_buf[TP_REG_PNT1YH] << 8);
  782. yp = (pdata->y_max) - yp;
  783. id = read_buf[TP_REG_PNT12_ID] >> 4 & 0xF;
  784.  
  785. input_report_key(this->input, BTN_TOUCH, down ? 1 : 0);
  786.  
  787. if (down == 1) {
  788. input_report_abs(this->input, ABS_X, xp);
  789. input_report_abs(this->input, ABS_Y, yp);
  790. };
  791.  
  792. if (down) {
  793. cy8ctma300_send_input(this, down, xp, yp, id);
  794.  
  795. if (down > 1) {
  796. xp = read_buf[TP_REG_PNT2XL] |
  797. (read_buf[TP_REG_PNT2XH] << 8);
  798. yp = read_buf[TP_REG_PNT2YL] |
  799. (read_buf[TP_REG_PNT2YH] << 8);
  800. yp = (pdata->y_max) - yp;
  801. id = read_buf[TP_REG_PNT12_ID] & 0xF;
  802. cy8ctma300_send_input(this, down, xp, yp, id);
  803. };
  804.  
  805. if (down > 2) {
  806. xp = read_buf[TP_REG_PNT3XL] |
  807. (read_buf[TP_REG_PNT3XH] << 8);
  808. yp = read_buf[TP_REG_PNT3YL] |
  809. (read_buf[TP_REG_PNT3YH] << 8);
  810. yp = (pdata->y_max) - yp;
  811. id = read_buf[TP_REG_PNT34_ID] >> 4 & 0xF;
  812. cy8ctma300_send_input(this, down, xp, yp, id);
  813. };
  814.  
  815. if (down > 3) {
  816. xp = read_buf[TP_REG_PNT4XL] |
  817. (read_buf[TP_REG_PNT4XH] << 8);
  818. yp = read_buf[TP_REG_PNT4YL] |
  819. (read_buf[TP_REG_PNT4YH] << 8);
  820. yp = (pdata->y_max) - yp;
  821. id = read_buf[TP_REG_PNT34_ID] & 0xF;
  822. cy8ctma300_send_input(this, down, xp, yp, id);
  823. };
  824. };
  825.  
  826. input_mt_sync(this->input);
  827. input_sync(this->input);
  828.  
  829. #ifdef MEASURE_PERFORMANCE
  830. if (this->dupe_x == xp && this->dupe_y == yp &&
  831. this->dupe_touch == (down ? 1 : 0))
  832. ++this->measure_dupes;
  833.  
  834. this->dupe_x = xp;
  835. this->dupe_y = yp;
  836. this->dupe_touch = (down ? 1 : 0);
  837. #endif
  838.  
  839. DEBUG_PRINTK(KERN_INFO
  840. "CY8CTMA300_TOUCH: touch %s at (%3d, %3d)\n",
  841. down ? "down" : "up ", xp, yp);
  842. #ifdef MEASURE_PERFORMANCE
  843. if (this->measure_start < jiffies) {
  844. printk(KERN_INFO
  845. "CY8CTMA300_TOUCH: "
  846. "%d interrupts/events per second and %d dupes\n",
  847. this->measure_count, this->measure_dupes);
  848. this->measure_start = jiffies + msecs_to_jiffies(1000);
  849. this->measure_count = 0;
  850. this->measure_dupes = 0;
  851. }
  852. ++this->measure_count;
  853. #endif
  854.  
  855. out:
  856. mutex_unlock(&this->mutex);
  857. ENABLE_IRQ(this);
  858. };
  859.  
  860. static irqreturn_t cy8ctma300_touch_irq(int irq, void *handle)
  861. {
  862. struct cy8ctma300_touch *this = handle;
  863. unsigned long flags;
  864.  
  865. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: cy8ctma300_touch_irq()\n");
  866.  
  867. /* first IRQ is side-effect of reset operation */
  868. if (!this->first_irq++)
  869. return IRQ_HANDLED;
  870.  
  871. DISABLE_IRQ(this);
  872. spin_lock_irqsave(&this->lock, flags);
  873. reg_read_intcause_async(this->spi, cy8ctma300_touch_isr);
  874. spin_unlock_irqrestore(&this->lock, flags);
  875.  
  876. return IRQ_HANDLED;
  877. };
  878.  
  879. /*--------------------------------------------------------------------------*/
  880.  
  881. #ifdef CONFIG_PM
  882. static int cy8ctma300_touch_suspend(struct spi_device *spi,
  883. pm_message_t message)
  884. {
  885. struct cy8ctma300_touch *this = dev_get_drvdata(&spi->dev);
  886.  
  887. DEBUG_PRINTK(KERN_INFO "CY8CTMA300_TOUCH: %s()\n", __func__);
  888. DISABLE_IRQ(this);
  889.  
  890. mutex_lock(&this->mutex);
  891. this->suspend = 1;
  892. mutex_unlock(&this->mutex);
  893.  
  894. return reg_write_byte(spi, TP_REG_HST_MODE, TP_TM_BIT_DEEPSLEEP);
  895. }
  896.  
  897. static int cy8ctma300_touch_resume(struct spi_device *spi)
  898. {
  899. int rc;
  900. u8 data;
  901. struct cy8ctma300_touch *this = dev_get_drvdata(&spi->dev);
  902. DEBUG_PRINTK(KERN_INFO "CY8CTMA300_TOUCH: %s()\n", __func__);
  903.  
  904. this->suspend = 0;
  905. ENABLE_IRQ(this);
  906.  
  907. rc = reg_write_byte(spi, TP_REG_HST_MODE, 0);
  908. mdelay(100);
  909. reg_read(spi, TP_REG_HST_MODE, &data, 1);
  910. if (data != TP_REG_HST_MODE)
  911. return -ENODEV;
  912.  
  913. return rc;
  914. };
  915. #endif
  916.  
  917. #if defined(CONFIG_EARLYSUSPEND) && defined(CONFIG_PM)
  918. static void cy8ctma300_touch_early_suspend(struct early_suspend *es)
  919. {
  920. struct cy8ctma300_touch *this;
  921. this = container_of(es, struct cy8ctma300_touch, early_suspend);
  922.  
  923. DEBUG_PRINTK(KERN_INFO "CY8CTMA300_TOUCH: %s()\n", __func__);
  924. dev_dbg(&this->spi->dev, "CY8CTMA300_TOUCH: early suspend\n");
  925.  
  926. cy8ctma300_touch_suspend(this->spi, PMSG_SUSPEND);
  927. };
  928.  
  929. static void cy8ctma300_touch_late_resume(struct early_suspend *es)
  930. {
  931. struct cy8ctma300_touch *this;
  932. this = container_of(es, struct cy8ctma300_touch, early_suspend);
  933.  
  934. DEBUG_PRINTK(KERN_INFO "CY8CTMA300_TOUCH: %s()\n", __func__);
  935. dev_dbg(&this->spi->dev, "CY8CTMA300_TOUCH: late resume\n");
  936.  
  937. cy8ctma300_touch_resume(this->spi);
  938. };
  939. #endif /* #ifdef CONFIG_EARLYSUSPEND */
  940.  
  941. static int cy8ctma300_touch_open(struct inode *inode, struct file *file)
  942. {
  943. struct cy8ctma300_touch *this =
  944. container_of(inode->i_cdev, struct cy8ctma300_touch,
  945. device_cdev);
  946. file->private_data = this;
  947. return 0;
  948. };
  949.  
  950. static int cy8ctma300_touch_release(struct inode *inode, struct file *file)
  951. {
  952. return 0;
  953. };
  954.  
  955. static ssize_t cy8ctma300_touch_ioctl(struct inode *inode, struct file *file,
  956. unsigned int cmd, unsigned long arg)
  957. {
  958. int err = 0;
  959.  
  960. switch (cmd) {
  961.  
  962. default:
  963. printk(KERN_ERR "CY8CTMA300_TOUCH: ioctl, cmd error\n");
  964. return -EINVAL;
  965. break;
  966. }
  967.  
  968. return err;
  969. };
  970.  
  971. static const struct file_operations cy8ctma300_touch_fops = {
  972. .owner = THIS_MODULE,
  973. .open = cy8ctma300_touch_open,
  974. .ioctl = cy8ctma300_touch_ioctl,
  975. .release = cy8ctma300_touch_release,
  976. };
  977.  
  978. static int cy8ctma300_deferred_init(struct cy8ctma300_touch *this)
  979. {
  980.  
  981. struct input_dev *input_dev;
  982. struct spi_device *spi = this->spi;
  983. struct cypress_touch_platform_data *pdata = spi->dev.platform_data;
  984. dev_t device_t = MKDEV(0, 0);
  985. struct device *class_dev_t = NULL;
  986. int err = 0;
  987.  
  988. if (this->has_been_initialized)
  989. return 0;
  990.  
  991. input_dev = input_allocate_device();
  992. if (!input_dev) {
  993. err = -ENOMEM;
  994. goto err_cleanup_mem;
  995. }
  996. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Allocated input device\n");
  997.  
  998. this->input = input_dev;
  999.  
  1000. input_dev->name = "cy8ctma300_touch";
  1001. input_dev->phys = "cy8ctma300_touch/input0";
  1002. input_dev->dev.parent = &spi->dev;
  1003.  
  1004. set_bit(EV_SYN, input_dev->evbit);
  1005. set_bit(EV_KEY, input_dev->evbit);
  1006. set_bit(EV_ABS, input_dev->evbit);
  1007.  
  1008. set_bit(BTN_TOUCH, input_dev->keybit);
  1009. set_bit(BTN_2, input_dev->keybit);
  1010.  
  1011. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  1012. pdata->x_min, pdata->x_max, 0, 0);
  1013. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  1014. pdata->y_min, pdata->y_max, 0, 0);
  1015. input_set_abs_params(this->input, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0);
  1016. input_set_abs_params(this->input, ABS_MT_TOUCH_MINOR, 0, 1, 0, 0);
  1017. set_bit(ABS_MT_TRACKING_ID, this->input->absbit);
  1018.  
  1019. /* Single-Touch stuff */
  1020. input_set_abs_params(input_dev, ABS_X, pdata->x_min,
  1021. pdata->x_max, 0, 0);
  1022. input_set_abs_params(input_dev, ABS_Y, pdata->y_min,
  1023. pdata->y_max, 0, 0);
  1024. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0, 0, 0);
  1025.  
  1026. err = input_register_device(input_dev);
  1027. if (err)
  1028. goto err_cleanup_mem;
  1029.  
  1030. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Registered input device\n");
  1031.  
  1032. err = alloc_chrdev_region(&device_t, 0, 1, "cy8ctma300_touch");
  1033. if (err)
  1034. goto err_cleanup_input;
  1035.  
  1036. DEBUG_PRINTK(KERN_DEBUG
  1037. "CY8CTMA300_TOUCH: Allocated character device\n");
  1038.  
  1039. this->device_major = MAJOR(device_t);
  1040.  
  1041. cdev_init(&(this->device_cdev), &cy8ctma300_touch_fops);
  1042. this->device_cdev.owner = THIS_MODULE;
  1043. this->device_cdev.ops = &cy8ctma300_touch_fops;
  1044. err = cdev_add(&(this->device_cdev), MKDEV(this->device_major, 0), 1);
  1045. if (err)
  1046. goto err_cleanup_chrdev;
  1047.  
  1048. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Character device added\n");
  1049.  
  1050. this->device_class = class_create(THIS_MODULE, "cy8ctma300_touch");
  1051. if (IS_ERR(this->device_class)) {
  1052. err = -1;
  1053. goto err_cleanup_cdev;
  1054. };
  1055. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Class created\n");
  1056.  
  1057. class_dev_t = device_create(this->device_class, NULL,
  1058. MKDEV(this->device_major, 0), NULL, "cy8ctma300_touch");
  1059. if (IS_ERR(class_dev_t)) {
  1060. err = -1;
  1061. goto err_cleanup_class;
  1062. }
  1063. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Device created\n");
  1064.  
  1065. #ifdef CONFIG_EARLYSUSPEND
  1066. /* register early suspend */
  1067. this->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  1068. this->early_suspend.suspend = cy8ctma300_touch_early_suspend;
  1069. this->early_suspend.resume = cy8ctma300_touch_late_resume;
  1070. register_early_suspend(&this->early_suspend);
  1071. #endif /* #ifdef CONFIG_EARLYSUSPEND */
  1072.  
  1073. err = request_irq(pdata->irq, cy8ctma300_touch_irq, pdata->irq_polarity,
  1074. this->spi->dev.driver->name, this);
  1075. if (err) {
  1076. dev_err(&this->spi->dev, "irq %d busy?\n", pdata->irq);
  1077. goto err_cleanup_device;
  1078. };
  1079. /* IRQs are enabled as a side-effect of requesting */
  1080. this->irq_suspend_enabled++;
  1081. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Registered IRQ\n");
  1082.  
  1083. /* Set the gesture active distance */
  1084. reg_write_byte(this->spi, TP_REG_GEST_SET, TP_GEST_ACTD);
  1085.  
  1086. DEBUG_PRINTK(KERN_DEBUG
  1087. "CY8CTMA300_TOUCH: cy8ctma300_deferred_init() ok\n");
  1088. this->init_complete++;
  1089. this->has_been_initialized = 1;
  1090. return 0;
  1091.  
  1092. err_cleanup_device:
  1093. #ifdef CONFIG_EARLYSUSPEND
  1094. unregister_early_suspend(&this->early_suspend);
  1095. #endif
  1096. device_destroy(this->device_class, MKDEV(this->device_major, 0));
  1097. err_cleanup_class:
  1098. class_destroy(this->device_class);
  1099. err_cleanup_cdev:
  1100. cdev_del(&(this->device_cdev));
  1101. err_cleanup_chrdev:
  1102. unregister_chrdev_region(device_t, 1);
  1103. err_cleanup_input:
  1104. input_unregister_device(input_dev);
  1105. err_cleanup_mem:
  1106. if (input_dev)
  1107. input_free_device(input_dev);
  1108.  
  1109. return err;
  1110. };
  1111.  
  1112. static int cy8ctma300_touch_probe(struct spi_device *spi)
  1113. {
  1114. struct cypress_touch_platform_data *pdata = spi->dev.platform_data;
  1115. struct cy8ctma300_touch *this;
  1116. int err;
  1117.  
  1118. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: cy8ctma300_touch_probe()\n");
  1119.  
  1120. if (!pdata) {
  1121. dev_dbg(&spi->dev, "no platform data?\n");
  1122. return -ENODEV;
  1123. };
  1124.  
  1125. if (!pdata->irq) {
  1126. dev_dbg(&spi->dev, "no IRQ?\n");
  1127. return -ENODEV;
  1128. }
  1129.  
  1130. /* Set up SPI */
  1131. spi->bits_per_word = 8;
  1132. spi->mode = SPI_MODE_0;
  1133. err = spi_setup(spi);
  1134. if (err < 0)
  1135. return err;
  1136.  
  1137. printk(KERN_DEBUG
  1138. "CY8CTMA300_TOUCH: SPI setup (requesting %uHz) OK\n",
  1139. spi->max_speed_hz);
  1140.  
  1141. /* GPIO: set up */
  1142. DEBUG_PRINTK(KERN_DEBUG
  1143. "CY8CTMA300_TOUCH: Requesting GPIO Reset ownership\n");
  1144. err = gpio_request(pdata->gpio_reset_pin,
  1145. "cy8ctma300_touch_gpio_reset");
  1146. if (err)
  1147. goto err_gpio_setup;
  1148.  
  1149. DEBUG_PRINTK(KERN_DEBUG
  1150. "CY8CTMA300_TOUCH: Requesting GPIO IRQ ownership\n");
  1151. err = gpio_request(pdata->gpio_irq_pin, "cy8ctma300_touch_gpio_irq");
  1152. if (err)
  1153. goto err_gpio_setup;
  1154.  
  1155. DEBUG_PRINTK(KERN_DEBUG
  1156. "CY8CTMA300_TOUCH: Configuring GPIO Reset direction\n");
  1157. err = gpio_direction_output(pdata->gpio_reset_pin, 0);
  1158. if (err)
  1159. goto err_gpio_setup;
  1160.  
  1161. DEBUG_PRINTK(KERN_DEBUG
  1162. "CY8CTMA300_TOUCH: Configuring GPIO IRQ direction\n");
  1163. err = gpio_direction_input(pdata->gpio_irq_pin);
  1164. if (err)
  1165. goto err_gpio_setup;
  1166.  
  1167. this = kzalloc(sizeof(struct cy8ctma300_touch), GFP_KERNEL);
  1168. if (!this) {
  1169. err = -ENOMEM;
  1170. goto err_gpio_setup;
  1171. };
  1172. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Allocated private data\n");
  1173.  
  1174. this->pdata = pdata;
  1175. this->spi = spi;
  1176. spin_lock_init(&this->lock);
  1177. mutex_init(&this->mutex);
  1178. dev_set_drvdata(&spi->dev, this);
  1179. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Driver data set\n");
  1180.  
  1181. /* register firmware validation and initialization to workqueue */
  1182. INIT_WORK(&this->fwupd_work, cy8ctma300_fwupd_work);
  1183. DEBUG_PRINTK(KERN_DEBUG
  1184. "CY8CTMA300_TOUCH: Firmware-update work queue init OK\n");
  1185.  
  1186. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: About to reset device\n");
  1187. err = reset_device(this);
  1188. if (err < 0)
  1189. goto err_mem_free;
  1190.  
  1191. if (!err) {
  1192. DEBUG_PRINTK(KERN_DEBUG "CY8CTMA300_TOUCH: Chip reset OK\n");
  1193. err = cy8ctma300_deferred_init(this);
  1194. if (err) {
  1195. goto err_mem_free;
  1196. } else {
  1197. printk(KERN_INFO "CY8CTMA300_TOUCH: Device registered OK\n");
  1198. return 0;
  1199. }
  1200. } else {
  1201. printk(KERN_INFO "CY8CTMA300_TOUCH: Deferring device init post-FW Update\n");
  1202. schedule_work(&this->fwupd_work);
  1203. return 0;
  1204. };
  1205.  
  1206. err_mem_free:
  1207. kfree(this);
  1208. err_gpio_setup:
  1209. gpio_free(pdata->gpio_reset_pin);
  1210. gpio_free(pdata->gpio_irq_pin);
  1211. printk(KERN_ERR "CY8CTMA300_TOUCH: probe() fail: %d\n", err);
  1212. return err;
  1213. };
  1214.  
  1215. static int __devexit cy8ctma300_touch_remove(struct spi_device *spi)
  1216. {
  1217. struct cy8ctma300_touch *this = dev_get_drvdata(&spi->dev);
  1218. struct cypress_touch_platform_data *pdata = spi->dev.platform_data;
  1219. dev_t device_t = MKDEV(this->device_major, 0);
  1220.  
  1221. DEBUG_PRINTK(KERN_INFO "CY8CTMA300_TOUCH: unregistering touchscreen\n");
  1222.  
  1223. if (!this)
  1224. return -ENODEV;
  1225.  
  1226. gpio_free(pdata->gpio_reset_pin);
  1227. gpio_free(pdata->gpio_irq_pin);
  1228.  
  1229. #ifdef CONFIG_PM
  1230. cy8ctma300_touch_suspend(spi, PMSG_SUSPEND);
  1231. #endif
  1232.  
  1233. if (!this->init_complete) {
  1234. printk(KERN_ERR "CY8CTMA300_TOUCH: unregistering partially-loaded driver\n");
  1235. return -EBUSY;
  1236. };
  1237.  
  1238. #ifdef CONFIG_EARLYSUSPEND
  1239. unregister_early_suspend(&this->early_suspend);
  1240. #endif
  1241.  
  1242. if (this->input)
  1243. input_unregister_device(this->input);
  1244. if (this->device_class) {
  1245. device_destroy(this->device_class,
  1246. MKDEV(this->device_major, 0));
  1247. class_destroy(this->device_class);
  1248. };
  1249. if (&this->device_cdev) {
  1250. cdev_del(&(this->device_cdev));
  1251. unregister_chrdev_region(device_t, 1);
  1252. };
  1253. free_irq(pdata->irq, this);
  1254. kfree(this);
  1255.  
  1256. dev_dbg(&spi->dev, "CY8CTMA300_TOUCH: unregistered touchscreen\n");
  1257.  
  1258. return 0;
  1259. };
  1260.  
  1261. static struct spi_driver cy8ctma300_touch_driver = {
  1262. .driver = {
  1263. .name = "cypress_touchscreen",
  1264. .bus = &spi_bus_type,
  1265. .owner = THIS_MODULE,
  1266. },
  1267. .probe = cy8ctma300_touch_probe,
  1268. .remove = __devexit_p(cy8ctma300_touch_remove),
  1269. #ifndef CONFIG_PM
  1270. .suspend = cy8ctma300_touch_suspend,
  1271. .resume = cy8ctma300_touch_resume,
  1272. #endif
  1273. };
  1274.  
  1275. static int __init cy8ctma300_touch_init(void)
  1276. {
  1277. int err;
  1278.  
  1279. printk(KERN_DEBUG
  1280. "CY8CTMA300_TOUCH: V%s built %s %s\n",
  1281. MODULE_VER, __DATE__, __TIME__);
  1282. err = spi_register_driver(&cy8ctma300_touch_driver);
  1283. DEBUG_PRINTK(KERN_DEBUG
  1284. "CY8CTMA300_TOUCH: module init, result=%d\n", err);
  1285.  
  1286. return err;
  1287. };
  1288.  
  1289. static void __exit cy8ctma300_touch_exit(void)
  1290. {
  1291. spi_unregister_driver(&cy8ctma300_touch_driver);
  1292. printk(KERN_DEBUG "CY8CTMA300_TOUCH: module exit\n");
  1293. };
  1294.  
  1295. module_init(cy8ctma300_touch_init);
  1296. module_exit(cy8ctma300_touch_exit);
  1297.  
  1298. MODULE_DESCRIPTION("Touchscreen driver for Cypress CY8CTMA300 hardware");
  1299. MODULE_LICENSE("GPL");