1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
5 *
6 * This driver provides the core support for a single RMI4-based device.
7 *
8 * The RMI4 specification can be found here (URL split for line length):
9 *
10 * http://www.synaptics.com/sites/default/files/
11 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
12 */
13
14 #include <linux/bitmap.h>
15 #include <linux/delay.h>
16 #include <linux/fs.h>
17 #include <linux/irq.h>
18 #include <linux/pm.h>
19 #include <linux/slab.h>
20 #include <linux/of.h>
21 #include <linux/irqdomain.h>
22 #include <uapi/linux/input.h>
23 #include <linux/rmi.h>
24 #include <linux/export.h>
25 #include "rmi_bus.h"
26 #include "rmi_driver.h"
27
28 #define HAS_NONSTANDARD_PDT_MASK 0x40
29 #define RMI4_MAX_PAGE 0xff
30 #define RMI4_PAGE_SIZE 0x100
31 #define RMI4_PAGE_MASK 0xFF00
32
33 #define RMI_DEVICE_RESET_CMD 0x01
34 #define DEFAULT_RESET_DELAY_MS 100
35
rmi_free_function_list(struct rmi_device * rmi_dev)36 void rmi_free_function_list(struct rmi_device *rmi_dev)
37 {
38 struct rmi_function *fn, *tmp;
39 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
40
41 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
42
43 /* Doing it in the reverse order so F01 will be removed last */
44 list_for_each_entry_safe_reverse(fn, tmp,
45 &data->function_list, node) {
46 list_del(&fn->node);
47 rmi_unregister_function(fn);
48 }
49
50 devm_kfree(&rmi_dev->dev, data->irq_memory);
51 data->irq_memory = NULL;
52 data->irq_status = NULL;
53 data->fn_irq_bits = NULL;
54 data->current_irq_mask = NULL;
55 data->new_irq_mask = NULL;
56
57 data->f01_container = NULL;
58 data->f34_container = NULL;
59 }
60
reset_one_function(struct rmi_function * fn)61 static int reset_one_function(struct rmi_function *fn)
62 {
63 struct rmi_function_handler *fh;
64 int retval = 0;
65
66 if (!fn || !fn->dev.driver)
67 return 0;
68
69 fh = to_rmi_function_handler(fn->dev.driver);
70 if (fh->reset) {
71 retval = fh->reset(fn);
72 if (retval < 0)
73 dev_err(&fn->dev, "Reset failed with code %d.\n",
74 retval);
75 }
76
77 return retval;
78 }
79
configure_one_function(struct rmi_function * fn)80 static int configure_one_function(struct rmi_function *fn)
81 {
82 struct rmi_function_handler *fh;
83 int retval = 0;
84
85 if (!fn || !fn->dev.driver)
86 return 0;
87
88 fh = to_rmi_function_handler(fn->dev.driver);
89 if (fh->config) {
90 retval = fh->config(fn);
91 if (retval < 0)
92 dev_err(&fn->dev, "Config failed with code %d.\n",
93 retval);
94 }
95
96 return retval;
97 }
98
rmi_driver_process_reset_requests(struct rmi_device * rmi_dev)99 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
100 {
101 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
102 struct rmi_function *entry;
103 int retval;
104
105 list_for_each_entry(entry, &data->function_list, node) {
106 retval = reset_one_function(entry);
107 if (retval < 0)
108 return retval;
109 }
110
111 return 0;
112 }
113
rmi_driver_process_config_requests(struct rmi_device * rmi_dev)114 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
115 {
116 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
117 struct rmi_function *entry;
118 int retval;
119
120 list_for_each_entry(entry, &data->function_list, node) {
121 retval = configure_one_function(entry);
122 if (retval < 0)
123 return retval;
124 }
125
126 return 0;
127 }
128
rmi_process_interrupt_requests(struct rmi_device * rmi_dev)129 static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
130 {
131 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
132 struct device *dev = &rmi_dev->dev;
133 int i;
134 int error;
135
136 if (!data)
137 return 0;
138
139 if (!data->attn_data.data) {
140 error = rmi_read_block(rmi_dev,
141 data->f01_container->fd.data_base_addr + 1,
142 data->irq_status, data->num_of_irq_regs);
143 if (error < 0) {
144 dev_err(dev, "Failed to read irqs, code=%d\n", error);
145 return error;
146 }
147 }
148
149 mutex_lock(&data->irq_mutex);
150 bitmap_and(data->irq_status, data->irq_status, data->fn_irq_bits,
151 data->irq_count);
152 /*
153 * At this point, irq_status has all bits that are set in the
154 * interrupt status register and are enabled.
155 */
156 mutex_unlock(&data->irq_mutex);
157
158 for_each_set_bit(i, data->irq_status, data->irq_count)
159 handle_nested_irq(irq_find_mapping(data->irqdomain, i));
160
161 if (data->input)
162 input_sync(data->input);
163
164 return 0;
165 }
166
rmi_set_attn_data(struct rmi_device * rmi_dev,unsigned long irq_status,void * data,size_t size)167 void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
168 void *data, size_t size)
169 {
170 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
171 struct rmi4_attn_data attn_data;
172 void *fifo_data;
173
174 if (!drvdata->enabled)
175 return;
176
177 fifo_data = kmemdup(data, size, GFP_ATOMIC);
178 if (!fifo_data)
179 return;
180
181 attn_data.irq_status = irq_status;
182 attn_data.size = size;
183 attn_data.data = fifo_data;
184
185 kfifo_put(&drvdata->attn_fifo, attn_data);
186 }
187 EXPORT_SYMBOL_GPL(rmi_set_attn_data);
188
rmi_irq_fn(int irq,void * dev_id)189 static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
190 {
191 struct rmi_device *rmi_dev = dev_id;
192 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
193 struct rmi4_attn_data attn_data = {0};
194 int ret, count;
195
196 count = kfifo_get(&drvdata->attn_fifo, &attn_data);
197 if (count) {
198 *(drvdata->irq_status) = attn_data.irq_status;
199 drvdata->attn_data = attn_data;
200 }
201
202 ret = rmi_process_interrupt_requests(rmi_dev);
203 if (ret)
204 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
205 "Failed to process interrupt request: %d\n", ret);
206
207 if (count) {
208 kfree(attn_data.data);
209 drvdata->attn_data.data = NULL;
210 }
211
212 if (!kfifo_is_empty(&drvdata->attn_fifo))
213 return rmi_irq_fn(irq, dev_id);
214
215 return IRQ_HANDLED;
216 }
217
rmi_irq_init(struct rmi_device * rmi_dev)218 static int rmi_irq_init(struct rmi_device *rmi_dev)
219 {
220 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
221 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
222 int irq_flags = irq_get_trigger_type(pdata->irq);
223 int ret;
224
225 if (!irq_flags)
226 irq_flags = IRQF_TRIGGER_LOW;
227
228 ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
229 rmi_irq_fn, irq_flags | IRQF_ONESHOT,
230 dev_driver_string(rmi_dev->xport->dev),
231 rmi_dev);
232 if (ret < 0) {
233 dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
234 pdata->irq);
235
236 return ret;
237 }
238
239 data->enabled = true;
240
241 return 0;
242 }
243
rmi_find_function(struct rmi_device * rmi_dev,u8 number)244 struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
245 {
246 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
247 struct rmi_function *entry;
248
249 list_for_each_entry(entry, &data->function_list, node) {
250 if (entry->fd.function_number == number)
251 return entry;
252 }
253
254 return NULL;
255 }
256
suspend_one_function(struct rmi_function * fn)257 static int suspend_one_function(struct rmi_function *fn)
258 {
259 struct rmi_function_handler *fh;
260 int retval = 0;
261
262 if (!fn || !fn->dev.driver)
263 return 0;
264
265 fh = to_rmi_function_handler(fn->dev.driver);
266 if (fh->suspend) {
267 retval = fh->suspend(fn);
268 if (retval < 0)
269 dev_err(&fn->dev, "Suspend failed with code %d.\n",
270 retval);
271 }
272
273 return retval;
274 }
275
rmi_suspend_functions(struct rmi_device * rmi_dev)276 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
277 {
278 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
279 struct rmi_function *entry;
280 int retval;
281
282 list_for_each_entry(entry, &data->function_list, node) {
283 retval = suspend_one_function(entry);
284 if (retval < 0)
285 return retval;
286 }
287
288 return 0;
289 }
290
resume_one_function(struct rmi_function * fn)291 static int resume_one_function(struct rmi_function *fn)
292 {
293 struct rmi_function_handler *fh;
294 int retval = 0;
295
296 if (!fn || !fn->dev.driver)
297 return 0;
298
299 fh = to_rmi_function_handler(fn->dev.driver);
300 if (fh->resume) {
301 retval = fh->resume(fn);
302 if (retval < 0)
303 dev_err(&fn->dev, "Resume failed with code %d.\n",
304 retval);
305 }
306
307 return retval;
308 }
309
rmi_resume_functions(struct rmi_device * rmi_dev)310 static int rmi_resume_functions(struct rmi_device *rmi_dev)
311 {
312 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
313 struct rmi_function *entry;
314 int retval;
315
316 list_for_each_entry(entry, &data->function_list, node) {
317 retval = resume_one_function(entry);
318 if (retval < 0)
319 return retval;
320 }
321
322 return 0;
323 }
324
rmi_enable_sensor(struct rmi_device * rmi_dev)325 int rmi_enable_sensor(struct rmi_device *rmi_dev)
326 {
327 int retval = 0;
328
329 retval = rmi_driver_process_config_requests(rmi_dev);
330 if (retval < 0)
331 return retval;
332
333 return rmi_process_interrupt_requests(rmi_dev);
334 }
335
336 /**
337 * rmi_driver_set_input_params - set input device id and other data.
338 *
339 * @rmi_dev: Pointer to an RMI device
340 * @input: Pointer to input device
341 *
342 */
rmi_driver_set_input_params(struct rmi_device * rmi_dev,struct input_dev * input)343 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
344 struct input_dev *input)
345 {
346 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
347 input->id.vendor = SYNAPTICS_VENDOR_ID;
348 input->id.bustype = BUS_RMI;
349 return 0;
350 }
351
rmi_driver_set_input_name(struct rmi_device * rmi_dev,struct input_dev * input)352 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
353 struct input_dev *input)
354 {
355 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
356 const char *device_name = rmi_f01_get_product_ID(data->f01_container);
357 char *name;
358
359 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
360 "Synaptics %s", device_name);
361 if (!name)
362 return;
363
364 input->name = name;
365 }
366
rmi_driver_set_irq_bits(struct rmi_device * rmi_dev,unsigned long * mask)367 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
368 unsigned long *mask)
369 {
370 int error = 0;
371 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
372 struct device *dev = &rmi_dev->dev;
373
374 mutex_lock(&data->irq_mutex);
375 bitmap_or(data->new_irq_mask,
376 data->current_irq_mask, mask, data->irq_count);
377
378 error = rmi_write_block(rmi_dev,
379 data->f01_container->fd.control_base_addr + 1,
380 data->new_irq_mask, data->num_of_irq_regs);
381 if (error < 0) {
382 dev_err(dev, "%s: Failed to change enabled interrupts!",
383 __func__);
384 goto error_unlock;
385 }
386 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
387 data->num_of_irq_regs);
388
389 bitmap_or(data->fn_irq_bits, data->fn_irq_bits, mask, data->irq_count);
390
391 error_unlock:
392 mutex_unlock(&data->irq_mutex);
393 return error;
394 }
395
rmi_driver_clear_irq_bits(struct rmi_device * rmi_dev,unsigned long * mask)396 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
397 unsigned long *mask)
398 {
399 int error = 0;
400 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
401 struct device *dev = &rmi_dev->dev;
402
403 mutex_lock(&data->irq_mutex);
404 bitmap_andnot(data->fn_irq_bits,
405 data->fn_irq_bits, mask, data->irq_count);
406 bitmap_andnot(data->new_irq_mask,
407 data->current_irq_mask, mask, data->irq_count);
408
409 error = rmi_write_block(rmi_dev,
410 data->f01_container->fd.control_base_addr + 1,
411 data->new_irq_mask, data->num_of_irq_regs);
412 if (error < 0) {
413 dev_err(dev, "%s: Failed to change enabled interrupts!",
414 __func__);
415 goto error_unlock;
416 }
417 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
418 data->num_of_irq_regs);
419
420 error_unlock:
421 mutex_unlock(&data->irq_mutex);
422 return error;
423 }
424
rmi_driver_reset_handler(struct rmi_device * rmi_dev)425 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
426 {
427 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
428 int error;
429
430 /*
431 * Can get called before the driver is fully ready to deal with
432 * this situation.
433 */
434 if (!data || !data->f01_container) {
435 dev_warn(&rmi_dev->dev,
436 "Not ready to handle reset yet!\n");
437 return 0;
438 }
439
440 error = rmi_read_block(rmi_dev,
441 data->f01_container->fd.control_base_addr + 1,
442 data->current_irq_mask, data->num_of_irq_regs);
443 if (error < 0) {
444 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
445 __func__);
446 return error;
447 }
448
449 error = rmi_driver_process_reset_requests(rmi_dev);
450 if (error < 0)
451 return error;
452
453 error = rmi_driver_process_config_requests(rmi_dev);
454 if (error < 0)
455 return error;
456
457 return 0;
458 }
459
rmi_read_pdt_entry(struct rmi_device * rmi_dev,struct pdt_entry * entry,u16 pdt_address)460 static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
461 struct pdt_entry *entry, u16 pdt_address)
462 {
463 u8 buf[RMI_PDT_ENTRY_SIZE];
464 int error;
465
466 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
467 if (error) {
468 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
469 pdt_address, error);
470 return error;
471 }
472
473 entry->page_start = pdt_address & RMI4_PAGE_MASK;
474 entry->query_base_addr = buf[0];
475 entry->command_base_addr = buf[1];
476 entry->control_base_addr = buf[2];
477 entry->data_base_addr = buf[3];
478 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
479 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
480 entry->function_number = buf[5];
481
482 return 0;
483 }
484
rmi_driver_copy_pdt_to_fd(const struct pdt_entry * pdt,struct rmi_function_descriptor * fd)485 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
486 struct rmi_function_descriptor *fd)
487 {
488 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
489 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
490 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
491 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
492 fd->function_number = pdt->function_number;
493 fd->interrupt_source_count = pdt->interrupt_source_count;
494 fd->function_version = pdt->function_version;
495 }
496
497 #define RMI_SCAN_CONTINUE 0
498 #define RMI_SCAN_DONE 1
499
rmi_scan_pdt_page(struct rmi_device * rmi_dev,int page,int * empty_pages,void * ctx,int (* callback)(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * entry))500 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
501 int page,
502 int *empty_pages,
503 void *ctx,
504 int (*callback)(struct rmi_device *rmi_dev,
505 void *ctx,
506 const struct pdt_entry *entry))
507 {
508 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
509 struct pdt_entry pdt_entry;
510 u16 page_start = RMI4_PAGE_SIZE * page;
511 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
512 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
513 u16 addr;
514 int error;
515 int retval;
516
517 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
518 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
519 if (error)
520 return error;
521
522 if (RMI4_END_OF_PDT(pdt_entry.function_number))
523 break;
524
525 retval = callback(rmi_dev, ctx, &pdt_entry);
526 if (retval != RMI_SCAN_CONTINUE)
527 return retval;
528 }
529
530 /*
531 * Count number of empty PDT pages. If a gap of two pages
532 * or more is found, stop scanning.
533 */
534 if (addr == pdt_start)
535 ++*empty_pages;
536 else
537 *empty_pages = 0;
538
539 return (data->bootloader_mode || *empty_pages >= 2) ?
540 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
541 }
542
rmi_scan_pdt(struct rmi_device * rmi_dev,void * ctx,int (* callback)(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * entry))543 int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
544 int (*callback)(struct rmi_device *rmi_dev,
545 void *ctx, const struct pdt_entry *entry))
546 {
547 int page;
548 int empty_pages = 0;
549 int retval = RMI_SCAN_DONE;
550
551 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
552 retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
553 ctx, callback);
554 if (retval != RMI_SCAN_CONTINUE)
555 break;
556 }
557
558 return retval < 0 ? retval : 0;
559 }
560
rmi_read_register_desc(struct rmi_device * d,u16 addr,struct rmi_register_descriptor * rdesc)561 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
562 struct rmi_register_descriptor *rdesc)
563 {
564 int ret;
565 u8 size_presence_reg;
566 u8 buf[35];
567 int presense_offset = 1;
568 u8 *struct_buf;
569 int reg;
570 int offset = 0;
571 int map_offset = 0;
572 int i;
573 int b;
574
575 /*
576 * The first register of the register descriptor is the size of
577 * the register descriptor's presense register.
578 */
579 ret = rmi_read(d, addr, &size_presence_reg);
580 if (ret)
581 return ret;
582 ++addr;
583
584 if (size_presence_reg < 0 || size_presence_reg > 35)
585 return -EIO;
586
587 memset(buf, 0, sizeof(buf));
588
589 /*
590 * The presence register contains the size of the register structure
591 * and a bitmap which identified which packet registers are present
592 * for this particular register type (ie query, control, or data).
593 */
594 ret = rmi_read_block(d, addr, buf, size_presence_reg);
595 if (ret)
596 return ret;
597 ++addr;
598
599 if (buf[0] == 0) {
600 presense_offset = 3;
601 rdesc->struct_size = buf[1] | (buf[2] << 8);
602 } else {
603 rdesc->struct_size = buf[0];
604 }
605
606 for (i = presense_offset; i < size_presence_reg; i++) {
607 for (b = 0; b < 8; b++) {
608 if (buf[i] & (0x1 << b))
609 bitmap_set(rdesc->presense_map, map_offset, 1);
610 ++map_offset;
611 }
612 }
613
614 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
615 RMI_REG_DESC_PRESENSE_BITS);
616
617 rdesc->registers = devm_kcalloc(&d->dev,
618 rdesc->num_registers,
619 sizeof(struct rmi_register_desc_item),
620 GFP_KERNEL);
621 if (!rdesc->registers)
622 return -ENOMEM;
623
624 /*
625 * Allocate a temporary buffer to hold the register structure.
626 * I'm not using devm_kzalloc here since it will not be retained
627 * after exiting this function
628 */
629 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
630 if (!struct_buf)
631 return -ENOMEM;
632
633 /*
634 * The register structure contains information about every packet
635 * register of this type. This includes the size of the packet
636 * register and a bitmap of all subpackets contained in the packet
637 * register.
638 */
639 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
640 if (ret)
641 goto free_struct_buff;
642
643 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
644 for (i = 0; i < rdesc->num_registers; i++) {
645 struct rmi_register_desc_item *item = &rdesc->registers[i];
646 int reg_size = struct_buf[offset];
647
648 ++offset;
649 if (reg_size == 0) {
650 reg_size = struct_buf[offset] |
651 (struct_buf[offset + 1] << 8);
652 offset += 2;
653 }
654
655 if (reg_size == 0) {
656 reg_size = struct_buf[offset] |
657 (struct_buf[offset + 1] << 8) |
658 (struct_buf[offset + 2] << 16) |
659 (struct_buf[offset + 3] << 24);
660 offset += 4;
661 }
662
663 item->reg = reg;
664 item->reg_size = reg_size;
665
666 map_offset = 0;
667
668 do {
669 for (b = 0; b < 7; b++) {
670 if (struct_buf[offset] & (0x1 << b))
671 bitmap_set(item->subpacket_map,
672 map_offset, 1);
673 ++map_offset;
674 }
675 } while (struct_buf[offset++] & 0x80);
676
677 item->num_subpackets = bitmap_weight(item->subpacket_map,
678 RMI_REG_DESC_SUBPACKET_BITS);
679
680 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
681 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
682 item->reg, item->reg_size, item->num_subpackets);
683
684 reg = find_next_bit(rdesc->presense_map,
685 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
686 }
687
688 free_struct_buff:
689 kfree(struct_buf);
690 return ret;
691 }
692
rmi_get_register_desc_item(struct rmi_register_descriptor * rdesc,u16 reg)693 const struct rmi_register_desc_item *rmi_get_register_desc_item(
694 struct rmi_register_descriptor *rdesc, u16 reg)
695 {
696 const struct rmi_register_desc_item *item;
697 int i;
698
699 for (i = 0; i < rdesc->num_registers; i++) {
700 item = &rdesc->registers[i];
701 if (item->reg == reg)
702 return item;
703 }
704
705 return NULL;
706 }
707
rmi_register_desc_calc_size(struct rmi_register_descriptor * rdesc)708 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
709 {
710 const struct rmi_register_desc_item *item;
711 int i;
712 size_t size = 0;
713
714 for (i = 0; i < rdesc->num_registers; i++) {
715 item = &rdesc->registers[i];
716 size += item->reg_size;
717 }
718 return size;
719 }
720
721 /* Compute the register offset relative to the base address */
rmi_register_desc_calc_reg_offset(struct rmi_register_descriptor * rdesc,u16 reg)722 int rmi_register_desc_calc_reg_offset(
723 struct rmi_register_descriptor *rdesc, u16 reg)
724 {
725 const struct rmi_register_desc_item *item;
726 int offset = 0;
727 int i;
728
729 for (i = 0; i < rdesc->num_registers; i++) {
730 item = &rdesc->registers[i];
731 if (item->reg == reg)
732 return offset;
733 ++offset;
734 }
735 return -1;
736 }
737
rmi_register_desc_has_subpacket(const struct rmi_register_desc_item * item,u8 subpacket)738 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
739 u8 subpacket)
740 {
741 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
742 subpacket) == subpacket;
743 }
744
rmi_check_bootloader_mode(struct rmi_device * rmi_dev,const struct pdt_entry * pdt)745 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
746 const struct pdt_entry *pdt)
747 {
748 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
749 int ret;
750 u8 status;
751
752 if (pdt->function_number == 0x34 && pdt->function_version > 1) {
753 ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
754 if (ret) {
755 dev_err(&rmi_dev->dev,
756 "Failed to read F34 status: %d.\n", ret);
757 return ret;
758 }
759
760 if (status & BIT(7))
761 data->bootloader_mode = true;
762 } else if (pdt->function_number == 0x01) {
763 ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
764 if (ret) {
765 dev_err(&rmi_dev->dev,
766 "Failed to read F01 status: %d.\n", ret);
767 return ret;
768 }
769
770 if (status & BIT(6))
771 data->bootloader_mode = true;
772 }
773
774 return 0;
775 }
776
rmi_count_irqs(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)777 static int rmi_count_irqs(struct rmi_device *rmi_dev,
778 void *ctx, const struct pdt_entry *pdt)
779 {
780 int *irq_count = ctx;
781 int ret;
782
783 *irq_count += pdt->interrupt_source_count;
784
785 ret = rmi_check_bootloader_mode(rmi_dev, pdt);
786 if (ret < 0)
787 return ret;
788
789 return RMI_SCAN_CONTINUE;
790 }
791
rmi_initial_reset(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)792 int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
793 const struct pdt_entry *pdt)
794 {
795 int error;
796
797 if (pdt->function_number == 0x01) {
798 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
799 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
800 const struct rmi_device_platform_data *pdata =
801 rmi_get_platform_data(rmi_dev);
802
803 if (rmi_dev->xport->ops->reset) {
804 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
805 cmd_addr);
806 if (error)
807 return error;
808
809 return RMI_SCAN_DONE;
810 }
811
812 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
813 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
814 if (error) {
815 dev_err(&rmi_dev->dev,
816 "Initial reset failed. Code = %d.\n", error);
817 return error;
818 }
819
820 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
821
822 return RMI_SCAN_DONE;
823 }
824
825 /* F01 should always be on page 0. If we don't find it there, fail. */
826 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
827 }
828
rmi_create_function(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)829 static int rmi_create_function(struct rmi_device *rmi_dev,
830 void *ctx, const struct pdt_entry *pdt)
831 {
832 struct device *dev = &rmi_dev->dev;
833 struct rmi_driver_data *data = dev_get_drvdata(dev);
834 int *current_irq_count = ctx;
835 struct rmi_function *fn;
836 int i;
837 int error;
838
839 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
840 pdt->function_number);
841
842 fn = kzalloc(sizeof(struct rmi_function) +
843 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
844 GFP_KERNEL);
845 if (!fn) {
846 dev_err(dev, "Failed to allocate memory for F%02X\n",
847 pdt->function_number);
848 return -ENOMEM;
849 }
850
851 INIT_LIST_HEAD(&fn->node);
852 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
853
854 fn->rmi_dev = rmi_dev;
855
856 fn->num_of_irqs = pdt->interrupt_source_count;
857 fn->irq_pos = *current_irq_count;
858 *current_irq_count += fn->num_of_irqs;
859
860 for (i = 0; i < fn->num_of_irqs; i++)
861 set_bit(fn->irq_pos + i, fn->irq_mask);
862
863 error = rmi_register_function(fn);
864 if (error)
865 return error;
866
867 if (pdt->function_number == 0x01)
868 data->f01_container = fn;
869 else if (pdt->function_number == 0x34)
870 data->f34_container = fn;
871
872 list_add_tail(&fn->node, &data->function_list);
873
874 return RMI_SCAN_CONTINUE;
875 }
876
rmi_enable_irq(struct rmi_device * rmi_dev,bool clear_wake)877 void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
878 {
879 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
880 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
881 int irq = pdata->irq;
882 int irq_flags;
883 int retval;
884
885 mutex_lock(&data->enabled_mutex);
886
887 if (data->enabled)
888 goto out;
889
890 enable_irq(irq);
891 data->enabled = true;
892 if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
893 retval = disable_irq_wake(irq);
894 if (retval)
895 dev_warn(&rmi_dev->dev,
896 "Failed to disable irq for wake: %d\n",
897 retval);
898 }
899
900 /*
901 * Call rmi_process_interrupt_requests() after enabling irq,
902 * otherwise we may lose interrupt on edge-triggered systems.
903 */
904 irq_flags = irq_get_trigger_type(pdata->irq);
905 if (irq_flags & IRQ_TYPE_EDGE_BOTH)
906 rmi_process_interrupt_requests(rmi_dev);
907
908 out:
909 mutex_unlock(&data->enabled_mutex);
910 }
911
rmi_disable_irq(struct rmi_device * rmi_dev,bool enable_wake)912 void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
913 {
914 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
915 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
916 struct rmi4_attn_data attn_data = {0};
917 int irq = pdata->irq;
918 int retval, count;
919
920 mutex_lock(&data->enabled_mutex);
921
922 if (!data->enabled)
923 goto out;
924
925 data->enabled = false;
926 disable_irq(irq);
927 if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
928 retval = enable_irq_wake(irq);
929 if (retval)
930 dev_warn(&rmi_dev->dev,
931 "Failed to enable irq for wake: %d\n",
932 retval);
933 }
934
935 /* make sure the fifo is clean */
936 while (!kfifo_is_empty(&data->attn_fifo)) {
937 count = kfifo_get(&data->attn_fifo, &attn_data);
938 if (count)
939 kfree(attn_data.data);
940 }
941
942 out:
943 mutex_unlock(&data->enabled_mutex);
944 }
945
rmi_driver_suspend(struct rmi_device * rmi_dev,bool enable_wake)946 int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
947 {
948 int retval;
949
950 retval = rmi_suspend_functions(rmi_dev);
951 if (retval)
952 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
953 retval);
954
955 rmi_disable_irq(rmi_dev, enable_wake);
956 return retval;
957 }
958 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
959
rmi_driver_resume(struct rmi_device * rmi_dev,bool clear_wake)960 int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
961 {
962 int retval;
963
964 rmi_enable_irq(rmi_dev, clear_wake);
965
966 retval = rmi_resume_functions(rmi_dev);
967 if (retval)
968 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
969 retval);
970
971 return retval;
972 }
973 EXPORT_SYMBOL_GPL(rmi_driver_resume);
974
rmi_driver_remove(struct device * dev)975 static int rmi_driver_remove(struct device *dev)
976 {
977 struct rmi_device *rmi_dev = to_rmi_device(dev);
978 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
979
980 rmi_disable_irq(rmi_dev, false);
981
982 rmi_f34_remove_sysfs(rmi_dev);
983 rmi_free_function_list(rmi_dev);
984
985 irq_domain_remove(data->irqdomain);
986 data->irqdomain = NULL;
987
988 return 0;
989 }
990
991 #ifdef CONFIG_OF
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)992 static int rmi_driver_of_probe(struct device *dev,
993 struct rmi_device_platform_data *pdata)
994 {
995 int retval;
996
997 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
998 "syna,reset-delay-ms", 1);
999 if (retval)
1000 return retval;
1001
1002 return 0;
1003 }
1004 #else
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)1005 static inline int rmi_driver_of_probe(struct device *dev,
1006 struct rmi_device_platform_data *pdata)
1007 {
1008 return -ENODEV;
1009 }
1010 #endif
1011
rmi_probe_interrupts(struct rmi_driver_data * data)1012 int rmi_probe_interrupts(struct rmi_driver_data *data)
1013 {
1014 struct rmi_device *rmi_dev = data->rmi_dev;
1015 struct device *dev = &rmi_dev->dev;
1016 struct fwnode_handle *fwnode = rmi_dev->xport->dev->fwnode;
1017 int irq_count = 0;
1018 size_t size;
1019 int retval;
1020
1021 /*
1022 * We need to count the IRQs and allocate their storage before scanning
1023 * the PDT and creating the function entries, because adding a new
1024 * function can trigger events that result in the IRQ related storage
1025 * being accessed.
1026 */
1027 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
1028 data->bootloader_mode = false;
1029
1030 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
1031 if (retval < 0) {
1032 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
1033 return retval;
1034 }
1035
1036 if (data->bootloader_mode)
1037 dev_warn(dev, "Device in bootloader mode.\n");
1038
1039 /* Allocate and register a linear revmap irq_domain */
1040 data->irqdomain = irq_domain_create_linear(fwnode, irq_count,
1041 &irq_domain_simple_ops,
1042 data);
1043 if (!data->irqdomain) {
1044 dev_err(&rmi_dev->dev, "Failed to create IRQ domain\n");
1045 return -ENOMEM;
1046 }
1047
1048 data->irq_count = irq_count;
1049 data->num_of_irq_regs = (data->irq_count + 7) / 8;
1050
1051 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1052 data->irq_memory = devm_kcalloc(dev, size, 4, GFP_KERNEL);
1053 if (!data->irq_memory) {
1054 dev_err(dev, "Failed to allocate memory for irq masks.\n");
1055 return -ENOMEM;
1056 }
1057
1058 data->irq_status = data->irq_memory + size * 0;
1059 data->fn_irq_bits = data->irq_memory + size * 1;
1060 data->current_irq_mask = data->irq_memory + size * 2;
1061 data->new_irq_mask = data->irq_memory + size * 3;
1062
1063 return retval;
1064 }
1065
rmi_init_functions(struct rmi_driver_data * data)1066 int rmi_init_functions(struct rmi_driver_data *data)
1067 {
1068 struct rmi_device *rmi_dev = data->rmi_dev;
1069 struct device *dev = &rmi_dev->dev;
1070 int irq_count = 0;
1071 int retval;
1072
1073 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
1074 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
1075 if (retval < 0) {
1076 dev_err(dev, "Function creation failed with code %d.\n",
1077 retval);
1078 goto err_destroy_functions;
1079 }
1080
1081 if (!data->f01_container) {
1082 dev_err(dev, "Missing F01 container!\n");
1083 retval = -EINVAL;
1084 goto err_destroy_functions;
1085 }
1086
1087 retval = rmi_read_block(rmi_dev,
1088 data->f01_container->fd.control_base_addr + 1,
1089 data->current_irq_mask, data->num_of_irq_regs);
1090 if (retval < 0) {
1091 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
1092 __func__);
1093 goto err_destroy_functions;
1094 }
1095
1096 return 0;
1097
1098 err_destroy_functions:
1099 rmi_free_function_list(rmi_dev);
1100 return retval;
1101 }
1102
rmi_driver_probe(struct device * dev)1103 static int rmi_driver_probe(struct device *dev)
1104 {
1105 struct rmi_driver *rmi_driver;
1106 struct rmi_driver_data *data;
1107 struct rmi_device_platform_data *pdata;
1108 struct rmi_device *rmi_dev;
1109 int retval;
1110
1111 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
1112 __func__);
1113
1114 if (!rmi_is_physical_device(dev)) {
1115 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
1116 return -ENODEV;
1117 }
1118
1119 rmi_dev = to_rmi_device(dev);
1120 rmi_driver = to_rmi_driver(dev->driver);
1121 rmi_dev->driver = rmi_driver;
1122
1123 pdata = rmi_get_platform_data(rmi_dev);
1124
1125 if (rmi_dev->xport->dev->of_node) {
1126 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1127 if (retval)
1128 return retval;
1129 }
1130
1131 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
1132 if (!data)
1133 return -ENOMEM;
1134
1135 INIT_LIST_HEAD(&data->function_list);
1136 data->rmi_dev = rmi_dev;
1137 dev_set_drvdata(&rmi_dev->dev, data);
1138
1139 /*
1140 * Right before a warm boot, the sensor might be in some unusual state,
1141 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1142 * or configuration update. In order to clear the sensor to a known
1143 * state and/or apply any updates, we issue a initial reset to clear any
1144 * previous settings and force it into normal operation.
1145 *
1146 * We have to do this before actually building the PDT because
1147 * the reflash updates (if any) might cause various registers to move
1148 * around.
1149 *
1150 * For a number of reasons, this initial reset may fail to return
1151 * within the specified time, but we'll still be able to bring up the
1152 * driver normally after that failure. This occurs most commonly in
1153 * a cold boot situation (where then firmware takes longer to come up
1154 * than from a warm boot) and the reset_delay_ms in the platform data
1155 * has been set too short to accommodate that. Since the sensor will
1156 * eventually come up and be usable, we don't want to just fail here
1157 * and leave the customer's device unusable. So we warn them, and
1158 * continue processing.
1159 */
1160 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
1161 if (retval < 0)
1162 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1163
1164 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1165 if (retval < 0) {
1166 /*
1167 * we'll print out a warning and continue since
1168 * failure to get the PDT properties is not a cause to fail
1169 */
1170 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1171 PDT_PROPERTIES_LOCATION, retval);
1172 }
1173
1174 mutex_init(&data->irq_mutex);
1175 mutex_init(&data->enabled_mutex);
1176
1177 retval = rmi_probe_interrupts(data);
1178 if (retval)
1179 goto err;
1180
1181 if (rmi_dev->xport->input) {
1182 /*
1183 * The transport driver already has an input device.
1184 * In some cases it is preferable to reuse the transport
1185 * devices input device instead of creating a new one here.
1186 * One example is some HID touchpads report "pass-through"
1187 * button events are not reported by rmi registers.
1188 */
1189 data->input = rmi_dev->xport->input;
1190 } else {
1191 data->input = devm_input_allocate_device(dev);
1192 if (!data->input) {
1193 dev_err(dev, "%s: Failed to allocate input device.\n",
1194 __func__);
1195 retval = -ENOMEM;
1196 goto err;
1197 }
1198 rmi_driver_set_input_params(rmi_dev, data->input);
1199 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1200 "%s/input0", dev_name(dev));
1201 if (!data->input->phys) {
1202 retval = -ENOMEM;
1203 goto err;
1204 }
1205 }
1206
1207 retval = rmi_init_functions(data);
1208 if (retval)
1209 goto err;
1210
1211 retval = rmi_f34_create_sysfs(rmi_dev);
1212 if (retval)
1213 goto err;
1214
1215 if (data->input) {
1216 rmi_driver_set_input_name(rmi_dev, data->input);
1217 if (!rmi_dev->xport->input) {
1218 retval = input_register_device(data->input);
1219 if (retval) {
1220 dev_err(dev, "%s: Failed to register input device.\n",
1221 __func__);
1222 goto err_destroy_functions;
1223 }
1224 }
1225 }
1226
1227 retval = rmi_irq_init(rmi_dev);
1228 if (retval < 0)
1229 goto err_destroy_functions;
1230
1231 if (data->f01_container->dev.driver) {
1232 /* Driver already bound, so enable ATTN now. */
1233 retval = rmi_enable_sensor(rmi_dev);
1234 if (retval)
1235 goto err_disable_irq;
1236 }
1237
1238 return 0;
1239
1240 err_disable_irq:
1241 rmi_disable_irq(rmi_dev, false);
1242 err_destroy_functions:
1243 rmi_free_function_list(rmi_dev);
1244 err:
1245 return retval;
1246 }
1247
1248 static struct rmi_driver rmi_physical_driver = {
1249 .driver = {
1250 .owner = THIS_MODULE,
1251 .name = "rmi4_physical",
1252 .bus = &rmi_bus_type,
1253 .probe = rmi_driver_probe,
1254 .remove = rmi_driver_remove,
1255 },
1256 .reset_handler = rmi_driver_reset_handler,
1257 .clear_irq_bits = rmi_driver_clear_irq_bits,
1258 .set_irq_bits = rmi_driver_set_irq_bits,
1259 .set_input_params = rmi_driver_set_input_params,
1260 };
1261
rmi_is_physical_driver(const struct device_driver * drv)1262 bool rmi_is_physical_driver(const struct device_driver *drv)
1263 {
1264 return drv == &rmi_physical_driver.driver;
1265 }
1266
rmi_register_physical_driver(void)1267 int __init rmi_register_physical_driver(void)
1268 {
1269 int error;
1270
1271 error = driver_register(&rmi_physical_driver.driver);
1272 if (error) {
1273 pr_err("%s: driver register failed, code=%d.\n", __func__,
1274 error);
1275 return error;
1276 }
1277
1278 return 0;
1279 }
1280
rmi_unregister_physical_driver(void)1281 void __exit rmi_unregister_physical_driver(void)
1282 {
1283 driver_unregister(&rmi_physical_driver.driver);
1284 }
1285