1 /* 2 * Copyright (c) 2012-2016 Synaptics Incorporated 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 version 2 as published by 6 * the Free Software Foundation. 7 */ 8 #include <linux/input.h> 9 #include <linux/input/mt.h> 10 #include <linux/rmi.h> 11 #include "rmi_driver.h" 12 #include "rmi_2d_sensor.h" 13 14 enum rmi_f12_object_type { 15 RMI_F12_OBJECT_NONE = 0x00, 16 RMI_F12_OBJECT_FINGER = 0x01, 17 RMI_F12_OBJECT_STYLUS = 0x02, 18 RMI_F12_OBJECT_PALM = 0x03, 19 RMI_F12_OBJECT_UNCLASSIFIED = 0x04, 20 RMI_F12_OBJECT_GLOVED_FINGER = 0x06, 21 RMI_F12_OBJECT_NARROW_OBJECT = 0x07, 22 RMI_F12_OBJECT_HAND_EDGE = 0x08, 23 RMI_F12_OBJECT_COVER = 0x0A, 24 RMI_F12_OBJECT_STYLUS_2 = 0x0B, 25 RMI_F12_OBJECT_ERASER = 0x0C, 26 RMI_F12_OBJECT_SMALL_OBJECT = 0x0D, 27 }; 28 29 #define F12_DATA1_BYTES_PER_OBJ 8 30 31 struct f12_data { 32 struct rmi_2d_sensor sensor; 33 struct rmi_2d_sensor_platform_data sensor_pdata; 34 bool has_dribble; 35 36 u16 data_addr; 37 38 struct rmi_register_descriptor query_reg_desc; 39 struct rmi_register_descriptor control_reg_desc; 40 struct rmi_register_descriptor data_reg_desc; 41 42 /* F12 Data1 describes sensed objects */ 43 const struct rmi_register_desc_item *data1; 44 u16 data1_offset; 45 46 /* F12 Data5 describes finger ACM */ 47 const struct rmi_register_desc_item *data5; 48 u16 data5_offset; 49 50 /* F12 Data5 describes Pen */ 51 const struct rmi_register_desc_item *data6; 52 u16 data6_offset; 53 54 55 /* F12 Data9 reports relative data */ 56 const struct rmi_register_desc_item *data9; 57 u16 data9_offset; 58 59 const struct rmi_register_desc_item *data15; 60 u16 data15_offset; 61 }; 62 63 static int rmi_f12_read_sensor_tuning(struct f12_data *f12) 64 { 65 const struct rmi_register_desc_item *item; 66 struct rmi_2d_sensor *sensor = &f12->sensor; 67 struct rmi_function *fn = sensor->fn; 68 struct rmi_device *rmi_dev = fn->rmi_dev; 69 int ret; 70 int offset; 71 u8 buf[15]; 72 int pitch_x = 0; 73 int pitch_y = 0; 74 int rx_receivers = 0; 75 int tx_receivers = 0; 76 int sensor_flags = 0; 77 78 item = rmi_get_register_desc_item(&f12->control_reg_desc, 8); 79 if (!item) { 80 dev_err(&fn->dev, 81 "F12 does not have the sensor tuning control register\n"); 82 return -ENODEV; 83 } 84 85 offset = rmi_register_desc_calc_reg_offset(&f12->control_reg_desc, 8); 86 87 if (item->reg_size > sizeof(buf)) { 88 dev_err(&fn->dev, 89 "F12 control8 should be no bigger than %zd bytes, not: %ld\n", 90 sizeof(buf), item->reg_size); 91 return -ENODEV; 92 } 93 94 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr + offset, buf, 95 item->reg_size); 96 if (ret) 97 return ret; 98 99 offset = 0; 100 if (rmi_register_desc_has_subpacket(item, 0)) { 101 sensor->max_x = (buf[offset + 1] << 8) | buf[offset]; 102 sensor->max_y = (buf[offset + 3] << 8) | buf[offset + 2]; 103 offset += 4; 104 } 105 106 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: max_x: %d max_y: %d\n", __func__, 107 sensor->max_x, sensor->max_y); 108 109 if (rmi_register_desc_has_subpacket(item, 1)) { 110 pitch_x = (buf[offset + 1] << 8) | buf[offset]; 111 pitch_y = (buf[offset + 3] << 8) | buf[offset + 2]; 112 offset += 4; 113 } 114 115 if (rmi_register_desc_has_subpacket(item, 2)) { 116 /* Units 1/128 sensor pitch */ 117 rmi_dbg(RMI_DEBUG_FN, &fn->dev, 118 "%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n", 119 __func__, 120 buf[offset], buf[offset + 1], 121 buf[offset + 2], buf[offset + 3]); 122 123 offset += 4; 124 } 125 126 if (rmi_register_desc_has_subpacket(item, 3)) { 127 rx_receivers = buf[offset]; 128 tx_receivers = buf[offset + 1]; 129 offset += 2; 130 } 131 132 if (rmi_register_desc_has_subpacket(item, 4)) { 133 sensor_flags = buf[offset]; 134 offset += 1; 135 } 136 137 sensor->x_mm = (pitch_x * rx_receivers) >> 12; 138 sensor->y_mm = (pitch_y * tx_receivers) >> 12; 139 140 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x_mm: %d y_mm: %d\n", __func__, 141 sensor->x_mm, sensor->y_mm); 142 143 return 0; 144 } 145 146 static void rmi_f12_process_objects(struct f12_data *f12, u8 *data1, int size) 147 { 148 int i; 149 struct rmi_2d_sensor *sensor = &f12->sensor; 150 int objects = f12->data1->num_subpackets; 151 152 if ((f12->data1->num_subpackets * F12_DATA1_BYTES_PER_OBJ) > size) 153 objects = size / F12_DATA1_BYTES_PER_OBJ; 154 155 for (i = 0; i < objects; i++) { 156 struct rmi_2d_sensor_abs_object *obj = &sensor->objs[i]; 157 158 obj->type = RMI_2D_OBJECT_NONE; 159 obj->mt_tool = MT_TOOL_FINGER; 160 161 switch (data1[0]) { 162 case RMI_F12_OBJECT_FINGER: 163 obj->type = RMI_2D_OBJECT_FINGER; 164 break; 165 case RMI_F12_OBJECT_STYLUS: 166 obj->type = RMI_2D_OBJECT_STYLUS; 167 obj->mt_tool = MT_TOOL_PEN; 168 break; 169 case RMI_F12_OBJECT_PALM: 170 obj->type = RMI_2D_OBJECT_PALM; 171 obj->mt_tool = MT_TOOL_PALM; 172 break; 173 case RMI_F12_OBJECT_UNCLASSIFIED: 174 obj->type = RMI_2D_OBJECT_UNCLASSIFIED; 175 break; 176 } 177 178 obj->x = (data1[2] << 8) | data1[1]; 179 obj->y = (data1[4] << 8) | data1[3]; 180 obj->z = data1[5]; 181 obj->wx = data1[6]; 182 obj->wy = data1[7]; 183 184 rmi_2d_sensor_abs_process(sensor, obj, i); 185 186 data1 += F12_DATA1_BYTES_PER_OBJ; 187 } 188 189 if (sensor->kernel_tracking) 190 input_mt_assign_slots(sensor->input, 191 sensor->tracking_slots, 192 sensor->tracking_pos, 193 sensor->nbr_fingers, 194 sensor->dmax); 195 196 for (i = 0; i < objects; i++) 197 rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i); 198 } 199 200 static int rmi_f12_attention(struct rmi_function *fn, 201 unsigned long *irq_nr_regs) 202 { 203 int retval; 204 struct rmi_device *rmi_dev = fn->rmi_dev; 205 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev); 206 struct f12_data *f12 = dev_get_drvdata(&fn->dev); 207 struct rmi_2d_sensor *sensor = &f12->sensor; 208 int valid_bytes = sensor->pkt_size; 209 210 if (drvdata->attn_data.data) { 211 if (sensor->attn_size > drvdata->attn_data.size) 212 valid_bytes = drvdata->attn_data.size; 213 else 214 valid_bytes = sensor->attn_size; 215 memcpy(sensor->data_pkt, drvdata->attn_data.data, 216 valid_bytes); 217 drvdata->attn_data.data += sensor->attn_size; 218 drvdata->attn_data.size -= sensor->attn_size; 219 } else { 220 retval = rmi_read_block(rmi_dev, f12->data_addr, 221 sensor->data_pkt, sensor->pkt_size); 222 if (retval < 0) { 223 dev_err(&fn->dev, "Failed to read object data. Code: %d.\n", 224 retval); 225 return retval; 226 } 227 } 228 229 if (f12->data1) 230 rmi_f12_process_objects(f12, 231 &sensor->data_pkt[f12->data1_offset], valid_bytes); 232 233 input_mt_sync_frame(sensor->input); 234 235 return 0; 236 } 237 238 static int rmi_f12_write_control_regs(struct rmi_function *fn) 239 { 240 int ret; 241 const struct rmi_register_desc_item *item; 242 struct rmi_device *rmi_dev = fn->rmi_dev; 243 struct f12_data *f12 = dev_get_drvdata(&fn->dev); 244 int control_size; 245 char buf[3]; 246 u16 control_offset = 0; 247 u8 subpacket_offset = 0; 248 249 if (f12->has_dribble 250 && (f12->sensor.dribble != RMI_REG_STATE_DEFAULT)) { 251 item = rmi_get_register_desc_item(&f12->control_reg_desc, 20); 252 if (item) { 253 control_offset = rmi_register_desc_calc_reg_offset( 254 &f12->control_reg_desc, 20); 255 256 /* 257 * The byte containing the EnableDribble bit will be 258 * in either byte 0 or byte 2 of control 20. Depending 259 * on the existence of subpacket 0. If control 20 is 260 * larger then 3 bytes, just read the first 3. 261 */ 262 control_size = min(item->reg_size, 3UL); 263 264 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr 265 + control_offset, buf, control_size); 266 if (ret) 267 return ret; 268 269 if (rmi_register_desc_has_subpacket(item, 0)) 270 subpacket_offset += 1; 271 272 switch (f12->sensor.dribble) { 273 case RMI_REG_STATE_OFF: 274 buf[subpacket_offset] &= ~BIT(2); 275 break; 276 case RMI_REG_STATE_ON: 277 buf[subpacket_offset] |= BIT(2); 278 break; 279 case RMI_REG_STATE_DEFAULT: 280 default: 281 break; 282 } 283 284 ret = rmi_write_block(rmi_dev, 285 fn->fd.control_base_addr + control_offset, 286 buf, control_size); 287 if (ret) 288 return ret; 289 } 290 } 291 292 return 0; 293 294 } 295 296 static int rmi_f12_config(struct rmi_function *fn) 297 { 298 struct rmi_driver *drv = fn->rmi_dev->driver; 299 int ret; 300 301 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask); 302 303 ret = rmi_f12_write_control_regs(fn); 304 if (ret) 305 dev_warn(&fn->dev, 306 "Failed to write F12 control registers: %d\n", ret); 307 308 return 0; 309 } 310 311 static int rmi_f12_probe(struct rmi_function *fn) 312 { 313 struct f12_data *f12; 314 int ret; 315 struct rmi_device *rmi_dev = fn->rmi_dev; 316 char buf; 317 u16 query_addr = fn->fd.query_base_addr; 318 const struct rmi_register_desc_item *item; 319 struct rmi_2d_sensor *sensor; 320 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev); 321 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev); 322 u16 data_offset = 0; 323 324 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s\n", __func__); 325 326 ret = rmi_read(fn->rmi_dev, query_addr, &buf); 327 if (ret < 0) { 328 dev_err(&fn->dev, "Failed to read general info register: %d\n", 329 ret); 330 return -ENODEV; 331 } 332 ++query_addr; 333 334 if (!(buf & BIT(0))) { 335 dev_err(&fn->dev, 336 "Behavior of F12 without register descriptors is undefined.\n"); 337 return -ENODEV; 338 } 339 340 f12 = devm_kzalloc(&fn->dev, sizeof(struct f12_data), GFP_KERNEL); 341 if (!f12) 342 return -ENOMEM; 343 344 f12->has_dribble = !!(buf & BIT(3)); 345 346 if (fn->dev.of_node) { 347 ret = rmi_2d_sensor_of_probe(&fn->dev, &f12->sensor_pdata); 348 if (ret) 349 return ret; 350 } else { 351 f12->sensor_pdata = pdata->sensor_pdata; 352 } 353 354 ret = rmi_read_register_desc(rmi_dev, query_addr, 355 &f12->query_reg_desc); 356 if (ret) { 357 dev_err(&fn->dev, 358 "Failed to read the Query Register Descriptor: %d\n", 359 ret); 360 return ret; 361 } 362 query_addr += 3; 363 364 ret = rmi_read_register_desc(rmi_dev, query_addr, 365 &f12->control_reg_desc); 366 if (ret) { 367 dev_err(&fn->dev, 368 "Failed to read the Control Register Descriptor: %d\n", 369 ret); 370 return ret; 371 } 372 query_addr += 3; 373 374 ret = rmi_read_register_desc(rmi_dev, query_addr, 375 &f12->data_reg_desc); 376 if (ret) { 377 dev_err(&fn->dev, 378 "Failed to read the Data Register Descriptor: %d\n", 379 ret); 380 return ret; 381 } 382 query_addr += 3; 383 384 sensor = &f12->sensor; 385 sensor->fn = fn; 386 f12->data_addr = fn->fd.data_base_addr; 387 sensor->pkt_size = rmi_register_desc_calc_size(&f12->data_reg_desc); 388 389 sensor->axis_align = 390 f12->sensor_pdata.axis_align; 391 392 sensor->x_mm = f12->sensor_pdata.x_mm; 393 sensor->y_mm = f12->sensor_pdata.y_mm; 394 sensor->dribble = f12->sensor_pdata.dribble; 395 396 if (sensor->sensor_type == rmi_sensor_default) 397 sensor->sensor_type = 398 f12->sensor_pdata.sensor_type; 399 400 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: data packet size: %d\n", __func__, 401 sensor->pkt_size); 402 sensor->data_pkt = devm_kzalloc(&fn->dev, sensor->pkt_size, GFP_KERNEL); 403 if (!sensor->data_pkt) 404 return -ENOMEM; 405 406 dev_set_drvdata(&fn->dev, f12); 407 408 ret = rmi_f12_read_sensor_tuning(f12); 409 if (ret) 410 return ret; 411 412 /* 413 * Figure out what data is contained in the data registers. HID devices 414 * may have registers defined, but their data is not reported in the 415 * HID attention report. Registers which are not reported in the HID 416 * attention report check to see if the device is receiving data from 417 * HID attention reports. 418 */ 419 item = rmi_get_register_desc_item(&f12->data_reg_desc, 0); 420 if (item && !drvdata->attn_data.data) 421 data_offset += item->reg_size; 422 423 item = rmi_get_register_desc_item(&f12->data_reg_desc, 1); 424 if (item) { 425 f12->data1 = item; 426 f12->data1_offset = data_offset; 427 data_offset += item->reg_size; 428 sensor->nbr_fingers = item->num_subpackets; 429 sensor->report_abs = 1; 430 sensor->attn_size += item->reg_size; 431 } 432 433 item = rmi_get_register_desc_item(&f12->data_reg_desc, 2); 434 if (item && !drvdata->attn_data.data) 435 data_offset += item->reg_size; 436 437 item = rmi_get_register_desc_item(&f12->data_reg_desc, 3); 438 if (item && !drvdata->attn_data.data) 439 data_offset += item->reg_size; 440 441 item = rmi_get_register_desc_item(&f12->data_reg_desc, 4); 442 if (item && !drvdata->attn_data.data) 443 data_offset += item->reg_size; 444 445 item = rmi_get_register_desc_item(&f12->data_reg_desc, 5); 446 if (item) { 447 f12->data5 = item; 448 f12->data5_offset = data_offset; 449 data_offset += item->reg_size; 450 sensor->attn_size += item->reg_size; 451 } 452 453 item = rmi_get_register_desc_item(&f12->data_reg_desc, 6); 454 if (item && !drvdata->attn_data.data) { 455 f12->data6 = item; 456 f12->data6_offset = data_offset; 457 data_offset += item->reg_size; 458 } 459 460 item = rmi_get_register_desc_item(&f12->data_reg_desc, 7); 461 if (item && !drvdata->attn_data.data) 462 data_offset += item->reg_size; 463 464 item = rmi_get_register_desc_item(&f12->data_reg_desc, 8); 465 if (item && !drvdata->attn_data.data) 466 data_offset += item->reg_size; 467 468 item = rmi_get_register_desc_item(&f12->data_reg_desc, 9); 469 if (item && !drvdata->attn_data.data) { 470 f12->data9 = item; 471 f12->data9_offset = data_offset; 472 data_offset += item->reg_size; 473 if (!sensor->report_abs) 474 sensor->report_rel = 1; 475 } 476 477 item = rmi_get_register_desc_item(&f12->data_reg_desc, 10); 478 if (item && !drvdata->attn_data.data) 479 data_offset += item->reg_size; 480 481 item = rmi_get_register_desc_item(&f12->data_reg_desc, 11); 482 if (item && !drvdata->attn_data.data) 483 data_offset += item->reg_size; 484 485 item = rmi_get_register_desc_item(&f12->data_reg_desc, 12); 486 if (item && !drvdata->attn_data.data) 487 data_offset += item->reg_size; 488 489 item = rmi_get_register_desc_item(&f12->data_reg_desc, 13); 490 if (item && !drvdata->attn_data.data) 491 data_offset += item->reg_size; 492 493 item = rmi_get_register_desc_item(&f12->data_reg_desc, 14); 494 if (item && !drvdata->attn_data.data) 495 data_offset += item->reg_size; 496 497 item = rmi_get_register_desc_item(&f12->data_reg_desc, 15); 498 if (item && !drvdata->attn_data.data) { 499 f12->data15 = item; 500 f12->data15_offset = data_offset; 501 data_offset += item->reg_size; 502 } 503 504 /* allocate the in-kernel tracking buffers */ 505 sensor->tracking_pos = devm_kzalloc(&fn->dev, 506 sizeof(struct input_mt_pos) * sensor->nbr_fingers, 507 GFP_KERNEL); 508 sensor->tracking_slots = devm_kzalloc(&fn->dev, 509 sizeof(int) * sensor->nbr_fingers, GFP_KERNEL); 510 sensor->objs = devm_kzalloc(&fn->dev, 511 sizeof(struct rmi_2d_sensor_abs_object) 512 * sensor->nbr_fingers, GFP_KERNEL); 513 if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs) 514 return -ENOMEM; 515 516 ret = rmi_2d_sensor_configure_input(fn, sensor); 517 if (ret) 518 return ret; 519 520 return 0; 521 } 522 523 struct rmi_function_handler rmi_f12_handler = { 524 .driver = { 525 .name = "rmi4_f12", 526 }, 527 .func = 0x12, 528 .probe = rmi_f12_probe, 529 .config = rmi_f12_config, 530 .attention = rmi_f12_attention, 531 }; 532