blob: 1479ca996647746cd453c075d244b4ad75fb28de [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 Du94897612015-07-20 16:49:06 -07009 * Copyright (C) 2011-2015 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>
Dudley Du36e96152015-07-30 11:19:18 -070024#include <linux/regulator/consumer.h>
Benson Leungd7e34d12013-01-09 16:25:11 -080025#include <linux/slab.h>
Dudley Du9f1cd852015-01-17 18:35:26 -080026#include <linux/uaccess.h>
Dudley Du67286502015-01-17 18:57:42 -080027#include <linux/pm_runtime.h>
Dudley Du7b2171d2015-01-17 22:18:59 -080028#include <linux/acpi.h>
Dudley Du9f1cd852015-01-17 18:35:26 -080029#include "cyapa.h"
Benson Leungd7e34d12013-01-09 16:25:11 -080030
Benson Leungd7e34d12013-01-09 16:25:11 -080031
Benson Leung6ddaf742013-02-13 13:56:03 -080032#define CYAPA_ADAPTER_FUNC_NONE 0
33#define CYAPA_ADAPTER_FUNC_I2C 1
34#define CYAPA_ADAPTER_FUNC_SMBUS 2
35#define CYAPA_ADAPTER_FUNC_BOTH 3
36
Dudley Duc806b0b2015-01-17 22:07:12 -080037#define CYAPA_FW_NAME "cyapa.bin"
38
Dudley Du9f1cd852015-01-17 18:35:26 -080039const char product_id[] = "CYTRA";
Benson Leung6ddaf742013-02-13 13:56:03 -080040
Dudley Du9f1cd852015-01-17 18:35:26 -080041static int cyapa_reinitialize(struct cyapa *cyapa);
Benson Leung6ddaf742013-02-13 13:56:03 -080042
Dudley Du94897612015-07-20 16:49:06 -070043bool cyapa_is_pip_bl_mode(struct cyapa *cyapa)
Dudley Du9f1cd852015-01-17 18:35:26 -080044{
Dudley Duc2c06c42015-07-20 16:53:30 -070045 if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_BL)
46 return true;
47
Dudley Du9f1cd852015-01-17 18:35:26 -080048 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL)
49 return true;
Benson Leung6ddaf742013-02-13 13:56:03 -080050
Dudley Du94897612015-07-20 16:49:06 -070051 return false;
52}
53
54bool cyapa_is_pip_app_mode(struct cyapa *cyapa)
55{
Dudley Duc2c06c42015-07-20 16:53:30 -070056 if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_APP)
57 return true;
58
Dudley Du94897612015-07-20 16:49:06 -070059 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP)
60 return true;
61
62 return false;
63}
64
65static bool cyapa_is_bootloader_mode(struct cyapa *cyapa)
66{
67 if (cyapa_is_pip_bl_mode(cyapa))
68 return true;
69
Dudley Du9f1cd852015-01-17 18:35:26 -080070 if (cyapa->gen == CYAPA_GEN3 &&
71 cyapa->state >= CYAPA_STATE_BL_BUSY &&
72 cyapa->state <= CYAPA_STATE_BL_ACTIVE)
73 return true;
Benson Leung6ddaf742013-02-13 13:56:03 -080074
Dudley Du9f1cd852015-01-17 18:35:26 -080075 return false;
76}
Benson Leung6ddaf742013-02-13 13:56:03 -080077
Dudley Du9f1cd852015-01-17 18:35:26 -080078static inline bool cyapa_is_operational_mode(struct cyapa *cyapa)
79{
Dudley Du94897612015-07-20 16:49:06 -070080 if (cyapa_is_pip_app_mode(cyapa))
Dudley Du9f1cd852015-01-17 18:35:26 -080081 return true;
Benson Leung6ddaf742013-02-13 13:56:03 -080082
Dudley Du9f1cd852015-01-17 18:35:26 -080083 if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
84 return true;
Benson Leungd7e34d12013-01-09 16:25:11 -080085
Dudley Du9f1cd852015-01-17 18:35:26 -080086 return false;
87}
Benson Leung6ddaf742013-02-13 13:56:03 -080088
Dudley Du9f1cd852015-01-17 18:35:26 -080089/* Returns 0 on success, else negative errno on failure. */
90static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
Benson Leungd7e34d12013-01-09 16:25:11 -080091 u8 *values)
92{
Benson Leung6ddaf742013-02-13 13:56:03 -080093 struct i2c_client *client = cyapa->client;
Dudley Du9f1cd852015-01-17 18:35:26 -080094 struct i2c_msg msgs[] = {
95 {
96 .addr = client->addr,
97 .flags = 0,
98 .len = 1,
99 .buf = &reg,
100 },
101 {
102 .addr = client->addr,
103 .flags = I2C_M_RD,
104 .len = len,
105 .buf = values,
106 },
107 };
108 int ret;
Benson Leung6ddaf742013-02-13 13:56:03 -0800109
Dudley Du9f1cd852015-01-17 18:35:26 -0800110 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Benson Leung6ddaf742013-02-13 13:56:03 -0800111
Dudley Du9f1cd852015-01-17 18:35:26 -0800112 if (ret != ARRAY_SIZE(msgs))
113 return ret < 0 ? ret : -EIO;
Benson Leung6ddaf742013-02-13 13:56:03 -0800114
Dudley Du9f1cd852015-01-17 18:35:26 -0800115 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -0800116}
117
Dudley Du9f1cd852015-01-17 18:35:26 -0800118/**
119 * cyapa_i2c_write - Execute i2c block data write operation
120 * @cyapa: Handle to this driver
121 * @ret: Offset of the data to written in the register map
122 * @len: number of bytes to write
123 * @values: Data to be written
124 *
125 * Return negative errno code on error; return zero when success.
126 */
127static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
128 size_t len, const void *values)
Benson Leungd7e34d12013-01-09 16:25:11 -0800129{
Dudley Du9f1cd852015-01-17 18:35:26 -0800130 struct i2c_client *client = cyapa->client;
131 char buf[32];
132 int ret;
Benson Leungd7e34d12013-01-09 16:25:11 -0800133
Dudley Du9f1cd852015-01-17 18:35:26 -0800134 if (len > sizeof(buf) - 1)
135 return -ENOMEM;
136
137 buf[0] = reg;
138 memcpy(&buf[1], values, len);
139
140 ret = i2c_master_send(client, buf, len + 1);
141 if (ret != len + 1)
142 return ret < 0 ? ret : -EIO;
143
144 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800145}
146
Dudley Du9f1cd852015-01-17 18:35:26 -0800147static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
Benson Leungd7e34d12013-01-09 16:25:11 -0800148{
Dudley Du9f1cd852015-01-17 18:35:26 -0800149 u8 ret = CYAPA_ADAPTER_FUNC_NONE;
Benson Leungd7e34d12013-01-09 16:25:11 -0800150
Dudley Du9f1cd852015-01-17 18:35:26 -0800151 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
152 ret |= CYAPA_ADAPTER_FUNC_I2C;
153 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
154 I2C_FUNC_SMBUS_BLOCK_DATA |
155 I2C_FUNC_SMBUS_I2C_BLOCK))
156 ret |= CYAPA_ADAPTER_FUNC_SMBUS;
157 return ret;
Benson Leungd7e34d12013-01-09 16:25:11 -0800158}
159
160/*
161 * Query device for its current operating state.
Benson Leungd7e34d12013-01-09 16:25:11 -0800162 */
163static int cyapa_get_state(struct cyapa *cyapa)
164{
Benson Leungd7e34d12013-01-09 16:25:11 -0800165 u8 status[BL_STATUS_SIZE];
Dudley Du9f1cd852015-01-17 18:35:26 -0800166 u8 cmd[32];
167 /* The i2c address of gen4 and gen5 trackpad device must be even. */
168 bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
169 bool smbus = false;
170 int retries = 2;
Dudley Du823a11f2014-12-04 07:00:03 -0800171 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800172
173 cyapa->state = CYAPA_STATE_NO_DEVICE;
174
175 /*
176 * Get trackpad status by reading 3 registers starting from 0.
177 * If the device is in the bootloader, this will be BL_HEAD.
178 * If the device is in operation mode, this will be the DATA regs.
179 *
180 */
Dudley Du823a11f2014-12-04 07:00:03 -0800181 error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
Dudley Du9f1cd852015-01-17 18:35:26 -0800182 status);
Benson Leung6ddaf742013-02-13 13:56:03 -0800183
184 /*
185 * On smbus systems in OP mode, the i2c_reg_read will fail with
186 * -ETIMEDOUT. In this case, try again using the smbus equivalent
187 * command. This should return a BL_HEAD indicating CYAPA_STATE_OP.
188 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800189 if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) {
190 if (!even_addr)
191 error = cyapa_read_block(cyapa,
192 CYAPA_CMD_BL_STATUS, status);
193 smbus = true;
194 }
Benson Leung6ddaf742013-02-13 13:56:03 -0800195
Dudley Du823a11f2014-12-04 07:00:03 -0800196 if (error != BL_STATUS_SIZE)
Benson Leungd7e34d12013-01-09 16:25:11 -0800197 goto error;
198
Dudley Du9f1cd852015-01-17 18:35:26 -0800199 /*
200 * Detect trackpad protocol based on characteristic registers and bits.
201 */
202 do {
203 cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
204 cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
205 cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
Benson Leungd7e34d12013-01-09 16:25:11 -0800206
Dudley Du9f1cd852015-01-17 18:35:26 -0800207 if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
208 cyapa->gen == CYAPA_GEN3) {
209 error = cyapa_gen3_ops.state_parse(cyapa,
210 status, BL_STATUS_SIZE);
211 if (!error)
212 goto out_detected;
213 }
Dudley Duc2c06c42015-07-20 16:53:30 -0700214 if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
215 cyapa->gen == CYAPA_GEN6 ||
216 cyapa->gen == CYAPA_GEN5) {
217 error = cyapa_pip_state_parse(cyapa,
218 status, BL_STATUS_SIZE);
219 if (!error)
220 goto out_detected;
221 }
222 /* For old Gen5 trackpads detecting. */
Dudley Du6972a852015-01-17 18:49:37 -0800223 if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
224 cyapa->gen == CYAPA_GEN5) &&
225 !smbus && even_addr) {
226 error = cyapa_gen5_ops.state_parse(cyapa,
227 status, BL_STATUS_SIZE);
228 if (!error)
229 goto out_detected;
230 }
Dudley Du9f1cd852015-01-17 18:35:26 -0800231
232 /*
233 * Write 0x00 0x00 to trackpad device to force update its
234 * status, then redo the detection again.
235 */
236 if (!smbus) {
237 cmd[0] = 0x00;
238 cmd[1] = 0x00;
239 error = cyapa_i2c_write(cyapa, 0, 2, cmd);
240 if (error)
241 goto error;
242
243 msleep(50);
244
245 error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
246 BL_STATUS_SIZE, status);
247 if (error)
248 goto error;
249 }
250 } while (--retries > 0 && !smbus);
251
252 goto error;
253
254out_detected:
255 if (cyapa->state <= CYAPA_STATE_BL_BUSY)
256 return -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800257 return 0;
Dudley Du9f1cd852015-01-17 18:35:26 -0800258
Benson Leungd7e34d12013-01-09 16:25:11 -0800259error:
Dudley Du823a11f2014-12-04 07:00:03 -0800260 return (error < 0) ? error : -EAGAIN;
Benson Leungd7e34d12013-01-09 16:25:11 -0800261}
262
263/*
264 * Poll device for its status in a loop, waiting up to timeout for a response.
265 *
266 * When the device switches state, it usually takes ~300 ms.
267 * However, when running a new firmware image, the device must calibrate its
268 * sensors, which can take as long as 2 seconds.
269 *
270 * Note: The timeout has granularity of the polling rate, which is 100 ms.
271 *
272 * Returns:
273 * 0 when the device eventually responds with a valid non-busy state.
274 * -ETIMEDOUT if device never responds (too many -EAGAIN)
Dudley Du9f1cd852015-01-17 18:35:26 -0800275 * -EAGAIN if bootload is busy, or unknown state.
276 * < 0 other errors
Benson Leungd7e34d12013-01-09 16:25:11 -0800277 */
Dudley Du9f1cd852015-01-17 18:35:26 -0800278int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
Benson Leungd7e34d12013-01-09 16:25:11 -0800279{
Dudley Du823a11f2014-12-04 07:00:03 -0800280 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800281 int tries = timeout / 100;
282
Dudley Du9f1cd852015-01-17 18:35:26 -0800283 do {
Dudley Du823a11f2014-12-04 07:00:03 -0800284 error = cyapa_get_state(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800285 if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
286 return 0;
287
288 msleep(100);
289 } while (tries--);
290
Dudley Du823a11f2014-12-04 07:00:03 -0800291 return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800292}
293
Benson Leungd7e34d12013-01-09 16:25:11 -0800294/*
295 * Check if device is operational.
296 *
297 * An operational device is responding, has exited bootloader, and has
298 * firmware supported by this driver.
299 *
300 * Returns:
Dudley Du9f1cd852015-01-17 18:35:26 -0800301 * -ENODEV no device
Benson Leungd7e34d12013-01-09 16:25:11 -0800302 * -EBUSY no device or in bootloader
303 * -EIO failure while reading from device
Dudley Du9f1cd852015-01-17 18:35:26 -0800304 * -ETIMEDOUT timeout failure for bus idle or bus no response
Benson Leungd7e34d12013-01-09 16:25:11 -0800305 * -EAGAIN device is still in bootloader
306 * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
307 * -EINVAL device is in operational mode, but not supported by this driver
308 * 0 device is supported
309 */
310static int cyapa_check_is_operational(struct cyapa *cyapa)
311{
Dudley Du823a11f2014-12-04 07:00:03 -0800312 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800313
Dudley Du9f1cd852015-01-17 18:35:26 -0800314 error = cyapa_poll_state(cyapa, 4000);
Dudley Du823a11f2014-12-04 07:00:03 -0800315 if (error)
316 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800317
Dudley Du9f1cd852015-01-17 18:35:26 -0800318 switch (cyapa->gen) {
Dudley Duc2c06c42015-07-20 16:53:30 -0700319 case CYAPA_GEN6:
320 cyapa->ops = &cyapa_gen6_ops;
321 break;
Dudley Du6972a852015-01-17 18:49:37 -0800322 case CYAPA_GEN5:
323 cyapa->ops = &cyapa_gen5_ops;
324 break;
Dudley Du9f1cd852015-01-17 18:35:26 -0800325 case CYAPA_GEN3:
326 cyapa->ops = &cyapa_gen3_ops;
327 break;
Benson Leungd7e34d12013-01-09 16:25:11 -0800328 default:
Dudley Du9f1cd852015-01-17 18:35:26 -0800329 return -ENODEV;
Benson Leungd7e34d12013-01-09 16:25:11 -0800330 }
Dudley Du9f1cd852015-01-17 18:35:26 -0800331
332 error = cyapa->ops->operational_check(cyapa);
333 if (!error && cyapa_is_operational_mode(cyapa))
334 cyapa->operational = true;
335 else
336 cyapa->operational = false;
337
338 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800339}
340
Dudley Du9f1cd852015-01-17 18:35:26 -0800341
342/*
343 * Returns 0 on device detected, negative errno on no device detected.
Dudley Du94897612015-07-20 16:49:06 -0700344 * And when the device is detected and operational, it will be reset to
Dudley Du9f1cd852015-01-17 18:35:26 -0800345 * full power active mode automatically.
346 */
347static int cyapa_detect(struct cyapa *cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -0800348{
Benson Leungd7e34d12013-01-09 16:25:11 -0800349 struct device *dev = &cyapa->client->dev;
Dudley Du9f1cd852015-01-17 18:35:26 -0800350 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800351
Dudley Du9f1cd852015-01-17 18:35:26 -0800352 error = cyapa_check_is_operational(cyapa);
353 if (error) {
354 if (error != -ETIMEDOUT && error != -ENODEV &&
355 cyapa_is_bootloader_mode(cyapa)) {
356 dev_warn(dev, "device detected but not operational\n");
357 return 0;
358 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800359
Dudley Du9f1cd852015-01-17 18:35:26 -0800360 dev_err(dev, "no device detected: %d\n", error);
361 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800362 }
363
Dudley Du9f1cd852015-01-17 18:35:26 -0800364 return 0;
Benson Leung6ddaf742013-02-13 13:56:03 -0800365}
366
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800367static int cyapa_open(struct input_dev *input)
368{
369 struct cyapa *cyapa = input_get_drvdata(input);
370 struct i2c_client *client = cyapa->client;
Dudley Du757cae52015-07-20 17:09:59 -0700371 struct device *dev = &client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800372 int error;
373
Dudley Du9f1cd852015-01-17 18:35:26 -0800374 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
375 if (error)
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800376 return error;
Dudley Du9f1cd852015-01-17 18:35:26 -0800377
378 if (cyapa->operational) {
379 /*
380 * though failed to set active power mode,
381 * but still may be able to work in lower scan rate
382 * when in operational mode.
383 */
384 error = cyapa->ops->set_power_mode(cyapa,
Dudley Du757cae52015-07-20 17:09:59 -0700385 PWR_MODE_FULL_ACTIVE, 0, false);
Dudley Du9f1cd852015-01-17 18:35:26 -0800386 if (error) {
Dudley Du757cae52015-07-20 17:09:59 -0700387 dev_warn(dev, "set active power failed: %d\n", error);
Dudley Du9f1cd852015-01-17 18:35:26 -0800388 goto out;
389 }
390 } else {
391 error = cyapa_reinitialize(cyapa);
392 if (error || !cyapa->operational) {
393 error = error ? error : -EAGAIN;
394 goto out;
395 }
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800396 }
397
398 enable_irq(client->irq);
Dudley Du757cae52015-07-20 17:09:59 -0700399 if (!pm_runtime_enabled(dev)) {
400 pm_runtime_set_active(dev);
401 pm_runtime_enable(dev);
Dudley Du67286502015-01-17 18:57:42 -0800402 }
Dudley Du757cae52015-07-20 17:09:59 -0700403
404 pm_runtime_get_sync(dev);
405 pm_runtime_mark_last_busy(dev);
406 pm_runtime_put_sync_autosuspend(dev);
Dudley Du9f1cd852015-01-17 18:35:26 -0800407out:
408 mutex_unlock(&cyapa->state_sync_lock);
409 return error;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800410}
411
412static void cyapa_close(struct input_dev *input)
413{
414 struct cyapa *cyapa = input_get_drvdata(input);
Dudley Du9f1cd852015-01-17 18:35:26 -0800415 struct i2c_client *client = cyapa->client;
Dudley Du757cae52015-07-20 17:09:59 -0700416 struct device *dev = &cyapa->client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800417
Dudley Du9f1cd852015-01-17 18:35:26 -0800418 mutex_lock(&cyapa->state_sync_lock);
419
420 disable_irq(client->irq);
Dudley Du757cae52015-07-20 17:09:59 -0700421 if (pm_runtime_enabled(dev))
422 pm_runtime_disable(dev);
423 pm_runtime_set_suspended(dev);
Dudley Du67286502015-01-17 18:57:42 -0800424
Dudley Du9f1cd852015-01-17 18:35:26 -0800425 if (cyapa->operational)
Dudley Du757cae52015-07-20 17:09:59 -0700426 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0, false);
Dudley Du9f1cd852015-01-17 18:35:26 -0800427
428 mutex_unlock(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800429}
430
Benson Leungd7e34d12013-01-09 16:25:11 -0800431static int cyapa_create_input_dev(struct cyapa *cyapa)
432{
433 struct device *dev = &cyapa->client->dev;
Benson Leungd7e34d12013-01-09 16:25:11 -0800434 struct input_dev *input;
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800435 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800436
437 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
438 return -EINVAL;
439
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800440 input = devm_input_allocate_device(dev);
Benson Leungd7e34d12013-01-09 16:25:11 -0800441 if (!input) {
Dudley Du823a11f2014-12-04 07:00:03 -0800442 dev_err(dev, "failed to allocate memory for input device.\n");
Benson Leungd7e34d12013-01-09 16:25:11 -0800443 return -ENOMEM;
444 }
445
446 input->name = CYAPA_NAME;
447 input->phys = cyapa->phys;
448 input->id.bustype = BUS_I2C;
449 input->id.version = 1;
Dudley Du823a11f2014-12-04 07:00:03 -0800450 input->id.product = 0; /* Means any product in eventcomm. */
Benson Leungd7e34d12013-01-09 16:25:11 -0800451 input->dev.parent = &cyapa->client->dev;
452
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800453 input->open = cyapa_open;
454 input->close = cyapa_close;
455
Benson Leungd7e34d12013-01-09 16:25:11 -0800456 input_set_drvdata(input, cyapa);
457
458 __set_bit(EV_ABS, input->evbit);
459
Dudley Du823a11f2014-12-04 07:00:03 -0800460 /* Finger position */
Benson Leungd7e34d12013-01-09 16:25:11 -0800461 input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
462 0);
463 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
464 0);
Dudley Du9f1cd852015-01-17 18:35:26 -0800465 input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
466 if (cyapa->gen > CYAPA_GEN3) {
467 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
468 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
469 /*
470 * Orientation is the angle between the vertical axis and
471 * the major axis of the contact ellipse.
472 * The range is -127 to 127.
473 * the positive direction is clockwise form the vertical axis.
474 * If the ellipse of contact degenerates into a circle,
475 * orientation is reported as 0.
476 *
477 * Also, for Gen5 trackpad the accurate of this orientation
478 * value is value + (-30 ~ 30).
479 */
480 input_set_abs_params(input, ABS_MT_ORIENTATION,
481 -127, 127, 0, 0);
482 }
483 if (cyapa->gen >= CYAPA_GEN5) {
484 input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
485 input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
Dudley Du945525e2015-07-20 16:57:53 -0700486 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
Dudley Du9f1cd852015-01-17 18:35:26 -0800487 }
Benson Leungd7e34d12013-01-09 16:25:11 -0800488
489 input_abs_set_res(input, ABS_MT_POSITION_X,
490 cyapa->max_abs_x / cyapa->physical_size_x);
491 input_abs_set_res(input, ABS_MT_POSITION_Y,
492 cyapa->max_abs_y / cyapa->physical_size_y);
493
494 if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
495 __set_bit(BTN_LEFT, input->keybit);
496 if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
497 __set_bit(BTN_MIDDLE, input->keybit);
498 if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
499 __set_bit(BTN_RIGHT, input->keybit);
500
501 if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
502 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
503
Dudley Du823a11f2014-12-04 07:00:03 -0800504 /* Handle pointer emulation and unused slots in core */
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800505 error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
506 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
507 if (error) {
508 dev_err(dev, "failed to initialize MT slots: %d\n", error);
509 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -0800510 }
511
Dudley Du9f1cd852015-01-17 18:35:26 -0800512 /* Register the device in input subsystem */
513 error = input_register_device(input);
514 if (error) {
515 dev_err(dev, "failed to register input device: %d\n", error);
516 return error;
517 }
518
Dudley Dub1cfa7b2014-11-09 12:36:34 -0800519 cyapa->input = input;
Benson Leungd7e34d12013-01-09 16:25:11 -0800520 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -0800521}
522
Dudley Duc806b0b2015-01-17 22:07:12 -0800523static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
524{
525 struct input_dev *input = cyapa->input;
526
527 if (!input || !input->users) {
528 /*
529 * When input is NULL, TP must be in deep sleep mode.
530 * In this mode, later non-power I2C command will always failed
531 * if not bring it out of deep sleep mode firstly,
532 * so must command TP to active mode here.
533 */
534 if (!input || cyapa->operational)
535 cyapa->ops->set_power_mode(cyapa,
Dudley Du757cae52015-07-20 17:09:59 -0700536 PWR_MODE_FULL_ACTIVE, 0, false);
Dudley Duc806b0b2015-01-17 22:07:12 -0800537 /* Gen3 always using polling mode for command. */
538 if (cyapa->gen >= CYAPA_GEN5)
539 enable_irq(cyapa->client->irq);
540 }
541}
542
543static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
544{
545 struct input_dev *input = cyapa->input;
546
547 if (!input || !input->users) {
548 if (cyapa->gen >= CYAPA_GEN5)
549 disable_irq(cyapa->client->irq);
550 if (!input || cyapa->operational)
Dudley Du757cae52015-07-20 17:09:59 -0700551 cyapa->ops->set_power_mode(cyapa,
552 PWR_MODE_OFF, 0, false);
Dudley Duc806b0b2015-01-17 22:07:12 -0800553 }
554}
555
Dudley Du9f1cd852015-01-17 18:35:26 -0800556/*
557 * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
558 *
559 * These are helper functions that convert to and from integer idle
560 * times and register settings to write to the PowerMode register.
561 * The trackpad supports between 20ms to 1000ms scan intervals.
562 * The time will be increased in increments of 10ms from 20ms to 100ms.
563 * From 100ms to 1000ms, time will be increased in increments of 20ms.
564 *
565 * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
566 * Idle_Command = Idle Time / 10;
567 * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
568 * Idle_Command = Idle Time / 20 + 5;
569 */
570u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
571{
572 u16 encoded_time;
573
574 sleep_time = clamp_val(sleep_time, 20, 1000);
575 encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
576 return (encoded_time << 2) & PWR_MODE_MASK;
577}
578
579u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
580{
581 u8 encoded_time = pwr_mode >> 2;
582
583 return (encoded_time < 10) ? encoded_time * 10
584 : (encoded_time - 5) * 20;
585}
586
587/* 0 on driver initialize and detected successfully, negative on failure. */
588static int cyapa_initialize(struct cyapa *cyapa)
589{
590 int error = 0;
591
592 cyapa->state = CYAPA_STATE_NO_DEVICE;
593 cyapa->gen = CYAPA_GEN_UNKNOWN;
594 mutex_init(&cyapa->state_sync_lock);
595
596 /*
597 * Set to hard code default, they will be updated with trackpad set
598 * default values after probe and initialized.
599 */
600 cyapa->suspend_power_mode = PWR_MODE_SLEEP;
601 cyapa->suspend_sleep_time =
602 cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
603
604 /* ops.initialize() is aimed to prepare for module communications. */
605 error = cyapa_gen3_ops.initialize(cyapa);
Dudley Du6972a852015-01-17 18:49:37 -0800606 if (!error)
607 error = cyapa_gen5_ops.initialize(cyapa);
Dudley Duc2c06c42015-07-20 16:53:30 -0700608 if (!error)
609 error = cyapa_gen6_ops.initialize(cyapa);
Dudley Du9f1cd852015-01-17 18:35:26 -0800610 if (error)
611 return error;
612
613 error = cyapa_detect(cyapa);
614 if (error)
615 return error;
616
617 /* Power down the device until we need it. */
618 if (cyapa->operational)
Dudley Du757cae52015-07-20 17:09:59 -0700619 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0, false);
Dudley Du9f1cd852015-01-17 18:35:26 -0800620
621 return 0;
622}
623
624static int cyapa_reinitialize(struct cyapa *cyapa)
625{
626 struct device *dev = &cyapa->client->dev;
627 struct input_dev *input = cyapa->input;
628 int error;
629
Dudley Du67286502015-01-17 18:57:42 -0800630 if (pm_runtime_enabled(dev))
631 pm_runtime_disable(dev);
632
Dudley Du9f1cd852015-01-17 18:35:26 -0800633 /* Avoid command failures when TP was in OFF state. */
634 if (cyapa->operational)
Dudley Du757cae52015-07-20 17:09:59 -0700635 cyapa->ops->set_power_mode(cyapa,
636 PWR_MODE_FULL_ACTIVE, 0, false);
Dudley Du9f1cd852015-01-17 18:35:26 -0800637
638 error = cyapa_detect(cyapa);
639 if (error)
640 goto out;
641
642 if (!input && cyapa->operational) {
643 error = cyapa_create_input_dev(cyapa);
644 if (error) {
645 dev_err(dev, "create input_dev instance failed: %d\n",
646 error);
647 goto out;
648 }
649 }
650
651out:
652 if (!input || !input->users) {
653 /* Reset to power OFF state to save power when no user open. */
654 if (cyapa->operational)
Dudley Du757cae52015-07-20 17:09:59 -0700655 cyapa->ops->set_power_mode(cyapa,
656 PWR_MODE_OFF, 0, false);
Dudley Du67286502015-01-17 18:57:42 -0800657 } else if (!error && cyapa->operational) {
658 /*
659 * Make sure only enable runtime PM when device is
660 * in operational mode and input->users > 0.
661 */
662 pm_runtime_set_active(dev);
663 pm_runtime_enable(dev);
Dudley Du757cae52015-07-20 17:09:59 -0700664
665 pm_runtime_get_sync(dev);
666 pm_runtime_mark_last_busy(dev);
667 pm_runtime_put_sync_autosuspend(dev);
Dudley Du9f1cd852015-01-17 18:35:26 -0800668 }
669
670 return error;
671}
672
673static irqreturn_t cyapa_irq(int irq, void *dev_id)
674{
675 struct cyapa *cyapa = dev_id;
676 struct device *dev = &cyapa->client->dev;
Dudley Du757cae52015-07-20 17:09:59 -0700677 int error;
Dudley Du9f1cd852015-01-17 18:35:26 -0800678
679 if (device_may_wakeup(dev))
680 pm_wakeup_event(dev, 0);
681
Dudley Du94897612015-07-20 16:49:06 -0700682 /* Interrupt event can be caused by host command to trackpad device. */
Dudley Du9f1cd852015-01-17 18:35:26 -0800683 if (cyapa->ops->irq_cmd_handler(cyapa)) {
684 /*
685 * Interrupt event maybe from trackpad device input reporting.
686 */
687 if (!cyapa->input) {
688 /*
Dudley Du94897612015-07-20 16:49:06 -0700689 * Still in probing or in firmware image
690 * updating or reading.
Dudley Du9f1cd852015-01-17 18:35:26 -0800691 */
692 cyapa->ops->sort_empty_output_data(cyapa,
693 NULL, NULL, NULL);
694 goto out;
695 }
696
Dudley Du757cae52015-07-20 17:09:59 -0700697 if (cyapa->operational) {
698 error = cyapa->ops->irq_handler(cyapa);
699
700 /*
701 * Apply runtime power management to touch report event
702 * except the events caused by the command responses.
703 * Note:
704 * It will introduce about 20~40 ms additional delay
705 * time in receiving for first valid touch report data.
706 * The time is used to execute device runtime resume
707 * process.
708 */
709 pm_runtime_get_sync(dev);
710 pm_runtime_mark_last_busy(dev);
711 pm_runtime_put_sync_autosuspend(dev);
712 }
713
714 if (!cyapa->operational || error) {
Dudley Du9f1cd852015-01-17 18:35:26 -0800715 if (!mutex_trylock(&cyapa->state_sync_lock)) {
716 cyapa->ops->sort_empty_output_data(cyapa,
717 NULL, NULL, NULL);
718 goto out;
719 }
720 cyapa_reinitialize(cyapa);
721 mutex_unlock(&cyapa->state_sync_lock);
722 }
723 }
724
725out:
726 return IRQ_HANDLED;
727}
728
Dudley Du22e7db82015-01-17 18:56:18 -0800729/*
730 **************************************************************
731 * sysfs interface
732 **************************************************************
733*/
734#ifdef CONFIG_PM_SLEEP
735static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
736 struct device_attribute *attr,
737 char *buf)
738{
739 struct cyapa *cyapa = dev_get_drvdata(dev);
740 u8 pwr_cmd = cyapa->suspend_power_mode;
741 u16 sleep_time;
742 int len;
743 int error;
744
745 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
746 if (error)
747 return error;
748
749 pwr_cmd = cyapa->suspend_power_mode;
750 sleep_time = cyapa->suspend_sleep_time;
751
752 mutex_unlock(&cyapa->state_sync_lock);
753
754 switch (pwr_cmd) {
755 case PWR_MODE_BTN_ONLY:
756 len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
757 break;
758
759 case PWR_MODE_OFF:
760 len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
761 break;
762
763 default:
764 len = scnprintf(buf, PAGE_SIZE, "%u\n",
765 cyapa->gen == CYAPA_GEN3 ?
766 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
767 sleep_time);
768 break;
769 }
770
771 return len;
772}
773
774static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
775 struct device_attribute *attr,
776 const char *buf, size_t count)
777{
778 struct cyapa *cyapa = dev_get_drvdata(dev);
779 u16 sleep_time;
780 int error;
781
782 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
783 if (error)
784 return error;
785
786 if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
787 cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
788 } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
789 cyapa->suspend_power_mode = PWR_MODE_OFF;
790 } else if (!kstrtou16(buf, 10, &sleep_time)) {
Dudley Dua333a032015-04-20 10:00:05 -0700791 cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
Dudley Du22e7db82015-01-17 18:56:18 -0800792 cyapa->suspend_power_mode =
793 cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
794 } else {
795 count = -EINVAL;
796 }
797
798 mutex_unlock(&cyapa->state_sync_lock);
799
800 return count;
801}
802
803static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
804 cyapa_show_suspend_scanrate,
805 cyapa_update_suspend_scanrate);
806
807static struct attribute *cyapa_power_wakeup_entries[] = {
808 &dev_attr_suspend_scanrate_ms.attr,
809 NULL,
810};
811
812static const struct attribute_group cyapa_power_wakeup_group = {
813 .name = power_group_name,
814 .attrs = cyapa_power_wakeup_entries,
815};
816
817static void cyapa_remove_power_wakeup_group(void *data)
818{
819 struct cyapa *cyapa = data;
820
821 sysfs_unmerge_group(&cyapa->client->dev.kobj,
822 &cyapa_power_wakeup_group);
823}
824
825static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
826{
827 struct i2c_client *client = cyapa->client;
828 struct device *dev = &client->dev;
829 int error;
830
831 if (device_can_wakeup(dev)) {
832 error = sysfs_merge_group(&client->dev.kobj,
833 &cyapa_power_wakeup_group);
834 if (error) {
835 dev_err(dev, "failed to add power wakeup group: %d\n",
836 error);
837 return error;
838 }
839
840 error = devm_add_action(dev,
841 cyapa_remove_power_wakeup_group, cyapa);
842 if (error) {
843 cyapa_remove_power_wakeup_group(cyapa);
844 dev_err(dev, "failed to add power cleanup action: %d\n",
845 error);
846 return error;
847 }
848 }
849
850 return 0;
851}
852#else
853static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
854{
855 return 0;
856}
857#endif /* CONFIG_PM_SLEEP */
858
Dudley Du67286502015-01-17 18:57:42 -0800859#ifdef CONFIG_PM
860static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
861 struct device_attribute *attr,
862 char *buf)
863{
864 struct cyapa *cyapa = dev_get_drvdata(dev);
865 u8 pwr_cmd;
866 u16 sleep_time;
867 int error;
868
869 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
870 if (error)
871 return error;
872
873 pwr_cmd = cyapa->runtime_suspend_power_mode;
874 sleep_time = cyapa->runtime_suspend_sleep_time;
875
876 mutex_unlock(&cyapa->state_sync_lock);
877
878 return scnprintf(buf, PAGE_SIZE, "%u\n",
879 cyapa->gen == CYAPA_GEN3 ?
880 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
881 sleep_time);
882}
883
884static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
887{
888 struct cyapa *cyapa = dev_get_drvdata(dev);
889 u16 time;
890 int error;
891
892 if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
893 dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
894 return -EINVAL;
895 }
896
897 /*
898 * When the suspend scanrate is changed, pm_runtime_get to resume
899 * a potentially suspended device, update to the new pwr_cmd
900 * and then pm_runtime_put to suspend into the new power mode.
901 */
902 pm_runtime_get_sync(dev);
903
904 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
905 if (error)
906 return error;
907
Dudley Dua333a032015-04-20 10:00:05 -0700908 cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
Dudley Du67286502015-01-17 18:57:42 -0800909 cyapa->runtime_suspend_power_mode =
910 cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
911
912 mutex_unlock(&cyapa->state_sync_lock);
913
914 pm_runtime_put_sync_autosuspend(dev);
915
916 return count;
917}
918
919static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
920 cyapa_show_rt_suspend_scanrate,
921 cyapa_update_rt_suspend_scanrate);
922
923static struct attribute *cyapa_power_runtime_entries[] = {
924 &dev_attr_runtime_suspend_scanrate_ms.attr,
925 NULL,
926};
927
928static const struct attribute_group cyapa_power_runtime_group = {
929 .name = power_group_name,
930 .attrs = cyapa_power_runtime_entries,
931};
932
933static void cyapa_remove_power_runtime_group(void *data)
934{
935 struct cyapa *cyapa = data;
936
937 sysfs_unmerge_group(&cyapa->client->dev.kobj,
938 &cyapa_power_runtime_group);
939}
940
941static int cyapa_start_runtime(struct cyapa *cyapa)
942{
943 struct device *dev = &cyapa->client->dev;
944 int error;
945
946 cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
947 cyapa->runtime_suspend_sleep_time =
948 cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
949
950 error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
951 if (error) {
952 dev_err(dev,
953 "failed to create power runtime group: %d\n", error);
954 return error;
955 }
956
957 error = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa);
958 if (error) {
959 cyapa_remove_power_runtime_group(cyapa);
960 dev_err(dev,
961 "failed to add power runtime cleanup action: %d\n",
962 error);
963 return error;
964 }
965
966 /* runtime is enabled until device is operational and opened. */
967 pm_runtime_set_suspended(dev);
968 pm_runtime_use_autosuspend(dev);
969 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
970
971 return 0;
972}
973#else
974static inline int cyapa_start_runtime(struct cyapa *cyapa)
975{
976 return 0;
977}
978#endif /* CONFIG_PM */
979
Dudley Duc806b0b2015-01-17 22:07:12 -0800980static ssize_t cyapa_show_fm_ver(struct device *dev,
981 struct device_attribute *attr, char *buf)
982{
983 int error;
984 struct cyapa *cyapa = dev_get_drvdata(dev);
985
986 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
987 if (error)
988 return error;
989 error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
990 cyapa->fw_min_ver);
991 mutex_unlock(&cyapa->state_sync_lock);
992 return error;
993}
994
995static ssize_t cyapa_show_product_id(struct device *dev,
996 struct device_attribute *attr, char *buf)
997{
998 struct cyapa *cyapa = dev_get_drvdata(dev);
999 int size;
1000 int error;
1001
1002 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1003 if (error)
1004 return error;
1005 size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
1006 mutex_unlock(&cyapa->state_sync_lock);
1007 return size;
1008}
1009
1010static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
1011{
1012 struct device *dev = &cyapa->client->dev;
1013 const struct firmware *fw;
1014 int error;
1015
1016 error = request_firmware(&fw, fw_name, dev);
1017 if (error) {
1018 dev_err(dev, "Could not load firmware from %s: %d\n",
1019 fw_name, error);
1020 return error;
1021 }
1022
1023 error = cyapa->ops->check_fw(cyapa, fw);
1024 if (error) {
1025 dev_err(dev, "Invalid CYAPA firmware image: %s\n",
1026 fw_name);
1027 goto done;
1028 }
1029
1030 /*
1031 * Resume the potentially suspended device because doing FW
1032 * update on a device not in the FULL mode has a chance to
1033 * fail.
1034 */
1035 pm_runtime_get_sync(dev);
1036
1037 /* Require IRQ support for firmware update commands. */
1038 cyapa_enable_irq_for_cmd(cyapa);
1039
1040 error = cyapa->ops->bl_enter(cyapa);
1041 if (error) {
1042 dev_err(dev, "bl_enter failed, %d\n", error);
1043 goto err_detect;
1044 }
1045
1046 error = cyapa->ops->bl_activate(cyapa);
1047 if (error) {
1048 dev_err(dev, "bl_activate failed, %d\n", error);
1049 goto err_detect;
1050 }
1051
1052 error = cyapa->ops->bl_initiate(cyapa, fw);
1053 if (error) {
1054 dev_err(dev, "bl_initiate failed, %d\n", error);
1055 goto err_detect;
1056 }
1057
1058 error = cyapa->ops->update_fw(cyapa, fw);
1059 if (error) {
1060 dev_err(dev, "update_fw failed, %d\n", error);
1061 goto err_detect;
1062 }
1063
1064err_detect:
1065 cyapa_disable_irq_for_cmd(cyapa);
1066 pm_runtime_put_noidle(dev);
1067
1068done:
1069 release_firmware(fw);
1070 return error;
1071}
1072
1073static ssize_t cyapa_update_fw_store(struct device *dev,
1074 struct device_attribute *attr,
1075 const char *buf, size_t count)
1076{
1077 struct cyapa *cyapa = dev_get_drvdata(dev);
1078 char fw_name[NAME_MAX];
1079 int ret, error;
1080
Dan Carpenterb4810772015-01-22 08:20:16 -08001081 if (count >= NAME_MAX) {
Dudley Duc806b0b2015-01-17 22:07:12 -08001082 dev_err(dev, "File name too long\n");
1083 return -EINVAL;
1084 }
1085
1086 memcpy(fw_name, buf, count);
1087 if (fw_name[count - 1] == '\n')
1088 fw_name[count - 1] = '\0';
1089 else
1090 fw_name[count] = '\0';
1091
1092 if (cyapa->input) {
1093 /*
1094 * Force the input device to be registered after the firmware
1095 * image is updated, so if the corresponding parameters updated
1096 * in the new firmware image can taken effect immediately.
1097 */
1098 input_unregister_device(cyapa->input);
1099 cyapa->input = NULL;
1100 }
1101
1102 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1103 if (error) {
1104 /*
1105 * Whatever, do reinitialize to try to recover TP state to
1106 * previous state just as it entered fw update entrance.
1107 */
1108 cyapa_reinitialize(cyapa);
1109 return error;
1110 }
1111
1112 error = cyapa_firmware(cyapa, fw_name);
1113 if (error)
1114 dev_err(dev, "firmware update failed: %d\n", error);
1115 else
1116 dev_dbg(dev, "firmware update successfully done.\n");
1117
1118 /*
Dudley Du94897612015-07-20 16:49:06 -07001119 * Re-detect trackpad device states because firmware update process
Dudley Duc806b0b2015-01-17 22:07:12 -08001120 * will reset trackpad device into bootloader mode.
1121 */
1122 ret = cyapa_reinitialize(cyapa);
1123 if (ret) {
Dudley Du94897612015-07-20 16:49:06 -07001124 dev_err(dev, "failed to re-detect after updated: %d\n", ret);
Dudley Duc806b0b2015-01-17 22:07:12 -08001125 error = error ? error : ret;
1126 }
1127
1128 mutex_unlock(&cyapa->state_sync_lock);
1129
1130 return error ? error : count;
1131}
1132
1133static ssize_t cyapa_calibrate_store(struct device *dev,
1134 struct device_attribute *attr,
1135 const char *buf, size_t count)
1136{
1137 struct cyapa *cyapa = dev_get_drvdata(dev);
1138 int error;
1139
1140 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1141 if (error)
1142 return error;
1143
1144 if (cyapa->operational) {
1145 cyapa_enable_irq_for_cmd(cyapa);
1146 error = cyapa->ops->calibrate_store(dev, attr, buf, count);
1147 cyapa_disable_irq_for_cmd(cyapa);
1148 } else {
1149 error = -EBUSY; /* Still running in bootloader mode. */
1150 }
1151
1152 mutex_unlock(&cyapa->state_sync_lock);
1153 return error < 0 ? error : count;
1154}
1155
1156static ssize_t cyapa_show_baseline(struct device *dev,
1157 struct device_attribute *attr, char *buf)
1158{
1159 struct cyapa *cyapa = dev_get_drvdata(dev);
1160 ssize_t error;
1161
1162 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1163 if (error)
1164 return error;
1165
1166 if (cyapa->operational) {
1167 cyapa_enable_irq_for_cmd(cyapa);
1168 error = cyapa->ops->show_baseline(dev, attr, buf);
1169 cyapa_disable_irq_for_cmd(cyapa);
1170 } else {
1171 error = -EBUSY; /* Still running in bootloader mode. */
1172 }
1173
1174 mutex_unlock(&cyapa->state_sync_lock);
1175 return error;
1176}
1177
1178static char *cyapa_state_to_string(struct cyapa *cyapa)
1179{
1180 switch (cyapa->state) {
1181 case CYAPA_STATE_BL_BUSY:
1182 return "bootloader busy";
1183 case CYAPA_STATE_BL_IDLE:
1184 return "bootloader idle";
1185 case CYAPA_STATE_BL_ACTIVE:
1186 return "bootloader active";
1187 case CYAPA_STATE_GEN5_BL:
Dudley Duc2c06c42015-07-20 16:53:30 -07001188 case CYAPA_STATE_GEN6_BL:
Dudley Duc806b0b2015-01-17 22:07:12 -08001189 return "bootloader";
1190 case CYAPA_STATE_OP:
1191 case CYAPA_STATE_GEN5_APP:
Dudley Duc2c06c42015-07-20 16:53:30 -07001192 case CYAPA_STATE_GEN6_APP:
Dudley Duc806b0b2015-01-17 22:07:12 -08001193 return "operational"; /* Normal valid state. */
1194 default:
1195 return "invalid mode";
1196 }
1197}
1198
1199static ssize_t cyapa_show_mode(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1201{
1202 struct cyapa *cyapa = dev_get_drvdata(dev);
1203 int size;
1204 int error;
1205
1206 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1207 if (error)
1208 return error;
1209
1210 size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
1211 cyapa->gen, cyapa_state_to_string(cyapa));
1212
1213 mutex_unlock(&cyapa->state_sync_lock);
1214 return size;
1215}
1216
1217static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
1218static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
1219static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
1220static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
1221static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
1222static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
1223
1224static struct attribute *cyapa_sysfs_entries[] = {
1225 &dev_attr_firmware_version.attr,
1226 &dev_attr_product_id.attr,
1227 &dev_attr_update_fw.attr,
1228 &dev_attr_baseline.attr,
1229 &dev_attr_calibrate.attr,
1230 &dev_attr_mode.attr,
1231 NULL,
1232};
1233
1234static const struct attribute_group cyapa_sysfs_group = {
1235 .attrs = cyapa_sysfs_entries,
1236};
1237
1238static void cyapa_remove_sysfs_group(void *data)
1239{
1240 struct cyapa *cyapa = data;
1241
1242 sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group);
1243}
1244
Dudley Du36e96152015-07-30 11:19:18 -07001245static void cyapa_disable_regulator(void *data)
1246{
1247 struct cyapa *cyapa = data;
1248
1249 regulator_disable(cyapa->vcc);
1250}
1251
Benson Leungd7e34d12013-01-09 16:25:11 -08001252static int cyapa_probe(struct i2c_client *client,
1253 const struct i2c_device_id *dev_id)
1254{
Benson Leungd7e34d12013-01-09 16:25:11 -08001255 struct device *dev = &client->dev;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001256 struct cyapa *cyapa;
1257 u8 adapter_func;
Dudley Du9f1cd852015-01-17 18:35:26 -08001258 union i2c_smbus_data dummy;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001259 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001260
Benson Leung6ddaf742013-02-13 13:56:03 -08001261 adapter_func = cyapa_check_adapter_functionality(client);
1262 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
1263 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1264 return -EIO;
1265 }
1266
Dudley Du9f1cd852015-01-17 18:35:26 -08001267 /* Make sure there is something at this address */
1268 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1269 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
1270 return -ENODEV;
1271
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001272 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
1273 if (!cyapa)
Benson Leungd7e34d12013-01-09 16:25:11 -08001274 return -ENOMEM;
Benson Leungd7e34d12013-01-09 16:25:11 -08001275
Benson Leung6ddaf742013-02-13 13:56:03 -08001276 /* i2c isn't supported, use smbus */
1277 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
1278 cyapa->smbus = true;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001279
Dudley Du9f1cd852015-01-17 18:35:26 -08001280 cyapa->client = client;
1281 i2c_set_clientdata(client, cyapa);
1282 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
1283 client->addr);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001284
Dudley Du36e96152015-07-30 11:19:18 -07001285 cyapa->vcc = devm_regulator_get(dev, "vcc");
1286 if (IS_ERR(cyapa->vcc)) {
1287 error = PTR_ERR(cyapa->vcc);
1288 dev_err(dev, "failed to get vcc regulator: %d\n", error);
1289 return error;
1290 }
1291
1292 error = regulator_enable(cyapa->vcc);
1293 if (error) {
1294 dev_err(dev, "failed to enable regulator: %d\n", error);
1295 return error;
1296 }
1297
1298 error = devm_add_action(dev, cyapa_disable_regulator, cyapa);
1299 if (error) {
1300 cyapa_disable_regulator(cyapa);
1301 dev_err(dev, "failed to add disable regulator action: %d\n",
1302 error);
1303 return error;
1304 }
1305
Dudley Du9f1cd852015-01-17 18:35:26 -08001306 error = cyapa_initialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001307 if (error) {
Dudley Du9f1cd852015-01-17 18:35:26 -08001308 dev_err(dev, "failed to detect and initialize tp device.\n");
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001309 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001310 }
1311
Dudley Duc806b0b2015-01-17 22:07:12 -08001312 error = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group);
1313 if (error) {
1314 dev_err(dev, "failed to create sysfs entries: %d\n", error);
1315 return error;
1316 }
1317
1318 error = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa);
1319 if (error) {
1320 cyapa_remove_sysfs_group(cyapa);
1321 dev_err(dev, "failed to add sysfs cleanup action: %d\n", error);
1322 return error;
1323 }
1324
Dudley Du22e7db82015-01-17 18:56:18 -08001325 error = cyapa_prepare_wakeup_controls(cyapa);
1326 if (error) {
1327 dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
1328 return error;
1329 }
1330
Dudley Du67286502015-01-17 18:57:42 -08001331 error = cyapa_start_runtime(cyapa);
1332 if (error) {
1333 dev_err(dev, "failed to start pm_runtime: %d\n", error);
1334 return error;
1335 }
1336
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001337 error = devm_request_threaded_irq(dev, client->irq,
1338 NULL, cyapa_irq,
1339 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1340 "cyapa", cyapa);
1341 if (error) {
Dudley Du823a11f2014-12-04 07:00:03 -08001342 dev_err(dev, "failed to request threaded irq: %d\n", error);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001343 return error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001344 }
1345
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001346 /* Disable IRQ until the device is opened */
1347 disable_irq(client->irq);
1348
Dudley Du9f1cd852015-01-17 18:35:26 -08001349 /*
1350 * Register the device in the input subsystem when it's operational.
1351 * Otherwise, keep in this driver, so it can be be recovered or updated
1352 * through the sysfs mode and update_fw interfaces by user or apps.
1353 */
1354 if (cyapa->operational) {
1355 error = cyapa_create_input_dev(cyapa);
1356 if (error) {
1357 dev_err(dev, "create input_dev instance failed: %d\n",
1358 error);
1359 return error;
1360 }
Benson Leungd7e34d12013-01-09 16:25:11 -08001361 }
1362
1363 return 0;
Benson Leungd7e34d12013-01-09 16:25:11 -08001364}
1365
Jingoo Han572081a2014-11-02 00:03:37 -07001366static int __maybe_unused cyapa_suspend(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -08001367{
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001368 struct i2c_client *client = to_i2c_client(dev);
1369 struct cyapa *cyapa = i2c_get_clientdata(client);
Benson Leungd7e34d12013-01-09 16:25:11 -08001370 u8 power_mode;
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001371 int error;
Benson Leungd7e34d12013-01-09 16:25:11 -08001372
Dudley Du9f1cd852015-01-17 18:35:26 -08001373 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001374 if (error)
1375 return error;
1376
Dudley Du67286502015-01-17 18:57:42 -08001377 /*
1378 * Runtime PM is enable only when device is in operational mode and
1379 * users in use, so need check it before disable it to
1380 * avoid unbalance warning.
1381 */
1382 if (pm_runtime_enabled(dev))
1383 pm_runtime_disable(dev);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001384 disable_irq(client->irq);
Benson Leungd7e34d12013-01-09 16:25:11 -08001385
1386 /*
1387 * Set trackpad device to idle mode if wakeup is allowed,
1388 * otherwise turn off.
1389 */
Dudley Du9f1cd852015-01-17 18:35:26 -08001390 if (cyapa->operational) {
1391 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
1392 : PWR_MODE_OFF;
1393 error = cyapa->ops->set_power_mode(cyapa, power_mode,
Dudley Du757cae52015-07-20 17:09:59 -07001394 cyapa->suspend_sleep_time, true);
Dudley Du9f1cd852015-01-17 18:35:26 -08001395 if (error)
1396 dev_err(dev, "suspend set power mode failed: %d\n",
1397 error);
1398 }
Benson Leungd7e34d12013-01-09 16:25:11 -08001399
Dudley Du945525e2015-07-20 16:57:53 -07001400 /*
1401 * Disable proximity interrupt when system idle, want true touch to
1402 * wake the system.
1403 */
1404 if (cyapa->dev_pwr_mode != PWR_MODE_OFF)
1405 cyapa->ops->set_proximity(cyapa, false);
1406
Benson Leungd7e34d12013-01-09 16:25:11 -08001407 if (device_may_wakeup(dev))
Dudley Duf68a95c2014-12-03 15:29:34 -08001408 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001409
Dudley Du9f1cd852015-01-17 18:35:26 -08001410 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001411 return 0;
1412}
1413
Jingoo Han572081a2014-11-02 00:03:37 -07001414static int __maybe_unused cyapa_resume(struct device *dev)
Benson Leungd7e34d12013-01-09 16:25:11 -08001415{
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001416 struct i2c_client *client = to_i2c_client(dev);
1417 struct cyapa *cyapa = i2c_get_clientdata(client);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001418 int error;
1419
Dudley Du9f1cd852015-01-17 18:35:26 -08001420 mutex_lock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001421
Dudley Du9f1cd852015-01-17 18:35:26 -08001422 if (device_may_wakeup(dev) && cyapa->irq_wake) {
Dudley Duf68a95c2014-12-03 15:29:34 -08001423 disable_irq_wake(client->irq);
Dudley Du9f1cd852015-01-17 18:35:26 -08001424 cyapa->irq_wake = false;
1425 }
Benson Leungd7e34d12013-01-09 16:25:11 -08001426
Dudley Du945525e2015-07-20 16:57:53 -07001427 /*
1428 * Update device states and runtime PM states.
1429 * Re-Enable proximity interrupt after enter operational mode.
1430 */
Dudley Du9f1cd852015-01-17 18:35:26 -08001431 error = cyapa_reinitialize(cyapa);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001432 if (error)
Dudley Du9f1cd852015-01-17 18:35:26 -08001433 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
Benson Leungd7e34d12013-01-09 16:25:11 -08001434
Dudley Duf68a95c2014-12-03 15:29:34 -08001435 enable_irq(client->irq);
Dudley Dub1cfa7b2014-11-09 12:36:34 -08001436
Dudley Du9f1cd852015-01-17 18:35:26 -08001437 mutex_unlock(&cyapa->state_sync_lock);
Benson Leungd7e34d12013-01-09 16:25:11 -08001438 return 0;
1439}
Benson Leungd7e34d12013-01-09 16:25:11 -08001440
Dudley Du67286502015-01-17 18:57:42 -08001441static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
1442{
1443 struct cyapa *cyapa = dev_get_drvdata(dev);
1444 int error;
1445
1446 error = cyapa->ops->set_power_mode(cyapa,
1447 cyapa->runtime_suspend_power_mode,
Dudley Du757cae52015-07-20 17:09:59 -07001448 cyapa->runtime_suspend_sleep_time,
1449 false);
Dudley Du67286502015-01-17 18:57:42 -08001450 if (error)
1451 dev_warn(dev, "runtime suspend failed: %d\n", error);
1452
1453 return 0;
1454}
1455
1456static int __maybe_unused cyapa_runtime_resume(struct device *dev)
1457{
1458 struct cyapa *cyapa = dev_get_drvdata(dev);
1459 int error;
1460
Dudley Du757cae52015-07-20 17:09:59 -07001461 error = cyapa->ops->set_power_mode(cyapa,
1462 PWR_MODE_FULL_ACTIVE, 0, false);
Dudley Du67286502015-01-17 18:57:42 -08001463 if (error)
1464 dev_warn(dev, "runtime resume failed: %d\n", error);
1465
1466 return 0;
1467}
1468
1469static const struct dev_pm_ops cyapa_pm_ops = {
1470 SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
1471 SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
1472};
Benson Leungd7e34d12013-01-09 16:25:11 -08001473
1474static const struct i2c_device_id cyapa_id_table[] = {
1475 { "cyapa", 0 },
1476 { },
1477};
1478MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
1479
Dudley Du7b2171d2015-01-17 22:18:59 -08001480#ifdef CONFIG_ACPI
1481static const struct acpi_device_id cyapa_acpi_id[] = {
1482 { "CYAP0000", 0 }, /* Gen3 trackpad with 0x67 I2C address. */
1483 { "CYAP0001", 0 }, /* Gen5 trackpad with 0x24 I2C address. */
Dudley Duce2ae9e2015-07-20 17:15:46 -07001484 { "CYAP0002", 0 }, /* Gen6 trackpad with 0x24 I2C address. */
Dudley Du7b2171d2015-01-17 22:18:59 -08001485 { }
1486};
1487MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
1488#endif
1489
Benson Leungd7e34d12013-01-09 16:25:11 -08001490static struct i2c_driver cyapa_driver = {
1491 .driver = {
1492 .name = "cyapa",
Benson Leungd7e34d12013-01-09 16:25:11 -08001493 .pm = &cyapa_pm_ops,
Dudley Du7b2171d2015-01-17 22:18:59 -08001494 .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
Benson Leungd7e34d12013-01-09 16:25:11 -08001495 },
1496
1497 .probe = cyapa_probe,
Benson Leungd7e34d12013-01-09 16:25:11 -08001498 .id_table = cyapa_id_table,
1499};
1500
1501module_i2c_driver(cyapa_driver);
1502
1503MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
1504MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
1505MODULE_LICENSE("GPL");