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