]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/hid-alps.c
HID: input: Ignore battery reported by Symbol DS4308
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / hid-alps.c
CommitLineData
2562756d
MO
1/*
2 * Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 */
9
10#include <linux/kernel.h>
11#include <linux/hid.h>
12#include <linux/input.h>
13#include <linux/input/mt.h>
14#include <linux/module.h>
15#include <asm/unaligned.h>
16#include "hid-ids.h"
17
18/* ALPS Device Product ID */
19#define HID_PRODUCT_ID_T3_BTNLESS 0xD0C0
20#define HID_PRODUCT_ID_COSMO 0x1202
21#define HID_PRODUCT_ID_U1_PTP_1 0x1207
22#define HID_PRODUCT_ID_U1 0x1209
23#define HID_PRODUCT_ID_U1_PTP_2 0x120A
24#define HID_PRODUCT_ID_U1_DUAL 0x120B
25#define HID_PRODUCT_ID_T4_BTNLESS 0x120C
26
27#define DEV_SINGLEPOINT 0x01
28#define DEV_DUALPOINT 0x02
29
30#define U1_MOUSE_REPORT_ID 0x01 /* Mouse data ReportID */
31#define U1_ABSOLUTE_REPORT_ID 0x03 /* Absolute data ReportID */
32#define U1_FEATURE_REPORT_ID 0x05 /* Feature ReportID */
33#define U1_SP_ABSOLUTE_REPORT_ID 0x06 /* Feature ReportID */
34
35#define U1_FEATURE_REPORT_LEN 0x08 /* Feature Report Length */
36#define U1_FEATURE_REPORT_LEN_ALL 0x0A
37#define U1_CMD_REGISTER_READ 0xD1
38#define U1_CMD_REGISTER_WRITE 0xD2
39
40#define U1_DEVTYPE_SP_SUPPORT 0x10 /* SP Support */
41#define U1_DISABLE_DEV 0x01
42#define U1_TP_ABS_MODE 0x02
43#define U1_SP_ABS_MODE 0x80
44
45#define ADDRESS_U1_DEV_CTRL_1 0x00800040
46#define ADDRESS_U1_DEVICE_TYP 0x00800043
47#define ADDRESS_U1_NUM_SENS_X 0x00800047
48#define ADDRESS_U1_NUM_SENS_Y 0x00800048
49#define ADDRESS_U1_PITCH_SENS_X 0x00800049
50#define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
51#define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
52#define ADDRESS_U1_PAD_BTN 0x00800052
53#define ADDRESS_U1_SP_BTN 0x0080009F
54
73196ebe
MO
55#define T4_INPUT_REPORT_LEN sizeof(struct t4_input_report)
56#define T4_FEATURE_REPORT_LEN T4_INPUT_REPORT_LEN
57#define T4_FEATURE_REPORT_ID 7
58#define T4_CMD_REGISTER_READ 0x08
59#define T4_CMD_REGISTER_WRITE 0x07
60
61#define T4_ADDRESS_BASE 0xC2C0
62#define PRM_SYS_CONFIG_1 (T4_ADDRESS_BASE + 0x0002)
63#define T4_PRM_FEED_CONFIG_1 (T4_ADDRESS_BASE + 0x0004)
64#define T4_PRM_FEED_CONFIG_4 (T4_ADDRESS_BASE + 0x001A)
65#define T4_PRM_ID_CONFIG_3 (T4_ADDRESS_BASE + 0x00B0)
66
67
68#define T4_FEEDCFG4_ADVANCED_ABS_ENABLE 0x01
69#define T4_I2C_ABS 0x78
70
71#define T4_COUNT_PER_ELECTRODE 256
2562756d
MO
72#define MAX_TOUCHES 5
73
73196ebe
MO
74enum dev_num {
75 U1,
76 T4,
77 UNKNOWN,
78};
2562756d
MO
79/**
80 * struct u1_data
81 *
82 * @input: pointer to the kernel input device
83 * @input2: pointer to the kernel input2 device
84 * @hdev: pointer to the struct hid_device
85 *
2562756d 86 * @dev_type: device type
5992262d
MO
87 * @max_fingers: total number of fingers
88 * @has_sp: boolean of sp existense
89 * @sp_btn_info: button information
2562756d
MO
90 * @x_active_len_mm: active area length of X (mm)
91 * @y_active_len_mm: active area length of Y (mm)
92 * @x_max: maximum x coordinate value
93 * @y_max: maximum y coordinate value
c7083d3f
MO
94 * @x_min: minimum x coordinate value
95 * @y_min: minimum y coordinate value
2562756d
MO
96 * @btn_cnt: number of buttons
97 * @sp_btn_cnt: number of stick buttons
98 */
73196ebe 99struct alps_dev {
2562756d
MO
100 struct input_dev *input;
101 struct input_dev *input2;
102 struct hid_device *hdev;
103
73196ebe 104 enum dev_num dev_type;
5992262d
MO
105 u8 max_fingers;
106 u8 has_sp;
2562756d
MO
107 u8 sp_btn_info;
108 u32 x_active_len_mm;
109 u32 y_active_len_mm;
110 u32 x_max;
111 u32 y_max;
c7083d3f
MO
112 u32 x_min;
113 u32 y_min;
2562756d
MO
114 u32 btn_cnt;
115 u32 sp_btn_cnt;
116};
117
73196ebe
MO
118struct t4_contact_data {
119 u8 palm;
120 u8 x_lo;
121 u8 x_hi;
122 u8 y_lo;
123 u8 y_hi;
124};
125
126struct t4_input_report {
127 u8 reportID;
128 u8 numContacts;
129 struct t4_contact_data contact[5];
130 u8 button;
131 u8 track[5];
132 u8 zx[5], zy[5];
133 u8 palmTime[5];
134 u8 kilroy;
135 u16 timeStamp;
136};
137
138static u16 t4_calc_check_sum(u8 *buffer,
139 unsigned long offset, unsigned long length)
140{
141 u16 sum1 = 0xFF, sum2 = 0xFF;
142 unsigned long i = 0;
143
144 if (offset + length >= 50)
145 return 0;
146
147 while (length > 0) {
148 u32 tlen = length > 20 ? 20 : length;
149
150 length -= tlen;
151
152 do {
153 sum1 += buffer[offset + i];
154 sum2 += sum1;
155 i++;
156 } while (--tlen > 0);
157
158 sum1 = (sum1 & 0xFF) + (sum1 >> 8);
159 sum2 = (sum2 & 0xFF) + (sum2 >> 8);
160 }
161
162 sum1 = (sum1 & 0xFF) + (sum1 >> 8);
163 sum2 = (sum2 & 0xFF) + (sum2 >> 8);
164
165 return(sum2 << 8 | sum1);
166}
167
168static int t4_read_write_register(struct hid_device *hdev, u32 address,
169 u8 *read_val, u8 write_val, bool read_flag)
170{
171 int ret;
172 u16 check_sum;
173 u8 *input;
174 u8 *readbuf;
175
176 input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
177 if (!input)
178 return -ENOMEM;
179
180 input[0] = T4_FEATURE_REPORT_ID;
181 if (read_flag) {
182 input[1] = T4_CMD_REGISTER_READ;
183 input[8] = 0x00;
184 } else {
185 input[1] = T4_CMD_REGISTER_WRITE;
186 input[8] = write_val;
187 }
188 put_unaligned_le32(address, input + 2);
189 input[6] = 1;
190 input[7] = 0;
191
192 /* Calculate the checksum */
193 check_sum = t4_calc_check_sum(input, 1, 8);
194 input[9] = (u8)check_sum;
195 input[10] = (u8)(check_sum >> 8);
196 input[11] = 0;
197
198 ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
199 T4_FEATURE_REPORT_LEN,
200 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
201
202 if (ret < 0) {
203 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
204 goto exit;
205 }
206
207 readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
208 if (read_flag) {
209 if (!readbuf) {
210 ret = -ENOMEM;
211 goto exit;
212 }
213
214 ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
215 T4_FEATURE_REPORT_LEN,
216 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
217 if (ret < 0) {
218 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
219 goto exit_readbuf;
220 }
221
222 if (*(u32 *)&readbuf[6] != address) {
223 dev_err(&hdev->dev, "read register address error (%x,%x)\n",
224 *(u32 *)&readbuf[6], address);
225 goto exit_readbuf;
226 }
227
228 if (*(u16 *)&readbuf[10] != 1) {
229 dev_err(&hdev->dev, "read register size error (%x)\n",
230 *(u16 *)&readbuf[10]);
231 goto exit_readbuf;
232 }
233
234 check_sum = t4_calc_check_sum(readbuf, 6, 7);
235 if (*(u16 *)&readbuf[13] != check_sum) {
236 dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
237 *(u16 *)&readbuf[13], check_sum);
238 goto exit_readbuf;
239 }
240
241 *read_val = readbuf[12];
242 }
243
244 ret = 0;
245
246exit_readbuf:
247 kfree(readbuf);
248exit:
249 kfree(input);
250 return ret;
251}
252
2562756d
MO
253static int u1_read_write_register(struct hid_device *hdev, u32 address,
254 u8 *read_val, u8 write_val, bool read_flag)
255{
256 int ret, i;
257 u8 check_sum;
258 u8 *input;
259 u8 *readbuf;
260
819d64e5 261 input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
2562756d
MO
262 if (!input)
263 return -ENOMEM;
264
2562756d
MO
265 input[0] = U1_FEATURE_REPORT_ID;
266 if (read_flag) {
267 input[1] = U1_CMD_REGISTER_READ;
268 input[6] = 0x00;
269 } else {
270 input[1] = U1_CMD_REGISTER_WRITE;
271 input[6] = write_val;
272 }
273
274 put_unaligned_le32(address, input + 2);
275
276 /* Calculate the checksum */
277 check_sum = U1_FEATURE_REPORT_LEN_ALL;
278 for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
279 check_sum += input[i];
280
281 input[7] = check_sum;
282 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
819d64e5
MO
283 U1_FEATURE_REPORT_LEN,
284 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
2562756d
MO
285
286 if (ret < 0) {
287 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
288 goto exit;
289 }
290
291 if (read_flag) {
819d64e5
MO
292 readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
293 if (!readbuf) {
7ee2eaa3
AL
294 ret = -ENOMEM;
295 goto exit;
819d64e5
MO
296 }
297
2562756d 298 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
819d64e5 299 U1_FEATURE_REPORT_LEN,
63b3a7d0 300 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
2562756d
MO
301
302 if (ret < 0) {
303 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
7ee2eaa3 304 kfree(readbuf);
2562756d
MO
305 goto exit;
306 }
307
308 *read_val = readbuf[6];
819d64e5
MO
309
310 kfree(readbuf);
2562756d
MO
311 }
312
819d64e5 313 ret = 0;
2562756d
MO
314
315exit:
316 kfree(input);
2562756d
MO
317 return ret;
318}
319
73196ebe
MO
320static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
321{
322 unsigned int x, y, z;
323 int i;
324 struct t4_input_report *p_report = (struct t4_input_report *)data;
325
326 if (!data)
327 return 0;
328 for (i = 0; i < hdata->max_fingers; i++) {
329 x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
330 y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
331 y = hdata->y_max - y + hdata->y_min;
332 z = (p_report->contact[i].palm < 0x80 &&
333 p_report->contact[i].palm > 0) * 62;
334 if (x == 0xffff) {
335 x = 0;
336 y = 0;
337 z = 0;
338 }
339 input_mt_slot(hdata->input, i);
340
341 input_mt_report_slot_state(hdata->input,
342 MT_TOOL_FINGER, z != 0);
343
344 if (!z)
345 continue;
346
347 input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
348 input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
349 input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
350 }
351 input_mt_sync_frame(hdata->input);
352
353 input_report_key(hdata->input, BTN_LEFT, p_report->button);
354
355 input_sync(hdata->input);
356 return 1;
357}
358
359static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
2562756d 360{
819d64e5
MO
361 unsigned int x, y, z;
362 int i;
363 short sp_x, sp_y;
2562756d 364
73196ebe
MO
365 if (!data)
366 return 0;
2562756d
MO
367 switch (data[0]) {
368 case U1_MOUSE_REPORT_ID:
369 break;
370 case U1_FEATURE_REPORT_ID:
371 break;
372 case U1_ABSOLUTE_REPORT_ID:
73196ebe 373 for (i = 0; i < hdata->max_fingers; i++) {
819d64e5
MO
374 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;
2562756d
MO
379
380 input_mt_slot(hdata->input, i);
381
819d64e5 382 if (z != 0) {
2562756d
MO
383 input_mt_report_slot_state(hdata->input,
384 MT_TOOL_FINGER, 1);
9a54cf46
MO
385 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);
2562756d
MO
391 } else {
392 input_mt_report_slot_state(hdata->input,
393 MT_TOOL_FINGER, 0);
2562756d 394 }
2562756d
MO
395 }
396
397 input_mt_sync_frame(hdata->input);
2562756d 398
819d64e5
MO
399 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);
2562756d
MO
407
408 return 1;
409
410 case U1_SP_ABSOLUTE_REPORT_ID:
819d64e5
MO
411 sp_x = get_unaligned_le16(data+2);
412 sp_y = get_unaligned_le16(data+4);
2562756d
MO
413
414 sp_x = sp_x / 8;
415 sp_y = sp_y / 8;
416
819d64e5
MO
417 input_report_rel(hdata->input2, REL_X, sp_x);
418 input_report_rel(hdata->input2, REL_Y, sp_y);
2562756d 419
819d64e5
MO
420 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));
2562756d 426
819d64e5 427 input_sync(hdata->input2);
2562756d
MO
428
429 return 1;
430 }
431
432 return 0;
433}
434
73196ebe
MO
435static int alps_raw_event(struct hid_device *hdev,
436 struct hid_report *report, u8 *data, int size)
2562756d 437{
73196ebe
MO
438 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;
2562756d
MO
450}
451
73196ebe 452static int __maybe_unused alps_post_reset(struct hid_device *hdev)
2562756d 453{
73196ebe
MO
454 int ret = -1;
455 struct alps_dev *data = hid_get_drvdata(hdev);
456
457 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);
461 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
462 NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
463 break;
464 case U1:
465 ret = u1_read_write_register(hdev,
466 ADDRESS_U1_DEV_CTRL_1, NULL,
467 U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
468 break;
469 default:
470 break;
471 }
472 return ret;
473}
474
475static int __maybe_unused alps_post_resume(struct hid_device *hdev)
476{
477 return alps_post_reset(hdev);
2562756d 478}
2562756d 479
73196ebe 480static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
2562756d 481{
2562756d 482 int ret;
5992262d
MO
483 u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
484 u8 pitch_x, pitch_y, resolution;
2562756d
MO
485
486 /* Device initialization */
487 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 488 &dev_ctrl, 0, true);
2562756d
MO
489 if (ret < 0) {
490 dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
491 goto exit;
492 }
493
5992262d
MO
494 dev_ctrl &= ~U1_DISABLE_DEV;
495 dev_ctrl |= U1_TP_ABS_MODE;
2562756d 496 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 497 NULL, dev_ctrl, false);
2562756d
MO
498 if (ret < 0) {
499 dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
500 goto exit;
501 }
502
503 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
5992262d 504 &sen_line_num_x, 0, true);
2562756d
MO
505 if (ret < 0) {
506 dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
507 goto exit;
508 }
509
510 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
5992262d 511 &sen_line_num_y, 0, true);
2562756d
MO
512 if (ret < 0) {
513 dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
514 goto exit;
515 }
516
517 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
5992262d 518 &pitch_x, 0, true);
2562756d
MO
519 if (ret < 0) {
520 dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
521 goto exit;
522 }
523
524 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
5992262d 525 &pitch_y, 0, true);
2562756d
MO
526 if (ret < 0) {
527 dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
528 goto exit;
529 }
530
531 ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
5992262d 532 &resolution, 0, true);
2562756d
MO
533 if (ret < 0) {
534 dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
535 goto exit;
536 }
5d8c720d 537 pri_data->x_active_len_mm =
5992262d 538 (pitch_x * (sen_line_num_x - 1)) / 10;
5d8c720d 539 pri_data->y_active_len_mm =
5992262d 540 (pitch_y * (sen_line_num_y - 1)) / 10;
5d8c720d
MO
541
542 pri_data->x_max =
5992262d 543 (resolution << 2) * (sen_line_num_x - 1);
c7083d3f 544 pri_data->x_min = 1;
5d8c720d 545 pri_data->y_max =
5992262d 546 (resolution << 2) * (sen_line_num_y - 1);
c7083d3f 547 pri_data->y_min = 1;
2562756d
MO
548
549 ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
5992262d 550 &tmp, 0, true);
2562756d
MO
551 if (ret < 0) {
552 dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
553 goto exit;
554 }
5992262d
MO
555 if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
556 pri_data->btn_cnt = (tmp & 0x0F);
c7083d3f
MO
557 } else {
558 /* Button pad */
559 pri_data->btn_cnt = 1;
560 }
2562756d 561
5d8c720d 562 pri_data->has_sp = 0;
2562756d
MO
563 /* Check StickPointer device */
564 ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
5992262d 565 &tmp, 0, true);
2562756d
MO
566 if (ret < 0) {
567 dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
568 goto exit;
569 }
5992262d
MO
570 if (tmp & U1_DEVTYPE_SP_SUPPORT) {
571 dev_ctrl |= U1_SP_ABS_MODE;
5d8c720d 572 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 573 NULL, dev_ctrl, false);
5d8c720d
MO
574 if (ret < 0) {
575 dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
576 goto exit;
577 }
578
579 ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
580 &pri_data->sp_btn_info, 0, true);
581 if (ret < 0) {
582 dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
583 goto exit;
584 }
585 pri_data->has_sp = 1;
586 }
c7083d3f 587 pri_data->max_fingers = 5;
5d8c720d
MO
588exit:
589 return ret;
590}
591
73196ebe
MO
592static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
593{
594 int ret;
595 u8 tmp, sen_line_num_x, sen_line_num_y;
596
597 ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
598 if (ret < 0) {
599 dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
600 goto exit;
601 }
602 sen_line_num_x = 16 + ((tmp & 0x0F) | (tmp & 0x08 ? 0xF0 : 0));
603 sen_line_num_y = 12 + (((tmp & 0xF0) >> 4) | (tmp & 0x80 ? 0xF0 : 0));
604
605 pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
606 pri_data->x_min = T4_COUNT_PER_ELECTRODE;
607 pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
608 pri_data->y_min = T4_COUNT_PER_ELECTRODE;
609 pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
610 pri_data->btn_cnt = 1;
611
612 ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
613 if (ret < 0) {
614 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
615 goto exit;
616 }
617 tmp |= 0x02;
618 ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
619 if (ret < 0) {
620 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
621 goto exit;
622 }
623
624 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
625 NULL, T4_I2C_ABS, false);
626 if (ret < 0) {
627 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
628 goto exit;
629 }
630
631 ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
632 T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
633 if (ret < 0) {
634 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
635 goto exit;
636 }
637 pri_data->max_fingers = 5;
638 pri_data->has_sp = 0;
639exit:
640 return ret;
641}
642
1f938a20
BT
643static int alps_sp_open(struct input_dev *dev)
644{
645 struct hid_device *hid = input_get_drvdata(dev);
646
647 return hid_hw_open(hid);
648}
649
650static void alps_sp_close(struct input_dev *dev)
651{
652 struct hid_device *hid = input_get_drvdata(dev);
653
654 hid_hw_close(hid);
655}
656
5d8c720d
MO
657static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
658{
73196ebe 659 struct alps_dev *data = hid_get_drvdata(hdev);
5d8c720d
MO
660 struct input_dev *input = hi->input, *input2;
661 int ret;
662 int res_x, res_y, i;
663
664 data->input = input;
665
666 hid_dbg(hdev, "Opening low level driver\n");
667 ret = hid_hw_open(hdev);
668 if (ret)
669 return ret;
670
671 /* Allow incoming hid reports */
672 hid_device_io_start(hdev);
73196ebe
MO
673 switch (data->dev_type) {
674 case T4:
675 ret = T4_init(hdev, data);
676 break;
677 case U1:
678 ret = u1_init(hdev, data);
679 break;
680 default:
681 break;
682 }
5d8c720d
MO
683
684 if (ret)
685 goto exit;
2562756d
MO
686
687 __set_bit(EV_ABS, input->evbit);
c7083d3f
MO
688 input_set_abs_params(input, ABS_MT_POSITION_X,
689 data->x_min, data->x_max, 0, 0);
690 input_set_abs_params(input, ABS_MT_POSITION_Y,
691 data->y_min, data->y_max, 0, 0);
2562756d 692
ce6abcf8
MO
693 if (data->x_active_len_mm && data->y_active_len_mm) {
694 res_x = (data->x_max - 1) / data->x_active_len_mm;
695 res_y = (data->y_max - 1) / data->y_active_len_mm;
2562756d
MO
696
697 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
698 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
699 }
700
701 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
702
c7083d3f 703 input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
2562756d
MO
704
705 __set_bit(EV_KEY, input->evbit);
c7083d3f
MO
706
707 if (data->btn_cnt == 1)
2562756d 708 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
2562756d 709
ce6abcf8 710 for (i = 0; i < data->btn_cnt; i++)
2562756d
MO
711 __set_bit(BTN_LEFT + i, input->keybit);
712
2562756d 713 /* Stick device initialization */
5d8c720d 714 if (data->has_sp) {
2562756d
MO
715 input2 = input_allocate_device();
716 if (!input2) {
c7083d3f 717 input_free_device(input2);
2562756d
MO
718 goto exit;
719 }
720
819d64e5 721 data->input2 = input2;
2562756d
MO
722 input2->phys = input->phys;
723 input2->name = "DualPoint Stick";
724 input2->id.bustype = BUS_I2C;
725 input2->id.vendor = input->id.vendor;
726 input2->id.product = input->id.product;
727 input2->id.version = input->id.version;
728 input2->dev.parent = input->dev.parent;
729
1f938a20
BT
730 input_set_drvdata(input2, hdev);
731 input2->open = alps_sp_open;
732 input2->close = alps_sp_close;
733
2562756d 734 __set_bit(EV_KEY, input2->evbit);
ce6abcf8
MO
735 data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
736 for (i = 0; i < data->sp_btn_cnt; i++)
2562756d
MO
737 __set_bit(BTN_LEFT + i, input2->keybit);
738
739 __set_bit(EV_REL, input2->evbit);
740 __set_bit(REL_X, input2->relbit);
741 __set_bit(REL_Y, input2->relbit);
742 __set_bit(INPUT_PROP_POINTER, input2->propbit);
743 __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
744
c7083d3f 745 if (input_register_device(data->input2)) {
2562756d
MO
746 input_free_device(input2);
747 goto exit;
748 }
749 }
750
751exit:
752 hid_device_io_stop(hdev);
753 hid_hw_close(hdev);
754 return ret;
755}
756
757static int alps_input_mapping(struct hid_device *hdev,
758 struct hid_input *hi, struct hid_field *field,
759 struct hid_usage *usage, unsigned long **bit, int *max)
760{
761 return -1;
762}
763
764static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
765{
73196ebe 766 struct alps_dev *data = NULL;
2562756d 767 int ret;
73196ebe 768 data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
2562756d
MO
769 if (!data)
770 return -ENOMEM;
771
772 data->hdev = hdev;
773 hid_set_drvdata(hdev, data);
774
775 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
776
777 ret = hid_parse(hdev);
778 if (ret) {
779 hid_err(hdev, "parse failed\n");
780 return ret;
781 }
782
73196ebe
MO
783 switch (hdev->product) {
784 case HID_DEVICE_ID_ALPS_T4_BTNLESS:
785 data->dev_type = T4;
786 break;
787 case HID_DEVICE_ID_ALPS_U1_DUAL:
287b8e11 788 case HID_DEVICE_ID_ALPS_U1:
73196ebe
MO
789 data->dev_type = U1;
790 break;
791 default:
792 data->dev_type = UNKNOWN;
793 }
794
2562756d
MO
795 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
796 if (ret) {
797 hid_err(hdev, "hw start failed\n");
798 return ret;
799 }
800
801 return 0;
802}
803
804static void alps_remove(struct hid_device *hdev)
805{
806 hid_hw_stop(hdev);
2562756d
MO
807}
808
809static const struct hid_device_id alps_id[] = {
810 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
819d64e5 811 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
287b8e11
MO
812 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
813 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
73196ebe
MO
814 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
815 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
2562756d
MO
816 { }
817};
818MODULE_DEVICE_TABLE(hid, alps_id);
819
820static struct hid_driver alps_driver = {
821 .name = "hid-alps",
822 .id_table = alps_id,
823 .probe = alps_probe,
824 .remove = alps_remove,
825 .raw_event = alps_raw_event,
826 .input_mapping = alps_input_mapping,
827 .input_configured = alps_input_configured,
828#ifdef CONFIG_PM
829 .resume = alps_post_resume,
830 .reset_resume = alps_post_reset,
831#endif
832};
833
834module_hid_driver(alps_driver);
835
836MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
837MODULE_DESCRIPTION("ALPS HID driver");
838MODULE_LICENSE("GPL");