blob: fa4d5010534df5bc4da8be41e1e2ceec52c5a138 [file] [log] [blame]
Benson Leungd7e34d12013-01-09 16:25:11 -08001/*
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 Du823a11f2014-12-04 07:00:03 -08009 * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
Benson Leungd7e34d12013-01-09 16:25:11 -080010 * 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 Du9f1cd852015-01-17 18:35:26 -080023#include <linux/mutex.h>
Benson Leungd7e34d12013-01-09 16:25:11 -080024#include <linux/slab.h>
Dudley Du9f1cd852015-01-17 18:35:26 -080025#include <linux/uaccess.h>
26#include "cyapa.h"
Benson Leungd7e34d12013-01-09 16:25:11 -080027
Benson Leungd7e34d12013-01-09 16:25:11 -080028
Benson Leung6ddaf742013-02-13 13:56:03 -080029#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 Du9f1cd852015-01-17 18:35:26 -080034const char product_id[] = "CYTRA";
Benson Leung6ddaf742013-02-13 13:56:03 -080035
Dudley Du9f1cd852015-01-17 18:35:26 -080036static int cyapa_reinitialize(struct cyapa *cyapa);
Benson Leung6ddaf742013-02-13 13:56:03 -080037
Dudley Du9f1cd852015-01-17 18:35:26 -080038static 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 Leung6ddaf742013-02-13 13:56:03 -080042
Dudley Du9f1cd852015-01-17 18:35:26 -080043 if (cyapa->gen == CYAPA_GEN3 &&
44 cyapa->state >= CYAPA_STATE_BL_BUSY &&
45 cyapa->state <= CYAPA_STATE_BL_ACTIVE)
46 return true;
Benson Leung6ddaf742013-02-13 13:56:03 -080047
Dudley Du9f1cd852015-01-17 18:35:26 -080048 return false;
49}
Benson Leung6ddaf742013-02-13 13:56:03 -080050
Dudley Du9f1cd852015-01-17 18:35:26 -080051static 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 Leung6ddaf742013-02-13 13:56:03 -080055
Dudley Du9f1cd852015-01-17 18:35:26 -080056 if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
57 return true;
Benson Leungd7e34d12013-01-09 16:25:11 -080058
Dudley Du9f1cd852015-01-17 18:35:26 -080059 return false;
60}
Benson Leung6ddaf742013-02-13 13:56:03 -080061
Dudley Du9f1cd852015-01-17 18:35:26 -080062/* Returns 0 on success, else negative errno on failure. */
63static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
Benson Leungd7e34d12013-01-09 16:25:11 -080064 u8 *values)
65{
Benson Leung6ddaf742013-02-13 13:56:03 -080066 struct i2c_client *client = cyapa->client;
Dudley Du9f1cd852015-01-17 18:35:26 -080067 struct i2c_msg msgs[] = {
68 {
69 .addr = client->addr,
70 .flags = 0,
71 .len = 1,
72 .buf = &reg,
73 },
74 {
75 .addr = client->addr,
76 .flags = I2C_M_RD,
77 .len = len,
78 .buf = values,
79 },
80 };
81 int ret;
Benson Leung6ddaf742013-02-13 13:56:03 -080082
Dudley Du9f1cd852015-01-17 18:35:26 -080083 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Benson Leung6ddaf742013-02-13 13:56:03 -080084
Dudley Du9f1cd852015-01-17 18:35:26 -080085 if (ret != ARRAY_SIZE(msgs))
86 return ret < 0 ? ret : -EIO;
Benson Leung6ddaf742013-02-13 13:56:03 -080087
Dudley Du9f1cd852015-01-17 18:35:26 -080088 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -080089}
90
Dudley Du9f1cd852015-01-17 18:35:26 -080091/**
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 */
100static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
101 size_t len, const void *values)
Benson Leungd7e34d12013-01-09 16:25:11 -0800102{
Dudley Du9f1cd852015-01-17 18:35:26 -0800103 struct i2c_client *client = cyapa->client;
104 char buf[32];
105 int ret;
Benson Leungd7e34d12013-01-09 16:25:11 -0800106
Dudley Du9f1cd852015-01-17 18:35:26 -0800107 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 Leungd7e34d12013-01-09 16:25:11 -0800118}
119
Dudley Du9f1cd852015-01-17 18:35:26 -0800120static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
Benson Leungd7e34d12013-01-09 16:25:11 -0800121{
Dudley Du9f1cd852015-01-17 18:35:26 -0800122 u8 ret = CYAPA_ADAPTER_FUNC_NONE;
Benson Leungd7e34d12013-01-09 16:25:11 -0800123
Dudley Du9f1cd852015-01-17 18:35:26 -0800124 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 Leungd7e34d12013-01-09 16:25:11 -0800131}
132
133/*
134 * Query device for its current operating state.
Benson Leungd7e34d12013-01-09 16:25:11 -0800135 */
136static int cyapa_get_state(struct cyapa *cyapa)
137{
Benson Leungd7e34d12013-01-09 16:25:11 -0800138 u8 status[BL_STATUS_SIZE];
Dudley Du9f1cd852015-01-17 18:35:26 -0800139 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 Du823a11f2014-12-04 07:00:03 -0800144 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800145
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 Du823a11f2014-12-04 07:00:03 -0800154 error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
Dudley Du9f1cd852015-01-17 18:35:26 -0800155 status);
Benson Leung6ddaf742013-02-13 13:56:03 -0800156
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 Du9f1cd852015-01-17 18:35:26 -0800162 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 Leung6ddaf742013-02-13 13:56:03 -0800168
Dudley Du823a11f2014-12-04 07:00:03 -0800169 if (error != BL_STATUS_SIZE)
Benson Leungd7e34d12013-01-09 16:25:11 -0800170 goto error;
171
Dudley Du9f1cd852015-01-17 18:35:26 -0800172 /*
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 Leungd7e34d12013-01-09 16:25:11 -0800179
Dudley Du9f1cd852015-01-17 18:35:26 -0800180 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 Du6972a852015-01-17 18:49:37 -0800187 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 Du9f1cd852015-01-17 18:35:26 -0800195
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
218out_detected:
219 if (cyapa->state <= CYAPA_STATE_BL_BUSY)
220 return -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800221 return 0;
Dudley Du9f1cd852015-01-17 18:35:26 -0800222
Benson Leungd7e34d12013-01-09 16:25:11 -0800223error:
Dudley Du823a11f2014-12-04 07:00:03 -0800224 return (error < 0) ? error : -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800225}
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 Du9f1cd852015-01-17 18:35:26 -0800239 * -EAGAIN if bootload is busy, or unknown state.
240 * < 0 other errors
Benson Leungd7e34d12013-01-09 16:25:11 -0800241 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800242int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
Benson Leungd7e34d12013-01-09 16:25:11 -0800243{
Dudley Du823a11f2014-12-04 07:00:03 -0800244 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800245 int tries = timeout / 100;
246
Dudley Du9f1cd852015-01-17 18:35:26 -0800247 do {
Dudley Du823a11f2014-12-04 07:00:03 -0800248 error = cyapa_get_state(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800249 if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
250 return 0;
251
252 msleep(100);
253 } while (tries--);
254
Dudley Du823a11f2014-12-04 07:00:03 -0800255 return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800256}
257
Benson Leungd7e34d12013-01-09 16:25:11 -0800258/*
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 Du9f1cd852015-01-17 18:35:26 -0800265 * -ENODEV no device
Benson Leungd7e34d12013-01-09 16:25:11 -0800266 * -EBUSY no device or in bootloader
267 * -EIO failure while reading from device
Dudley Du9f1cd852015-01-17 18:35:26 -0800268 * -ETIMEDOUT timeout failure for bus idle or bus no response
Benson Leungd7e34d12013-01-09 16:25:11 -0800269 * -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 */
274static int cyapa_check_is_operational(struct cyapa *cyapa)
275{
Dudley Du823a11f2014-12-04 07:00:03 -0800276 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800277
Dudley Du9f1cd852015-01-17 18:35:26 -0800278 error = cyapa_poll_state(cyapa, 4000);
Dudley Du823a11f2014-12-04 07:00:03 -0800279 if (error)
280 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800281
Dudley Du9f1cd852015-01-17 18:35:26 -0800282 switch (cyapa->gen) {
Dudley Du6972a852015-01-17 18:49:37 -0800283 case CYAPA_GEN5:
284 cyapa->ops = &cyapa_gen5_ops;
285 break;
Dudley Du9f1cd852015-01-17 18:35:26 -0800286 case CYAPA_GEN3:
287 cyapa->ops = &cyapa_gen3_ops;
288 break;
Benson Leungd7e34d12013-01-09 16:25:11 -0800289 default:
Dudley Du9f1cd852015-01-17 18:35:26 -0800290 return -ENODEV;
Benson Leungd7e34d12013-01-09 16:25:11 -0800291 }
Dudley Du9f1cd852015-01-17 18:35:26 -0800292
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 Leungd7e34d12013-01-09 16:25:11 -0800300}
301
Dudley Du9f1cd852015-01-17 18:35:26 -0800302
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 */
308static int cyapa_detect(struct cyapa *cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -0800309{
Benson Leungd7e34d12013-01-09 16:25:11 -0800310 struct device *dev = &cyapa->client->dev;
Dudley Du9f1cd852015-01-17 18:35:26 -0800311 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800312
Dudley Du9f1cd852015-01-17 18:35:26 -0800313 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 Leungd7e34d12013-01-09 16:25:11 -0800320
Dudley Du9f1cd852015-01-17 18:35:26 -0800321 dev_err(dev, "no device detected: %d\n", error);
322 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800323 }
324
Dudley Du9f1cd852015-01-17 18:35:26 -0800325 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -0800326}
327
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800328static 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 Du9f1cd852015-01-17 18:35:26 -0800334 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
335 if (error)
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800336 return error;
Dudley Du9f1cd852015-01-17 18:35:26 -0800337
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 Dub1cfa7b2014-11-09 12:36:34 -0800357 }
358
359 enable_irq(client->irq);
Dudley Du9f1cd852015-01-17 18:35:26 -0800360out:
361 mutex_unlock(&cyapa->state_sync_lock);
362 return error;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800363}
364
365static void cyapa_close(struct input_dev *input)
366{
367 struct cyapa *cyapa = input_get_drvdata(input);
Dudley Du9f1cd852015-01-17 18:35:26 -0800368 struct i2c_client *client = cyapa->client;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800369
Dudley Du9f1cd852015-01-17 18:35:26 -0800370 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 Dub1cfa7b2014-11-09 12:36:34 -0800377}
378
Benson Leungd7e34d12013-01-09 16:25:11 -0800379static int cyapa_create_input_dev(struct cyapa *cyapa)
380{
381 struct device *dev = &cyapa->client->dev;
Benson Leungd7e34d12013-01-09 16:25:11 -0800382 struct input_dev *input;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800383 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800384
385 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
386 return -EINVAL;
387
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800388 input = devm_input_allocate_device(dev);
Benson Leungd7e34d12013-01-09 16:25:11 -0800389 if (!input) {
Dudley Du823a11f2014-12-04 07:00:03 -0800390 dev_err(dev, "failed to allocate memory for input device.\n");
Benson Leungd7e34d12013-01-09 16:25:11 -0800391 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 Du823a11f2014-12-04 07:00:03 -0800398 input->id.product = 0; /* Means any product in eventcomm. */
Benson Leungd7e34d12013-01-09 16:25:11 -0800399 input->dev.parent = &cyapa->client->dev;
400
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800401 input->open = cyapa_open;
402 input->close = cyapa_close;
403
Benson Leungd7e34d12013-01-09 16:25:11 -0800404 input_set_drvdata(input, cyapa);
405
406 __set_bit(EV_ABS, input->evbit);
407
Dudley Du823a11f2014-12-04 07:00:03 -0800408 /* Finger position */
Benson Leungd7e34d12013-01-09 16:25:11 -0800409 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 Du9f1cd852015-01-17 18:35:26 -0800413 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 Leungd7e34d12013-01-09 16:25:11 -0800435
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 Du823a11f2014-12-04 07:00:03 -0800451 /* Handle pointer emulation and unused slots in core */
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800452 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 Leungd7e34d12013-01-09 16:25:11 -0800457 }
458
Dudley Du9f1cd852015-01-17 18:35:26 -0800459 /* 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 Dub1cfa7b2014-11-09 12:36:34 -0800466 cyapa->input = input;
Benson Leungd7e34d12013-01-09 16:25:11 -0800467 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800468}
469
Dudley Du9f1cd852015-01-17 18:35:26 -0800470/*
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 */
484u8 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
493u16 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. */
502static 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 Du6972a852015-01-17 18:49:37 -0800520 if (!error)
521 error = cyapa_gen5_ops.initialize(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800522 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
536static 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
559out:
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
569static 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
603out:
604 return IRQ_HANDLED;
605}
606
Benson Leungd7e34d12013-01-09 16:25:11 -0800607static int cyapa_probe(struct i2c_client *client,
608 const struct i2c_device_id *dev_id)
609{
Benson Leungd7e34d12013-01-09 16:25:11 -0800610 struct device *dev = &client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800611 struct cyapa *cyapa;
612 u8 adapter_func;
Dudley Du9f1cd852015-01-17 18:35:26 -0800613 union i2c_smbus_data dummy;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800614 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800615
Benson Leung6ddaf742013-02-13 13:56:03 -0800616 adapter_func = cyapa_check_adapter_functionality(client);
617 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
618 dev_err(dev, "not a supported I2C/SMBus adapter\n");
619 return -EIO;
620 }
621
Dudley Du9f1cd852015-01-17 18:35:26 -0800622 /* Make sure there is something at this address */
623 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
624 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
625 return -ENODEV;
626
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800627 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
628 if (!cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -0800629 return -ENOMEM;
Benson Leungd7e34d12013-01-09 16:25:11 -0800630
Benson Leung6ddaf742013-02-13 13:56:03 -0800631 /* i2c isn't supported, use smbus */
632 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
633 cyapa->smbus = true;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800634
Dudley Du9f1cd852015-01-17 18:35:26 -0800635 cyapa->client = client;
636 i2c_set_clientdata(client, cyapa);
637 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
638 client->addr);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800639
Dudley Du9f1cd852015-01-17 18:35:26 -0800640 error = cyapa_initialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800641 if (error) {
Dudley Du9f1cd852015-01-17 18:35:26 -0800642 dev_err(dev, "failed to detect and initialize tp device.\n");
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800643 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800644 }
645
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800646 error = devm_request_threaded_irq(dev, client->irq,
647 NULL, cyapa_irq,
648 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
649 "cyapa", cyapa);
650 if (error) {
Dudley Du823a11f2014-12-04 07:00:03 -0800651 dev_err(dev, "failed to request threaded irq: %d\n", error);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800652 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800653 }
654
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800655 /* Disable IRQ until the device is opened */
656 disable_irq(client->irq);
657
Dudley Du9f1cd852015-01-17 18:35:26 -0800658 /*
659 * Register the device in the input subsystem when it's operational.
660 * Otherwise, keep in this driver, so it can be be recovered or updated
661 * through the sysfs mode and update_fw interfaces by user or apps.
662 */
663 if (cyapa->operational) {
664 error = cyapa_create_input_dev(cyapa);
665 if (error) {
666 dev_err(dev, "create input_dev instance failed: %d\n",
667 error);
668 return error;
669 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800670 }
671
672 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800673}
674
Jingoo Han572081a2014-11-02 00:03:37 -0700675static int __maybe_unused cyapa_suspend(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -0800676{
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800677 struct i2c_client *client = to_i2c_client(dev);
678 struct cyapa *cyapa = i2c_get_clientdata(client);
Benson Leungd7e34d12013-01-09 16:25:11 -0800679 u8 power_mode;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800680 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800681
Dudley Du9f1cd852015-01-17 18:35:26 -0800682 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800683 if (error)
684 return error;
685
686 disable_irq(client->irq);
Benson Leungd7e34d12013-01-09 16:25:11 -0800687
688 /*
689 * Set trackpad device to idle mode if wakeup is allowed,
690 * otherwise turn off.
691 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800692 if (cyapa->operational) {
693 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
694 : PWR_MODE_OFF;
695 error = cyapa->ops->set_power_mode(cyapa, power_mode,
696 cyapa->suspend_sleep_time);
697 if (error)
698 dev_err(dev, "suspend set power mode failed: %d\n",
699 error);
700 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800701
702 if (device_may_wakeup(dev))
Dudley Duf68a95c2014-12-03 15:29:34 -0800703 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800704
Dudley Du9f1cd852015-01-17 18:35:26 -0800705 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800706 return 0;
707}
708
Jingoo Han572081a2014-11-02 00:03:37 -0700709static int __maybe_unused cyapa_resume(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -0800710{
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800711 struct i2c_client *client = to_i2c_client(dev);
712 struct cyapa *cyapa = i2c_get_clientdata(client);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800713 int error;
714
Dudley Du9f1cd852015-01-17 18:35:26 -0800715 mutex_lock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800716
Dudley Du9f1cd852015-01-17 18:35:26 -0800717 if (device_may_wakeup(dev) && cyapa->irq_wake) {
Dudley Duf68a95c2014-12-03 15:29:34 -0800718 disable_irq_wake(client->irq);
Dudley Du9f1cd852015-01-17 18:35:26 -0800719 cyapa->irq_wake = false;
720 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800721
Dudley Du9f1cd852015-01-17 18:35:26 -0800722 error = cyapa_reinitialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800723 if (error)
Dudley Du9f1cd852015-01-17 18:35:26 -0800724 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
Benson Leungd7e34d12013-01-09 16:25:11 -0800725
Dudley Duf68a95c2014-12-03 15:29:34 -0800726 enable_irq(client->irq);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800727
Dudley Du9f1cd852015-01-17 18:35:26 -0800728 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800729 return 0;
730}
Benson Leungd7e34d12013-01-09 16:25:11 -0800731
732static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
733
734static const struct i2c_device_id cyapa_id_table[] = {
735 { "cyapa", 0 },
736 { },
737};
738MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
739
740static struct i2c_driver cyapa_driver = {
741 .driver = {
742 .name = "cyapa",
743 .owner = THIS_MODULE,
744 .pm = &cyapa_pm_ops,
745 },
746
747 .probe = cyapa_probe,
Benson Leungd7e34d12013-01-09 16:25:11 -0800748 .id_table = cyapa_id_table,
749};
750
751module_i2c_driver(cyapa_driver);
752
753MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
754MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
755MODULE_LICENSE("GPL");