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