mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f12.c
blob88e91559c84e2dcad267a0e20fd4e65bc56238bb
1 /*
2 * Copyright (c) 2012-2016 Synaptics Incorporated
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"
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,
29 struct f12_data {
30 struct rmi_function *fn;
31 struct rmi_2d_sensor sensor;
32 struct rmi_2d_sensor_platform_data sensor_pdata;
34 u16 data_addr;
36 struct rmi_register_descriptor query_reg_desc;
37 struct rmi_register_descriptor control_reg_desc;
38 struct rmi_register_descriptor data_reg_desc;
40 /* F12 Data1 describes sensed objects */
41 const struct rmi_register_desc_item *data1;
42 u16 data1_offset;
44 /* F12 Data5 describes finger ACM */
45 const struct rmi_register_desc_item *data5;
46 u16 data5_offset;
48 /* F12 Data5 describes Pen */
49 const struct rmi_register_desc_item *data6;
50 u16 data6_offset;
53 /* F12 Data9 reports relative data */
54 const struct rmi_register_desc_item *data9;
55 u16 data9_offset;
57 const struct rmi_register_desc_item *data15;
58 u16 data15_offset;
61 static int rmi_f12_read_sensor_tuning(struct f12_data *f12)
63 const struct rmi_register_desc_item *item;
64 struct rmi_2d_sensor *sensor = &f12->sensor;
65 struct rmi_function *fn = sensor->fn;
66 struct rmi_device *rmi_dev = fn->rmi_dev;
67 int ret;
68 int offset;
69 u8 buf[15];
70 int pitch_x = 0;
71 int pitch_y = 0;
72 int clip_x_low = 0;
73 int clip_x_high = 0;
74 int clip_y_low = 0;
75 int clip_y_high = 0;
76 int rx_receivers = 0;
77 int tx_receivers = 0;
78 int sensor_flags = 0;
80 item = rmi_get_register_desc_item(&f12->control_reg_desc, 8);
81 if (!item) {
82 dev_err(&fn->dev,
83 "F12 does not have the sensor tuning control register\n");
84 return -ENODEV;
87 offset = rmi_register_desc_calc_reg_offset(&f12->control_reg_desc, 8);
89 if (item->reg_size > sizeof(buf)) {
90 dev_err(&fn->dev,
91 "F12 control8 should be no bigger than %zd bytes, not: %ld\n",
92 sizeof(buf), item->reg_size);
93 return -ENODEV;
96 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr + offset, buf,
97 item->reg_size);
98 if (ret)
99 return ret;
101 offset = 0;
102 if (rmi_register_desc_has_subpacket(item, 0)) {
103 sensor->max_x = (buf[offset + 1] << 8) | buf[offset];
104 sensor->max_y = (buf[offset + 3] << 8) | buf[offset + 2];
105 offset += 4;
108 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: max_x: %d max_y: %d\n", __func__,
109 sensor->max_x, sensor->max_y);
111 if (rmi_register_desc_has_subpacket(item, 1)) {
112 pitch_x = (buf[offset + 1] << 8) | buf[offset];
113 pitch_y = (buf[offset + 3] << 8) | buf[offset + 2];
114 offset += 4;
117 if (rmi_register_desc_has_subpacket(item, 2)) {
118 sensor->axis_align.clip_x_low = buf[offset];
119 sensor->axis_align.clip_x_high = sensor->max_x
120 - buf[offset + 1];
121 sensor->axis_align.clip_y_low = buf[offset + 2];
122 sensor->axis_align.clip_y_high = sensor->max_y
123 - buf[offset + 3];
124 offset += 4;
127 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x low: %d x high: %d y low: %d y high: %d\n",
128 __func__, clip_x_low, clip_x_high, clip_y_low, clip_y_high);
130 if (rmi_register_desc_has_subpacket(item, 3)) {
131 rx_receivers = buf[offset];
132 tx_receivers = buf[offset + 1];
133 offset += 2;
136 if (rmi_register_desc_has_subpacket(item, 4)) {
137 sensor_flags = buf[offset];
138 offset += 1;
141 sensor->x_mm = (pitch_x * rx_receivers) >> 12;
142 sensor->y_mm = (pitch_y * tx_receivers) >> 12;
144 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x_mm: %d y_mm: %d\n", __func__,
145 sensor->x_mm, sensor->y_mm);
147 return 0;
150 static void rmi_f12_process_objects(struct f12_data *f12, u8 *data1)
152 int i;
153 struct rmi_2d_sensor *sensor = &f12->sensor;
155 for (i = 0; i < f12->data1->num_subpackets; i++) {
156 struct rmi_2d_sensor_abs_object *obj = &sensor->objs[i];
158 obj->type = RMI_2D_OBJECT_NONE;
159 obj->mt_tool = MT_TOOL_FINGER;
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;
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];
184 rmi_2d_sensor_abs_process(sensor, obj, i);
186 data1 += 8;
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);
196 for (i = 0; i < sensor->nbr_fingers; i++)
197 rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
200 static int rmi_f12_attention(struct rmi_function *fn,
201 unsigned long *irq_nr_regs)
203 int retval;
204 struct rmi_device *rmi_dev = fn->rmi_dev;
205 struct f12_data *f12 = dev_get_drvdata(&fn->dev);
206 struct rmi_2d_sensor *sensor = &f12->sensor;
208 if (rmi_dev->xport->attn_data) {
209 memcpy(sensor->data_pkt, rmi_dev->xport->attn_data,
210 sensor->attn_size);
211 rmi_dev->xport->attn_data += sensor->attn_size;
212 rmi_dev->xport->attn_size -= sensor->attn_size;
213 } else {
214 retval = rmi_read_block(rmi_dev, f12->data_addr,
215 sensor->data_pkt, sensor->pkt_size);
216 if (retval < 0) {
217 dev_err(&fn->dev, "Failed to read object data. Code: %d.\n",
218 retval);
219 return retval;
223 if (f12->data1)
224 rmi_f12_process_objects(f12,
225 &sensor->data_pkt[f12->data1_offset]);
227 input_mt_sync_frame(sensor->input);
229 return 0;
232 static int rmi_f12_config(struct rmi_function *fn)
234 struct rmi_driver *drv = fn->rmi_dev->driver;
236 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
238 return 0;
241 static int rmi_f12_probe(struct rmi_function *fn)
243 struct f12_data *f12;
244 int ret;
245 struct rmi_device *rmi_dev = fn->rmi_dev;
246 char buf;
247 u16 query_addr = fn->fd.query_base_addr;
248 const struct rmi_register_desc_item *item;
249 struct rmi_2d_sensor *sensor;
250 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
251 struct rmi_transport_dev *xport = rmi_dev->xport;
252 u16 data_offset = 0;
254 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s\n", __func__);
256 ret = rmi_read(fn->rmi_dev, query_addr, &buf);
257 if (ret < 0) {
258 dev_err(&fn->dev, "Failed to read general info register: %d\n",
259 ret);
260 return -ENODEV;
262 ++query_addr;
264 if (!(buf & 0x1)) {
265 dev_err(&fn->dev,
266 "Behavior of F12 without register descriptors is undefined.\n");
267 return -ENODEV;
270 f12 = devm_kzalloc(&fn->dev, sizeof(struct f12_data), GFP_KERNEL);
271 if (!f12)
272 return -ENOMEM;
274 if (fn->dev.of_node) {
275 ret = rmi_2d_sensor_of_probe(&fn->dev, &f12->sensor_pdata);
276 if (ret)
277 return ret;
278 } else if (pdata->sensor_pdata) {
279 f12->sensor_pdata = *pdata->sensor_pdata;
282 ret = rmi_read_register_desc(rmi_dev, query_addr,
283 &f12->query_reg_desc);
284 if (ret) {
285 dev_err(&fn->dev,
286 "Failed to read the Query Register Descriptor: %d\n",
287 ret);
288 return ret;
290 query_addr += 3;
292 ret = rmi_read_register_desc(rmi_dev, query_addr,
293 &f12->control_reg_desc);
294 if (ret) {
295 dev_err(&fn->dev,
296 "Failed to read the Control Register Descriptor: %d\n",
297 ret);
298 return ret;
300 query_addr += 3;
302 ret = rmi_read_register_desc(rmi_dev, query_addr,
303 &f12->data_reg_desc);
304 if (ret) {
305 dev_err(&fn->dev,
306 "Failed to read the Data Register Descriptor: %d\n",
307 ret);
308 return ret;
310 query_addr += 3;
312 sensor = &f12->sensor;
313 sensor->fn = fn;
314 f12->data_addr = fn->fd.data_base_addr;
315 sensor->pkt_size = rmi_register_desc_calc_size(&f12->data_reg_desc);
317 sensor->axis_align =
318 f12->sensor_pdata.axis_align;
320 sensor->x_mm = f12->sensor_pdata.x_mm;
321 sensor->y_mm = f12->sensor_pdata.y_mm;
323 if (sensor->sensor_type == rmi_sensor_default)
324 sensor->sensor_type =
325 f12->sensor_pdata.sensor_type;
327 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: data packet size: %d\n", __func__,
328 sensor->pkt_size);
329 sensor->data_pkt = devm_kzalloc(&fn->dev, sensor->pkt_size, GFP_KERNEL);
330 if (!sensor->data_pkt)
331 return -ENOMEM;
333 dev_set_drvdata(&fn->dev, f12);
335 ret = rmi_f12_read_sensor_tuning(f12);
336 if (ret)
337 return ret;
340 * Figure out what data is contained in the data registers. HID devices
341 * may have registers defined, but their data is not reported in the
342 * HID attention report. Registers which are not reported in the HID
343 * attention report check to see if the device is receiving data from
344 * HID attention reports.
346 item = rmi_get_register_desc_item(&f12->data_reg_desc, 0);
347 if (item && !xport->attn_data)
348 data_offset += item->reg_size;
350 item = rmi_get_register_desc_item(&f12->data_reg_desc, 1);
351 if (item) {
352 f12->data1 = item;
353 f12->data1_offset = data_offset;
354 data_offset += item->reg_size;
355 sensor->nbr_fingers = item->num_subpackets;
356 sensor->report_abs = 1;
357 sensor->attn_size += item->reg_size;
360 item = rmi_get_register_desc_item(&f12->data_reg_desc, 2);
361 if (item && !xport->attn_data)
362 data_offset += item->reg_size;
364 item = rmi_get_register_desc_item(&f12->data_reg_desc, 3);
365 if (item && !xport->attn_data)
366 data_offset += item->reg_size;
368 item = rmi_get_register_desc_item(&f12->data_reg_desc, 4);
369 if (item && !xport->attn_data)
370 data_offset += item->reg_size;
372 item = rmi_get_register_desc_item(&f12->data_reg_desc, 5);
373 if (item) {
374 f12->data5 = item;
375 f12->data5_offset = data_offset;
376 data_offset += item->reg_size;
377 sensor->attn_size += item->reg_size;
380 item = rmi_get_register_desc_item(&f12->data_reg_desc, 6);
381 if (item && !xport->attn_data) {
382 f12->data6 = item;
383 f12->data6_offset = data_offset;
384 data_offset += item->reg_size;
387 item = rmi_get_register_desc_item(&f12->data_reg_desc, 7);
388 if (item && !xport->attn_data)
389 data_offset += item->reg_size;
391 item = rmi_get_register_desc_item(&f12->data_reg_desc, 8);
392 if (item && !xport->attn_data)
393 data_offset += item->reg_size;
395 item = rmi_get_register_desc_item(&f12->data_reg_desc, 9);
396 if (item && !xport->attn_data) {
397 f12->data9 = item;
398 f12->data9_offset = data_offset;
399 data_offset += item->reg_size;
400 if (!sensor->report_abs)
401 sensor->report_rel = 1;
404 item = rmi_get_register_desc_item(&f12->data_reg_desc, 10);
405 if (item && !xport->attn_data)
406 data_offset += item->reg_size;
408 item = rmi_get_register_desc_item(&f12->data_reg_desc, 11);
409 if (item && !xport->attn_data)
410 data_offset += item->reg_size;
412 item = rmi_get_register_desc_item(&f12->data_reg_desc, 12);
413 if (item && !xport->attn_data)
414 data_offset += item->reg_size;
416 item = rmi_get_register_desc_item(&f12->data_reg_desc, 13);
417 if (item && !xport->attn_data)
418 data_offset += item->reg_size;
420 item = rmi_get_register_desc_item(&f12->data_reg_desc, 14);
421 if (item && !xport->attn_data)
422 data_offset += item->reg_size;
424 item = rmi_get_register_desc_item(&f12->data_reg_desc, 15);
425 if (item && !xport->attn_data) {
426 f12->data15 = item;
427 f12->data15_offset = data_offset;
428 data_offset += item->reg_size;
431 /* allocate the in-kernel tracking buffers */
432 sensor->tracking_pos = devm_kzalloc(&fn->dev,
433 sizeof(struct input_mt_pos) * sensor->nbr_fingers,
434 GFP_KERNEL);
435 sensor->tracking_slots = devm_kzalloc(&fn->dev,
436 sizeof(int) * sensor->nbr_fingers, GFP_KERNEL);
437 sensor->objs = devm_kzalloc(&fn->dev,
438 sizeof(struct rmi_2d_sensor_abs_object)
439 * sensor->nbr_fingers, GFP_KERNEL);
440 if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
441 return -ENOMEM;
443 ret = rmi_2d_sensor_configure_input(fn, sensor);
444 if (ret)
445 return ret;
447 return 0;
450 struct rmi_function_handler rmi_f12_handler = {
451 .driver = {
452 .name = "rmi4_f12",
454 .func = 0x12,
455 .probe = rmi_f12_probe,
456 .config = rmi_f12_config,
457 .attention = rmi_f12_attention,