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 retval = rmi_suspend_functions(rmi_dev);
995 if (retval)
996 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
997 retval);
998
999 rmi_disable_irq(rmi_dev, enable_wake);
1000 return retval;
1001 }
1002 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
1003
rmi_driver_resume(struct rmi_device * rmi_dev,bool clear_wake)1004 int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
1005 {
1006 int retval;
1007
1008 rmi_enable_irq(rmi_dev, clear_wake);
1009
1010 retval = rmi_resume_functions(rmi_dev);
1011 if (retval)
1012 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
1013 retval);
1014
1015 return retval;
1016 }
1017 EXPORT_SYMBOL_GPL(rmi_driver_resume);
1018
rmi_driver_remove(struct device * dev)1019 static int rmi_driver_remove(struct device *dev)
1020 {
1021 struct rmi_device *rmi_dev = to_rmi_device(dev);
1022 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
1023
1024 rmi_disable_irq(rmi_dev, false);
1025
1026 rmi_f34_remove_sysfs(rmi_dev);
1027 rmi_free_function_list(rmi_dev);
1028
1029 irq_domain_remove(data->irqdomain);
1030 data->irqdomain = NULL;
1031
1032 return 0;
1033 }
1034
1035 #ifdef CONFIG_OF
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)1036 static int rmi_driver_of_probe(struct device *dev,
1037 struct rmi_device_platform_data *pdata)
1038 {
1039 int retval;
1040
1041 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
1042 "syna,reset-delay-ms", 1);
1043 if (retval)
1044 return retval;
1045
1046 return 0;
1047 }
1048 #else
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)1049 static inline int rmi_driver_of_probe(struct device *dev,
1050 struct rmi_device_platform_data *pdata)
1051 {
1052 return -ENODEV;
1053 }
1054 #endif
1055
rmi_probe_interrupts(struct rmi_driver_data * data)1056 int rmi_probe_interrupts(struct rmi_driver_data *data)
1057 {
1058 struct rmi_device *rmi_dev = data->rmi_dev;
1059 struct device *dev = &rmi_dev->dev;
1060 struct fwnode_handle *fwnode = rmi_dev->xport->dev->fwnode;
1061 int irq_count = 0;
1062 size_t size;
1063 int retval;
1064
1065 /*
1066 * We need to count the IRQs and allocate their storage before scanning
1067 * the PDT and creating the function entries, because adding a new
1068 * function can trigger events that result in the IRQ related storage
1069 * being accessed.
1070 */
1071 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
1072 data->bootloader_mode = false;
1073
1074 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
1075 if (retval < 0) {
1076 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
1077 return retval;
1078 }
1079
1080 if (data->bootloader_mode)
1081 dev_warn(dev, "Device in bootloader mode.\n");
1082
1083 /* Allocate and register a linear revmap irq_domain */
1084 data->irqdomain = irq_domain_create_linear(fwnode, irq_count,
1085 &irq_domain_simple_ops,
1086 data);
1087 if (!data->irqdomain) {
1088 dev_err(&rmi_dev->dev, "Failed to create IRQ domain\n");
1089 return -ENOMEM;
1090 }
1091
1092 data->irq_count = irq_count;
1093 data->num_of_irq_regs = (data->irq_count + 7) / 8;
1094
1095 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1096 data->irq_memory = devm_kcalloc(dev, size, 4, GFP_KERNEL);
1097 if (!data->irq_memory) {
1098 dev_err(dev, "Failed to allocate memory for irq masks.\n");
1099 return -ENOMEM;
1100 }
1101
1102 data->irq_status = data->irq_memory + size * 0;
1103 data->fn_irq_bits = data->irq_memory + size * 1;
1104 data->current_irq_mask = data->irq_memory + size * 2;
1105 data->new_irq_mask = data->irq_memory + size * 3;
1106
1107 return retval;
1108 }
1109
rmi_init_functions(struct rmi_driver_data * data)1110 int rmi_init_functions(struct rmi_driver_data *data)
1111 {
1112 struct rmi_device *rmi_dev = data->rmi_dev;
1113 struct device *dev = &rmi_dev->dev;
1114 int irq_count = 0;
1115 int retval;
1116
1117 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
1118 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
1119 if (retval < 0) {
1120 dev_err(dev, "Function creation failed with code %d.\n",
1121 retval);
1122 goto err_destroy_functions;
1123 }
1124
1125 if (!data->f01_container) {
1126 dev_err(dev, "Missing F01 container!\n");
1127 retval = -EINVAL;
1128 goto err_destroy_functions;
1129 }
1130
1131 retval = rmi_read_block(rmi_dev,
1132 data->f01_container->fd.control_base_addr + 1,
1133 data->current_irq_mask, data->num_of_irq_regs);
1134 if (retval < 0) {
1135 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
1136 __func__);
1137 goto err_destroy_functions;
1138 }
1139
1140 return 0;
1141
1142 err_destroy_functions:
1143 rmi_free_function_list(rmi_dev);
1144 return retval;
1145 }
1146
rmi_driver_probe(struct device * dev)1147 static int rmi_driver_probe(struct device *dev)
1148 {
1149 struct rmi_driver *rmi_driver;
1150 struct rmi_driver_data *data;
1151 struct rmi_device_platform_data *pdata;
1152 struct rmi_device *rmi_dev;
1153 int retval;
1154
1155 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
1156 __func__);
1157
1158 if (!rmi_is_physical_device(dev)) {
1159 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
1160 return -ENODEV;
1161 }
1162
1163 rmi_dev = to_rmi_device(dev);
1164 rmi_driver = to_rmi_driver(dev->driver);
1165 rmi_dev->driver = rmi_driver;
1166
1167 pdata = rmi_get_platform_data(rmi_dev);
1168
1169 if (rmi_dev->xport->dev->of_node) {
1170 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1171 if (retval)
1172 return retval;
1173 }
1174
1175 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
1176 if (!data)
1177 return -ENOMEM;
1178
1179 INIT_LIST_HEAD(&data->function_list);
1180 INIT_KFIFO(data->attn_fifo);
1181 data->rmi_dev = rmi_dev;
1182 dev_set_drvdata(&rmi_dev->dev, data);
1183
1184 /*
1185 * Right before a warm boot, the sensor might be in some unusual state,
1186 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1187 * or configuration update. In order to clear the sensor to a known
1188 * state and/or apply any updates, we issue a initial reset to clear any
1189 * previous settings and force it into normal operation.
1190 *
1191 * We have to do this before actually building the PDT because
1192 * the reflash updates (if any) might cause various registers to move
1193 * around.
1194 *
1195 * For a number of reasons, this initial reset may fail to return
1196 * within the specified time, but we'll still be able to bring up the
1197 * driver normally after that failure. This occurs most commonly in
1198 * a cold boot situation (where then firmware takes longer to come up
1199 * than from a warm boot) and the reset_delay_ms in the platform data
1200 * has been set too short to accommodate that. Since the sensor will
1201 * eventually come up and be usable, we don't want to just fail here
1202 * and leave the customer's device unusable. So we warn them, and
1203 * continue processing.
1204 */
1205 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
1206 if (retval < 0)
1207 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1208
1209 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1210 if (retval < 0) {
1211 /*
1212 * we'll print out a warning and continue since
1213 * failure to get the PDT properties is not a cause to fail
1214 */
1215 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1216 PDT_PROPERTIES_LOCATION, retval);
1217 }
1218
1219 mutex_init(&data->irq_mutex);
1220 mutex_init(&data->enabled_mutex);
1221
1222 retval = rmi_probe_interrupts(data);
1223 if (retval)
1224 goto err;
1225
1226 if (rmi_dev->xport->input) {
1227 /*
1228 * The transport driver already has an input device.
1229 * In some cases it is preferable to reuse the transport
1230 * devices input device instead of creating a new one here.
1231 * One example is some HID touchpads report "pass-through"
1232 * button events are not reported by rmi registers.
1233 */
1234 data->input = rmi_dev->xport->input;
1235 } else {
1236 data->input = devm_input_allocate_device(dev);
1237 if (!data->input) {
1238 dev_err(dev, "%s: Failed to allocate input device.\n",
1239 __func__);
1240 retval = -ENOMEM;
1241 goto err;
1242 }
1243 rmi_driver_set_input_params(rmi_dev, data->input);
1244 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1245 "%s/input0", dev_name(dev));
1246 if (!data->input->phys) {
1247 retval = -ENOMEM;
1248 goto err;
1249 }
1250 }
1251
1252 retval = rmi_init_functions(data);
1253 if (retval)
1254 goto err;
1255
1256 retval = rmi_f34_create_sysfs(rmi_dev);
1257 if (retval)
1258 goto err;
1259
1260 if (data->input) {
1261 rmi_driver_set_input_name(rmi_dev, data->input);
1262 if (!rmi_dev->xport->input) {
1263 retval = input_register_device(data->input);
1264 if (retval) {
1265 dev_err(dev, "%s: Failed to register input device.\n",
1266 __func__);
1267 goto err_destroy_functions;
1268 }
1269 }
1270 }
1271
1272 retval = rmi_irq_init(rmi_dev);
1273 if (retval < 0)
1274 goto err_destroy_functions;
1275
1276 if (data->f01_container->dev.driver) {
1277 /* Driver already bound, so enable ATTN now. */
1278 retval = rmi_enable_sensor(rmi_dev);
1279 if (retval)
1280 goto err_disable_irq;
1281 }
1282
1283 return 0;
1284
1285 err_disable_irq:
1286 rmi_disable_irq(rmi_dev, false);
1287 err_destroy_functions:
1288 rmi_free_function_list(rmi_dev);
1289 err:
1290 return retval;
1291 }
1292
1293 static struct rmi_driver rmi_physical_driver = {
1294 .driver = {
1295 .owner = THIS_MODULE,
1296 .name = "rmi4_physical",
1297 .bus = &rmi_bus_type,
1298 .probe = rmi_driver_probe,
1299 .remove = rmi_driver_remove,
1300 },
1301 .reset_handler = rmi_driver_reset_handler,
1302 .clear_irq_bits = rmi_driver_clear_irq_bits,
1303 .set_irq_bits = rmi_driver_set_irq_bits,
1304 .set_input_params = rmi_driver_set_input_params,
1305 };
1306
rmi_is_physical_driver(const struct device_driver * drv)1307 bool rmi_is_physical_driver(const struct device_driver *drv)
1308 {
1309 return drv == &rmi_physical_driver.driver;
1310 }
1311
rmi_register_physical_driver(void)1312 int __init rmi_register_physical_driver(void)
1313 {
1314 int error;
1315
1316 error = driver_register(&rmi_physical_driver.driver);
1317 if (error) {
1318 pr_err("%s: driver register failed, code=%d.\n", __func__,
1319 error);
1320 return error;
1321 }
1322
1323 return 0;
1324 }
1325
rmi_unregister_physical_driver(void)1326 void __exit rmi_unregister_physical_driver(void)
1327 {
1328 driver_unregister(&rmi_physical_driver.driver);
1329 }
1330