1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
6 * This driver provides the core support for a single RMI4-based device.
8 * The RMI4 specification can be found here (URL split for line length):
10 * http://www.synaptics.com/sites/default/files/
11 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
14 #include <linux/bitmap.h>
15 #include <linux/delay.h>
17 #include <linux/irq.h>
19 #include <linux/slab.h>
21 #include <linux/irqdomain.h>
22 #include <uapi/linux/input.h>
23 #include <linux/rmi.h>
25 #include "rmi_driver.h"
27 #define HAS_NONSTANDARD_PDT_MASK 0x40
28 #define RMI4_MAX_PAGE 0xff
29 #define RMI4_PAGE_SIZE 0x100
30 #define RMI4_PAGE_MASK 0xFF00
32 #define RMI_DEVICE_RESET_CMD 0x01
33 #define DEFAULT_RESET_DELAY_MS 100
35 void rmi_free_function_list(struct rmi_device
*rmi_dev
)
37 struct rmi_function
*fn
, *tmp
;
38 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
40 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Freeing function list\n");
42 /* Doing it in the reverse order so F01 will be removed last */
43 list_for_each_entry_safe_reverse(fn
, tmp
,
44 &data
->function_list
, node
) {
46 rmi_unregister_function(fn
);
49 devm_kfree(&rmi_dev
->dev
, data
->irq_memory
);
50 data
->irq_memory
= NULL
;
51 data
->irq_status
= NULL
;
52 data
->fn_irq_bits
= NULL
;
53 data
->current_irq_mask
= NULL
;
54 data
->new_irq_mask
= NULL
;
56 data
->f01_container
= NULL
;
57 data
->f34_container
= NULL
;
60 static int reset_one_function(struct rmi_function
*fn
)
62 struct rmi_function_handler
*fh
;
65 if (!fn
|| !fn
->dev
.driver
)
68 fh
= to_rmi_function_handler(fn
->dev
.driver
);
70 retval
= fh
->reset(fn
);
72 dev_err(&fn
->dev
, "Reset failed with code %d.\n",
79 static int configure_one_function(struct rmi_function
*fn
)
81 struct rmi_function_handler
*fh
;
84 if (!fn
|| !fn
->dev
.driver
)
87 fh
= to_rmi_function_handler(fn
->dev
.driver
);
89 retval
= fh
->config(fn
);
91 dev_err(&fn
->dev
, "Config failed with code %d.\n",
98 static int rmi_driver_process_reset_requests(struct rmi_device
*rmi_dev
)
100 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
101 struct rmi_function
*entry
;
104 list_for_each_entry(entry
, &data
->function_list
, node
) {
105 retval
= reset_one_function(entry
);
113 static int rmi_driver_process_config_requests(struct rmi_device
*rmi_dev
)
115 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
116 struct rmi_function
*entry
;
119 list_for_each_entry(entry
, &data
->function_list
, node
) {
120 retval
= configure_one_function(entry
);
128 static int rmi_process_interrupt_requests(struct rmi_device
*rmi_dev
)
130 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
131 struct device
*dev
= &rmi_dev
->dev
;
138 if (!data
->attn_data
.data
) {
139 error
= rmi_read_block(rmi_dev
,
140 data
->f01_container
->fd
.data_base_addr
+ 1,
141 data
->irq_status
, data
->num_of_irq_regs
);
143 dev_err(dev
, "Failed to read irqs, code=%d\n", error
);
148 mutex_lock(&data
->irq_mutex
);
149 bitmap_and(data
->irq_status
, data
->irq_status
, data
->fn_irq_bits
,
152 * At this point, irq_status has all bits that are set in the
153 * interrupt status register and are enabled.
155 mutex_unlock(&data
->irq_mutex
);
157 for_each_set_bit(i
, data
->irq_status
, data
->irq_count
)
158 handle_nested_irq(irq_find_mapping(data
->irqdomain
, i
));
161 input_sync(data
->input
);
166 void rmi_set_attn_data(struct rmi_device
*rmi_dev
, unsigned long irq_status
,
167 void *data
, size_t size
)
169 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
170 struct rmi4_attn_data attn_data
;
173 if (!drvdata
->enabled
)
176 fifo_data
= kmemdup(data
, size
, GFP_ATOMIC
);
180 attn_data
.irq_status
= irq_status
;
181 attn_data
.size
= size
;
182 attn_data
.data
= fifo_data
;
184 kfifo_put(&drvdata
->attn_fifo
, attn_data
);
186 EXPORT_SYMBOL_GPL(rmi_set_attn_data
);
188 static irqreturn_t
rmi_irq_fn(int irq
, void *dev_id
)
190 struct rmi_device
*rmi_dev
= dev_id
;
191 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
192 struct rmi4_attn_data attn_data
= {0};
195 count
= kfifo_get(&drvdata
->attn_fifo
, &attn_data
);
197 *(drvdata
->irq_status
) = attn_data
.irq_status
;
198 drvdata
->attn_data
= attn_data
;
201 ret
= rmi_process_interrupt_requests(rmi_dev
);
203 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
,
204 "Failed to process interrupt request: %d\n", ret
);
207 kfree(attn_data
.data
);
208 drvdata
->attn_data
.data
= NULL
;
211 if (!kfifo_is_empty(&drvdata
->attn_fifo
))
212 return rmi_irq_fn(irq
, dev_id
);
217 static int rmi_irq_init(struct rmi_device
*rmi_dev
)
219 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
220 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
221 int irq_flags
= irq_get_trigger_type(pdata
->irq
);
225 irq_flags
= IRQF_TRIGGER_LOW
;
227 ret
= devm_request_threaded_irq(&rmi_dev
->dev
, pdata
->irq
, NULL
,
228 rmi_irq_fn
, irq_flags
| IRQF_ONESHOT
,
229 dev_driver_string(rmi_dev
->xport
->dev
),
232 dev_err(&rmi_dev
->dev
, "Failed to register interrupt %d\n",
238 data
->enabled
= true;
243 struct rmi_function
*rmi_find_function(struct rmi_device
*rmi_dev
, u8 number
)
245 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
246 struct rmi_function
*entry
;
248 list_for_each_entry(entry
, &data
->function_list
, node
) {
249 if (entry
->fd
.function_number
== number
)
256 static int suspend_one_function(struct rmi_function
*fn
)
258 struct rmi_function_handler
*fh
;
261 if (!fn
|| !fn
->dev
.driver
)
264 fh
= to_rmi_function_handler(fn
->dev
.driver
);
266 retval
= fh
->suspend(fn
);
268 dev_err(&fn
->dev
, "Suspend failed with code %d.\n",
275 static int rmi_suspend_functions(struct rmi_device
*rmi_dev
)
277 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
278 struct rmi_function
*entry
;
281 list_for_each_entry(entry
, &data
->function_list
, node
) {
282 retval
= suspend_one_function(entry
);
290 static int resume_one_function(struct rmi_function
*fn
)
292 struct rmi_function_handler
*fh
;
295 if (!fn
|| !fn
->dev
.driver
)
298 fh
= to_rmi_function_handler(fn
->dev
.driver
);
300 retval
= fh
->resume(fn
);
302 dev_err(&fn
->dev
, "Resume failed with code %d.\n",
309 static int rmi_resume_functions(struct rmi_device
*rmi_dev
)
311 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
312 struct rmi_function
*entry
;
315 list_for_each_entry(entry
, &data
->function_list
, node
) {
316 retval
= resume_one_function(entry
);
324 int rmi_enable_sensor(struct rmi_device
*rmi_dev
)
328 retval
= rmi_driver_process_config_requests(rmi_dev
);
332 return rmi_process_interrupt_requests(rmi_dev
);
336 * rmi_driver_set_input_params - set input device id and other data.
338 * @rmi_dev: Pointer to an RMI device
339 * @input: Pointer to input device
342 static int rmi_driver_set_input_params(struct rmi_device
*rmi_dev
,
343 struct input_dev
*input
)
345 input
->name
= SYNAPTICS_INPUT_DEVICE_NAME
;
346 input
->id
.vendor
= SYNAPTICS_VENDOR_ID
;
347 input
->id
.bustype
= BUS_RMI
;
351 static void rmi_driver_set_input_name(struct rmi_device
*rmi_dev
,
352 struct input_dev
*input
)
354 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
355 const char *device_name
= rmi_f01_get_product_ID(data
->f01_container
);
358 name
= devm_kasprintf(&rmi_dev
->dev
, GFP_KERNEL
,
359 "Synaptics %s", device_name
);
366 static int rmi_driver_set_irq_bits(struct rmi_device
*rmi_dev
,
370 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
371 struct device
*dev
= &rmi_dev
->dev
;
373 mutex_lock(&data
->irq_mutex
);
374 bitmap_or(data
->new_irq_mask
,
375 data
->current_irq_mask
, mask
, data
->irq_count
);
377 error
= rmi_write_block(rmi_dev
,
378 data
->f01_container
->fd
.control_base_addr
+ 1,
379 data
->new_irq_mask
, data
->num_of_irq_regs
);
381 dev_err(dev
, "%s: Failed to change enabled interrupts!",
385 bitmap_copy(data
->current_irq_mask
, data
->new_irq_mask
,
386 data
->num_of_irq_regs
);
388 bitmap_or(data
->fn_irq_bits
, data
->fn_irq_bits
, mask
, data
->irq_count
);
391 mutex_unlock(&data
->irq_mutex
);
395 static int rmi_driver_clear_irq_bits(struct rmi_device
*rmi_dev
,
399 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
400 struct device
*dev
= &rmi_dev
->dev
;
402 mutex_lock(&data
->irq_mutex
);
403 bitmap_andnot(data
->fn_irq_bits
,
404 data
->fn_irq_bits
, mask
, data
->irq_count
);
405 bitmap_andnot(data
->new_irq_mask
,
406 data
->current_irq_mask
, mask
, data
->irq_count
);
408 error
= rmi_write_block(rmi_dev
,
409 data
->f01_container
->fd
.control_base_addr
+ 1,
410 data
->new_irq_mask
, data
->num_of_irq_regs
);
412 dev_err(dev
, "%s: Failed to change enabled interrupts!",
416 bitmap_copy(data
->current_irq_mask
, data
->new_irq_mask
,
417 data
->num_of_irq_regs
);
420 mutex_unlock(&data
->irq_mutex
);
424 static int rmi_driver_reset_handler(struct rmi_device
*rmi_dev
)
426 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
430 * Can get called before the driver is fully ready to deal with
433 if (!data
|| !data
->f01_container
) {
434 dev_warn(&rmi_dev
->dev
,
435 "Not ready to handle reset yet!\n");
439 error
= rmi_read_block(rmi_dev
,
440 data
->f01_container
->fd
.control_base_addr
+ 1,
441 data
->current_irq_mask
, data
->num_of_irq_regs
);
443 dev_err(&rmi_dev
->dev
, "%s: Failed to read current IRQ mask.\n",
448 error
= rmi_driver_process_reset_requests(rmi_dev
);
452 error
= rmi_driver_process_config_requests(rmi_dev
);
459 static int rmi_read_pdt_entry(struct rmi_device
*rmi_dev
,
460 struct pdt_entry
*entry
, u16 pdt_address
)
462 u8 buf
[RMI_PDT_ENTRY_SIZE
];
465 error
= rmi_read_block(rmi_dev
, pdt_address
, buf
, RMI_PDT_ENTRY_SIZE
);
467 dev_err(&rmi_dev
->dev
, "Read PDT entry at %#06x failed, code: %d.\n",
472 entry
->page_start
= pdt_address
& RMI4_PAGE_MASK
;
473 entry
->query_base_addr
= buf
[0];
474 entry
->command_base_addr
= buf
[1];
475 entry
->control_base_addr
= buf
[2];
476 entry
->data_base_addr
= buf
[3];
477 entry
->interrupt_source_count
= buf
[4] & RMI_PDT_INT_SOURCE_COUNT_MASK
;
478 entry
->function_version
= (buf
[4] & RMI_PDT_FUNCTION_VERSION_MASK
) >> 5;
479 entry
->function_number
= buf
[5];
484 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry
*pdt
,
485 struct rmi_function_descriptor
*fd
)
487 fd
->query_base_addr
= pdt
->query_base_addr
+ pdt
->page_start
;
488 fd
->command_base_addr
= pdt
->command_base_addr
+ pdt
->page_start
;
489 fd
->control_base_addr
= pdt
->control_base_addr
+ pdt
->page_start
;
490 fd
->data_base_addr
= pdt
->data_base_addr
+ pdt
->page_start
;
491 fd
->function_number
= pdt
->function_number
;
492 fd
->interrupt_source_count
= pdt
->interrupt_source_count
;
493 fd
->function_version
= pdt
->function_version
;
496 #define RMI_SCAN_CONTINUE 0
497 #define RMI_SCAN_DONE 1
499 static int rmi_scan_pdt_page(struct rmi_device
*rmi_dev
,
503 int (*callback
)(struct rmi_device
*rmi_dev
,
505 const struct pdt_entry
*entry
))
507 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
508 struct pdt_entry pdt_entry
;
509 u16 page_start
= RMI4_PAGE_SIZE
* page
;
510 u16 pdt_start
= page_start
+ PDT_START_SCAN_LOCATION
;
511 u16 pdt_end
= page_start
+ PDT_END_SCAN_LOCATION
;
516 for (addr
= pdt_start
; addr
>= pdt_end
; addr
-= RMI_PDT_ENTRY_SIZE
) {
517 error
= rmi_read_pdt_entry(rmi_dev
, &pdt_entry
, addr
);
521 if (RMI4_END_OF_PDT(pdt_entry
.function_number
))
524 retval
= callback(rmi_dev
, ctx
, &pdt_entry
);
525 if (retval
!= RMI_SCAN_CONTINUE
)
530 * Count number of empty PDT pages. If a gap of two pages
531 * or more is found, stop scanning.
533 if (addr
== pdt_start
)
538 return (data
->bootloader_mode
|| *empty_pages
>= 2) ?
539 RMI_SCAN_DONE
: RMI_SCAN_CONTINUE
;
542 int rmi_scan_pdt(struct rmi_device
*rmi_dev
, void *ctx
,
543 int (*callback
)(struct rmi_device
*rmi_dev
,
544 void *ctx
, const struct pdt_entry
*entry
))
548 int retval
= RMI_SCAN_DONE
;
550 for (page
= 0; page
<= RMI4_MAX_PAGE
; page
++) {
551 retval
= rmi_scan_pdt_page(rmi_dev
, page
, &empty_pages
,
553 if (retval
!= RMI_SCAN_CONTINUE
)
557 return retval
< 0 ? retval
: 0;
560 int rmi_read_register_desc(struct rmi_device
*d
, u16 addr
,
561 struct rmi_register_descriptor
*rdesc
)
564 u8 size_presence_reg
;
566 int presense_offset
= 1;
575 * The first register of the register descriptor is the size of
576 * the register descriptor's presense register.
578 ret
= rmi_read(d
, addr
, &size_presence_reg
);
583 if (size_presence_reg
< 0 || size_presence_reg
> 35)
586 memset(buf
, 0, sizeof(buf
));
589 * The presence register contains the size of the register structure
590 * and a bitmap which identified which packet registers are present
591 * for this particular register type (ie query, control, or data).
593 ret
= rmi_read_block(d
, addr
, buf
, size_presence_reg
);
600 rdesc
->struct_size
= buf
[1] | (buf
[2] << 8);
602 rdesc
->struct_size
= buf
[0];
605 for (i
= presense_offset
; i
< size_presence_reg
; i
++) {
606 for (b
= 0; b
< 8; b
++) {
607 if (buf
[i
] & (0x1 << b
))
608 bitmap_set(rdesc
->presense_map
, map_offset
, 1);
613 rdesc
->num_registers
= bitmap_weight(rdesc
->presense_map
,
614 RMI_REG_DESC_PRESENSE_BITS
);
616 rdesc
->registers
= devm_kcalloc(&d
->dev
,
617 rdesc
->num_registers
,
618 sizeof(struct rmi_register_desc_item
),
620 if (!rdesc
->registers
)
624 * Allocate a temporary buffer to hold the register structure.
625 * I'm not using devm_kzalloc here since it will not be retained
626 * after exiting this function
628 struct_buf
= kzalloc(rdesc
->struct_size
, GFP_KERNEL
);
633 * The register structure contains information about every packet
634 * register of this type. This includes the size of the packet
635 * register and a bitmap of all subpackets contained in the packet
638 ret
= rmi_read_block(d
, addr
, struct_buf
, rdesc
->struct_size
);
640 goto free_struct_buff
;
642 reg
= find_first_bit(rdesc
->presense_map
, RMI_REG_DESC_PRESENSE_BITS
);
643 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
644 struct rmi_register_desc_item
*item
= &rdesc
->registers
[i
];
645 int reg_size
= struct_buf
[offset
];
649 reg_size
= struct_buf
[offset
] |
650 (struct_buf
[offset
+ 1] << 8);
655 reg_size
= struct_buf
[offset
] |
656 (struct_buf
[offset
+ 1] << 8) |
657 (struct_buf
[offset
+ 2] << 16) |
658 (struct_buf
[offset
+ 3] << 24);
663 item
->reg_size
= reg_size
;
668 for (b
= 0; b
< 7; b
++) {
669 if (struct_buf
[offset
] & (0x1 << b
))
670 bitmap_set(item
->subpacket_map
,
674 } while (struct_buf
[offset
++] & 0x80);
676 item
->num_subpackets
= bitmap_weight(item
->subpacket_map
,
677 RMI_REG_DESC_SUBPACKET_BITS
);
679 rmi_dbg(RMI_DEBUG_CORE
, &d
->dev
,
680 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__
,
681 item
->reg
, item
->reg_size
, item
->num_subpackets
);
683 reg
= find_next_bit(rdesc
->presense_map
,
684 RMI_REG_DESC_PRESENSE_BITS
, reg
+ 1);
692 const struct rmi_register_desc_item
*rmi_get_register_desc_item(
693 struct rmi_register_descriptor
*rdesc
, u16 reg
)
695 const struct rmi_register_desc_item
*item
;
698 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
699 item
= &rdesc
->registers
[i
];
700 if (item
->reg
== reg
)
707 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor
*rdesc
)
709 const struct rmi_register_desc_item
*item
;
713 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
714 item
= &rdesc
->registers
[i
];
715 size
+= item
->reg_size
;
720 /* Compute the register offset relative to the base address */
721 int rmi_register_desc_calc_reg_offset(
722 struct rmi_register_descriptor
*rdesc
, u16 reg
)
724 const struct rmi_register_desc_item
*item
;
728 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
729 item
= &rdesc
->registers
[i
];
730 if (item
->reg
== reg
)
737 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item
*item
,
740 return find_next_bit(item
->subpacket_map
, RMI_REG_DESC_PRESENSE_BITS
,
741 subpacket
) == subpacket
;
744 static int rmi_check_bootloader_mode(struct rmi_device
*rmi_dev
,
745 const struct pdt_entry
*pdt
)
747 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
751 if (pdt
->function_number
== 0x34 && pdt
->function_version
> 1) {
752 ret
= rmi_read(rmi_dev
, pdt
->data_base_addr
, &status
);
754 dev_err(&rmi_dev
->dev
,
755 "Failed to read F34 status: %d.\n", ret
);
760 data
->bootloader_mode
= true;
761 } else if (pdt
->function_number
== 0x01) {
762 ret
= rmi_read(rmi_dev
, pdt
->data_base_addr
, &status
);
764 dev_err(&rmi_dev
->dev
,
765 "Failed to read F01 status: %d.\n", ret
);
770 data
->bootloader_mode
= true;
776 static int rmi_count_irqs(struct rmi_device
*rmi_dev
,
777 void *ctx
, const struct pdt_entry
*pdt
)
779 int *irq_count
= ctx
;
782 *irq_count
+= pdt
->interrupt_source_count
;
784 ret
= rmi_check_bootloader_mode(rmi_dev
, pdt
);
788 return RMI_SCAN_CONTINUE
;
791 int rmi_initial_reset(struct rmi_device
*rmi_dev
, void *ctx
,
792 const struct pdt_entry
*pdt
)
796 if (pdt
->function_number
== 0x01) {
797 u16 cmd_addr
= pdt
->page_start
+ pdt
->command_base_addr
;
798 u8 cmd_buf
= RMI_DEVICE_RESET_CMD
;
799 const struct rmi_device_platform_data
*pdata
=
800 rmi_get_platform_data(rmi_dev
);
802 if (rmi_dev
->xport
->ops
->reset
) {
803 error
= rmi_dev
->xport
->ops
->reset(rmi_dev
->xport
,
808 return RMI_SCAN_DONE
;
811 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Sending reset\n");
812 error
= rmi_write_block(rmi_dev
, cmd_addr
, &cmd_buf
, 1);
814 dev_err(&rmi_dev
->dev
,
815 "Initial reset failed. Code = %d.\n", error
);
819 mdelay(pdata
->reset_delay_ms
?: DEFAULT_RESET_DELAY_MS
);
821 return RMI_SCAN_DONE
;
824 /* F01 should always be on page 0. If we don't find it there, fail. */
825 return pdt
->page_start
== 0 ? RMI_SCAN_CONTINUE
: -ENODEV
;
828 static int rmi_create_function(struct rmi_device
*rmi_dev
,
829 void *ctx
, const struct pdt_entry
*pdt
)
831 struct device
*dev
= &rmi_dev
->dev
;
832 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
833 int *current_irq_count
= ctx
;
834 struct rmi_function
*fn
;
838 rmi_dbg(RMI_DEBUG_CORE
, dev
, "Initializing F%02X.\n",
839 pdt
->function_number
);
841 fn
= kzalloc(sizeof(struct rmi_function
) +
842 BITS_TO_LONGS(data
->irq_count
) * sizeof(unsigned long),
845 dev_err(dev
, "Failed to allocate memory for F%02X\n",
846 pdt
->function_number
);
850 INIT_LIST_HEAD(&fn
->node
);
851 rmi_driver_copy_pdt_to_fd(pdt
, &fn
->fd
);
853 fn
->rmi_dev
= rmi_dev
;
855 fn
->num_of_irqs
= pdt
->interrupt_source_count
;
856 fn
->irq_pos
= *current_irq_count
;
857 *current_irq_count
+= fn
->num_of_irqs
;
859 for (i
= 0; i
< fn
->num_of_irqs
; i
++)
860 set_bit(fn
->irq_pos
+ i
, fn
->irq_mask
);
862 error
= rmi_register_function(fn
);
866 if (pdt
->function_number
== 0x01)
867 data
->f01_container
= fn
;
868 else if (pdt
->function_number
== 0x34)
869 data
->f34_container
= fn
;
871 list_add_tail(&fn
->node
, &data
->function_list
);
873 return RMI_SCAN_CONTINUE
;
876 void rmi_enable_irq(struct rmi_device
*rmi_dev
, bool clear_wake
)
878 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
879 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
880 int irq
= pdata
->irq
;
884 mutex_lock(&data
->enabled_mutex
);
890 data
->enabled
= true;
891 if (clear_wake
&& device_may_wakeup(rmi_dev
->xport
->dev
)) {
892 retval
= disable_irq_wake(irq
);
894 dev_warn(&rmi_dev
->dev
,
895 "Failed to disable irq for wake: %d\n",
900 * Call rmi_process_interrupt_requests() after enabling irq,
901 * otherwise we may lose interrupt on edge-triggered systems.
903 irq_flags
= irq_get_trigger_type(pdata
->irq
);
904 if (irq_flags
& IRQ_TYPE_EDGE_BOTH
)
905 rmi_process_interrupt_requests(rmi_dev
);
908 mutex_unlock(&data
->enabled_mutex
);
911 void rmi_disable_irq(struct rmi_device
*rmi_dev
, bool enable_wake
)
913 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
914 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
915 struct rmi4_attn_data attn_data
= {0};
916 int irq
= pdata
->irq
;
919 mutex_lock(&data
->enabled_mutex
);
924 data
->enabled
= false;
926 if (enable_wake
&& device_may_wakeup(rmi_dev
->xport
->dev
)) {
927 retval
= enable_irq_wake(irq
);
929 dev_warn(&rmi_dev
->dev
,
930 "Failed to enable irq for wake: %d\n",
934 /* make sure the fifo is clean */
935 while (!kfifo_is_empty(&data
->attn_fifo
)) {
936 count
= kfifo_get(&data
->attn_fifo
, &attn_data
);
938 kfree(attn_data
.data
);
942 mutex_unlock(&data
->enabled_mutex
);
945 int rmi_driver_suspend(struct rmi_device
*rmi_dev
, bool enable_wake
)
949 retval
= rmi_suspend_functions(rmi_dev
);
951 dev_warn(&rmi_dev
->dev
, "Failed to suspend functions: %d\n",
954 rmi_disable_irq(rmi_dev
, enable_wake
);
957 EXPORT_SYMBOL_GPL(rmi_driver_suspend
);
959 int rmi_driver_resume(struct rmi_device
*rmi_dev
, bool clear_wake
)
963 rmi_enable_irq(rmi_dev
, clear_wake
);
965 retval
= rmi_resume_functions(rmi_dev
);
967 dev_warn(&rmi_dev
->dev
, "Failed to suspend functions: %d\n",
972 EXPORT_SYMBOL_GPL(rmi_driver_resume
);
974 static int rmi_driver_remove(struct device
*dev
)
976 struct rmi_device
*rmi_dev
= to_rmi_device(dev
);
977 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
979 rmi_disable_irq(rmi_dev
, false);
981 irq_domain_remove(data
->irqdomain
);
982 data
->irqdomain
= NULL
;
984 rmi_f34_remove_sysfs(rmi_dev
);
985 rmi_free_function_list(rmi_dev
);
991 static int rmi_driver_of_probe(struct device
*dev
,
992 struct rmi_device_platform_data
*pdata
)
996 retval
= rmi_of_property_read_u32(dev
, &pdata
->reset_delay_ms
,
997 "syna,reset-delay-ms", 1);
1004 static inline int rmi_driver_of_probe(struct device
*dev
,
1005 struct rmi_device_platform_data
*pdata
)
1011 int rmi_probe_interrupts(struct rmi_driver_data
*data
)
1013 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
1014 struct device
*dev
= &rmi_dev
->dev
;
1015 struct fwnode_handle
*fwnode
= rmi_dev
->xport
->dev
->fwnode
;
1021 * We need to count the IRQs and allocate their storage before scanning
1022 * the PDT and creating the function entries, because adding a new
1023 * function can trigger events that result in the IRQ related storage
1026 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Counting IRQs.\n", __func__
);
1027 data
->bootloader_mode
= false;
1029 retval
= rmi_scan_pdt(rmi_dev
, &irq_count
, rmi_count_irqs
);
1031 dev_err(dev
, "IRQ counting failed with code %d.\n", retval
);
1035 if (data
->bootloader_mode
)
1036 dev_warn(dev
, "Device in bootloader mode.\n");
1038 /* Allocate and register a linear revmap irq_domain */
1039 data
->irqdomain
= irq_domain_create_linear(fwnode
, irq_count
,
1040 &irq_domain_simple_ops
,
1042 if (!data
->irqdomain
) {
1043 dev_err(&rmi_dev
->dev
, "Failed to create IRQ domain\n");
1047 data
->irq_count
= irq_count
;
1048 data
->num_of_irq_regs
= (data
->irq_count
+ 7) / 8;
1050 size
= BITS_TO_LONGS(data
->irq_count
) * sizeof(unsigned long);
1051 data
->irq_memory
= devm_kcalloc(dev
, size
, 4, GFP_KERNEL
);
1052 if (!data
->irq_memory
) {
1053 dev_err(dev
, "Failed to allocate memory for irq masks.\n");
1057 data
->irq_status
= data
->irq_memory
+ size
* 0;
1058 data
->fn_irq_bits
= data
->irq_memory
+ size
* 1;
1059 data
->current_irq_mask
= data
->irq_memory
+ size
* 2;
1060 data
->new_irq_mask
= data
->irq_memory
+ size
* 3;
1065 int rmi_init_functions(struct rmi_driver_data
*data
)
1067 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
1068 struct device
*dev
= &rmi_dev
->dev
;
1072 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Creating functions.\n", __func__
);
1073 retval
= rmi_scan_pdt(rmi_dev
, &irq_count
, rmi_create_function
);
1075 dev_err(dev
, "Function creation failed with code %d.\n",
1077 goto err_destroy_functions
;
1080 if (!data
->f01_container
) {
1081 dev_err(dev
, "Missing F01 container!\n");
1083 goto err_destroy_functions
;
1086 retval
= rmi_read_block(rmi_dev
,
1087 data
->f01_container
->fd
.control_base_addr
+ 1,
1088 data
->current_irq_mask
, data
->num_of_irq_regs
);
1090 dev_err(dev
, "%s: Failed to read current IRQ mask.\n",
1092 goto err_destroy_functions
;
1097 err_destroy_functions
:
1098 rmi_free_function_list(rmi_dev
);
1102 static int rmi_driver_probe(struct device
*dev
)
1104 struct rmi_driver
*rmi_driver
;
1105 struct rmi_driver_data
*data
;
1106 struct rmi_device_platform_data
*pdata
;
1107 struct rmi_device
*rmi_dev
;
1110 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Starting probe.\n",
1113 if (!rmi_is_physical_device(dev
)) {
1114 rmi_dbg(RMI_DEBUG_CORE
, dev
, "Not a physical device.\n");
1118 rmi_dev
= to_rmi_device(dev
);
1119 rmi_driver
= to_rmi_driver(dev
->driver
);
1120 rmi_dev
->driver
= rmi_driver
;
1122 pdata
= rmi_get_platform_data(rmi_dev
);
1124 if (rmi_dev
->xport
->dev
->of_node
) {
1125 retval
= rmi_driver_of_probe(rmi_dev
->xport
->dev
, pdata
);
1130 data
= devm_kzalloc(dev
, sizeof(struct rmi_driver_data
), GFP_KERNEL
);
1134 INIT_LIST_HEAD(&data
->function_list
);
1135 data
->rmi_dev
= rmi_dev
;
1136 dev_set_drvdata(&rmi_dev
->dev
, data
);
1139 * Right before a warm boot, the sensor might be in some unusual state,
1140 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1141 * or configuration update. In order to clear the sensor to a known
1142 * state and/or apply any updates, we issue a initial reset to clear any
1143 * previous settings and force it into normal operation.
1145 * We have to do this before actually building the PDT because
1146 * the reflash updates (if any) might cause various registers to move
1149 * For a number of reasons, this initial reset may fail to return
1150 * within the specified time, but we'll still be able to bring up the
1151 * driver normally after that failure. This occurs most commonly in
1152 * a cold boot situation (where then firmware takes longer to come up
1153 * than from a warm boot) and the reset_delay_ms in the platform data
1154 * has been set too short to accommodate that. Since the sensor will
1155 * eventually come up and be usable, we don't want to just fail here
1156 * and leave the customer's device unusable. So we warn them, and
1157 * continue processing.
1159 retval
= rmi_scan_pdt(rmi_dev
, NULL
, rmi_initial_reset
);
1161 dev_warn(dev
, "RMI initial reset failed! Continuing in spite of this.\n");
1163 retval
= rmi_read(rmi_dev
, PDT_PROPERTIES_LOCATION
, &data
->pdt_props
);
1166 * we'll print out a warning and continue since
1167 * failure to get the PDT properties is not a cause to fail
1169 dev_warn(dev
, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1170 PDT_PROPERTIES_LOCATION
, retval
);
1173 mutex_init(&data
->irq_mutex
);
1174 mutex_init(&data
->enabled_mutex
);
1176 retval
= rmi_probe_interrupts(data
);
1180 if (rmi_dev
->xport
->input
) {
1182 * The transport driver already has an input device.
1183 * In some cases it is preferable to reuse the transport
1184 * devices input device instead of creating a new one here.
1185 * One example is some HID touchpads report "pass-through"
1186 * button events are not reported by rmi registers.
1188 data
->input
= rmi_dev
->xport
->input
;
1190 data
->input
= devm_input_allocate_device(dev
);
1192 dev_err(dev
, "%s: Failed to allocate input device.\n",
1197 rmi_driver_set_input_params(rmi_dev
, data
->input
);
1198 data
->input
->phys
= devm_kasprintf(dev
, GFP_KERNEL
,
1199 "%s/input0", dev_name(dev
));
1202 retval
= rmi_init_functions(data
);
1206 retval
= rmi_f34_create_sysfs(rmi_dev
);
1211 rmi_driver_set_input_name(rmi_dev
, data
->input
);
1212 if (!rmi_dev
->xport
->input
) {
1213 retval
= input_register_device(data
->input
);
1215 dev_err(dev
, "%s: Failed to register input device.\n",
1217 goto err_destroy_functions
;
1222 retval
= rmi_irq_init(rmi_dev
);
1224 goto err_destroy_functions
;
1226 if (data
->f01_container
->dev
.driver
) {
1227 /* Driver already bound, so enable ATTN now. */
1228 retval
= rmi_enable_sensor(rmi_dev
);
1230 goto err_disable_irq
;
1236 rmi_disable_irq(rmi_dev
, false);
1237 err_destroy_functions
:
1238 rmi_free_function_list(rmi_dev
);
1243 static struct rmi_driver rmi_physical_driver
= {
1245 .owner
= THIS_MODULE
,
1246 .name
= "rmi4_physical",
1247 .bus
= &rmi_bus_type
,
1248 .probe
= rmi_driver_probe
,
1249 .remove
= rmi_driver_remove
,
1251 .reset_handler
= rmi_driver_reset_handler
,
1252 .clear_irq_bits
= rmi_driver_clear_irq_bits
,
1253 .set_irq_bits
= rmi_driver_set_irq_bits
,
1254 .set_input_params
= rmi_driver_set_input_params
,
1257 bool rmi_is_physical_driver(struct device_driver
*drv
)
1259 return drv
== &rmi_physical_driver
.driver
;
1262 int __init
rmi_register_physical_driver(void)
1266 error
= driver_register(&rmi_physical_driver
.driver
);
1268 pr_err("%s: driver register failed, code=%d.\n", __func__
,
1276 void __exit
rmi_unregister_physical_driver(void)
1278 driver_unregister(&rmi_physical_driver
.driver
);