1
2
3
4
5
6
7
8
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"
14
15#define RMI_F30_QUERY_SIZE 2
16
17
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)
25
26
27#define RMI_F30_GPIO_LED_COUNT 0x1F
28
29
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
34
35struct rmi_f30_ctrl_data {
36 int address;
37 int length;
38 u8 *regs;
39};
40
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
44
45#define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \
46 + 1 \
47 + RMI_F30_CTRL_MAX_BYTES \
48 + RMI_F30_CTRL_MAX_BYTES \
49 + RMI_F30_CTRL_MAX_BYTES \
50 + 6 \
51 + RMI_F30_CTRL_MAX_REGS \
52 + RMI_F30_CTRL_MAX_REGS \
53 + RMI_F30_CTRL_MAX_BYTES \
54 + 1 \
55 + 1)
56
57struct f30_data {
58
59 bool has_extended_pattern;
60 bool has_mappable_buttons;
61 bool has_led;
62 bool has_gpio;
63 bool has_haptic;
64 bool has_gpio_driver_control;
65 bool has_mech_mouse_btns;
66 u8 gpioled_count;
67
68 u8 register_count;
69
70
71 struct rmi_f30_ctrl_data ctrl[RMI_F30_CTRL_MAX_REG_BLOCKS];
72 u8 ctrl_regs[RMI_F30_CTRL_REGS_MAX_SIZE];
73 u32 ctrl_regs_size;
74
75 u8 data_regs[RMI_F30_CTRL_MAX_BYTES];
76 u16 *gpioled_key_map;
77
78 struct input_dev *input;
79};
80
81static int rmi_f30_read_control_parameters(struct rmi_function *fn,
82 struct f30_data *f30)
83{
84 struct rmi_device *rmi_dev = fn->rmi_dev;
85 int error = 0;
86
87 error = rmi_read_block(rmi_dev, fn->fd.control_base_addr,
88 f30->ctrl_regs, f30->ctrl_regs_size);
89 if (error) {
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);
92 return error;
93 }
94
95 return 0;
96}
97
98static int rmi_f30_attention(struct rmi_function *fn, unsigned long *irq_bits)
99{
100 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
101 struct rmi_device *rmi_dev = fn->rmi_dev;
102 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
103 int retval;
104 int gpiled = 0;
105 int value = 0;
106 int i;
107 int reg_num;
108
109 if (!f30->input)
110 return 0;
111
112
113 if (drvdata->attn_data.data) {
114 if (drvdata->attn_data.size < f30->register_count) {
115 dev_warn(&fn->dev, "F30 interrupted, but data is missing\n");
116 return 0;
117 }
118 memcpy(f30->data_regs, drvdata->attn_data.data,
119 f30->register_count);
120 drvdata->attn_data.data += f30->register_count;
121 drvdata->attn_data.size -= f30->register_count;
122 } else {
123 retval = rmi_read_block(rmi_dev, fn->fd.data_base_addr,
124 f30->data_regs, f30->register_count);
125
126 if (retval) {
127 dev_err(&fn->dev, "%s: Failed to read F30 data registers.\n",
128 __func__);
129 return retval;
130 }
131 }
132
133 for (reg_num = 0; reg_num < f30->register_count; ++reg_num) {
134 for (i = 0; gpiled < f30->gpioled_count && i < 8; ++i,
135 ++gpiled) {
136 if (f30->gpioled_key_map[gpiled] != 0) {
137
138 value = (((f30->data_regs[reg_num] >> i) & 0x01)
139 == 0);
140
141 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
142 "%s: call input report key (0x%04x) value (0x%02x)",
143 __func__,
144 f30->gpioled_key_map[gpiled], value);
145 input_report_key(f30->input,
146 f30->gpioled_key_map[gpiled],
147 value);
148 }
149
150 }
151 }
152
153 return 0;
154}
155
156static int rmi_f30_register_device(struct rmi_function *fn)
157{
158 int i;
159 struct rmi_device *rmi_dev = fn->rmi_dev;
160 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
161 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
162 struct input_dev *input_dev;
163 int button_count = 0;
164
165 input_dev = drv_data->input;
166 if (!input_dev) {
167 dev_info(&fn->dev, "F30: no input device found, ignoring.\n");
168 return -EINVAL;
169 }
170
171 f30->input = input_dev;
172
173 set_bit(EV_KEY, input_dev->evbit);
174
175 input_dev->keycode = f30->gpioled_key_map;
176 input_dev->keycodesize = sizeof(u16);
177 input_dev->keycodemax = f30->gpioled_count;
178
179 for (i = 0; i < f30->gpioled_count; i++) {
180 if (f30->gpioled_key_map[i] != 0) {
181 input_set_capability(input_dev, EV_KEY,
182 f30->gpioled_key_map[i]);
183 button_count++;
184 }
185 }
186
187 if (button_count == 1)
188 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
189 return 0;
190}
191
192static int rmi_f30_config(struct rmi_function *fn)
193{
194 struct f30_data *f30 = dev_get_drvdata(&fn->dev);
195 struct rmi_driver *drv = fn->rmi_dev->driver;
196 const struct rmi_device_platform_data *pdata =
197 rmi_get_platform_data(fn->rmi_dev);
198 int error;
199
200 if (pdata->f30_data.disable) {
201 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
202 } else {
203
204 error = rmi_write_block(fn->rmi_dev, fn->fd.control_base_addr,
205 f30->ctrl_regs, f30->ctrl_regs_size);
206 if (error) {
207 dev_err(&fn->rmi_dev->dev,
208 "%s : Could not write control registers at 0x%x error (%d)\n",
209 __func__, fn->fd.control_base_addr, error);
210 return error;
211 }
212
213 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
214 }
215 return 0;
216}
217
218static inline void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data *ctrl,
219 int *ctrl_addr, int len, u8 **reg)
220{
221 ctrl->address = *ctrl_addr;
222 ctrl->length = len;
223 ctrl->regs = *reg;
224 *ctrl_addr += len;
225 *reg += len;
226}
227
228static inline bool rmi_f30_is_valid_button(int button,
229 struct rmi_f30_ctrl_data *ctrl)
230{
231 int byte_position = button >> 3;
232 int bit_position = button & 0x07;
233
234
235
236
237
238 return !(ctrl[2].regs[byte_position] & BIT(bit_position)) &&
239 (ctrl[3].regs[byte_position] & BIT(bit_position));
240}
241
242static inline int rmi_f30_initialize(struct rmi_function *fn)
243{
244 struct f30_data *f30;
245 struct rmi_device *rmi_dev = fn->rmi_dev;
246 const struct rmi_device_platform_data *pdata;
247 int retval = 0;
248 int control_address;
249 int i;
250 int button;
251 u8 buf[RMI_F30_QUERY_SIZE];
252 u8 *ctrl_reg;
253 u8 *map_memory;
254
255 f30 = devm_kzalloc(&fn->dev, sizeof(struct f30_data),
256 GFP_KERNEL);
257 if (!f30)
258 return -ENOMEM;
259
260 dev_set_drvdata(&fn->dev, f30);
261
262 retval = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr, buf,
263 RMI_F30_QUERY_SIZE);
264
265 if (retval) {
266 dev_err(&fn->dev, "Failed to read query register.\n");
267 return retval;
268 }
269
270 f30->has_extended_pattern = buf[0] & RMI_F30_EXTENDED_PATTERNS;
271 f30->has_mappable_buttons = buf[0] & RMI_F30_HAS_MAPPABLE_BUTTONS;
272 f30->has_led = buf[0] & RMI_F30_HAS_LED;
273 f30->has_gpio = buf[0] & RMI_F30_HAS_GPIO;
274 f30->has_haptic = buf[0] & RMI_F30_HAS_HAPTIC;
275 f30->has_gpio_driver_control = buf[0] & RMI_F30_HAS_GPIO_DRV_CTL;
276 f30->has_mech_mouse_btns = buf[0] & RMI_F30_HAS_MECH_MOUSE_BTNS;
277 f30->gpioled_count = buf[1] & RMI_F30_GPIO_LED_COUNT;
278
279 f30->register_count = (f30->gpioled_count + 7) >> 3;
280
281 control_address = fn->fd.control_base_addr;
282 ctrl_reg = f30->ctrl_regs;
283
284 if (f30->has_gpio && f30->has_led)
285 rmi_f30_set_ctrl_data(&f30->ctrl[0], &control_address,
286 f30->register_count, &ctrl_reg);
287
288 rmi_f30_set_ctrl_data(&f30->ctrl[1], &control_address, sizeof(u8),
289 &ctrl_reg);
290
291 if (f30->has_gpio) {
292 rmi_f30_set_ctrl_data(&f30->ctrl[2], &control_address,
293 f30->register_count, &ctrl_reg);
294
295 rmi_f30_set_ctrl_data(&f30->ctrl[3], &control_address,
296 f30->register_count, &ctrl_reg);
297 }
298
299 if (f30->has_led) {
300 int ctrl5_len;
301
302 rmi_f30_set_ctrl_data(&f30->ctrl[4], &control_address,
303 f30->register_count, &ctrl_reg);
304
305 if (f30->has_extended_pattern)
306 ctrl5_len = 6;
307 else
308 ctrl5_len = 2;
309
310 rmi_f30_set_ctrl_data(&f30->ctrl[5], &control_address,
311 ctrl5_len, &ctrl_reg);
312 }
313
314 if (f30->has_led || f30->has_gpio_driver_control) {
315
316 rmi_f30_set_ctrl_data(&f30->ctrl[6], &control_address,
317 f30->gpioled_count, &ctrl_reg);
318 }
319
320 if (f30->has_mappable_buttons) {
321
322 rmi_f30_set_ctrl_data(&f30->ctrl[7], &control_address,
323 f30->gpioled_count, &ctrl_reg);
324 }
325
326 if (f30->has_haptic) {
327 rmi_f30_set_ctrl_data(&f30->ctrl[8], &control_address,
328 f30->register_count, &ctrl_reg);
329
330 rmi_f30_set_ctrl_data(&f30->ctrl[9], &control_address,
331 sizeof(u8), &ctrl_reg);
332 }
333
334 if (f30->has_mech_mouse_btns)
335 rmi_f30_set_ctrl_data(&f30->ctrl[10], &control_address,
336 sizeof(u8), &ctrl_reg);
337
338 f30->ctrl_regs_size = ctrl_reg - f30->ctrl_regs
339 ?: RMI_F30_CTRL_REGS_MAX_SIZE;
340
341 retval = rmi_f30_read_control_parameters(fn, f30);
342 if (retval < 0) {
343 dev_err(&fn->dev,
344 "Failed to initialize F19 control params.\n");
345 return retval;
346 }
347
348 map_memory = devm_kzalloc(&fn->dev,
349 (f30->gpioled_count * (sizeof(u16))),
350 GFP_KERNEL);
351 if (!map_memory) {
352 dev_err(&fn->dev, "Failed to allocate gpioled map memory.\n");
353 return -ENOMEM;
354 }
355
356 f30->gpioled_key_map = (u16 *)map_memory;
357
358 pdata = rmi_get_platform_data(rmi_dev);
359 if (f30->has_gpio) {
360 button = BTN_LEFT;
361 for (i = 0; i < f30->gpioled_count; i++) {
362 if (rmi_f30_is_valid_button(i, f30->ctrl)) {
363 f30->gpioled_key_map[i] = button++;
364
365
366
367
368
369
370 if (pdata->f30_data.buttonpad)
371 break;
372 }
373 }
374 }
375
376 return 0;
377}
378
379static int rmi_f30_probe(struct rmi_function *fn)
380{
381 int rc;
382 const struct rmi_device_platform_data *pdata =
383 rmi_get_platform_data(fn->rmi_dev);
384
385 if (pdata->f30_data.disable)
386 return 0;
387
388 rc = rmi_f30_initialize(fn);
389 if (rc < 0)
390 goto error_exit;
391
392 rc = rmi_f30_register_device(fn);
393 if (rc < 0)
394 goto error_exit;
395
396 return 0;
397
398error_exit:
399 return rc;
400
401}
402
403struct rmi_function_handler rmi_f30_handler = {
404 .driver = {
405 .name = "rmi4_f30",
406 },
407 .func = 0x30,
408 .probe = rmi_f30_probe,
409 .config = rmi_f30_config,
410 .attention = rmi_f30_attention,
411};
412