xref: /openbmc/linux/drivers/hid/hid-alps.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22562756dSMasaki Ota /*
32562756dSMasaki Ota  *  Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
42562756dSMasaki Ota  */
52562756dSMasaki Ota 
62562756dSMasaki Ota #include <linux/kernel.h>
72562756dSMasaki Ota #include <linux/hid.h>
82562756dSMasaki Ota #include <linux/input.h>
92562756dSMasaki Ota #include <linux/input/mt.h>
102562756dSMasaki Ota #include <linux/module.h>
112562756dSMasaki Ota #include <asm/unaligned.h>
122562756dSMasaki Ota #include "hid-ids.h"
132562756dSMasaki Ota 
142562756dSMasaki Ota /* ALPS Device Product ID */
152562756dSMasaki Ota #define HID_PRODUCT_ID_T3_BTNLESS	0xD0C0
162562756dSMasaki Ota #define HID_PRODUCT_ID_COSMO		0x1202
172562756dSMasaki Ota #define HID_PRODUCT_ID_U1_PTP_1		0x1207
182562756dSMasaki Ota #define HID_PRODUCT_ID_U1			0x1209
192562756dSMasaki Ota #define HID_PRODUCT_ID_U1_PTP_2		0x120A
202562756dSMasaki Ota #define HID_PRODUCT_ID_U1_DUAL		0x120B
212562756dSMasaki Ota #define HID_PRODUCT_ID_T4_BTNLESS	0x120C
222562756dSMasaki Ota 
232562756dSMasaki Ota #define DEV_SINGLEPOINT				0x01
242562756dSMasaki Ota #define DEV_DUALPOINT				0x02
252562756dSMasaki Ota 
262562756dSMasaki Ota #define U1_MOUSE_REPORT_ID			0x01 /* Mouse data ReportID */
272562756dSMasaki Ota #define U1_ABSOLUTE_REPORT_ID		0x03 /* Absolute data ReportID */
28aa3c439cSCaiyuan Xie #define U1_ABSOLUTE_REPORT_ID_SECD  0x02 /* FW-PTP Absolute data ReportID */
292562756dSMasaki Ota #define U1_FEATURE_REPORT_ID		0x05 /* Feature ReportID */
302562756dSMasaki Ota #define U1_SP_ABSOLUTE_REPORT_ID	0x06 /* Feature ReportID */
312562756dSMasaki Ota 
322562756dSMasaki Ota #define U1_FEATURE_REPORT_LEN		0x08 /* Feature Report Length */
332562756dSMasaki Ota #define U1_FEATURE_REPORT_LEN_ALL	0x0A
342562756dSMasaki Ota #define U1_CMD_REGISTER_READ		0xD1
352562756dSMasaki Ota #define U1_CMD_REGISTER_WRITE		0xD2
362562756dSMasaki Ota 
372562756dSMasaki Ota #define	U1_DEVTYPE_SP_SUPPORT		0x10 /* SP Support */
382562756dSMasaki Ota #define	U1_DISABLE_DEV				0x01
392562756dSMasaki Ota #define U1_TP_ABS_MODE				0x02
402562756dSMasaki Ota #define	U1_SP_ABS_MODE				0x80
412562756dSMasaki Ota 
422562756dSMasaki Ota #define ADDRESS_U1_DEV_CTRL_1	0x00800040
432562756dSMasaki Ota #define ADDRESS_U1_DEVICE_TYP	0x00800043
442562756dSMasaki Ota #define ADDRESS_U1_NUM_SENS_X	0x00800047
452562756dSMasaki Ota #define ADDRESS_U1_NUM_SENS_Y	0x00800048
462562756dSMasaki Ota #define ADDRESS_U1_PITCH_SENS_X	0x00800049
472562756dSMasaki Ota #define ADDRESS_U1_PITCH_SENS_Y	0x0080004A
482562756dSMasaki Ota #define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
492562756dSMasaki Ota #define ADDRESS_U1_PAD_BTN		0x00800052
502562756dSMasaki Ota #define ADDRESS_U1_SP_BTN		0x0080009F
512562756dSMasaki Ota 
5273196ebeSMasaki Ota #define T4_INPUT_REPORT_LEN			sizeof(struct t4_input_report)
5373196ebeSMasaki Ota #define T4_FEATURE_REPORT_LEN		T4_INPUT_REPORT_LEN
5473196ebeSMasaki Ota #define T4_FEATURE_REPORT_ID		7
5573196ebeSMasaki Ota #define T4_CMD_REGISTER_READ			0x08
5673196ebeSMasaki Ota #define T4_CMD_REGISTER_WRITE			0x07
5773196ebeSMasaki Ota 
5873196ebeSMasaki Ota #define T4_ADDRESS_BASE				0xC2C0
5973196ebeSMasaki Ota #define PRM_SYS_CONFIG_1			(T4_ADDRESS_BASE + 0x0002)
6073196ebeSMasaki Ota #define T4_PRM_FEED_CONFIG_1		(T4_ADDRESS_BASE + 0x0004)
6173196ebeSMasaki Ota #define T4_PRM_FEED_CONFIG_4		(T4_ADDRESS_BASE + 0x001A)
6273196ebeSMasaki Ota #define T4_PRM_ID_CONFIG_3			(T4_ADDRESS_BASE + 0x00B0)
6373196ebeSMasaki Ota 
6473196ebeSMasaki Ota 
6573196ebeSMasaki Ota #define T4_FEEDCFG4_ADVANCED_ABS_ENABLE			0x01
6673196ebeSMasaki Ota #define T4_I2C_ABS	0x78
6773196ebeSMasaki Ota 
6873196ebeSMasaki Ota #define T4_COUNT_PER_ELECTRODE		256
692562756dSMasaki Ota #define MAX_TOUCHES	5
702562756dSMasaki Ota 
7173196ebeSMasaki Ota enum dev_num {
7273196ebeSMasaki Ota 	U1,
7373196ebeSMasaki Ota 	T4,
7473196ebeSMasaki Ota 	UNKNOWN,
7573196ebeSMasaki Ota };
762562756dSMasaki Ota /**
778802d154SLee Jones  * struct alps_dev
782562756dSMasaki Ota  *
792562756dSMasaki Ota  * @input: pointer to the kernel input device
802562756dSMasaki Ota  * @input2: pointer to the kernel input2 device
812562756dSMasaki Ota  * @hdev: pointer to the struct hid_device
822562756dSMasaki Ota  *
832562756dSMasaki Ota  * @dev_type: device type
845992262dSMasaki Ota  * @max_fingers: total number of fingers
855992262dSMasaki Ota  * @has_sp: boolean of sp existense
865992262dSMasaki Ota  * @sp_btn_info: button information
872562756dSMasaki Ota  * @x_active_len_mm: active area length of X (mm)
882562756dSMasaki Ota  * @y_active_len_mm: active area length of Y (mm)
892562756dSMasaki Ota  * @x_max: maximum x coordinate value
902562756dSMasaki Ota  * @y_max: maximum y coordinate value
91c7083d3fSMasaki Ota  * @x_min: minimum x coordinate value
92c7083d3fSMasaki Ota  * @y_min: minimum y coordinate value
932562756dSMasaki Ota  * @btn_cnt: number of buttons
942562756dSMasaki Ota  * @sp_btn_cnt: number of stick buttons
952562756dSMasaki Ota  */
9673196ebeSMasaki Ota struct alps_dev {
972562756dSMasaki Ota 	struct input_dev *input;
982562756dSMasaki Ota 	struct input_dev *input2;
992562756dSMasaki Ota 	struct hid_device *hdev;
1002562756dSMasaki Ota 
10173196ebeSMasaki Ota 	enum dev_num dev_type;
1025992262dSMasaki Ota 	u8  max_fingers;
1035992262dSMasaki Ota 	u8  has_sp;
1042562756dSMasaki Ota 	u8	sp_btn_info;
1052562756dSMasaki Ota 	u32	x_active_len_mm;
1062562756dSMasaki Ota 	u32	y_active_len_mm;
1072562756dSMasaki Ota 	u32	x_max;
1082562756dSMasaki Ota 	u32	y_max;
109c7083d3fSMasaki Ota 	u32	x_min;
110c7083d3fSMasaki Ota 	u32	y_min;
1112562756dSMasaki Ota 	u32	btn_cnt;
1122562756dSMasaki Ota 	u32	sp_btn_cnt;
1132562756dSMasaki Ota };
1142562756dSMasaki Ota 
11573196ebeSMasaki Ota struct t4_contact_data {
11673196ebeSMasaki Ota 	u8  palm;
11773196ebeSMasaki Ota 	u8	x_lo;
11873196ebeSMasaki Ota 	u8	x_hi;
11973196ebeSMasaki Ota 	u8	y_lo;
12073196ebeSMasaki Ota 	u8	y_hi;
12173196ebeSMasaki Ota };
12273196ebeSMasaki Ota 
12373196ebeSMasaki Ota struct t4_input_report {
12473196ebeSMasaki Ota 	u8  reportID;
12573196ebeSMasaki Ota 	u8  numContacts;
12673196ebeSMasaki Ota 	struct t4_contact_data contact[5];
12773196ebeSMasaki Ota 	u8  button;
12873196ebeSMasaki Ota 	u8  track[5];
12973196ebeSMasaki Ota 	u8  zx[5], zy[5];
13073196ebeSMasaki Ota 	u8  palmTime[5];
13173196ebeSMasaki Ota 	u8  kilroy;
13273196ebeSMasaki Ota 	u16 timeStamp;
13373196ebeSMasaki Ota };
13473196ebeSMasaki Ota 
t4_calc_check_sum(u8 * buffer,unsigned long offset,unsigned long length)13573196ebeSMasaki Ota static u16 t4_calc_check_sum(u8 *buffer,
13673196ebeSMasaki Ota 		unsigned long offset, unsigned long length)
13773196ebeSMasaki Ota {
13873196ebeSMasaki Ota 	u16 sum1 = 0xFF, sum2 = 0xFF;
13973196ebeSMasaki Ota 	unsigned long i = 0;
14073196ebeSMasaki Ota 
14173196ebeSMasaki Ota 	if (offset + length >= 50)
14273196ebeSMasaki Ota 		return 0;
14373196ebeSMasaki Ota 
14473196ebeSMasaki Ota 	while (length > 0) {
14573196ebeSMasaki Ota 		u32 tlen = length > 20 ? 20 : length;
14673196ebeSMasaki Ota 
14773196ebeSMasaki Ota 		length -= tlen;
14873196ebeSMasaki Ota 
14973196ebeSMasaki Ota 		do {
15073196ebeSMasaki Ota 			sum1 += buffer[offset + i];
15173196ebeSMasaki Ota 			sum2 += sum1;
15273196ebeSMasaki Ota 			i++;
15373196ebeSMasaki Ota 		} while (--tlen > 0);
15473196ebeSMasaki Ota 
15573196ebeSMasaki Ota 		sum1 = (sum1 & 0xFF) + (sum1 >> 8);
15673196ebeSMasaki Ota 		sum2 = (sum2 & 0xFF) + (sum2 >> 8);
15773196ebeSMasaki Ota 	}
15873196ebeSMasaki Ota 
15973196ebeSMasaki Ota 	sum1 = (sum1 & 0xFF) + (sum1 >> 8);
16073196ebeSMasaki Ota 	sum2 = (sum2 & 0xFF) + (sum2 >> 8);
16173196ebeSMasaki Ota 
16273196ebeSMasaki Ota 	return(sum2 << 8 | sum1);
16373196ebeSMasaki Ota }
16473196ebeSMasaki Ota 
t4_read_write_register(struct hid_device * hdev,u32 address,u8 * read_val,u8 write_val,bool read_flag)16573196ebeSMasaki Ota static int t4_read_write_register(struct hid_device *hdev, u32 address,
16673196ebeSMasaki Ota 	u8 *read_val, u8 write_val, bool read_flag)
16773196ebeSMasaki Ota {
16873196ebeSMasaki Ota 	int ret;
16973196ebeSMasaki Ota 	u16 check_sum;
17073196ebeSMasaki Ota 	u8 *input;
171edb6cb3dSChristophe JAILLET 	u8 *readbuf = NULL;
17273196ebeSMasaki Ota 
17373196ebeSMasaki Ota 	input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
17473196ebeSMasaki Ota 	if (!input)
17573196ebeSMasaki Ota 		return -ENOMEM;
17673196ebeSMasaki Ota 
17773196ebeSMasaki Ota 	input[0] = T4_FEATURE_REPORT_ID;
17873196ebeSMasaki Ota 	if (read_flag) {
17973196ebeSMasaki Ota 		input[1] = T4_CMD_REGISTER_READ;
18073196ebeSMasaki Ota 		input[8] = 0x00;
18173196ebeSMasaki Ota 	} else {
18273196ebeSMasaki Ota 		input[1] = T4_CMD_REGISTER_WRITE;
18373196ebeSMasaki Ota 		input[8] = write_val;
18473196ebeSMasaki Ota 	}
18573196ebeSMasaki Ota 	put_unaligned_le32(address, input + 2);
18673196ebeSMasaki Ota 	input[6] = 1;
18773196ebeSMasaki Ota 	input[7] = 0;
18873196ebeSMasaki Ota 
18973196ebeSMasaki Ota 	/* Calculate the checksum */
19073196ebeSMasaki Ota 	check_sum = t4_calc_check_sum(input, 1, 8);
19173196ebeSMasaki Ota 	input[9] = (u8)check_sum;
19273196ebeSMasaki Ota 	input[10] = (u8)(check_sum >> 8);
19373196ebeSMasaki Ota 	input[11] = 0;
19473196ebeSMasaki Ota 
19573196ebeSMasaki Ota 	ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
19673196ebeSMasaki Ota 			T4_FEATURE_REPORT_LEN,
19773196ebeSMasaki Ota 			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
19873196ebeSMasaki Ota 
19973196ebeSMasaki Ota 	if (ret < 0) {
20073196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
20173196ebeSMasaki Ota 		goto exit;
20273196ebeSMasaki Ota 	}
20373196ebeSMasaki Ota 
20473196ebeSMasaki Ota 	if (read_flag) {
205edb6cb3dSChristophe JAILLET 		readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
20673196ebeSMasaki Ota 		if (!readbuf) {
20773196ebeSMasaki Ota 			ret = -ENOMEM;
20873196ebeSMasaki Ota 			goto exit;
20973196ebeSMasaki Ota 		}
21073196ebeSMasaki Ota 
21173196ebeSMasaki Ota 		ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
21273196ebeSMasaki Ota 				T4_FEATURE_REPORT_LEN,
21373196ebeSMasaki Ota 				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
21473196ebeSMasaki Ota 		if (ret < 0) {
21573196ebeSMasaki Ota 			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
21673196ebeSMasaki Ota 			goto exit_readbuf;
21773196ebeSMasaki Ota 		}
21873196ebeSMasaki Ota 
219605f0772SChristophe JAILLET 		ret = -EINVAL;
220605f0772SChristophe JAILLET 
22173196ebeSMasaki Ota 		if (*(u32 *)&readbuf[6] != address) {
22273196ebeSMasaki Ota 			dev_err(&hdev->dev, "read register address error (%x,%x)\n",
22373196ebeSMasaki Ota 				*(u32 *)&readbuf[6], address);
22473196ebeSMasaki Ota 			goto exit_readbuf;
22573196ebeSMasaki Ota 		}
22673196ebeSMasaki Ota 
22773196ebeSMasaki Ota 		if (*(u16 *)&readbuf[10] != 1) {
22873196ebeSMasaki Ota 			dev_err(&hdev->dev, "read register size error (%x)\n",
22973196ebeSMasaki Ota 				*(u16 *)&readbuf[10]);
23073196ebeSMasaki Ota 			goto exit_readbuf;
23173196ebeSMasaki Ota 		}
23273196ebeSMasaki Ota 
23373196ebeSMasaki Ota 		check_sum = t4_calc_check_sum(readbuf, 6, 7);
23473196ebeSMasaki Ota 		if (*(u16 *)&readbuf[13] != check_sum) {
23573196ebeSMasaki Ota 			dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
23673196ebeSMasaki Ota 				*(u16 *)&readbuf[13], check_sum);
23773196ebeSMasaki Ota 			goto exit_readbuf;
23873196ebeSMasaki Ota 		}
23973196ebeSMasaki Ota 
24073196ebeSMasaki Ota 		*read_val = readbuf[12];
24173196ebeSMasaki Ota 	}
24273196ebeSMasaki Ota 
24373196ebeSMasaki Ota 	ret = 0;
24473196ebeSMasaki Ota 
24573196ebeSMasaki Ota exit_readbuf:
24673196ebeSMasaki Ota 	kfree(readbuf);
24773196ebeSMasaki Ota exit:
24873196ebeSMasaki Ota 	kfree(input);
24973196ebeSMasaki Ota 	return ret;
25073196ebeSMasaki Ota }
25173196ebeSMasaki Ota 
u1_read_write_register(struct hid_device * hdev,u32 address,u8 * read_val,u8 write_val,bool read_flag)2522562756dSMasaki Ota static int u1_read_write_register(struct hid_device *hdev, u32 address,
2532562756dSMasaki Ota 	u8 *read_val, u8 write_val, bool read_flag)
2542562756dSMasaki Ota {
2552562756dSMasaki Ota 	int ret, i;
2562562756dSMasaki Ota 	u8 check_sum;
2572562756dSMasaki Ota 	u8 *input;
2582562756dSMasaki Ota 	u8 *readbuf;
2592562756dSMasaki Ota 
260819d64e5SMasaki Ota 	input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
2612562756dSMasaki Ota 	if (!input)
2622562756dSMasaki Ota 		return -ENOMEM;
2632562756dSMasaki Ota 
2642562756dSMasaki Ota 	input[0] = U1_FEATURE_REPORT_ID;
2652562756dSMasaki Ota 	if (read_flag) {
2662562756dSMasaki Ota 		input[1] = U1_CMD_REGISTER_READ;
2672562756dSMasaki Ota 		input[6] = 0x00;
2682562756dSMasaki Ota 	} else {
2692562756dSMasaki Ota 		input[1] = U1_CMD_REGISTER_WRITE;
2702562756dSMasaki Ota 		input[6] = write_val;
2712562756dSMasaki Ota 	}
2722562756dSMasaki Ota 
2732562756dSMasaki Ota 	put_unaligned_le32(address, input + 2);
2742562756dSMasaki Ota 
2752562756dSMasaki Ota 	/* Calculate the checksum */
2762562756dSMasaki Ota 	check_sum = U1_FEATURE_REPORT_LEN_ALL;
2772562756dSMasaki Ota 	for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
2782562756dSMasaki Ota 		check_sum += input[i];
2792562756dSMasaki Ota 
2802562756dSMasaki Ota 	input[7] = check_sum;
2812562756dSMasaki Ota 	ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
282819d64e5SMasaki Ota 			U1_FEATURE_REPORT_LEN,
283819d64e5SMasaki Ota 			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
2842562756dSMasaki Ota 
2852562756dSMasaki Ota 	if (ret < 0) {
2862562756dSMasaki Ota 		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
2872562756dSMasaki Ota 		goto exit;
2882562756dSMasaki Ota 	}
2892562756dSMasaki Ota 
2902562756dSMasaki Ota 	if (read_flag) {
291819d64e5SMasaki Ota 		readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
292819d64e5SMasaki Ota 		if (!readbuf) {
2937ee2eaa3SAxel Lin 			ret = -ENOMEM;
2947ee2eaa3SAxel Lin 			goto exit;
295819d64e5SMasaki Ota 		}
296819d64e5SMasaki Ota 
2972562756dSMasaki Ota 		ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
298819d64e5SMasaki Ota 				U1_FEATURE_REPORT_LEN,
29963b3a7d0SJiri Kosina 				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
3002562756dSMasaki Ota 
3012562756dSMasaki Ota 		if (ret < 0) {
3022562756dSMasaki Ota 			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
3037ee2eaa3SAxel Lin 			kfree(readbuf);
3042562756dSMasaki Ota 			goto exit;
3052562756dSMasaki Ota 		}
3062562756dSMasaki Ota 
3072562756dSMasaki Ota 		*read_val = readbuf[6];
308819d64e5SMasaki Ota 
309819d64e5SMasaki Ota 		kfree(readbuf);
3102562756dSMasaki Ota 	}
3112562756dSMasaki Ota 
312819d64e5SMasaki Ota 	ret = 0;
3132562756dSMasaki Ota 
3142562756dSMasaki Ota exit:
3152562756dSMasaki Ota 	kfree(input);
3162562756dSMasaki Ota 	return ret;
3172562756dSMasaki Ota }
3182562756dSMasaki Ota 
t4_raw_event(struct alps_dev * hdata,u8 * data,int size)31973196ebeSMasaki Ota static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
32073196ebeSMasaki Ota {
32173196ebeSMasaki Ota 	unsigned int x, y, z;
32273196ebeSMasaki Ota 	int i;
32373196ebeSMasaki Ota 	struct t4_input_report *p_report = (struct t4_input_report *)data;
32473196ebeSMasaki Ota 
32573196ebeSMasaki Ota 	if (!data)
32673196ebeSMasaki Ota 		return 0;
32773196ebeSMasaki Ota 	for (i = 0; i < hdata->max_fingers; i++) {
32873196ebeSMasaki Ota 		x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
32973196ebeSMasaki Ota 		y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
33073196ebeSMasaki Ota 		y = hdata->y_max - y + hdata->y_min;
33173196ebeSMasaki Ota 		z = (p_report->contact[i].palm < 0x80 &&
33273196ebeSMasaki Ota 			p_report->contact[i].palm > 0) * 62;
33373196ebeSMasaki Ota 		if (x == 0xffff) {
33473196ebeSMasaki Ota 			x = 0;
33573196ebeSMasaki Ota 			y = 0;
33673196ebeSMasaki Ota 			z = 0;
33773196ebeSMasaki Ota 		}
33873196ebeSMasaki Ota 		input_mt_slot(hdata->input, i);
33973196ebeSMasaki Ota 
34073196ebeSMasaki Ota 		input_mt_report_slot_state(hdata->input,
34173196ebeSMasaki Ota 			MT_TOOL_FINGER, z != 0);
34273196ebeSMasaki Ota 
34373196ebeSMasaki Ota 		if (!z)
34473196ebeSMasaki Ota 			continue;
34573196ebeSMasaki Ota 
34673196ebeSMasaki Ota 		input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
34773196ebeSMasaki Ota 		input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
34873196ebeSMasaki Ota 		input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
34973196ebeSMasaki Ota 	}
35073196ebeSMasaki Ota 	input_mt_sync_frame(hdata->input);
35173196ebeSMasaki Ota 
35273196ebeSMasaki Ota 	input_report_key(hdata->input, BTN_LEFT, p_report->button);
35373196ebeSMasaki Ota 
35473196ebeSMasaki Ota 	input_sync(hdata->input);
35573196ebeSMasaki Ota 	return 1;
35673196ebeSMasaki Ota }
35773196ebeSMasaki Ota 
u1_raw_event(struct alps_dev * hdata,u8 * data,int size)35873196ebeSMasaki Ota static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
3592562756dSMasaki Ota {
360819d64e5SMasaki Ota 	unsigned int x, y, z;
361819d64e5SMasaki Ota 	int i;
362819d64e5SMasaki Ota 	short sp_x, sp_y;
3632562756dSMasaki Ota 
36473196ebeSMasaki Ota 	if (!data)
36573196ebeSMasaki Ota 		return 0;
3662562756dSMasaki Ota 	switch (data[0]) {
3672562756dSMasaki Ota 	case U1_MOUSE_REPORT_ID:
3682562756dSMasaki Ota 		break;
3692562756dSMasaki Ota 	case U1_FEATURE_REPORT_ID:
3702562756dSMasaki Ota 		break;
3712562756dSMasaki Ota 	case U1_ABSOLUTE_REPORT_ID:
372aa3c439cSCaiyuan Xie 	case U1_ABSOLUTE_REPORT_ID_SECD:
37373196ebeSMasaki Ota 		for (i = 0; i < hdata->max_fingers; i++) {
374819d64e5SMasaki Ota 			u8 *contact = &data[i * 5];
375819d64e5SMasaki Ota 
376819d64e5SMasaki Ota 			x = get_unaligned_le16(contact + 3);
377819d64e5SMasaki Ota 			y = get_unaligned_le16(contact + 5);
378819d64e5SMasaki Ota 			z = contact[7] & 0x7F;
3792562756dSMasaki Ota 
3802562756dSMasaki Ota 			input_mt_slot(hdata->input, i);
3812562756dSMasaki Ota 
382819d64e5SMasaki Ota 			if (z != 0) {
3832562756dSMasaki Ota 				input_mt_report_slot_state(hdata->input,
3842562756dSMasaki Ota 					MT_TOOL_FINGER, 1);
3859a54cf46SMasaki Ota 				input_report_abs(hdata->input,
3869a54cf46SMasaki Ota 					ABS_MT_POSITION_X, x);
3879a54cf46SMasaki Ota 				input_report_abs(hdata->input,
3889a54cf46SMasaki Ota 					ABS_MT_POSITION_Y, y);
3899a54cf46SMasaki Ota 				input_report_abs(hdata->input,
3909a54cf46SMasaki Ota 					ABS_MT_PRESSURE, z);
3912562756dSMasaki Ota 			} else {
3925fc70e35SJiada Wang 				input_mt_report_slot_inactive(hdata->input);
3932562756dSMasaki Ota 			}
3942562756dSMasaki Ota 		}
3952562756dSMasaki Ota 
3962562756dSMasaki Ota 		input_mt_sync_frame(hdata->input);
3972562756dSMasaki Ota 
398819d64e5SMasaki Ota 		input_report_key(hdata->input, BTN_LEFT,
399819d64e5SMasaki Ota 			data[1] & 0x1);
400819d64e5SMasaki Ota 		input_report_key(hdata->input, BTN_RIGHT,
401819d64e5SMasaki Ota 			(data[1] & 0x2));
402819d64e5SMasaki Ota 		input_report_key(hdata->input, BTN_MIDDLE,
403819d64e5SMasaki Ota 			(data[1] & 0x4));
404819d64e5SMasaki Ota 
405819d64e5SMasaki Ota 		input_sync(hdata->input);
4062562756dSMasaki Ota 
4072562756dSMasaki Ota 		return 1;
4082562756dSMasaki Ota 
4092562756dSMasaki Ota 	case U1_SP_ABSOLUTE_REPORT_ID:
410819d64e5SMasaki Ota 		sp_x = get_unaligned_le16(data+2);
411819d64e5SMasaki Ota 		sp_y = get_unaligned_le16(data+4);
4122562756dSMasaki Ota 
4132562756dSMasaki Ota 		sp_x = sp_x / 8;
4142562756dSMasaki Ota 		sp_y = sp_y / 8;
4152562756dSMasaki Ota 
416819d64e5SMasaki Ota 		input_report_rel(hdata->input2, REL_X, sp_x);
417819d64e5SMasaki Ota 		input_report_rel(hdata->input2, REL_Y, sp_y);
4182562756dSMasaki Ota 
419819d64e5SMasaki Ota 		input_report_key(hdata->input2, BTN_LEFT,
420819d64e5SMasaki Ota 			data[1] & 0x1);
421819d64e5SMasaki Ota 		input_report_key(hdata->input2, BTN_RIGHT,
422819d64e5SMasaki Ota 			(data[1] & 0x2));
423819d64e5SMasaki Ota 		input_report_key(hdata->input2, BTN_MIDDLE,
424819d64e5SMasaki Ota 			(data[1] & 0x4));
4252562756dSMasaki Ota 
426819d64e5SMasaki Ota 		input_sync(hdata->input2);
4272562756dSMasaki Ota 
4282562756dSMasaki Ota 		return 1;
4292562756dSMasaki Ota 	}
4302562756dSMasaki Ota 
4312562756dSMasaki Ota 	return 0;
4322562756dSMasaki Ota }
4332562756dSMasaki Ota 
alps_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)43473196ebeSMasaki Ota static int alps_raw_event(struct hid_device *hdev,
43573196ebeSMasaki Ota 		struct hid_report *report, u8 *data, int size)
4362562756dSMasaki Ota {
43773196ebeSMasaki Ota 	int ret = 0;
43873196ebeSMasaki Ota 	struct alps_dev *hdata = hid_get_drvdata(hdev);
43973196ebeSMasaki Ota 
44073196ebeSMasaki Ota 	switch (hdev->product) {
44173196ebeSMasaki Ota 	case HID_PRODUCT_ID_T4_BTNLESS:
44273196ebeSMasaki Ota 		ret = t4_raw_event(hdata, data, size);
44373196ebeSMasaki Ota 		break;
44473196ebeSMasaki Ota 	default:
44573196ebeSMasaki Ota 		ret = u1_raw_event(hdata, data, size);
44673196ebeSMasaki Ota 		break;
44773196ebeSMasaki Ota 	}
44873196ebeSMasaki Ota 	return ret;
4492562756dSMasaki Ota }
4502562756dSMasaki Ota 
alps_post_reset(struct hid_device * hdev)45173196ebeSMasaki Ota static int __maybe_unused alps_post_reset(struct hid_device *hdev)
4522562756dSMasaki Ota {
45373196ebeSMasaki Ota 	int ret = -1;
45473196ebeSMasaki Ota 	struct alps_dev *data = hid_get_drvdata(hdev);
4552562756dSMasaki Ota 
45673196ebeSMasaki Ota 	switch (data->dev_type) {
45773196ebeSMasaki Ota 	case T4:
45873196ebeSMasaki Ota 		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
45973196ebeSMasaki Ota 			NULL, T4_I2C_ABS, false);
46069934012SChristophe JAILLET 		if (ret < 0) {
46169934012SChristophe JAILLET 			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
46269934012SChristophe JAILLET 				ret);
46369934012SChristophe JAILLET 			goto exit;
46469934012SChristophe JAILLET 		}
46569934012SChristophe JAILLET 
46673196ebeSMasaki Ota 		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
46773196ebeSMasaki Ota 			NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
46869934012SChristophe JAILLET 		if (ret < 0) {
46969934012SChristophe JAILLET 			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
47069934012SChristophe JAILLET 				ret);
47169934012SChristophe JAILLET 			goto exit;
47269934012SChristophe JAILLET 		}
47373196ebeSMasaki Ota 		break;
47473196ebeSMasaki Ota 	case U1:
47573196ebeSMasaki Ota 		ret = u1_read_write_register(hdev,
47673196ebeSMasaki Ota 			ADDRESS_U1_DEV_CTRL_1, NULL,
47773196ebeSMasaki Ota 			U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
47869934012SChristophe JAILLET 		if (ret < 0) {
47969934012SChristophe JAILLET 			dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
48069934012SChristophe JAILLET 				ret);
48169934012SChristophe JAILLET 			goto exit;
48269934012SChristophe JAILLET 		}
48373196ebeSMasaki Ota 		break;
48473196ebeSMasaki Ota 	default:
48573196ebeSMasaki Ota 		break;
48673196ebeSMasaki Ota 	}
48769934012SChristophe JAILLET 
48869934012SChristophe JAILLET exit:
48973196ebeSMasaki Ota 	return ret;
49073196ebeSMasaki Ota }
49173196ebeSMasaki Ota 
alps_post_resume(struct hid_device * hdev)49273196ebeSMasaki Ota static int __maybe_unused alps_post_resume(struct hid_device *hdev)
49373196ebeSMasaki Ota {
49473196ebeSMasaki Ota 	return alps_post_reset(hdev);
49573196ebeSMasaki Ota }
49673196ebeSMasaki Ota 
u1_init(struct hid_device * hdev,struct alps_dev * pri_data)49773196ebeSMasaki Ota static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
4985d8c720dSMasaki Ota {
4995d8c720dSMasaki Ota 	int ret;
5005992262dSMasaki Ota 	u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
5015992262dSMasaki Ota 	u8 pitch_x, pitch_y, resolution;
5025d8c720dSMasaki Ota 
5035d8c720dSMasaki Ota 	/* Device initialization */
5045d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5055992262dSMasaki Ota 			&dev_ctrl, 0, true);
5065d8c720dSMasaki Ota 	if (ret < 0) {
5075d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
5085d8c720dSMasaki Ota 		goto exit;
5095d8c720dSMasaki Ota 	}
5105d8c720dSMasaki Ota 
5115992262dSMasaki Ota 	dev_ctrl &= ~U1_DISABLE_DEV;
5125992262dSMasaki Ota 	dev_ctrl |= U1_TP_ABS_MODE;
5135d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5145992262dSMasaki Ota 			NULL, dev_ctrl, false);
5155d8c720dSMasaki Ota 	if (ret < 0) {
5165d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
5175d8c720dSMasaki Ota 		goto exit;
5185d8c720dSMasaki Ota 	}
5195d8c720dSMasaki Ota 
5205d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
5215992262dSMasaki Ota 			&sen_line_num_x, 0, true);
5225d8c720dSMasaki Ota 	if (ret < 0) {
5235d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
5245d8c720dSMasaki Ota 		goto exit;
5255d8c720dSMasaki Ota 	}
5265d8c720dSMasaki Ota 
5275d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
5285992262dSMasaki Ota 			&sen_line_num_y, 0, true);
5295d8c720dSMasaki Ota 	if (ret < 0) {
5305d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
5315d8c720dSMasaki Ota 		goto exit;
5325d8c720dSMasaki Ota 	}
5335d8c720dSMasaki Ota 
5345d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
5355992262dSMasaki Ota 			&pitch_x, 0, true);
5365d8c720dSMasaki Ota 	if (ret < 0) {
5375d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
5385d8c720dSMasaki Ota 		goto exit;
5395d8c720dSMasaki Ota 	}
5405d8c720dSMasaki Ota 
5415d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
5425992262dSMasaki Ota 			&pitch_y, 0, true);
5435d8c720dSMasaki Ota 	if (ret < 0) {
5445d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
5455d8c720dSMasaki Ota 		goto exit;
5465d8c720dSMasaki Ota 	}
5475d8c720dSMasaki Ota 
5485d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
5495992262dSMasaki Ota 		&resolution, 0, true);
5505d8c720dSMasaki Ota 	if (ret < 0) {
5515d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
5525d8c720dSMasaki Ota 		goto exit;
5535d8c720dSMasaki Ota 	}
5545d8c720dSMasaki Ota 	pri_data->x_active_len_mm =
5555992262dSMasaki Ota 		(pitch_x * (sen_line_num_x - 1)) / 10;
5565d8c720dSMasaki Ota 	pri_data->y_active_len_mm =
5575992262dSMasaki Ota 		(pitch_y * (sen_line_num_y - 1)) / 10;
5585d8c720dSMasaki Ota 
5595d8c720dSMasaki Ota 	pri_data->x_max =
5605992262dSMasaki Ota 		(resolution << 2) * (sen_line_num_x - 1);
561c7083d3fSMasaki Ota 	pri_data->x_min = 1;
5625d8c720dSMasaki Ota 	pri_data->y_max =
5635992262dSMasaki Ota 		(resolution << 2) * (sen_line_num_y - 1);
564c7083d3fSMasaki Ota 	pri_data->y_min = 1;
5655d8c720dSMasaki Ota 
5665d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
5675992262dSMasaki Ota 			&tmp, 0, true);
5685d8c720dSMasaki Ota 	if (ret < 0) {
5695d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
5705d8c720dSMasaki Ota 		goto exit;
5715d8c720dSMasaki Ota 	}
5725992262dSMasaki Ota 	if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
5735992262dSMasaki Ota 		pri_data->btn_cnt = (tmp & 0x0F);
574c7083d3fSMasaki Ota 	} else {
575c7083d3fSMasaki Ota 		/* Button pad */
576c7083d3fSMasaki Ota 		pri_data->btn_cnt = 1;
577c7083d3fSMasaki Ota 	}
5785d8c720dSMasaki Ota 
5795d8c720dSMasaki Ota 	pri_data->has_sp = 0;
5805d8c720dSMasaki Ota 	/* Check StickPointer device */
5815d8c720dSMasaki Ota 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
5825992262dSMasaki Ota 			&tmp, 0, true);
5835d8c720dSMasaki Ota 	if (ret < 0) {
5845d8c720dSMasaki Ota 		dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
5855d8c720dSMasaki Ota 		goto exit;
5865d8c720dSMasaki Ota 	}
5875992262dSMasaki Ota 	if (tmp & U1_DEVTYPE_SP_SUPPORT) {
5885992262dSMasaki Ota 		dev_ctrl |= U1_SP_ABS_MODE;
5895d8c720dSMasaki Ota 		ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5905992262dSMasaki Ota 			NULL, dev_ctrl, false);
5915d8c720dSMasaki Ota 		if (ret < 0) {
5925d8c720dSMasaki Ota 			dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
5935d8c720dSMasaki Ota 			goto exit;
5945d8c720dSMasaki Ota 		}
5955d8c720dSMasaki Ota 
5965d8c720dSMasaki Ota 		ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
5975d8c720dSMasaki Ota 			&pri_data->sp_btn_info, 0, true);
5985d8c720dSMasaki Ota 		if (ret < 0) {
5995d8c720dSMasaki Ota 			dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
6005d8c720dSMasaki Ota 			goto exit;
6015d8c720dSMasaki Ota 		}
6025d8c720dSMasaki Ota 		pri_data->has_sp = 1;
6035d8c720dSMasaki Ota 	}
604c7083d3fSMasaki Ota 	pri_data->max_fingers = 5;
6055d8c720dSMasaki Ota exit:
6065d8c720dSMasaki Ota 	return ret;
6075d8c720dSMasaki Ota }
6085d8c720dSMasaki Ota 
T4_init(struct hid_device * hdev,struct alps_dev * pri_data)60973196ebeSMasaki Ota static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
61073196ebeSMasaki Ota {
61173196ebeSMasaki Ota 	int ret;
61273196ebeSMasaki Ota 	u8 tmp, sen_line_num_x, sen_line_num_y;
61373196ebeSMasaki Ota 
61473196ebeSMasaki Ota 	ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
61573196ebeSMasaki Ota 	if (ret < 0) {
61673196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
61773196ebeSMasaki Ota 		goto exit;
61873196ebeSMasaki Ota 	}
61973196ebeSMasaki Ota 	sen_line_num_x = 16 + ((tmp & 0x0F)  | (tmp & 0x08 ? 0xF0 : 0));
62073196ebeSMasaki Ota 	sen_line_num_y = 12 + (((tmp & 0xF0) >> 4)  | (tmp & 0x80 ? 0xF0 : 0));
62173196ebeSMasaki Ota 
62273196ebeSMasaki Ota 	pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
62373196ebeSMasaki Ota 	pri_data->x_min = T4_COUNT_PER_ELECTRODE;
62473196ebeSMasaki Ota 	pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
62573196ebeSMasaki Ota 	pri_data->y_min = T4_COUNT_PER_ELECTRODE;
62673196ebeSMasaki Ota 	pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
62773196ebeSMasaki Ota 	pri_data->btn_cnt = 1;
62873196ebeSMasaki Ota 
62973196ebeSMasaki Ota 	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
63073196ebeSMasaki Ota 	if (ret < 0) {
63173196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
63273196ebeSMasaki Ota 		goto exit;
63373196ebeSMasaki Ota 	}
63473196ebeSMasaki Ota 	tmp |= 0x02;
63573196ebeSMasaki Ota 	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
63673196ebeSMasaki Ota 	if (ret < 0) {
63773196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
63873196ebeSMasaki Ota 		goto exit;
63973196ebeSMasaki Ota 	}
64073196ebeSMasaki Ota 
64173196ebeSMasaki Ota 	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
64273196ebeSMasaki Ota 					NULL, T4_I2C_ABS, false);
64373196ebeSMasaki Ota 	if (ret < 0) {
64473196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
64573196ebeSMasaki Ota 		goto exit;
64673196ebeSMasaki Ota 	}
64773196ebeSMasaki Ota 
64873196ebeSMasaki Ota 	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
64973196ebeSMasaki Ota 				T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
65073196ebeSMasaki Ota 	if (ret < 0) {
65173196ebeSMasaki Ota 		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
65273196ebeSMasaki Ota 		goto exit;
65373196ebeSMasaki Ota 	}
65473196ebeSMasaki Ota 	pri_data->max_fingers = 5;
65573196ebeSMasaki Ota 	pri_data->has_sp = 0;
65673196ebeSMasaki Ota exit:
65773196ebeSMasaki Ota 	return ret;
65873196ebeSMasaki Ota }
65973196ebeSMasaki Ota 
alps_sp_open(struct input_dev * dev)6607dd8db68SBenjamin Tissoires static int alps_sp_open(struct input_dev *dev)
6617dd8db68SBenjamin Tissoires {
6627dd8db68SBenjamin Tissoires 	struct hid_device *hid = input_get_drvdata(dev);
6637dd8db68SBenjamin Tissoires 
6647dd8db68SBenjamin Tissoires 	return hid_hw_open(hid);
6657dd8db68SBenjamin Tissoires }
6667dd8db68SBenjamin Tissoires 
alps_sp_close(struct input_dev * dev)6677dd8db68SBenjamin Tissoires static void alps_sp_close(struct input_dev *dev)
6687dd8db68SBenjamin Tissoires {
6697dd8db68SBenjamin Tissoires 	struct hid_device *hid = input_get_drvdata(dev);
6707dd8db68SBenjamin Tissoires 
6717dd8db68SBenjamin Tissoires 	hid_hw_close(hid);
6727dd8db68SBenjamin Tissoires }
6737dd8db68SBenjamin Tissoires 
alps_input_configured(struct hid_device * hdev,struct hid_input * hi)6742562756dSMasaki Ota static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
6752562756dSMasaki Ota {
67673196ebeSMasaki Ota 	struct alps_dev *data = hid_get_drvdata(hdev);
6772562756dSMasaki Ota 	struct input_dev *input = hi->input, *input2;
6782562756dSMasaki Ota 	int ret;
6792562756dSMasaki Ota 	int res_x, res_y, i;
6802562756dSMasaki Ota 
6812562756dSMasaki Ota 	data->input = input;
6822562756dSMasaki Ota 
6832562756dSMasaki Ota 	hid_dbg(hdev, "Opening low level driver\n");
6842562756dSMasaki Ota 	ret = hid_hw_open(hdev);
6852562756dSMasaki Ota 	if (ret)
6862562756dSMasaki Ota 		return ret;
6872562756dSMasaki Ota 
6882562756dSMasaki Ota 	/* Allow incoming hid reports */
6892562756dSMasaki Ota 	hid_device_io_start(hdev);
69073196ebeSMasaki Ota 	switch (data->dev_type) {
69173196ebeSMasaki Ota 	case T4:
69273196ebeSMasaki Ota 		ret = T4_init(hdev, data);
69373196ebeSMasaki Ota 		break;
69473196ebeSMasaki Ota 	case U1:
6955d8c720dSMasaki Ota 		ret = u1_init(hdev, data);
69673196ebeSMasaki Ota 		break;
69773196ebeSMasaki Ota 	default:
69873196ebeSMasaki Ota 		break;
69973196ebeSMasaki Ota 	}
7005d8c720dSMasaki Ota 
7015d8c720dSMasaki Ota 	if (ret)
7022562756dSMasaki Ota 		goto exit;
7032562756dSMasaki Ota 
7042562756dSMasaki Ota 	__set_bit(EV_ABS, input->evbit);
705c7083d3fSMasaki Ota 	input_set_abs_params(input, ABS_MT_POSITION_X,
706c7083d3fSMasaki Ota 						data->x_min, data->x_max, 0, 0);
707c7083d3fSMasaki Ota 	input_set_abs_params(input, ABS_MT_POSITION_Y,
708c7083d3fSMasaki Ota 						data->y_min, data->y_max, 0, 0);
7092562756dSMasaki Ota 
710ce6abcf8SMasaki Ota 	if (data->x_active_len_mm && data->y_active_len_mm) {
711ce6abcf8SMasaki Ota 		res_x = (data->x_max - 1) / data->x_active_len_mm;
712ce6abcf8SMasaki Ota 		res_y = (data->y_max - 1) / data->y_active_len_mm;
7132562756dSMasaki Ota 
7142562756dSMasaki Ota 		input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
7152562756dSMasaki Ota 		input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
7162562756dSMasaki Ota 	}
7172562756dSMasaki Ota 
7182562756dSMasaki Ota 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
7192562756dSMasaki Ota 
720c7083d3fSMasaki Ota 	input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
7212562756dSMasaki Ota 
7222562756dSMasaki Ota 	__set_bit(EV_KEY, input->evbit);
723c7083d3fSMasaki Ota 
724c7083d3fSMasaki Ota 	if (data->btn_cnt == 1)
7252562756dSMasaki Ota 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
7262562756dSMasaki Ota 
727ce6abcf8SMasaki Ota 	for (i = 0; i < data->btn_cnt; i++)
7282562756dSMasaki Ota 		__set_bit(BTN_LEFT + i, input->keybit);
7292562756dSMasaki Ota 
7302562756dSMasaki Ota 	/* Stick device initialization */
7315d8c720dSMasaki Ota 	if (data->has_sp) {
7322562756dSMasaki Ota 		input2 = input_allocate_device();
7332562756dSMasaki Ota 		if (!input2) {
7348d2e77b3SChristophe JAILLET 			ret = -ENOMEM;
7352562756dSMasaki Ota 			goto exit;
7362562756dSMasaki Ota 		}
7372562756dSMasaki Ota 
738819d64e5SMasaki Ota 		data->input2 = input2;
7392562756dSMasaki Ota 		input2->phys = input->phys;
7402562756dSMasaki Ota 		input2->name = "DualPoint Stick";
7412562756dSMasaki Ota 		input2->id.bustype = BUS_I2C;
7422562756dSMasaki Ota 		input2->id.vendor  = input->id.vendor;
7432562756dSMasaki Ota 		input2->id.product = input->id.product;
7442562756dSMasaki Ota 		input2->id.version = input->id.version;
7452562756dSMasaki Ota 		input2->dev.parent = input->dev.parent;
7462562756dSMasaki Ota 
7477dd8db68SBenjamin Tissoires 		input_set_drvdata(input2, hdev);
7487dd8db68SBenjamin Tissoires 		input2->open = alps_sp_open;
7497dd8db68SBenjamin Tissoires 		input2->close = alps_sp_close;
7507dd8db68SBenjamin Tissoires 
7512562756dSMasaki Ota 		__set_bit(EV_KEY, input2->evbit);
752ce6abcf8SMasaki Ota 		data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
753ce6abcf8SMasaki Ota 		for (i = 0; i < data->sp_btn_cnt; i++)
7542562756dSMasaki Ota 			__set_bit(BTN_LEFT + i, input2->keybit);
7552562756dSMasaki Ota 
7562562756dSMasaki Ota 		__set_bit(EV_REL, input2->evbit);
7572562756dSMasaki Ota 		__set_bit(REL_X, input2->relbit);
7582562756dSMasaki Ota 		__set_bit(REL_Y, input2->relbit);
7592562756dSMasaki Ota 		__set_bit(INPUT_PROP_POINTER, input2->propbit);
7602562756dSMasaki Ota 		__set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
7612562756dSMasaki Ota 
762c7083d3fSMasaki Ota 		if (input_register_device(data->input2)) {
7632562756dSMasaki Ota 			input_free_device(input2);
764fa8ba6e5SJia-Ju Bai 			ret = -ENOENT;
7652562756dSMasaki Ota 			goto exit;
7662562756dSMasaki Ota 		}
7672562756dSMasaki Ota 	}
7682562756dSMasaki Ota 
7692562756dSMasaki Ota exit:
7702562756dSMasaki Ota 	hid_device_io_stop(hdev);
7712562756dSMasaki Ota 	hid_hw_close(hdev);
7722562756dSMasaki Ota 	return ret;
7732562756dSMasaki Ota }
7742562756dSMasaki Ota 
alps_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)7752562756dSMasaki Ota static int alps_input_mapping(struct hid_device *hdev,
7762562756dSMasaki Ota 		struct hid_input *hi, struct hid_field *field,
7772562756dSMasaki Ota 		struct hid_usage *usage, unsigned long **bit, int *max)
7782562756dSMasaki Ota {
7792562756dSMasaki Ota 	return -1;
7802562756dSMasaki Ota }
7812562756dSMasaki Ota 
alps_probe(struct hid_device * hdev,const struct hid_device_id * id)7822562756dSMasaki Ota static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
7832562756dSMasaki Ota {
78473196ebeSMasaki Ota 	struct alps_dev *data = NULL;
7852562756dSMasaki Ota 	int ret;
78673196ebeSMasaki Ota 	data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
7872562756dSMasaki Ota 	if (!data)
7882562756dSMasaki Ota 		return -ENOMEM;
7892562756dSMasaki Ota 
7902562756dSMasaki Ota 	data->hdev = hdev;
7912562756dSMasaki Ota 	hid_set_drvdata(hdev, data);
7922562756dSMasaki Ota 
7932562756dSMasaki Ota 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
7942562756dSMasaki Ota 
7952562756dSMasaki Ota 	ret = hid_parse(hdev);
7962562756dSMasaki Ota 	if (ret) {
7972562756dSMasaki Ota 		hid_err(hdev, "parse failed\n");
7982562756dSMasaki Ota 		return ret;
7992562756dSMasaki Ota 	}
8002562756dSMasaki Ota 
80173196ebeSMasaki Ota 	switch (hdev->product) {
80273196ebeSMasaki Ota 	case HID_DEVICE_ID_ALPS_T4_BTNLESS:
80373196ebeSMasaki Ota 		data->dev_type = T4;
80473196ebeSMasaki Ota 		break;
80573196ebeSMasaki Ota 	case HID_DEVICE_ID_ALPS_U1_DUAL:
806287b8e11SMasaki Ota 	case HID_DEVICE_ID_ALPS_U1:
807185af3e7SJiri Kosina 	case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
80873196ebeSMasaki Ota 		data->dev_type = U1;
80973196ebeSMasaki Ota 		break;
81073196ebeSMasaki Ota 	default:
81173196ebeSMasaki Ota 		data->dev_type = UNKNOWN;
81273196ebeSMasaki Ota 	}
81373196ebeSMasaki Ota 
8142562756dSMasaki Ota 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
8152562756dSMasaki Ota 	if (ret) {
8162562756dSMasaki Ota 		hid_err(hdev, "hw start failed\n");
8172562756dSMasaki Ota 		return ret;
8182562756dSMasaki Ota 	}
8192562756dSMasaki Ota 
8202562756dSMasaki Ota 	return 0;
8212562756dSMasaki Ota }
8222562756dSMasaki Ota 
8232562756dSMasaki Ota static const struct hid_device_id alps_id[] = {
8242562756dSMasaki Ota 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
825819d64e5SMasaki Ota 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
82673196ebeSMasaki Ota 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
827287b8e11SMasaki Ota 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
828287b8e11SMasaki Ota 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
829*1117d182SArtem Borisov 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY) },
830*1117d182SArtem Borisov 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
83173196ebeSMasaki Ota 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
8322562756dSMasaki Ota 	{ }
8332562756dSMasaki Ota };
8342562756dSMasaki Ota MODULE_DEVICE_TABLE(hid, alps_id);
8352562756dSMasaki Ota 
8362562756dSMasaki Ota static struct hid_driver alps_driver = {
8372562756dSMasaki Ota 	.name = "hid-alps",
8382562756dSMasaki Ota 	.id_table		= alps_id,
8392562756dSMasaki Ota 	.probe			= alps_probe,
8402562756dSMasaki Ota 	.raw_event		= alps_raw_event,
8412562756dSMasaki Ota 	.input_mapping		= alps_input_mapping,
8422562756dSMasaki Ota 	.input_configured	= alps_input_configured,
8432562756dSMasaki Ota #ifdef CONFIG_PM
8442562756dSMasaki Ota 	.resume			= alps_post_resume,
8452562756dSMasaki Ota 	.reset_resume		= alps_post_reset,
8462562756dSMasaki Ota #endif
8472562756dSMasaki Ota };
8482562756dSMasaki Ota 
8492562756dSMasaki Ota module_hid_driver(alps_driver);
8502562756dSMasaki Ota 
8512562756dSMasaki Ota MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
8522562756dSMasaki Ota MODULE_DESCRIPTION("ALPS HID driver");
8532562756dSMasaki Ota MODULE_LICENSE("GPL");
854