cyapa_gen3.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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-2015 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/module.h>
  21. #include <linux/slab.h>
  22. #include <asm/unaligned.h>
  23. #include "cyapa.h"
  24. #define GEN3_MAX_FINGERS 5
  25. #define GEN3_FINGER_NUM(x) (((x) >> 4) & 0x07)
  26. #define BLK_HEAD_BYTES 32
  27. /* Macro for register map group offset. */
  28. #define PRODUCT_ID_SIZE 16
  29. #define QUERY_DATA_SIZE 27
  30. #define REG_PROTOCOL_GEN_QUERY_OFFSET 20
  31. #define REG_OFFSET_DATA_BASE 0x0000
  32. #define REG_OFFSET_COMMAND_BASE 0x0028
  33. #define REG_OFFSET_QUERY_BASE 0x002a
  34. #define CYAPA_OFFSET_SOFT_RESET REG_OFFSET_COMMAND_BASE
  35. #define OP_RECALIBRATION_MASK 0x80
  36. #define OP_REPORT_BASELINE_MASK 0x40
  37. #define REG_OFFSET_MAX_BASELINE 0x0026
  38. #define REG_OFFSET_MIN_BASELINE 0x0027
  39. #define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
  40. #define SET_POWER_MODE_DELAY 10000 /* Unit: us */
  41. #define SET_POWER_MODE_TRIES 5
  42. #define GEN3_BL_CMD_CHECKSUM_SEED 0xff
  43. #define GEN3_BL_CMD_INITIATE_BL 0x38
  44. #define GEN3_BL_CMD_WRITE_BLOCK 0x39
  45. #define GEN3_BL_CMD_VERIFY_BLOCK 0x3a
  46. #define GEN3_BL_CMD_TERMINATE_BL 0x3b
  47. #define GEN3_BL_CMD_LAUNCH_APP 0xa5
  48. /*
  49. * CYAPA trackpad device states.
  50. * Used in register 0x00, bit1-0, DeviceStatus field.
  51. * Other values indicate device is in an abnormal state and must be reset.
  52. */
  53. #define CYAPA_DEV_NORMAL 0x03
  54. #define CYAPA_DEV_BUSY 0x01
  55. #define CYAPA_FW_BLOCK_SIZE 64
  56. #define CYAPA_FW_READ_SIZE 16
  57. #define CYAPA_FW_HDR_START 0x0780
  58. #define CYAPA_FW_HDR_BLOCK_COUNT 2
  59. #define CYAPA_FW_HDR_BLOCK_START (CYAPA_FW_HDR_START / CYAPA_FW_BLOCK_SIZE)
  60. #define CYAPA_FW_HDR_SIZE (CYAPA_FW_HDR_BLOCK_COUNT * \
  61. CYAPA_FW_BLOCK_SIZE)
  62. #define CYAPA_FW_DATA_START 0x0800
  63. #define CYAPA_FW_DATA_BLOCK_COUNT 480
  64. #define CYAPA_FW_DATA_BLOCK_START (CYAPA_FW_DATA_START / CYAPA_FW_BLOCK_SIZE)
  65. #define CYAPA_FW_DATA_SIZE (CYAPA_FW_DATA_BLOCK_COUNT * \
  66. CYAPA_FW_BLOCK_SIZE)
  67. #define CYAPA_FW_SIZE (CYAPA_FW_HDR_SIZE + CYAPA_FW_DATA_SIZE)
  68. #define CYAPA_CMD_LEN 16
  69. #define GEN3_BL_IDLE_FW_MAJ_VER_OFFSET 0x0b
  70. #define GEN3_BL_IDLE_FW_MIN_VER_OFFSET (GEN3_BL_IDLE_FW_MAJ_VER_OFFSET + 1)
  71. struct cyapa_touch {
  72. /*
  73. * high bits or x/y position value
  74. * bit 7 - 4: high 4 bits of x position value
  75. * bit 3 - 0: high 4 bits of y position value
  76. */
  77. u8 xy_hi;
  78. u8 x_lo; /* low 8 bits of x position value. */
  79. u8 y_lo; /* low 8 bits of y position value. */
  80. u8 pressure;
  81. /* id range is 1 - 15. It is incremented with every new touch. */
  82. u8 id;
  83. } __packed;
  84. struct cyapa_reg_data {
  85. /*
  86. * bit 0 - 1: device status
  87. * bit 3 - 2: power mode
  88. * bit 6 - 4: reserved
  89. * bit 7: interrupt valid bit
  90. */
  91. u8 device_status;
  92. /*
  93. * bit 7 - 4: number of fingers currently touching pad
  94. * bit 3: valid data check bit
  95. * bit 2: middle mechanism button state if exists
  96. * bit 1: right mechanism button state if exists
  97. * bit 0: left mechanism button state if exists
  98. */
  99. u8 finger_btn;
  100. /* CYAPA reports up to 5 touches per packet. */
  101. struct cyapa_touch touches[5];
  102. } __packed;
  103. struct gen3_write_block_cmd {
  104. u8 checksum_seed; /* Always be 0xff */
  105. u8 cmd_code; /* command code: 0x39 */
  106. u8 key[8]; /* 8-byte security key */
  107. __be16 block_num;
  108. u8 block_data[CYAPA_FW_BLOCK_SIZE];
  109. u8 block_checksum; /* Calculated using bytes 12 - 75 */
  110. u8 cmd_checksum; /* Calculated using bytes 0-76 */
  111. } __packed;
  112. static const u8 security_key[] = {
  113. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  114. static const u8 bl_activate[] = { 0x00, 0xff, 0x38, 0x00, 0x01, 0x02, 0x03,
  115. 0x04, 0x05, 0x06, 0x07 };
  116. static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
  117. 0x04, 0x05, 0x06, 0x07 };
  118. static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
  119. 0x05, 0x06, 0x07 };
  120. /* for byte read/write command */
  121. #define CMD_RESET 0
  122. #define CMD_POWER_MODE 1
  123. #define CMD_DEV_STATUS 2
  124. #define CMD_REPORT_MAX_BASELINE 3
  125. #define CMD_REPORT_MIN_BASELINE 4
  126. #define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
  127. #define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
  128. #define CYAPA_SMBUS_POWER_MODE SMBUS_BYTE_CMD(CMD_POWER_MODE)
  129. #define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
  130. #define CYAPA_SMBUS_MAX_BASELINE SMBUS_BYTE_CMD(CMD_REPORT_MAX_BASELINE)
  131. #define CYAPA_SMBUS_MIN_BASELINE SMBUS_BYTE_CMD(CMD_REPORT_MIN_BASELINE)
  132. /* for group registers read/write command */
  133. #define REG_GROUP_DATA 0
  134. #define REG_GROUP_CMD 2
  135. #define REG_GROUP_QUERY 3
  136. #define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
  137. #define CYAPA_SMBUS_GROUP_DATA SMBUS_GROUP_CMD(REG_GROUP_DATA)
  138. #define CYAPA_SMBUS_GROUP_CMD SMBUS_GROUP_CMD(REG_GROUP_CMD)
  139. #define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
  140. /* for register block read/write command */
  141. #define CMD_BL_STATUS 0
  142. #define CMD_BL_HEAD 1
  143. #define CMD_BL_CMD 2
  144. #define CMD_BL_DATA 3
  145. #define CMD_BL_ALL 4
  146. #define CMD_BLK_PRODUCT_ID 5
  147. #define CMD_BLK_HEAD 6
  148. #define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
  149. /* register block read/write command in bootloader mode */
  150. #define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
  151. #define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
  152. #define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
  153. #define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
  154. #define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
  155. /* register block read/write command in operational mode */
  156. #define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
  157. #define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
  158. struct cyapa_cmd_len {
  159. u8 cmd;
  160. u8 len;
  161. };
  162. /* maps generic CYAPA_CMD_* code to the I2C equivalent */
  163. static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
  164. { CYAPA_OFFSET_SOFT_RESET, 1 }, /* CYAPA_CMD_SOFT_RESET */
  165. { REG_OFFSET_COMMAND_BASE + 1, 1 }, /* CYAPA_CMD_POWER_MODE */
  166. { REG_OFFSET_DATA_BASE, 1 }, /* CYAPA_CMD_DEV_STATUS */
  167. { REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
  168. /* CYAPA_CMD_GROUP_DATA */
  169. { REG_OFFSET_COMMAND_BASE, 0 }, /* CYAPA_CMD_GROUP_CMD */
  170. { REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE }, /* CYAPA_CMD_GROUP_QUERY */
  171. { BL_HEAD_OFFSET, 3 }, /* CYAPA_CMD_BL_STATUS */
  172. { BL_HEAD_OFFSET, 16 }, /* CYAPA_CMD_BL_HEAD */
  173. { BL_HEAD_OFFSET, 16 }, /* CYAPA_CMD_BL_CMD */
  174. { BL_DATA_OFFSET, 16 }, /* CYAPA_CMD_BL_DATA */
  175. { BL_HEAD_OFFSET, 32 }, /* CYAPA_CMD_BL_ALL */
  176. { REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
  177. /* CYAPA_CMD_BLK_PRODUCT_ID */
  178. { REG_OFFSET_DATA_BASE, 32 }, /* CYAPA_CMD_BLK_HEAD */
  179. { REG_OFFSET_MAX_BASELINE, 1 }, /* CYAPA_CMD_MAX_BASELINE */
  180. { REG_OFFSET_MIN_BASELINE, 1 }, /* CYAPA_CMD_MIN_BASELINE */
  181. };
  182. static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
  183. { CYAPA_SMBUS_RESET, 1 }, /* CYAPA_CMD_SOFT_RESET */
  184. { CYAPA_SMBUS_POWER_MODE, 1 }, /* CYAPA_CMD_POWER_MODE */
  185. { CYAPA_SMBUS_DEV_STATUS, 1 }, /* CYAPA_CMD_DEV_STATUS */
  186. { CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
  187. /* CYAPA_CMD_GROUP_DATA */
  188. { CYAPA_SMBUS_GROUP_CMD, 2 }, /* CYAPA_CMD_GROUP_CMD */
  189. { CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
  190. /* CYAPA_CMD_GROUP_QUERY */
  191. { CYAPA_SMBUS_BL_STATUS, 3 }, /* CYAPA_CMD_BL_STATUS */
  192. { CYAPA_SMBUS_BL_HEAD, 16 }, /* CYAPA_CMD_BL_HEAD */
  193. { CYAPA_SMBUS_BL_CMD, 16 }, /* CYAPA_CMD_BL_CMD */
  194. { CYAPA_SMBUS_BL_DATA, 16 }, /* CYAPA_CMD_BL_DATA */
  195. { CYAPA_SMBUS_BL_ALL, 32 }, /* CYAPA_CMD_BL_ALL */
  196. { CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
  197. /* CYAPA_CMD_BLK_PRODUCT_ID */
  198. { CYAPA_SMBUS_BLK_HEAD, 16 }, /* CYAPA_CMD_BLK_HEAD */
  199. { CYAPA_SMBUS_MAX_BASELINE, 1 }, /* CYAPA_CMD_MAX_BASELINE */
  200. { CYAPA_SMBUS_MIN_BASELINE, 1 }, /* CYAPA_CMD_MIN_BASELINE */
  201. };
  202. static int cyapa_gen3_try_poll_handler(struct cyapa *cyapa);
  203. /*
  204. * cyapa_smbus_read_block - perform smbus block read command
  205. * @cyapa - private data structure of the driver
  206. * @cmd - the properly encoded smbus command
  207. * @len - expected length of smbus command result
  208. * @values - buffer to store smbus command result
  209. *
  210. * Returns negative errno, else the number of bytes written.
  211. *
  212. * Note:
  213. * In trackpad device, the memory block allocated for I2C register map
  214. * is 256 bytes, so the max read block for I2C bus is 256 bytes.
  215. */
  216. ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
  217. u8 *values)
  218. {
  219. ssize_t ret;
  220. u8 index;
  221. u8 smbus_cmd;
  222. u8 *buf;
  223. struct i2c_client *client = cyapa->client;
  224. if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
  225. return -EINVAL;
  226. if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
  227. /* read specific block registers command. */
  228. smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  229. ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
  230. goto out;
  231. }
  232. ret = 0;
  233. for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
  234. smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
  235. smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
  236. buf = values + I2C_SMBUS_BLOCK_MAX * index;
  237. ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
  238. if (ret < 0)
  239. goto out;
  240. }
  241. out:
  242. return ret > 0 ? len : ret;
  243. }
  244. static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
  245. {
  246. u8 cmd;
  247. if (cyapa->smbus) {
  248. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  249. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  250. } else {
  251. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  252. }
  253. return i2c_smbus_read_byte_data(cyapa->client, cmd);
  254. }
  255. static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
  256. {
  257. u8 cmd;
  258. if (cyapa->smbus) {
  259. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  260. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
  261. } else {
  262. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  263. }
  264. return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
  265. }
  266. ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
  267. u8 *values)
  268. {
  269. return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
  270. }
  271. static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
  272. size_t len, const u8 *values)
  273. {
  274. return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
  275. }
  276. ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
  277. {
  278. u8 cmd;
  279. size_t len;
  280. if (cyapa->smbus) {
  281. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  282. len = cyapa_smbus_cmds[cmd_idx].len;
  283. return cyapa_smbus_read_block(cyapa, cmd, len, values);
  284. }
  285. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  286. len = cyapa_i2c_cmds[cmd_idx].len;
  287. return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
  288. }
  289. /*
  290. * Determine the Gen3 trackpad device's current operating state.
  291. *
  292. */
  293. static int cyapa_gen3_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
  294. {
  295. cyapa->state = CYAPA_STATE_NO_DEVICE;
  296. /* Parse based on Gen3 characteristic registers and bits */
  297. if (reg_data[REG_BL_FILE] == BL_FILE &&
  298. reg_data[REG_BL_ERROR] == BL_ERROR_NO_ERR_IDLE &&
  299. (reg_data[REG_BL_STATUS] ==
  300. (BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID) ||
  301. reg_data[REG_BL_STATUS] == BL_STATUS_RUNNING)) {
  302. /*
  303. * Normal state after power on or reset,
  304. * REG_BL_STATUS == 0x11, firmware image checksum is valid.
  305. * REG_BL_STATUS == 0x10, firmware image checksum is invalid.
  306. */
  307. cyapa->gen = CYAPA_GEN3;
  308. cyapa->state = CYAPA_STATE_BL_IDLE;
  309. } else if (reg_data[REG_BL_FILE] == BL_FILE &&
  310. (reg_data[REG_BL_STATUS] & BL_STATUS_RUNNING) ==
  311. BL_STATUS_RUNNING) {
  312. cyapa->gen = CYAPA_GEN3;
  313. if (reg_data[REG_BL_STATUS] & BL_STATUS_BUSY) {
  314. cyapa->state = CYAPA_STATE_BL_BUSY;
  315. } else {
  316. if ((reg_data[REG_BL_ERROR] & BL_ERROR_BOOTLOADING) ==
  317. BL_ERROR_BOOTLOADING)
  318. cyapa->state = CYAPA_STATE_BL_ACTIVE;
  319. else
  320. cyapa->state = CYAPA_STATE_BL_IDLE;
  321. }
  322. } else if ((reg_data[REG_OP_STATUS] & OP_STATUS_SRC) &&
  323. (reg_data[REG_OP_DATA1] & OP_DATA_VALID)) {
  324. /*
  325. * Normal state when running in operational mode,
  326. * may also not in full power state or
  327. * busying in command process.
  328. */
  329. if (GEN3_FINGER_NUM(reg_data[REG_OP_DATA1]) <=
  330. GEN3_MAX_FINGERS) {
  331. /* Finger number data is valid. */
  332. cyapa->gen = CYAPA_GEN3;
  333. cyapa->state = CYAPA_STATE_OP;
  334. }
  335. } else if (reg_data[REG_OP_STATUS] == 0x0C &&
  336. reg_data[REG_OP_DATA1] == 0x08) {
  337. /* Op state when first two registers overwritten with 0x00 */
  338. cyapa->gen = CYAPA_GEN3;
  339. cyapa->state = CYAPA_STATE_OP;
  340. } else if (reg_data[REG_BL_STATUS] &
  341. (BL_STATUS_RUNNING | BL_STATUS_BUSY)) {
  342. cyapa->gen = CYAPA_GEN3;
  343. cyapa->state = CYAPA_STATE_BL_BUSY;
  344. }
  345. if (cyapa->gen == CYAPA_GEN3 && (cyapa->state == CYAPA_STATE_OP ||
  346. cyapa->state == CYAPA_STATE_BL_IDLE ||
  347. cyapa->state == CYAPA_STATE_BL_ACTIVE ||
  348. cyapa->state == CYAPA_STATE_BL_BUSY))
  349. return 0;
  350. return -EAGAIN;
  351. }
  352. /*
  353. * Enter bootloader by soft resetting the device.
  354. *
  355. * If device is already in the bootloader, the function just returns.
  356. * Otherwise, reset the device; after reset, device enters bootloader idle
  357. * state immediately.
  358. *
  359. * Returns:
  360. * 0 on success
  361. * -EAGAIN device was reset, but is not now in bootloader idle state
  362. * < 0 if the device never responds within the timeout
  363. */
  364. static int cyapa_gen3_bl_enter(struct cyapa *cyapa)
  365. {
  366. int error;
  367. int waiting_time;
  368. error = cyapa_poll_state(cyapa, 500);
  369. if (error)
  370. return error;
  371. if (cyapa->state == CYAPA_STATE_BL_IDLE) {
  372. /* Already in BL_IDLE. Skipping reset. */
  373. return 0;
  374. }
  375. if (cyapa->state != CYAPA_STATE_OP)
  376. return -EAGAIN;
  377. cyapa->operational = false;
  378. cyapa->state = CYAPA_STATE_NO_DEVICE;
  379. error = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET, 0x01);
  380. if (error)
  381. return -EIO;
  382. usleep_range(25000, 50000);
  383. waiting_time = 2000; /* For some shipset, max waiting time is 1~2s. */
  384. do {
  385. error = cyapa_poll_state(cyapa, 500);
  386. if (error) {
  387. if (error == -ETIMEDOUT) {
  388. waiting_time -= 500;
  389. continue;
  390. }
  391. return error;
  392. }
  393. if ((cyapa->state == CYAPA_STATE_BL_IDLE) &&
  394. !(cyapa->status[REG_BL_STATUS] & BL_STATUS_WATCHDOG))
  395. break;
  396. msleep(100);
  397. waiting_time -= 100;
  398. } while (waiting_time > 0);
  399. if ((cyapa->state != CYAPA_STATE_BL_IDLE) ||
  400. (cyapa->status[REG_BL_STATUS] & BL_STATUS_WATCHDOG))
  401. return -EAGAIN;
  402. return 0;
  403. }
  404. static int cyapa_gen3_bl_activate(struct cyapa *cyapa)
  405. {
  406. int error;
  407. error = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_activate),
  408. bl_activate);
  409. if (error)
  410. return error;
  411. /* Wait for bootloader to activate; takes between 2 and 12 seconds */
  412. msleep(2000);
  413. error = cyapa_poll_state(cyapa, 11000);
  414. if (error)
  415. return error;
  416. if (cyapa->state != CYAPA_STATE_BL_ACTIVE)
  417. return -EAGAIN;
  418. return 0;
  419. }
  420. static int cyapa_gen3_bl_deactivate(struct cyapa *cyapa)
  421. {
  422. int error;
  423. error = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
  424. bl_deactivate);
  425. if (error)
  426. return error;
  427. /* Wait for bootloader to switch to idle state; should take < 100ms */
  428. msleep(100);
  429. error = cyapa_poll_state(cyapa, 500);
  430. if (error)
  431. return error;
  432. if (cyapa->state != CYAPA_STATE_BL_IDLE)
  433. return -EAGAIN;
  434. return 0;
  435. }
  436. /*
  437. * Exit bootloader
  438. *
  439. * Send bl_exit command, then wait 50 - 100 ms to let device transition to
  440. * operational mode. If this is the first time the device's firmware is
  441. * running, it can take up to 2 seconds to calibrate its sensors. So, poll
  442. * the device's new state for up to 2 seconds.
  443. *
  444. * Returns:
  445. * -EIO failure while reading from device
  446. * -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
  447. * 0 device is supported and in operational mode
  448. */
  449. static int cyapa_gen3_bl_exit(struct cyapa *cyapa)
  450. {
  451. int error;
  452. error = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
  453. if (error)
  454. return error;
  455. /*
  456. * Wait for bootloader to exit, and operation mode to start.
  457. * Normally, this takes at least 50 ms.
  458. */
  459. msleep(50);
  460. /*
  461. * In addition, when a device boots for the first time after being
  462. * updated to new firmware, it must first calibrate its sensors, which
  463. * can take up to an additional 2 seconds. If the device power is
  464. * running low, this may take even longer.
  465. */
  466. error = cyapa_poll_state(cyapa, 4000);
  467. if (error < 0)
  468. return error;
  469. if (cyapa->state != CYAPA_STATE_OP)
  470. return -EAGAIN;
  471. return 0;
  472. }
  473. static u16 cyapa_gen3_csum(const u8 *buf, size_t count)
  474. {
  475. int i;
  476. u16 csum = 0;
  477. for (i = 0; i < count; i++)
  478. csum += buf[i];
  479. return csum;
  480. }
  481. /*
  482. * Verify the integrity of a CYAPA firmware image file.
  483. *
  484. * The firmware image file is 30848 bytes, composed of 482 64-byte blocks.
  485. *
  486. * The first 2 blocks are the firmware header.
  487. * The next 480 blocks are the firmware image.
  488. *
  489. * The first two bytes of the header hold the header checksum, computed by
  490. * summing the other 126 bytes of the header.
  491. * The last two bytes of the header hold the firmware image checksum, computed
  492. * by summing the 30720 bytes of the image modulo 0xffff.
  493. *
  494. * Both checksums are stored little-endian.
  495. */
  496. static int cyapa_gen3_check_fw(struct cyapa *cyapa, const struct firmware *fw)
  497. {
  498. struct device *dev = &cyapa->client->dev;
  499. u16 csum;
  500. u16 csum_expected;
  501. /* Firmware must match exact 30848 bytes = 482 64-byte blocks. */
  502. if (fw->size != CYAPA_FW_SIZE) {
  503. dev_err(dev, "invalid firmware size = %zu, expected %u.\n",
  504. fw->size, CYAPA_FW_SIZE);
  505. return -EINVAL;
  506. }
  507. /* Verify header block */
  508. csum_expected = (fw->data[0] << 8) | fw->data[1];
  509. csum = cyapa_gen3_csum(&fw->data[2], CYAPA_FW_HDR_SIZE - 2);
  510. if (csum != csum_expected) {
  511. dev_err(dev, "%s %04x, expected: %04x\n",
  512. "invalid firmware header checksum = ",
  513. csum, csum_expected);
  514. return -EINVAL;
  515. }
  516. /* Verify firmware image */
  517. csum_expected = (fw->data[CYAPA_FW_HDR_SIZE - 2] << 8) |
  518. fw->data[CYAPA_FW_HDR_SIZE - 1];
  519. csum = cyapa_gen3_csum(&fw->data[CYAPA_FW_HDR_SIZE],
  520. CYAPA_FW_DATA_SIZE);
  521. if (csum != csum_expected) {
  522. dev_err(dev, "%s %04x, expected: %04x\n",
  523. "invalid firmware header checksum = ",
  524. csum, csum_expected);
  525. return -EINVAL;
  526. }
  527. return 0;
  528. }
  529. /*
  530. * Write a |len| byte long buffer |buf| to the device, by chopping it up into a
  531. * sequence of smaller |CYAPA_CMD_LEN|-length write commands.
  532. *
  533. * The data bytes for a write command are prepended with the 1-byte offset
  534. * of the data relative to the start of |buf|.
  535. */
  536. static int cyapa_gen3_write_buffer(struct cyapa *cyapa,
  537. const u8 *buf, size_t len)
  538. {
  539. int error;
  540. size_t i;
  541. unsigned char cmd[CYAPA_CMD_LEN + 1];
  542. size_t cmd_len;
  543. for (i = 0; i < len; i += CYAPA_CMD_LEN) {
  544. const u8 *payload = &buf[i];
  545. cmd_len = (len - i >= CYAPA_CMD_LEN) ? CYAPA_CMD_LEN : len - i;
  546. cmd[0] = i;
  547. memcpy(&cmd[1], payload, cmd_len);
  548. error = cyapa_i2c_reg_write_block(cyapa, 0, cmd_len + 1, cmd);
  549. if (error)
  550. return error;
  551. }
  552. return 0;
  553. }
  554. /*
  555. * A firmware block write command writes 64 bytes of data to a single flash
  556. * page in the device. The 78-byte block write command has the format:
  557. * <0xff> <CMD> <Key> <Start> <Data> <Data-Checksum> <CMD Checksum>
  558. *
  559. * <0xff> - every command starts with 0xff
  560. * <CMD> - the write command value is 0x39
  561. * <Key> - write commands include an 8-byte key: { 00 01 02 03 04 05 06 07 }
  562. * <Block> - Memory Block number (address / 64) (16-bit, big-endian)
  563. * <Data> - 64 bytes of firmware image data
  564. * <Data Checksum> - sum of 64 <Data> bytes, modulo 0xff
  565. * <CMD Checksum> - sum of 77 bytes, from 0xff to <Data Checksum>
  566. *
  567. * Each write command is split into 5 i2c write transactions of up to 16 bytes.
  568. * Each transaction starts with an i2c register offset: (00, 10, 20, 30, 40).
  569. */
  570. static int cyapa_gen3_write_fw_block(struct cyapa *cyapa,
  571. u16 block, const u8 *data)
  572. {
  573. int ret;
  574. struct gen3_write_block_cmd write_block_cmd;
  575. u8 status[BL_STATUS_SIZE];
  576. int tries;
  577. u8 bl_status, bl_error;
  578. /* Set write command and security key bytes. */
  579. write_block_cmd.checksum_seed = GEN3_BL_CMD_CHECKSUM_SEED;
  580. write_block_cmd.cmd_code = GEN3_BL_CMD_WRITE_BLOCK;
  581. memcpy(write_block_cmd.key, security_key, sizeof(security_key));
  582. put_unaligned_be16(block, &write_block_cmd.block_num);
  583. memcpy(write_block_cmd.block_data, data, CYAPA_FW_BLOCK_SIZE);
  584. write_block_cmd.block_checksum = cyapa_gen3_csum(
  585. write_block_cmd.block_data, CYAPA_FW_BLOCK_SIZE);
  586. write_block_cmd.cmd_checksum = cyapa_gen3_csum((u8 *)&write_block_cmd,
  587. sizeof(write_block_cmd) - 1);
  588. ret = cyapa_gen3_write_buffer(cyapa, (u8 *)&write_block_cmd,
  589. sizeof(write_block_cmd));
  590. if (ret)
  591. return ret;
  592. /* Wait for write to finish */
  593. tries = 11; /* Programming for one block can take about 100ms. */
  594. do {
  595. usleep_range(10000, 20000);
  596. /* Check block write command result status. */
  597. ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET,
  598. BL_STATUS_SIZE, status);
  599. if (ret != BL_STATUS_SIZE)
  600. return (ret < 0) ? ret : -EIO;
  601. } while ((status[REG_BL_STATUS] & BL_STATUS_BUSY) && --tries);
  602. /* Ignore WATCHDOG bit and reserved bits. */
  603. bl_status = status[REG_BL_STATUS] & ~BL_STATUS_REV_MASK;
  604. bl_error = status[REG_BL_ERROR] & ~BL_ERROR_RESERVED;
  605. if (bl_status & BL_STATUS_BUSY)
  606. ret = -ETIMEDOUT;
  607. else if (bl_status != BL_STATUS_RUNNING ||
  608. bl_error != BL_ERROR_BOOTLOADING)
  609. ret = -EIO;
  610. else
  611. ret = 0;
  612. return ret;
  613. }
  614. static int cyapa_gen3_write_blocks(struct cyapa *cyapa,
  615. size_t start_block, size_t block_count,
  616. const u8 *image_data)
  617. {
  618. int error;
  619. int i;
  620. for (i = 0; i < block_count; i++) {
  621. size_t block = start_block + i;
  622. size_t addr = i * CYAPA_FW_BLOCK_SIZE;
  623. const u8 *data = &image_data[addr];
  624. error = cyapa_gen3_write_fw_block(cyapa, block, data);
  625. if (error)
  626. return error;
  627. }
  628. return 0;
  629. }
  630. static int cyapa_gen3_do_fw_update(struct cyapa *cyapa,
  631. const struct firmware *fw)
  632. {
  633. struct device *dev = &cyapa->client->dev;
  634. int error;
  635. /* First write data, starting at byte 128 of fw->data */
  636. error = cyapa_gen3_write_blocks(cyapa,
  637. CYAPA_FW_DATA_BLOCK_START, CYAPA_FW_DATA_BLOCK_COUNT,
  638. &fw->data[CYAPA_FW_HDR_BLOCK_COUNT * CYAPA_FW_BLOCK_SIZE]);
  639. if (error) {
  640. dev_err(dev, "FW update aborted, write image: %d\n", error);
  641. return error;
  642. }
  643. /* Then write checksum */
  644. error = cyapa_gen3_write_blocks(cyapa,
  645. CYAPA_FW_HDR_BLOCK_START, CYAPA_FW_HDR_BLOCK_COUNT,
  646. &fw->data[0]);
  647. if (error) {
  648. dev_err(dev, "FW update aborted, write checksum: %d\n", error);
  649. return error;
  650. }
  651. return 0;
  652. }
  653. static ssize_t cyapa_gen3_do_calibrate(struct device *dev,
  654. struct device_attribute *attr,
  655. const char *buf, size_t count)
  656. {
  657. struct cyapa *cyapa = dev_get_drvdata(dev);
  658. unsigned long timeout;
  659. int ret;
  660. ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
  661. if (ret < 0) {
  662. dev_err(dev, "Error reading dev status: %d\n", ret);
  663. goto out;
  664. }
  665. if ((ret & CYAPA_DEV_NORMAL) != CYAPA_DEV_NORMAL) {
  666. dev_warn(dev, "Trackpad device is busy, device state: 0x%02x\n",
  667. ret);
  668. ret = -EAGAIN;
  669. goto out;
  670. }
  671. ret = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET,
  672. OP_RECALIBRATION_MASK);
  673. if (ret < 0) {
  674. dev_err(dev, "Failed to send calibrate command: %d\n",
  675. ret);
  676. goto out;
  677. }
  678. /* max recalibration timeout 2s. */
  679. timeout = jiffies + 2 * HZ;
  680. do {
  681. /*
  682. * For this recalibration, the max time will not exceed 2s.
  683. * The average time is approximately 500 - 700 ms, and we
  684. * will check the status every 100 - 200ms.
  685. */
  686. msleep(100);
  687. ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
  688. if (ret < 0) {
  689. dev_err(dev, "Error reading dev status: %d\n", ret);
  690. goto out;
  691. }
  692. if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL) {
  693. dev_dbg(dev, "Calibration successful.\n");
  694. goto out;
  695. }
  696. } while (time_is_after_jiffies(timeout));
  697. dev_err(dev, "Failed to calibrate. Timeout.\n");
  698. ret = -ETIMEDOUT;
  699. out:
  700. return ret < 0 ? ret : count;
  701. }
  702. static ssize_t cyapa_gen3_show_baseline(struct device *dev,
  703. struct device_attribute *attr, char *buf)
  704. {
  705. struct cyapa *cyapa = dev_get_drvdata(dev);
  706. int max_baseline, min_baseline;
  707. int tries;
  708. int ret;
  709. ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
  710. if (ret < 0) {
  711. dev_err(dev, "Error reading dev status. err = %d\n", ret);
  712. goto out;
  713. }
  714. if ((ret & CYAPA_DEV_NORMAL) != CYAPA_DEV_NORMAL) {
  715. dev_warn(dev, "Trackpad device is busy. device state = 0x%x\n",
  716. ret);
  717. ret = -EAGAIN;
  718. goto out;
  719. }
  720. ret = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET,
  721. OP_REPORT_BASELINE_MASK);
  722. if (ret < 0) {
  723. dev_err(dev, "Failed to send report baseline command. %d\n",
  724. ret);
  725. goto out;
  726. }
  727. tries = 3; /* Try for 30 to 60 ms */
  728. do {
  729. usleep_range(10000, 20000);
  730. ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
  731. if (ret < 0) {
  732. dev_err(dev, "Error reading dev status. err = %d\n",
  733. ret);
  734. goto out;
  735. }
  736. if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL)
  737. break;
  738. } while (--tries);
  739. if (tries == 0) {
  740. dev_err(dev, "Device timed out going to Normal state.\n");
  741. ret = -ETIMEDOUT;
  742. goto out;
  743. }
  744. ret = cyapa_read_byte(cyapa, CYAPA_CMD_MAX_BASELINE);
  745. if (ret < 0) {
  746. dev_err(dev, "Failed to read max baseline. err = %d\n", ret);
  747. goto out;
  748. }
  749. max_baseline = ret;
  750. ret = cyapa_read_byte(cyapa, CYAPA_CMD_MIN_BASELINE);
  751. if (ret < 0) {
  752. dev_err(dev, "Failed to read min baseline. err = %d\n", ret);
  753. goto out;
  754. }
  755. min_baseline = ret;
  756. dev_dbg(dev, "Baseline report successful. Max: %d Min: %d\n",
  757. max_baseline, min_baseline);
  758. ret = scnprintf(buf, PAGE_SIZE, "%d %d\n", max_baseline, min_baseline);
  759. out:
  760. return ret;
  761. }
  762. /*
  763. * cyapa_get_wait_time_for_pwr_cmd
  764. *
  765. * Compute the amount of time we need to wait after updating the touchpad
  766. * power mode. The touchpad needs to consume the incoming power mode set
  767. * command at the current clock rate.
  768. */
  769. static u16 cyapa_get_wait_time_for_pwr_cmd(u8 pwr_mode)
  770. {
  771. switch (pwr_mode) {
  772. case PWR_MODE_FULL_ACTIVE: return 20;
  773. case PWR_MODE_BTN_ONLY: return 20;
  774. case PWR_MODE_OFF: return 20;
  775. default: return cyapa_pwr_cmd_to_sleep_time(pwr_mode) + 50;
  776. }
  777. }
  778. /*
  779. * Set device power mode
  780. *
  781. * Write to the field to configure power state. Power states include :
  782. * Full : Max scans and report rate.
  783. * Idle : Report rate set by user specified time.
  784. * ButtonOnly : No scans for fingers. When the button is triggered,
  785. * a slave interrupt is asserted to notify host to wake up.
  786. * Off : Only awake for i2c commands from host. No function for button
  787. * or touch sensors.
  788. *
  789. * The power_mode command should conform to the following :
  790. * Full : 0x3f
  791. * Idle : Configurable from 20 to 1000ms. See note below for
  792. * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
  793. * ButtonOnly : 0x01
  794. * Off : 0x00
  795. *
  796. * Device power mode can only be set when device is in operational mode.
  797. */
  798. static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
  799. u16 always_unused, enum cyapa_pm_stage pm_stage)
  800. {
  801. struct input_dev *input = cyapa->input;
  802. u8 power;
  803. int tries;
  804. int sleep_time;
  805. int interval;
  806. int ret;
  807. if (cyapa->state != CYAPA_STATE_OP)
  808. return 0;
  809. tries = SET_POWER_MODE_TRIES;
  810. while (tries--) {
  811. ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
  812. if (ret >= 0)
  813. break;
  814. usleep_range(SET_POWER_MODE_DELAY, 2 * SET_POWER_MODE_DELAY);
  815. }
  816. if (ret < 0)
  817. return ret;
  818. /*
  819. * Return early if the power mode to set is the same as the current
  820. * one.
  821. */
  822. if ((ret & PWR_MODE_MASK) == power_mode)
  823. return 0;
  824. sleep_time = (int)cyapa_get_wait_time_for_pwr_cmd(ret & PWR_MODE_MASK);
  825. power = ret;
  826. power &= ~PWR_MODE_MASK;
  827. power |= power_mode & PWR_MODE_MASK;
  828. tries = SET_POWER_MODE_TRIES;
  829. while (tries--) {
  830. ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
  831. if (!ret)
  832. break;
  833. usleep_range(SET_POWER_MODE_DELAY, 2 * SET_POWER_MODE_DELAY);
  834. }
  835. /*
  836. * Wait for the newly set power command to go in at the previous
  837. * clock speed (scanrate) used by the touchpad firmware. Not
  838. * doing so before issuing the next command may result in errors
  839. * depending on the command's content.
  840. */
  841. if (cyapa->operational && input && input->users &&
  842. (pm_stage == CYAPA_PM_RUNTIME_SUSPEND ||
  843. pm_stage == CYAPA_PM_RUNTIME_RESUME)) {
  844. /* Try to polling in 120Hz, read may fail, just ignore it. */
  845. interval = 1000 / 120;
  846. while (sleep_time > 0) {
  847. if (sleep_time > interval)
  848. msleep(interval);
  849. else
  850. msleep(sleep_time);
  851. sleep_time -= interval;
  852. cyapa_gen3_try_poll_handler(cyapa);
  853. }
  854. } else {
  855. msleep(sleep_time);
  856. }
  857. return ret;
  858. }
  859. static int cyapa_gen3_set_proximity(struct cyapa *cyapa, bool enable)
  860. {
  861. return -EOPNOTSUPP;
  862. }
  863. static int cyapa_gen3_get_query_data(struct cyapa *cyapa)
  864. {
  865. u8 query_data[QUERY_DATA_SIZE];
  866. int ret;
  867. if (cyapa->state != CYAPA_STATE_OP)
  868. return -EBUSY;
  869. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
  870. if (ret != QUERY_DATA_SIZE)
  871. return (ret < 0) ? ret : -EIO;
  872. memcpy(&cyapa->product_id[0], &query_data[0], 5);
  873. cyapa->product_id[5] = '-';
  874. memcpy(&cyapa->product_id[6], &query_data[5], 6);
  875. cyapa->product_id[12] = '-';
  876. memcpy(&cyapa->product_id[13], &query_data[11], 2);
  877. cyapa->product_id[15] = '\0';
  878. cyapa->fw_maj_ver = query_data[15];
  879. cyapa->fw_min_ver = query_data[16];
  880. cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
  881. cyapa->gen = query_data[20] & 0x0f;
  882. cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
  883. cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
  884. cyapa->physical_size_x =
  885. ((query_data[24] & 0xf0) << 4) | query_data[25];
  886. cyapa->physical_size_y =
  887. ((query_data[24] & 0x0f) << 8) | query_data[26];
  888. cyapa->max_z = 255;
  889. return 0;
  890. }
  891. static int cyapa_gen3_bl_query_data(struct cyapa *cyapa)
  892. {
  893. u8 bl_data[CYAPA_CMD_LEN];
  894. int ret;
  895. ret = cyapa_i2c_reg_read_block(cyapa, 0, CYAPA_CMD_LEN, bl_data);
  896. if (ret != CYAPA_CMD_LEN)
  897. return (ret < 0) ? ret : -EIO;
  898. /*
  899. * This value will be updated again when entered application mode.
  900. * If TP failed to enter application mode, this fw version values
  901. * can be used as a reference.
  902. * This firmware version valid when fw image checksum is valid.
  903. */
  904. if (bl_data[REG_BL_STATUS] ==
  905. (BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID)) {
  906. cyapa->fw_maj_ver = bl_data[GEN3_BL_IDLE_FW_MAJ_VER_OFFSET];
  907. cyapa->fw_min_ver = bl_data[GEN3_BL_IDLE_FW_MIN_VER_OFFSET];
  908. }
  909. return 0;
  910. }
  911. /*
  912. * Check if device is operational.
  913. *
  914. * An operational device is responding, has exited bootloader, and has
  915. * firmware supported by this driver.
  916. *
  917. * Returns:
  918. * -EBUSY no device or in bootloader
  919. * -EIO failure while reading from device
  920. * -EAGAIN device is still in bootloader
  921. * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
  922. * -EINVAL device is in operational mode, but not supported by this driver
  923. * 0 device is supported
  924. */
  925. static int cyapa_gen3_do_operational_check(struct cyapa *cyapa)
  926. {
  927. struct device *dev = &cyapa->client->dev;
  928. int error;
  929. switch (cyapa->state) {
  930. case CYAPA_STATE_BL_ACTIVE:
  931. error = cyapa_gen3_bl_deactivate(cyapa);
  932. if (error) {
  933. dev_err(dev, "failed to bl_deactivate: %d\n", error);
  934. return error;
  935. }
  936. fallthrough;
  937. case CYAPA_STATE_BL_IDLE:
  938. /* Try to get firmware version in bootloader mode. */
  939. cyapa_gen3_bl_query_data(cyapa);
  940. error = cyapa_gen3_bl_exit(cyapa);
  941. if (error) {
  942. dev_err(dev, "failed to bl_exit: %d\n", error);
  943. return error;
  944. }
  945. fallthrough;
  946. case CYAPA_STATE_OP:
  947. /*
  948. * Reading query data before going back to the full mode
  949. * may cause problems, so we set the power mode first here.
  950. */
  951. error = cyapa_gen3_set_power_mode(cyapa,
  952. PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
  953. if (error)
  954. dev_err(dev, "%s: set full power mode failed: %d\n",
  955. __func__, error);
  956. error = cyapa_gen3_get_query_data(cyapa);
  957. if (error < 0)
  958. return error;
  959. /* Only support firmware protocol gen3 */
  960. if (cyapa->gen != CYAPA_GEN3) {
  961. dev_err(dev, "unsupported protocol version (%d)",
  962. cyapa->gen);
  963. return -EINVAL;
  964. }
  965. /* Only support product ID starting with CYTRA */
  966. if (memcmp(cyapa->product_id, product_id,
  967. strlen(product_id)) != 0) {
  968. dev_err(dev, "unsupported product ID (%s)\n",
  969. cyapa->product_id);
  970. return -EINVAL;
  971. }
  972. return 0;
  973. default:
  974. return -EIO;
  975. }
  976. return 0;
  977. }
  978. /*
  979. * Return false, do not continue process
  980. * Return true, continue process.
  981. */
  982. static bool cyapa_gen3_irq_cmd_handler(struct cyapa *cyapa)
  983. {
  984. /* Not gen3 irq command response, skip for continue. */
  985. if (cyapa->gen != CYAPA_GEN3)
  986. return true;
  987. if (cyapa->operational)
  988. return true;
  989. /*
  990. * Driver in detecting or other interface function processing,
  991. * so, stop cyapa_gen3_irq_handler to continue process to
  992. * avoid unwanted to error detecting and processing.
  993. *
  994. * And also, avoid the periodically asserted interrupts to be processed
  995. * as touch inputs when gen3 failed to launch into application mode,
  996. * which will cause gen3 stays in bootloader mode.
  997. */
  998. return false;
  999. }
  1000. static int cyapa_gen3_event_process(struct cyapa *cyapa,
  1001. struct cyapa_reg_data *data)
  1002. {
  1003. struct input_dev *input = cyapa->input;
  1004. int num_fingers;
  1005. int i;
  1006. num_fingers = (data->finger_btn >> 4) & 0x0f;
  1007. for (i = 0; i < num_fingers; i++) {
  1008. const struct cyapa_touch *touch = &data->touches[i];
  1009. /* Note: touch->id range is 1 to 15; slots are 0 to 14. */
  1010. int slot = touch->id - 1;
  1011. input_mt_slot(input, slot);
  1012. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  1013. input_report_abs(input, ABS_MT_POSITION_X,
  1014. ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
  1015. input_report_abs(input, ABS_MT_POSITION_Y,
  1016. ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
  1017. input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
  1018. }
  1019. input_mt_sync_frame(input);
  1020. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  1021. input_report_key(input, BTN_LEFT,
  1022. !!(data->finger_btn & OP_DATA_LEFT_BTN));
  1023. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  1024. input_report_key(input, BTN_MIDDLE,
  1025. !!(data->finger_btn & OP_DATA_MIDDLE_BTN));
  1026. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  1027. input_report_key(input, BTN_RIGHT,
  1028. !!(data->finger_btn & OP_DATA_RIGHT_BTN));
  1029. input_sync(input);
  1030. return 0;
  1031. }
  1032. static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
  1033. {
  1034. struct device *dev = &cyapa->client->dev;
  1035. struct cyapa_reg_data data;
  1036. int ret;
  1037. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
  1038. if (ret != sizeof(data)) {
  1039. dev_err(dev, "failed to read report data, (%d)\n", ret);
  1040. return -EINVAL;
  1041. }
  1042. if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
  1043. (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
  1044. (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
  1045. dev_err(dev, "invalid device state bytes: %02x %02x\n",
  1046. data.device_status, data.finger_btn);
  1047. return -EINVAL;
  1048. }
  1049. return cyapa_gen3_event_process(cyapa, &data);
  1050. }
  1051. /*
  1052. * This function will be called in the cyapa_gen3_set_power_mode function,
  1053. * and it's known that it may failed in some situation after the set power
  1054. * mode command was sent. So this function is aimed to avoid the knwon
  1055. * and unwanted output I2C and data parse error messages.
  1056. */
  1057. static int cyapa_gen3_try_poll_handler(struct cyapa *cyapa)
  1058. {
  1059. struct cyapa_reg_data data;
  1060. int ret;
  1061. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
  1062. if (ret != sizeof(data))
  1063. return -EINVAL;
  1064. if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
  1065. (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
  1066. (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID)
  1067. return -EINVAL;
  1068. return cyapa_gen3_event_process(cyapa, &data);
  1069. }
  1070. static int cyapa_gen3_initialize(struct cyapa *cyapa) { return 0; }
  1071. static int cyapa_gen3_bl_initiate(struct cyapa *cyapa,
  1072. const struct firmware *fw) { return 0; }
  1073. static int cyapa_gen3_empty_output_data(struct cyapa *cyapa,
  1074. u8 *buf, int *len, cb_sort func) { return 0; }
  1075. const struct cyapa_dev_ops cyapa_gen3_ops = {
  1076. .check_fw = cyapa_gen3_check_fw,
  1077. .bl_enter = cyapa_gen3_bl_enter,
  1078. .bl_activate = cyapa_gen3_bl_activate,
  1079. .update_fw = cyapa_gen3_do_fw_update,
  1080. .bl_deactivate = cyapa_gen3_bl_deactivate,
  1081. .bl_initiate = cyapa_gen3_bl_initiate,
  1082. .show_baseline = cyapa_gen3_show_baseline,
  1083. .calibrate_store = cyapa_gen3_do_calibrate,
  1084. .initialize = cyapa_gen3_initialize,
  1085. .state_parse = cyapa_gen3_state_parse,
  1086. .operational_check = cyapa_gen3_do_operational_check,
  1087. .irq_handler = cyapa_gen3_irq_handler,
  1088. .irq_cmd_handler = cyapa_gen3_irq_cmd_handler,
  1089. .sort_empty_output_data = cyapa_gen3_empty_output_data,
  1090. .set_power_mode = cyapa_gen3_set_power_mode,
  1091. .set_proximity = cyapa_gen3_set_proximity,
  1092. };