Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 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 | * |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 9 | * Copyright (C) 2011-2014 Cypress Semiconductor, Inc. |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 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> |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 24 | #include <linux/slab.h> |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
| 26 | #include "cyapa.h" |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 27 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 28 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 29 | #define CYAPA_ADAPTER_FUNC_NONE 0 |
| 30 | #define CYAPA_ADAPTER_FUNC_I2C 1 |
| 31 | #define CYAPA_ADAPTER_FUNC_SMBUS 2 |
| 32 | #define CYAPA_ADAPTER_FUNC_BOTH 3 |
| 33 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 34 | const char product_id[] = "CYTRA"; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 35 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 36 | static int cyapa_reinitialize(struct cyapa *cyapa); |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 37 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 38 | static inline bool cyapa_is_bootloader_mode(struct cyapa *cyapa) |
| 39 | { |
| 40 | if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL) |
| 41 | return true; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 42 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 43 | if (cyapa->gen == CYAPA_GEN3 && |
| 44 | cyapa->state >= CYAPA_STATE_BL_BUSY && |
| 45 | cyapa->state <= CYAPA_STATE_BL_ACTIVE) |
| 46 | return true; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 47 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 48 | return false; |
| 49 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 50 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 51 | static inline bool cyapa_is_operational_mode(struct cyapa *cyapa) |
| 52 | { |
| 53 | if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP) |
| 54 | return true; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 55 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 56 | if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP) |
| 57 | return true; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 58 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 59 | return false; |
| 60 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 61 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 62 | /* Returns 0 on success, else negative errno on failure. */ |
| 63 | static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len, |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 64 | u8 *values) |
| 65 | { |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 66 | struct i2c_client *client = cyapa->client; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 67 | struct i2c_msg msgs[] = { |
| 68 | { |
| 69 | .addr = client->addr, |
| 70 | .flags = 0, |
| 71 | .len = 1, |
| 72 | .buf = ®, |
| 73 | }, |
| 74 | { |
| 75 | .addr = client->addr, |
| 76 | .flags = I2C_M_RD, |
| 77 | .len = len, |
| 78 | .buf = values, |
| 79 | }, |
| 80 | }; |
| 81 | int ret; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 82 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 83 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 84 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 85 | if (ret != ARRAY_SIZE(msgs)) |
| 86 | return ret < 0 ? ret : -EIO; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 87 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 88 | return 0; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 91 | /** |
| 92 | * cyapa_i2c_write - Execute i2c block data write operation |
| 93 | * @cyapa: Handle to this driver |
| 94 | * @ret: Offset of the data to written in the register map |
| 95 | * @len: number of bytes to write |
| 96 | * @values: Data to be written |
| 97 | * |
| 98 | * Return negative errno code on error; return zero when success. |
| 99 | */ |
| 100 | static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg, |
| 101 | size_t len, const void *values) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 102 | { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 103 | struct i2c_client *client = cyapa->client; |
| 104 | char buf[32]; |
| 105 | int ret; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 106 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 107 | if (len > sizeof(buf) - 1) |
| 108 | return -ENOMEM; |
| 109 | |
| 110 | buf[0] = reg; |
| 111 | memcpy(&buf[1], values, len); |
| 112 | |
| 113 | ret = i2c_master_send(client, buf, len + 1); |
| 114 | if (ret != len + 1) |
| 115 | return ret < 0 ? ret : -EIO; |
| 116 | |
| 117 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 120 | static u8 cyapa_check_adapter_functionality(struct i2c_client *client) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 121 | { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 122 | u8 ret = CYAPA_ADAPTER_FUNC_NONE; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 123 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 124 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 125 | ret |= CYAPA_ADAPTER_FUNC_I2C; |
| 126 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 127 | I2C_FUNC_SMBUS_BLOCK_DATA | |
| 128 | I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 129 | ret |= CYAPA_ADAPTER_FUNC_SMBUS; |
| 130 | return ret; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Query device for its current operating state. |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 135 | */ |
| 136 | static int cyapa_get_state(struct cyapa *cyapa) |
| 137 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 138 | u8 status[BL_STATUS_SIZE]; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 139 | u8 cmd[32]; |
| 140 | /* The i2c address of gen4 and gen5 trackpad device must be even. */ |
| 141 | bool even_addr = ((cyapa->client->addr & 0x0001) == 0); |
| 142 | bool smbus = false; |
| 143 | int retries = 2; |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 144 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 145 | |
| 146 | cyapa->state = CYAPA_STATE_NO_DEVICE; |
| 147 | |
| 148 | /* |
| 149 | * Get trackpad status by reading 3 registers starting from 0. |
| 150 | * If the device is in the bootloader, this will be BL_HEAD. |
| 151 | * If the device is in operation mode, this will be the DATA regs. |
| 152 | * |
| 153 | */ |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 154 | error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE, |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 155 | status); |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * On smbus systems in OP mode, the i2c_reg_read will fail with |
| 159 | * -ETIMEDOUT. In this case, try again using the smbus equivalent |
| 160 | * command. This should return a BL_HEAD indicating CYAPA_STATE_OP. |
| 161 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 162 | if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) { |
| 163 | if (!even_addr) |
| 164 | error = cyapa_read_block(cyapa, |
| 165 | CYAPA_CMD_BL_STATUS, status); |
| 166 | smbus = true; |
| 167 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 168 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 169 | if (error != BL_STATUS_SIZE) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 170 | goto error; |
| 171 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 172 | /* |
| 173 | * Detect trackpad protocol based on characteristic registers and bits. |
| 174 | */ |
| 175 | do { |
| 176 | cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS]; |
| 177 | cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS]; |
| 178 | cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR]; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 179 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 180 | if (cyapa->gen == CYAPA_GEN_UNKNOWN || |
| 181 | cyapa->gen == CYAPA_GEN3) { |
| 182 | error = cyapa_gen3_ops.state_parse(cyapa, |
| 183 | status, BL_STATUS_SIZE); |
| 184 | if (!error) |
| 185 | goto out_detected; |
| 186 | } |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 187 | if ((cyapa->gen == CYAPA_GEN_UNKNOWN || |
| 188 | cyapa->gen == CYAPA_GEN5) && |
| 189 | !smbus && even_addr) { |
| 190 | error = cyapa_gen5_ops.state_parse(cyapa, |
| 191 | status, BL_STATUS_SIZE); |
| 192 | if (!error) |
| 193 | goto out_detected; |
| 194 | } |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Write 0x00 0x00 to trackpad device to force update its |
| 198 | * status, then redo the detection again. |
| 199 | */ |
| 200 | if (!smbus) { |
| 201 | cmd[0] = 0x00; |
| 202 | cmd[1] = 0x00; |
| 203 | error = cyapa_i2c_write(cyapa, 0, 2, cmd); |
| 204 | if (error) |
| 205 | goto error; |
| 206 | |
| 207 | msleep(50); |
| 208 | |
| 209 | error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET, |
| 210 | BL_STATUS_SIZE, status); |
| 211 | if (error) |
| 212 | goto error; |
| 213 | } |
| 214 | } while (--retries > 0 && !smbus); |
| 215 | |
| 216 | goto error; |
| 217 | |
| 218 | out_detected: |
| 219 | if (cyapa->state <= CYAPA_STATE_BL_BUSY) |
| 220 | return -EAGAIN; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 221 | return 0; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 222 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 223 | error: |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 224 | return (error < 0) ? error : -EAGAIN; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Poll device for its status in a loop, waiting up to timeout for a response. |
| 229 | * |
| 230 | * When the device switches state, it usually takes ~300 ms. |
| 231 | * However, when running a new firmware image, the device must calibrate its |
| 232 | * sensors, which can take as long as 2 seconds. |
| 233 | * |
| 234 | * Note: The timeout has granularity of the polling rate, which is 100 ms. |
| 235 | * |
| 236 | * Returns: |
| 237 | * 0 when the device eventually responds with a valid non-busy state. |
| 238 | * -ETIMEDOUT if device never responds (too many -EAGAIN) |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 239 | * -EAGAIN if bootload is busy, or unknown state. |
| 240 | * < 0 other errors |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 241 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 242 | int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 243 | { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 244 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 245 | int tries = timeout / 100; |
| 246 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 247 | do { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 248 | error = cyapa_get_state(cyapa); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 249 | if (!error && cyapa->state > CYAPA_STATE_BL_BUSY) |
| 250 | return 0; |
| 251 | |
| 252 | msleep(100); |
| 253 | } while (tries--); |
| 254 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 255 | return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 258 | /* |
| 259 | * Check if device is operational. |
| 260 | * |
| 261 | * An operational device is responding, has exited bootloader, and has |
| 262 | * firmware supported by this driver. |
| 263 | * |
| 264 | * Returns: |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 265 | * -ENODEV no device |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 266 | * -EBUSY no device or in bootloader |
| 267 | * -EIO failure while reading from device |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 268 | * -ETIMEDOUT timeout failure for bus idle or bus no response |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 269 | * -EAGAIN device is still in bootloader |
| 270 | * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware |
| 271 | * -EINVAL device is in operational mode, but not supported by this driver |
| 272 | * 0 device is supported |
| 273 | */ |
| 274 | static int cyapa_check_is_operational(struct cyapa *cyapa) |
| 275 | { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 276 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 277 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 278 | error = cyapa_poll_state(cyapa, 4000); |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 279 | if (error) |
| 280 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 281 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 282 | switch (cyapa->gen) { |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 283 | case CYAPA_GEN5: |
| 284 | cyapa->ops = &cyapa_gen5_ops; |
| 285 | break; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 286 | case CYAPA_GEN3: |
| 287 | cyapa->ops = &cyapa_gen3_ops; |
| 288 | break; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 289 | default: |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 290 | return -ENODEV; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 291 | } |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 292 | |
| 293 | error = cyapa->ops->operational_check(cyapa); |
| 294 | if (!error && cyapa_is_operational_mode(cyapa)) |
| 295 | cyapa->operational = true; |
| 296 | else |
| 297 | cyapa->operational = false; |
| 298 | |
| 299 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Returns 0 on device detected, negative errno on no device detected. |
| 305 | * And when the device is detected and opertaional, it will be reset to |
| 306 | * full power active mode automatically. |
| 307 | */ |
| 308 | static int cyapa_detect(struct cyapa *cyapa) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 309 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 310 | struct device *dev = &cyapa->client->dev; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 311 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 312 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 313 | error = cyapa_check_is_operational(cyapa); |
| 314 | if (error) { |
| 315 | if (error != -ETIMEDOUT && error != -ENODEV && |
| 316 | cyapa_is_bootloader_mode(cyapa)) { |
| 317 | dev_warn(dev, "device detected but not operational\n"); |
| 318 | return 0; |
| 319 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 320 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 321 | dev_err(dev, "no device detected: %d\n", error); |
| 322 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 325 | return 0; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 328 | static int cyapa_open(struct input_dev *input) |
| 329 | { |
| 330 | struct cyapa *cyapa = input_get_drvdata(input); |
| 331 | struct i2c_client *client = cyapa->client; |
| 332 | int error; |
| 333 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 334 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 335 | if (error) |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 336 | return error; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 337 | |
| 338 | if (cyapa->operational) { |
| 339 | /* |
| 340 | * though failed to set active power mode, |
| 341 | * but still may be able to work in lower scan rate |
| 342 | * when in operational mode. |
| 343 | */ |
| 344 | error = cyapa->ops->set_power_mode(cyapa, |
| 345 | PWR_MODE_FULL_ACTIVE, 0); |
| 346 | if (error) { |
| 347 | dev_warn(&client->dev, |
| 348 | "set active power failed: %d\n", error); |
| 349 | goto out; |
| 350 | } |
| 351 | } else { |
| 352 | error = cyapa_reinitialize(cyapa); |
| 353 | if (error || !cyapa->operational) { |
| 354 | error = error ? error : -EAGAIN; |
| 355 | goto out; |
| 356 | } |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | enable_irq(client->irq); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 360 | out: |
| 361 | mutex_unlock(&cyapa->state_sync_lock); |
| 362 | return error; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void cyapa_close(struct input_dev *input) |
| 366 | { |
| 367 | struct cyapa *cyapa = input_get_drvdata(input); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 368 | struct i2c_client *client = cyapa->client; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 369 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 370 | mutex_lock(&cyapa->state_sync_lock); |
| 371 | |
| 372 | disable_irq(client->irq); |
| 373 | if (cyapa->operational) |
| 374 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 375 | |
| 376 | mutex_unlock(&cyapa->state_sync_lock); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 379 | static int cyapa_create_input_dev(struct cyapa *cyapa) |
| 380 | { |
| 381 | struct device *dev = &cyapa->client->dev; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 382 | struct input_dev *input; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 383 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 384 | |
| 385 | if (!cyapa->physical_size_x || !cyapa->physical_size_y) |
| 386 | return -EINVAL; |
| 387 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 388 | input = devm_input_allocate_device(dev); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 389 | if (!input) { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 390 | dev_err(dev, "failed to allocate memory for input device.\n"); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | input->name = CYAPA_NAME; |
| 395 | input->phys = cyapa->phys; |
| 396 | input->id.bustype = BUS_I2C; |
| 397 | input->id.version = 1; |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 398 | input->id.product = 0; /* Means any product in eventcomm. */ |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 399 | input->dev.parent = &cyapa->client->dev; |
| 400 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 401 | input->open = cyapa_open; |
| 402 | input->close = cyapa_close; |
| 403 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 404 | input_set_drvdata(input, cyapa); |
| 405 | |
| 406 | __set_bit(EV_ABS, input->evbit); |
| 407 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 408 | /* Finger position */ |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 409 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0, |
| 410 | 0); |
| 411 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0, |
| 412 | 0); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 413 | input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0); |
| 414 | if (cyapa->gen > CYAPA_GEN3) { |
| 415 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); |
| 416 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); |
| 417 | /* |
| 418 | * Orientation is the angle between the vertical axis and |
| 419 | * the major axis of the contact ellipse. |
| 420 | * The range is -127 to 127. |
| 421 | * the positive direction is clockwise form the vertical axis. |
| 422 | * If the ellipse of contact degenerates into a circle, |
| 423 | * orientation is reported as 0. |
| 424 | * |
| 425 | * Also, for Gen5 trackpad the accurate of this orientation |
| 426 | * value is value + (-30 ~ 30). |
| 427 | */ |
| 428 | input_set_abs_params(input, ABS_MT_ORIENTATION, |
| 429 | -127, 127, 0, 0); |
| 430 | } |
| 431 | if (cyapa->gen >= CYAPA_GEN5) { |
| 432 | input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); |
| 433 | input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0); |
| 434 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 435 | |
| 436 | input_abs_set_res(input, ABS_MT_POSITION_X, |
| 437 | cyapa->max_abs_x / cyapa->physical_size_x); |
| 438 | input_abs_set_res(input, ABS_MT_POSITION_Y, |
| 439 | cyapa->max_abs_y / cyapa->physical_size_y); |
| 440 | |
| 441 | if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK) |
| 442 | __set_bit(BTN_LEFT, input->keybit); |
| 443 | if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK) |
| 444 | __set_bit(BTN_MIDDLE, input->keybit); |
| 445 | if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK) |
| 446 | __set_bit(BTN_RIGHT, input->keybit); |
| 447 | |
| 448 | if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK) |
| 449 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); |
| 450 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 451 | /* Handle pointer emulation and unused slots in core */ |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 452 | error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS, |
| 453 | INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); |
| 454 | if (error) { |
| 455 | dev_err(dev, "failed to initialize MT slots: %d\n", error); |
| 456 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 459 | /* Register the device in input subsystem */ |
| 460 | error = input_register_device(input); |
| 461 | if (error) { |
| 462 | dev_err(dev, "failed to register input device: %d\n", error); |
| 463 | return error; |
| 464 | } |
| 465 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 466 | cyapa->input = input; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 467 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 470 | /* |
| 471 | * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time |
| 472 | * |
| 473 | * These are helper functions that convert to and from integer idle |
| 474 | * times and register settings to write to the PowerMode register. |
| 475 | * The trackpad supports between 20ms to 1000ms scan intervals. |
| 476 | * The time will be increased in increments of 10ms from 20ms to 100ms. |
| 477 | * From 100ms to 1000ms, time will be increased in increments of 20ms. |
| 478 | * |
| 479 | * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is: |
| 480 | * Idle_Command = Idle Time / 10; |
| 481 | * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is: |
| 482 | * Idle_Command = Idle Time / 20 + 5; |
| 483 | */ |
| 484 | u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time) |
| 485 | { |
| 486 | u16 encoded_time; |
| 487 | |
| 488 | sleep_time = clamp_val(sleep_time, 20, 1000); |
| 489 | encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5; |
| 490 | return (encoded_time << 2) & PWR_MODE_MASK; |
| 491 | } |
| 492 | |
| 493 | u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) |
| 494 | { |
| 495 | u8 encoded_time = pwr_mode >> 2; |
| 496 | |
| 497 | return (encoded_time < 10) ? encoded_time * 10 |
| 498 | : (encoded_time - 5) * 20; |
| 499 | } |
| 500 | |
| 501 | /* 0 on driver initialize and detected successfully, negative on failure. */ |
| 502 | static int cyapa_initialize(struct cyapa *cyapa) |
| 503 | { |
| 504 | int error = 0; |
| 505 | |
| 506 | cyapa->state = CYAPA_STATE_NO_DEVICE; |
| 507 | cyapa->gen = CYAPA_GEN_UNKNOWN; |
| 508 | mutex_init(&cyapa->state_sync_lock); |
| 509 | |
| 510 | /* |
| 511 | * Set to hard code default, they will be updated with trackpad set |
| 512 | * default values after probe and initialized. |
| 513 | */ |
| 514 | cyapa->suspend_power_mode = PWR_MODE_SLEEP; |
| 515 | cyapa->suspend_sleep_time = |
| 516 | cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode); |
| 517 | |
| 518 | /* ops.initialize() is aimed to prepare for module communications. */ |
| 519 | error = cyapa_gen3_ops.initialize(cyapa); |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 520 | if (!error) |
| 521 | error = cyapa_gen5_ops.initialize(cyapa); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 522 | if (error) |
| 523 | return error; |
| 524 | |
| 525 | error = cyapa_detect(cyapa); |
| 526 | if (error) |
| 527 | return error; |
| 528 | |
| 529 | /* Power down the device until we need it. */ |
| 530 | if (cyapa->operational) |
| 531 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int cyapa_reinitialize(struct cyapa *cyapa) |
| 537 | { |
| 538 | struct device *dev = &cyapa->client->dev; |
| 539 | struct input_dev *input = cyapa->input; |
| 540 | int error; |
| 541 | |
| 542 | /* Avoid command failures when TP was in OFF state. */ |
| 543 | if (cyapa->operational) |
| 544 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0); |
| 545 | |
| 546 | error = cyapa_detect(cyapa); |
| 547 | if (error) |
| 548 | goto out; |
| 549 | |
| 550 | if (!input && cyapa->operational) { |
| 551 | error = cyapa_create_input_dev(cyapa); |
| 552 | if (error) { |
| 553 | dev_err(dev, "create input_dev instance failed: %d\n", |
| 554 | error); |
| 555 | goto out; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | out: |
| 560 | if (!input || !input->users) { |
| 561 | /* Reset to power OFF state to save power when no user open. */ |
| 562 | if (cyapa->operational) |
| 563 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 564 | } |
| 565 | |
| 566 | return error; |
| 567 | } |
| 568 | |
| 569 | static irqreturn_t cyapa_irq(int irq, void *dev_id) |
| 570 | { |
| 571 | struct cyapa *cyapa = dev_id; |
| 572 | struct device *dev = &cyapa->client->dev; |
| 573 | |
| 574 | if (device_may_wakeup(dev)) |
| 575 | pm_wakeup_event(dev, 0); |
| 576 | |
| 577 | /* Interrupt event maybe cuased by host command to trackpad device. */ |
| 578 | if (cyapa->ops->irq_cmd_handler(cyapa)) { |
| 579 | /* |
| 580 | * Interrupt event maybe from trackpad device input reporting. |
| 581 | */ |
| 582 | if (!cyapa->input) { |
| 583 | /* |
| 584 | * Still in probling or in firware image |
| 585 | * udpating or reading. |
| 586 | */ |
| 587 | cyapa->ops->sort_empty_output_data(cyapa, |
| 588 | NULL, NULL, NULL); |
| 589 | goto out; |
| 590 | } |
| 591 | |
| 592 | if (!cyapa->operational || cyapa->ops->irq_handler(cyapa)) { |
| 593 | if (!mutex_trylock(&cyapa->state_sync_lock)) { |
| 594 | cyapa->ops->sort_empty_output_data(cyapa, |
| 595 | NULL, NULL, NULL); |
| 596 | goto out; |
| 597 | } |
| 598 | cyapa_reinitialize(cyapa); |
| 599 | mutex_unlock(&cyapa->state_sync_lock); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | out: |
| 604 | return IRQ_HANDLED; |
| 605 | } |
| 606 | |
Dudley Du | 22e7db8 | 2015-01-17 18:56:18 -0800 | [diff] [blame^] | 607 | /* |
| 608 | ************************************************************** |
| 609 | * sysfs interface |
| 610 | ************************************************************** |
| 611 | */ |
| 612 | #ifdef CONFIG_PM_SLEEP |
| 613 | static ssize_t cyapa_show_suspend_scanrate(struct device *dev, |
| 614 | struct device_attribute *attr, |
| 615 | char *buf) |
| 616 | { |
| 617 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 618 | u8 pwr_cmd = cyapa->suspend_power_mode; |
| 619 | u16 sleep_time; |
| 620 | int len; |
| 621 | int error; |
| 622 | |
| 623 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 624 | if (error) |
| 625 | return error; |
| 626 | |
| 627 | pwr_cmd = cyapa->suspend_power_mode; |
| 628 | sleep_time = cyapa->suspend_sleep_time; |
| 629 | |
| 630 | mutex_unlock(&cyapa->state_sync_lock); |
| 631 | |
| 632 | switch (pwr_cmd) { |
| 633 | case PWR_MODE_BTN_ONLY: |
| 634 | len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME); |
| 635 | break; |
| 636 | |
| 637 | case PWR_MODE_OFF: |
| 638 | len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME); |
| 639 | break; |
| 640 | |
| 641 | default: |
| 642 | len = scnprintf(buf, PAGE_SIZE, "%u\n", |
| 643 | cyapa->gen == CYAPA_GEN3 ? |
| 644 | cyapa_pwr_cmd_to_sleep_time(pwr_cmd) : |
| 645 | sleep_time); |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | return len; |
| 650 | } |
| 651 | |
| 652 | static ssize_t cyapa_update_suspend_scanrate(struct device *dev, |
| 653 | struct device_attribute *attr, |
| 654 | const char *buf, size_t count) |
| 655 | { |
| 656 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 657 | u16 sleep_time; |
| 658 | int error; |
| 659 | |
| 660 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 661 | if (error) |
| 662 | return error; |
| 663 | |
| 664 | if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) { |
| 665 | cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY; |
| 666 | } else if (sysfs_streq(buf, OFF_MODE_NAME)) { |
| 667 | cyapa->suspend_power_mode = PWR_MODE_OFF; |
| 668 | } else if (!kstrtou16(buf, 10, &sleep_time)) { |
| 669 | cyapa->suspend_sleep_time = max_t(u16, sleep_time, 1000); |
| 670 | cyapa->suspend_power_mode = |
| 671 | cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time); |
| 672 | } else { |
| 673 | count = -EINVAL; |
| 674 | } |
| 675 | |
| 676 | mutex_unlock(&cyapa->state_sync_lock); |
| 677 | |
| 678 | return count; |
| 679 | } |
| 680 | |
| 681 | static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR, |
| 682 | cyapa_show_suspend_scanrate, |
| 683 | cyapa_update_suspend_scanrate); |
| 684 | |
| 685 | static struct attribute *cyapa_power_wakeup_entries[] = { |
| 686 | &dev_attr_suspend_scanrate_ms.attr, |
| 687 | NULL, |
| 688 | }; |
| 689 | |
| 690 | static const struct attribute_group cyapa_power_wakeup_group = { |
| 691 | .name = power_group_name, |
| 692 | .attrs = cyapa_power_wakeup_entries, |
| 693 | }; |
| 694 | |
| 695 | static void cyapa_remove_power_wakeup_group(void *data) |
| 696 | { |
| 697 | struct cyapa *cyapa = data; |
| 698 | |
| 699 | sysfs_unmerge_group(&cyapa->client->dev.kobj, |
| 700 | &cyapa_power_wakeup_group); |
| 701 | } |
| 702 | |
| 703 | static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa) |
| 704 | { |
| 705 | struct i2c_client *client = cyapa->client; |
| 706 | struct device *dev = &client->dev; |
| 707 | int error; |
| 708 | |
| 709 | if (device_can_wakeup(dev)) { |
| 710 | error = sysfs_merge_group(&client->dev.kobj, |
| 711 | &cyapa_power_wakeup_group); |
| 712 | if (error) { |
| 713 | dev_err(dev, "failed to add power wakeup group: %d\n", |
| 714 | error); |
| 715 | return error; |
| 716 | } |
| 717 | |
| 718 | error = devm_add_action(dev, |
| 719 | cyapa_remove_power_wakeup_group, cyapa); |
| 720 | if (error) { |
| 721 | cyapa_remove_power_wakeup_group(cyapa); |
| 722 | dev_err(dev, "failed to add power cleanup action: %d\n", |
| 723 | error); |
| 724 | return error; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | #else |
| 731 | static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa) |
| 732 | { |
| 733 | return 0; |
| 734 | } |
| 735 | #endif /* CONFIG_PM_SLEEP */ |
| 736 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 737 | static int cyapa_probe(struct i2c_client *client, |
| 738 | const struct i2c_device_id *dev_id) |
| 739 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 740 | struct device *dev = &client->dev; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 741 | struct cyapa *cyapa; |
| 742 | u8 adapter_func; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 743 | union i2c_smbus_data dummy; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 744 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 745 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 746 | adapter_func = cyapa_check_adapter_functionality(client); |
| 747 | if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) { |
| 748 | dev_err(dev, "not a supported I2C/SMBus adapter\n"); |
| 749 | return -EIO; |
| 750 | } |
| 751 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 752 | /* Make sure there is something at this address */ |
| 753 | if (i2c_smbus_xfer(client->adapter, client->addr, 0, |
| 754 | I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) |
| 755 | return -ENODEV; |
| 756 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 757 | cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL); |
| 758 | if (!cyapa) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 759 | return -ENOMEM; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 760 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 761 | /* i2c isn't supported, use smbus */ |
| 762 | if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS) |
| 763 | cyapa->smbus = true; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 764 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 765 | cyapa->client = client; |
| 766 | i2c_set_clientdata(client, cyapa); |
| 767 | sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr, |
| 768 | client->addr); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 769 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 770 | error = cyapa_initialize(cyapa); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 771 | if (error) { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 772 | dev_err(dev, "failed to detect and initialize tp device.\n"); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 773 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Dudley Du | 22e7db8 | 2015-01-17 18:56:18 -0800 | [diff] [blame^] | 776 | error = cyapa_prepare_wakeup_controls(cyapa); |
| 777 | if (error) { |
| 778 | dev_err(dev, "failed to prepare wakeup controls: %d\n", error); |
| 779 | return error; |
| 780 | } |
| 781 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 782 | error = devm_request_threaded_irq(dev, client->irq, |
| 783 | NULL, cyapa_irq, |
| 784 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 785 | "cyapa", cyapa); |
| 786 | if (error) { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 787 | dev_err(dev, "failed to request threaded irq: %d\n", error); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 788 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 791 | /* Disable IRQ until the device is opened */ |
| 792 | disable_irq(client->irq); |
| 793 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 794 | /* |
| 795 | * Register the device in the input subsystem when it's operational. |
| 796 | * Otherwise, keep in this driver, so it can be be recovered or updated |
| 797 | * through the sysfs mode and update_fw interfaces by user or apps. |
| 798 | */ |
| 799 | if (cyapa->operational) { |
| 800 | error = cyapa_create_input_dev(cyapa); |
| 801 | if (error) { |
| 802 | dev_err(dev, "create input_dev instance failed: %d\n", |
| 803 | error); |
| 804 | return error; |
| 805 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Jingoo Han | 572081a | 2014-11-02 00:03:37 -0700 | [diff] [blame] | 811 | static int __maybe_unused cyapa_suspend(struct device *dev) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 812 | { |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 813 | struct i2c_client *client = to_i2c_client(dev); |
| 814 | struct cyapa *cyapa = i2c_get_clientdata(client); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 815 | u8 power_mode; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 816 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 817 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 818 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 819 | if (error) |
| 820 | return error; |
| 821 | |
| 822 | disable_irq(client->irq); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | * Set trackpad device to idle mode if wakeup is allowed, |
| 826 | * otherwise turn off. |
| 827 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 828 | if (cyapa->operational) { |
| 829 | power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode |
| 830 | : PWR_MODE_OFF; |
| 831 | error = cyapa->ops->set_power_mode(cyapa, power_mode, |
| 832 | cyapa->suspend_sleep_time); |
| 833 | if (error) |
| 834 | dev_err(dev, "suspend set power mode failed: %d\n", |
| 835 | error); |
| 836 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 837 | |
| 838 | if (device_may_wakeup(dev)) |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 839 | cyapa->irq_wake = (enable_irq_wake(client->irq) == 0); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 840 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 841 | mutex_unlock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
Jingoo Han | 572081a | 2014-11-02 00:03:37 -0700 | [diff] [blame] | 845 | static int __maybe_unused cyapa_resume(struct device *dev) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 846 | { |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 847 | struct i2c_client *client = to_i2c_client(dev); |
| 848 | struct cyapa *cyapa = i2c_get_clientdata(client); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 849 | int error; |
| 850 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 851 | mutex_lock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 852 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 853 | if (device_may_wakeup(dev) && cyapa->irq_wake) { |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 854 | disable_irq_wake(client->irq); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 855 | cyapa->irq_wake = false; |
| 856 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 857 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 858 | error = cyapa_reinitialize(cyapa); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 859 | if (error) |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 860 | dev_warn(dev, "failed to reinitialize TP device: %d\n", error); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 861 | |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 862 | enable_irq(client->irq); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 863 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 864 | mutex_unlock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 865 | return 0; |
| 866 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 867 | |
| 868 | static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume); |
| 869 | |
| 870 | static const struct i2c_device_id cyapa_id_table[] = { |
| 871 | { "cyapa", 0 }, |
| 872 | { }, |
| 873 | }; |
| 874 | MODULE_DEVICE_TABLE(i2c, cyapa_id_table); |
| 875 | |
| 876 | static struct i2c_driver cyapa_driver = { |
| 877 | .driver = { |
| 878 | .name = "cyapa", |
| 879 | .owner = THIS_MODULE, |
| 880 | .pm = &cyapa_pm_ops, |
| 881 | }, |
| 882 | |
| 883 | .probe = cyapa_probe, |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 884 | .id_table = cyapa_id_table, |
| 885 | }; |
| 886 | |
| 887 | module_i2c_driver(cyapa_driver); |
| 888 | |
| 889 | MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver"); |
| 890 | MODULE_AUTHOR("Dudley Du <dudl@cypress.com>"); |
| 891 | MODULE_LICENSE("GPL"); |