blob: f59553f72dfac4ddbe7dcebaec2703bb840d0ca1 [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>
Dudley Du67286502015-01-17 18:57:42 -080026#include <linux/pm_runtime.h>
Dudley Du9f1cd852015-01-17 18:35:26 -080027#include "cyapa.h"
Benson Leungd7e34d12013-01-09 16:25:11 -080028
Benson Leungd7e34d12013-01-09 16:25:11 -080029
Benson Leung6ddaf742013-02-13 13:56:03 -080030#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 Duc806b0b2015-01-17 22:07:12 -080035#define CYAPA_FW_NAME "cyapa.bin"
36
Dudley Du9f1cd852015-01-17 18:35:26 -080037const char product_id[] = "CYTRA";
Benson Leung6ddaf742013-02-13 13:56:03 -080038
Dudley Du9f1cd852015-01-17 18:35:26 -080039static int cyapa_reinitialize(struct cyapa *cyapa);
Benson Leung6ddaf742013-02-13 13:56:03 -080040
Dudley Du9f1cd852015-01-17 18:35:26 -080041static 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 Leung6ddaf742013-02-13 13:56:03 -080045
Dudley Du9f1cd852015-01-17 18:35:26 -080046 if (cyapa->gen == CYAPA_GEN3 &&
47 cyapa->state >= CYAPA_STATE_BL_BUSY &&
48 cyapa->state <= CYAPA_STATE_BL_ACTIVE)
49 return true;
Benson Leung6ddaf742013-02-13 13:56:03 -080050
Dudley Du9f1cd852015-01-17 18:35:26 -080051 return false;
52}
Benson Leung6ddaf742013-02-13 13:56:03 -080053
Dudley Du9f1cd852015-01-17 18:35:26 -080054static 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 Leung6ddaf742013-02-13 13:56:03 -080058
Dudley Du9f1cd852015-01-17 18:35:26 -080059 if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
60 return true;
Benson Leungd7e34d12013-01-09 16:25:11 -080061
Dudley Du9f1cd852015-01-17 18:35:26 -080062 return false;
63}
Benson Leung6ddaf742013-02-13 13:56:03 -080064
Dudley Du9f1cd852015-01-17 18:35:26 -080065/* Returns 0 on success, else negative errno on failure. */
66static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
Benson Leungd7e34d12013-01-09 16:25:11 -080067 u8 *values)
68{
Benson Leung6ddaf742013-02-13 13:56:03 -080069 struct i2c_client *client = cyapa->client;
Dudley Du9f1cd852015-01-17 18:35:26 -080070 struct i2c_msg msgs[] = {
71 {
72 .addr = client->addr,
73 .flags = 0,
74 .len = 1,
75 .buf = &reg,
76 },
77 {
78 .addr = client->addr,
79 .flags = I2C_M_RD,
80 .len = len,
81 .buf = values,
82 },
83 };
84 int ret;
Benson Leung6ddaf742013-02-13 13:56:03 -080085
Dudley Du9f1cd852015-01-17 18:35:26 -080086 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Benson Leung6ddaf742013-02-13 13:56:03 -080087
Dudley Du9f1cd852015-01-17 18:35:26 -080088 if (ret != ARRAY_SIZE(msgs))
89 return ret < 0 ? ret : -EIO;
Benson Leung6ddaf742013-02-13 13:56:03 -080090
Dudley Du9f1cd852015-01-17 18:35:26 -080091 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -080092}
93
Dudley Du9f1cd852015-01-17 18:35:26 -080094/**
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 */
103static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
104 size_t len, const void *values)
Benson Leungd7e34d12013-01-09 16:25:11 -0800105{
Dudley Du9f1cd852015-01-17 18:35:26 -0800106 struct i2c_client *client = cyapa->client;
107 char buf[32];
108 int ret;
Benson Leungd7e34d12013-01-09 16:25:11 -0800109
Dudley Du9f1cd852015-01-17 18:35:26 -0800110 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 Leungd7e34d12013-01-09 16:25:11 -0800121}
122
Dudley Du9f1cd852015-01-17 18:35:26 -0800123static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
Benson Leungd7e34d12013-01-09 16:25:11 -0800124{
Dudley Du9f1cd852015-01-17 18:35:26 -0800125 u8 ret = CYAPA_ADAPTER_FUNC_NONE;
Benson Leungd7e34d12013-01-09 16:25:11 -0800126
Dudley Du9f1cd852015-01-17 18:35:26 -0800127 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 Leungd7e34d12013-01-09 16:25:11 -0800134}
135
136/*
137 * Query device for its current operating state.
Benson Leungd7e34d12013-01-09 16:25:11 -0800138 */
139static int cyapa_get_state(struct cyapa *cyapa)
140{
Benson Leungd7e34d12013-01-09 16:25:11 -0800141 u8 status[BL_STATUS_SIZE];
Dudley Du9f1cd852015-01-17 18:35:26 -0800142 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 Du823a11f2014-12-04 07:00:03 -0800147 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800148
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 Du823a11f2014-12-04 07:00:03 -0800157 error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
Dudley Du9f1cd852015-01-17 18:35:26 -0800158 status);
Benson Leung6ddaf742013-02-13 13:56:03 -0800159
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 Du9f1cd852015-01-17 18:35:26 -0800165 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 Leung6ddaf742013-02-13 13:56:03 -0800171
Dudley Du823a11f2014-12-04 07:00:03 -0800172 if (error != BL_STATUS_SIZE)
Benson Leungd7e34d12013-01-09 16:25:11 -0800173 goto error;
174
Dudley Du9f1cd852015-01-17 18:35:26 -0800175 /*
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 Leungd7e34d12013-01-09 16:25:11 -0800182
Dudley Du9f1cd852015-01-17 18:35:26 -0800183 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 Du6972a852015-01-17 18:49:37 -0800190 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 Du9f1cd852015-01-17 18:35:26 -0800198
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
221out_detected:
222 if (cyapa->state <= CYAPA_STATE_BL_BUSY)
223 return -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800224 return 0;
Dudley Du9f1cd852015-01-17 18:35:26 -0800225
Benson Leungd7e34d12013-01-09 16:25:11 -0800226error:
Dudley Du823a11f2014-12-04 07:00:03 -0800227 return (error < 0) ? error : -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800228}
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 Du9f1cd852015-01-17 18:35:26 -0800242 * -EAGAIN if bootload is busy, or unknown state.
243 * < 0 other errors
Benson Leungd7e34d12013-01-09 16:25:11 -0800244 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800245int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
Benson Leungd7e34d12013-01-09 16:25:11 -0800246{
Dudley Du823a11f2014-12-04 07:00:03 -0800247 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800248 int tries = timeout / 100;
249
Dudley Du9f1cd852015-01-17 18:35:26 -0800250 do {
Dudley Du823a11f2014-12-04 07:00:03 -0800251 error = cyapa_get_state(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800252 if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
253 return 0;
254
255 msleep(100);
256 } while (tries--);
257
Dudley Du823a11f2014-12-04 07:00:03 -0800258 return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800259}
260
Benson Leungd7e34d12013-01-09 16:25:11 -0800261/*
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 Du9f1cd852015-01-17 18:35:26 -0800268 * -ENODEV no device
Benson Leungd7e34d12013-01-09 16:25:11 -0800269 * -EBUSY no device or in bootloader
270 * -EIO failure while reading from device
Dudley Du9f1cd852015-01-17 18:35:26 -0800271 * -ETIMEDOUT timeout failure for bus idle or bus no response
Benson Leungd7e34d12013-01-09 16:25:11 -0800272 * -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 */
277static int cyapa_check_is_operational(struct cyapa *cyapa)
278{
Dudley Du823a11f2014-12-04 07:00:03 -0800279 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800280
Dudley Du9f1cd852015-01-17 18:35:26 -0800281 error = cyapa_poll_state(cyapa, 4000);
Dudley Du823a11f2014-12-04 07:00:03 -0800282 if (error)
283 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800284
Dudley Du9f1cd852015-01-17 18:35:26 -0800285 switch (cyapa->gen) {
Dudley Du6972a852015-01-17 18:49:37 -0800286 case CYAPA_GEN5:
287 cyapa->ops = &cyapa_gen5_ops;
288 break;
Dudley Du9f1cd852015-01-17 18:35:26 -0800289 case CYAPA_GEN3:
290 cyapa->ops = &cyapa_gen3_ops;
291 break;
Benson Leungd7e34d12013-01-09 16:25:11 -0800292 default:
Dudley Du9f1cd852015-01-17 18:35:26 -0800293 return -ENODEV;
Benson Leungd7e34d12013-01-09 16:25:11 -0800294 }
Dudley Du9f1cd852015-01-17 18:35:26 -0800295
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 Leungd7e34d12013-01-09 16:25:11 -0800303}
304
Dudley Du9f1cd852015-01-17 18:35:26 -0800305
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 */
311static int cyapa_detect(struct cyapa *cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -0800312{
Benson Leungd7e34d12013-01-09 16:25:11 -0800313 struct device *dev = &cyapa->client->dev;
Dudley Du9f1cd852015-01-17 18:35:26 -0800314 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800315
Dudley Du9f1cd852015-01-17 18:35:26 -0800316 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 Leungd7e34d12013-01-09 16:25:11 -0800323
Dudley Du9f1cd852015-01-17 18:35:26 -0800324 dev_err(dev, "no device detected: %d\n", error);
325 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800326 }
327
Dudley Du9f1cd852015-01-17 18:35:26 -0800328 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -0800329}
330
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800331static 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 Du9f1cd852015-01-17 18:35:26 -0800337 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
338 if (error)
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800339 return error;
Dudley Du9f1cd852015-01-17 18:35:26 -0800340
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 Dub1cfa7b2014-11-09 12:36:34 -0800360 }
361
362 enable_irq(client->irq);
Dudley Du67286502015-01-17 18:57:42 -0800363 if (!pm_runtime_enabled(&client->dev)) {
364 pm_runtime_set_active(&client->dev);
365 pm_runtime_enable(&client->dev);
366 }
Dudley Du9f1cd852015-01-17 18:35:26 -0800367out:
368 mutex_unlock(&cyapa->state_sync_lock);
369 return error;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800370}
371
372static void cyapa_close(struct input_dev *input)
373{
374 struct cyapa *cyapa = input_get_drvdata(input);
Dudley Du9f1cd852015-01-17 18:35:26 -0800375 struct i2c_client *client = cyapa->client;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800376
Dudley Du9f1cd852015-01-17 18:35:26 -0800377 mutex_lock(&cyapa->state_sync_lock);
378
379 disable_irq(client->irq);
Dudley Du67286502015-01-17 18:57:42 -0800380 if (pm_runtime_enabled(&client->dev))
381 pm_runtime_disable(&client->dev);
382 pm_runtime_set_suspended(&client->dev);
383
Dudley Du9f1cd852015-01-17 18:35:26 -0800384 if (cyapa->operational)
385 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
386
387 mutex_unlock(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800388}
389
Benson Leungd7e34d12013-01-09 16:25:11 -0800390static int cyapa_create_input_dev(struct cyapa *cyapa)
391{
392 struct device *dev = &cyapa->client->dev;
Benson Leungd7e34d12013-01-09 16:25:11 -0800393 struct input_dev *input;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800394 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800395
396 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
397 return -EINVAL;
398
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800399 input = devm_input_allocate_device(dev);
Benson Leungd7e34d12013-01-09 16:25:11 -0800400 if (!input) {
Dudley Du823a11f2014-12-04 07:00:03 -0800401 dev_err(dev, "failed to allocate memory for input device.\n");
Benson Leungd7e34d12013-01-09 16:25:11 -0800402 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 Du823a11f2014-12-04 07:00:03 -0800409 input->id.product = 0; /* Means any product in eventcomm. */
Benson Leungd7e34d12013-01-09 16:25:11 -0800410 input->dev.parent = &cyapa->client->dev;
411
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800412 input->open = cyapa_open;
413 input->close = cyapa_close;
414
Benson Leungd7e34d12013-01-09 16:25:11 -0800415 input_set_drvdata(input, cyapa);
416
417 __set_bit(EV_ABS, input->evbit);
418
Dudley Du823a11f2014-12-04 07:00:03 -0800419 /* Finger position */
Benson Leungd7e34d12013-01-09 16:25:11 -0800420 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 Du9f1cd852015-01-17 18:35:26 -0800424 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 Leungd7e34d12013-01-09 16:25:11 -0800446
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 Du823a11f2014-12-04 07:00:03 -0800462 /* Handle pointer emulation and unused slots in core */
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800463 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 Leungd7e34d12013-01-09 16:25:11 -0800468 }
469
Dudley Du9f1cd852015-01-17 18:35:26 -0800470 /* 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 Dub1cfa7b2014-11-09 12:36:34 -0800477 cyapa->input = input;
Benson Leungd7e34d12013-01-09 16:25:11 -0800478 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800479}
480
Dudley Duc806b0b2015-01-17 22:07:12 -0800481static 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
501static 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 Du9f1cd852015-01-17 18:35:26 -0800513/*
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 */
527u8 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
536u16 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. */
545static 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 Du6972a852015-01-17 18:49:37 -0800563 if (!error)
564 error = cyapa_gen5_ops.initialize(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800565 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
579static 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 Du67286502015-01-17 18:57:42 -0800585 if (pm_runtime_enabled(dev))
586 pm_runtime_disable(dev);
587
Dudley Du9f1cd852015-01-17 18:35:26 -0800588 /* 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
605out:
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 Du67286502015-01-17 18:57:42 -0800610 } 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 Du9f1cd852015-01-17 18:35:26 -0800617 }
618
619 return error;
620}
621
622static 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 Du67286502015-01-17 18:57:42 -0800627 pm_runtime_get_sync(dev);
Dudley Du9f1cd852015-01-17 18:35:26 -0800628 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
657out:
Dudley Du67286502015-01-17 18:57:42 -0800658 pm_runtime_mark_last_busy(dev);
659 pm_runtime_put_sync_autosuspend(dev);
Dudley Du9f1cd852015-01-17 18:35:26 -0800660 return IRQ_HANDLED;
661}
662
Dudley Du22e7db82015-01-17 18:56:18 -0800663/*
664 **************************************************************
665 * sysfs interface
666 **************************************************************
667*/
668#ifdef CONFIG_PM_SLEEP
669static 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
708static 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
737static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
738 cyapa_show_suspend_scanrate,
739 cyapa_update_suspend_scanrate);
740
741static struct attribute *cyapa_power_wakeup_entries[] = {
742 &dev_attr_suspend_scanrate_ms.attr,
743 NULL,
744};
745
746static const struct attribute_group cyapa_power_wakeup_group = {
747 .name = power_group_name,
748 .attrs = cyapa_power_wakeup_entries,
749};
750
751static 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
759static 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
787static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
788{
789 return 0;
790}
791#endif /* CONFIG_PM_SLEEP */
792
Dudley Du67286502015-01-17 18:57:42 -0800793#ifdef CONFIG_PM
794static 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
818static 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
853static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
854 cyapa_show_rt_suspend_scanrate,
855 cyapa_update_rt_suspend_scanrate);
856
857static struct attribute *cyapa_power_runtime_entries[] = {
858 &dev_attr_runtime_suspend_scanrate_ms.attr,
859 NULL,
860};
861
862static const struct attribute_group cyapa_power_runtime_group = {
863 .name = power_group_name,
864 .attrs = cyapa_power_runtime_entries,
865};
866
867static 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
875static 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
908static inline int cyapa_start_runtime(struct cyapa *cyapa)
909{
910 return 0;
911}
912#endif /* CONFIG_PM */
913
Dudley Duc806b0b2015-01-17 22:07:12 -0800914static 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
929static 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
944static 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
998err_detect:
999 cyapa_disable_irq_for_cmd(cyapa);
1000 pm_runtime_put_noidle(dev);
1001
1002done:
1003 release_firmware(fw);
1004 return error;
1005}
1006
1007static 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
1067static 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
1090static 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
1112static 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
1131static 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
1149static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
1150static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
1151static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
1152static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
1153static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
1154static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
1155
1156static 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
1166static const struct attribute_group cyapa_sysfs_group = {
1167 .attrs = cyapa_sysfs_entries,
1168};
1169
1170static 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 Leungd7e34d12013-01-09 16:25:11 -08001177static int cyapa_probe(struct i2c_client *client,
1178 const struct i2c_device_id *dev_id)
1179{
Benson Leungd7e34d12013-01-09 16:25:11 -08001180 struct device *dev = &client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001181 struct cyapa *cyapa;
1182 u8 adapter_func;
Dudley Du9f1cd852015-01-17 18:35:26 -08001183 union i2c_smbus_data dummy;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001184 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001185
Benson Leung6ddaf742013-02-13 13:56:03 -08001186 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 Du9f1cd852015-01-17 18:35:26 -08001192 /* 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 Dub1cfa7b2014-11-09 12:36:34 -08001197 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
1198 if (!cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -08001199 return -ENOMEM;
Benson Leungd7e34d12013-01-09 16:25:11 -08001200
Benson Leung6ddaf742013-02-13 13:56:03 -08001201 /* i2c isn't supported, use smbus */
1202 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
1203 cyapa->smbus = true;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001204
Dudley Du9f1cd852015-01-17 18:35:26 -08001205 cyapa->client = client;
1206 i2c_set_clientdata(client, cyapa);
1207 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
1208 client->addr);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001209
Dudley Du9f1cd852015-01-17 18:35:26 -08001210 error = cyapa_initialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001211 if (error) {
Dudley Du9f1cd852015-01-17 18:35:26 -08001212 dev_err(dev, "failed to detect and initialize tp device.\n");
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001213 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001214 }
1215
Dudley Duc806b0b2015-01-17 22:07:12 -08001216 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 Du22e7db82015-01-17 18:56:18 -08001229 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 Du67286502015-01-17 18:57:42 -08001235 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 Dub1cfa7b2014-11-09 12:36:34 -08001241 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 Du823a11f2014-12-04 07:00:03 -08001246 dev_err(dev, "failed to request threaded irq: %d\n", error);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001247 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001248 }
1249
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001250 /* Disable IRQ until the device is opened */
1251 disable_irq(client->irq);
1252
Dudley Du9f1cd852015-01-17 18:35:26 -08001253 /*
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 Leungd7e34d12013-01-09 16:25:11 -08001265 }
1266
1267 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -08001268}
1269
Jingoo Han572081a2014-11-02 00:03:37 -07001270static int __maybe_unused cyapa_suspend(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -08001271{
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001272 struct i2c_client *client = to_i2c_client(dev);
1273 struct cyapa *cyapa = i2c_get_clientdata(client);
Benson Leungd7e34d12013-01-09 16:25:11 -08001274 u8 power_mode;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001275 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001276
Dudley Du9f1cd852015-01-17 18:35:26 -08001277 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001278 if (error)
1279 return error;
1280
Dudley Du67286502015-01-17 18:57:42 -08001281 /*
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 Dub1cfa7b2014-11-09 12:36:34 -08001288 disable_irq(client->irq);
Benson Leungd7e34d12013-01-09 16:25:11 -08001289
1290 /*
1291 * Set trackpad device to idle mode if wakeup is allowed,
1292 * otherwise turn off.
1293 */
Dudley Du9f1cd852015-01-17 18:35:26 -08001294 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 Leungd7e34d12013-01-09 16:25:11 -08001303
1304 if (device_may_wakeup(dev))
Dudley Duf68a95c2014-12-03 15:29:34 -08001305 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001306
Dudley Du9f1cd852015-01-17 18:35:26 -08001307 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001308 return 0;
1309}
1310
Jingoo Han572081a2014-11-02 00:03:37 -07001311static int __maybe_unused cyapa_resume(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -08001312{
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001313 struct i2c_client *client = to_i2c_client(dev);
1314 struct cyapa *cyapa = i2c_get_clientdata(client);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001315 int error;
1316
Dudley Du9f1cd852015-01-17 18:35:26 -08001317 mutex_lock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001318
Dudley Du9f1cd852015-01-17 18:35:26 -08001319 if (device_may_wakeup(dev) && cyapa->irq_wake) {
Dudley Duf68a95c2014-12-03 15:29:34 -08001320 disable_irq_wake(client->irq);
Dudley Du9f1cd852015-01-17 18:35:26 -08001321 cyapa->irq_wake = false;
1322 }
Benson Leungd7e34d12013-01-09 16:25:11 -08001323
Dudley Du67286502015-01-17 18:57:42 -08001324 /* Update device states and runtime PM states. */
Dudley Du9f1cd852015-01-17 18:35:26 -08001325 error = cyapa_reinitialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001326 if (error)
Dudley Du9f1cd852015-01-17 18:35:26 -08001327 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
Benson Leungd7e34d12013-01-09 16:25:11 -08001328
Dudley Duf68a95c2014-12-03 15:29:34 -08001329 enable_irq(client->irq);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001330
Dudley Du9f1cd852015-01-17 18:35:26 -08001331 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001332 return 0;
1333}
Benson Leungd7e34d12013-01-09 16:25:11 -08001334
Dudley Du67286502015-01-17 18:57:42 -08001335static 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
1349static 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
1361static 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 Leungd7e34d12013-01-09 16:25:11 -08001365
1366static const struct i2c_device_id cyapa_id_table[] = {
1367 { "cyapa", 0 },
1368 { },
1369};
1370MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
1371
1372static 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 Leungd7e34d12013-01-09 16:25:11 -08001380 .id_table = cyapa_id_table,
1381};
1382
1383module_i2c_driver(cyapa_driver);
1384
1385MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
1386MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
1387MODULE_LICENSE("GPL");