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