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