cyapa.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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-2014 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. bool irq_wake; /* irq wake is enabled */
  184. bool smbus;
  185. /* read from query data region. */
  186. char product_id[16];
  187. u8 btn_capability;
  188. u8 gen;
  189. int max_abs_x;
  190. int max_abs_y;
  191. int physical_size_x;
  192. int physical_size_y;
  193. };
  194. static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
  195. 0x04, 0x05, 0x06, 0x07 };
  196. static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
  197. 0x05, 0x06, 0x07 };
  198. struct cyapa_cmd_len {
  199. u8 cmd;
  200. u8 len;
  201. };
  202. #define CYAPA_ADAPTER_FUNC_NONE 0
  203. #define CYAPA_ADAPTER_FUNC_I2C 1
  204. #define CYAPA_ADAPTER_FUNC_SMBUS 2
  205. #define CYAPA_ADAPTER_FUNC_BOTH 3
  206. /*
  207. * macros for SMBus communication
  208. */
  209. #define SMBUS_READ 0x01
  210. #define SMBUS_WRITE 0x00
  211. #define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
  212. #define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
  213. #define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
  214. #define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
  215. /* for byte read/write command */
  216. #define CMD_RESET 0
  217. #define CMD_POWER_MODE 1
  218. #define CMD_DEV_STATUS 2
  219. #define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
  220. #define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
  221. #define CYAPA_SMBUS_POWER_MODE SMBUS_BYTE_CMD(CMD_POWER_MODE)
  222. #define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
  223. /* for group registers read/write command */
  224. #define REG_GROUP_DATA 0
  225. #define REG_GROUP_CMD 2
  226. #define REG_GROUP_QUERY 3
  227. #define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
  228. #define CYAPA_SMBUS_GROUP_DATA SMBUS_GROUP_CMD(REG_GROUP_DATA)
  229. #define CYAPA_SMBUS_GROUP_CMD SMBUS_GROUP_CMD(REG_GROUP_CMD)
  230. #define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
  231. /* for register block read/write command */
  232. #define CMD_BL_STATUS 0
  233. #define CMD_BL_HEAD 1
  234. #define CMD_BL_CMD 2
  235. #define CMD_BL_DATA 3
  236. #define CMD_BL_ALL 4
  237. #define CMD_BLK_PRODUCT_ID 5
  238. #define CMD_BLK_HEAD 6
  239. #define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
  240. /* register block read/write command in bootloader mode */
  241. #define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
  242. #define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
  243. #define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
  244. #define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
  245. #define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
  246. /* register block read/write command in operational mode */
  247. #define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
  248. #define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
  249. static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
  250. { CYAPA_OFFSET_SOFT_RESET, 1 },
  251. { REG_OFFSET_COMMAND_BASE + 1, 1 },
  252. { REG_OFFSET_DATA_BASE, 1 },
  253. { REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
  254. { REG_OFFSET_COMMAND_BASE, 0 },
  255. { REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE },
  256. { BL_HEAD_OFFSET, 3 },
  257. { BL_HEAD_OFFSET, 16 },
  258. { BL_HEAD_OFFSET, 16 },
  259. { BL_DATA_OFFSET, 16 },
  260. { BL_HEAD_OFFSET, 32 },
  261. { REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
  262. { REG_OFFSET_DATA_BASE, 32 }
  263. };
  264. static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
  265. { CYAPA_SMBUS_RESET, 1 },
  266. { CYAPA_SMBUS_POWER_MODE, 1 },
  267. { CYAPA_SMBUS_DEV_STATUS, 1 },
  268. { CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
  269. { CYAPA_SMBUS_GROUP_CMD, 2 },
  270. { CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
  271. { CYAPA_SMBUS_BL_STATUS, 3 },
  272. { CYAPA_SMBUS_BL_HEAD, 16 },
  273. { CYAPA_SMBUS_BL_CMD, 16 },
  274. { CYAPA_SMBUS_BL_DATA, 16 },
  275. { CYAPA_SMBUS_BL_ALL, 32 },
  276. { CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
  277. { CYAPA_SMBUS_BLK_HEAD, 16 },
  278. };
  279. static ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
  280. u8 *values)
  281. {
  282. return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
  283. }
  284. static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
  285. size_t len, const u8 *values)
  286. {
  287. return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
  288. }
  289. /*
  290. * cyapa_smbus_read_block - perform smbus block read command
  291. * @cyapa - private data structure of the driver
  292. * @cmd - the properly encoded smbus command
  293. * @len - expected length of smbus command result
  294. * @values - buffer to store smbus command result
  295. *
  296. * Returns negative errno, else the number of bytes written.
  297. *
  298. * Note:
  299. * In trackpad device, the memory block allocated for I2C register map
  300. * is 256 bytes, so the max read block for I2C bus is 256 bytes.
  301. */
  302. static ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
  303. u8 *values)
  304. {
  305. ssize_t ret;
  306. u8 index;
  307. u8 smbus_cmd;
  308. u8 *buf;
  309. struct i2c_client *client = cyapa->client;
  310. if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
  311. return -EINVAL;
  312. if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
  313. /* read specific block registers command. */
  314. smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  315. ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
  316. goto out;
  317. }
  318. ret = 0;
  319. for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
  320. smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
  321. smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
  322. buf = values + I2C_SMBUS_BLOCK_MAX * index;
  323. ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
  324. if (ret < 0)
  325. goto out;
  326. }
  327. out:
  328. return ret > 0 ? len : ret;
  329. }
  330. static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
  331. {
  332. u8 cmd;
  333. if (cyapa->smbus) {
  334. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  335. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
  336. } else {
  337. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  338. }
  339. return i2c_smbus_read_byte_data(cyapa->client, cmd);
  340. }
  341. static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
  342. {
  343. u8 cmd;
  344. if (cyapa->smbus) {
  345. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  346. cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
  347. } else {
  348. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  349. }
  350. return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
  351. }
  352. static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
  353. {
  354. u8 cmd;
  355. size_t len;
  356. if (cyapa->smbus) {
  357. cmd = cyapa_smbus_cmds[cmd_idx].cmd;
  358. len = cyapa_smbus_cmds[cmd_idx].len;
  359. return cyapa_smbus_read_block(cyapa, cmd, len, values);
  360. } else {
  361. cmd = cyapa_i2c_cmds[cmd_idx].cmd;
  362. len = cyapa_i2c_cmds[cmd_idx].len;
  363. return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
  364. }
  365. }
  366. /*
  367. * Query device for its current operating state.
  368. *
  369. */
  370. static int cyapa_get_state(struct cyapa *cyapa)
  371. {
  372. u8 status[BL_STATUS_SIZE];
  373. int error;
  374. cyapa->state = CYAPA_STATE_NO_DEVICE;
  375. /*
  376. * Get trackpad status by reading 3 registers starting from 0.
  377. * If the device is in the bootloader, this will be BL_HEAD.
  378. * If the device is in operation mode, this will be the DATA regs.
  379. *
  380. */
  381. error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
  382. status);
  383. /*
  384. * On smbus systems in OP mode, the i2c_reg_read will fail with
  385. * -ETIMEDOUT. In this case, try again using the smbus equivalent
  386. * command. This should return a BL_HEAD indicating CYAPA_STATE_OP.
  387. */
  388. if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO))
  389. error = cyapa_read_block(cyapa, CYAPA_CMD_BL_STATUS, status);
  390. if (error != BL_STATUS_SIZE)
  391. goto error;
  392. if ((status[REG_OP_STATUS] & OP_STATUS_SRC) == OP_STATUS_SRC) {
  393. switch (status[REG_OP_STATUS] & OP_STATUS_DEV) {
  394. case CYAPA_DEV_NORMAL:
  395. case CYAPA_DEV_BUSY:
  396. cyapa->state = CYAPA_STATE_OP;
  397. break;
  398. default:
  399. error = -EAGAIN;
  400. goto error;
  401. }
  402. } else {
  403. if (status[REG_BL_STATUS] & BL_STATUS_BUSY)
  404. cyapa->state = CYAPA_STATE_BL_BUSY;
  405. else if (status[REG_BL_ERROR] & BL_ERROR_BOOTLOADING)
  406. cyapa->state = CYAPA_STATE_BL_ACTIVE;
  407. else
  408. cyapa->state = CYAPA_STATE_BL_IDLE;
  409. }
  410. return 0;
  411. error:
  412. return (error < 0) ? error : -EAGAIN;
  413. }
  414. /*
  415. * Poll device for its status in a loop, waiting up to timeout for a response.
  416. *
  417. * When the device switches state, it usually takes ~300 ms.
  418. * However, when running a new firmware image, the device must calibrate its
  419. * sensors, which can take as long as 2 seconds.
  420. *
  421. * Note: The timeout has granularity of the polling rate, which is 100 ms.
  422. *
  423. * Returns:
  424. * 0 when the device eventually responds with a valid non-busy state.
  425. * -ETIMEDOUT if device never responds (too many -EAGAIN)
  426. * < 0 other errors
  427. */
  428. static int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
  429. {
  430. int error;
  431. int tries = timeout / 100;
  432. error = cyapa_get_state(cyapa);
  433. while ((error || cyapa->state >= CYAPA_STATE_BL_BUSY) && tries--) {
  434. msleep(100);
  435. error = cyapa_get_state(cyapa);
  436. }
  437. return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
  438. }
  439. static int cyapa_bl_deactivate(struct cyapa *cyapa)
  440. {
  441. int error;
  442. error = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
  443. bl_deactivate);
  444. if (error)
  445. return error;
  446. /* wait for bootloader to switch to idle state; should take < 100ms */
  447. msleep(100);
  448. error = cyapa_poll_state(cyapa, 500);
  449. if (error)
  450. return error;
  451. if (cyapa->state != CYAPA_STATE_BL_IDLE)
  452. return -EAGAIN;
  453. return 0;
  454. }
  455. /*
  456. * Exit bootloader
  457. *
  458. * Send bl_exit command, then wait 50 - 100 ms to let device transition to
  459. * operational mode. If this is the first time the device's firmware is
  460. * running, it can take up to 2 seconds to calibrate its sensors. So, poll
  461. * the device's new state for up to 2 seconds.
  462. *
  463. * Returns:
  464. * -EIO failure while reading from device
  465. * -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
  466. * 0 device is supported and in operational mode
  467. */
  468. static int cyapa_bl_exit(struct cyapa *cyapa)
  469. {
  470. int error;
  471. error = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
  472. if (error)
  473. return error;
  474. /*
  475. * Wait for bootloader to exit, and operation mode to start.
  476. * Normally, this takes at least 50 ms.
  477. */
  478. usleep_range(50000, 100000);
  479. /*
  480. * In addition, when a device boots for the first time after being
  481. * updated to new firmware, it must first calibrate its sensors, which
  482. * can take up to an additional 2 seconds.
  483. */
  484. error = cyapa_poll_state(cyapa, 2000);
  485. if (error < 0)
  486. return error;
  487. if (cyapa->state != CYAPA_STATE_OP)
  488. return -EAGAIN;
  489. return 0;
  490. }
  491. /*
  492. * Set device power mode
  493. *
  494. */
  495. static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
  496. {
  497. struct device *dev = &cyapa->client->dev;
  498. int ret;
  499. u8 power;
  500. if (cyapa->state != CYAPA_STATE_OP)
  501. return 0;
  502. ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
  503. if (ret < 0)
  504. return ret;
  505. power = ret & ~PWR_MODE_MASK;
  506. power |= power_mode & PWR_MODE_MASK;
  507. ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
  508. if (ret < 0) {
  509. dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
  510. power_mode, ret);
  511. return ret;
  512. }
  513. return 0;
  514. }
  515. static int cyapa_get_query_data(struct cyapa *cyapa)
  516. {
  517. u8 query_data[QUERY_DATA_SIZE];
  518. int ret;
  519. if (cyapa->state != CYAPA_STATE_OP)
  520. return -EBUSY;
  521. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
  522. if (ret < 0)
  523. return ret;
  524. if (ret != QUERY_DATA_SIZE)
  525. return -EIO;
  526. memcpy(&cyapa->product_id[0], &query_data[0], 5);
  527. cyapa->product_id[5] = '-';
  528. memcpy(&cyapa->product_id[6], &query_data[5], 6);
  529. cyapa->product_id[12] = '-';
  530. memcpy(&cyapa->product_id[13], &query_data[11], 2);
  531. cyapa->product_id[15] = '\0';
  532. cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
  533. cyapa->gen = query_data[20] & 0x0f;
  534. cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
  535. cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
  536. cyapa->physical_size_x =
  537. ((query_data[24] & 0xf0) << 4) | query_data[25];
  538. cyapa->physical_size_y =
  539. ((query_data[24] & 0x0f) << 8) | query_data[26];
  540. return 0;
  541. }
  542. /*
  543. * Check if device is operational.
  544. *
  545. * An operational device is responding, has exited bootloader, and has
  546. * firmware supported by this driver.
  547. *
  548. * Returns:
  549. * -EBUSY no device or in bootloader
  550. * -EIO failure while reading from device
  551. * -EAGAIN device is still in bootloader
  552. * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
  553. * -EINVAL device is in operational mode, but not supported by this driver
  554. * 0 device is supported
  555. */
  556. static int cyapa_check_is_operational(struct cyapa *cyapa)
  557. {
  558. struct device *dev = &cyapa->client->dev;
  559. static const char unique_str[] = "CYTRA";
  560. int error;
  561. error = cyapa_poll_state(cyapa, 2000);
  562. if (error)
  563. return error;
  564. switch (cyapa->state) {
  565. case CYAPA_STATE_BL_ACTIVE:
  566. error = cyapa_bl_deactivate(cyapa);
  567. if (error)
  568. return error;
  569. /* Fallthrough state */
  570. case CYAPA_STATE_BL_IDLE:
  571. error = cyapa_bl_exit(cyapa);
  572. if (error)
  573. return error;
  574. /* Fallthrough state */
  575. case CYAPA_STATE_OP:
  576. error = cyapa_get_query_data(cyapa);
  577. if (error)
  578. return error;
  579. /* only support firmware protocol gen3 */
  580. if (cyapa->gen != CYAPA_GEN3) {
  581. dev_err(dev, "unsupported protocol version (%d)",
  582. cyapa->gen);
  583. return -EINVAL;
  584. }
  585. /* only support product ID starting with CYTRA */
  586. if (memcmp(cyapa->product_id, unique_str,
  587. sizeof(unique_str) - 1) != 0) {
  588. dev_err(dev, "unsupported product ID (%s)\n",
  589. cyapa->product_id);
  590. return -EINVAL;
  591. }
  592. return 0;
  593. default:
  594. return -EIO;
  595. }
  596. return 0;
  597. }
  598. static irqreturn_t cyapa_irq(int irq, void *dev_id)
  599. {
  600. struct cyapa *cyapa = dev_id;
  601. struct device *dev = &cyapa->client->dev;
  602. struct input_dev *input = cyapa->input;
  603. struct cyapa_reg_data data;
  604. int i;
  605. int ret;
  606. int num_fingers;
  607. if (device_may_wakeup(dev))
  608. pm_wakeup_event(dev, 0);
  609. ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
  610. if (ret != sizeof(data))
  611. goto out;
  612. if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
  613. (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
  614. (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
  615. goto out;
  616. }
  617. num_fingers = (data.finger_btn >> 4) & 0x0f;
  618. for (i = 0; i < num_fingers; i++) {
  619. const struct cyapa_touch *touch = &data.touches[i];
  620. /* Note: touch->id range is 1 to 15; slots are 0 to 14. */
  621. int slot = touch->id - 1;
  622. input_mt_slot(input, slot);
  623. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  624. input_report_abs(input, ABS_MT_POSITION_X,
  625. ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
  626. input_report_abs(input, ABS_MT_POSITION_Y,
  627. ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
  628. input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
  629. }
  630. input_mt_sync_frame(input);
  631. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  632. input_report_key(input, BTN_LEFT,
  633. data.finger_btn & OP_DATA_LEFT_BTN);
  634. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  635. input_report_key(input, BTN_MIDDLE,
  636. data.finger_btn & OP_DATA_MIDDLE_BTN);
  637. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  638. input_report_key(input, BTN_RIGHT,
  639. data.finger_btn & OP_DATA_RIGHT_BTN);
  640. input_sync(input);
  641. out:
  642. return IRQ_HANDLED;
  643. }
  644. static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
  645. {
  646. u8 ret = CYAPA_ADAPTER_FUNC_NONE;
  647. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  648. ret |= CYAPA_ADAPTER_FUNC_I2C;
  649. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  650. I2C_FUNC_SMBUS_BLOCK_DATA |
  651. I2C_FUNC_SMBUS_I2C_BLOCK))
  652. ret |= CYAPA_ADAPTER_FUNC_SMBUS;
  653. return ret;
  654. }
  655. static int cyapa_open(struct input_dev *input)
  656. {
  657. struct cyapa *cyapa = input_get_drvdata(input);
  658. struct i2c_client *client = cyapa->client;
  659. int error;
  660. error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
  661. if (error) {
  662. dev_err(&client->dev, "set active power failed: %d\n", error);
  663. return error;
  664. }
  665. enable_irq(client->irq);
  666. return 0;
  667. }
  668. static void cyapa_close(struct input_dev *input)
  669. {
  670. struct cyapa *cyapa = input_get_drvdata(input);
  671. disable_irq(cyapa->client->irq);
  672. cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
  673. }
  674. static int cyapa_create_input_dev(struct cyapa *cyapa)
  675. {
  676. struct device *dev = &cyapa->client->dev;
  677. struct input_dev *input;
  678. int error;
  679. if (!cyapa->physical_size_x || !cyapa->physical_size_y)
  680. return -EINVAL;
  681. input = devm_input_allocate_device(dev);
  682. if (!input) {
  683. dev_err(dev, "failed to allocate memory for input device.\n");
  684. return -ENOMEM;
  685. }
  686. input->name = CYAPA_NAME;
  687. input->phys = cyapa->phys;
  688. input->id.bustype = BUS_I2C;
  689. input->id.version = 1;
  690. input->id.product = 0; /* Means any product in eventcomm. */
  691. input->dev.parent = &cyapa->client->dev;
  692. input->open = cyapa_open;
  693. input->close = cyapa_close;
  694. input_set_drvdata(input, cyapa);
  695. __set_bit(EV_ABS, input->evbit);
  696. /* Finger position */
  697. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
  698. 0);
  699. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
  700. 0);
  701. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  702. input_abs_set_res(input, ABS_MT_POSITION_X,
  703. cyapa->max_abs_x / cyapa->physical_size_x);
  704. input_abs_set_res(input, ABS_MT_POSITION_Y,
  705. cyapa->max_abs_y / cyapa->physical_size_y);
  706. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
  707. __set_bit(BTN_LEFT, input->keybit);
  708. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
  709. __set_bit(BTN_MIDDLE, input->keybit);
  710. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
  711. __set_bit(BTN_RIGHT, input->keybit);
  712. if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
  713. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  714. /* Handle pointer emulation and unused slots in core */
  715. error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
  716. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
  717. if (error) {
  718. dev_err(dev, "failed to initialize MT slots: %d\n", error);
  719. return error;
  720. }
  721. cyapa->input = input;
  722. return 0;
  723. }
  724. static int cyapa_probe(struct i2c_client *client,
  725. const struct i2c_device_id *dev_id)
  726. {
  727. struct device *dev = &client->dev;
  728. struct cyapa *cyapa;
  729. u8 adapter_func;
  730. int error;
  731. adapter_func = cyapa_check_adapter_functionality(client);
  732. if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
  733. dev_err(dev, "not a supported I2C/SMBus adapter\n");
  734. return -EIO;
  735. }
  736. cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
  737. if (!cyapa)
  738. return -ENOMEM;
  739. cyapa->gen = CYAPA_GEN3;
  740. cyapa->client = client;
  741. i2c_set_clientdata(client, cyapa);
  742. sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
  743. client->addr);
  744. /* i2c isn't supported, use smbus */
  745. if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
  746. cyapa->smbus = true;
  747. cyapa->state = CYAPA_STATE_NO_DEVICE;
  748. error = cyapa_check_is_operational(cyapa);
  749. if (error) {
  750. dev_err(dev, "device not operational, %d\n", error);
  751. return error;
  752. }
  753. /* Power down the device until we need it */
  754. error = cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
  755. if (error) {
  756. dev_err(dev, "failed to quiesce the device: %d\n", error);
  757. return error;
  758. }
  759. error = cyapa_create_input_dev(cyapa);
  760. if (error)
  761. return error;
  762. error = devm_request_threaded_irq(dev, client->irq,
  763. NULL, cyapa_irq,
  764. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  765. "cyapa", cyapa);
  766. if (error) {
  767. dev_err(dev, "failed to request threaded irq: %d\n", error);
  768. return error;
  769. }
  770. /* Disable IRQ until the device is opened */
  771. disable_irq(client->irq);
  772. /* Register the device in input subsystem */
  773. error = input_register_device(cyapa->input);
  774. if (error) {
  775. dev_err(dev, "failed to register input device: %d\n", error);
  776. return error;
  777. }
  778. return 0;
  779. }
  780. static int __maybe_unused cyapa_suspend(struct device *dev)
  781. {
  782. struct i2c_client *client = to_i2c_client(dev);
  783. struct cyapa *cyapa = i2c_get_clientdata(client);
  784. struct input_dev *input = cyapa->input;
  785. u8 power_mode;
  786. int error;
  787. error = mutex_lock_interruptible(&input->mutex);
  788. if (error)
  789. return error;
  790. disable_irq(client->irq);
  791. /*
  792. * Set trackpad device to idle mode if wakeup is allowed,
  793. * otherwise turn off.
  794. */
  795. power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
  796. : PWR_MODE_OFF;
  797. error = cyapa_set_power_mode(cyapa, power_mode);
  798. if (error)
  799. dev_err(dev, "resume: set power mode to %d failed: %d\n",
  800. power_mode, error);
  801. if (device_may_wakeup(dev))
  802. cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
  803. mutex_unlock(&input->mutex);
  804. return 0;
  805. }
  806. static int __maybe_unused cyapa_resume(struct device *dev)
  807. {
  808. struct i2c_client *client = to_i2c_client(dev);
  809. struct cyapa *cyapa = i2c_get_clientdata(client);
  810. struct input_dev *input = cyapa->input;
  811. u8 power_mode;
  812. int error;
  813. mutex_lock(&input->mutex);
  814. if (device_may_wakeup(dev) && cyapa->irq_wake)
  815. disable_irq_wake(client->irq);
  816. power_mode = input->users ? PWR_MODE_FULL_ACTIVE : PWR_MODE_OFF;
  817. error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
  818. if (error)
  819. dev_warn(dev, "resume: set power mode to %d failed: %d\n",
  820. power_mode, error);
  821. enable_irq(client->irq);
  822. mutex_unlock(&input->mutex);
  823. return 0;
  824. }
  825. static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
  826. static const struct i2c_device_id cyapa_id_table[] = {
  827. { "cyapa", 0 },
  828. { },
  829. };
  830. MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
  831. static struct i2c_driver cyapa_driver = {
  832. .driver = {
  833. .name = "cyapa",
  834. .owner = THIS_MODULE,
  835. .pm = &cyapa_pm_ops,
  836. },
  837. .probe = cyapa_probe,
  838. .id_table = cyapa_id_table,
  839. };
  840. module_i2c_driver(cyapa_driver);
  841. MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
  842. MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
  843. MODULE_LICENSE("GPL");