2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
5 * This driver provides the core support for a single RMI4-based device.
7 * The RMI4 specification can be found here (URL split for line length):
9 * http://www.synaptics.com/sites/default/files/
10 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
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.
17 #include <linux/bitmap.h>
18 #include <linux/delay.h>
20 #include <linux/irq.h>
22 #include <linux/slab.h>
24 #include <linux/irqdomain.h>
25 #include <uapi/linux/input.h>
26 #include <linux/rmi.h>
28 #include "rmi_driver.h"
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
35 #define RMI_DEVICE_RESET_CMD 0x01
36 #define DEFAULT_RESET_DELAY_MS 100
38 void rmi_free_function_list(struct rmi_device
*rmi_dev
)
40 struct rmi_function
*fn
, *tmp
;
41 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
43 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Freeing function list\n");
45 /* Doing it in the reverse order so F01 will be removed last */
46 list_for_each_entry_safe_reverse(fn
, tmp
,
47 &data
->function_list
, node
) {
49 rmi_unregister_function(fn
);
52 devm_kfree(&rmi_dev
->dev
, data
->irq_memory
);
53 data
->irq_memory
= NULL
;
54 data
->irq_status
= NULL
;
55 data
->fn_irq_bits
= NULL
;
56 data
->current_irq_mask
= NULL
;
57 data
->new_irq_mask
= NULL
;
59 data
->f01_container
= NULL
;
60 data
->f34_container
= NULL
;
63 static int reset_one_function(struct rmi_function
*fn
)
65 struct rmi_function_handler
*fh
;
68 if (!fn
|| !fn
->dev
.driver
)
71 fh
= to_rmi_function_handler(fn
->dev
.driver
);
73 retval
= fh
->reset(fn
);
75 dev_err(&fn
->dev
, "Reset failed with code %d.\n",
82 static int configure_one_function(struct rmi_function
*fn
)
84 struct rmi_function_handler
*fh
;
87 if (!fn
|| !fn
->dev
.driver
)
90 fh
= to_rmi_function_handler(fn
->dev
.driver
);
92 retval
= fh
->config(fn
);
94 dev_err(&fn
->dev
, "Config failed with code %d.\n",
101 static int rmi_driver_process_reset_requests(struct rmi_device
*rmi_dev
)
103 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
104 struct rmi_function
*entry
;
107 list_for_each_entry(entry
, &data
->function_list
, node
) {
108 retval
= reset_one_function(entry
);
116 static int rmi_driver_process_config_requests(struct rmi_device
*rmi_dev
)
118 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
119 struct rmi_function
*entry
;
122 list_for_each_entry(entry
, &data
->function_list
, node
) {
123 retval
= configure_one_function(entry
);
131 static int rmi_process_interrupt_requests(struct rmi_device
*rmi_dev
)
133 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
134 struct device
*dev
= &rmi_dev
->dev
;
141 if (!data
->attn_data
.data
) {
142 error
= rmi_read_block(rmi_dev
,
143 data
->f01_container
->fd
.data_base_addr
+ 1,
144 data
->irq_status
, data
->num_of_irq_regs
);
146 dev_err(dev
, "Failed to read irqs, code=%d\n", error
);
151 mutex_lock(&data
->irq_mutex
);
152 bitmap_and(data
->irq_status
, data
->irq_status
, data
->fn_irq_bits
,
155 * At this point, irq_status has all bits that are set in the
156 * interrupt status register and are enabled.
158 mutex_unlock(&data
->irq_mutex
);
160 for_each_set_bit(i
, data
->irq_status
, data
->irq_count
)
161 handle_nested_irq(irq_find_mapping(data
->irqdomain
, i
));
164 input_sync(data
->input
);
169 void rmi_set_attn_data(struct rmi_device
*rmi_dev
, unsigned long irq_status
,
170 void *data
, size_t size
)
172 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
173 struct rmi4_attn_data attn_data
;
176 if (!drvdata
->enabled
)
179 fifo_data
= kmemdup(data
, size
, GFP_ATOMIC
);
183 attn_data
.irq_status
= irq_status
;
184 attn_data
.size
= size
;
185 attn_data
.data
= fifo_data
;
187 kfifo_put(&drvdata
->attn_fifo
, attn_data
);
189 EXPORT_SYMBOL_GPL(rmi_set_attn_data
);
191 static irqreturn_t
rmi_irq_fn(int irq
, void *dev_id
)
193 struct rmi_device
*rmi_dev
= dev_id
;
194 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
195 struct rmi4_attn_data attn_data
= {0};
198 count
= kfifo_get(&drvdata
->attn_fifo
, &attn_data
);
200 *(drvdata
->irq_status
) = attn_data
.irq_status
;
201 drvdata
->attn_data
= attn_data
;
204 ret
= rmi_process_interrupt_requests(rmi_dev
);
206 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
,
207 "Failed to process interrupt request: %d\n", ret
);
210 kfree(attn_data
.data
);
211 drvdata
->attn_data
.data
= NULL
;
214 if (!kfifo_is_empty(&drvdata
->attn_fifo
))
215 return rmi_irq_fn(irq
, dev_id
);
220 static int rmi_irq_init(struct rmi_device
*rmi_dev
)
222 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
223 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
224 int irq_flags
= irq_get_trigger_type(pdata
->irq
);
228 irq_flags
= IRQF_TRIGGER_LOW
;
230 ret
= devm_request_threaded_irq(&rmi_dev
->dev
, pdata
->irq
, NULL
,
231 rmi_irq_fn
, irq_flags
| IRQF_ONESHOT
,
232 dev_driver_string(rmi_dev
->xport
->dev
),
235 dev_err(&rmi_dev
->dev
, "Failed to register interrupt %d\n",
241 data
->enabled
= true;
246 struct rmi_function
*rmi_find_function(struct rmi_device
*rmi_dev
, u8 number
)
248 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
249 struct rmi_function
*entry
;
251 list_for_each_entry(entry
, &data
->function_list
, node
) {
252 if (entry
->fd
.function_number
== number
)
259 static int suspend_one_function(struct rmi_function
*fn
)
261 struct rmi_function_handler
*fh
;
264 if (!fn
|| !fn
->dev
.driver
)
267 fh
= to_rmi_function_handler(fn
->dev
.driver
);
269 retval
= fh
->suspend(fn
);
271 dev_err(&fn
->dev
, "Suspend failed with code %d.\n",
278 static int rmi_suspend_functions(struct rmi_device
*rmi_dev
)
280 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
281 struct rmi_function
*entry
;
284 list_for_each_entry(entry
, &data
->function_list
, node
) {
285 retval
= suspend_one_function(entry
);
293 static int resume_one_function(struct rmi_function
*fn
)
295 struct rmi_function_handler
*fh
;
298 if (!fn
|| !fn
->dev
.driver
)
301 fh
= to_rmi_function_handler(fn
->dev
.driver
);
303 retval
= fh
->resume(fn
);
305 dev_err(&fn
->dev
, "Resume failed with code %d.\n",
312 static int rmi_resume_functions(struct rmi_device
*rmi_dev
)
314 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
315 struct rmi_function
*entry
;
318 list_for_each_entry(entry
, &data
->function_list
, node
) {
319 retval
= resume_one_function(entry
);
327 int rmi_enable_sensor(struct rmi_device
*rmi_dev
)
331 retval
= rmi_driver_process_config_requests(rmi_dev
);
335 return rmi_process_interrupt_requests(rmi_dev
);
339 * rmi_driver_set_input_params - set input device id and other data.
341 * @rmi_dev: Pointer to an RMI device
342 * @input: Pointer to input device
345 static int rmi_driver_set_input_params(struct rmi_device
*rmi_dev
,
346 struct input_dev
*input
)
348 input
->name
= SYNAPTICS_INPUT_DEVICE_NAME
;
349 input
->id
.vendor
= SYNAPTICS_VENDOR_ID
;
350 input
->id
.bustype
= BUS_RMI
;
354 static void rmi_driver_set_input_name(struct rmi_device
*rmi_dev
,
355 struct input_dev
*input
)
357 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
358 const char *device_name
= rmi_f01_get_product_ID(data
->f01_container
);
361 name
= devm_kasprintf(&rmi_dev
->dev
, GFP_KERNEL
,
362 "Synaptics %s", device_name
);
369 static int rmi_driver_set_irq_bits(struct rmi_device
*rmi_dev
,
373 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
374 struct device
*dev
= &rmi_dev
->dev
;
376 mutex_lock(&data
->irq_mutex
);
377 bitmap_or(data
->new_irq_mask
,
378 data
->current_irq_mask
, mask
, data
->irq_count
);
380 error
= rmi_write_block(rmi_dev
,
381 data
->f01_container
->fd
.control_base_addr
+ 1,
382 data
->new_irq_mask
, data
->num_of_irq_regs
);
384 dev_err(dev
, "%s: Failed to change enabled interrupts!",
388 bitmap_copy(data
->current_irq_mask
, data
->new_irq_mask
,
389 data
->num_of_irq_regs
);
391 bitmap_or(data
->fn_irq_bits
, data
->fn_irq_bits
, mask
, data
->irq_count
);
394 mutex_unlock(&data
->irq_mutex
);
398 static int rmi_driver_clear_irq_bits(struct rmi_device
*rmi_dev
,
402 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
403 struct device
*dev
= &rmi_dev
->dev
;
405 mutex_lock(&data
->irq_mutex
);
406 bitmap_andnot(data
->fn_irq_bits
,
407 data
->fn_irq_bits
, mask
, data
->irq_count
);
408 bitmap_andnot(data
->new_irq_mask
,
409 data
->current_irq_mask
, mask
, data
->irq_count
);
411 error
= rmi_write_block(rmi_dev
,
412 data
->f01_container
->fd
.control_base_addr
+ 1,
413 data
->new_irq_mask
, data
->num_of_irq_regs
);
415 dev_err(dev
, "%s: Failed to change enabled interrupts!",
419 bitmap_copy(data
->current_irq_mask
, data
->new_irq_mask
,
420 data
->num_of_irq_regs
);
423 mutex_unlock(&data
->irq_mutex
);
427 static int rmi_driver_reset_handler(struct rmi_device
*rmi_dev
)
429 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
433 * Can get called before the driver is fully ready to deal with
436 if (!data
|| !data
->f01_container
) {
437 dev_warn(&rmi_dev
->dev
,
438 "Not ready to handle reset yet!\n");
442 error
= rmi_read_block(rmi_dev
,
443 data
->f01_container
->fd
.control_base_addr
+ 1,
444 data
->current_irq_mask
, data
->num_of_irq_regs
);
446 dev_err(&rmi_dev
->dev
, "%s: Failed to read current IRQ mask.\n",
451 error
= rmi_driver_process_reset_requests(rmi_dev
);
455 error
= rmi_driver_process_config_requests(rmi_dev
);
462 static int rmi_read_pdt_entry(struct rmi_device
*rmi_dev
,
463 struct pdt_entry
*entry
, u16 pdt_address
)
465 u8 buf
[RMI_PDT_ENTRY_SIZE
];
468 error
= rmi_read_block(rmi_dev
, pdt_address
, buf
, RMI_PDT_ENTRY_SIZE
);
470 dev_err(&rmi_dev
->dev
, "Read PDT entry at %#06x failed, code: %d.\n",
475 entry
->page_start
= pdt_address
& RMI4_PAGE_MASK
;
476 entry
->query_base_addr
= buf
[0];
477 entry
->command_base_addr
= buf
[1];
478 entry
->control_base_addr
= buf
[2];
479 entry
->data_base_addr
= buf
[3];
480 entry
->interrupt_source_count
= buf
[4] & RMI_PDT_INT_SOURCE_COUNT_MASK
;
481 entry
->function_version
= (buf
[4] & RMI_PDT_FUNCTION_VERSION_MASK
) >> 5;
482 entry
->function_number
= buf
[5];
487 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry
*pdt
,
488 struct rmi_function_descriptor
*fd
)
490 fd
->query_base_addr
= pdt
->query_base_addr
+ pdt
->page_start
;
491 fd
->command_base_addr
= pdt
->command_base_addr
+ pdt
->page_start
;
492 fd
->control_base_addr
= pdt
->control_base_addr
+ pdt
->page_start
;
493 fd
->data_base_addr
= pdt
->data_base_addr
+ pdt
->page_start
;
494 fd
->function_number
= pdt
->function_number
;
495 fd
->interrupt_source_count
= pdt
->interrupt_source_count
;
496 fd
->function_version
= pdt
->function_version
;
499 #define RMI_SCAN_CONTINUE 0
500 #define RMI_SCAN_DONE 1
502 static int rmi_scan_pdt_page(struct rmi_device
*rmi_dev
,
506 int (*callback
)(struct rmi_device
*rmi_dev
,
508 const struct pdt_entry
*entry
))
510 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
511 struct pdt_entry pdt_entry
;
512 u16 page_start
= RMI4_PAGE_SIZE
* page
;
513 u16 pdt_start
= page_start
+ PDT_START_SCAN_LOCATION
;
514 u16 pdt_end
= page_start
+ PDT_END_SCAN_LOCATION
;
519 for (addr
= pdt_start
; addr
>= pdt_end
; addr
-= RMI_PDT_ENTRY_SIZE
) {
520 error
= rmi_read_pdt_entry(rmi_dev
, &pdt_entry
, addr
);
524 if (RMI4_END_OF_PDT(pdt_entry
.function_number
))
527 retval
= callback(rmi_dev
, ctx
, &pdt_entry
);
528 if (retval
!= RMI_SCAN_CONTINUE
)
533 * Count number of empty PDT pages. If a gap of two pages
534 * or more is found, stop scanning.
536 if (addr
== pdt_start
)
541 return (data
->bootloader_mode
|| *empty_pages
>= 2) ?
542 RMI_SCAN_DONE
: RMI_SCAN_CONTINUE
;
545 int rmi_scan_pdt(struct rmi_device
*rmi_dev
, void *ctx
,
546 int (*callback
)(struct rmi_device
*rmi_dev
,
547 void *ctx
, const struct pdt_entry
*entry
))
551 int retval
= RMI_SCAN_DONE
;
553 for (page
= 0; page
<= RMI4_MAX_PAGE
; page
++) {
554 retval
= rmi_scan_pdt_page(rmi_dev
, page
, &empty_pages
,
556 if (retval
!= RMI_SCAN_CONTINUE
)
560 return retval
< 0 ? retval
: 0;
563 int rmi_read_register_desc(struct rmi_device
*d
, u16 addr
,
564 struct rmi_register_descriptor
*rdesc
)
567 u8 size_presence_reg
;
569 int presense_offset
= 1;
578 * The first register of the register descriptor is the size of
579 * the register descriptor's presense register.
581 ret
= rmi_read(d
, addr
, &size_presence_reg
);
586 if (size_presence_reg
< 0 || size_presence_reg
> 35)
589 memset(buf
, 0, sizeof(buf
));
592 * The presence register contains the size of the register structure
593 * and a bitmap which identified which packet registers are present
594 * for this particular register type (ie query, control, or data).
596 ret
= rmi_read_block(d
, addr
, buf
, size_presence_reg
);
603 rdesc
->struct_size
= buf
[1] | (buf
[2] << 8);
605 rdesc
->struct_size
= buf
[0];
608 for (i
= presense_offset
; i
< size_presence_reg
; i
++) {
609 for (b
= 0; b
< 8; b
++) {
610 if (buf
[i
] & (0x1 << b
))
611 bitmap_set(rdesc
->presense_map
, map_offset
, 1);
616 rdesc
->num_registers
= bitmap_weight(rdesc
->presense_map
,
617 RMI_REG_DESC_PRESENSE_BITS
);
619 rdesc
->registers
= devm_kcalloc(&d
->dev
,
620 rdesc
->num_registers
,
621 sizeof(struct rmi_register_desc_item
),
623 if (!rdesc
->registers
)
627 * Allocate a temporary buffer to hold the register structure.
628 * I'm not using devm_kzalloc here since it will not be retained
629 * after exiting this function
631 struct_buf
= kzalloc(rdesc
->struct_size
, GFP_KERNEL
);
636 * The register structure contains information about every packet
637 * register of this type. This includes the size of the packet
638 * register and a bitmap of all subpackets contained in the packet
641 ret
= rmi_read_block(d
, addr
, struct_buf
, rdesc
->struct_size
);
643 goto free_struct_buff
;
645 reg
= find_first_bit(rdesc
->presense_map
, RMI_REG_DESC_PRESENSE_BITS
);
646 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
647 struct rmi_register_desc_item
*item
= &rdesc
->registers
[i
];
648 int reg_size
= struct_buf
[offset
];
652 reg_size
= struct_buf
[offset
] |
653 (struct_buf
[offset
+ 1] << 8);
658 reg_size
= struct_buf
[offset
] |
659 (struct_buf
[offset
+ 1] << 8) |
660 (struct_buf
[offset
+ 2] << 16) |
661 (struct_buf
[offset
+ 3] << 24);
666 item
->reg_size
= reg_size
;
671 for (b
= 0; b
< 7; b
++) {
672 if (struct_buf
[offset
] & (0x1 << b
))
673 bitmap_set(item
->subpacket_map
,
677 } while (struct_buf
[offset
++] & 0x80);
679 item
->num_subpackets
= bitmap_weight(item
->subpacket_map
,
680 RMI_REG_DESC_SUBPACKET_BITS
);
682 rmi_dbg(RMI_DEBUG_CORE
, &d
->dev
,
683 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__
,
684 item
->reg
, item
->reg_size
, item
->num_subpackets
);
686 reg
= find_next_bit(rdesc
->presense_map
,
687 RMI_REG_DESC_PRESENSE_BITS
, reg
+ 1);
695 const struct rmi_register_desc_item
*rmi_get_register_desc_item(
696 struct rmi_register_descriptor
*rdesc
, u16 reg
)
698 const struct rmi_register_desc_item
*item
;
701 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
702 item
= &rdesc
->registers
[i
];
703 if (item
->reg
== reg
)
710 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor
*rdesc
)
712 const struct rmi_register_desc_item
*item
;
716 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
717 item
= &rdesc
->registers
[i
];
718 size
+= item
->reg_size
;
723 /* Compute the register offset relative to the base address */
724 int rmi_register_desc_calc_reg_offset(
725 struct rmi_register_descriptor
*rdesc
, u16 reg
)
727 const struct rmi_register_desc_item
*item
;
731 for (i
= 0; i
< rdesc
->num_registers
; i
++) {
732 item
= &rdesc
->registers
[i
];
733 if (item
->reg
== reg
)
740 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item
*item
,
743 return find_next_bit(item
->subpacket_map
, RMI_REG_DESC_PRESENSE_BITS
,
744 subpacket
) == subpacket
;
747 static int rmi_check_bootloader_mode(struct rmi_device
*rmi_dev
,
748 const struct pdt_entry
*pdt
)
750 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
754 if (pdt
->function_number
== 0x34 && pdt
->function_version
> 1) {
755 ret
= rmi_read(rmi_dev
, pdt
->data_base_addr
, &status
);
757 dev_err(&rmi_dev
->dev
,
758 "Failed to read F34 status: %d.\n", ret
);
763 data
->bootloader_mode
= true;
764 } else if (pdt
->function_number
== 0x01) {
765 ret
= rmi_read(rmi_dev
, pdt
->data_base_addr
, &status
);
767 dev_err(&rmi_dev
->dev
,
768 "Failed to read F01 status: %d.\n", ret
);
773 data
->bootloader_mode
= true;
779 static int rmi_count_irqs(struct rmi_device
*rmi_dev
,
780 void *ctx
, const struct pdt_entry
*pdt
)
782 int *irq_count
= ctx
;
785 *irq_count
+= pdt
->interrupt_source_count
;
787 ret
= rmi_check_bootloader_mode(rmi_dev
, pdt
);
791 return RMI_SCAN_CONTINUE
;
794 int rmi_initial_reset(struct rmi_device
*rmi_dev
, void *ctx
,
795 const struct pdt_entry
*pdt
)
799 if (pdt
->function_number
== 0x01) {
800 u16 cmd_addr
= pdt
->page_start
+ pdt
->command_base_addr
;
801 u8 cmd_buf
= RMI_DEVICE_RESET_CMD
;
802 const struct rmi_device_platform_data
*pdata
=
803 rmi_get_platform_data(rmi_dev
);
805 if (rmi_dev
->xport
->ops
->reset
) {
806 error
= rmi_dev
->xport
->ops
->reset(rmi_dev
->xport
,
811 return RMI_SCAN_DONE
;
814 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Sending reset\n");
815 error
= rmi_write_block(rmi_dev
, cmd_addr
, &cmd_buf
, 1);
817 dev_err(&rmi_dev
->dev
,
818 "Initial reset failed. Code = %d.\n", error
);
822 mdelay(pdata
->reset_delay_ms
?: DEFAULT_RESET_DELAY_MS
);
824 return RMI_SCAN_DONE
;
827 /* F01 should always be on page 0. If we don't find it there, fail. */
828 return pdt
->page_start
== 0 ? RMI_SCAN_CONTINUE
: -ENODEV
;
831 static int rmi_create_function(struct rmi_device
*rmi_dev
,
832 void *ctx
, const struct pdt_entry
*pdt
)
834 struct device
*dev
= &rmi_dev
->dev
;
835 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
836 int *current_irq_count
= ctx
;
837 struct rmi_function
*fn
;
841 rmi_dbg(RMI_DEBUG_CORE
, dev
, "Initializing F%02X.\n",
842 pdt
->function_number
);
844 fn
= kzalloc(sizeof(struct rmi_function
) +
845 BITS_TO_LONGS(data
->irq_count
) * sizeof(unsigned long),
848 dev_err(dev
, "Failed to allocate memory for F%02X\n",
849 pdt
->function_number
);
853 INIT_LIST_HEAD(&fn
->node
);
854 rmi_driver_copy_pdt_to_fd(pdt
, &fn
->fd
);
856 fn
->rmi_dev
= rmi_dev
;
858 fn
->num_of_irqs
= pdt
->interrupt_source_count
;
859 fn
->irq_pos
= *current_irq_count
;
860 *current_irq_count
+= fn
->num_of_irqs
;
862 for (i
= 0; i
< fn
->num_of_irqs
; i
++)
863 set_bit(fn
->irq_pos
+ i
, fn
->irq_mask
);
865 error
= rmi_register_function(fn
);
869 if (pdt
->function_number
== 0x01)
870 data
->f01_container
= fn
;
871 else if (pdt
->function_number
== 0x34)
872 data
->f34_container
= fn
;
874 list_add_tail(&fn
->node
, &data
->function_list
);
876 return RMI_SCAN_CONTINUE
;
879 void rmi_enable_irq(struct rmi_device
*rmi_dev
, bool clear_wake
)
881 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
882 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
883 int irq
= pdata
->irq
;
887 mutex_lock(&data
->enabled_mutex
);
893 data
->enabled
= true;
894 if (clear_wake
&& device_may_wakeup(rmi_dev
->xport
->dev
)) {
895 retval
= disable_irq_wake(irq
);
897 dev_warn(&rmi_dev
->dev
,
898 "Failed to disable irq for wake: %d\n",
903 * Call rmi_process_interrupt_requests() after enabling irq,
904 * otherwise we may lose interrupt on edge-triggered systems.
906 irq_flags
= irq_get_trigger_type(pdata
->irq
);
907 if (irq_flags
& IRQ_TYPE_EDGE_BOTH
)
908 rmi_process_interrupt_requests(rmi_dev
);
911 mutex_unlock(&data
->enabled_mutex
);
914 void rmi_disable_irq(struct rmi_device
*rmi_dev
, bool enable_wake
)
916 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
917 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
918 struct rmi4_attn_data attn_data
= {0};
919 int irq
= pdata
->irq
;
922 mutex_lock(&data
->enabled_mutex
);
927 data
->enabled
= false;
929 if (enable_wake
&& device_may_wakeup(rmi_dev
->xport
->dev
)) {
930 retval
= enable_irq_wake(irq
);
932 dev_warn(&rmi_dev
->dev
,
933 "Failed to enable irq for wake: %d\n",
937 /* make sure the fifo is clean */
938 while (!kfifo_is_empty(&data
->attn_fifo
)) {
939 count
= kfifo_get(&data
->attn_fifo
, &attn_data
);
941 kfree(attn_data
.data
);
945 mutex_unlock(&data
->enabled_mutex
);
948 int rmi_driver_suspend(struct rmi_device
*rmi_dev
, bool enable_wake
)
952 retval
= rmi_suspend_functions(rmi_dev
);
954 dev_warn(&rmi_dev
->dev
, "Failed to suspend functions: %d\n",
957 rmi_disable_irq(rmi_dev
, enable_wake
);
960 EXPORT_SYMBOL_GPL(rmi_driver_suspend
);
962 int rmi_driver_resume(struct rmi_device
*rmi_dev
, bool clear_wake
)
966 rmi_enable_irq(rmi_dev
, clear_wake
);
968 retval
= rmi_resume_functions(rmi_dev
);
970 dev_warn(&rmi_dev
->dev
, "Failed to suspend functions: %d\n",
975 EXPORT_SYMBOL_GPL(rmi_driver_resume
);
977 static int rmi_driver_remove(struct device
*dev
)
979 struct rmi_device
*rmi_dev
= to_rmi_device(dev
);
980 struct rmi_driver_data
*data
= dev_get_drvdata(&rmi_dev
->dev
);
982 rmi_disable_irq(rmi_dev
, false);
984 irq_domain_remove(data
->irqdomain
);
985 data
->irqdomain
= NULL
;
987 rmi_f34_remove_sysfs(rmi_dev
);
988 rmi_free_function_list(rmi_dev
);
994 static int rmi_driver_of_probe(struct device
*dev
,
995 struct rmi_device_platform_data
*pdata
)
999 retval
= rmi_of_property_read_u32(dev
, &pdata
->reset_delay_ms
,
1000 "syna,reset-delay-ms", 1);
1007 static inline int rmi_driver_of_probe(struct device
*dev
,
1008 struct rmi_device_platform_data
*pdata
)
1014 int rmi_probe_interrupts(struct rmi_driver_data
*data
)
1016 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
1017 struct device
*dev
= &rmi_dev
->dev
;
1018 struct fwnode_handle
*fwnode
= rmi_dev
->xport
->dev
->fwnode
;
1024 * We need to count the IRQs and allocate their storage before scanning
1025 * the PDT and creating the function entries, because adding a new
1026 * function can trigger events that result in the IRQ related storage
1029 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Counting IRQs.\n", __func__
);
1030 data
->bootloader_mode
= false;
1032 retval
= rmi_scan_pdt(rmi_dev
, &irq_count
, rmi_count_irqs
);
1034 dev_err(dev
, "IRQ counting failed with code %d.\n", retval
);
1038 if (data
->bootloader_mode
)
1039 dev_warn(dev
, "Device in bootloader mode.\n");
1041 /* Allocate and register a linear revmap irq_domain */
1042 data
->irqdomain
= irq_domain_create_linear(fwnode
, irq_count
,
1043 &irq_domain_simple_ops
,
1045 if (!data
->irqdomain
) {
1046 dev_err(&rmi_dev
->dev
, "Failed to create IRQ domain\n");
1050 data
->irq_count
= irq_count
;
1051 data
->num_of_irq_regs
= (data
->irq_count
+ 7) / 8;
1053 size
= BITS_TO_LONGS(data
->irq_count
) * sizeof(unsigned long);
1054 data
->irq_memory
= devm_kcalloc(dev
, size
, 4, GFP_KERNEL
);
1055 if (!data
->irq_memory
) {
1056 dev_err(dev
, "Failed to allocate memory for irq masks.\n");
1060 data
->irq_status
= data
->irq_memory
+ size
* 0;
1061 data
->fn_irq_bits
= data
->irq_memory
+ size
* 1;
1062 data
->current_irq_mask
= data
->irq_memory
+ size
* 2;
1063 data
->new_irq_mask
= data
->irq_memory
+ size
* 3;
1068 int rmi_init_functions(struct rmi_driver_data
*data
)
1070 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
1071 struct device
*dev
= &rmi_dev
->dev
;
1075 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Creating functions.\n", __func__
);
1076 retval
= rmi_scan_pdt(rmi_dev
, &irq_count
, rmi_create_function
);
1078 dev_err(dev
, "Function creation failed with code %d.\n",
1080 goto err_destroy_functions
;
1083 if (!data
->f01_container
) {
1084 dev_err(dev
, "Missing F01 container!\n");
1086 goto err_destroy_functions
;
1089 retval
= rmi_read_block(rmi_dev
,
1090 data
->f01_container
->fd
.control_base_addr
+ 1,
1091 data
->current_irq_mask
, data
->num_of_irq_regs
);
1093 dev_err(dev
, "%s: Failed to read current IRQ mask.\n",
1095 goto err_destroy_functions
;
1100 err_destroy_functions
:
1101 rmi_free_function_list(rmi_dev
);
1105 static int rmi_driver_probe(struct device
*dev
)
1107 struct rmi_driver
*rmi_driver
;
1108 struct rmi_driver_data
*data
;
1109 struct rmi_device_platform_data
*pdata
;
1110 struct rmi_device
*rmi_dev
;
1113 rmi_dbg(RMI_DEBUG_CORE
, dev
, "%s: Starting probe.\n",
1116 if (!rmi_is_physical_device(dev
)) {
1117 rmi_dbg(RMI_DEBUG_CORE
, dev
, "Not a physical device.\n");
1121 rmi_dev
= to_rmi_device(dev
);
1122 rmi_driver
= to_rmi_driver(dev
->driver
);
1123 rmi_dev
->driver
= rmi_driver
;
1125 pdata
= rmi_get_platform_data(rmi_dev
);
1127 if (rmi_dev
->xport
->dev
->of_node
) {
1128 retval
= rmi_driver_of_probe(rmi_dev
->xport
->dev
, pdata
);
1133 data
= devm_kzalloc(dev
, sizeof(struct rmi_driver_data
), GFP_KERNEL
);
1137 INIT_LIST_HEAD(&data
->function_list
);
1138 data
->rmi_dev
= rmi_dev
;
1139 dev_set_drvdata(&rmi_dev
->dev
, data
);
1142 * Right before a warm boot, the sensor might be in some unusual state,
1143 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1144 * or configuration update. In order to clear the sensor to a known
1145 * state and/or apply any updates, we issue a initial reset to clear any
1146 * previous settings and force it into normal operation.
1148 * We have to do this before actually building the PDT because
1149 * the reflash updates (if any) might cause various registers to move
1152 * For a number of reasons, this initial reset may fail to return
1153 * within the specified time, but we'll still be able to bring up the
1154 * driver normally after that failure. This occurs most commonly in
1155 * a cold boot situation (where then firmware takes longer to come up
1156 * than from a warm boot) and the reset_delay_ms in the platform data
1157 * has been set too short to accommodate that. Since the sensor will
1158 * eventually come up and be usable, we don't want to just fail here
1159 * and leave the customer's device unusable. So we warn them, and
1160 * continue processing.
1162 retval
= rmi_scan_pdt(rmi_dev
, NULL
, rmi_initial_reset
);
1164 dev_warn(dev
, "RMI initial reset failed! Continuing in spite of this.\n");
1166 retval
= rmi_read(rmi_dev
, PDT_PROPERTIES_LOCATION
, &data
->pdt_props
);
1169 * we'll print out a warning and continue since
1170 * failure to get the PDT properties is not a cause to fail
1172 dev_warn(dev
, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1173 PDT_PROPERTIES_LOCATION
, retval
);
1176 mutex_init(&data
->irq_mutex
);
1177 mutex_init(&data
->enabled_mutex
);
1179 retval
= rmi_probe_interrupts(data
);
1183 if (rmi_dev
->xport
->input
) {
1185 * The transport driver already has an input device.
1186 * In some cases it is preferable to reuse the transport
1187 * devices input device instead of creating a new one here.
1188 * One example is some HID touchpads report "pass-through"
1189 * button events are not reported by rmi registers.
1191 data
->input
= rmi_dev
->xport
->input
;
1193 data
->input
= devm_input_allocate_device(dev
);
1195 dev_err(dev
, "%s: Failed to allocate input device.\n",
1200 rmi_driver_set_input_params(rmi_dev
, data
->input
);
1201 data
->input
->phys
= devm_kasprintf(dev
, GFP_KERNEL
,
1202 "%s/input0", dev_name(dev
));
1205 retval
= rmi_init_functions(data
);
1209 retval
= rmi_f34_create_sysfs(rmi_dev
);
1214 rmi_driver_set_input_name(rmi_dev
, data
->input
);
1215 if (!rmi_dev
->xport
->input
) {
1216 retval
= input_register_device(data
->input
);
1218 dev_err(dev
, "%s: Failed to register input device.\n",
1220 goto err_destroy_functions
;
1225 retval
= rmi_irq_init(rmi_dev
);
1227 goto err_destroy_functions
;
1229 if (data
->f01_container
->dev
.driver
) {
1230 /* Driver already bound, so enable ATTN now. */
1231 retval
= rmi_enable_sensor(rmi_dev
);
1233 goto err_disable_irq
;
1239 rmi_disable_irq(rmi_dev
, false);
1240 err_destroy_functions
:
1241 rmi_free_function_list(rmi_dev
);
1246 static struct rmi_driver rmi_physical_driver
= {
1248 .owner
= THIS_MODULE
,
1249 .name
= "rmi4_physical",
1250 .bus
= &rmi_bus_type
,
1251 .probe
= rmi_driver_probe
,
1252 .remove
= rmi_driver_remove
,
1254 .reset_handler
= rmi_driver_reset_handler
,
1255 .clear_irq_bits
= rmi_driver_clear_irq_bits
,
1256 .set_irq_bits
= rmi_driver_set_irq_bits
,
1257 .set_input_params
= rmi_driver_set_input_params
,
1260 bool rmi_is_physical_driver(struct device_driver
*drv
)
1262 return drv
== &rmi_physical_driver
.driver
;
1265 int __init
rmi_register_physical_driver(void)
1269 error
= driver_register(&rmi_physical_driver
.driver
);
1271 pr_err("%s: driver register failed, code=%d.\n", __func__
,
1279 void __exit
rmi_unregister_physical_driver(void)
1281 driver_unregister(&rmi_physical_driver
.driver
);