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