1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020 Synaptics Incorporated
6 #include <linux/kernel.h>
8 #include <linux/input.h>
9 #include <linux/slab.h>
10 #include "rmi_driver.h"
12 #define RMI_F3A_MAX_GPIO_COUNT 128
13 #define RMI_F3A_MAX_REG_SIZE DIV_ROUND_UP(RMI_F3A_MAX_GPIO_COUNT, 8)
15 /* Defs for Query 0 */
16 #define RMI_F3A_GPIO_COUNT 0x7F
18 #define RMI_F3A_DATA_REGS_MAX_SIZE RMI_F3A_MAX_REG_SIZE
20 #define TRACKSTICK_RANGE_START 3
21 #define TRACKSTICK_RANGE_END 6
29 u8 data_regs
[RMI_F3A_DATA_REGS_MAX_SIZE
];
32 struct input_dev
*input
;
34 struct rmi_function
*f03
;
35 bool trackstick_buttons
;
38 static void rmi_f3a_report_button(struct rmi_function
*fn
,
39 struct f3a_data
*f3a
, unsigned int button
)
41 u16 key_code
= f3a
->gpio_key_map
[button
];
42 bool key_down
= !(f3a
->data_regs
[0] & BIT(button
));
44 if (f3a
->trackstick_buttons
&&
45 button
>= TRACKSTICK_RANGE_START
&&
46 button
<= TRACKSTICK_RANGE_END
) {
47 rmi_f03_overwrite_button(f3a
->f03
, key_code
, key_down
);
49 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
50 "%s: call input report key (0x%04x) value (0x%02x)",
51 __func__
, key_code
, key_down
);
52 input_report_key(f3a
->input
, key_code
, key_down
);
56 static irqreturn_t
rmi_f3a_attention(int irq
, void *ctx
)
58 struct rmi_function
*fn
= ctx
;
59 struct f3a_data
*f3a
= dev_get_drvdata(&fn
->dev
);
60 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&fn
->rmi_dev
->dev
);
64 if (drvdata
->attn_data
.data
) {
65 if (drvdata
->attn_data
.size
< f3a
->register_count
) {
67 "F3A interrupted, but data is missing\n");
70 memcpy(f3a
->data_regs
, drvdata
->attn_data
.data
,
72 drvdata
->attn_data
.data
+= f3a
->register_count
;
73 drvdata
->attn_data
.size
-= f3a
->register_count
;
75 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.data_base_addr
,
76 f3a
->data_regs
, f3a
->register_count
);
79 "%s: Failed to read F3a data registers: %d\n",
81 return IRQ_RETVAL(error
);
85 for (i
= 0; i
< f3a
->gpio_count
; i
++)
86 if (f3a
->gpio_key_map
[i
] != KEY_RESERVED
)
87 rmi_f3a_report_button(fn
, f3a
, i
);
88 if (f3a
->trackstick_buttons
)
89 rmi_f03_commit_buttons(f3a
->f03
);
94 static int rmi_f3a_config(struct rmi_function
*fn
)
96 struct f3a_data
*f3a
= dev_get_drvdata(&fn
->dev
);
97 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
98 const struct rmi_device_platform_data
*pdata
=
99 rmi_get_platform_data(fn
->rmi_dev
);
104 if (pdata
->gpio_data
.trackstick_buttons
) {
105 /* Try [re-]establish link to F03. */
106 f3a
->f03
= rmi_find_function(fn
->rmi_dev
, 0x03);
107 f3a
->trackstick_buttons
= f3a
->f03
!= NULL
;
110 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
115 static bool rmi_f3a_is_valid_button(int button
, struct f3a_data
*f3a
,
116 u8
*query1_regs
, u8
*ctrl1_regs
)
118 /* gpio exist && direction input */
119 return (query1_regs
[0] & BIT(button
)) && !(ctrl1_regs
[0] & BIT(button
));
122 static int rmi_f3a_map_gpios(struct rmi_function
*fn
, struct f3a_data
*f3a
,
123 u8
*query1_regs
, u8
*ctrl1_regs
)
125 const struct rmi_device_platform_data
*pdata
=
126 rmi_get_platform_data(fn
->rmi_dev
);
127 struct input_dev
*input
= f3a
->input
;
128 unsigned int button
= BTN_LEFT
;
129 unsigned int trackstick_button
= BTN_LEFT
;
130 bool button_mapped
= false;
132 int button_count
= min_t(u8
, f3a
->gpio_count
, TRACKSTICK_RANGE_END
);
134 f3a
->gpio_key_map
= devm_kcalloc(&fn
->dev
,
136 sizeof(f3a
->gpio_key_map
[0]),
138 if (!f3a
->gpio_key_map
) {
139 dev_err(&fn
->dev
, "Failed to allocate gpio map memory.\n");
143 for (i
= 0; i
< button_count
; i
++) {
144 if (!rmi_f3a_is_valid_button(i
, f3a
, query1_regs
, ctrl1_regs
))
147 if (pdata
->gpio_data
.trackstick_buttons
&&
148 i
>= TRACKSTICK_RANGE_START
&&
149 i
< TRACKSTICK_RANGE_END
) {
150 f3a
->gpio_key_map
[i
] = trackstick_button
++;
151 } else if (!pdata
->gpio_data
.buttonpad
|| !button_mapped
) {
152 f3a
->gpio_key_map
[i
] = button
;
153 input_set_capability(input
, EV_KEY
, button
++);
154 button_mapped
= true;
157 input
->keycode
= f3a
->gpio_key_map
;
158 input
->keycodesize
= sizeof(f3a
->gpio_key_map
[0]);
159 input
->keycodemax
= f3a
->gpio_count
;
161 if (pdata
->gpio_data
.buttonpad
|| (button
- BTN_LEFT
== 1))
162 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
167 static int rmi_f3a_initialize(struct rmi_function
*fn
, struct f3a_data
*f3a
)
169 u8 query1
[RMI_F3A_MAX_REG_SIZE
];
170 u8 ctrl1
[RMI_F3A_MAX_REG_SIZE
];
174 error
= rmi_read(fn
->rmi_dev
, fn
->fd
.query_base_addr
, &buf
);
176 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
181 f3a
->gpio_count
= buf
& RMI_F3A_GPIO_COUNT
;
182 f3a
->register_count
= DIV_ROUND_UP(f3a
->gpio_count
, 8);
184 /* Query1 -> gpio exist */
185 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
+ 1,
186 query1
, f3a
->register_count
);
188 dev_err(&fn
->dev
, "Failed to read query1 register\n");
192 /* Ctrl1 -> gpio direction */
193 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
+ 1,
194 ctrl1
, f3a
->register_count
);
196 dev_err(&fn
->dev
, "Failed to read control1 register\n");
200 error
= rmi_f3a_map_gpios(fn
, f3a
, query1
, ctrl1
);
207 static int rmi_f3a_probe(struct rmi_function
*fn
)
209 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
210 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
211 struct f3a_data
*f3a
;
214 if (!drv_data
->input
) {
215 dev_info(&fn
->dev
, "F3A: no input device found, ignoring\n");
219 f3a
= devm_kzalloc(&fn
->dev
, sizeof(*f3a
), GFP_KERNEL
);
223 f3a
->input
= drv_data
->input
;
225 error
= rmi_f3a_initialize(fn
, f3a
);
229 dev_set_drvdata(&fn
->dev
, f3a
);
233 struct rmi_function_handler rmi_f3a_handler
= {
238 .probe
= rmi_f3a_probe
,
239 .config
= rmi_f3a_config
,
240 .attention
= rmi_f3a_attention
,