2 * Copyright (c) 2012-2016 Synaptics Incorporated
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/rmi.h>
11 #include <linux/input.h>
12 #include <linux/slab.h>
13 #include "rmi_driver.h"
15 #define RMI_F30_QUERY_SIZE 2
17 /* Defs for Query 0 */
18 #define RMI_F30_EXTENDED_PATTERNS 0x01
19 #define RMI_F30_HAS_MAPPABLE_BUTTONS BIT(1)
20 #define RMI_F30_HAS_LED BIT(2)
21 #define RMI_F30_HAS_GPIO BIT(3)
22 #define RMI_F30_HAS_HAPTIC BIT(4)
23 #define RMI_F30_HAS_GPIO_DRV_CTL BIT(5)
24 #define RMI_F30_HAS_MECH_MOUSE_BTNS BIT(6)
26 /* Defs for Query 1 */
27 #define RMI_F30_GPIO_LED_COUNT 0x1F
29 /* Defs for Control Registers */
30 #define RMI_F30_CTRL_1_GPIO_DEBOUNCE 0x01
31 #define RMI_F30_CTRL_1_HALT BIT(4)
32 #define RMI_F30_CTRL_1_HALTED BIT(5)
33 #define RMI_F30_CTRL_10_NUM_MECH_MOUSE_BTNS 0x03
35 #define RMI_F30_CTRL_MAX_REGS 32
36 #define RMI_F30_CTRL_MAX_BYTES DIV_ROUND_UP(RMI_F30_CTRL_MAX_REGS, 8)
37 #define RMI_F30_CTRL_MAX_REG_BLOCKS 11
39 #define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \
41 + RMI_F30_CTRL_MAX_BYTES \
42 + RMI_F30_CTRL_MAX_BYTES \
43 + RMI_F30_CTRL_MAX_BYTES \
45 + RMI_F30_CTRL_MAX_REGS \
46 + RMI_F30_CTRL_MAX_REGS \
47 + RMI_F30_CTRL_MAX_BYTES \
51 #define TRACKSTICK_RANGE_START 3
52 #define TRACKSTICK_RANGE_END 6
54 struct rmi_f30_ctrl_data
{
62 bool has_extended_pattern
;
63 bool has_mappable_buttons
;
67 bool has_gpio_driver_control
;
68 bool has_mech_mouse_btns
;
73 /* Control Register Data */
74 struct rmi_f30_ctrl_data ctrl
[RMI_F30_CTRL_MAX_REG_BLOCKS
];
75 u8 ctrl_regs
[RMI_F30_CTRL_REGS_MAX_SIZE
];
78 u8 data_regs
[RMI_F30_CTRL_MAX_BYTES
];
81 struct input_dev
*input
;
83 struct rmi_function
*f03
;
84 bool trackstick_buttons
;
87 static int rmi_f30_read_control_parameters(struct rmi_function
*fn
,
92 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
93 f30
->ctrl_regs
, f30
->ctrl_regs_size
);
96 "%s: Could not read control registers at 0x%x: %d\n",
97 __func__
, fn
->fd
.control_base_addr
, error
);
104 static void rmi_f30_report_button(struct rmi_function
*fn
,
105 struct f30_data
*f30
, unsigned int button
)
107 unsigned int reg_num
= button
>> 3;
108 unsigned int bit_num
= button
& 0x07;
109 u16 key_code
= f30
->gpioled_key_map
[button
];
110 bool key_down
= !(f30
->data_regs
[reg_num
] & BIT(bit_num
));
112 if (f30
->trackstick_buttons
&&
113 button
>= TRACKSTICK_RANGE_START
&&
114 button
<= TRACKSTICK_RANGE_END
) {
115 rmi_f03_overwrite_button(f30
->f03
, key_code
, key_down
);
117 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
118 "%s: call input report key (0x%04x) value (0x%02x)",
119 __func__
, key_code
, key_down
);
121 input_report_key(f30
->input
, key_code
, key_down
);
125 static irqreturn_t
rmi_f30_attention(int irq
, void *ctx
)
127 struct rmi_function
*fn
= ctx
;
128 struct f30_data
*f30
= dev_get_drvdata(&fn
->dev
);
129 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&fn
->rmi_dev
->dev
);
133 /* Read the gpi led data. */
134 if (drvdata
->attn_data
.data
) {
135 if (drvdata
->attn_data
.size
< f30
->register_count
) {
137 "F30 interrupted, but data is missing\n");
140 memcpy(f30
->data_regs
, drvdata
->attn_data
.data
,
141 f30
->register_count
);
142 drvdata
->attn_data
.data
+= f30
->register_count
;
143 drvdata
->attn_data
.size
-= f30
->register_count
;
145 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.data_base_addr
,
146 f30
->data_regs
, f30
->register_count
);
149 "%s: Failed to read F30 data registers: %d\n",
151 return IRQ_RETVAL(error
);
156 for (i
= 0; i
< f30
->gpioled_count
; i
++)
157 if (f30
->gpioled_key_map
[i
] != KEY_RESERVED
)
158 rmi_f30_report_button(fn
, f30
, i
);
159 if (f30
->trackstick_buttons
)
160 rmi_f03_commit_buttons(f30
->f03
);
166 static int rmi_f30_config(struct rmi_function
*fn
)
168 struct f30_data
*f30
= dev_get_drvdata(&fn
->dev
);
169 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
170 const struct rmi_device_platform_data
*pdata
=
171 rmi_get_platform_data(fn
->rmi_dev
);
174 /* can happen if f30_data.disable is set */
178 if (pdata
->f30_data
.trackstick_buttons
) {
179 /* Try [re-]establish link to F03. */
180 f30
->f03
= rmi_find_function(fn
->rmi_dev
, 0x03);
181 f30
->trackstick_buttons
= f30
->f03
!= NULL
;
184 if (pdata
->f30_data
.disable
) {
185 drv
->clear_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
187 /* Write Control Register values back to device */
188 error
= rmi_write_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
189 f30
->ctrl_regs
, f30
->ctrl_regs_size
);
192 "%s: Could not write control registers at 0x%x: %d\n",
193 __func__
, fn
->fd
.control_base_addr
, error
);
197 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
203 static void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data
*ctrl
,
204 int *ctrl_addr
, int len
, u8
**reg
)
206 ctrl
->address
= *ctrl_addr
;
213 static bool rmi_f30_is_valid_button(int button
, struct rmi_f30_ctrl_data
*ctrl
)
215 int byte_position
= button
>> 3;
216 int bit_position
= button
& 0x07;
219 * ctrl2 -> dir == 0 -> input mode
220 * ctrl3 -> data == 1 -> actual button
222 return !(ctrl
[2].regs
[byte_position
] & BIT(bit_position
)) &&
223 (ctrl
[3].regs
[byte_position
] & BIT(bit_position
));
226 static int rmi_f30_map_gpios(struct rmi_function
*fn
,
227 struct f30_data
*f30
)
229 const struct rmi_device_platform_data
*pdata
=
230 rmi_get_platform_data(fn
->rmi_dev
);
231 struct input_dev
*input
= f30
->input
;
232 unsigned int button
= BTN_LEFT
;
233 unsigned int trackstick_button
= BTN_LEFT
;
234 bool button_mapped
= false;
236 int button_count
= min_t(u8
, f30
->gpioled_count
, TRACKSTICK_RANGE_END
);
238 f30
->gpioled_key_map
= devm_kcalloc(&fn
->dev
,
240 sizeof(f30
->gpioled_key_map
[0]),
242 if (!f30
->gpioled_key_map
) {
243 dev_err(&fn
->dev
, "Failed to allocate gpioled map memory.\n");
247 for (i
= 0; i
< button_count
; i
++) {
248 if (!rmi_f30_is_valid_button(i
, f30
->ctrl
))
251 if (pdata
->f30_data
.trackstick_buttons
&&
252 i
>= TRACKSTICK_RANGE_START
&& i
< TRACKSTICK_RANGE_END
) {
253 f30
->gpioled_key_map
[i
] = trackstick_button
++;
254 } else if (!pdata
->f30_data
.buttonpad
|| !button_mapped
) {
255 f30
->gpioled_key_map
[i
] = button
;
256 input_set_capability(input
, EV_KEY
, button
++);
257 button_mapped
= true;
261 input
->keycode
= f30
->gpioled_key_map
;
262 input
->keycodesize
= sizeof(f30
->gpioled_key_map
[0]);
263 input
->keycodemax
= f30
->gpioled_count
;
266 * Buttonpad could be also inferred from f30->has_mech_mouse_btns,
267 * but I am not sure, so use only the pdata info and the number of
270 if (pdata
->f30_data
.buttonpad
|| (button
- BTN_LEFT
== 1))
271 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
276 static int rmi_f30_initialize(struct rmi_function
*fn
, struct f30_data
*f30
)
278 u8
*ctrl_reg
= f30
->ctrl_regs
;
279 int control_address
= fn
->fd
.control_base_addr
;
280 u8 buf
[RMI_F30_QUERY_SIZE
];
283 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
284 buf
, RMI_F30_QUERY_SIZE
);
286 dev_err(&fn
->dev
, "Failed to read query register\n");
290 f30
->has_extended_pattern
= buf
[0] & RMI_F30_EXTENDED_PATTERNS
;
291 f30
->has_mappable_buttons
= buf
[0] & RMI_F30_HAS_MAPPABLE_BUTTONS
;
292 f30
->has_led
= buf
[0] & RMI_F30_HAS_LED
;
293 f30
->has_gpio
= buf
[0] & RMI_F30_HAS_GPIO
;
294 f30
->has_haptic
= buf
[0] & RMI_F30_HAS_HAPTIC
;
295 f30
->has_gpio_driver_control
= buf
[0] & RMI_F30_HAS_GPIO_DRV_CTL
;
296 f30
->has_mech_mouse_btns
= buf
[0] & RMI_F30_HAS_MECH_MOUSE_BTNS
;
297 f30
->gpioled_count
= buf
[1] & RMI_F30_GPIO_LED_COUNT
;
299 f30
->register_count
= DIV_ROUND_UP(f30
->gpioled_count
, 8);
301 if (f30
->has_gpio
&& f30
->has_led
)
302 rmi_f30_set_ctrl_data(&f30
->ctrl
[0], &control_address
,
303 f30
->register_count
, &ctrl_reg
);
305 rmi_f30_set_ctrl_data(&f30
->ctrl
[1], &control_address
,
306 sizeof(u8
), &ctrl_reg
);
309 rmi_f30_set_ctrl_data(&f30
->ctrl
[2], &control_address
,
310 f30
->register_count
, &ctrl_reg
);
312 rmi_f30_set_ctrl_data(&f30
->ctrl
[3], &control_address
,
313 f30
->register_count
, &ctrl_reg
);
317 rmi_f30_set_ctrl_data(&f30
->ctrl
[4], &control_address
,
318 f30
->register_count
, &ctrl_reg
);
320 rmi_f30_set_ctrl_data(&f30
->ctrl
[5], &control_address
,
321 f30
->has_extended_pattern
? 6 : 2,
325 if (f30
->has_led
|| f30
->has_gpio_driver_control
) {
326 /* control 6 uses a byte per gpio/led */
327 rmi_f30_set_ctrl_data(&f30
->ctrl
[6], &control_address
,
328 f30
->gpioled_count
, &ctrl_reg
);
331 if (f30
->has_mappable_buttons
) {
332 /* control 7 uses a byte per gpio/led */
333 rmi_f30_set_ctrl_data(&f30
->ctrl
[7], &control_address
,
334 f30
->gpioled_count
, &ctrl_reg
);
337 if (f30
->has_haptic
) {
338 rmi_f30_set_ctrl_data(&f30
->ctrl
[8], &control_address
,
339 f30
->register_count
, &ctrl_reg
);
341 rmi_f30_set_ctrl_data(&f30
->ctrl
[9], &control_address
,
342 sizeof(u8
), &ctrl_reg
);
345 if (f30
->has_mech_mouse_btns
)
346 rmi_f30_set_ctrl_data(&f30
->ctrl
[10], &control_address
,
347 sizeof(u8
), &ctrl_reg
);
349 f30
->ctrl_regs_size
= ctrl_reg
-
350 f30
->ctrl_regs
?: RMI_F30_CTRL_REGS_MAX_SIZE
;
352 error
= rmi_f30_read_control_parameters(fn
, f30
);
355 "Failed to initialize F30 control params: %d\n",
361 error
= rmi_f30_map_gpios(fn
, f30
);
369 static int rmi_f30_probe(struct rmi_function
*fn
)
371 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
372 const struct rmi_device_platform_data
*pdata
=
373 rmi_get_platform_data(rmi_dev
);
374 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
375 struct f30_data
*f30
;
378 if (pdata
->f30_data
.disable
)
381 if (!drv_data
->input
) {
382 dev_info(&fn
->dev
, "F30: no input device found, ignoring\n");
386 f30
= devm_kzalloc(&fn
->dev
, sizeof(*f30
), GFP_KERNEL
);
390 f30
->input
= drv_data
->input
;
392 error
= rmi_f30_initialize(fn
, f30
);
396 dev_set_drvdata(&fn
->dev
, f30
);
400 struct rmi_function_handler rmi_f30_handler
= {
405 .probe
= rmi_f30_probe
,
406 .config
= rmi_f30_config
,
407 .attention
= rmi_f30_attention
,