blob: d33f5abc8f64d271bbd998801273e3aa1cb691f3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masaki Ota25627562016-06-16 18:45:57 +09002/*
3 * Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
Masaki Ota25627562016-06-16 18:45:57 +09004 */
5
6#include <linux/kernel.h>
7#include <linux/hid.h>
8#include <linux/input.h>
9#include <linux/input/mt.h>
10#include <linux/module.h>
11#include <asm/unaligned.h>
12#include "hid-ids.h"
13
14/* ALPS Device Product ID */
15#define HID_PRODUCT_ID_T3_BTNLESS 0xD0C0
16#define HID_PRODUCT_ID_COSMO 0x1202
17#define HID_PRODUCT_ID_U1_PTP_1 0x1207
18#define HID_PRODUCT_ID_U1 0x1209
19#define HID_PRODUCT_ID_U1_PTP_2 0x120A
20#define HID_PRODUCT_ID_U1_DUAL 0x120B
21#define HID_PRODUCT_ID_T4_BTNLESS 0x120C
22
23#define DEV_SINGLEPOINT 0x01
24#define DEV_DUALPOINT 0x02
25
26#define U1_MOUSE_REPORT_ID 0x01 /* Mouse data ReportID */
27#define U1_ABSOLUTE_REPORT_ID 0x03 /* Absolute data ReportID */
Caiyuan Xieaa3c4392020-05-22 05:06:10 -040028#define U1_ABSOLUTE_REPORT_ID_SECD 0x02 /* FW-PTP Absolute data ReportID */
Masaki Ota25627562016-06-16 18:45:57 +090029#define U1_FEATURE_REPORT_ID 0x05 /* Feature ReportID */
30#define U1_SP_ABSOLUTE_REPORT_ID 0x06 /* Feature ReportID */
31
32#define U1_FEATURE_REPORT_LEN 0x08 /* Feature Report Length */
33#define U1_FEATURE_REPORT_LEN_ALL 0x0A
34#define U1_CMD_REGISTER_READ 0xD1
35#define U1_CMD_REGISTER_WRITE 0xD2
36
37#define U1_DEVTYPE_SP_SUPPORT 0x10 /* SP Support */
38#define U1_DISABLE_DEV 0x01
39#define U1_TP_ABS_MODE 0x02
40#define U1_SP_ABS_MODE 0x80
41
42#define ADDRESS_U1_DEV_CTRL_1 0x00800040
43#define ADDRESS_U1_DEVICE_TYP 0x00800043
44#define ADDRESS_U1_NUM_SENS_X 0x00800047
45#define ADDRESS_U1_NUM_SENS_Y 0x00800048
46#define ADDRESS_U1_PITCH_SENS_X 0x00800049
47#define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
48#define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
49#define ADDRESS_U1_PAD_BTN 0x00800052
50#define ADDRESS_U1_SP_BTN 0x0080009F
51
Masaki Ota73196eb2017-10-06 11:53:17 +090052#define T4_INPUT_REPORT_LEN sizeof(struct t4_input_report)
53#define T4_FEATURE_REPORT_LEN T4_INPUT_REPORT_LEN
54#define T4_FEATURE_REPORT_ID 7
55#define T4_CMD_REGISTER_READ 0x08
56#define T4_CMD_REGISTER_WRITE 0x07
57
58#define T4_ADDRESS_BASE 0xC2C0
59#define PRM_SYS_CONFIG_1 (T4_ADDRESS_BASE + 0x0002)
60#define T4_PRM_FEED_CONFIG_1 (T4_ADDRESS_BASE + 0x0004)
61#define T4_PRM_FEED_CONFIG_4 (T4_ADDRESS_BASE + 0x001A)
62#define T4_PRM_ID_CONFIG_3 (T4_ADDRESS_BASE + 0x00B0)
63
64
65#define T4_FEEDCFG4_ADVANCED_ABS_ENABLE 0x01
66#define T4_I2C_ABS 0x78
67
68#define T4_COUNT_PER_ELECTRODE 256
Masaki Ota25627562016-06-16 18:45:57 +090069#define MAX_TOUCHES 5
70
Masaki Ota73196eb2017-10-06 11:53:17 +090071enum dev_num {
72 U1,
73 T4,
74 UNKNOWN,
75};
Masaki Ota25627562016-06-16 18:45:57 +090076/**
77 * struct u1_data
78 *
79 * @input: pointer to the kernel input device
80 * @input2: pointer to the kernel input2 device
81 * @hdev: pointer to the struct hid_device
82 *
Masaki Ota25627562016-06-16 18:45:57 +090083 * @dev_type: device type
Masaki Ota59922622017-10-06 11:53:16 +090084 * @max_fingers: total number of fingers
85 * @has_sp: boolean of sp existense
86 * @sp_btn_info: button information
Masaki Ota25627562016-06-16 18:45:57 +090087 * @x_active_len_mm: active area length of X (mm)
88 * @y_active_len_mm: active area length of Y (mm)
89 * @x_max: maximum x coordinate value
90 * @y_max: maximum y coordinate value
Masaki Otac7083d32017-10-06 11:53:15 +090091 * @x_min: minimum x coordinate value
92 * @y_min: minimum y coordinate value
Masaki Ota25627562016-06-16 18:45:57 +090093 * @btn_cnt: number of buttons
94 * @sp_btn_cnt: number of stick buttons
95 */
Masaki Ota73196eb2017-10-06 11:53:17 +090096struct alps_dev {
Masaki Ota25627562016-06-16 18:45:57 +090097 struct input_dev *input;
98 struct input_dev *input2;
99 struct hid_device *hdev;
100
Masaki Ota73196eb2017-10-06 11:53:17 +0900101 enum dev_num dev_type;
Masaki Ota59922622017-10-06 11:53:16 +0900102 u8 max_fingers;
103 u8 has_sp;
Masaki Ota25627562016-06-16 18:45:57 +0900104 u8 sp_btn_info;
105 u32 x_active_len_mm;
106 u32 y_active_len_mm;
107 u32 x_max;
108 u32 y_max;
Masaki Otac7083d32017-10-06 11:53:15 +0900109 u32 x_min;
110 u32 y_min;
Masaki Ota25627562016-06-16 18:45:57 +0900111 u32 btn_cnt;
112 u32 sp_btn_cnt;
113};
114
Masaki Ota73196eb2017-10-06 11:53:17 +0900115struct t4_contact_data {
116 u8 palm;
117 u8 x_lo;
118 u8 x_hi;
119 u8 y_lo;
120 u8 y_hi;
121};
122
123struct t4_input_report {
124 u8 reportID;
125 u8 numContacts;
126 struct t4_contact_data contact[5];
127 u8 button;
128 u8 track[5];
129 u8 zx[5], zy[5];
130 u8 palmTime[5];
131 u8 kilroy;
132 u16 timeStamp;
133};
134
135static u16 t4_calc_check_sum(u8 *buffer,
136 unsigned long offset, unsigned long length)
137{
138 u16 sum1 = 0xFF, sum2 = 0xFF;
139 unsigned long i = 0;
140
141 if (offset + length >= 50)
142 return 0;
143
144 while (length > 0) {
145 u32 tlen = length > 20 ? 20 : length;
146
147 length -= tlen;
148
149 do {
150 sum1 += buffer[offset + i];
151 sum2 += sum1;
152 i++;
153 } while (--tlen > 0);
154
155 sum1 = (sum1 & 0xFF) + (sum1 >> 8);
156 sum2 = (sum2 & 0xFF) + (sum2 >> 8);
157 }
158
159 sum1 = (sum1 & 0xFF) + (sum1 >> 8);
160 sum2 = (sum2 & 0xFF) + (sum2 >> 8);
161
162 return(sum2 << 8 | sum1);
163}
164
165static int t4_read_write_register(struct hid_device *hdev, u32 address,
166 u8 *read_val, u8 write_val, bool read_flag)
167{
168 int ret;
169 u16 check_sum;
170 u8 *input;
Christophe JAILLETedb6cb32018-03-19 21:53:27 +0100171 u8 *readbuf = NULL;
Masaki Ota73196eb2017-10-06 11:53:17 +0900172
173 input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
174 if (!input)
175 return -ENOMEM;
176
177 input[0] = T4_FEATURE_REPORT_ID;
178 if (read_flag) {
179 input[1] = T4_CMD_REGISTER_READ;
180 input[8] = 0x00;
181 } else {
182 input[1] = T4_CMD_REGISTER_WRITE;
183 input[8] = write_val;
184 }
185 put_unaligned_le32(address, input + 2);
186 input[6] = 1;
187 input[7] = 0;
188
189 /* Calculate the checksum */
190 check_sum = t4_calc_check_sum(input, 1, 8);
191 input[9] = (u8)check_sum;
192 input[10] = (u8)(check_sum >> 8);
193 input[11] = 0;
194
195 ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
196 T4_FEATURE_REPORT_LEN,
197 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
198
199 if (ret < 0) {
200 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
201 goto exit;
202 }
203
Masaki Ota73196eb2017-10-06 11:53:17 +0900204 if (read_flag) {
Christophe JAILLETedb6cb32018-03-19 21:53:27 +0100205 readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
Masaki Ota73196eb2017-10-06 11:53:17 +0900206 if (!readbuf) {
207 ret = -ENOMEM;
208 goto exit;
209 }
210
211 ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
212 T4_FEATURE_REPORT_LEN,
213 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
214 if (ret < 0) {
215 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
216 goto exit_readbuf;
217 }
218
Christophe JAILLET605f0772018-03-19 21:53:26 +0100219 ret = -EINVAL;
220
Masaki Ota73196eb2017-10-06 11:53:17 +0900221 if (*(u32 *)&readbuf[6] != address) {
222 dev_err(&hdev->dev, "read register address error (%x,%x)\n",
Christophe JAILLETa317e552018-03-19 21:53:29 +0100223 *(u32 *)&readbuf[6], address);
Masaki Ota73196eb2017-10-06 11:53:17 +0900224 goto exit_readbuf;
225 }
226
227 if (*(u16 *)&readbuf[10] != 1) {
228 dev_err(&hdev->dev, "read register size error (%x)\n",
Christophe JAILLETa317e552018-03-19 21:53:29 +0100229 *(u16 *)&readbuf[10]);
Masaki Ota73196eb2017-10-06 11:53:17 +0900230 goto exit_readbuf;
231 }
232
233 check_sum = t4_calc_check_sum(readbuf, 6, 7);
234 if (*(u16 *)&readbuf[13] != check_sum) {
235 dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
Christophe JAILLETa317e552018-03-19 21:53:29 +0100236 *(u16 *)&readbuf[13], check_sum);
Masaki Ota73196eb2017-10-06 11:53:17 +0900237 goto exit_readbuf;
238 }
239
240 *read_val = readbuf[12];
241 }
242
243 ret = 0;
244
245exit_readbuf:
246 kfree(readbuf);
247exit:
248 kfree(input);
249 return ret;
250}
251
Masaki Ota25627562016-06-16 18:45:57 +0900252static int u1_read_write_register(struct hid_device *hdev, u32 address,
253 u8 *read_val, u8 write_val, bool read_flag)
254{
255 int ret, i;
256 u8 check_sum;
257 u8 *input;
258 u8 *readbuf;
259
Masaki Ota819d64e2016-06-22 13:11:08 +0900260 input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
Masaki Ota25627562016-06-16 18:45:57 +0900261 if (!input)
262 return -ENOMEM;
263
Masaki Ota25627562016-06-16 18:45:57 +0900264 input[0] = U1_FEATURE_REPORT_ID;
265 if (read_flag) {
266 input[1] = U1_CMD_REGISTER_READ;
267 input[6] = 0x00;
268 } else {
269 input[1] = U1_CMD_REGISTER_WRITE;
270 input[6] = write_val;
271 }
272
273 put_unaligned_le32(address, input + 2);
274
275 /* Calculate the checksum */
276 check_sum = U1_FEATURE_REPORT_LEN_ALL;
277 for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
278 check_sum += input[i];
279
280 input[7] = check_sum;
281 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
Masaki Ota819d64e2016-06-22 13:11:08 +0900282 U1_FEATURE_REPORT_LEN,
283 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Masaki Ota25627562016-06-16 18:45:57 +0900284
285 if (ret < 0) {
286 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
287 goto exit;
288 }
289
290 if (read_flag) {
Masaki Ota819d64e2016-06-22 13:11:08 +0900291 readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
292 if (!readbuf) {
Axel Lin7ee2eaa2016-09-11 12:09:01 +0800293 ret = -ENOMEM;
294 goto exit;
Masaki Ota819d64e2016-06-22 13:11:08 +0900295 }
296
Masaki Ota25627562016-06-16 18:45:57 +0900297 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
Masaki Ota819d64e2016-06-22 13:11:08 +0900298 U1_FEATURE_REPORT_LEN,
Jiri Kosina63b3a7d2016-06-20 11:16:18 +0200299 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Masaki Ota25627562016-06-16 18:45:57 +0900300
301 if (ret < 0) {
302 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
Axel Lin7ee2eaa2016-09-11 12:09:01 +0800303 kfree(readbuf);
Masaki Ota25627562016-06-16 18:45:57 +0900304 goto exit;
305 }
306
307 *read_val = readbuf[6];
Masaki Ota819d64e2016-06-22 13:11:08 +0900308
309 kfree(readbuf);
Masaki Ota25627562016-06-16 18:45:57 +0900310 }
311
Masaki Ota819d64e2016-06-22 13:11:08 +0900312 ret = 0;
Masaki Ota25627562016-06-16 18:45:57 +0900313
314exit:
315 kfree(input);
Masaki Ota25627562016-06-16 18:45:57 +0900316 return ret;
317}
318
Masaki Ota73196eb2017-10-06 11:53:17 +0900319static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
320{
321 unsigned int x, y, z;
322 int i;
323 struct t4_input_report *p_report = (struct t4_input_report *)data;
324
325 if (!data)
326 return 0;
327 for (i = 0; i < hdata->max_fingers; i++) {
328 x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
329 y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
330 y = hdata->y_max - y + hdata->y_min;
331 z = (p_report->contact[i].palm < 0x80 &&
332 p_report->contact[i].palm > 0) * 62;
333 if (x == 0xffff) {
334 x = 0;
335 y = 0;
336 z = 0;
337 }
338 input_mt_slot(hdata->input, i);
339
340 input_mt_report_slot_state(hdata->input,
341 MT_TOOL_FINGER, z != 0);
342
343 if (!z)
344 continue;
345
346 input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
347 input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
348 input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
349 }
350 input_mt_sync_frame(hdata->input);
351
352 input_report_key(hdata->input, BTN_LEFT, p_report->button);
353
354 input_sync(hdata->input);
355 return 1;
356}
357
358static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
Masaki Ota25627562016-06-16 18:45:57 +0900359{
Masaki Ota819d64e2016-06-22 13:11:08 +0900360 unsigned int x, y, z;
361 int i;
362 short sp_x, sp_y;
Masaki Ota25627562016-06-16 18:45:57 +0900363
Masaki Ota73196eb2017-10-06 11:53:17 +0900364 if (!data)
365 return 0;
Masaki Ota25627562016-06-16 18:45:57 +0900366 switch (data[0]) {
367 case U1_MOUSE_REPORT_ID:
368 break;
369 case U1_FEATURE_REPORT_ID:
370 break;
371 case U1_ABSOLUTE_REPORT_ID:
Caiyuan Xieaa3c4392020-05-22 05:06:10 -0400372 case U1_ABSOLUTE_REPORT_ID_SECD:
Masaki Ota73196eb2017-10-06 11:53:17 +0900373 for (i = 0; i < hdata->max_fingers; i++) {
Masaki Ota819d64e2016-06-22 13:11:08 +0900374 u8 *contact = &data[i * 5];
375
376 x = get_unaligned_le16(contact + 3);
377 y = get_unaligned_le16(contact + 5);
378 z = contact[7] & 0x7F;
Masaki Ota25627562016-06-16 18:45:57 +0900379
380 input_mt_slot(hdata->input, i);
381
Masaki Ota819d64e2016-06-22 13:11:08 +0900382 if (z != 0) {
Masaki Ota25627562016-06-16 18:45:57 +0900383 input_mt_report_slot_state(hdata->input,
384 MT_TOOL_FINGER, 1);
Masaki Ota9a54cf42016-09-27 14:04:37 +0900385 input_report_abs(hdata->input,
386 ABS_MT_POSITION_X, x);
387 input_report_abs(hdata->input,
388 ABS_MT_POSITION_Y, y);
389 input_report_abs(hdata->input,
390 ABS_MT_PRESSURE, z);
Masaki Ota25627562016-06-16 18:45:57 +0900391 } else {
392 input_mt_report_slot_state(hdata->input,
393 MT_TOOL_FINGER, 0);
Masaki Ota25627562016-06-16 18:45:57 +0900394 }
Masaki Ota25627562016-06-16 18:45:57 +0900395 }
396
397 input_mt_sync_frame(hdata->input);
Masaki Ota25627562016-06-16 18:45:57 +0900398
Masaki Ota819d64e2016-06-22 13:11:08 +0900399 input_report_key(hdata->input, BTN_LEFT,
400 data[1] & 0x1);
401 input_report_key(hdata->input, BTN_RIGHT,
402 (data[1] & 0x2));
403 input_report_key(hdata->input, BTN_MIDDLE,
404 (data[1] & 0x4));
405
406 input_sync(hdata->input);
Masaki Ota25627562016-06-16 18:45:57 +0900407
408 return 1;
409
410 case U1_SP_ABSOLUTE_REPORT_ID:
Masaki Ota819d64e2016-06-22 13:11:08 +0900411 sp_x = get_unaligned_le16(data+2);
412 sp_y = get_unaligned_le16(data+4);
Masaki Ota25627562016-06-16 18:45:57 +0900413
414 sp_x = sp_x / 8;
415 sp_y = sp_y / 8;
416
Masaki Ota819d64e2016-06-22 13:11:08 +0900417 input_report_rel(hdata->input2, REL_X, sp_x);
418 input_report_rel(hdata->input2, REL_Y, sp_y);
Masaki Ota25627562016-06-16 18:45:57 +0900419
Masaki Ota819d64e2016-06-22 13:11:08 +0900420 input_report_key(hdata->input2, BTN_LEFT,
421 data[1] & 0x1);
422 input_report_key(hdata->input2, BTN_RIGHT,
423 (data[1] & 0x2));
424 input_report_key(hdata->input2, BTN_MIDDLE,
425 (data[1] & 0x4));
Masaki Ota25627562016-06-16 18:45:57 +0900426
Masaki Ota819d64e2016-06-22 13:11:08 +0900427 input_sync(hdata->input2);
Masaki Ota25627562016-06-16 18:45:57 +0900428
429 return 1;
430 }
431
432 return 0;
433}
434
Masaki Ota73196eb2017-10-06 11:53:17 +0900435static int alps_raw_event(struct hid_device *hdev,
436 struct hid_report *report, u8 *data, int size)
Masaki Ota25627562016-06-16 18:45:57 +0900437{
Masaki Ota73196eb2017-10-06 11:53:17 +0900438 int ret = 0;
439 struct alps_dev *hdata = hid_get_drvdata(hdev);
440
441 switch (hdev->product) {
442 case HID_PRODUCT_ID_T4_BTNLESS:
443 ret = t4_raw_event(hdata, data, size);
444 break;
445 default:
446 ret = u1_raw_event(hdata, data, size);
447 break;
448 }
449 return ret;
Masaki Ota25627562016-06-16 18:45:57 +0900450}
451
Masaki Ota73196eb2017-10-06 11:53:17 +0900452static int __maybe_unused alps_post_reset(struct hid_device *hdev)
Masaki Ota25627562016-06-16 18:45:57 +0900453{
Masaki Ota73196eb2017-10-06 11:53:17 +0900454 int ret = -1;
455 struct alps_dev *data = hid_get_drvdata(hdev);
Masaki Ota25627562016-06-16 18:45:57 +0900456
Masaki Ota73196eb2017-10-06 11:53:17 +0900457 switch (data->dev_type) {
458 case T4:
459 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
460 NULL, T4_I2C_ABS, false);
Christophe JAILLET69934012018-03-19 21:53:28 +0100461 if (ret < 0) {
462 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
463 ret);
464 goto exit;
465 }
466
Masaki Ota73196eb2017-10-06 11:53:17 +0900467 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
468 NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
Christophe JAILLET69934012018-03-19 21:53:28 +0100469 if (ret < 0) {
470 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
471 ret);
472 goto exit;
473 }
Masaki Ota73196eb2017-10-06 11:53:17 +0900474 break;
475 case U1:
476 ret = u1_read_write_register(hdev,
477 ADDRESS_U1_DEV_CTRL_1, NULL,
478 U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
Christophe JAILLET69934012018-03-19 21:53:28 +0100479 if (ret < 0) {
480 dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
481 ret);
482 goto exit;
483 }
Masaki Ota73196eb2017-10-06 11:53:17 +0900484 break;
485 default:
486 break;
487 }
Christophe JAILLET69934012018-03-19 21:53:28 +0100488
489exit:
Masaki Ota73196eb2017-10-06 11:53:17 +0900490 return ret;
491}
492
493static int __maybe_unused alps_post_resume(struct hid_device *hdev)
494{
495 return alps_post_reset(hdev);
496}
497
498static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
Masaki Ota5d8c7202017-10-06 11:53:14 +0900499{
500 int ret;
Masaki Ota59922622017-10-06 11:53:16 +0900501 u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
502 u8 pitch_x, pitch_y, resolution;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900503
504 /* Device initialization */
505 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
Masaki Ota59922622017-10-06 11:53:16 +0900506 &dev_ctrl, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900507 if (ret < 0) {
508 dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
509 goto exit;
510 }
511
Masaki Ota59922622017-10-06 11:53:16 +0900512 dev_ctrl &= ~U1_DISABLE_DEV;
513 dev_ctrl |= U1_TP_ABS_MODE;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900514 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
Masaki Ota59922622017-10-06 11:53:16 +0900515 NULL, dev_ctrl, false);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900516 if (ret < 0) {
517 dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
518 goto exit;
519 }
520
521 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
Masaki Ota59922622017-10-06 11:53:16 +0900522 &sen_line_num_x, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900523 if (ret < 0) {
524 dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
525 goto exit;
526 }
527
528 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
Masaki Ota59922622017-10-06 11:53:16 +0900529 &sen_line_num_y, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900530 if (ret < 0) {
531 dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
532 goto exit;
533 }
534
535 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
Masaki Ota59922622017-10-06 11:53:16 +0900536 &pitch_x, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900537 if (ret < 0) {
538 dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
539 goto exit;
540 }
541
542 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
Masaki Ota59922622017-10-06 11:53:16 +0900543 &pitch_y, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900544 if (ret < 0) {
545 dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
546 goto exit;
547 }
548
549 ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
Masaki Ota59922622017-10-06 11:53:16 +0900550 &resolution, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900551 if (ret < 0) {
552 dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
553 goto exit;
554 }
555 pri_data->x_active_len_mm =
Masaki Ota59922622017-10-06 11:53:16 +0900556 (pitch_x * (sen_line_num_x - 1)) / 10;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900557 pri_data->y_active_len_mm =
Masaki Ota59922622017-10-06 11:53:16 +0900558 (pitch_y * (sen_line_num_y - 1)) / 10;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900559
560 pri_data->x_max =
Masaki Ota59922622017-10-06 11:53:16 +0900561 (resolution << 2) * (sen_line_num_x - 1);
Masaki Otac7083d32017-10-06 11:53:15 +0900562 pri_data->x_min = 1;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900563 pri_data->y_max =
Masaki Ota59922622017-10-06 11:53:16 +0900564 (resolution << 2) * (sen_line_num_y - 1);
Masaki Otac7083d32017-10-06 11:53:15 +0900565 pri_data->y_min = 1;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900566
567 ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
Masaki Ota59922622017-10-06 11:53:16 +0900568 &tmp, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900569 if (ret < 0) {
570 dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
571 goto exit;
572 }
Masaki Ota59922622017-10-06 11:53:16 +0900573 if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
574 pri_data->btn_cnt = (tmp & 0x0F);
Masaki Otac7083d32017-10-06 11:53:15 +0900575 } else {
576 /* Button pad */
577 pri_data->btn_cnt = 1;
578 }
Masaki Ota5d8c7202017-10-06 11:53:14 +0900579
580 pri_data->has_sp = 0;
581 /* Check StickPointer device */
582 ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
Masaki Ota59922622017-10-06 11:53:16 +0900583 &tmp, 0, true);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900584 if (ret < 0) {
585 dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
586 goto exit;
587 }
Masaki Ota59922622017-10-06 11:53:16 +0900588 if (tmp & U1_DEVTYPE_SP_SUPPORT) {
589 dev_ctrl |= U1_SP_ABS_MODE;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900590 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
Masaki Ota59922622017-10-06 11:53:16 +0900591 NULL, dev_ctrl, false);
Masaki Ota5d8c7202017-10-06 11:53:14 +0900592 if (ret < 0) {
593 dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
594 goto exit;
595 }
596
597 ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
598 &pri_data->sp_btn_info, 0, true);
599 if (ret < 0) {
600 dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
601 goto exit;
602 }
603 pri_data->has_sp = 1;
604 }
Masaki Otac7083d32017-10-06 11:53:15 +0900605 pri_data->max_fingers = 5;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900606exit:
607 return ret;
608}
609
Masaki Ota73196eb2017-10-06 11:53:17 +0900610static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
611{
612 int ret;
613 u8 tmp, sen_line_num_x, sen_line_num_y;
614
615 ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
616 if (ret < 0) {
617 dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
618 goto exit;
619 }
620 sen_line_num_x = 16 + ((tmp & 0x0F) | (tmp & 0x08 ? 0xF0 : 0));
621 sen_line_num_y = 12 + (((tmp & 0xF0) >> 4) | (tmp & 0x80 ? 0xF0 : 0));
622
623 pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
624 pri_data->x_min = T4_COUNT_PER_ELECTRODE;
625 pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
626 pri_data->y_min = T4_COUNT_PER_ELECTRODE;
627 pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
628 pri_data->btn_cnt = 1;
629
630 ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
631 if (ret < 0) {
632 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
633 goto exit;
634 }
635 tmp |= 0x02;
636 ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
637 if (ret < 0) {
638 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
639 goto exit;
640 }
641
642 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
643 NULL, T4_I2C_ABS, false);
644 if (ret < 0) {
645 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
646 goto exit;
647 }
648
649 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
650 T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
651 if (ret < 0) {
652 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
653 goto exit;
654 }
655 pri_data->max_fingers = 5;
656 pri_data->has_sp = 0;
657exit:
658 return ret;
659}
660
Benjamin Tissoires7dd8db62018-10-12 16:05:25 +0200661static int alps_sp_open(struct input_dev *dev)
662{
663 struct hid_device *hid = input_get_drvdata(dev);
664
665 return hid_hw_open(hid);
666}
667
668static void alps_sp_close(struct input_dev *dev)
669{
670 struct hid_device *hid = input_get_drvdata(dev);
671
672 hid_hw_close(hid);
673}
674
Masaki Ota25627562016-06-16 18:45:57 +0900675static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
676{
Masaki Ota73196eb2017-10-06 11:53:17 +0900677 struct alps_dev *data = hid_get_drvdata(hdev);
Masaki Ota25627562016-06-16 18:45:57 +0900678 struct input_dev *input = hi->input, *input2;
Masaki Ota25627562016-06-16 18:45:57 +0900679 int ret;
680 int res_x, res_y, i;
681
Masaki Ota25627562016-06-16 18:45:57 +0900682 data->input = input;
683
684 hid_dbg(hdev, "Opening low level driver\n");
685 ret = hid_hw_open(hdev);
686 if (ret)
687 return ret;
688
689 /* Allow incoming hid reports */
690 hid_device_io_start(hdev);
Masaki Ota73196eb2017-10-06 11:53:17 +0900691 switch (data->dev_type) {
692 case T4:
693 ret = T4_init(hdev, data);
694 break;
695 case U1:
696 ret = u1_init(hdev, data);
697 break;
698 default:
699 break;
700 }
Masaki Ota5d8c7202017-10-06 11:53:14 +0900701
702 if (ret)
Masaki Ota25627562016-06-16 18:45:57 +0900703 goto exit;
Masaki Ota25627562016-06-16 18:45:57 +0900704
705 __set_bit(EV_ABS, input->evbit);
Masaki Otac7083d32017-10-06 11:53:15 +0900706 input_set_abs_params(input, ABS_MT_POSITION_X,
707 data->x_min, data->x_max, 0, 0);
708 input_set_abs_params(input, ABS_MT_POSITION_Y,
709 data->y_min, data->y_max, 0, 0);
Masaki Ota25627562016-06-16 18:45:57 +0900710
Masaki Otace6abcf2017-10-06 11:53:13 +0900711 if (data->x_active_len_mm && data->y_active_len_mm) {
712 res_x = (data->x_max - 1) / data->x_active_len_mm;
713 res_y = (data->y_max - 1) / data->y_active_len_mm;
Masaki Ota25627562016-06-16 18:45:57 +0900714
715 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
716 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
717 }
718
719 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
720
Masaki Otac7083d32017-10-06 11:53:15 +0900721 input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
Masaki Ota25627562016-06-16 18:45:57 +0900722
723 __set_bit(EV_KEY, input->evbit);
Masaki Otac7083d32017-10-06 11:53:15 +0900724
725 if (data->btn_cnt == 1)
Masaki Ota25627562016-06-16 18:45:57 +0900726 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
Masaki Ota25627562016-06-16 18:45:57 +0900727
Masaki Otace6abcf2017-10-06 11:53:13 +0900728 for (i = 0; i < data->btn_cnt; i++)
Masaki Ota25627562016-06-16 18:45:57 +0900729 __set_bit(BTN_LEFT + i, input->keybit);
730
Masaki Ota25627562016-06-16 18:45:57 +0900731 /* Stick device initialization */
Masaki Ota5d8c7202017-10-06 11:53:14 +0900732 if (data->has_sp) {
Masaki Ota25627562016-06-16 18:45:57 +0900733 input2 = input_allocate_device();
734 if (!input2) {
Christophe JAILLET8d2e77b2019-12-04 04:35:25 +0100735 ret = -ENOMEM;
Masaki Ota25627562016-06-16 18:45:57 +0900736 goto exit;
737 }
738
Masaki Ota819d64e2016-06-22 13:11:08 +0900739 data->input2 = input2;
Masaki Ota25627562016-06-16 18:45:57 +0900740 input2->phys = input->phys;
741 input2->name = "DualPoint Stick";
742 input2->id.bustype = BUS_I2C;
743 input2->id.vendor = input->id.vendor;
744 input2->id.product = input->id.product;
745 input2->id.version = input->id.version;
746 input2->dev.parent = input->dev.parent;
747
Benjamin Tissoires7dd8db62018-10-12 16:05:25 +0200748 input_set_drvdata(input2, hdev);
749 input2->open = alps_sp_open;
750 input2->close = alps_sp_close;
751
Masaki Ota25627562016-06-16 18:45:57 +0900752 __set_bit(EV_KEY, input2->evbit);
Masaki Otace6abcf2017-10-06 11:53:13 +0900753 data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
754 for (i = 0; i < data->sp_btn_cnt; i++)
Masaki Ota25627562016-06-16 18:45:57 +0900755 __set_bit(BTN_LEFT + i, input2->keybit);
756
757 __set_bit(EV_REL, input2->evbit);
758 __set_bit(REL_X, input2->relbit);
759 __set_bit(REL_Y, input2->relbit);
760 __set_bit(INPUT_PROP_POINTER, input2->propbit);
761 __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
762
Masaki Otac7083d32017-10-06 11:53:15 +0900763 if (input_register_device(data->input2)) {
Masaki Ota25627562016-06-16 18:45:57 +0900764 input_free_device(input2);
765 goto exit;
766 }
767 }
768
769exit:
770 hid_device_io_stop(hdev);
771 hid_hw_close(hdev);
772 return ret;
773}
774
775static int alps_input_mapping(struct hid_device *hdev,
776 struct hid_input *hi, struct hid_field *field,
777 struct hid_usage *usage, unsigned long **bit, int *max)
778{
779 return -1;
780}
781
782static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
783{
Masaki Ota73196eb2017-10-06 11:53:17 +0900784 struct alps_dev *data = NULL;
Masaki Ota25627562016-06-16 18:45:57 +0900785 int ret;
Masaki Ota73196eb2017-10-06 11:53:17 +0900786 data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
Masaki Ota25627562016-06-16 18:45:57 +0900787 if (!data)
788 return -ENOMEM;
789
790 data->hdev = hdev;
791 hid_set_drvdata(hdev, data);
792
793 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
794
795 ret = hid_parse(hdev);
796 if (ret) {
797 hid_err(hdev, "parse failed\n");
798 return ret;
799 }
800
Masaki Ota73196eb2017-10-06 11:53:17 +0900801 switch (hdev->product) {
802 case HID_DEVICE_ID_ALPS_T4_BTNLESS:
803 data->dev_type = T4;
804 break;
805 case HID_DEVICE_ID_ALPS_U1_DUAL:
Masaki Ota287b8e12017-10-06 11:53:18 +0900806 case HID_DEVICE_ID_ALPS_U1:
Jiri Kosina185af3e2020-04-15 14:51:42 +0200807 case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
Masaki Ota73196eb2017-10-06 11:53:17 +0900808 data->dev_type = U1;
809 break;
810 default:
811 data->dev_type = UNKNOWN;
812 }
813
Masaki Ota25627562016-06-16 18:45:57 +0900814 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
815 if (ret) {
816 hid_err(hdev, "hw start failed\n");
817 return ret;
818 }
819
820 return 0;
821}
822
823static void alps_remove(struct hid_device *hdev)
824{
825 hid_hw_stop(hdev);
Masaki Ota25627562016-06-16 18:45:57 +0900826}
827
828static const struct hid_device_id alps_id[] = {
829 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
Masaki Ota819d64e2016-06-22 13:11:08 +0900830 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
Masaki Ota73196eb2017-10-06 11:53:17 +0900831 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
Masaki Ota287b8e12017-10-06 11:53:18 +0900832 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
833 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
Masaki Ota73196eb2017-10-06 11:53:17 +0900834 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
Masaki Ota25627562016-06-16 18:45:57 +0900835 { }
836};
837MODULE_DEVICE_TABLE(hid, alps_id);
838
839static struct hid_driver alps_driver = {
840 .name = "hid-alps",
841 .id_table = alps_id,
842 .probe = alps_probe,
843 .remove = alps_remove,
844 .raw_event = alps_raw_event,
845 .input_mapping = alps_input_mapping,
846 .input_configured = alps_input_configured,
847#ifdef CONFIG_PM
848 .resume = alps_post_resume,
849 .reset_resume = alps_post_reset,
850#endif
851};
852
853module_hid_driver(alps_driver);
854
855MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
856MODULE_DESCRIPTION("ALPS HID driver");
857MODULE_LICENSE("GPL");