Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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