blob: 63f6646faf5fbdeb9cdf9aa6589e84bcc7f6cc50 [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
Dudley Du22e7db82015-01-17 18:56:18 -0800607/*
608 **************************************************************
609 * sysfs interface
610 **************************************************************
611*/
612#ifdef CONFIG_PM_SLEEP
613static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
614 struct device_attribute *attr,
615 char *buf)
616{
617 struct cyapa *cyapa = dev_get_drvdata(dev);
618 u8 pwr_cmd = cyapa->suspend_power_mode;
619 u16 sleep_time;
620 int len;
621 int error;
622
623 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
624 if (error)
625 return error;
626
627 pwr_cmd = cyapa->suspend_power_mode;
628 sleep_time = cyapa->suspend_sleep_time;
629
630 mutex_unlock(&cyapa->state_sync_lock);
631
632 switch (pwr_cmd) {
633 case PWR_MODE_BTN_ONLY:
634 len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
635 break;
636
637 case PWR_MODE_OFF:
638 len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
639 break;
640
641 default:
642 len = scnprintf(buf, PAGE_SIZE, "%u\n",
643 cyapa->gen == CYAPA_GEN3 ?
644 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
645 sleep_time);
646 break;
647 }
648
649 return len;
650}
651
652static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
653 struct device_attribute *attr,
654 const char *buf, size_t count)
655{
656 struct cyapa *cyapa = dev_get_drvdata(dev);
657 u16 sleep_time;
658 int error;
659
660 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
661 if (error)
662 return error;
663
664 if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
665 cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
666 } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
667 cyapa->suspend_power_mode = PWR_MODE_OFF;
668 } else if (!kstrtou16(buf, 10, &sleep_time)) {
669 cyapa->suspend_sleep_time = max_t(u16, sleep_time, 1000);
670 cyapa->suspend_power_mode =
671 cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
672 } else {
673 count = -EINVAL;
674 }
675
676 mutex_unlock(&cyapa->state_sync_lock);
677
678 return count;
679}
680
681static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
682 cyapa_show_suspend_scanrate,
683 cyapa_update_suspend_scanrate);
684
685static struct attribute *cyapa_power_wakeup_entries[] = {
686 &dev_attr_suspend_scanrate_ms.attr,
687 NULL,
688};
689
690static const struct attribute_group cyapa_power_wakeup_group = {
691 .name = power_group_name,
692 .attrs = cyapa_power_wakeup_entries,
693};
694
695static void cyapa_remove_power_wakeup_group(void *data)
696{
697 struct cyapa *cyapa = data;
698
699 sysfs_unmerge_group(&cyapa->client->dev.kobj,
700 &cyapa_power_wakeup_group);
701}
702
703static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
704{
705 struct i2c_client *client = cyapa->client;
706 struct device *dev = &client->dev;
707 int error;
708
709 if (device_can_wakeup(dev)) {
710 error = sysfs_merge_group(&client->dev.kobj,
711 &cyapa_power_wakeup_group);
712 if (error) {
713 dev_err(dev, "failed to add power wakeup group: %d\n",
714 error);
715 return error;
716 }
717
718 error = devm_add_action(dev,
719 cyapa_remove_power_wakeup_group, cyapa);
720 if (error) {
721 cyapa_remove_power_wakeup_group(cyapa);
722 dev_err(dev, "failed to add power cleanup action: %d\n",
723 error);
724 return error;
725 }
726 }
727
728 return 0;
729}
730#else
731static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
732{
733 return 0;
734}
735#endif /* CONFIG_PM_SLEEP */
736
Benson Leungd7e34d12013-01-09 16:25:11 -0800737static int cyapa_probe(struct i2c_client *client,
738 const struct i2c_device_id *dev_id)
739{
Benson Leungd7e34d12013-01-09 16:25:11 -0800740 struct device *dev = &client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800741 struct cyapa *cyapa;
742 u8 adapter_func;
Dudley Du9f1cd852015-01-17 18:35:26 -0800743 union i2c_smbus_data dummy;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800744 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800745
Benson Leung6ddaf742013-02-13 13:56:03 -0800746 adapter_func = cyapa_check_adapter_functionality(client);
747 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
748 dev_err(dev, "not a supported I2C/SMBus adapter\n");
749 return -EIO;
750 }
751
Dudley Du9f1cd852015-01-17 18:35:26 -0800752 /* Make sure there is something at this address */
753 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
754 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
755 return -ENODEV;
756
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800757 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
758 if (!cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -0800759 return -ENOMEM;
Benson Leungd7e34d12013-01-09 16:25:11 -0800760
Benson Leung6ddaf742013-02-13 13:56:03 -0800761 /* i2c isn't supported, use smbus */
762 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
763 cyapa->smbus = true;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800764
Dudley Du9f1cd852015-01-17 18:35:26 -0800765 cyapa->client = client;
766 i2c_set_clientdata(client, cyapa);
767 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
768 client->addr);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800769
Dudley Du9f1cd852015-01-17 18:35:26 -0800770 error = cyapa_initialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800771 if (error) {
Dudley Du9f1cd852015-01-17 18:35:26 -0800772 dev_err(dev, "failed to detect and initialize tp device.\n");
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800773 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800774 }
775
Dudley Du22e7db82015-01-17 18:56:18 -0800776 error = cyapa_prepare_wakeup_controls(cyapa);
777 if (error) {
778 dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
779 return error;
780 }
781
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800782 error = devm_request_threaded_irq(dev, client->irq,
783 NULL, cyapa_irq,
784 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
785 "cyapa", cyapa);
786 if (error) {
Dudley Du823a11f2014-12-04 07:00:03 -0800787 dev_err(dev, "failed to request threaded irq: %d\n", error);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800788 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800789 }
790
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800791 /* Disable IRQ until the device is opened */
792 disable_irq(client->irq);
793
Dudley Du9f1cd852015-01-17 18:35:26 -0800794 /*
795 * Register the device in the input subsystem when it's operational.
796 * Otherwise, keep in this driver, so it can be be recovered or updated
797 * through the sysfs mode and update_fw interfaces by user or apps.
798 */
799 if (cyapa->operational) {
800 error = cyapa_create_input_dev(cyapa);
801 if (error) {
802 dev_err(dev, "create input_dev instance failed: %d\n",
803 error);
804 return error;
805 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800806 }
807
808 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800809}
810
Jingoo Han572081a2014-11-02 00:03:37 -0700811static int __maybe_unused cyapa_suspend(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -0800812{
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800813 struct i2c_client *client = to_i2c_client(dev);
814 struct cyapa *cyapa = i2c_get_clientdata(client);
Benson Leungd7e34d12013-01-09 16:25:11 -0800815 u8 power_mode;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800816 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800817
Dudley Du9f1cd852015-01-17 18:35:26 -0800818 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800819 if (error)
820 return error;
821
822 disable_irq(client->irq);
Benson Leungd7e34d12013-01-09 16:25:11 -0800823
824 /*
825 * Set trackpad device to idle mode if wakeup is allowed,
826 * otherwise turn off.
827 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800828 if (cyapa->operational) {
829 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
830 : PWR_MODE_OFF;
831 error = cyapa->ops->set_power_mode(cyapa, power_mode,
832 cyapa->suspend_sleep_time);
833 if (error)
834 dev_err(dev, "suspend set power mode failed: %d\n",
835 error);
836 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800837
838 if (device_may_wakeup(dev))
Dudley Duf68a95c2014-12-03 15:29:34 -0800839 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800840
Dudley Du9f1cd852015-01-17 18:35:26 -0800841 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800842 return 0;
843}
844
Jingoo Han572081a2014-11-02 00:03:37 -0700845static int __maybe_unused cyapa_resume(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -0800846{
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800847 struct i2c_client *client = to_i2c_client(dev);
848 struct cyapa *cyapa = i2c_get_clientdata(client);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800849 int error;
850
Dudley Du9f1cd852015-01-17 18:35:26 -0800851 mutex_lock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800852
Dudley Du9f1cd852015-01-17 18:35:26 -0800853 if (device_may_wakeup(dev) && cyapa->irq_wake) {
Dudley Duf68a95c2014-12-03 15:29:34 -0800854 disable_irq_wake(client->irq);
Dudley Du9f1cd852015-01-17 18:35:26 -0800855 cyapa->irq_wake = false;
856 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800857
Dudley Du9f1cd852015-01-17 18:35:26 -0800858 error = cyapa_reinitialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800859 if (error)
Dudley Du9f1cd852015-01-17 18:35:26 -0800860 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
Benson Leungd7e34d12013-01-09 16:25:11 -0800861
Dudley Duf68a95c2014-12-03 15:29:34 -0800862 enable_irq(client->irq);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800863
Dudley Du9f1cd852015-01-17 18:35:26 -0800864 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -0800865 return 0;
866}
Benson Leungd7e34d12013-01-09 16:25:11 -0800867
868static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
869
870static const struct i2c_device_id cyapa_id_table[] = {
871 { "cyapa", 0 },
872 { },
873};
874MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
875
876static struct i2c_driver cyapa_driver = {
877 .driver = {
878 .name = "cyapa",
879 .owner = THIS_MODULE,
880 .pm = &cyapa_pm_ops,
881 },
882
883 .probe = cyapa_probe,
Benson Leungd7e34d12013-01-09 16:25:11 -0800884 .id_table = cyapa_id_table,
885};
886
887module_i2c_driver(cyapa_driver);
888
889MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
890MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
891MODULE_LICENSE("GPL");