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