2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
5 * This driver provides the core support for a single RMI4-based device.
7 * The RMI4 specification can be found here (URL split for line length):
9 * http://www.synaptics.com/sites/default/files/
10 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
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.
17 #include <linux/bitmap.h>
18 #include <linux/delay.h>
20 #include <linux/irq.h>
21 #include <linux/kconfig.h>
23 #include <linux/slab.h>
25 #include <uapi/linux/input.h>
26 #include <linux/rmi.h>
28 #include "rmi_driver.h"
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
35 #define RMI_DEVICE_RESET_CMD 0x01
36 #define DEFAULT_RESET_DELAY_MS 100
38 void rmi_free_function_list(struct rmi_device *rmi_dev)
40 struct rmi_function *fn, *tmp;
41 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
43 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
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;
52 data->f01_container = NULL;
53 data->f34_container = NULL;
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) {
59 rmi_unregister_function(fn);
62 EXPORT_SYMBOL_GPL(rmi_free_function_list);
64 static int reset_one_function(struct rmi_function *fn)
66 struct rmi_function_handler *fh;
69 if (!fn || !fn->dev.driver)
72 fh = to_rmi_function_handler(fn->dev.driver);
74 retval = fh->reset(fn);
76 dev_err(&fn->dev, "Reset failed with code %d.\n",
83 static int configure_one_function(struct rmi_function *fn)
85 struct rmi_function_handler *fh;
88 if (!fn || !fn->dev.driver)
91 fh = to_rmi_function_handler(fn->dev.driver);
93 retval = fh->config(fn);
95 dev_err(&fn->dev, "Config failed with code %d.\n",
102 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
104 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
105 struct rmi_function *entry;
108 list_for_each_entry(entry, &data->function_list, node) {
109 retval = reset_one_function(entry);
117 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
119 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
120 struct rmi_function *entry;
123 list_for_each_entry(entry, &data->function_list, node) {
124 retval = configure_one_function(entry);
132 static void process_one_interrupt(struct rmi_driver_data *data,
133 struct rmi_function *fn)
135 struct rmi_function_handler *fh;
137 if (!fn || !fn->dev.driver)
140 fh = to_rmi_function_handler(fn->dev.driver);
142 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
144 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
145 fh->attention(fn, data->fn_irq_bits);
149 static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
151 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
152 struct device *dev = &rmi_dev->dev;
153 struct rmi_function *entry;
159 if (!rmi_dev->xport->attn_data) {
160 error = rmi_read_block(rmi_dev,
161 data->f01_container->fd.data_base_addr + 1,
162 data->irq_status, data->num_of_irq_regs);
164 dev_err(dev, "Failed to read irqs, code=%d\n", error);
169 mutex_lock(&data->irq_mutex);
170 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
173 * At this point, irq_status has all bits that are set in the
174 * interrupt status register and are enabled.
176 mutex_unlock(&data->irq_mutex);
179 * It would be nice to be able to use irq_chip to handle these
180 * nested IRQs. Unfortunately, most of the current customers for
181 * this driver are using older kernels (3.0.x) that don't support
182 * the features required for that. Once they've shifted to more
183 * recent kernels (say, 3.3 and higher), this should be switched to
186 list_for_each_entry(entry, &data->function_list, node)
187 process_one_interrupt(data, entry);
190 input_sync(data->input);
195 static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
197 struct rmi_device *rmi_dev = dev_id;
200 ret = rmi_process_interrupt_requests(rmi_dev);
202 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
203 "Failed to process interrupt request: %d\n", ret);
208 static int rmi_irq_init(struct rmi_device *rmi_dev)
210 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
211 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
212 int irq_flags = irq_get_trigger_type(pdata->irq);
216 irq_flags = IRQF_TRIGGER_LOW;
218 ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
219 rmi_irq_fn, irq_flags | IRQF_ONESHOT,
220 dev_name(rmi_dev->xport->dev),
223 dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
229 data->enabled = true;
234 static int suspend_one_function(struct rmi_function *fn)
236 struct rmi_function_handler *fh;
239 if (!fn || !fn->dev.driver)
242 fh = to_rmi_function_handler(fn->dev.driver);
244 retval = fh->suspend(fn);
246 dev_err(&fn->dev, "Suspend failed with code %d.\n",
253 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
255 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
256 struct rmi_function *entry;
259 list_for_each_entry(entry, &data->function_list, node) {
260 retval = suspend_one_function(entry);
268 static int resume_one_function(struct rmi_function *fn)
270 struct rmi_function_handler *fh;
273 if (!fn || !fn->dev.driver)
276 fh = to_rmi_function_handler(fn->dev.driver);
278 retval = fh->resume(fn);
280 dev_err(&fn->dev, "Resume failed with code %d.\n",
287 static int rmi_resume_functions(struct rmi_device *rmi_dev)
289 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
290 struct rmi_function *entry;
293 list_for_each_entry(entry, &data->function_list, node) {
294 retval = resume_one_function(entry);
302 int rmi_enable_sensor(struct rmi_device *rmi_dev)
306 retval = rmi_driver_process_config_requests(rmi_dev);
310 return rmi_process_interrupt_requests(rmi_dev);
312 EXPORT_SYMBOL_GPL(rmi_enable_sensor);
315 * rmi_driver_set_input_params - set input device id and other data.
317 * @rmi_dev: Pointer to an RMI device
318 * @input: Pointer to input device
321 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
322 struct input_dev *input)
324 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
325 input->id.vendor = SYNAPTICS_VENDOR_ID;
326 input->id.bustype = BUS_RMI;
330 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
331 struct input_dev *input)
333 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
334 char *device_name = rmi_f01_get_product_ID(data->f01_container);
337 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
338 "Synaptics %s", device_name);
345 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
349 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
350 struct device *dev = &rmi_dev->dev;
352 mutex_lock(&data->irq_mutex);
353 bitmap_or(data->new_irq_mask,
354 data->current_irq_mask, mask, data->irq_count);
356 error = rmi_write_block(rmi_dev,
357 data->f01_container->fd.control_base_addr + 1,
358 data->new_irq_mask, data->num_of_irq_regs);
360 dev_err(dev, "%s: Failed to change enabled interrupts!",
364 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
365 data->num_of_irq_regs);
368 mutex_unlock(&data->irq_mutex);
372 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
376 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
377 struct device *dev = &rmi_dev->dev;
379 mutex_lock(&data->irq_mutex);
380 bitmap_andnot(data->new_irq_mask,
381 data->current_irq_mask, mask, data->irq_count);
383 error = rmi_write_block(rmi_dev,
384 data->f01_container->fd.control_base_addr + 1,
385 data->new_irq_mask, data->num_of_irq_regs);
387 dev_err(dev, "%s: Failed to change enabled interrupts!",
391 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
392 data->num_of_irq_regs);
395 mutex_unlock(&data->irq_mutex);
399 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
401 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
405 * Can get called before the driver is fully ready to deal with
408 if (!data || !data->f01_container) {
409 dev_warn(&rmi_dev->dev,
410 "Not ready to handle reset yet!\n");
414 error = rmi_read_block(rmi_dev,
415 data->f01_container->fd.control_base_addr + 1,
416 data->current_irq_mask, data->num_of_irq_regs);
418 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
423 error = rmi_driver_process_reset_requests(rmi_dev);
427 error = rmi_driver_process_config_requests(rmi_dev);
434 int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
437 u8 buf[RMI_PDT_ENTRY_SIZE];
440 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
442 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
447 entry->page_start = pdt_address & RMI4_PAGE_MASK;
448 entry->query_base_addr = buf[0];
449 entry->command_base_addr = buf[1];
450 entry->control_base_addr = buf[2];
451 entry->data_base_addr = buf[3];
452 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
453 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
454 entry->function_number = buf[5];
458 EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
460 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
461 struct rmi_function_descriptor *fd)
463 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
464 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
465 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
466 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
467 fd->function_number = pdt->function_number;
468 fd->interrupt_source_count = pdt->interrupt_source_count;
469 fd->function_version = pdt->function_version;
472 #define RMI_SCAN_CONTINUE 0
473 #define RMI_SCAN_DONE 1
475 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
479 int (*callback)(struct rmi_device *rmi_dev,
481 const struct pdt_entry *entry))
483 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
484 struct pdt_entry pdt_entry;
485 u16 page_start = RMI4_PAGE_SIZE * page;
486 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
487 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
492 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
493 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
497 if (RMI4_END_OF_PDT(pdt_entry.function_number))
500 retval = callback(rmi_dev, ctx, &pdt_entry);
501 if (retval != RMI_SCAN_CONTINUE)
506 * Count number of empty PDT pages. If a gap of two pages
507 * or more is found, stop scanning.
509 if (addr == pdt_start)
514 return (data->f01_bootloader_mode || *empty_pages >= 2) ?
515 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
518 int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
519 int (*callback)(struct rmi_device *rmi_dev,
520 void *ctx, const struct pdt_entry *entry))
524 int retval = RMI_SCAN_DONE;
526 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
527 retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
529 if (retval != RMI_SCAN_CONTINUE)
533 return retval < 0 ? retval : 0;
535 EXPORT_SYMBOL_GPL(rmi_scan_pdt);
537 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
538 struct rmi_register_descriptor *rdesc)
541 u8 size_presence_reg;
543 int presense_offset = 1;
552 * The first register of the register descriptor is the size of
553 * the register descriptor's presense register.
555 ret = rmi_read(d, addr, &size_presence_reg);
560 if (size_presence_reg < 0 || size_presence_reg > 35)
563 memset(buf, 0, sizeof(buf));
566 * The presence register contains the size of the register structure
567 * and a bitmap which identified which packet registers are present
568 * for this particular register type (ie query, control, or data).
570 ret = rmi_read_block(d, addr, buf, size_presence_reg);
577 rdesc->struct_size = buf[1] | (buf[2] << 8);
579 rdesc->struct_size = buf[0];
582 for (i = presense_offset; i < size_presence_reg; i++) {
583 for (b = 0; b < 8; b++) {
584 if (buf[i] & (0x1 << b))
585 bitmap_set(rdesc->presense_map, map_offset, 1);
590 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
591 RMI_REG_DESC_PRESENSE_BITS);
593 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
594 sizeof(struct rmi_register_desc_item),
596 if (!rdesc->registers)
600 * Allocate a temporary buffer to hold the register structure.
601 * I'm not using devm_kzalloc here since it will not be retained
602 * after exiting this function
604 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
609 * The register structure contains information about every packet
610 * register of this type. This includes the size of the packet
611 * register and a bitmap of all subpackets contained in the packet
614 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
616 goto free_struct_buff;
618 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
619 for (i = 0; i < rdesc->num_registers; i++) {
620 struct rmi_register_desc_item *item = &rdesc->registers[i];
621 int reg_size = struct_buf[offset];
625 reg_size = struct_buf[offset] |
626 (struct_buf[offset + 1] << 8);
631 reg_size = struct_buf[offset] |
632 (struct_buf[offset + 1] << 8) |
633 (struct_buf[offset + 2] << 16) |
634 (struct_buf[offset + 3] << 24);
639 item->reg_size = reg_size;
644 for (b = 0; b < 7; b++) {
645 if (struct_buf[offset] & (0x1 << b))
646 bitmap_set(item->subpacket_map,
650 } while (struct_buf[offset++] & 0x80);
652 item->num_subpackets = bitmap_weight(item->subpacket_map,
653 RMI_REG_DESC_SUBPACKET_BITS);
655 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
656 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
657 item->reg, item->reg_size, item->num_subpackets);
659 reg = find_next_bit(rdesc->presense_map,
660 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
667 EXPORT_SYMBOL_GPL(rmi_read_register_desc);
669 const struct rmi_register_desc_item *rmi_get_register_desc_item(
670 struct rmi_register_descriptor *rdesc, u16 reg)
672 const struct rmi_register_desc_item *item;
675 for (i = 0; i < rdesc->num_registers; i++) {
676 item = &rdesc->registers[i];
677 if (item->reg == reg)
683 EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
685 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
687 const struct rmi_register_desc_item *item;
691 for (i = 0; i < rdesc->num_registers; i++) {
692 item = &rdesc->registers[i];
693 size += item->reg_size;
697 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
699 /* Compute the register offset relative to the base address */
700 int rmi_register_desc_calc_reg_offset(
701 struct rmi_register_descriptor *rdesc, u16 reg)
703 const struct rmi_register_desc_item *item;
707 for (i = 0; i < rdesc->num_registers; i++) {
708 item = &rdesc->registers[i];
709 if (item->reg == reg)
715 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
717 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
720 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
721 subpacket) == subpacket;
724 /* Indicates that flash programming is enabled (bootloader mode). */
725 #define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
728 * Given the PDT entry for F01, read the device status register to determine
729 * if we're stuck in bootloader mode or not.
732 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
733 const struct pdt_entry *pdt)
738 error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
741 dev_err(&rmi_dev->dev,
742 "Failed to read device status: %d.\n", error);
746 return RMI_F01_STATUS_BOOTLOADER(device_status);
749 static int rmi_count_irqs(struct rmi_device *rmi_dev,
750 void *ctx, const struct pdt_entry *pdt)
752 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
753 int *irq_count = ctx;
755 *irq_count += pdt->interrupt_source_count;
756 if (pdt->function_number == 0x01)
757 data->f01_bootloader_mode =
758 rmi_check_bootloader_mode(rmi_dev, pdt);
760 return RMI_SCAN_CONTINUE;
763 int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
764 const struct pdt_entry *pdt)
768 if (pdt->function_number == 0x01) {
769 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
770 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
771 const struct rmi_device_platform_data *pdata =
772 rmi_get_platform_data(rmi_dev);
774 if (rmi_dev->xport->ops->reset) {
775 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
780 return RMI_SCAN_DONE;
783 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
784 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
786 dev_err(&rmi_dev->dev,
787 "Initial reset failed. Code = %d.\n", error);
791 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
793 return RMI_SCAN_DONE;
796 /* F01 should always be on page 0. If we don't find it there, fail. */
797 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
799 EXPORT_SYMBOL_GPL(rmi_initial_reset);
801 static int rmi_create_function(struct rmi_device *rmi_dev,
802 void *ctx, const struct pdt_entry *pdt)
804 struct device *dev = &rmi_dev->dev;
805 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
806 int *current_irq_count = ctx;
807 struct rmi_function *fn;
811 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
812 pdt->function_number);
814 fn = kzalloc(sizeof(struct rmi_function) +
815 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
818 dev_err(dev, "Failed to allocate memory for F%02X\n",
819 pdt->function_number);
823 INIT_LIST_HEAD(&fn->node);
824 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
826 fn->rmi_dev = rmi_dev;
828 fn->num_of_irqs = pdt->interrupt_source_count;
829 fn->irq_pos = *current_irq_count;
830 *current_irq_count += fn->num_of_irqs;
832 for (i = 0; i < fn->num_of_irqs; i++)
833 set_bit(fn->irq_pos + i, fn->irq_mask);
835 error = rmi_register_function(fn);
839 if (pdt->function_number == 0x01)
840 data->f01_container = fn;
841 else if (pdt->function_number == 0x34)
842 data->f34_container = fn;
844 list_add_tail(&fn->node, &data->function_list);
846 return RMI_SCAN_CONTINUE;
849 put_device(&fn->dev);
853 void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
855 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
856 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
857 int irq = pdata->irq;
861 mutex_lock(&data->enabled_mutex);
867 data->enabled = true;
868 if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
869 retval = disable_irq_wake(irq);
871 dev_warn(&rmi_dev->dev,
872 "Failed to disable irq for wake: %d\n",
877 * Call rmi_process_interrupt_requests() after enabling irq,
878 * otherwise we may lose interrupt on edge-triggered systems.
880 irq_flags = irq_get_trigger_type(pdata->irq);
881 if (irq_flags & IRQ_TYPE_EDGE_BOTH)
882 rmi_process_interrupt_requests(rmi_dev);
885 mutex_unlock(&data->enabled_mutex);
888 void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
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;
895 mutex_lock(&data->enabled_mutex);
900 data->enabled = false;
902 if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
903 retval = enable_irq_wake(irq);
905 dev_warn(&rmi_dev->dev,
906 "Failed to enable irq for wake: %d\n",
911 mutex_unlock(&data->enabled_mutex);
914 int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
918 retval = rmi_suspend_functions(rmi_dev);
920 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
923 rmi_disable_irq(rmi_dev, enable_wake);
926 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
928 int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
932 rmi_enable_irq(rmi_dev, clear_wake);
934 retval = rmi_resume_functions(rmi_dev);
936 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
941 EXPORT_SYMBOL_GPL(rmi_driver_resume);
943 static int rmi_driver_remove(struct device *dev)
945 struct rmi_device *rmi_dev = to_rmi_device(dev);
947 rmi_disable_irq(rmi_dev, false);
949 rmi_f34_remove_sysfs(rmi_dev);
950 rmi_free_function_list(rmi_dev);
956 static int rmi_driver_of_probe(struct device *dev,
957 struct rmi_device_platform_data *pdata)
961 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
962 "syna,reset-delay-ms", 1);
969 static inline int rmi_driver_of_probe(struct device *dev,
970 struct rmi_device_platform_data *pdata)
976 int rmi_probe_interrupts(struct rmi_driver_data *data)
978 struct rmi_device *rmi_dev = data->rmi_dev;
979 struct device *dev = &rmi_dev->dev;
985 * We need to count the IRQs and allocate their storage before scanning
986 * the PDT and creating the function entries, because adding a new
987 * function can trigger events that result in the IRQ related storage
990 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
992 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
994 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
998 if (data->f01_bootloader_mode)
999 dev_warn(&rmi_dev->dev, "Device in bootloader mode.\n");
1001 data->irq_count = irq_count;
1002 data->num_of_irq_regs = (data->irq_count + 7) / 8;
1004 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1005 data->irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
1006 if (!data->irq_memory) {
1007 dev_err(dev, "Failed to allocate memory for irq masks.\n");
1011 data->irq_status = data->irq_memory + size * 0;
1012 data->fn_irq_bits = data->irq_memory + size * 1;
1013 data->current_irq_mask = data->irq_memory + size * 2;
1014 data->new_irq_mask = data->irq_memory + size * 3;
1018 EXPORT_SYMBOL_GPL(rmi_probe_interrupts);
1020 int rmi_init_functions(struct rmi_driver_data *data)
1022 struct rmi_device *rmi_dev = data->rmi_dev;
1023 struct device *dev = &rmi_dev->dev;
1028 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
1029 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
1031 dev_err(dev, "Function creation failed with code %d.\n",
1033 goto err_destroy_functions;
1036 if (!data->f01_container) {
1037 dev_err(dev, "Missing F01 container!\n");
1039 goto err_destroy_functions;
1042 retval = rmi_read_block(rmi_dev,
1043 data->f01_container->fd.control_base_addr + 1,
1044 data->current_irq_mask, data->num_of_irq_regs);
1046 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
1048 goto err_destroy_functions;
1053 err_destroy_functions:
1054 rmi_free_function_list(rmi_dev);
1057 EXPORT_SYMBOL_GPL(rmi_init_functions);
1059 static int rmi_driver_probe(struct device *dev)
1061 struct rmi_driver *rmi_driver;
1062 struct rmi_driver_data *data;
1063 struct rmi_device_platform_data *pdata;
1064 struct rmi_device *rmi_dev;
1067 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
1070 if (!rmi_is_physical_device(dev)) {
1071 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
1075 rmi_dev = to_rmi_device(dev);
1076 rmi_driver = to_rmi_driver(dev->driver);
1077 rmi_dev->driver = rmi_driver;
1079 pdata = rmi_get_platform_data(rmi_dev);
1081 if (rmi_dev->xport->dev->of_node) {
1082 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1087 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
1091 INIT_LIST_HEAD(&data->function_list);
1092 data->rmi_dev = rmi_dev;
1093 dev_set_drvdata(&rmi_dev->dev, data);
1096 * Right before a warm boot, the sensor might be in some unusual state,
1097 * such as F54 diagnostics, or F34 bootloader mode after a firmware
1098 * or configuration update. In order to clear the sensor to a known
1099 * state and/or apply any updates, we issue a initial reset to clear any
1100 * previous settings and force it into normal operation.
1102 * We have to do this before actually building the PDT because
1103 * the reflash updates (if any) might cause various registers to move
1106 * For a number of reasons, this initial reset may fail to return
1107 * within the specified time, but we'll still be able to bring up the
1108 * driver normally after that failure. This occurs most commonly in
1109 * a cold boot situation (where then firmware takes longer to come up
1110 * than from a warm boot) and the reset_delay_ms in the platform data
1111 * has been set too short to accommodate that. Since the sensor will
1112 * eventually come up and be usable, we don't want to just fail here
1113 * and leave the customer's device unusable. So we warn them, and
1114 * continue processing.
1116 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
1118 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1120 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1123 * we'll print out a warning and continue since
1124 * failure to get the PDT properties is not a cause to fail
1126 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1127 PDT_PROPERTIES_LOCATION, retval);
1130 mutex_init(&data->irq_mutex);
1131 mutex_init(&data->enabled_mutex);
1133 retval = rmi_probe_interrupts(data);
1137 if (rmi_dev->xport->input) {
1139 * The transport driver already has an input device.
1140 * In some cases it is preferable to reuse the transport
1141 * devices input device instead of creating a new one here.
1142 * One example is some HID touchpads report "pass-through"
1143 * button events are not reported by rmi registers.
1145 data->input = rmi_dev->xport->input;
1147 data->input = devm_input_allocate_device(dev);
1149 dev_err(dev, "%s: Failed to allocate input device.\n",
1154 rmi_driver_set_input_params(rmi_dev, data->input);
1155 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1156 "%s/input0", dev_name(dev));
1159 retval = rmi_init_functions(data);
1163 retval = rmi_f34_create_sysfs(rmi_dev);
1168 rmi_driver_set_input_name(rmi_dev, data->input);
1169 if (!rmi_dev->xport->input) {
1170 if (input_register_device(data->input)) {
1171 dev_err(dev, "%s: Failed to register input device.\n",
1173 goto err_destroy_functions;
1178 retval = rmi_irq_init(rmi_dev);
1180 goto err_destroy_functions;
1182 if (data->f01_container->dev.driver)
1183 /* Driver already bound, so enable ATTN now. */
1184 return rmi_enable_sensor(rmi_dev);
1188 err_destroy_functions:
1189 rmi_free_function_list(rmi_dev);
1191 return retval < 0 ? retval : 0;
1194 static struct rmi_driver rmi_physical_driver = {
1196 .owner = THIS_MODULE,
1197 .name = "rmi4_physical",
1198 .bus = &rmi_bus_type,
1199 .probe = rmi_driver_probe,
1200 .remove = rmi_driver_remove,
1202 .reset_handler = rmi_driver_reset_handler,
1203 .clear_irq_bits = rmi_driver_clear_irq_bits,
1204 .set_irq_bits = rmi_driver_set_irq_bits,
1205 .set_input_params = rmi_driver_set_input_params,
1208 bool rmi_is_physical_driver(struct device_driver *drv)
1210 return drv == &rmi_physical_driver.driver;
1213 int __init rmi_register_physical_driver(void)
1217 error = driver_register(&rmi_physical_driver.driver);
1219 pr_err("%s: driver register failed, code=%d.\n", __func__,
1227 void __exit rmi_unregister_physical_driver(void)
1229 driver_unregister(&rmi_physical_driver.driver);