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