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