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