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> |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 27 | #include "cyapa.h" |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 28 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 29 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 30 | #define CYAPA_ADAPTER_FUNC_NONE 0 |
| 31 | #define CYAPA_ADAPTER_FUNC_I2C 1 |
| 32 | #define CYAPA_ADAPTER_FUNC_SMBUS 2 |
| 33 | #define CYAPA_ADAPTER_FUNC_BOTH 3 |
| 34 | |
Dudley Du | c806b0b | 2015-01-17 22:07:12 -0800 | [diff] [blame^] | 35 | #define CYAPA_FW_NAME "cyapa.bin" |
| 36 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 37 | const char product_id[] = "CYTRA"; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 38 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 39 | static int cyapa_reinitialize(struct cyapa *cyapa); |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 40 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 41 | static inline bool cyapa_is_bootloader_mode(struct cyapa *cyapa) |
| 42 | { |
| 43 | if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL) |
| 44 | return true; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 45 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 46 | if (cyapa->gen == CYAPA_GEN3 && |
| 47 | cyapa->state >= CYAPA_STATE_BL_BUSY && |
| 48 | cyapa->state <= CYAPA_STATE_BL_ACTIVE) |
| 49 | return true; |
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 | return false; |
| 52 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 53 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 54 | static inline bool cyapa_is_operational_mode(struct cyapa *cyapa) |
| 55 | { |
| 56 | if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP) |
| 57 | return true; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 58 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 59 | if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP) |
| 60 | return true; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 61 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 64 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 65 | /* Returns 0 on success, else negative errno on failure. */ |
| 66 | 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] | 67 | u8 *values) |
| 68 | { |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 69 | struct i2c_client *client = cyapa->client; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 70 | struct i2c_msg msgs[] = { |
| 71 | { |
| 72 | .addr = client->addr, |
| 73 | .flags = 0, |
| 74 | .len = 1, |
| 75 | .buf = ®, |
| 76 | }, |
| 77 | { |
| 78 | .addr = client->addr, |
| 79 | .flags = I2C_M_RD, |
| 80 | .len = len, |
| 81 | .buf = values, |
| 82 | }, |
| 83 | }; |
| 84 | int ret; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 85 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 86 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
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 | if (ret != ARRAY_SIZE(msgs)) |
| 89 | return ret < 0 ? ret : -EIO; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 90 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 91 | return 0; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 94 | /** |
| 95 | * cyapa_i2c_write - Execute i2c block data write operation |
| 96 | * @cyapa: Handle to this driver |
| 97 | * @ret: Offset of the data to written in the register map |
| 98 | * @len: number of bytes to write |
| 99 | * @values: Data to be written |
| 100 | * |
| 101 | * Return negative errno code on error; return zero when success. |
| 102 | */ |
| 103 | static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg, |
| 104 | size_t len, const void *values) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 105 | { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 106 | struct i2c_client *client = cyapa->client; |
| 107 | char buf[32]; |
| 108 | int ret; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 109 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 110 | if (len > sizeof(buf) - 1) |
| 111 | return -ENOMEM; |
| 112 | |
| 113 | buf[0] = reg; |
| 114 | memcpy(&buf[1], values, len); |
| 115 | |
| 116 | ret = i2c_master_send(client, buf, len + 1); |
| 117 | if (ret != len + 1) |
| 118 | return ret < 0 ? ret : -EIO; |
| 119 | |
| 120 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 123 | static u8 cyapa_check_adapter_functionality(struct i2c_client *client) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 124 | { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 125 | u8 ret = CYAPA_ADAPTER_FUNC_NONE; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 126 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 127 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 128 | ret |= CYAPA_ADAPTER_FUNC_I2C; |
| 129 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 130 | I2C_FUNC_SMBUS_BLOCK_DATA | |
| 131 | I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 132 | ret |= CYAPA_ADAPTER_FUNC_SMBUS; |
| 133 | return ret; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Query device for its current operating state. |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 138 | */ |
| 139 | static int cyapa_get_state(struct cyapa *cyapa) |
| 140 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 141 | u8 status[BL_STATUS_SIZE]; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 142 | u8 cmd[32]; |
| 143 | /* The i2c address of gen4 and gen5 trackpad device must be even. */ |
| 144 | bool even_addr = ((cyapa->client->addr & 0x0001) == 0); |
| 145 | bool smbus = false; |
| 146 | int retries = 2; |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 147 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 148 | |
| 149 | cyapa->state = CYAPA_STATE_NO_DEVICE; |
| 150 | |
| 151 | /* |
| 152 | * Get trackpad status by reading 3 registers starting from 0. |
| 153 | * If the device is in the bootloader, this will be BL_HEAD. |
| 154 | * If the device is in operation mode, this will be the DATA regs. |
| 155 | * |
| 156 | */ |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 157 | 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] | 158 | status); |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * On smbus systems in OP mode, the i2c_reg_read will fail with |
| 162 | * -ETIMEDOUT. In this case, try again using the smbus equivalent |
| 163 | * command. This should return a BL_HEAD indicating CYAPA_STATE_OP. |
| 164 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 165 | if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) { |
| 166 | if (!even_addr) |
| 167 | error = cyapa_read_block(cyapa, |
| 168 | CYAPA_CMD_BL_STATUS, status); |
| 169 | smbus = true; |
| 170 | } |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 171 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 172 | if (error != BL_STATUS_SIZE) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 173 | goto error; |
| 174 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 175 | /* |
| 176 | * Detect trackpad protocol based on characteristic registers and bits. |
| 177 | */ |
| 178 | do { |
| 179 | cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS]; |
| 180 | cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS]; |
| 181 | cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR]; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 182 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 183 | if (cyapa->gen == CYAPA_GEN_UNKNOWN || |
| 184 | cyapa->gen == CYAPA_GEN3) { |
| 185 | error = cyapa_gen3_ops.state_parse(cyapa, |
| 186 | status, BL_STATUS_SIZE); |
| 187 | if (!error) |
| 188 | goto out_detected; |
| 189 | } |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 190 | if ((cyapa->gen == CYAPA_GEN_UNKNOWN || |
| 191 | cyapa->gen == CYAPA_GEN5) && |
| 192 | !smbus && even_addr) { |
| 193 | error = cyapa_gen5_ops.state_parse(cyapa, |
| 194 | status, BL_STATUS_SIZE); |
| 195 | if (!error) |
| 196 | goto out_detected; |
| 197 | } |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Write 0x00 0x00 to trackpad device to force update its |
| 201 | * status, then redo the detection again. |
| 202 | */ |
| 203 | if (!smbus) { |
| 204 | cmd[0] = 0x00; |
| 205 | cmd[1] = 0x00; |
| 206 | error = cyapa_i2c_write(cyapa, 0, 2, cmd); |
| 207 | if (error) |
| 208 | goto error; |
| 209 | |
| 210 | msleep(50); |
| 211 | |
| 212 | error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET, |
| 213 | BL_STATUS_SIZE, status); |
| 214 | if (error) |
| 215 | goto error; |
| 216 | } |
| 217 | } while (--retries > 0 && !smbus); |
| 218 | |
| 219 | goto error; |
| 220 | |
| 221 | out_detected: |
| 222 | if (cyapa->state <= CYAPA_STATE_BL_BUSY) |
| 223 | return -EAGAIN; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 224 | return 0; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 225 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 226 | error: |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 227 | return (error < 0) ? error : -EAGAIN; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Poll device for its status in a loop, waiting up to timeout for a response. |
| 232 | * |
| 233 | * When the device switches state, it usually takes ~300 ms. |
| 234 | * However, when running a new firmware image, the device must calibrate its |
| 235 | * sensors, which can take as long as 2 seconds. |
| 236 | * |
| 237 | * Note: The timeout has granularity of the polling rate, which is 100 ms. |
| 238 | * |
| 239 | * Returns: |
| 240 | * 0 when the device eventually responds with a valid non-busy state. |
| 241 | * -ETIMEDOUT if device never responds (too many -EAGAIN) |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 242 | * -EAGAIN if bootload is busy, or unknown state. |
| 243 | * < 0 other errors |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 244 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 245 | int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 246 | { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 247 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 248 | int tries = timeout / 100; |
| 249 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 250 | do { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 251 | error = cyapa_get_state(cyapa); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 252 | if (!error && cyapa->state > CYAPA_STATE_BL_BUSY) |
| 253 | return 0; |
| 254 | |
| 255 | msleep(100); |
| 256 | } while (tries--); |
| 257 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 258 | return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 261 | /* |
| 262 | * Check if device is operational. |
| 263 | * |
| 264 | * An operational device is responding, has exited bootloader, and has |
| 265 | * firmware supported by this driver. |
| 266 | * |
| 267 | * Returns: |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 268 | * -ENODEV no device |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 269 | * -EBUSY no device or in bootloader |
| 270 | * -EIO failure while reading from device |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 271 | * -ETIMEDOUT timeout failure for bus idle or bus no response |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 272 | * -EAGAIN device is still in bootloader |
| 273 | * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware |
| 274 | * -EINVAL device is in operational mode, but not supported by this driver |
| 275 | * 0 device is supported |
| 276 | */ |
| 277 | static int cyapa_check_is_operational(struct cyapa *cyapa) |
| 278 | { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 279 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 280 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 281 | error = cyapa_poll_state(cyapa, 4000); |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 282 | if (error) |
| 283 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 284 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 285 | switch (cyapa->gen) { |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 286 | case CYAPA_GEN5: |
| 287 | cyapa->ops = &cyapa_gen5_ops; |
| 288 | break; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 289 | case CYAPA_GEN3: |
| 290 | cyapa->ops = &cyapa_gen3_ops; |
| 291 | break; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 292 | default: |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 293 | return -ENODEV; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 294 | } |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 295 | |
| 296 | error = cyapa->ops->operational_check(cyapa); |
| 297 | if (!error && cyapa_is_operational_mode(cyapa)) |
| 298 | cyapa->operational = true; |
| 299 | else |
| 300 | cyapa->operational = false; |
| 301 | |
| 302 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Returns 0 on device detected, negative errno on no device detected. |
| 308 | * And when the device is detected and opertaional, it will be reset to |
| 309 | * full power active mode automatically. |
| 310 | */ |
| 311 | static int cyapa_detect(struct cyapa *cyapa) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 312 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 313 | struct device *dev = &cyapa->client->dev; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 314 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 315 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 316 | error = cyapa_check_is_operational(cyapa); |
| 317 | if (error) { |
| 318 | if (error != -ETIMEDOUT && error != -ENODEV && |
| 319 | cyapa_is_bootloader_mode(cyapa)) { |
| 320 | dev_warn(dev, "device detected but not operational\n"); |
| 321 | return 0; |
| 322 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 323 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 324 | dev_err(dev, "no device detected: %d\n", error); |
| 325 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 328 | return 0; |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 331 | static int cyapa_open(struct input_dev *input) |
| 332 | { |
| 333 | struct cyapa *cyapa = input_get_drvdata(input); |
| 334 | struct i2c_client *client = cyapa->client; |
| 335 | int error; |
| 336 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 337 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 338 | if (error) |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 339 | return error; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 340 | |
| 341 | if (cyapa->operational) { |
| 342 | /* |
| 343 | * though failed to set active power mode, |
| 344 | * but still may be able to work in lower scan rate |
| 345 | * when in operational mode. |
| 346 | */ |
| 347 | error = cyapa->ops->set_power_mode(cyapa, |
| 348 | PWR_MODE_FULL_ACTIVE, 0); |
| 349 | if (error) { |
| 350 | dev_warn(&client->dev, |
| 351 | "set active power failed: %d\n", error); |
| 352 | goto out; |
| 353 | } |
| 354 | } else { |
| 355 | error = cyapa_reinitialize(cyapa); |
| 356 | if (error || !cyapa->operational) { |
| 357 | error = error ? error : -EAGAIN; |
| 358 | goto out; |
| 359 | } |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | enable_irq(client->irq); |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 363 | if (!pm_runtime_enabled(&client->dev)) { |
| 364 | pm_runtime_set_active(&client->dev); |
| 365 | pm_runtime_enable(&client->dev); |
| 366 | } |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 367 | out: |
| 368 | mutex_unlock(&cyapa->state_sync_lock); |
| 369 | return error; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void cyapa_close(struct input_dev *input) |
| 373 | { |
| 374 | struct cyapa *cyapa = input_get_drvdata(input); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 375 | struct i2c_client *client = cyapa->client; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 376 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 377 | mutex_lock(&cyapa->state_sync_lock); |
| 378 | |
| 379 | disable_irq(client->irq); |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 380 | if (pm_runtime_enabled(&client->dev)) |
| 381 | pm_runtime_disable(&client->dev); |
| 382 | pm_runtime_set_suspended(&client->dev); |
| 383 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 384 | if (cyapa->operational) |
| 385 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 386 | |
| 387 | mutex_unlock(&cyapa->state_sync_lock); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 390 | static int cyapa_create_input_dev(struct cyapa *cyapa) |
| 391 | { |
| 392 | struct device *dev = &cyapa->client->dev; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 393 | struct input_dev *input; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 394 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 395 | |
| 396 | if (!cyapa->physical_size_x || !cyapa->physical_size_y) |
| 397 | return -EINVAL; |
| 398 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 399 | input = devm_input_allocate_device(dev); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 400 | if (!input) { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 401 | dev_err(dev, "failed to allocate memory for input device.\n"); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
| 405 | input->name = CYAPA_NAME; |
| 406 | input->phys = cyapa->phys; |
| 407 | input->id.bustype = BUS_I2C; |
| 408 | input->id.version = 1; |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 409 | input->id.product = 0; /* Means any product in eventcomm. */ |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 410 | input->dev.parent = &cyapa->client->dev; |
| 411 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 412 | input->open = cyapa_open; |
| 413 | input->close = cyapa_close; |
| 414 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 415 | input_set_drvdata(input, cyapa); |
| 416 | |
| 417 | __set_bit(EV_ABS, input->evbit); |
| 418 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 419 | /* Finger position */ |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 420 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0, |
| 421 | 0); |
| 422 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0, |
| 423 | 0); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 424 | input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0); |
| 425 | if (cyapa->gen > CYAPA_GEN3) { |
| 426 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); |
| 427 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); |
| 428 | /* |
| 429 | * Orientation is the angle between the vertical axis and |
| 430 | * the major axis of the contact ellipse. |
| 431 | * The range is -127 to 127. |
| 432 | * the positive direction is clockwise form the vertical axis. |
| 433 | * If the ellipse of contact degenerates into a circle, |
| 434 | * orientation is reported as 0. |
| 435 | * |
| 436 | * Also, for Gen5 trackpad the accurate of this orientation |
| 437 | * value is value + (-30 ~ 30). |
| 438 | */ |
| 439 | input_set_abs_params(input, ABS_MT_ORIENTATION, |
| 440 | -127, 127, 0, 0); |
| 441 | } |
| 442 | if (cyapa->gen >= CYAPA_GEN5) { |
| 443 | input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); |
| 444 | input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0); |
| 445 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 446 | |
| 447 | input_abs_set_res(input, ABS_MT_POSITION_X, |
| 448 | cyapa->max_abs_x / cyapa->physical_size_x); |
| 449 | input_abs_set_res(input, ABS_MT_POSITION_Y, |
| 450 | cyapa->max_abs_y / cyapa->physical_size_y); |
| 451 | |
| 452 | if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK) |
| 453 | __set_bit(BTN_LEFT, input->keybit); |
| 454 | if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK) |
| 455 | __set_bit(BTN_MIDDLE, input->keybit); |
| 456 | if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK) |
| 457 | __set_bit(BTN_RIGHT, input->keybit); |
| 458 | |
| 459 | if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK) |
| 460 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); |
| 461 | |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 462 | /* Handle pointer emulation and unused slots in core */ |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 463 | error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS, |
| 464 | INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); |
| 465 | if (error) { |
| 466 | dev_err(dev, "failed to initialize MT slots: %d\n", error); |
| 467 | return error; |
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 | /* Register the device in input subsystem */ |
| 471 | error = input_register_device(input); |
| 472 | if (error) { |
| 473 | dev_err(dev, "failed to register input device: %d\n", error); |
| 474 | return error; |
| 475 | } |
| 476 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 477 | cyapa->input = input; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 478 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Dudley Du | c806b0b | 2015-01-17 22:07:12 -0800 | [diff] [blame^] | 481 | static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa) |
| 482 | { |
| 483 | struct input_dev *input = cyapa->input; |
| 484 | |
| 485 | if (!input || !input->users) { |
| 486 | /* |
| 487 | * When input is NULL, TP must be in deep sleep mode. |
| 488 | * In this mode, later non-power I2C command will always failed |
| 489 | * if not bring it out of deep sleep mode firstly, |
| 490 | * so must command TP to active mode here. |
| 491 | */ |
| 492 | if (!input || cyapa->operational) |
| 493 | cyapa->ops->set_power_mode(cyapa, |
| 494 | PWR_MODE_FULL_ACTIVE, 0); |
| 495 | /* Gen3 always using polling mode for command. */ |
| 496 | if (cyapa->gen >= CYAPA_GEN5) |
| 497 | enable_irq(cyapa->client->irq); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa) |
| 502 | { |
| 503 | struct input_dev *input = cyapa->input; |
| 504 | |
| 505 | if (!input || !input->users) { |
| 506 | if (cyapa->gen >= CYAPA_GEN5) |
| 507 | disable_irq(cyapa->client->irq); |
| 508 | if (!input || cyapa->operational) |
| 509 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 510 | } |
| 511 | } |
| 512 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 513 | /* |
| 514 | * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time |
| 515 | * |
| 516 | * These are helper functions that convert to and from integer idle |
| 517 | * times and register settings to write to the PowerMode register. |
| 518 | * The trackpad supports between 20ms to 1000ms scan intervals. |
| 519 | * The time will be increased in increments of 10ms from 20ms to 100ms. |
| 520 | * From 100ms to 1000ms, time will be increased in increments of 20ms. |
| 521 | * |
| 522 | * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is: |
| 523 | * Idle_Command = Idle Time / 10; |
| 524 | * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is: |
| 525 | * Idle_Command = Idle Time / 20 + 5; |
| 526 | */ |
| 527 | u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time) |
| 528 | { |
| 529 | u16 encoded_time; |
| 530 | |
| 531 | sleep_time = clamp_val(sleep_time, 20, 1000); |
| 532 | encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5; |
| 533 | return (encoded_time << 2) & PWR_MODE_MASK; |
| 534 | } |
| 535 | |
| 536 | u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) |
| 537 | { |
| 538 | u8 encoded_time = pwr_mode >> 2; |
| 539 | |
| 540 | return (encoded_time < 10) ? encoded_time * 10 |
| 541 | : (encoded_time - 5) * 20; |
| 542 | } |
| 543 | |
| 544 | /* 0 on driver initialize and detected successfully, negative on failure. */ |
| 545 | static int cyapa_initialize(struct cyapa *cyapa) |
| 546 | { |
| 547 | int error = 0; |
| 548 | |
| 549 | cyapa->state = CYAPA_STATE_NO_DEVICE; |
| 550 | cyapa->gen = CYAPA_GEN_UNKNOWN; |
| 551 | mutex_init(&cyapa->state_sync_lock); |
| 552 | |
| 553 | /* |
| 554 | * Set to hard code default, they will be updated with trackpad set |
| 555 | * default values after probe and initialized. |
| 556 | */ |
| 557 | cyapa->suspend_power_mode = PWR_MODE_SLEEP; |
| 558 | cyapa->suspend_sleep_time = |
| 559 | cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode); |
| 560 | |
| 561 | /* ops.initialize() is aimed to prepare for module communications. */ |
| 562 | error = cyapa_gen3_ops.initialize(cyapa); |
Dudley Du | 6972a85 | 2015-01-17 18:49:37 -0800 | [diff] [blame] | 563 | if (!error) |
| 564 | error = cyapa_gen5_ops.initialize(cyapa); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 565 | if (error) |
| 566 | return error; |
| 567 | |
| 568 | error = cyapa_detect(cyapa); |
| 569 | if (error) |
| 570 | return error; |
| 571 | |
| 572 | /* Power down the device until we need it. */ |
| 573 | if (cyapa->operational) |
| 574 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static int cyapa_reinitialize(struct cyapa *cyapa) |
| 580 | { |
| 581 | struct device *dev = &cyapa->client->dev; |
| 582 | struct input_dev *input = cyapa->input; |
| 583 | int error; |
| 584 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 585 | if (pm_runtime_enabled(dev)) |
| 586 | pm_runtime_disable(dev); |
| 587 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 588 | /* Avoid command failures when TP was in OFF state. */ |
| 589 | if (cyapa->operational) |
| 590 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0); |
| 591 | |
| 592 | error = cyapa_detect(cyapa); |
| 593 | if (error) |
| 594 | goto out; |
| 595 | |
| 596 | if (!input && cyapa->operational) { |
| 597 | error = cyapa_create_input_dev(cyapa); |
| 598 | if (error) { |
| 599 | dev_err(dev, "create input_dev instance failed: %d\n", |
| 600 | error); |
| 601 | goto out; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | out: |
| 606 | if (!input || !input->users) { |
| 607 | /* Reset to power OFF state to save power when no user open. */ |
| 608 | if (cyapa->operational) |
| 609 | cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 610 | } else if (!error && cyapa->operational) { |
| 611 | /* |
| 612 | * Make sure only enable runtime PM when device is |
| 613 | * in operational mode and input->users > 0. |
| 614 | */ |
| 615 | pm_runtime_set_active(dev); |
| 616 | pm_runtime_enable(dev); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return error; |
| 620 | } |
| 621 | |
| 622 | static irqreturn_t cyapa_irq(int irq, void *dev_id) |
| 623 | { |
| 624 | struct cyapa *cyapa = dev_id; |
| 625 | struct device *dev = &cyapa->client->dev; |
| 626 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 627 | pm_runtime_get_sync(dev); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 628 | if (device_may_wakeup(dev)) |
| 629 | pm_wakeup_event(dev, 0); |
| 630 | |
| 631 | /* Interrupt event maybe cuased by host command to trackpad device. */ |
| 632 | if (cyapa->ops->irq_cmd_handler(cyapa)) { |
| 633 | /* |
| 634 | * Interrupt event maybe from trackpad device input reporting. |
| 635 | */ |
| 636 | if (!cyapa->input) { |
| 637 | /* |
| 638 | * Still in probling or in firware image |
| 639 | * udpating or reading. |
| 640 | */ |
| 641 | cyapa->ops->sort_empty_output_data(cyapa, |
| 642 | NULL, NULL, NULL); |
| 643 | goto out; |
| 644 | } |
| 645 | |
| 646 | if (!cyapa->operational || cyapa->ops->irq_handler(cyapa)) { |
| 647 | if (!mutex_trylock(&cyapa->state_sync_lock)) { |
| 648 | cyapa->ops->sort_empty_output_data(cyapa, |
| 649 | NULL, NULL, NULL); |
| 650 | goto out; |
| 651 | } |
| 652 | cyapa_reinitialize(cyapa); |
| 653 | mutex_unlock(&cyapa->state_sync_lock); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | out: |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 658 | pm_runtime_mark_last_busy(dev); |
| 659 | pm_runtime_put_sync_autosuspend(dev); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 660 | return IRQ_HANDLED; |
| 661 | } |
| 662 | |
Dudley Du | 22e7db8 | 2015-01-17 18:56:18 -0800 | [diff] [blame] | 663 | /* |
| 664 | ************************************************************** |
| 665 | * sysfs interface |
| 666 | ************************************************************** |
| 667 | */ |
| 668 | #ifdef CONFIG_PM_SLEEP |
| 669 | static ssize_t cyapa_show_suspend_scanrate(struct device *dev, |
| 670 | struct device_attribute *attr, |
| 671 | char *buf) |
| 672 | { |
| 673 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 674 | u8 pwr_cmd = cyapa->suspend_power_mode; |
| 675 | u16 sleep_time; |
| 676 | int len; |
| 677 | int error; |
| 678 | |
| 679 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 680 | if (error) |
| 681 | return error; |
| 682 | |
| 683 | pwr_cmd = cyapa->suspend_power_mode; |
| 684 | sleep_time = cyapa->suspend_sleep_time; |
| 685 | |
| 686 | mutex_unlock(&cyapa->state_sync_lock); |
| 687 | |
| 688 | switch (pwr_cmd) { |
| 689 | case PWR_MODE_BTN_ONLY: |
| 690 | len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME); |
| 691 | break; |
| 692 | |
| 693 | case PWR_MODE_OFF: |
| 694 | len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME); |
| 695 | break; |
| 696 | |
| 697 | default: |
| 698 | len = scnprintf(buf, PAGE_SIZE, "%u\n", |
| 699 | cyapa->gen == CYAPA_GEN3 ? |
| 700 | cyapa_pwr_cmd_to_sleep_time(pwr_cmd) : |
| 701 | sleep_time); |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | return len; |
| 706 | } |
| 707 | |
| 708 | static ssize_t cyapa_update_suspend_scanrate(struct device *dev, |
| 709 | struct device_attribute *attr, |
| 710 | const char *buf, size_t count) |
| 711 | { |
| 712 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 713 | u16 sleep_time; |
| 714 | int error; |
| 715 | |
| 716 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 717 | if (error) |
| 718 | return error; |
| 719 | |
| 720 | if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) { |
| 721 | cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY; |
| 722 | } else if (sysfs_streq(buf, OFF_MODE_NAME)) { |
| 723 | cyapa->suspend_power_mode = PWR_MODE_OFF; |
| 724 | } else if (!kstrtou16(buf, 10, &sleep_time)) { |
| 725 | cyapa->suspend_sleep_time = max_t(u16, sleep_time, 1000); |
| 726 | cyapa->suspend_power_mode = |
| 727 | cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time); |
| 728 | } else { |
| 729 | count = -EINVAL; |
| 730 | } |
| 731 | |
| 732 | mutex_unlock(&cyapa->state_sync_lock); |
| 733 | |
| 734 | return count; |
| 735 | } |
| 736 | |
| 737 | static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR, |
| 738 | cyapa_show_suspend_scanrate, |
| 739 | cyapa_update_suspend_scanrate); |
| 740 | |
| 741 | static struct attribute *cyapa_power_wakeup_entries[] = { |
| 742 | &dev_attr_suspend_scanrate_ms.attr, |
| 743 | NULL, |
| 744 | }; |
| 745 | |
| 746 | static const struct attribute_group cyapa_power_wakeup_group = { |
| 747 | .name = power_group_name, |
| 748 | .attrs = cyapa_power_wakeup_entries, |
| 749 | }; |
| 750 | |
| 751 | static void cyapa_remove_power_wakeup_group(void *data) |
| 752 | { |
| 753 | struct cyapa *cyapa = data; |
| 754 | |
| 755 | sysfs_unmerge_group(&cyapa->client->dev.kobj, |
| 756 | &cyapa_power_wakeup_group); |
| 757 | } |
| 758 | |
| 759 | static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa) |
| 760 | { |
| 761 | struct i2c_client *client = cyapa->client; |
| 762 | struct device *dev = &client->dev; |
| 763 | int error; |
| 764 | |
| 765 | if (device_can_wakeup(dev)) { |
| 766 | error = sysfs_merge_group(&client->dev.kobj, |
| 767 | &cyapa_power_wakeup_group); |
| 768 | if (error) { |
| 769 | dev_err(dev, "failed to add power wakeup group: %d\n", |
| 770 | error); |
| 771 | return error; |
| 772 | } |
| 773 | |
| 774 | error = devm_add_action(dev, |
| 775 | cyapa_remove_power_wakeup_group, cyapa); |
| 776 | if (error) { |
| 777 | cyapa_remove_power_wakeup_group(cyapa); |
| 778 | dev_err(dev, "failed to add power cleanup action: %d\n", |
| 779 | error); |
| 780 | return error; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | #else |
| 787 | static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa) |
| 788 | { |
| 789 | return 0; |
| 790 | } |
| 791 | #endif /* CONFIG_PM_SLEEP */ |
| 792 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 793 | #ifdef CONFIG_PM |
| 794 | static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev, |
| 795 | struct device_attribute *attr, |
| 796 | char *buf) |
| 797 | { |
| 798 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 799 | u8 pwr_cmd; |
| 800 | u16 sleep_time; |
| 801 | int error; |
| 802 | |
| 803 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 804 | if (error) |
| 805 | return error; |
| 806 | |
| 807 | pwr_cmd = cyapa->runtime_suspend_power_mode; |
| 808 | sleep_time = cyapa->runtime_suspend_sleep_time; |
| 809 | |
| 810 | mutex_unlock(&cyapa->state_sync_lock); |
| 811 | |
| 812 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 813 | cyapa->gen == CYAPA_GEN3 ? |
| 814 | cyapa_pwr_cmd_to_sleep_time(pwr_cmd) : |
| 815 | sleep_time); |
| 816 | } |
| 817 | |
| 818 | static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev, |
| 819 | struct device_attribute *attr, |
| 820 | const char *buf, size_t count) |
| 821 | { |
| 822 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 823 | u16 time; |
| 824 | int error; |
| 825 | |
| 826 | if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) { |
| 827 | dev_err(dev, "invalid runtime suspend scanrate ms parameter\n"); |
| 828 | return -EINVAL; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * When the suspend scanrate is changed, pm_runtime_get to resume |
| 833 | * a potentially suspended device, update to the new pwr_cmd |
| 834 | * and then pm_runtime_put to suspend into the new power mode. |
| 835 | */ |
| 836 | pm_runtime_get_sync(dev); |
| 837 | |
| 838 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 839 | if (error) |
| 840 | return error; |
| 841 | |
| 842 | cyapa->runtime_suspend_sleep_time = max_t(u16, time, 1000); |
| 843 | cyapa->runtime_suspend_power_mode = |
| 844 | cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time); |
| 845 | |
| 846 | mutex_unlock(&cyapa->state_sync_lock); |
| 847 | |
| 848 | pm_runtime_put_sync_autosuspend(dev); |
| 849 | |
| 850 | return count; |
| 851 | } |
| 852 | |
| 853 | static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR, |
| 854 | cyapa_show_rt_suspend_scanrate, |
| 855 | cyapa_update_rt_suspend_scanrate); |
| 856 | |
| 857 | static struct attribute *cyapa_power_runtime_entries[] = { |
| 858 | &dev_attr_runtime_suspend_scanrate_ms.attr, |
| 859 | NULL, |
| 860 | }; |
| 861 | |
| 862 | static const struct attribute_group cyapa_power_runtime_group = { |
| 863 | .name = power_group_name, |
| 864 | .attrs = cyapa_power_runtime_entries, |
| 865 | }; |
| 866 | |
| 867 | static void cyapa_remove_power_runtime_group(void *data) |
| 868 | { |
| 869 | struct cyapa *cyapa = data; |
| 870 | |
| 871 | sysfs_unmerge_group(&cyapa->client->dev.kobj, |
| 872 | &cyapa_power_runtime_group); |
| 873 | } |
| 874 | |
| 875 | static int cyapa_start_runtime(struct cyapa *cyapa) |
| 876 | { |
| 877 | struct device *dev = &cyapa->client->dev; |
| 878 | int error; |
| 879 | |
| 880 | cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE; |
| 881 | cyapa->runtime_suspend_sleep_time = |
| 882 | cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode); |
| 883 | |
| 884 | error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group); |
| 885 | if (error) { |
| 886 | dev_err(dev, |
| 887 | "failed to create power runtime group: %d\n", error); |
| 888 | return error; |
| 889 | } |
| 890 | |
| 891 | error = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa); |
| 892 | if (error) { |
| 893 | cyapa_remove_power_runtime_group(cyapa); |
| 894 | dev_err(dev, |
| 895 | "failed to add power runtime cleanup action: %d\n", |
| 896 | error); |
| 897 | return error; |
| 898 | } |
| 899 | |
| 900 | /* runtime is enabled until device is operational and opened. */ |
| 901 | pm_runtime_set_suspended(dev); |
| 902 | pm_runtime_use_autosuspend(dev); |
| 903 | pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY); |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | #else |
| 908 | static inline int cyapa_start_runtime(struct cyapa *cyapa) |
| 909 | { |
| 910 | return 0; |
| 911 | } |
| 912 | #endif /* CONFIG_PM */ |
| 913 | |
Dudley Du | c806b0b | 2015-01-17 22:07:12 -0800 | [diff] [blame^] | 914 | static ssize_t cyapa_show_fm_ver(struct device *dev, |
| 915 | struct device_attribute *attr, char *buf) |
| 916 | { |
| 917 | int error; |
| 918 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 919 | |
| 920 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 921 | if (error) |
| 922 | return error; |
| 923 | error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver, |
| 924 | cyapa->fw_min_ver); |
| 925 | mutex_unlock(&cyapa->state_sync_lock); |
| 926 | return error; |
| 927 | } |
| 928 | |
| 929 | static ssize_t cyapa_show_product_id(struct device *dev, |
| 930 | struct device_attribute *attr, char *buf) |
| 931 | { |
| 932 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 933 | int size; |
| 934 | int error; |
| 935 | |
| 936 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 937 | if (error) |
| 938 | return error; |
| 939 | size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id); |
| 940 | mutex_unlock(&cyapa->state_sync_lock); |
| 941 | return size; |
| 942 | } |
| 943 | |
| 944 | static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name) |
| 945 | { |
| 946 | struct device *dev = &cyapa->client->dev; |
| 947 | const struct firmware *fw; |
| 948 | int error; |
| 949 | |
| 950 | error = request_firmware(&fw, fw_name, dev); |
| 951 | if (error) { |
| 952 | dev_err(dev, "Could not load firmware from %s: %d\n", |
| 953 | fw_name, error); |
| 954 | return error; |
| 955 | } |
| 956 | |
| 957 | error = cyapa->ops->check_fw(cyapa, fw); |
| 958 | if (error) { |
| 959 | dev_err(dev, "Invalid CYAPA firmware image: %s\n", |
| 960 | fw_name); |
| 961 | goto done; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Resume the potentially suspended device because doing FW |
| 966 | * update on a device not in the FULL mode has a chance to |
| 967 | * fail. |
| 968 | */ |
| 969 | pm_runtime_get_sync(dev); |
| 970 | |
| 971 | /* Require IRQ support for firmware update commands. */ |
| 972 | cyapa_enable_irq_for_cmd(cyapa); |
| 973 | |
| 974 | error = cyapa->ops->bl_enter(cyapa); |
| 975 | if (error) { |
| 976 | dev_err(dev, "bl_enter failed, %d\n", error); |
| 977 | goto err_detect; |
| 978 | } |
| 979 | |
| 980 | error = cyapa->ops->bl_activate(cyapa); |
| 981 | if (error) { |
| 982 | dev_err(dev, "bl_activate failed, %d\n", error); |
| 983 | goto err_detect; |
| 984 | } |
| 985 | |
| 986 | error = cyapa->ops->bl_initiate(cyapa, fw); |
| 987 | if (error) { |
| 988 | dev_err(dev, "bl_initiate failed, %d\n", error); |
| 989 | goto err_detect; |
| 990 | } |
| 991 | |
| 992 | error = cyapa->ops->update_fw(cyapa, fw); |
| 993 | if (error) { |
| 994 | dev_err(dev, "update_fw failed, %d\n", error); |
| 995 | goto err_detect; |
| 996 | } |
| 997 | |
| 998 | err_detect: |
| 999 | cyapa_disable_irq_for_cmd(cyapa); |
| 1000 | pm_runtime_put_noidle(dev); |
| 1001 | |
| 1002 | done: |
| 1003 | release_firmware(fw); |
| 1004 | return error; |
| 1005 | } |
| 1006 | |
| 1007 | static ssize_t cyapa_update_fw_store(struct device *dev, |
| 1008 | struct device_attribute *attr, |
| 1009 | const char *buf, size_t count) |
| 1010 | { |
| 1011 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1012 | char fw_name[NAME_MAX]; |
| 1013 | int ret, error; |
| 1014 | |
| 1015 | if (count > NAME_MAX) { |
| 1016 | dev_err(dev, "File name too long\n"); |
| 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | |
| 1020 | memcpy(fw_name, buf, count); |
| 1021 | if (fw_name[count - 1] == '\n') |
| 1022 | fw_name[count - 1] = '\0'; |
| 1023 | else |
| 1024 | fw_name[count] = '\0'; |
| 1025 | |
| 1026 | if (cyapa->input) { |
| 1027 | /* |
| 1028 | * Force the input device to be registered after the firmware |
| 1029 | * image is updated, so if the corresponding parameters updated |
| 1030 | * in the new firmware image can taken effect immediately. |
| 1031 | */ |
| 1032 | input_unregister_device(cyapa->input); |
| 1033 | cyapa->input = NULL; |
| 1034 | } |
| 1035 | |
| 1036 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 1037 | if (error) { |
| 1038 | /* |
| 1039 | * Whatever, do reinitialize to try to recover TP state to |
| 1040 | * previous state just as it entered fw update entrance. |
| 1041 | */ |
| 1042 | cyapa_reinitialize(cyapa); |
| 1043 | return error; |
| 1044 | } |
| 1045 | |
| 1046 | error = cyapa_firmware(cyapa, fw_name); |
| 1047 | if (error) |
| 1048 | dev_err(dev, "firmware update failed: %d\n", error); |
| 1049 | else |
| 1050 | dev_dbg(dev, "firmware update successfully done.\n"); |
| 1051 | |
| 1052 | /* |
| 1053 | * Redetect trackpad device states because firmware update process |
| 1054 | * will reset trackpad device into bootloader mode. |
| 1055 | */ |
| 1056 | ret = cyapa_reinitialize(cyapa); |
| 1057 | if (ret) { |
| 1058 | dev_err(dev, "failed to redetect after updated: %d\n", ret); |
| 1059 | error = error ? error : ret; |
| 1060 | } |
| 1061 | |
| 1062 | mutex_unlock(&cyapa->state_sync_lock); |
| 1063 | |
| 1064 | return error ? error : count; |
| 1065 | } |
| 1066 | |
| 1067 | static ssize_t cyapa_calibrate_store(struct device *dev, |
| 1068 | struct device_attribute *attr, |
| 1069 | const char *buf, size_t count) |
| 1070 | { |
| 1071 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1072 | int error; |
| 1073 | |
| 1074 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 1075 | if (error) |
| 1076 | return error; |
| 1077 | |
| 1078 | if (cyapa->operational) { |
| 1079 | cyapa_enable_irq_for_cmd(cyapa); |
| 1080 | error = cyapa->ops->calibrate_store(dev, attr, buf, count); |
| 1081 | cyapa_disable_irq_for_cmd(cyapa); |
| 1082 | } else { |
| 1083 | error = -EBUSY; /* Still running in bootloader mode. */ |
| 1084 | } |
| 1085 | |
| 1086 | mutex_unlock(&cyapa->state_sync_lock); |
| 1087 | return error < 0 ? error : count; |
| 1088 | } |
| 1089 | |
| 1090 | static ssize_t cyapa_show_baseline(struct device *dev, |
| 1091 | struct device_attribute *attr, char *buf) |
| 1092 | { |
| 1093 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1094 | ssize_t error; |
| 1095 | |
| 1096 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 1097 | if (error) |
| 1098 | return error; |
| 1099 | |
| 1100 | if (cyapa->operational) { |
| 1101 | cyapa_enable_irq_for_cmd(cyapa); |
| 1102 | error = cyapa->ops->show_baseline(dev, attr, buf); |
| 1103 | cyapa_disable_irq_for_cmd(cyapa); |
| 1104 | } else { |
| 1105 | error = -EBUSY; /* Still running in bootloader mode. */ |
| 1106 | } |
| 1107 | |
| 1108 | mutex_unlock(&cyapa->state_sync_lock); |
| 1109 | return error; |
| 1110 | } |
| 1111 | |
| 1112 | static char *cyapa_state_to_string(struct cyapa *cyapa) |
| 1113 | { |
| 1114 | switch (cyapa->state) { |
| 1115 | case CYAPA_STATE_BL_BUSY: |
| 1116 | return "bootloader busy"; |
| 1117 | case CYAPA_STATE_BL_IDLE: |
| 1118 | return "bootloader idle"; |
| 1119 | case CYAPA_STATE_BL_ACTIVE: |
| 1120 | return "bootloader active"; |
| 1121 | case CYAPA_STATE_GEN5_BL: |
| 1122 | return "bootloader"; |
| 1123 | case CYAPA_STATE_OP: |
| 1124 | case CYAPA_STATE_GEN5_APP: |
| 1125 | return "operational"; /* Normal valid state. */ |
| 1126 | default: |
| 1127 | return "invalid mode"; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | static ssize_t cyapa_show_mode(struct device *dev, |
| 1132 | struct device_attribute *attr, char *buf) |
| 1133 | { |
| 1134 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1135 | int size; |
| 1136 | int error; |
| 1137 | |
| 1138 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
| 1139 | if (error) |
| 1140 | return error; |
| 1141 | |
| 1142 | size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n", |
| 1143 | cyapa->gen, cyapa_state_to_string(cyapa)); |
| 1144 | |
| 1145 | mutex_unlock(&cyapa->state_sync_lock); |
| 1146 | return size; |
| 1147 | } |
| 1148 | |
| 1149 | static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL); |
| 1150 | static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL); |
| 1151 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store); |
| 1152 | static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL); |
| 1153 | static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store); |
| 1154 | static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL); |
| 1155 | |
| 1156 | static struct attribute *cyapa_sysfs_entries[] = { |
| 1157 | &dev_attr_firmware_version.attr, |
| 1158 | &dev_attr_product_id.attr, |
| 1159 | &dev_attr_update_fw.attr, |
| 1160 | &dev_attr_baseline.attr, |
| 1161 | &dev_attr_calibrate.attr, |
| 1162 | &dev_attr_mode.attr, |
| 1163 | NULL, |
| 1164 | }; |
| 1165 | |
| 1166 | static const struct attribute_group cyapa_sysfs_group = { |
| 1167 | .attrs = cyapa_sysfs_entries, |
| 1168 | }; |
| 1169 | |
| 1170 | static void cyapa_remove_sysfs_group(void *data) |
| 1171 | { |
| 1172 | struct cyapa *cyapa = data; |
| 1173 | |
| 1174 | sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group); |
| 1175 | } |
| 1176 | |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1177 | static int cyapa_probe(struct i2c_client *client, |
| 1178 | const struct i2c_device_id *dev_id) |
| 1179 | { |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1180 | struct device *dev = &client->dev; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1181 | struct cyapa *cyapa; |
| 1182 | u8 adapter_func; |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1183 | union i2c_smbus_data dummy; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1184 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1185 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 1186 | adapter_func = cyapa_check_adapter_functionality(client); |
| 1187 | if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) { |
| 1188 | dev_err(dev, "not a supported I2C/SMBus adapter\n"); |
| 1189 | return -EIO; |
| 1190 | } |
| 1191 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1192 | /* Make sure there is something at this address */ |
| 1193 | if (i2c_smbus_xfer(client->adapter, client->addr, 0, |
| 1194 | I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) |
| 1195 | return -ENODEV; |
| 1196 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1197 | cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL); |
| 1198 | if (!cyapa) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1199 | return -ENOMEM; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1200 | |
Benson Leung | 6ddaf74 | 2013-02-13 13:56:03 -0800 | [diff] [blame] | 1201 | /* i2c isn't supported, use smbus */ |
| 1202 | if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS) |
| 1203 | cyapa->smbus = true; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1204 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1205 | cyapa->client = client; |
| 1206 | i2c_set_clientdata(client, cyapa); |
| 1207 | sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr, |
| 1208 | client->addr); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1209 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1210 | error = cyapa_initialize(cyapa); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1211 | if (error) { |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1212 | dev_err(dev, "failed to detect and initialize tp device.\n"); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1213 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
Dudley Du | c806b0b | 2015-01-17 22:07:12 -0800 | [diff] [blame^] | 1216 | error = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group); |
| 1217 | if (error) { |
| 1218 | dev_err(dev, "failed to create sysfs entries: %d\n", error); |
| 1219 | return error; |
| 1220 | } |
| 1221 | |
| 1222 | error = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa); |
| 1223 | if (error) { |
| 1224 | cyapa_remove_sysfs_group(cyapa); |
| 1225 | dev_err(dev, "failed to add sysfs cleanup action: %d\n", error); |
| 1226 | return error; |
| 1227 | } |
| 1228 | |
Dudley Du | 22e7db8 | 2015-01-17 18:56:18 -0800 | [diff] [blame] | 1229 | error = cyapa_prepare_wakeup_controls(cyapa); |
| 1230 | if (error) { |
| 1231 | dev_err(dev, "failed to prepare wakeup controls: %d\n", error); |
| 1232 | return error; |
| 1233 | } |
| 1234 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 1235 | error = cyapa_start_runtime(cyapa); |
| 1236 | if (error) { |
| 1237 | dev_err(dev, "failed to start pm_runtime: %d\n", error); |
| 1238 | return error; |
| 1239 | } |
| 1240 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1241 | error = devm_request_threaded_irq(dev, client->irq, |
| 1242 | NULL, cyapa_irq, |
| 1243 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 1244 | "cyapa", cyapa); |
| 1245 | if (error) { |
Dudley Du | 823a11f | 2014-12-04 07:00:03 -0800 | [diff] [blame] | 1246 | dev_err(dev, "failed to request threaded irq: %d\n", error); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1247 | return error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1250 | /* Disable IRQ until the device is opened */ |
| 1251 | disable_irq(client->irq); |
| 1252 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1253 | /* |
| 1254 | * Register the device in the input subsystem when it's operational. |
| 1255 | * Otherwise, keep in this driver, so it can be be recovered or updated |
| 1256 | * through the sysfs mode and update_fw interfaces by user or apps. |
| 1257 | */ |
| 1258 | if (cyapa->operational) { |
| 1259 | error = cyapa_create_input_dev(cyapa); |
| 1260 | if (error) { |
| 1261 | dev_err(dev, "create input_dev instance failed: %d\n", |
| 1262 | error); |
| 1263 | return error; |
| 1264 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | return 0; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Jingoo Han | 572081a | 2014-11-02 00:03:37 -0700 | [diff] [blame] | 1270 | static int __maybe_unused cyapa_suspend(struct device *dev) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1271 | { |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1272 | struct i2c_client *client = to_i2c_client(dev); |
| 1273 | struct cyapa *cyapa = i2c_get_clientdata(client); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1274 | u8 power_mode; |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1275 | int error; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1276 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1277 | error = mutex_lock_interruptible(&cyapa->state_sync_lock); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1278 | if (error) |
| 1279 | return error; |
| 1280 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 1281 | /* |
| 1282 | * Runtime PM is enable only when device is in operational mode and |
| 1283 | * users in use, so need check it before disable it to |
| 1284 | * avoid unbalance warning. |
| 1285 | */ |
| 1286 | if (pm_runtime_enabled(dev)) |
| 1287 | pm_runtime_disable(dev); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1288 | disable_irq(client->irq); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * Set trackpad device to idle mode if wakeup is allowed, |
| 1292 | * otherwise turn off. |
| 1293 | */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1294 | if (cyapa->operational) { |
| 1295 | power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode |
| 1296 | : PWR_MODE_OFF; |
| 1297 | error = cyapa->ops->set_power_mode(cyapa, power_mode, |
| 1298 | cyapa->suspend_sleep_time); |
| 1299 | if (error) |
| 1300 | dev_err(dev, "suspend set power mode failed: %d\n", |
| 1301 | error); |
| 1302 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1303 | |
| 1304 | if (device_may_wakeup(dev)) |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 1305 | cyapa->irq_wake = (enable_irq_wake(client->irq) == 0); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1306 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1307 | mutex_unlock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Jingoo Han | 572081a | 2014-11-02 00:03:37 -0700 | [diff] [blame] | 1311 | static int __maybe_unused cyapa_resume(struct device *dev) |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1312 | { |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1313 | struct i2c_client *client = to_i2c_client(dev); |
| 1314 | struct cyapa *cyapa = i2c_get_clientdata(client); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1315 | int error; |
| 1316 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1317 | mutex_lock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1318 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1319 | if (device_may_wakeup(dev) && cyapa->irq_wake) { |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 1320 | disable_irq_wake(client->irq); |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1321 | cyapa->irq_wake = false; |
| 1322 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1323 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 1324 | /* Update device states and runtime PM states. */ |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1325 | error = cyapa_reinitialize(cyapa); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1326 | if (error) |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1327 | dev_warn(dev, "failed to reinitialize TP device: %d\n", error); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1328 | |
Dudley Du | f68a95c | 2014-12-03 15:29:34 -0800 | [diff] [blame] | 1329 | enable_irq(client->irq); |
Dudley Du | b1cfa7b | 2014-11-09 12:36:34 -0800 | [diff] [blame] | 1330 | |
Dudley Du | 9f1cd85 | 2015-01-17 18:35:26 -0800 | [diff] [blame] | 1331 | mutex_unlock(&cyapa->state_sync_lock); |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1334 | |
Dudley Du | 6728650 | 2015-01-17 18:57:42 -0800 | [diff] [blame] | 1335 | static int __maybe_unused cyapa_runtime_suspend(struct device *dev) |
| 1336 | { |
| 1337 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1338 | int error; |
| 1339 | |
| 1340 | error = cyapa->ops->set_power_mode(cyapa, |
| 1341 | cyapa->runtime_suspend_power_mode, |
| 1342 | cyapa->runtime_suspend_sleep_time); |
| 1343 | if (error) |
| 1344 | dev_warn(dev, "runtime suspend failed: %d\n", error); |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static int __maybe_unused cyapa_runtime_resume(struct device *dev) |
| 1350 | { |
| 1351 | struct cyapa *cyapa = dev_get_drvdata(dev); |
| 1352 | int error; |
| 1353 | |
| 1354 | error = cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0); |
| 1355 | if (error) |
| 1356 | dev_warn(dev, "runtime resume failed: %d\n", error); |
| 1357 | |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | static const struct dev_pm_ops cyapa_pm_ops = { |
| 1362 | SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume) |
| 1363 | SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL) |
| 1364 | }; |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1365 | |
| 1366 | static const struct i2c_device_id cyapa_id_table[] = { |
| 1367 | { "cyapa", 0 }, |
| 1368 | { }, |
| 1369 | }; |
| 1370 | MODULE_DEVICE_TABLE(i2c, cyapa_id_table); |
| 1371 | |
| 1372 | static struct i2c_driver cyapa_driver = { |
| 1373 | .driver = { |
| 1374 | .name = "cyapa", |
| 1375 | .owner = THIS_MODULE, |
| 1376 | .pm = &cyapa_pm_ops, |
| 1377 | }, |
| 1378 | |
| 1379 | .probe = cyapa_probe, |
Benson Leung | d7e34d1 | 2013-01-09 16:25:11 -0800 | [diff] [blame] | 1380 | .id_table = cyapa_id_table, |
| 1381 | }; |
| 1382 | |
| 1383 | module_i2c_driver(cyapa_driver); |
| 1384 | |
| 1385 | MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver"); |
| 1386 | MODULE_AUTHOR("Dudley Du <dudl@cypress.com>"); |
| 1387 | MODULE_LICENSE("GPL"); |