cyapa.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Cypress APA trackpad with I2C interface
  3. *
  4. * Author: Dudley Du <dudl@cypress.com>
  5. * Further cleanup and restructuring by:
  6. * Daniel Kurtz <djkurtz@chromium.org>
  7. * Benson Leung <bleung@chromium.org>
  8. *
  9. * Copyright (C) 2011-2012 Cypress Semiconductor, Inc.
  10. * Copyright (C) 2011-2012 Google, Inc.
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file COPYING in the main directory of this archive for
  14. * more details.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. /* APA trackpad firmware generation */
  24. #define CYAPA_GEN3 0x03 /* support MT-protocol B with tracking ID. */
  25. #define CYAPA_NAME "Cypress APA Trackpad (cyapa)"
  26. /* commands for read/write registers of Cypress trackpad */
  27. #define CYAPA_CMD_SOFT_RESET 0x00
  28. #define CYAPA_CMD_POWER_MODE 0x01
  29. #define CYAPA_CMD_DEV_STATUS 0x02
  30. #define CYAPA_CMD_GROUP_DATA 0x03
  31. #define CYAPA_CMD_GROUP_CMD 0x04
  32. #define CYAPA_CMD_GROUP_QUERY 0x05
  33. #define CYAPA_CMD_BL_STATUS 0x06
  34. #define CYAPA_CMD_BL_HEAD 0x07
  35. #define CYAPA_CMD_BL_CMD 0x08
  36. #define CYAPA_CMD_BL_DATA 0x09
  37. #define CYAPA_CMD_BL_ALL 0x0a
  38. #define CYAPA_CMD_BLK_PRODUCT_ID 0x0b
  39. #define CYAPA_CMD_BLK_HEAD 0x0c
  40. /* report data start reg offset address. */
  41. #define DATA_REG_START_OFFSET 0x0000
  42. #define BL_HEAD_OFFSET 0x00
  43. #define BL_DATA_OFFSET 0x10
  44. /*
  45. * Operational Device Status Register
  46. *
  47. * bit 7: Valid interrupt source
  48. * bit 6 - 4: Reserved
  49. * bit 3 - 2: Power status
  50. * bit 1 - 0: Device status
  51. */
  52. #define REG_OP_STATUS 0x00
  53. #define OP_STATUS_SRC 0x80
  54. #define OP_STATUS_POWER 0x0c
  55. #define OP_STATUS_DEV 0x03
  56. #define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER | OP_STATUS_DEV)
  57. /*
  58. * Operational Finger Count/Button Flags Register
  59. *
  60. * bit 7 - 4: Number of touched finger
  61. * bit 3: Valid data
  62. * bit 2: Middle Physical Button
  63. * bit 1: Right Physical Button
  64. * bit 0: Left physical Button
  65. */
  66. #define REG_OP_DATA1 0x01
  67. #define OP_DATA_VALID 0x08
  68. #define OP_DATA_MIDDLE_BTN 0x04
  69. #define OP_DATA_RIGHT_BTN 0x02
  70. #define OP_DATA_LEFT_BTN 0x01
  71. #define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN | \
  72. OP_DATA_LEFT_BTN)
  73. /*
  74. * Bootloader Status Register
  75. *
  76. * bit 7: Busy
  77. * bit 6 - 5: Reserved
  78. * bit 4: Bootloader running
  79. * bit 3 - 1: Reserved
  80. * bit 0: Checksum valid
  81. */
  82. #define REG_BL_STATUS 0x01
  83. #define BL_STATUS_BUSY 0x80
  84. #define BL_STATUS_RUNNING 0x10
  85. #define BL_STATUS_DATA_VALID 0x08
  86. #define BL_STATUS_CSUM_VALID 0x01
  87. /*
  88. * Bootloader Error Register
  89. *
  90. * bit 7: Invalid
  91. * bit 6: Invalid security key
  92. * bit 5: Bootloading
  93. * bit 4: Command checksum
  94. * bit 3: Flash protection error
  95. * bit 2: Flash checksum error
  96. * bit 1 - 0: Reserved
  97. */
  98. #define REG_BL_ERROR 0x02
  99. #define BL_ERROR_INVALID 0x80
  100. #define BL_ERROR_INVALID_KEY 0x40
  101. #define BL_ERROR_BOOTLOADING 0x20
  102. #define BL_ERROR_CMD_CSUM 0x10
  103. #define BL_ERROR_FLASH_PROT 0x08
  104. #define BL_ERROR_FLASH_CSUM 0x04
  105. #define BL_STATUS_SIZE 3 /* length of bootloader status registers */
  106. #define BLK_HEAD_BYTES 32
  107. #define PRODUCT_ID_SIZE 16
  108. #define QUERY_DATA_SIZE 27
  109. #define REG_PROTOCOL_GEN_QUERY_OFFSET 20
  110. #define REG_OFFSET_DATA_BASE 0x0000
  111. #define REG_OFFSET_COMMAND_BASE 0x0028
  112. #define REG_OFFSET_QUERY_BASE 0x002a
  113. #define CAPABILITY_LEFT_BTN_MASK (0x01 << 3)
  114. #define CAPABILITY_RIGHT_BTN_MASK (0x01 << 4)
  115. #define CAPABILITY_MIDDLE_BTN_MASK (0x01 << 5)
  116. #define CAPABILITY_BTN_MASK (CAPABILITY_LEFT_BTN_MASK | \
  117. CAPABILITY_RIGHT_BTN_MASK | \
  118. CAPABILITY_MIDDLE_BTN_MASK)
  119. #define CYAPA_OFFSET_SOFT_RESET REG_OFFSET_COMMAND_BASE
  120. #define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
  121. #define PWR_MODE_MASK 0xfc
  122. #define PWR_MODE_FULL_ACTIVE (0x3f << 2)
  123. #define PWR_MODE_IDLE (0x05 << 2) /* default sleep time is 50 ms. */
  124. #define PWR_MODE_OFF (0x00 << 2)
  125. #define PWR_STATUS_MASK 0x0c
  126. #define PWR_STATUS_ACTIVE (0x03 << 2)
  127. #define PWR_STATUS_IDLE (0x02 << 2)
  128. #define PWR_STATUS_OFF (0x00 << 2)
  129. /*
  130. * CYAPA trackpad device states.
  131. * Used in register 0x00, bit1-0, DeviceStatus field.
  132. * Other values indicate device is in an abnormal state and must be reset.
  133. */
  134. #define CYAPA_DEV_NORMAL 0x03
  135. #define CYAPA_DEV_BUSY 0x01
  136. enum cyapa_state {
  137. CYAPA_STATE_OP,
  138. CYAPA_STATE_BL_IDLE,
  139. CYAPA_STATE_BL_ACTIVE,
  140. CYAPA_STATE_BL_BUSY,
  141. CYAPA_STATE_NO_DEVICE,
  142. };
  143. struct cyapa_touch {
  144. /*
  145. * high bits or x/y position value
  146. * bit 7 - 4: high 4 bits of x position value
  147. * bit 3 - 0: high 4 bits of y position value
  148. */
  149. u8 xy_hi;
  150. u8 x_lo; /* low 8 bits of x position value. */
  151. u8 y_lo; /* low 8 bits of y position value. */
  152. u8 pressure;
  153. /* id range is 1 - 15. It is incremented with every new touch. */
  154. u8 id;
  155. } __packed;
  156. /* The touch.id is used as the MT slot id, thus max MT slot is 15 */
  157. #define CYAPA_MAX_MT_SLOTS 15
  158. struct cyapa_reg_data {
  159. /*
  160. * bit 0 - 1: device status
  161. * bit 3 - 2: power mode
  162. * bit 6 - 4: reserved
  163. * bit 7: interrupt valid bit
  164. */
  165. u8 device_status;
  166. /*
  167. * bit 7 - 4: number of fingers currently touching pad
  168. * bit 3: valid data check bit
  169. * bit 2: middle mechanism button state if exists
  170. * bit 1: right mechanism button state if exists
  171. * bit 0: left mechanism button state if exists
  172. */
  173. u8 finger_btn;
  174. /* CYAPA reports up to 5 touches per packet. */
  175. struct cyapa_touch touches[5];
  176. } __packed;
  177. /* The main device structure */
  178. struct cyapa {
  179. enum cyapa_state state;
  180. struct i2c_client *client;
  181. struct input_dev *input;
  182. char phys[32]; /* device physical location */
  183. int irq;
  184. bool irq_wake; /* irq wake is enabled */
  185. bool smbus;
  186. /* read from query data region. */
  187. char product_id[16];
  188. u8 btn_capability;
  189. u8 gen;
  190. int max_abs_x;
  191. int max_abs_y;
  192. int physical_size_x;
  193. int physical_size_y;
  194. };
  195. static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
  196. 0x04, 0x05, 0x06, 0x07 };
  197. static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
  198. 0x05, 0x06, 0x07 };
  199. struct cyapa_cmd_len {
  200. u8 cmd;
  201. u8 len;
  202. };
  203. #define CYAPA_ADAPTER_FUNC_NONE 0
  204. #define CYAPA_ADAPTER_FUNC_I2C 1
  205. #define CYAPA_ADAPTER_FUNC_SMBUS 2
  206. #define CYAPA_ADAPTER_FUNC_BOTH 3
  207. /*
  208. * macros for SMBus communication
  209. */
  210. #define SMBUS_READ 0x01
  211. #define SMBUS_WRITE 0x00
  212. #define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
  213. #define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
  214. #define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
  215. #define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
  216. /* for byte read/write command */
  217. #define CMD_RESET 0
  218. #define CMD_POWER_MODE 1
  219. #define CMD_DEV_STATUS 2
  220. #define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
  221. #define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
  222. #define CYAPA_SMBUS_POWER_MODE SMBUS_BYTE_CMD(CMD_POWER_MODE)
  223. #define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
  224. /* for group registers read/write command */
  225. #define REG_GROUP_DATA 0
  226. #define REG_GROUP_CMD 2
  227. #define REG_GROUP_QUERY 3
  228. #define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
  229. #define CYAPA_SMBUS_GROUP_DATA SMBUS_GROUP_CMD(REG_GROUP_DATA)
  230. #define CYAPA_SMBUS_GROUP_CMD SMBUS_GROUP_CMD(REG_GROUP_CMD)
  231. #define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
  232. /* for register block read/write command */
  233. #define CMD_BL_STATUS 0
  234. #define CMD_BL_HEAD 1
  235. #define CMD_BL_CMD 2
  236. #define CMD_BL_DATA 3
  237. #define CMD_BL_ALL 4
  238. #define CMD_BLK_PRODUCT_ID 5
  239. #define CMD_BLK_HEAD 6
  240. #define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
  241. /* register block read/write command in bootloader mode */
  242. #define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
  243. #define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
  244. #define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
  245. #define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
  246. #define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
  247. /* register block read/write command in operational mode */
  248. #define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
  249. #define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
  250. static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
  251. { CYAPA_OFFSET_SOFT_RESET, 1 },
  252. { REG_OFFSET_COMMAND_BASE + 1, 1 },
  253. { REG_OFFSET_DATA_BASE, 1 },
  254. { REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
  255. { REG_OFFSET_COMMAND_BASE, 0 },
  256. { REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE },
  257. { BL_HEAD_OFFSET, 3 },
  258. { BL_HEAD_OFFSET, 16 },
  259. { BL_HEAD_OFFSET, 16 },
  260. { BL_DATA_OFFSET, 16 },
  261. { BL_HEAD_OFFSET, 32 },
  262. { REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
  263. { REG_OFFSET_DATA_BASE, 32 }
  264. };
  265. static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
  266. { CYAPA_SMBUS_RESET, 1 },
  267. { CYAPA_SMBUS_POWER_MODE, 1 },
  268. { CYAPA_SMBUS_DEV_STATUS, 1 },
  269. { CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
  270. { CYAPA_SMBUS_GROUP_CMD, 2 },
  271. { CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
  272. { CYAPA_SMBUS_BL_STATUS, 3 },
  273. { CYAPA_SMBUS_BL_HEAD, 16 },
  274. { CYAPA_SMBUS_BL_CMD, 16 },
  275. { CYAPA_SMBUS_BL_DATA, 16 },
  276. { CYAPA_SMBUS_BL_ALL, 32 },
  277. { CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
  278. { CYAPA_SMBUS_BLK_HEAD, 16 },
  279. };
  280. static ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
  281. u8 *values)
  282. {
  283. return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
  284. }
  285. static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
  286. size_t len, const u8 *values)
  287. {
  288. return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
  289. }
  290. /*
  291. * cyapa_smbus_read_block - perform smbus block read command
  292. * @cyapa - private data structure of the driver
  293. * @cmd - the properly encoded smbus command
  294. * @len - expected length of smbus command result
  295. * @values - buffer to store smbus command result
  296. *
  297. * Returns negative errno, else the number of bytes written.
  298. *
  299. * Note:
  300. * In trackpad device, the memory block allocated for I2C register map
  301. * is 256 bytes, so the max read block for I2C bus is 256 bytes.
  302. */
  303. static ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
  304. u8 *values)
  305. {
  306. ssize_t ret;
  307. u8 index;
  308. u8 smbus_cmd;
  309. u8 *buf;
  310. struct i2c_client *client = cyapa->client;
  311. if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
  312. return -EINVAL;
  313. if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
  314. /* read specific block registers command. */
  315. smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  316. ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
  317. goto out;
  318. }
  319. ret = 0;
  320. for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
  321. smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
  322. smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
  323. buf = values + I2C_SMBUS_BLOCK_MAX * index;
  324. ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
  325. if (ret < 0)
  326. goto out;
  327. }
  328. out:
  329. return ret > 0 ? len : ret;
  330. }
  331. static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
  332. {
  333. u8 cmd;
  334. if (cyapa->smbus) {
  335. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  336. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  337. } else {
  338. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  339. }
  340. return i2c_smbus_read_byte_data(cyapa->client, cmd);
  341. }
  342. static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
  343. {
  344. u8 cmd;
  345. if (cyapa->smbus) {
  346. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  347. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
  348. } else {
  349. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  350. }
  351. return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
  352. }
  353. static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
  354. {
  355. u8 cmd;
  356. size_t len;
  357. if (cyapa->smbus) {
  358. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  359. len = cyapa_smbus_cmds[cmd_idx].len;
  360. return cyapa_smbus_read_block(cyapa, cmd, len, values);
  361. } else {
  362. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  363. len = cyapa_i2c_cmds[cmd_idx].len;
  364. return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
  365. }
  366. }
  367. /*
  368. * Query device for its current operating state.
  369. *
  370. */
  371. static int cyapa_get_state(struct cyapa *cyapa)
  372. {
  373. int ret;
  374. u8 status[BL_STATUS_SIZE];
  375. cyapa->state = CYAPA_STATE_NO_DEVICE;
  376. /*
  377. * Get trackpad status by reading 3 registers starting from 0.
  378. * If the device is in the bootloader, this will be BL_HEAD.
  379. * If the device is in operation mode, this will be the DATA regs.
  380. *
  381. */
  382. ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
  383. status);
  384. /*
  385. * On smbus systems in OP mode, the i2c_reg_read will fail with
  386. * -ETIMEDOUT. In this case, try again using the smbus equivalent
  387. * command. This should return a BL_HEAD indicating CYAPA_STATE_OP.
  388. */
  389. if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO))
  390. ret = cyapa_read_block(cyapa, CYAPA_CMD_BL_STATUS, status);
  391. if (ret != BL_STATUS_SIZE)
  392. goto error;
  393. if ((status[REG_OP_STATUS] & OP_STATUS_SRC) == OP_STATUS_SRC) {
  394. switch (status[REG_OP_STATUS] & OP_STATUS_DEV) {
  395. case CYAPA_DEV_NORMAL:
  396. case CYAPA_DEV_BUSY:
  397. cyapa->state = CYAPA_STATE_OP;
  398. break;
  399. default:
  400. ret = -EAGAIN;
  401. goto error;
  402. }
  403. } else {
  404. if (status[REG_BL_STATUS] & BL_STATUS_BUSY)
  405. cyapa->state = CYAPA_STATE_BL_BUSY;
  406. else if (status[REG_BL_ERROR] & BL_ERROR_BOOTLOADING)
  407. cyapa->state = CYAPA_STATE_BL_ACTIVE;
  408. else
  409. cyapa->state = CYAPA_STATE_BL_IDLE;
  410. }
  411. return 0;
  412. error:
  413. return (ret < 0) ? ret : -EAGAIN;
  414. }
  415. /*
  416. * Poll device for its status in a loop, waiting up to timeout for a response.
  417. *
  418. * When the device switches state, it usually takes ~300 ms.
  419. * However, when running a new firmware image, the device must calibrate its
  420. * sensors, which can take as long as 2 seconds.
  421. *
  422. * Note: The timeout has granularity of the polling rate, which is 100 ms.
  423. *
  424. * Returns:
  425. * 0 when the device eventually responds with a valid non-busy state.
  426. * -ETIMEDOUT if device never responds (too many -EAGAIN)
  427. * < 0 other errors
  428. */
  429. static int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
  430. {
  431. int ret;
  432. int tries = timeout / 100;
  433. ret = cyapa_get_state(cyapa);
  434. while ((ret || cyapa->state >= CYAPA_STATE_BL_BUSY) && tries--) {
  435. msleep(100);
  436. ret = cyapa_get_state(cyapa);
  437. }
  438. return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
  439. }
  440. static int cyapa_bl_deactivate(struct cyapa *cyapa)
  441. {
  442. int ret;
  443. ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
  444. bl_deactivate);
  445. if (ret < 0)
  446. return ret;
  447. /* wait for bootloader to switch to idle state; should take < 100ms */
  448. msleep(100);
  449. ret = cyapa_poll_state(cyapa, 500);
  450. if (ret < 0)
  451. return ret;
  452. if (cyapa->state != CYAPA_STATE_BL_IDLE)
  453. return -EAGAIN;
  454. return 0;
  455. }
  456. /*
  457. * Exit bootloader
  458. *
  459. * Send bl_exit command, then wait 50 - 100 ms to let device transition to
  460. * operational mode. If this is the first time the device's firmware is
  461. * running, it can take up to 2 seconds to calibrate its sensors. So, poll
  462. * the device's new state for up to 2 seconds.
  463. *
  464. * Returns:
  465. * -EIO failure while reading from device
  466. * -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
  467. * 0 device is supported and in operational mode
  468. */
  469. static int cyapa_bl_exit(struct cyapa *cyapa)
  470. {
  471. int ret;
  472. ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
  473. if (ret < 0)
  474. return ret;
  475. /*
  476. * Wait for bootloader to exit, and operation mode to start.
  477. * Normally, this takes at least 50 ms.
  478. */
  479. usleep_range(50000, 100000);
  480. /*
  481. * In addition, when a device boots for the first time after being
  482. * updated to new firmware, it must first calibrate its sensors, which
  483. * can take up to an additional 2 seconds.
  484. */
  485. ret = cyapa_poll_state(cyapa, 2000);
  486. if (ret < 0)
  487. return ret;
  488. if (cyapa->state != CYAPA_STATE_OP)
  489. return -EAGAIN;
  490. return 0;
  491. }
  492. /*
  493. * Set device power mode
  494. *
  495. */
  496. static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
  497. {
  498. struct device *dev = &cyapa->client->dev;
  499. int ret;
  500. u8 power;
  501. if (cyapa->state != CYAPA_STATE_OP)
  502. return 0;
  503. ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
  504. if (ret < 0)
  505. return ret;
  506. power = ret & ~PWR_MODE_MASK;
  507. power |= power_mode & PWR_MODE_MASK;
  508. ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
  509. if (ret < 0)
  510. dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
  511. power_mode, ret);
  512. return ret;
  513. }
  514. static int cyapa_get_query_data(struct cyapa *cyapa)
  515. {
  516. u8 query_data[QUERY_DATA_SIZE];
  517. int ret;
  518. if (cyapa->state != CYAPA_STATE_OP)
  519. return -EBUSY;
  520. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
  521. if (ret < 0)
  522. return ret;
  523. if (ret != QUERY_DATA_SIZE)
  524. return -EIO;
  525. memcpy(&cyapa->product_id[0], &query_data[0], 5);
  526. cyapa->product_id[5] = '-';
  527. memcpy(&cyapa->product_id[6], &query_data[5], 6);
  528. cyapa->product_id[12] = '-';
  529. memcpy(&cyapa->product_id[13], &query_data[11], 2);
  530. cyapa->product_id[15] = '\0';
  531. cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
  532. cyapa->gen = query_data[20] & 0x0f;
  533. cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
  534. cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
  535. cyapa->physical_size_x =
  536. ((query_data[24] & 0xf0) << 4) | query_data[25];
  537. cyapa->physical_size_y =
  538. ((query_data[24] & 0x0f) << 8) | query_data[26];
  539. return 0;
  540. }
  541. /*
  542. * Check if device is operational.
  543. *
  544. * An operational device is responding, has exited bootloader, and has
  545. * firmware supported by this driver.
  546. *
  547. * Returns:
  548. * -EBUSY no device or in bootloader
  549. * -EIO failure while reading from device
  550. * -EAGAIN device is still in bootloader
  551. * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
  552. * -EINVAL device is in operational mode, but not supported by this driver
  553. * 0 device is supported
  554. */
  555. static int cyapa_check_is_operational(struct cyapa *cyapa)
  556. {
  557. struct device *dev = &cyapa->client->dev;
  558. static const char unique_str[] = "CYTRA";
  559. int ret;
  560. ret = cyapa_poll_state(cyapa, 2000);
  561. if (ret < 0)
  562. return ret;
  563. switch (cyapa->state) {
  564. case CYAPA_STATE_BL_ACTIVE:
  565. ret = cyapa_bl_deactivate(cyapa);
  566. if (ret)
  567. return ret;
  568. /* Fallthrough state */
  569. case CYAPA_STATE_BL_IDLE:
  570. ret = cyapa_bl_exit(cyapa);
  571. if (ret)
  572. return ret;
  573. /* Fallthrough state */
  574. case CYAPA_STATE_OP:
  575. ret = cyapa_get_query_data(cyapa);
  576. if (ret < 0)
  577. return ret;
  578. /* only support firmware protocol gen3 */
  579. if (cyapa->gen != CYAPA_GEN3) {
  580. dev_err(dev, "unsupported protocol version (%d)",
  581. cyapa->gen);
  582. return -EINVAL;
  583. }
  584. /* only support product ID starting with CYTRA */
  585. if (memcmp(cyapa->product_id, unique_str,
  586. sizeof(unique_str) - 1) != 0) {
  587. dev_err(dev, "unsupported product ID (%s)\n",
  588. cyapa->product_id);
  589. return -EINVAL;
  590. }
  591. return 0;
  592. default:
  593. return -EIO;
  594. }
  595. return 0;
  596. }
  597. static irqreturn_t cyapa_irq(int irq, void *dev_id)
  598. {
  599. struct cyapa *cyapa = dev_id;
  600. struct device *dev = &cyapa->client->dev;
  601. struct input_dev *input = cyapa->input;
  602. struct cyapa_reg_data data;
  603. int i;
  604. int ret;
  605. int num_fingers;
  606. if (device_may_wakeup(dev))
  607. pm_wakeup_event(dev, 0);
  608. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
  609. if (ret != sizeof(data))
  610. goto out;
  611. if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
  612. (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
  613. (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
  614. goto out;
  615. }
  616. num_fingers = (data.finger_btn >> 4) & 0x0f;
  617. for (i = 0; i < num_fingers; i++) {
  618. const struct cyapa_touch *touch = &data.touches[i];
  619. /* Note: touch->id range is 1 to 15; slots are 0 to 14. */
  620. int slot = touch->id - 1;
  621. input_mt_slot(input, slot);
  622. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  623. input_report_abs(input, ABS_MT_POSITION_X,
  624. ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
  625. input_report_abs(input, ABS_MT_POSITION_Y,
  626. ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
  627. input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
  628. }
  629. input_mt_sync_frame(input);
  630. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  631. input_report_key(input, BTN_LEFT,
  632. data.finger_btn & OP_DATA_LEFT_BTN);
  633. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  634. input_report_key(input, BTN_MIDDLE,
  635. data.finger_btn & OP_DATA_MIDDLE_BTN);
  636. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  637. input_report_key(input, BTN_RIGHT,
  638. data.finger_btn & OP_DATA_RIGHT_BTN);
  639. input_sync(input);
  640. out:
  641. return IRQ_HANDLED;
  642. }
  643. static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
  644. {
  645. u8 ret = CYAPA_ADAPTER_FUNC_NONE;
  646. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  647. ret |= CYAPA_ADAPTER_FUNC_I2C;
  648. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  649. I2C_FUNC_SMBUS_BLOCK_DATA |
  650. I2C_FUNC_SMBUS_I2C_BLOCK))
  651. ret |= CYAPA_ADAPTER_FUNC_SMBUS;
  652. return ret;
  653. }
  654. static int cyapa_create_input_dev(struct cyapa *cyapa)
  655. {
  656. struct device *dev = &cyapa->client->dev;
  657. int ret;
  658. struct input_dev *input;
  659. if (!cyapa->physical_size_x || !cyapa->physical_size_y)
  660. return -EINVAL;
  661. input = cyapa->input = input_allocate_device();
  662. if (!input) {
  663. dev_err(dev, "allocate memory for input device failed\n");
  664. return -ENOMEM;
  665. }
  666. input->name = CYAPA_NAME;
  667. input->phys = cyapa->phys;
  668. input->id.bustype = BUS_I2C;
  669. input->id.version = 1;
  670. input->id.product = 0; /* means any product in eventcomm. */
  671. input->dev.parent = &cyapa->client->dev;
  672. input_set_drvdata(input, cyapa);
  673. __set_bit(EV_ABS, input->evbit);
  674. /* finger position */
  675. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
  676. 0);
  677. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
  678. 0);
  679. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  680. input_abs_set_res(input, ABS_MT_POSITION_X,
  681. cyapa->max_abs_x / cyapa->physical_size_x);
  682. input_abs_set_res(input, ABS_MT_POSITION_Y,
  683. cyapa->max_abs_y / cyapa->physical_size_y);
  684. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  685. __set_bit(BTN_LEFT, input->keybit);
  686. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  687. __set_bit(BTN_MIDDLE, input->keybit);
  688. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  689. __set_bit(BTN_RIGHT, input->keybit);
  690. if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
  691. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  692. /* handle pointer emulation and unused slots in core */
  693. ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
  694. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
  695. if (ret) {
  696. dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
  697. goto err_free_device;
  698. }
  699. /* Register the device in input subsystem */
  700. ret = input_register_device(input);
  701. if (ret) {
  702. dev_err(dev, "input device register failed, %d\n", ret);
  703. goto err_free_device;
  704. }
  705. return 0;
  706. err_free_device:
  707. input_free_device(input);
  708. cyapa->input = NULL;
  709. return ret;
  710. }
  711. static int cyapa_probe(struct i2c_client *client,
  712. const struct i2c_device_id *dev_id)
  713. {
  714. int ret;
  715. u8 adapter_func;
  716. struct cyapa *cyapa;
  717. struct device *dev = &client->dev;
  718. adapter_func = cyapa_check_adapter_functionality(client);
  719. if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
  720. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  721. return -EIO;
  722. }
  723. cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
  724. if (!cyapa) {
  725. dev_err(dev, "allocate memory for cyapa failed\n");
  726. return -ENOMEM;
  727. }
  728. cyapa->gen = CYAPA_GEN3;
  729. cyapa->client = client;
  730. i2c_set_clientdata(client, cyapa);
  731. sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
  732. client->addr);
  733. /* i2c isn't supported, use smbus */
  734. if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
  735. cyapa->smbus = true;
  736. cyapa->state = CYAPA_STATE_NO_DEVICE;
  737. ret = cyapa_check_is_operational(cyapa);
  738. if (ret) {
  739. dev_err(dev, "device not operational, %d\n", ret);
  740. goto err_mem_free;
  741. }
  742. ret = cyapa_create_input_dev(cyapa);
  743. if (ret) {
  744. dev_err(dev, "create input_dev instance failed, %d\n", ret);
  745. goto err_mem_free;
  746. }
  747. ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
  748. if (ret) {
  749. dev_err(dev, "set active power failed, %d\n", ret);
  750. goto err_unregister_device;
  751. }
  752. cyapa->irq = client->irq;
  753. ret = request_threaded_irq(cyapa->irq,
  754. NULL,
  755. cyapa_irq,
  756. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  757. "cyapa",
  758. cyapa);
  759. if (ret) {
  760. dev_err(dev, "IRQ request failed: %d\n, ", ret);
  761. goto err_unregister_device;
  762. }
  763. return 0;
  764. err_unregister_device:
  765. input_unregister_device(cyapa->input);
  766. err_mem_free:
  767. kfree(cyapa);
  768. return ret;
  769. }
  770. static int cyapa_remove(struct i2c_client *client)
  771. {
  772. struct cyapa *cyapa = i2c_get_clientdata(client);
  773. free_irq(cyapa->irq, cyapa);
  774. input_unregister_device(cyapa->input);
  775. cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
  776. kfree(cyapa);
  777. return 0;
  778. }
  779. #ifdef CONFIG_PM_SLEEP
  780. static int cyapa_suspend(struct device *dev)
  781. {
  782. int ret;
  783. u8 power_mode;
  784. struct cyapa *cyapa = dev_get_drvdata(dev);
  785. disable_irq(cyapa->irq);
  786. /*
  787. * Set trackpad device to idle mode if wakeup is allowed,
  788. * otherwise turn off.
  789. */
  790. power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
  791. : PWR_MODE_OFF;
  792. ret = cyapa_set_power_mode(cyapa, power_mode);
  793. if (ret < 0)
  794. dev_err(dev, "set power mode failed, %d\n", ret);
  795. if (device_may_wakeup(dev))
  796. cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
  797. return 0;
  798. }
  799. static int cyapa_resume(struct device *dev)
  800. {
  801. int ret;
  802. struct cyapa *cyapa = dev_get_drvdata(dev);
  803. if (device_may_wakeup(dev) && cyapa->irq_wake)
  804. disable_irq_wake(cyapa->irq);
  805. ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
  806. if (ret)
  807. dev_warn(dev, "resume active power failed, %d\n", ret);
  808. enable_irq(cyapa->irq);
  809. return 0;
  810. }
  811. #endif /* CONFIG_PM_SLEEP */
  812. static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
  813. static const struct i2c_device_id cyapa_id_table[] = {
  814. { "cyapa", 0 },
  815. { },
  816. };
  817. MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
  818. static struct i2c_driver cyapa_driver = {
  819. .driver = {
  820. .name = "cyapa",
  821. .owner = THIS_MODULE,
  822. .pm = &cyapa_pm_ops,
  823. },
  824. .probe = cyapa_probe,
  825. .remove = cyapa_remove,
  826. .id_table = cyapa_id_table,
  827. };
  828. module_i2c_driver(cyapa_driver);
  829. MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
  830. MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
  831. MODULE_LICENSE("GPL");