18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/hid.h>
88c2ecf20Sopenharmony_ci#include <linux/input.h>
98c2ecf20Sopenharmony_ci#include <linux/input/mt.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
128c2ecf20Sopenharmony_ci#include "hid-ids.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* ALPS Device Product ID */
158c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_T3_BTNLESS	0xD0C0
168c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_COSMO		0x1202
178c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_U1_PTP_1		0x1207
188c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_U1			0x1209
198c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_U1_PTP_2		0x120A
208c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_U1_DUAL		0x120B
218c2ecf20Sopenharmony_ci#define HID_PRODUCT_ID_T4_BTNLESS	0x120C
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DEV_SINGLEPOINT				0x01
248c2ecf20Sopenharmony_ci#define DEV_DUALPOINT				0x02
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define U1_MOUSE_REPORT_ID			0x01 /* Mouse data ReportID */
278c2ecf20Sopenharmony_ci#define U1_ABSOLUTE_REPORT_ID		0x03 /* Absolute data ReportID */
288c2ecf20Sopenharmony_ci#define U1_ABSOLUTE_REPORT_ID_SECD  0x02 /* FW-PTP Absolute data ReportID */
298c2ecf20Sopenharmony_ci#define U1_FEATURE_REPORT_ID		0x05 /* Feature ReportID */
308c2ecf20Sopenharmony_ci#define U1_SP_ABSOLUTE_REPORT_ID	0x06 /* Feature ReportID */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define U1_FEATURE_REPORT_LEN		0x08 /* Feature Report Length */
338c2ecf20Sopenharmony_ci#define U1_FEATURE_REPORT_LEN_ALL	0x0A
348c2ecf20Sopenharmony_ci#define U1_CMD_REGISTER_READ		0xD1
358c2ecf20Sopenharmony_ci#define U1_CMD_REGISTER_WRITE		0xD2
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define	U1_DEVTYPE_SP_SUPPORT		0x10 /* SP Support */
388c2ecf20Sopenharmony_ci#define	U1_DISABLE_DEV				0x01
398c2ecf20Sopenharmony_ci#define U1_TP_ABS_MODE				0x02
408c2ecf20Sopenharmony_ci#define	U1_SP_ABS_MODE				0x80
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define ADDRESS_U1_DEV_CTRL_1	0x00800040
438c2ecf20Sopenharmony_ci#define ADDRESS_U1_DEVICE_TYP	0x00800043
448c2ecf20Sopenharmony_ci#define ADDRESS_U1_NUM_SENS_X	0x00800047
458c2ecf20Sopenharmony_ci#define ADDRESS_U1_NUM_SENS_Y	0x00800048
468c2ecf20Sopenharmony_ci#define ADDRESS_U1_PITCH_SENS_X	0x00800049
478c2ecf20Sopenharmony_ci#define ADDRESS_U1_PITCH_SENS_Y	0x0080004A
488c2ecf20Sopenharmony_ci#define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
498c2ecf20Sopenharmony_ci#define ADDRESS_U1_PAD_BTN		0x00800052
508c2ecf20Sopenharmony_ci#define ADDRESS_U1_SP_BTN		0x0080009F
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define T4_INPUT_REPORT_LEN			sizeof(struct t4_input_report)
538c2ecf20Sopenharmony_ci#define T4_FEATURE_REPORT_LEN		T4_INPUT_REPORT_LEN
548c2ecf20Sopenharmony_ci#define T4_FEATURE_REPORT_ID		7
558c2ecf20Sopenharmony_ci#define T4_CMD_REGISTER_READ			0x08
568c2ecf20Sopenharmony_ci#define T4_CMD_REGISTER_WRITE			0x07
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define T4_ADDRESS_BASE				0xC2C0
598c2ecf20Sopenharmony_ci#define PRM_SYS_CONFIG_1			(T4_ADDRESS_BASE + 0x0002)
608c2ecf20Sopenharmony_ci#define T4_PRM_FEED_CONFIG_1		(T4_ADDRESS_BASE + 0x0004)
618c2ecf20Sopenharmony_ci#define T4_PRM_FEED_CONFIG_4		(T4_ADDRESS_BASE + 0x001A)
628c2ecf20Sopenharmony_ci#define T4_PRM_ID_CONFIG_3			(T4_ADDRESS_BASE + 0x00B0)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define T4_FEEDCFG4_ADVANCED_ABS_ENABLE			0x01
668c2ecf20Sopenharmony_ci#define T4_I2C_ABS	0x78
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define T4_COUNT_PER_ELECTRODE		256
698c2ecf20Sopenharmony_ci#define MAX_TOUCHES	5
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cienum dev_num {
728c2ecf20Sopenharmony_ci	U1,
738c2ecf20Sopenharmony_ci	T4,
748c2ecf20Sopenharmony_ci	UNKNOWN,
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci/**
778c2ecf20Sopenharmony_ci * struct u1_data
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * @input: pointer to the kernel input device
808c2ecf20Sopenharmony_ci * @input2: pointer to the kernel input2 device
818c2ecf20Sopenharmony_ci * @hdev: pointer to the struct hid_device
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * @dev_type: device type
848c2ecf20Sopenharmony_ci * @max_fingers: total number of fingers
858c2ecf20Sopenharmony_ci * @has_sp: boolean of sp existense
868c2ecf20Sopenharmony_ci * @sp_btn_info: button information
878c2ecf20Sopenharmony_ci * @x_active_len_mm: active area length of X (mm)
888c2ecf20Sopenharmony_ci * @y_active_len_mm: active area length of Y (mm)
898c2ecf20Sopenharmony_ci * @x_max: maximum x coordinate value
908c2ecf20Sopenharmony_ci * @y_max: maximum y coordinate value
918c2ecf20Sopenharmony_ci * @x_min: minimum x coordinate value
928c2ecf20Sopenharmony_ci * @y_min: minimum y coordinate value
938c2ecf20Sopenharmony_ci * @btn_cnt: number of buttons
948c2ecf20Sopenharmony_ci * @sp_btn_cnt: number of stick buttons
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistruct alps_dev {
978c2ecf20Sopenharmony_ci	struct input_dev *input;
988c2ecf20Sopenharmony_ci	struct input_dev *input2;
998c2ecf20Sopenharmony_ci	struct hid_device *hdev;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	enum dev_num dev_type;
1028c2ecf20Sopenharmony_ci	u8  max_fingers;
1038c2ecf20Sopenharmony_ci	u8  has_sp;
1048c2ecf20Sopenharmony_ci	u8	sp_btn_info;
1058c2ecf20Sopenharmony_ci	u32	x_active_len_mm;
1068c2ecf20Sopenharmony_ci	u32	y_active_len_mm;
1078c2ecf20Sopenharmony_ci	u32	x_max;
1088c2ecf20Sopenharmony_ci	u32	y_max;
1098c2ecf20Sopenharmony_ci	u32	x_min;
1108c2ecf20Sopenharmony_ci	u32	y_min;
1118c2ecf20Sopenharmony_ci	u32	btn_cnt;
1128c2ecf20Sopenharmony_ci	u32	sp_btn_cnt;
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistruct t4_contact_data {
1168c2ecf20Sopenharmony_ci	u8  palm;
1178c2ecf20Sopenharmony_ci	u8	x_lo;
1188c2ecf20Sopenharmony_ci	u8	x_hi;
1198c2ecf20Sopenharmony_ci	u8	y_lo;
1208c2ecf20Sopenharmony_ci	u8	y_hi;
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistruct t4_input_report {
1248c2ecf20Sopenharmony_ci	u8  reportID;
1258c2ecf20Sopenharmony_ci	u8  numContacts;
1268c2ecf20Sopenharmony_ci	struct t4_contact_data contact[5];
1278c2ecf20Sopenharmony_ci	u8  button;
1288c2ecf20Sopenharmony_ci	u8  track[5];
1298c2ecf20Sopenharmony_ci	u8  zx[5], zy[5];
1308c2ecf20Sopenharmony_ci	u8  palmTime[5];
1318c2ecf20Sopenharmony_ci	u8  kilroy;
1328c2ecf20Sopenharmony_ci	u16 timeStamp;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic u16 t4_calc_check_sum(u8 *buffer,
1368c2ecf20Sopenharmony_ci		unsigned long offset, unsigned long length)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	u16 sum1 = 0xFF, sum2 = 0xFF;
1398c2ecf20Sopenharmony_ci	unsigned long i = 0;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (offset + length >= 50)
1428c2ecf20Sopenharmony_ci		return 0;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	while (length > 0) {
1458c2ecf20Sopenharmony_ci		u32 tlen = length > 20 ? 20 : length;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		length -= tlen;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		do {
1508c2ecf20Sopenharmony_ci			sum1 += buffer[offset + i];
1518c2ecf20Sopenharmony_ci			sum2 += sum1;
1528c2ecf20Sopenharmony_ci			i++;
1538c2ecf20Sopenharmony_ci		} while (--tlen > 0);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		sum1 = (sum1 & 0xFF) + (sum1 >> 8);
1568c2ecf20Sopenharmony_ci		sum2 = (sum2 & 0xFF) + (sum2 >> 8);
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	sum1 = (sum1 & 0xFF) + (sum1 >> 8);
1608c2ecf20Sopenharmony_ci	sum2 = (sum2 & 0xFF) + (sum2 >> 8);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return(sum2 << 8 | sum1);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int t4_read_write_register(struct hid_device *hdev, u32 address,
1668c2ecf20Sopenharmony_ci	u8 *read_val, u8 write_val, bool read_flag)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	int ret;
1698c2ecf20Sopenharmony_ci	u16 check_sum;
1708c2ecf20Sopenharmony_ci	u8 *input;
1718c2ecf20Sopenharmony_ci	u8 *readbuf = NULL;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
1748c2ecf20Sopenharmony_ci	if (!input)
1758c2ecf20Sopenharmony_ci		return -ENOMEM;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	input[0] = T4_FEATURE_REPORT_ID;
1788c2ecf20Sopenharmony_ci	if (read_flag) {
1798c2ecf20Sopenharmony_ci		input[1] = T4_CMD_REGISTER_READ;
1808c2ecf20Sopenharmony_ci		input[8] = 0x00;
1818c2ecf20Sopenharmony_ci	} else {
1828c2ecf20Sopenharmony_ci		input[1] = T4_CMD_REGISTER_WRITE;
1838c2ecf20Sopenharmony_ci		input[8] = write_val;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci	put_unaligned_le32(address, input + 2);
1868c2ecf20Sopenharmony_ci	input[6] = 1;
1878c2ecf20Sopenharmony_ci	input[7] = 0;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	/* Calculate the checksum */
1908c2ecf20Sopenharmony_ci	check_sum = t4_calc_check_sum(input, 1, 8);
1918c2ecf20Sopenharmony_ci	input[9] = (u8)check_sum;
1928c2ecf20Sopenharmony_ci	input[10] = (u8)(check_sum >> 8);
1938c2ecf20Sopenharmony_ci	input[11] = 0;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
1968c2ecf20Sopenharmony_ci			T4_FEATURE_REPORT_LEN,
1978c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (ret < 0) {
2008c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
2018c2ecf20Sopenharmony_ci		goto exit;
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (read_flag) {
2058c2ecf20Sopenharmony_ci		readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
2068c2ecf20Sopenharmony_ci		if (!readbuf) {
2078c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2088c2ecf20Sopenharmony_ci			goto exit;
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
2128c2ecf20Sopenharmony_ci				T4_FEATURE_REPORT_LEN,
2138c2ecf20Sopenharmony_ci				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
2148c2ecf20Sopenharmony_ci		if (ret < 0) {
2158c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
2168c2ecf20Sopenharmony_ci			goto exit_readbuf;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci		ret = -EINVAL;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		if (*(u32 *)&readbuf[6] != address) {
2228c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "read register address error (%x,%x)\n",
2238c2ecf20Sopenharmony_ci				*(u32 *)&readbuf[6], address);
2248c2ecf20Sopenharmony_ci			goto exit_readbuf;
2258c2ecf20Sopenharmony_ci		}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci		if (*(u16 *)&readbuf[10] != 1) {
2288c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "read register size error (%x)\n",
2298c2ecf20Sopenharmony_ci				*(u16 *)&readbuf[10]);
2308c2ecf20Sopenharmony_ci			goto exit_readbuf;
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		check_sum = t4_calc_check_sum(readbuf, 6, 7);
2348c2ecf20Sopenharmony_ci		if (*(u16 *)&readbuf[13] != check_sum) {
2358c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
2368c2ecf20Sopenharmony_ci				*(u16 *)&readbuf[13], check_sum);
2378c2ecf20Sopenharmony_ci			goto exit_readbuf;
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		*read_val = readbuf[12];
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = 0;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ciexit_readbuf:
2468c2ecf20Sopenharmony_ci	kfree(readbuf);
2478c2ecf20Sopenharmony_ciexit:
2488c2ecf20Sopenharmony_ci	kfree(input);
2498c2ecf20Sopenharmony_ci	return ret;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic int u1_read_write_register(struct hid_device *hdev, u32 address,
2538c2ecf20Sopenharmony_ci	u8 *read_val, u8 write_val, bool read_flag)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	int ret, i;
2568c2ecf20Sopenharmony_ci	u8 check_sum;
2578c2ecf20Sopenharmony_ci	u8 *input;
2588c2ecf20Sopenharmony_ci	u8 *readbuf;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
2618c2ecf20Sopenharmony_ci	if (!input)
2628c2ecf20Sopenharmony_ci		return -ENOMEM;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	input[0] = U1_FEATURE_REPORT_ID;
2658c2ecf20Sopenharmony_ci	if (read_flag) {
2668c2ecf20Sopenharmony_ci		input[1] = U1_CMD_REGISTER_READ;
2678c2ecf20Sopenharmony_ci		input[6] = 0x00;
2688c2ecf20Sopenharmony_ci	} else {
2698c2ecf20Sopenharmony_ci		input[1] = U1_CMD_REGISTER_WRITE;
2708c2ecf20Sopenharmony_ci		input[6] = write_val;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	put_unaligned_le32(address, input + 2);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* Calculate the checksum */
2768c2ecf20Sopenharmony_ci	check_sum = U1_FEATURE_REPORT_LEN_ALL;
2778c2ecf20Sopenharmony_ci	for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
2788c2ecf20Sopenharmony_ci		check_sum += input[i];
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	input[7] = check_sum;
2818c2ecf20Sopenharmony_ci	ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
2828c2ecf20Sopenharmony_ci			U1_FEATURE_REPORT_LEN,
2838c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (ret < 0) {
2868c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
2878c2ecf20Sopenharmony_ci		goto exit;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (read_flag) {
2918c2ecf20Sopenharmony_ci		readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
2928c2ecf20Sopenharmony_ci		if (!readbuf) {
2938c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2948c2ecf20Sopenharmony_ci			goto exit;
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
2988c2ecf20Sopenharmony_ci				U1_FEATURE_REPORT_LEN,
2998c2ecf20Sopenharmony_ci				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		if (ret < 0) {
3028c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
3038c2ecf20Sopenharmony_ci			kfree(readbuf);
3048c2ecf20Sopenharmony_ci			goto exit;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		*read_val = readbuf[6];
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci		kfree(readbuf);
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	ret = 0;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ciexit:
3158c2ecf20Sopenharmony_ci	kfree(input);
3168c2ecf20Sopenharmony_ci	return ret;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	unsigned int x, y, z;
3228c2ecf20Sopenharmony_ci	int i;
3238c2ecf20Sopenharmony_ci	struct t4_input_report *p_report = (struct t4_input_report *)data;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (!data)
3268c2ecf20Sopenharmony_ci		return 0;
3278c2ecf20Sopenharmony_ci	for (i = 0; i < hdata->max_fingers; i++) {
3288c2ecf20Sopenharmony_ci		x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
3298c2ecf20Sopenharmony_ci		y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
3308c2ecf20Sopenharmony_ci		y = hdata->y_max - y + hdata->y_min;
3318c2ecf20Sopenharmony_ci		z = (p_report->contact[i].palm < 0x80 &&
3328c2ecf20Sopenharmony_ci			p_report->contact[i].palm > 0) * 62;
3338c2ecf20Sopenharmony_ci		if (x == 0xffff) {
3348c2ecf20Sopenharmony_ci			x = 0;
3358c2ecf20Sopenharmony_ci			y = 0;
3368c2ecf20Sopenharmony_ci			z = 0;
3378c2ecf20Sopenharmony_ci		}
3388c2ecf20Sopenharmony_ci		input_mt_slot(hdata->input, i);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		input_mt_report_slot_state(hdata->input,
3418c2ecf20Sopenharmony_ci			MT_TOOL_FINGER, z != 0);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci		if (!z)
3448c2ecf20Sopenharmony_ci			continue;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci		input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
3478c2ecf20Sopenharmony_ci		input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
3488c2ecf20Sopenharmony_ci		input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci	input_mt_sync_frame(hdata->input);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	input_report_key(hdata->input, BTN_LEFT, p_report->button);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	input_sync(hdata->input);
3558c2ecf20Sopenharmony_ci	return 1;
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	unsigned int x, y, z;
3618c2ecf20Sopenharmony_ci	int i;
3628c2ecf20Sopenharmony_ci	short sp_x, sp_y;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	if (!data)
3658c2ecf20Sopenharmony_ci		return 0;
3668c2ecf20Sopenharmony_ci	switch (data[0]) {
3678c2ecf20Sopenharmony_ci	case U1_MOUSE_REPORT_ID:
3688c2ecf20Sopenharmony_ci		break;
3698c2ecf20Sopenharmony_ci	case U1_FEATURE_REPORT_ID:
3708c2ecf20Sopenharmony_ci		break;
3718c2ecf20Sopenharmony_ci	case U1_ABSOLUTE_REPORT_ID:
3728c2ecf20Sopenharmony_ci	case U1_ABSOLUTE_REPORT_ID_SECD:
3738c2ecf20Sopenharmony_ci		for (i = 0; i < hdata->max_fingers; i++) {
3748c2ecf20Sopenharmony_ci			u8 *contact = &data[i * 5];
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci			x = get_unaligned_le16(contact + 3);
3778c2ecf20Sopenharmony_ci			y = get_unaligned_le16(contact + 5);
3788c2ecf20Sopenharmony_ci			z = contact[7] & 0x7F;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci			input_mt_slot(hdata->input, i);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci			if (z != 0) {
3838c2ecf20Sopenharmony_ci				input_mt_report_slot_state(hdata->input,
3848c2ecf20Sopenharmony_ci					MT_TOOL_FINGER, 1);
3858c2ecf20Sopenharmony_ci				input_report_abs(hdata->input,
3868c2ecf20Sopenharmony_ci					ABS_MT_POSITION_X, x);
3878c2ecf20Sopenharmony_ci				input_report_abs(hdata->input,
3888c2ecf20Sopenharmony_ci					ABS_MT_POSITION_Y, y);
3898c2ecf20Sopenharmony_ci				input_report_abs(hdata->input,
3908c2ecf20Sopenharmony_ci					ABS_MT_PRESSURE, z);
3918c2ecf20Sopenharmony_ci			} else {
3928c2ecf20Sopenharmony_ci				input_mt_report_slot_inactive(hdata->input);
3938c2ecf20Sopenharmony_ci			}
3948c2ecf20Sopenharmony_ci		}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		input_mt_sync_frame(hdata->input);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		input_report_key(hdata->input, BTN_LEFT,
3998c2ecf20Sopenharmony_ci			data[1] & 0x1);
4008c2ecf20Sopenharmony_ci		input_report_key(hdata->input, BTN_RIGHT,
4018c2ecf20Sopenharmony_ci			(data[1] & 0x2));
4028c2ecf20Sopenharmony_ci		input_report_key(hdata->input, BTN_MIDDLE,
4038c2ecf20Sopenharmony_ci			(data[1] & 0x4));
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		input_sync(hdata->input);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci		return 1;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	case U1_SP_ABSOLUTE_REPORT_ID:
4108c2ecf20Sopenharmony_ci		sp_x = get_unaligned_le16(data+2);
4118c2ecf20Sopenharmony_ci		sp_y = get_unaligned_le16(data+4);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci		sp_x = sp_x / 8;
4148c2ecf20Sopenharmony_ci		sp_y = sp_y / 8;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		input_report_rel(hdata->input2, REL_X, sp_x);
4178c2ecf20Sopenharmony_ci		input_report_rel(hdata->input2, REL_Y, sp_y);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		input_report_key(hdata->input2, BTN_LEFT,
4208c2ecf20Sopenharmony_ci			data[1] & 0x1);
4218c2ecf20Sopenharmony_ci		input_report_key(hdata->input2, BTN_RIGHT,
4228c2ecf20Sopenharmony_ci			(data[1] & 0x2));
4238c2ecf20Sopenharmony_ci		input_report_key(hdata->input2, BTN_MIDDLE,
4248c2ecf20Sopenharmony_ci			(data[1] & 0x4));
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci		input_sync(hdata->input2);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		return 1;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return 0;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic int alps_raw_event(struct hid_device *hdev,
4358c2ecf20Sopenharmony_ci		struct hid_report *report, u8 *data, int size)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	int ret = 0;
4388c2ecf20Sopenharmony_ci	struct alps_dev *hdata = hid_get_drvdata(hdev);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	switch (hdev->product) {
4418c2ecf20Sopenharmony_ci	case HID_PRODUCT_ID_T4_BTNLESS:
4428c2ecf20Sopenharmony_ci		ret = t4_raw_event(hdata, data, size);
4438c2ecf20Sopenharmony_ci		break;
4448c2ecf20Sopenharmony_ci	default:
4458c2ecf20Sopenharmony_ci		ret = u1_raw_event(hdata, data, size);
4468c2ecf20Sopenharmony_ci		break;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci	return ret;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic int __maybe_unused alps_post_reset(struct hid_device *hdev)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	int ret = -1;
4548c2ecf20Sopenharmony_ci	struct alps_dev *data = hid_get_drvdata(hdev);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	switch (data->dev_type) {
4578c2ecf20Sopenharmony_ci	case T4:
4588c2ecf20Sopenharmony_ci		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
4598c2ecf20Sopenharmony_ci			NULL, T4_I2C_ABS, false);
4608c2ecf20Sopenharmony_ci		if (ret < 0) {
4618c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
4628c2ecf20Sopenharmony_ci				ret);
4638c2ecf20Sopenharmony_ci			goto exit;
4648c2ecf20Sopenharmony_ci		}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
4678c2ecf20Sopenharmony_ci			NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
4688c2ecf20Sopenharmony_ci		if (ret < 0) {
4698c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
4708c2ecf20Sopenharmony_ci				ret);
4718c2ecf20Sopenharmony_ci			goto exit;
4728c2ecf20Sopenharmony_ci		}
4738c2ecf20Sopenharmony_ci		break;
4748c2ecf20Sopenharmony_ci	case U1:
4758c2ecf20Sopenharmony_ci		ret = u1_read_write_register(hdev,
4768c2ecf20Sopenharmony_ci			ADDRESS_U1_DEV_CTRL_1, NULL,
4778c2ecf20Sopenharmony_ci			U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
4788c2ecf20Sopenharmony_ci		if (ret < 0) {
4798c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
4808c2ecf20Sopenharmony_ci				ret);
4818c2ecf20Sopenharmony_ci			goto exit;
4828c2ecf20Sopenharmony_ci		}
4838c2ecf20Sopenharmony_ci		break;
4848c2ecf20Sopenharmony_ci	default:
4858c2ecf20Sopenharmony_ci		break;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ciexit:
4898c2ecf20Sopenharmony_ci	return ret;
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int __maybe_unused alps_post_resume(struct hid_device *hdev)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	return alps_post_reset(hdev);
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	int ret;
5008c2ecf20Sopenharmony_ci	u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
5018c2ecf20Sopenharmony_ci	u8 pitch_x, pitch_y, resolution;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	/* Device initialization */
5048c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5058c2ecf20Sopenharmony_ci			&dev_ctrl, 0, true);
5068c2ecf20Sopenharmony_ci	if (ret < 0) {
5078c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
5088c2ecf20Sopenharmony_ci		goto exit;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	dev_ctrl &= ~U1_DISABLE_DEV;
5128c2ecf20Sopenharmony_ci	dev_ctrl |= U1_TP_ABS_MODE;
5138c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5148c2ecf20Sopenharmony_ci			NULL, dev_ctrl, false);
5158c2ecf20Sopenharmony_ci	if (ret < 0) {
5168c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
5178c2ecf20Sopenharmony_ci		goto exit;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
5218c2ecf20Sopenharmony_ci			&sen_line_num_x, 0, true);
5228c2ecf20Sopenharmony_ci	if (ret < 0) {
5238c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
5248c2ecf20Sopenharmony_ci		goto exit;
5258c2ecf20Sopenharmony_ci	}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
5288c2ecf20Sopenharmony_ci			&sen_line_num_y, 0, true);
5298c2ecf20Sopenharmony_ci	if (ret < 0) {
5308c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
5318c2ecf20Sopenharmony_ci		goto exit;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
5358c2ecf20Sopenharmony_ci			&pitch_x, 0, true);
5368c2ecf20Sopenharmony_ci	if (ret < 0) {
5378c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
5388c2ecf20Sopenharmony_ci		goto exit;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
5428c2ecf20Sopenharmony_ci			&pitch_y, 0, true);
5438c2ecf20Sopenharmony_ci	if (ret < 0) {
5448c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
5458c2ecf20Sopenharmony_ci		goto exit;
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
5498c2ecf20Sopenharmony_ci		&resolution, 0, true);
5508c2ecf20Sopenharmony_ci	if (ret < 0) {
5518c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
5528c2ecf20Sopenharmony_ci		goto exit;
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci	pri_data->x_active_len_mm =
5558c2ecf20Sopenharmony_ci		(pitch_x * (sen_line_num_x - 1)) / 10;
5568c2ecf20Sopenharmony_ci	pri_data->y_active_len_mm =
5578c2ecf20Sopenharmony_ci		(pitch_y * (sen_line_num_y - 1)) / 10;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	pri_data->x_max =
5608c2ecf20Sopenharmony_ci		(resolution << 2) * (sen_line_num_x - 1);
5618c2ecf20Sopenharmony_ci	pri_data->x_min = 1;
5628c2ecf20Sopenharmony_ci	pri_data->y_max =
5638c2ecf20Sopenharmony_ci		(resolution << 2) * (sen_line_num_y - 1);
5648c2ecf20Sopenharmony_ci	pri_data->y_min = 1;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
5678c2ecf20Sopenharmony_ci			&tmp, 0, true);
5688c2ecf20Sopenharmony_ci	if (ret < 0) {
5698c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
5708c2ecf20Sopenharmony_ci		goto exit;
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci	if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
5738c2ecf20Sopenharmony_ci		pri_data->btn_cnt = (tmp & 0x0F);
5748c2ecf20Sopenharmony_ci	} else {
5758c2ecf20Sopenharmony_ci		/* Button pad */
5768c2ecf20Sopenharmony_ci		pri_data->btn_cnt = 1;
5778c2ecf20Sopenharmony_ci	}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	pri_data->has_sp = 0;
5808c2ecf20Sopenharmony_ci	/* Check StickPointer device */
5818c2ecf20Sopenharmony_ci	ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
5828c2ecf20Sopenharmony_ci			&tmp, 0, true);
5838c2ecf20Sopenharmony_ci	if (ret < 0) {
5848c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
5858c2ecf20Sopenharmony_ci		goto exit;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci	if (tmp & U1_DEVTYPE_SP_SUPPORT) {
5888c2ecf20Sopenharmony_ci		dev_ctrl |= U1_SP_ABS_MODE;
5898c2ecf20Sopenharmony_ci		ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5908c2ecf20Sopenharmony_ci			NULL, dev_ctrl, false);
5918c2ecf20Sopenharmony_ci		if (ret < 0) {
5928c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
5938c2ecf20Sopenharmony_ci			goto exit;
5948c2ecf20Sopenharmony_ci		}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci		ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
5978c2ecf20Sopenharmony_ci			&pri_data->sp_btn_info, 0, true);
5988c2ecf20Sopenharmony_ci		if (ret < 0) {
5998c2ecf20Sopenharmony_ci			dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
6008c2ecf20Sopenharmony_ci			goto exit;
6018c2ecf20Sopenharmony_ci		}
6028c2ecf20Sopenharmony_ci		pri_data->has_sp = 1;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci	pri_data->max_fingers = 5;
6058c2ecf20Sopenharmony_ciexit:
6068c2ecf20Sopenharmony_ci	return ret;
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	int ret;
6128c2ecf20Sopenharmony_ci	u8 tmp, sen_line_num_x, sen_line_num_y;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
6158c2ecf20Sopenharmony_ci	if (ret < 0) {
6168c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
6178c2ecf20Sopenharmony_ci		goto exit;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci	sen_line_num_x = 16 + ((tmp & 0x0F)  | (tmp & 0x08 ? 0xF0 : 0));
6208c2ecf20Sopenharmony_ci	sen_line_num_y = 12 + (((tmp & 0xF0) >> 4)  | (tmp & 0x80 ? 0xF0 : 0));
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
6238c2ecf20Sopenharmony_ci	pri_data->x_min = T4_COUNT_PER_ELECTRODE;
6248c2ecf20Sopenharmony_ci	pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
6258c2ecf20Sopenharmony_ci	pri_data->y_min = T4_COUNT_PER_ELECTRODE;
6268c2ecf20Sopenharmony_ci	pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
6278c2ecf20Sopenharmony_ci	pri_data->btn_cnt = 1;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
6308c2ecf20Sopenharmony_ci	if (ret < 0) {
6318c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
6328c2ecf20Sopenharmony_ci		goto exit;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci	tmp |= 0x02;
6358c2ecf20Sopenharmony_ci	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
6368c2ecf20Sopenharmony_ci	if (ret < 0) {
6378c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
6388c2ecf20Sopenharmony_ci		goto exit;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
6428c2ecf20Sopenharmony_ci					NULL, T4_I2C_ABS, false);
6438c2ecf20Sopenharmony_ci	if (ret < 0) {
6448c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
6458c2ecf20Sopenharmony_ci		goto exit;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
6498c2ecf20Sopenharmony_ci				T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
6508c2ecf20Sopenharmony_ci	if (ret < 0) {
6518c2ecf20Sopenharmony_ci		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
6528c2ecf20Sopenharmony_ci		goto exit;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	pri_data->max_fingers = 5;
6558c2ecf20Sopenharmony_ci	pri_data->has_sp = 0;
6568c2ecf20Sopenharmony_ciexit:
6578c2ecf20Sopenharmony_ci	return ret;
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic int alps_sp_open(struct input_dev *dev)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	struct hid_device *hid = input_get_drvdata(dev);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return hid_hw_open(hid);
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic void alps_sp_close(struct input_dev *dev)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	struct hid_device *hid = input_get_drvdata(dev);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	hid_hw_close(hid);
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_cistatic int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	struct alps_dev *data = hid_get_drvdata(hdev);
6778c2ecf20Sopenharmony_ci	struct input_dev *input = hi->input, *input2;
6788c2ecf20Sopenharmony_ci	int ret;
6798c2ecf20Sopenharmony_ci	int res_x, res_y, i;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	data->input = input;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	hid_dbg(hdev, "Opening low level driver\n");
6848c2ecf20Sopenharmony_ci	ret = hid_hw_open(hdev);
6858c2ecf20Sopenharmony_ci	if (ret)
6868c2ecf20Sopenharmony_ci		return ret;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/* Allow incoming hid reports */
6898c2ecf20Sopenharmony_ci	hid_device_io_start(hdev);
6908c2ecf20Sopenharmony_ci	switch (data->dev_type) {
6918c2ecf20Sopenharmony_ci	case T4:
6928c2ecf20Sopenharmony_ci		ret = T4_init(hdev, data);
6938c2ecf20Sopenharmony_ci		break;
6948c2ecf20Sopenharmony_ci	case U1:
6958c2ecf20Sopenharmony_ci		ret = u1_init(hdev, data);
6968c2ecf20Sopenharmony_ci		break;
6978c2ecf20Sopenharmony_ci	default:
6988c2ecf20Sopenharmony_ci		break;
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	if (ret)
7028c2ecf20Sopenharmony_ci		goto exit;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	__set_bit(EV_ABS, input->evbit);
7058c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_MT_POSITION_X,
7068c2ecf20Sopenharmony_ci						data->x_min, data->x_max, 0, 0);
7078c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_MT_POSITION_Y,
7088c2ecf20Sopenharmony_ci						data->y_min, data->y_max, 0, 0);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	if (data->x_active_len_mm && data->y_active_len_mm) {
7118c2ecf20Sopenharmony_ci		res_x = (data->x_max - 1) / data->x_active_len_mm;
7128c2ecf20Sopenharmony_ci		res_y = (data->y_max - 1) / data->y_active_len_mm;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci		input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
7158c2ecf20Sopenharmony_ci		input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	__set_bit(EV_KEY, input->evbit);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	if (data->btn_cnt == 1)
7258c2ecf20Sopenharmony_ci		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	for (i = 0; i < data->btn_cnt; i++)
7288c2ecf20Sopenharmony_ci		__set_bit(BTN_LEFT + i, input->keybit);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	/* Stick device initialization */
7318c2ecf20Sopenharmony_ci	if (data->has_sp) {
7328c2ecf20Sopenharmony_ci		input2 = input_allocate_device();
7338c2ecf20Sopenharmony_ci		if (!input2) {
7348c2ecf20Sopenharmony_ci			ret = -ENOMEM;
7358c2ecf20Sopenharmony_ci			goto exit;
7368c2ecf20Sopenharmony_ci		}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci		data->input2 = input2;
7398c2ecf20Sopenharmony_ci		input2->phys = input->phys;
7408c2ecf20Sopenharmony_ci		input2->name = "DualPoint Stick";
7418c2ecf20Sopenharmony_ci		input2->id.bustype = BUS_I2C;
7428c2ecf20Sopenharmony_ci		input2->id.vendor  = input->id.vendor;
7438c2ecf20Sopenharmony_ci		input2->id.product = input->id.product;
7448c2ecf20Sopenharmony_ci		input2->id.version = input->id.version;
7458c2ecf20Sopenharmony_ci		input2->dev.parent = input->dev.parent;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci		input_set_drvdata(input2, hdev);
7488c2ecf20Sopenharmony_ci		input2->open = alps_sp_open;
7498c2ecf20Sopenharmony_ci		input2->close = alps_sp_close;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		__set_bit(EV_KEY, input2->evbit);
7528c2ecf20Sopenharmony_ci		data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
7538c2ecf20Sopenharmony_ci		for (i = 0; i < data->sp_btn_cnt; i++)
7548c2ecf20Sopenharmony_ci			__set_bit(BTN_LEFT + i, input2->keybit);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		__set_bit(EV_REL, input2->evbit);
7578c2ecf20Sopenharmony_ci		__set_bit(REL_X, input2->relbit);
7588c2ecf20Sopenharmony_ci		__set_bit(REL_Y, input2->relbit);
7598c2ecf20Sopenharmony_ci		__set_bit(INPUT_PROP_POINTER, input2->propbit);
7608c2ecf20Sopenharmony_ci		__set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		if (input_register_device(data->input2)) {
7638c2ecf20Sopenharmony_ci			input_free_device(input2);
7648c2ecf20Sopenharmony_ci			ret = -ENOENT;
7658c2ecf20Sopenharmony_ci			goto exit;
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ciexit:
7708c2ecf20Sopenharmony_ci	hid_device_io_stop(hdev);
7718c2ecf20Sopenharmony_ci	hid_hw_close(hdev);
7728c2ecf20Sopenharmony_ci	return ret;
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic int alps_input_mapping(struct hid_device *hdev,
7768c2ecf20Sopenharmony_ci		struct hid_input *hi, struct hid_field *field,
7778c2ecf20Sopenharmony_ci		struct hid_usage *usage, unsigned long **bit, int *max)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	return -1;
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
7838c2ecf20Sopenharmony_ci{
7848c2ecf20Sopenharmony_ci	struct alps_dev *data = NULL;
7858c2ecf20Sopenharmony_ci	int ret;
7868c2ecf20Sopenharmony_ci	data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
7878c2ecf20Sopenharmony_ci	if (!data)
7888c2ecf20Sopenharmony_ci		return -ENOMEM;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	data->hdev = hdev;
7918c2ecf20Sopenharmony_ci	hid_set_drvdata(hdev, data);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	ret = hid_parse(hdev);
7968c2ecf20Sopenharmony_ci	if (ret) {
7978c2ecf20Sopenharmony_ci		hid_err(hdev, "parse failed\n");
7988c2ecf20Sopenharmony_ci		return ret;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	switch (hdev->product) {
8028c2ecf20Sopenharmony_ci	case HID_DEVICE_ID_ALPS_T4_BTNLESS:
8038c2ecf20Sopenharmony_ci		data->dev_type = T4;
8048c2ecf20Sopenharmony_ci		break;
8058c2ecf20Sopenharmony_ci	case HID_DEVICE_ID_ALPS_U1_DUAL:
8068c2ecf20Sopenharmony_ci	case HID_DEVICE_ID_ALPS_U1:
8078c2ecf20Sopenharmony_ci	case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
8088c2ecf20Sopenharmony_ci		data->dev_type = U1;
8098c2ecf20Sopenharmony_ci		break;
8108c2ecf20Sopenharmony_ci	default:
8118c2ecf20Sopenharmony_ci		data->dev_type = UNKNOWN;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
8158c2ecf20Sopenharmony_ci	if (ret) {
8168c2ecf20Sopenharmony_ci		hid_err(hdev, "hw start failed\n");
8178c2ecf20Sopenharmony_ci		return ret;
8188c2ecf20Sopenharmony_ci	}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	return 0;
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic void alps_remove(struct hid_device *hdev)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	hid_hw_stop(hdev);
8268c2ecf20Sopenharmony_ci}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_cistatic const struct hid_device_id alps_id[] = {
8298c2ecf20Sopenharmony_ci	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
8308c2ecf20Sopenharmony_ci		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
8318c2ecf20Sopenharmony_ci	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
8328c2ecf20Sopenharmony_ci		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
8338c2ecf20Sopenharmony_ci	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
8348c2ecf20Sopenharmony_ci		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY) },
8358c2ecf20Sopenharmony_ci	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
8368c2ecf20Sopenharmony_ci		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
8378c2ecf20Sopenharmony_ci	{ }
8388c2ecf20Sopenharmony_ci};
8398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, alps_id);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic struct hid_driver alps_driver = {
8428c2ecf20Sopenharmony_ci	.name = "hid-alps",
8438c2ecf20Sopenharmony_ci	.id_table		= alps_id,
8448c2ecf20Sopenharmony_ci	.probe			= alps_probe,
8458c2ecf20Sopenharmony_ci	.remove			= alps_remove,
8468c2ecf20Sopenharmony_ci	.raw_event		= alps_raw_event,
8478c2ecf20Sopenharmony_ci	.input_mapping		= alps_input_mapping,
8488c2ecf20Sopenharmony_ci	.input_configured	= alps_input_configured,
8498c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
8508c2ecf20Sopenharmony_ci	.resume			= alps_post_resume,
8518c2ecf20Sopenharmony_ci	.reset_resume		= alps_post_reset,
8528c2ecf20Sopenharmony_ci#endif
8538c2ecf20Sopenharmony_ci};
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cimodule_hid_driver(alps_driver);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
8588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ALPS HID driver");
8598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
860