]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/input/rmi4/rmi_driver.c
Input: synaptics-rmi4 - add rmi_find_function()
[mirror_ubuntu-bionic-kernel.git] / drivers / input / rmi4 / rmi_driver.c
1 /*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This driver provides the core support for a single RMI4-based device.
6 *
7 * The RMI4 specification can be found here (URL split for line length):
8 *
9 * http://www.synaptics.com/sites/default/files/
10 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 */
16
17 #include <linux/bitmap.h>
18 #include <linux/delay.h>
19 #include <linux/fs.h>
20 #include <linux/irq.h>
21 #include <linux/kconfig.h>
22 #include <linux/pm.h>
23 #include <linux/slab.h>
24 #include <linux/of.h>
25 #include <uapi/linux/input.h>
26 #include <linux/rmi.h>
27 #include "rmi_bus.h"
28 #include "rmi_driver.h"
29
30 #define HAS_NONSTANDARD_PDT_MASK 0x40
31 #define RMI4_MAX_PAGE 0xff
32 #define RMI4_PAGE_SIZE 0x100
33 #define RMI4_PAGE_MASK 0xFF00
34
35 #define RMI_DEVICE_RESET_CMD 0x01
36 #define DEFAULT_RESET_DELAY_MS 100
37
38 void rmi_free_function_list(struct rmi_device *rmi_dev)
39 {
40 struct rmi_function *fn, *tmp;
41 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
42
43 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
44
45 devm_kfree(&rmi_dev->dev, data->irq_memory);
46 data->irq_memory = NULL;
47 data->irq_status = NULL;
48 data->fn_irq_bits = NULL;
49 data->current_irq_mask = NULL;
50 data->new_irq_mask = NULL;
51
52 data->f01_container = NULL;
53 data->f34_container = NULL;
54
55 /* Doing it in the reverse order so F01 will be removed last */
56 list_for_each_entry_safe_reverse(fn, tmp,
57 &data->function_list, node) {
58 list_del(&fn->node);
59 rmi_unregister_function(fn);
60 }
61 }
62
63 static int reset_one_function(struct rmi_function *fn)
64 {
65 struct rmi_function_handler *fh;
66 int retval = 0;
67
68 if (!fn || !fn->dev.driver)
69 return 0;
70
71 fh = to_rmi_function_handler(fn->dev.driver);
72 if (fh->reset) {
73 retval = fh->reset(fn);
74 if (retval < 0)
75 dev_err(&fn->dev, "Reset failed with code %d.\n",
76 retval);
77 }
78
79 return retval;
80 }
81
82 static int configure_one_function(struct rmi_function *fn)
83 {
84 struct rmi_function_handler *fh;
85 int retval = 0;
86
87 if (!fn || !fn->dev.driver)
88 return 0;
89
90 fh = to_rmi_function_handler(fn->dev.driver);
91 if (fh->config) {
92 retval = fh->config(fn);
93 if (retval < 0)
94 dev_err(&fn->dev, "Config failed with code %d.\n",
95 retval);
96 }
97
98 return retval;
99 }
100
101 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
102 {
103 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
104 struct rmi_function *entry;
105 int retval;
106
107 list_for_each_entry(entry, &data->function_list, node) {
108 retval = reset_one_function(entry);
109 if (retval < 0)
110 return retval;
111 }
112
113 return 0;
114 }
115
116 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
117 {
118 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
119 struct rmi_function *entry;
120 int retval;
121
122 list_for_each_entry(entry, &data->function_list, node) {
123 retval = configure_one_function(entry);
124 if (retval < 0)
125 return retval;
126 }
127
128 return 0;
129 }
130
131 static void process_one_interrupt(struct rmi_driver_data *data,
132 struct rmi_function *fn)
133 {
134 struct rmi_function_handler *fh;
135
136 if (!fn || !fn->dev.driver)
137 return;
138
139 fh = to_rmi_function_handler(fn->dev.driver);
140 if (fh->attention) {
141 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
142 data->irq_count);
143 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
144 fh->attention(fn, data->fn_irq_bits);
145 }
146 }
147
148 static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
149 {
150 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
151 struct device *dev = &rmi_dev->dev;
152 struct rmi_function *entry;
153 int error;
154
155 if (!data)
156 return 0;
157
158 if (!data->attn_data.data) {
159 error = rmi_read_block(rmi_dev,
160 data->f01_container->fd.data_base_addr + 1,
161 data->irq_status, data->num_of_irq_regs);
162 if (error < 0) {
163 dev_err(dev, "Failed to read irqs, code=%d\n", error);
164 return error;
165 }
166 }
167
168 mutex_lock(&data->irq_mutex);
169 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
170 data->irq_count);
171 /*
172 * At this point, irq_status has all bits that are set in the
173 * interrupt status register and are enabled.
174 */
175 mutex_unlock(&data->irq_mutex);
176
177 /*
178 * It would be nice to be able to use irq_chip to handle these
179 * nested IRQs. Unfortunately, most of the current customers for
180 * this driver are using older kernels (3.0.x) that don't support
181 * the features required for that. Once they've shifted to more
182 * recent kernels (say, 3.3 and higher), this should be switched to
183 * use irq_chip.
184 */
185 list_for_each_entry(entry, &data->function_list, node)
186 process_one_interrupt(data, entry);
187
188 if (data->input)
189 input_sync(data->input);
190
191 return 0;
192 }
193
194 void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
195 void *data, size_t size)
196 {
197 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
198 struct rmi4_attn_data attn_data;
199 void *fifo_data;
200
201 if (!drvdata->enabled)
202 return;
203
204 fifo_data = kmemdup(data, size, GFP_ATOMIC);
205 if (!fifo_data)
206 return;
207
208 attn_data.irq_status = irq_status;
209 attn_data.size = size;
210 attn_data.data = fifo_data;
211
212 kfifo_put(&drvdata->attn_fifo, attn_data);
213 }
214 EXPORT_SYMBOL_GPL(rmi_set_attn_data);
215
216 static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
217 {
218 struct rmi_device *rmi_dev = dev_id;
219 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
220 struct rmi4_attn_data attn_data = {0};
221 int ret, count;
222
223 count = kfifo_get(&drvdata->attn_fifo, &attn_data);
224 if (count) {
225 *(drvdata->irq_status) = attn_data.irq_status;
226 drvdata->attn_data = attn_data;
227 }
228
229 ret = rmi_process_interrupt_requests(rmi_dev);
230 if (ret)
231 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
232 "Failed to process interrupt request: %d\n", ret);
233
234 if (count)
235 kfree(attn_data.data);
236
237 if (!kfifo_is_empty(&drvdata->attn_fifo))
238 return rmi_irq_fn(irq, dev_id);
239
240 return IRQ_HANDLED;
241 }
242
243 static int rmi_irq_init(struct rmi_device *rmi_dev)
244 {
245 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
246 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
247 int irq_flags = irq_get_trigger_type(pdata->irq);
248 int ret;
249
250 if (!irq_flags)
251 irq_flags = IRQF_TRIGGER_LOW;
252
253 ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
254 rmi_irq_fn, irq_flags | IRQF_ONESHOT,
255 dev_name(rmi_dev->xport->dev),
256 rmi_dev);
257 if (ret < 0) {
258 dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
259 pdata->irq);
260
261 return ret;
262 }
263
264 data->enabled = true;
265
266 return 0;
267 }
268
269 struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
270 {
271 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
272 struct rmi_function *entry;
273
274 list_for_each_entry(entry, &data->function_list, node) {
275 if (entry->fd.function_number == number)
276 return entry;
277 }
278
279 return NULL;
280 }
281
282 static int suspend_one_function(struct rmi_function *fn)
283 {
284 struct rmi_function_handler *fh;
285 int retval = 0;
286
287 if (!fn || !fn->dev.driver)
288 return 0;
289
290 fh = to_rmi_function_handler(fn->dev.driver);
291 if (fh->suspend) {
292 retval = fh->suspend(fn);
293 if (retval < 0)
294 dev_err(&fn->dev, "Suspend failed with code %d.\n",
295 retval);
296 }
297
298 return retval;
299 }
300
301 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
302 {
303 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
304 struct rmi_function *entry;
305 int retval;
306
307 list_for_each_entry(entry, &data->function_list, node) {
308 retval = suspend_one_function(entry);
309 if (retval < 0)
310 return retval;
311 }
312
313 return 0;
314 }
315
316 static int resume_one_function(struct rmi_function *fn)
317 {
318 struct rmi_function_handler *fh;
319 int retval = 0;
320
321 if (!fn || !fn->dev.driver)
322 return 0;
323
324 fh = to_rmi_function_handler(fn->dev.driver);
325 if (fh->resume) {
326 retval = fh->resume(fn);
327 if (retval < 0)
328 dev_err(&fn->dev, "Resume failed with code %d.\n",
329 retval);
330 }
331
332 return retval;
333 }
334
335 static int rmi_resume_functions(struct rmi_device *rmi_dev)
336 {
337 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
338 struct rmi_function *entry;
339 int retval;
340
341 list_for_each_entry(entry, &data->function_list, node) {
342 retval = resume_one_function(entry);
343 if (retval < 0)
344 return retval;
345 }
346
347 return 0;
348 }
349
350 int rmi_enable_sensor(struct rmi_device *rmi_dev)
351 {
352 int retval = 0;
353
354 retval = rmi_driver_process_config_requests(rmi_dev);
355 if (retval < 0)
356 return retval;
357
358 return rmi_process_interrupt_requests(rmi_dev);
359 }
360
361 /**
362 * rmi_driver_set_input_params - set input device id and other data.
363 *
364 * @rmi_dev: Pointer to an RMI device
365 * @input: Pointer to input device
366 *
367 */
368 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
369 struct input_dev *input)
370 {
371 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
372 input->id.vendor = SYNAPTICS_VENDOR_ID;
373 input->id.bustype = BUS_RMI;
374 return 0;
375 }
376
377 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
378 struct input_dev *input)
379 {
380 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
381 const char *device_name = rmi_f01_get_product_ID(data->f01_container);
382 char *name;
383
384 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
385 "Synaptics %s", device_name);
386 if (!name)
387 return;
388
389 input->name = name;
390 }
391
392 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
393 unsigned long *mask)
394 {
395 int error = 0;
396 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
397 struct device *dev = &rmi_dev->dev;
398
399 mutex_lock(&data->irq_mutex);
400 bitmap_or(data->new_irq_mask,
401 data->current_irq_mask, mask, data->irq_count);
402
403 error = rmi_write_block(rmi_dev,
404 data->f01_container->fd.control_base_addr + 1,
405 data->new_irq_mask, data->num_of_irq_regs);
406 if (error < 0) {
407 dev_err(dev, "%s: Failed to change enabled interrupts!",
408 __func__);
409 goto error_unlock;
410 }
411 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
412 data->num_of_irq_regs);
413
414 error_unlock:
415 mutex_unlock(&data->irq_mutex);
416 return error;
417 }
418
419 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
420 unsigned long *mask)
421 {
422 int error = 0;
423 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
424 struct device *dev = &rmi_dev->dev;
425
426 mutex_lock(&data->irq_mutex);
427 bitmap_andnot(data->new_irq_mask,
428 data->current_irq_mask, mask, data->irq_count);
429
430 error = rmi_write_block(rmi_dev,
431 data->f01_container->fd.control_base_addr + 1,
432 data->new_irq_mask, data->num_of_irq_regs);
433 if (error < 0) {
434 dev_err(dev, "%s: Failed to change enabled interrupts!",
435 __func__);
436 goto error_unlock;
437 }
438 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
439 data->num_of_irq_regs);
440
441 error_unlock:
442 mutex_unlock(&data->irq_mutex);
443 return error;
444 }
445
446 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
447 {
448 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
449 int error;
450
451 /*
452 * Can get called before the driver is fully ready to deal with
453 * this situation.
454 */
455 if (!data || !data->f01_container) {
456 dev_warn(&rmi_dev->dev,
457 "Not ready to handle reset yet!\n");
458 return 0;
459 }
460
461 error = rmi_read_block(rmi_dev,
462 data->f01_container->fd.control_base_addr + 1,
463 data->current_irq_mask, data->num_of_irq_regs);
464 if (error < 0) {
465 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
466 __func__);
467 return error;
468 }
469
470 error = rmi_driver_process_reset_requests(rmi_dev);
471 if (error < 0)
472 return error;
473
474 error = rmi_driver_process_config_requests(rmi_dev);
475 if (error < 0)
476 return error;
477
478 return 0;
479 }
480
481 static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
482 struct pdt_entry *entry, u16 pdt_address)
483 {
484 u8 buf[RMI_PDT_ENTRY_SIZE];
485 int error;
486
487 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
488 if (error) {
489 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
490 pdt_address, error);
491 return error;
492 }
493
494 entry->page_start = pdt_address & RMI4_PAGE_MASK;
495 entry->query_base_addr = buf[0];
496 entry->command_base_addr = buf[1];
497 entry->control_base_addr = buf[2];
498 entry->data_base_addr = buf[3];
499 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
500 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
501 entry->function_number = buf[5];
502
503 return 0;
504 }
505
506 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
507 struct rmi_function_descriptor *fd)
508 {
509 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
510 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
511 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
512 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
513 fd->function_number = pdt->function_number;
514 fd->interrupt_source_count = pdt->interrupt_source_count;
515 fd->function_version = pdt->function_version;
516 }
517
518 #define RMI_SCAN_CONTINUE 0
519 #define RMI_SCAN_DONE 1
520
521 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
522 int page,
523 int *empty_pages,
524 void *ctx,
525 int (*callback)(struct rmi_device *rmi_dev,
526 void *ctx,
527 const struct pdt_entry *entry))
528 {
529 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
530 struct pdt_entry pdt_entry;
531 u16 page_start = RMI4_PAGE_SIZE * page;
532 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
533 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
534 u16 addr;
535 int error;
536 int retval;
537
538 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
539 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
540 if (error)
541 return error;
542
543 if (RMI4_END_OF_PDT(pdt_entry.function_number))
544 break;
545
546 retval = callback(rmi_dev, ctx, &pdt_entry);
547 if (retval != RMI_SCAN_CONTINUE)
548 return retval;
549 }
550
551 /*
552 * Count number of empty PDT pages. If a gap of two pages
553 * or more is found, stop scanning.
554 */
555 if (addr == pdt_start)
556 ++*empty_pages;
557 else
558 *empty_pages = 0;
559
560 return (data->bootloader_mode || *empty_pages >= 2) ?
561 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
562 }
563
564 int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
565 int (*callback)(struct rmi_device *rmi_dev,
566 void *ctx, const struct pdt_entry *entry))
567 {
568 int page;
569 int empty_pages = 0;
570 int retval = RMI_SCAN_DONE;
571
572 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
573 retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
574 ctx, callback);
575 if (retval != RMI_SCAN_CONTINUE)
576 break;
577 }
578
579 return retval < 0 ? retval : 0;
580 }
581
582 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
583 struct rmi_register_descriptor *rdesc)
584 {
585 int ret;
586 u8 size_presence_reg;
587 u8 buf[35];
588 int presense_offset = 1;
589 u8 *struct_buf;
590 int reg;
591 int offset = 0;
592 int map_offset = 0;
593 int i;
594 int b;
595
596 /*
597 * The first register of the register descriptor is the size of
598 * the register descriptor's presense register.
599 */
600 ret = rmi_read(d, addr, &size_presence_reg);
601 if (ret)
602 return ret;
603 ++addr;
604
605 if (size_presence_reg < 0 || size_presence_reg > 35)
606 return -EIO;
607
608 memset(buf, 0, sizeof(buf));
609
610 /*
611 * The presence register contains the size of the register structure
612 * and a bitmap which identified which packet registers are present
613 * for this particular register type (ie query, control, or data).
614 */
615 ret = rmi_read_block(d, addr, buf, size_presence_reg);
616 if (ret)
617 return ret;
618 ++addr;
619
620 if (buf[0] == 0) {
621 presense_offset = 3;
622 rdesc->struct_size = buf[1] | (buf[2] << 8);
623 } else {
624 rdesc->struct_size = buf[0];
625 }
626
627 for (i = presense_offset; i < size_presence_reg; i++) {
628 for (b = 0; b < 8; b++) {
629 if (buf[i] & (0x1 << b))
630 bitmap_set(rdesc->presense_map, map_offset, 1);
631 ++map_offset;
632 }
633 }
634
635 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
636 RMI_REG_DESC_PRESENSE_BITS);
637
638 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
639 sizeof(struct rmi_register_desc_item),
640 GFP_KERNEL);
641 if (!rdesc->registers)
642 return -ENOMEM;
643
644 /*
645 * Allocate a temporary buffer to hold the register structure.
646 * I'm not using devm_kzalloc here since it will not be retained
647 * after exiting this function
648 */
649 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
650 if (!struct_buf)
651 return -ENOMEM;
652
653 /*
654 * The register structure contains information about every packet
655 * register of this type. This includes the size of the packet
656 * register and a bitmap of all subpackets contained in the packet
657 * register.
658 */
659 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
660 if (ret)
661 goto free_struct_buff;
662
663 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
664 for (i = 0; i < rdesc->num_registers; i++) {
665 struct rmi_register_desc_item *item = &rdesc->registers[i];
666 int reg_size = struct_buf[offset];
667
668 ++offset;
669 if (reg_size == 0) {
670 reg_size = struct_buf[offset] |
671 (struct_buf[offset + 1] << 8);
672 offset += 2;
673 }
674
675 if (reg_size == 0) {
676 reg_size = struct_buf[offset] |
677 (struct_buf[offset + 1] << 8) |
678 (struct_buf[offset + 2] << 16) |
679 (struct_buf[offset + 3] << 24);
680 offset += 4;
681 }
682
683 item->reg = reg;
684 item->reg_size = reg_size;
685
686 map_offset = 0;
687
688 do {
689 for (b = 0; b < 7; b++) {
690 if (struct_buf[offset] & (0x1 << b))
691 bitmap_set(item->subpacket_map,
692 map_offset, 1);
693 ++map_offset;
694 }
695 } while (struct_buf[offset++] & 0x80);
696
697 item->num_subpackets = bitmap_weight(item->subpacket_map,
698 RMI_REG_DESC_SUBPACKET_BITS);
699
700 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
701 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
702 item->reg, item->reg_size, item->num_subpackets);
703
704 reg = find_next_bit(rdesc->presense_map,
705 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
706 }
707
708 free_struct_buff:
709 kfree(struct_buf);
710 return ret;
711 }
712
713 const struct rmi_register_desc_item *rmi_get_register_desc_item(
714 struct rmi_register_descriptor *rdesc, u16 reg)
715 {
716 const struct rmi_register_desc_item *item;
717 int i;
718
719 for (i = 0; i < rdesc->num_registers; i++) {
720 item = &rdesc->registers[i];
721 if (item->reg == reg)
722 return item;
723 }
724
725 return NULL;
726 }
727
728 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
729 {
730 const struct rmi_register_desc_item *item;
731 int i;
732 size_t size = 0;
733
734 for (i = 0; i < rdesc->num_registers; i++) {
735 item = &rdesc->registers[i];
736 size += item->reg_size;
737 }
738 return size;
739 }
740
741 /* Compute the register offset relative to the base address */
742 int rmi_register_desc_calc_reg_offset(
743 struct rmi_register_descriptor *rdesc, u16 reg)
744 {
745 const struct rmi_register_desc_item *item;
746 int offset = 0;
747 int i;
748
749 for (i = 0; i < rdesc->num_registers; i++) {
750 item = &rdesc->registers[i];
751 if (item->reg == reg)
752 return offset;
753 ++offset;
754 }
755 return -1;
756 }
757
758 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
759 u8 subpacket)
760 {
761 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
762 subpacket) == subpacket;
763 }
764
765 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
766 const struct pdt_entry *pdt)
767 {
768 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
769 int ret;
770 u8 status;
771
772 if (pdt->function_number == 0x34 && pdt->function_version > 1) {
773 ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
774 if (ret) {
775 dev_err(&rmi_dev->dev,
776 "Failed to read F34 status: %d.\n", ret);
777 return ret;
778 }
779
780 if (status & BIT(7))
781 data->bootloader_mode = true;
782 } else if (pdt->function_number == 0x01) {
783 ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
784 if (ret) {
785 dev_err(&rmi_dev->dev,
786 "Failed to read F01 status: %d.\n", ret);
787 return ret;
788 }
789
790 if (status & BIT(6))
791 data->bootloader_mode = true;
792 }
793
794 return 0;
795 }
796
797 static int rmi_count_irqs(struct rmi_device *rmi_dev,
798 void *ctx, const struct pdt_entry *pdt)
799 {
800 int *irq_count = ctx;
801 int ret;
802
803 *irq_count += pdt->interrupt_source_count;
804
805 ret = rmi_check_bootloader_mode(rmi_dev, pdt);
806 if (ret < 0)
807 return ret;
808
809 return RMI_SCAN_CONTINUE;
810 }
811
812 int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
813 const struct pdt_entry *pdt)
814 {
815 int error;
816
817 if (pdt->function_number == 0x01) {
818 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
819 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
820 const struct rmi_device_platform_data *pdata =
821 rmi_get_platform_data(rmi_dev);
822
823 if (rmi_dev->xport->ops->reset) {
824 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
825 cmd_addr);
826 if (error)
827 return error;
828
829 return RMI_SCAN_DONE;
830 }
831
832 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
833 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
834 if (error) {
835 dev_err(&rmi_dev->dev,
836 "Initial reset failed. Code = %d.\n", error);
837 return error;
838 }
839
840 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
841
842 return RMI_SCAN_DONE;
843 }
844
845 /* F01 should always be on page 0. If we don't find it there, fail. */
846 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
847 }
848
849 static int rmi_create_function(struct rmi_device *rmi_dev,
850 void *ctx, const struct pdt_entry *pdt)
851 {
852 struct device *dev = &rmi_dev->dev;
853 struct rmi_driver_data *data = dev_get_drvdata(dev);
854 int *current_irq_count = ctx;
855 struct rmi_function *fn;
856 int i;
857 int error;
858
859 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
860 pdt->function_number);
861
862 fn = kzalloc(sizeof(struct rmi_function) +
863 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
864 GFP_KERNEL);
865 if (!fn) {
866 dev_err(dev, "Failed to allocate memory for F%02X\n",
867 pdt->function_number);
868 return -ENOMEM;
869 }
870
871 INIT_LIST_HEAD(&fn->node);
872 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
873
874 fn->rmi_dev = rmi_dev;
875
876 fn->num_of_irqs = pdt->interrupt_source_count;
877 fn->irq_pos = *current_irq_count;
878 *current_irq_count += fn->num_of_irqs;
879
880 for (i = 0; i < fn->num_of_irqs; i++)
881 set_bit(fn->irq_pos + i, fn->irq_mask);
882
883 error = rmi_register_function(fn);
884 if (error)
885 goto err_put_fn;
886
887 if (pdt->function_number == 0x01)
888 data->f01_container = fn;
889 else if (pdt->function_number == 0x34)
890 data->f34_container = fn;
891
892 list_add_tail(&fn->node, &data->function_list);
893
894 return RMI_SCAN_CONTINUE;
895
896 err_put_fn:
897 put_device(&fn->dev);
898 return error;
899 }
900
901 void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
902 {
903 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
904 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
905 int irq = pdata->irq;
906 int irq_flags;
907 int retval;
908
909 mutex_lock(&data->enabled_mutex);
910
911 if (data->enabled)
912 goto out;
913
914 enable_irq(irq);
915 data->enabled = true;
916 if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
917 retval = disable_irq_wake(irq);
918 if (!retval)
919 dev_warn(&rmi_dev->dev,
920 "Failed to disable irq for wake: %d\n",
921 retval);
922 }
923
924 /*
925 * Call rmi_process_interrupt_requests() after enabling irq,
926 * otherwise we may lose interrupt on edge-triggered systems.
927 */
928 irq_flags = irq_get_trigger_type(pdata->irq);
929 if (irq_flags & IRQ_TYPE_EDGE_BOTH)
930 rmi_process_interrupt_requests(rmi_dev);
931
932 out:
933 mutex_unlock(&data->enabled_mutex);
934 }
935
936 void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
937 {
938 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
939 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
940 struct rmi4_attn_data attn_data = {0};
941 int irq = pdata->irq;
942 int retval, count;
943
944 mutex_lock(&data->enabled_mutex);
945
946 if (!data->enabled)
947 goto out;
948
949 data->enabled = false;
950 disable_irq(irq);
951 if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
952 retval = enable_irq_wake(irq);
953 if (!retval)
954 dev_warn(&rmi_dev->dev,
955 "Failed to enable irq for wake: %d\n",
956 retval);
957 }
958
959 /* make sure the fifo is clean */
960 while (!kfifo_is_empty(&data->attn_fifo)) {
961 count = kfifo_get(&data->attn_fifo, &attn_data);
962 if (count)
963 kfree(attn_data.data);
964 }
965
966 out:
967 mutex_unlock(&data->enabled_mutex);
968 }
969
970 int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
971 {
972 int retval;
973
974 retval = rmi_suspend_functions(rmi_dev);
975 if (retval)
976 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
977 retval);
978
979 rmi_disable_irq(rmi_dev, enable_wake);
980 return retval;
981 }
982 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
983
984 int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
985 {
986 int retval;
987
988 rmi_enable_irq(rmi_dev, clear_wake);
989
990 retval = rmi_resume_functions(rmi_dev);
991 if (retval)
992 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
993 retval);
994
995 return retval;
996 }
997 EXPORT_SYMBOL_GPL(rmi_driver_resume);
998
999 static int rmi_driver_remove(struct device *dev)
1000 {
1001 struct rmi_device *rmi_dev = to_rmi_device(dev);
1002
1003 rmi_disable_irq(rmi_dev, false);
1004
1005 rmi_f34_remove_sysfs(rmi_dev);
1006 rmi_free_function_list(rmi_dev);
1007
1008 return 0;
1009 }
1010
1011 #ifdef CONFIG_OF
1012 static int rmi_driver_of_probe(struct device *dev,
1013 struct rmi_device_platform_data *pdata)
1014 {
1015 int retval;
1016
1017 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
1018 "syna,reset-delay-ms", 1);
1019 if (retval)
1020 return retval;
1021
1022 return 0;
1023 }
1024 #else
1025 static inline int rmi_driver_of_probe(struct device *dev,
1026 struct rmi_device_platform_data *pdata)
1027 {
1028 return -ENODEV;
1029 }
1030 #endif
1031
1032 int rmi_probe_interrupts(struct rmi_driver_data *data)
1033 {
1034 struct rmi_device *rmi_dev = data->rmi_dev;
1035 struct device *dev = &rmi_dev->dev;
1036 int irq_count;
1037 size_t size;
1038 int retval;
1039
1040 /*
1041 * We need to count the IRQs and allocate their storage before scanning
1042 * the PDT and creating the function entries, because adding a new
1043 * function can trigger events that result in the IRQ related storage
1044 * being accessed.
1045 */
1046 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
1047 irq_count = 0;
1048 data->bootloader_mode = false;
1049
1050 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
1051 if (retval < 0) {
1052 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
1053 return retval;
1054 }
1055
1056 if (data->bootloader_mode)
1057 dev_warn(dev, "Device in bootloader mode.\n");
1058
1059 data->irq_count = irq_count;
1060 data->num_of_irq_regs = (data->irq_count + 7) / 8;
1061
1062 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1063 data->irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
1064 if (!data->irq_memory) {
1065 dev_err(dev, "Failed to allocate memory for irq masks.\n");
1066 return retval;
1067 }
1068
1069 data->irq_status = data->irq_memory + size * 0;
1070 data->fn_irq_bits = data->irq_memory + size * 1;
1071 data->current_irq_mask = data->irq_memory + size * 2;
1072 data->new_irq_mask = data->irq_memory + size * 3;
1073
1074 return retval;
1075 }
1076
1077 int rmi_init_functions(struct rmi_driver_data *data)
1078 {
1079 struct rmi_device *rmi_dev = data->rmi_dev;
1080 struct device *dev = &rmi_dev->dev;
1081 int irq_count;
1082 int retval;
1083
1084 irq_count = 0;
1085 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
1086 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
1087 if (retval < 0) {
1088 dev_err(dev, "Function creation failed with code %d.\n",
1089 retval);
1090 goto err_destroy_functions;
1091 }
1092
1093 if (!data->f01_container) {
1094 dev_err(dev, "Missing F01 container!\n");
1095 retval = -EINVAL;
1096 goto err_destroy_functions;
1097 }
1098
1099 retval = rmi_read_block(rmi_dev,
1100 data->f01_container->fd.control_base_addr + 1,
1101 data->current_irq_mask, data->num_of_irq_regs);
1102 if (retval < 0) {
1103 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
1104 __func__);
1105 goto err_destroy_functions;
1106 }
1107
1108 return 0;
1109
1110 err_destroy_functions:
1111 rmi_free_function_list(rmi_dev);
1112 return retval;
1113 }
1114
1115 static int rmi_driver_probe(struct device *dev)
1116 {
1117 struct rmi_driver *rmi_driver;
1118 struct rmi_driver_data *data;
1119 struct rmi_device_platform_data *pdata;
1120 struct rmi_device *rmi_dev;
1121 int retval;
1122
1123 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
1124 __func__);
1125
1126 if (!rmi_is_physical_device(dev)) {
1127 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
1128 return -ENODEV;
1129 }
1130
1131 rmi_dev = to_rmi_device(dev);
1132 rmi_driver = to_rmi_driver(dev->driver);
1133 rmi_dev->driver = rmi_driver;
1134
1135 pdata = rmi_get_platform_data(rmi_dev);
1136
1137 if (rmi_dev->xport->dev->of_node) {
1138 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1139 if (retval)
1140 return retval;
1141 }
1142
1143 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
1144 if (!data)
1145 return -ENOMEM;
1146
1147 INIT_LIST_HEAD(&data->function_list);
1148 data->rmi_dev = rmi_dev;
1149 dev_set_drvdata(&rmi_dev->dev, data);
1150
1151 /*
1152 * Right before a warm boot, the sensor might be in some unusual state,
1153 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1154 * or configuration update. In order to clear the sensor to a known
1155 * state and/or apply any updates, we issue a initial reset to clear any
1156 * previous settings and force it into normal operation.
1157 *
1158 * We have to do this before actually building the PDT because
1159 * the reflash updates (if any) might cause various registers to move
1160 * around.
1161 *
1162 * For a number of reasons, this initial reset may fail to return
1163 * within the specified time, but we'll still be able to bring up the
1164 * driver normally after that failure. This occurs most commonly in
1165 * a cold boot situation (where then firmware takes longer to come up
1166 * than from a warm boot) and the reset_delay_ms in the platform data
1167 * has been set too short to accommodate that. Since the sensor will
1168 * eventually come up and be usable, we don't want to just fail here
1169 * and leave the customer's device unusable. So we warn them, and
1170 * continue processing.
1171 */
1172 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
1173 if (retval < 0)
1174 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1175
1176 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1177 if (retval < 0) {
1178 /*
1179 * we'll print out a warning and continue since
1180 * failure to get the PDT properties is not a cause to fail
1181 */
1182 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1183 PDT_PROPERTIES_LOCATION, retval);
1184 }
1185
1186 mutex_init(&data->irq_mutex);
1187 mutex_init(&data->enabled_mutex);
1188
1189 retval = rmi_probe_interrupts(data);
1190 if (retval)
1191 goto err;
1192
1193 if (rmi_dev->xport->input) {
1194 /*
1195 * The transport driver already has an input device.
1196 * In some cases it is preferable to reuse the transport
1197 * devices input device instead of creating a new one here.
1198 * One example is some HID touchpads report "pass-through"
1199 * button events are not reported by rmi registers.
1200 */
1201 data->input = rmi_dev->xport->input;
1202 } else {
1203 data->input = devm_input_allocate_device(dev);
1204 if (!data->input) {
1205 dev_err(dev, "%s: Failed to allocate input device.\n",
1206 __func__);
1207 retval = -ENOMEM;
1208 goto err;
1209 }
1210 rmi_driver_set_input_params(rmi_dev, data->input);
1211 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1212 "%s/input0", dev_name(dev));
1213 }
1214
1215 retval = rmi_init_functions(data);
1216 if (retval)
1217 goto err;
1218
1219 retval = rmi_f34_create_sysfs(rmi_dev);
1220 if (retval)
1221 goto err;
1222
1223 if (data->input) {
1224 rmi_driver_set_input_name(rmi_dev, data->input);
1225 if (!rmi_dev->xport->input) {
1226 if (input_register_device(data->input)) {
1227 dev_err(dev, "%s: Failed to register input device.\n",
1228 __func__);
1229 goto err_destroy_functions;
1230 }
1231 }
1232 }
1233
1234 retval = rmi_irq_init(rmi_dev);
1235 if (retval < 0)
1236 goto err_destroy_functions;
1237
1238 if (data->f01_container->dev.driver)
1239 /* Driver already bound, so enable ATTN now. */
1240 return rmi_enable_sensor(rmi_dev);
1241
1242 return 0;
1243
1244 err_destroy_functions:
1245 rmi_free_function_list(rmi_dev);
1246 err:
1247 return retval < 0 ? retval : 0;
1248 }
1249
1250 static struct rmi_driver rmi_physical_driver = {
1251 .driver = {
1252 .owner = THIS_MODULE,
1253 .name = "rmi4_physical",
1254 .bus = &rmi_bus_type,
1255 .probe = rmi_driver_probe,
1256 .remove = rmi_driver_remove,
1257 },
1258 .reset_handler = rmi_driver_reset_handler,
1259 .clear_irq_bits = rmi_driver_clear_irq_bits,
1260 .set_irq_bits = rmi_driver_set_irq_bits,
1261 .set_input_params = rmi_driver_set_input_params,
1262 };
1263
1264 bool rmi_is_physical_driver(struct device_driver *drv)
1265 {
1266 return drv == &rmi_physical_driver.driver;
1267 }
1268
1269 int __init rmi_register_physical_driver(void)
1270 {
1271 int error;
1272
1273 error = driver_register(&rmi_physical_driver.driver);
1274 if (error) {
1275 pr_err("%s: driver register failed, code=%d.\n", __func__,
1276 error);
1277 return error;
1278 }
1279
1280 return 0;
1281 }
1282
1283 void __exit rmi_unregister_physical_driver(void)
1284 {
1285 driver_unregister(&rmi_physical_driver.driver);
1286 }