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