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