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