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 (1 << 1)
20 #define RMI_F30_HAS_LED (1 << 2)
21 #define RMI_F30_HAS_GPIO (1 << 3)
22 #define RMI_F30_HAS_HAPTIC (1 << 4)
23 #define RMI_F30_HAS_GPIO_DRV_CTL (1 << 5)
24 #define RMI_F30_HAS_MECH_MOUSE_BTNS (1 << 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 (1 << 4)
32 #define RMI_F30_CTRL_1_HALTED (1 << 5)
33 #define RMI_F30_CTRL_10_NUM_MECH_MOUSE_BTNS 0x03
35 struct rmi_f30_ctrl_data
{
41 #define RMI_F30_CTRL_MAX_REGS 32
42 #define RMI_F30_CTRL_MAX_BYTES ((RMI_F30_CTRL_MAX_REGS + 7) >> 3)
43 #define RMI_F30_CTRL_MAX_REG_BLOCKS 11
45 #define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \
47 + RMI_F30_CTRL_MAX_BYTES \
48 + RMI_F30_CTRL_MAX_BYTES \
49 + RMI_F30_CTRL_MAX_BYTES \
51 + RMI_F30_CTRL_MAX_REGS \
52 + RMI_F30_CTRL_MAX_REGS \
53 + RMI_F30_CTRL_MAX_BYTES \
59 bool has_extended_pattern
;
60 bool has_mappable_buttons
;
64 bool has_gpio_driver_control
;
65 bool has_mech_mouse_btns
;
70 /* Control Register Data */
71 struct rmi_f30_ctrl_data ctrl
[RMI_F30_CTRL_MAX_REG_BLOCKS
];
72 u8 ctrl_regs
[RMI_F30_CTRL_REGS_MAX_SIZE
];
75 u8 data_regs
[RMI_F30_CTRL_MAX_BYTES
];
78 struct input_dev
*input
;
81 static int rmi_f30_read_control_parameters(struct rmi_function
*fn
,
84 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
87 error
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
,
88 f30
->ctrl_regs
, f30
->ctrl_regs_size
);
90 dev_err(&rmi_dev
->dev
, "%s : Could not read control registers at 0x%x error (%d)\n",
91 __func__
, fn
->fd
.control_base_addr
, error
);
98 static int rmi_f30_attention(struct rmi_function
*fn
, unsigned long *irq_bits
)
100 struct f30_data
*f30
= dev_get_drvdata(&fn
->dev
);
101 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
111 /* Read the gpi led data. */
112 if (rmi_dev
->xport
->attn_data
) {
113 memcpy(f30
->data_regs
, rmi_dev
->xport
->attn_data
,
114 f30
->register_count
);
115 rmi_dev
->xport
->attn_data
+= f30
->register_count
;
116 rmi_dev
->xport
->attn_size
-= f30
->register_count
;
118 retval
= rmi_read_block(rmi_dev
, fn
->fd
.data_base_addr
,
119 f30
->data_regs
, f30
->register_count
);
122 dev_err(&fn
->dev
, "%s: Failed to read F30 data registers.\n",
128 for (reg_num
= 0; reg_num
< f30
->register_count
; ++reg_num
) {
129 for (i
= 0; gpiled
< f30
->gpioled_count
&& i
< 8; ++i
,
131 if (f30
->gpioled_key_map
[gpiled
] != 0) {
132 /* buttons have pull up resistors */
133 value
= (((f30
->data_regs
[reg_num
] >> i
) & 0x01)
136 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
137 "%s: call input report key (0x%04x) value (0x%02x)",
139 f30
->gpioled_key_map
[gpiled
], value
);
140 input_report_key(f30
->input
,
141 f30
->gpioled_key_map
[gpiled
],
151 static int rmi_f30_register_device(struct rmi_function
*fn
)
154 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
155 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
156 struct f30_data
*f30
= dev_get_drvdata(&fn
->dev
);
157 struct input_dev
*input_dev
;
158 int button_count
= 0;
160 input_dev
= drv_data
->input
;
162 dev_info(&fn
->dev
, "F30: no input device found, ignoring.\n");
166 f30
->input
= input_dev
;
168 set_bit(EV_KEY
, input_dev
->evbit
);
170 input_dev
->keycode
= f30
->gpioled_key_map
;
171 input_dev
->keycodesize
= sizeof(u16
);
172 input_dev
->keycodemax
= f30
->gpioled_count
;
174 for (i
= 0; i
< f30
->gpioled_count
; i
++) {
175 if (f30
->gpioled_key_map
[i
] != 0) {
176 input_set_capability(input_dev
, EV_KEY
,
177 f30
->gpioled_key_map
[i
]);
182 if (button_count
== 1)
183 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
187 static int rmi_f30_config(struct rmi_function
*fn
)
189 struct f30_data
*f30
= dev_get_drvdata(&fn
->dev
);
190 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
191 const struct rmi_device_platform_data
*pdata
=
192 rmi_get_platform_data(fn
->rmi_dev
);
195 if (pdata
->f30_data
&& pdata
->f30_data
->disable
) {
196 drv
->clear_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
198 /* Write Control Register values back to device */
199 error
= rmi_write_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
200 f30
->ctrl_regs
, f30
->ctrl_regs_size
);
202 dev_err(&fn
->rmi_dev
->dev
,
203 "%s : Could not write control registers at 0x%x error (%d)\n",
204 __func__
, fn
->fd
.control_base_addr
, error
);
208 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
213 static inline void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data
*ctrl
,
214 int *ctrl_addr
, int len
, u8
**reg
)
216 ctrl
->address
= *ctrl_addr
;
223 static inline bool rmi_f30_is_valid_button(int button
,
224 struct rmi_f30_ctrl_data
*ctrl
)
226 int byte_position
= button
>> 3;
227 int bit_position
= button
& 0x07;
230 * ctrl2 -> dir == 0 -> input mode
231 * ctrl3 -> data == 1 -> actual button
233 return !(ctrl
[2].regs
[byte_position
] & BIT(bit_position
)) &&
234 (ctrl
[3].regs
[byte_position
] & BIT(bit_position
));
237 static inline int rmi_f30_initialize(struct rmi_function
*fn
)
239 struct f30_data
*f30
;
240 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
241 const struct rmi_device_platform_data
*pdata
;
246 u8 buf
[RMI_F30_QUERY_SIZE
];
250 f30
= devm_kzalloc(&fn
->dev
, sizeof(struct f30_data
),
255 dev_set_drvdata(&fn
->dev
, f30
);
257 retval
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
, buf
,
261 dev_err(&fn
->dev
, "Failed to read query register.\n");
265 f30
->has_extended_pattern
= buf
[0] & RMI_F30_EXTENDED_PATTERNS
;
266 f30
->has_mappable_buttons
= buf
[0] & RMI_F30_HAS_MAPPABLE_BUTTONS
;
267 f30
->has_led
= buf
[0] & RMI_F30_HAS_LED
;
268 f30
->has_gpio
= buf
[0] & RMI_F30_HAS_GPIO
;
269 f30
->has_haptic
= buf
[0] & RMI_F30_HAS_HAPTIC
;
270 f30
->has_gpio_driver_control
= buf
[0] & RMI_F30_HAS_GPIO_DRV_CTL
;
271 f30
->has_mech_mouse_btns
= buf
[0] & RMI_F30_HAS_MECH_MOUSE_BTNS
;
272 f30
->gpioled_count
= buf
[1] & RMI_F30_GPIO_LED_COUNT
;
274 f30
->register_count
= (f30
->gpioled_count
+ 7) >> 3;
276 control_address
= fn
->fd
.control_base_addr
;
277 ctrl_reg
= f30
->ctrl_regs
;
279 if (f30
->has_gpio
&& f30
->has_led
)
280 rmi_f30_set_ctrl_data(&f30
->ctrl
[0], &control_address
,
281 f30
->register_count
, &ctrl_reg
);
283 rmi_f30_set_ctrl_data(&f30
->ctrl
[1], &control_address
, sizeof(u8
),
287 rmi_f30_set_ctrl_data(&f30
->ctrl
[2], &control_address
,
288 f30
->register_count
, &ctrl_reg
);
290 rmi_f30_set_ctrl_data(&f30
->ctrl
[3], &control_address
,
291 f30
->register_count
, &ctrl_reg
);
297 rmi_f30_set_ctrl_data(&f30
->ctrl
[4], &control_address
,
298 f30
->register_count
, &ctrl_reg
);
300 if (f30
->has_extended_pattern
)
305 rmi_f30_set_ctrl_data(&f30
->ctrl
[5], &control_address
,
306 ctrl5_len
, &ctrl_reg
);
309 if (f30
->has_led
|| f30
->has_gpio_driver_control
) {
310 /* control 6 uses a byte per gpio/led */
311 rmi_f30_set_ctrl_data(&f30
->ctrl
[6], &control_address
,
312 f30
->gpioled_count
, &ctrl_reg
);
315 if (f30
->has_mappable_buttons
) {
316 /* control 7 uses a byte per gpio/led */
317 rmi_f30_set_ctrl_data(&f30
->ctrl
[7], &control_address
,
318 f30
->gpioled_count
, &ctrl_reg
);
321 if (f30
->has_haptic
) {
322 rmi_f30_set_ctrl_data(&f30
->ctrl
[8], &control_address
,
323 f30
->register_count
, &ctrl_reg
);
325 rmi_f30_set_ctrl_data(&f30
->ctrl
[9], &control_address
,
326 sizeof(u8
), &ctrl_reg
);
329 if (f30
->has_mech_mouse_btns
)
330 rmi_f30_set_ctrl_data(&f30
->ctrl
[10], &control_address
,
331 sizeof(u8
), &ctrl_reg
);
333 f30
->ctrl_regs_size
= ctrl_reg
- f30
->ctrl_regs
334 ?: RMI_F30_CTRL_REGS_MAX_SIZE
;
336 retval
= rmi_f30_read_control_parameters(fn
, f30
);
339 "Failed to initialize F19 control params.\n");
343 map_memory
= devm_kzalloc(&fn
->dev
,
344 (f30
->gpioled_count
* (sizeof(u16
))),
347 dev_err(&fn
->dev
, "Failed to allocate gpioled map memory.\n");
351 f30
->gpioled_key_map
= (u16
*)map_memory
;
353 pdata
= rmi_get_platform_data(rmi_dev
);
354 if (pdata
&& f30
->has_gpio
) {
356 for (i
= 0; i
< f30
->gpioled_count
; i
++) {
357 if (rmi_f30_is_valid_button(i
, f30
->ctrl
)) {
358 f30
->gpioled_key_map
[i
] = button
++;
361 * buttonpad might be given by
362 * f30->has_mech_mouse_btns, but I am
363 * not sure, so use only the pdata info
365 if (pdata
->f30_data
&&
366 pdata
->f30_data
->buttonpad
)
375 static int rmi_f30_probe(struct rmi_function
*fn
)
378 const struct rmi_device_platform_data
*pdata
=
379 rmi_get_platform_data(fn
->rmi_dev
);
381 if (pdata
->f30_data
&& pdata
->f30_data
->disable
)
384 rc
= rmi_f30_initialize(fn
);
388 rc
= rmi_f30_register_device(fn
);
399 struct rmi_function_handler rmi_f30_handler
= {
404 .probe
= rmi_f30_probe
,
405 .config
= rmi_f30_config
,
406 .attention
= rmi_f30_attention
,