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/kconfig.h>
22 #include <linux/slab.h>
24 #include <uapi/linux/input.h>
25 #include <linux/rmi.h>
27 #include "rmi_driver.h"
29 #define HAS_NONSTANDARD_PDT_MASK 0x40
30 #define RMI4_MAX_PAGE 0xff
31 #define RMI4_PAGE_SIZE 0x100
32 #define RMI4_PAGE_MASK 0xFF00
34 #define RMI_DEVICE_RESET_CMD 0x01
35 #define DEFAULT_RESET_DELAY_MS 100
37 static void rmi_free_function_list(struct rmi_device *rmi_dev)
39 struct rmi_function *fn, *tmp;
40 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
42 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
44 data->f01_container = NULL;
46 /* Doing it in the reverse order so F01 will be removed last */
47 list_for_each_entry_safe_reverse(fn, tmp,
48 &data->function_list, node) {
50 rmi_unregister_function(fn);
54 static int reset_one_function(struct rmi_function *fn)
56 struct rmi_function_handler *fh;
59 if (!fn || !fn->dev.driver)
62 fh = to_rmi_function_handler(fn->dev.driver);
64 retval = fh->reset(fn);
66 dev_err(&fn->dev, "Reset failed with code %d.\n",
73 static int configure_one_function(struct rmi_function *fn)
75 struct rmi_function_handler *fh;
78 if (!fn || !fn->dev.driver)
81 fh = to_rmi_function_handler(fn->dev.driver);
83 retval = fh->config(fn);
85 dev_err(&fn->dev, "Config failed with code %d.\n",
92 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
94 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
95 struct rmi_function *entry;
98 list_for_each_entry(entry, &data->function_list, node) {
99 retval = reset_one_function(entry);
107 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
109 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
110 struct rmi_function *entry;
113 list_for_each_entry(entry, &data->function_list, node) {
114 retval = configure_one_function(entry);
122 static void process_one_interrupt(struct rmi_driver_data *data,
123 struct rmi_function *fn)
125 struct rmi_function_handler *fh;
127 if (!fn || !fn->dev.driver)
130 fh = to_rmi_function_handler(fn->dev.driver);
132 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
134 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
135 fh->attention(fn, data->fn_irq_bits);
139 int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
141 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
142 struct device *dev = &rmi_dev->dev;
143 struct rmi_function *entry;
149 if (!rmi_dev->xport->attn_data) {
150 error = rmi_read_block(rmi_dev,
151 data->f01_container->fd.data_base_addr + 1,
152 data->irq_status, data->num_of_irq_regs);
154 dev_err(dev, "Failed to read irqs, code=%d\n", error);
159 mutex_lock(&data->irq_mutex);
160 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
163 * At this point, irq_status has all bits that are set in the
164 * interrupt status register and are enabled.
166 mutex_unlock(&data->irq_mutex);
169 * It would be nice to be able to use irq_chip to handle these
170 * nested IRQs. Unfortunately, most of the current customers for
171 * this driver are using older kernels (3.0.x) that don't support
172 * the features required for that. Once they've shifted to more
173 * recent kernels (say, 3.3 and higher), this should be switched to
176 list_for_each_entry(entry, &data->function_list, node)
177 process_one_interrupt(data, entry);
180 input_sync(data->input);
184 EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
186 static int suspend_one_function(struct rmi_function *fn)
188 struct rmi_function_handler *fh;
191 if (!fn || !fn->dev.driver)
194 fh = to_rmi_function_handler(fn->dev.driver);
196 retval = fh->suspend(fn);
198 dev_err(&fn->dev, "Suspend failed with code %d.\n",
205 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
207 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
208 struct rmi_function *entry;
211 list_for_each_entry(entry, &data->function_list, node) {
212 retval = suspend_one_function(entry);
220 static int resume_one_function(struct rmi_function *fn)
222 struct rmi_function_handler *fh;
225 if (!fn || !fn->dev.driver)
228 fh = to_rmi_function_handler(fn->dev.driver);
230 retval = fh->resume(fn);
232 dev_err(&fn->dev, "Resume failed with code %d.\n",
239 static int rmi_resume_functions(struct rmi_device *rmi_dev)
241 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
242 struct rmi_function *entry;
245 list_for_each_entry(entry, &data->function_list, node) {
246 retval = resume_one_function(entry);
254 static int enable_sensor(struct rmi_device *rmi_dev)
258 retval = rmi_driver_process_config_requests(rmi_dev);
262 return rmi_process_interrupt_requests(rmi_dev);
266 * rmi_driver_set_input_params - set input device id and other data.
268 * @rmi_dev: Pointer to an RMI device
269 * @input: Pointer to input device
272 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
273 struct input_dev *input)
275 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
276 input->id.vendor = SYNAPTICS_VENDOR_ID;
277 input->id.bustype = BUS_RMI;
281 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
282 struct input_dev *input)
284 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
285 char *device_name = rmi_f01_get_product_ID(data->f01_container);
288 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
289 "Synaptics %s", device_name);
296 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
300 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
301 struct device *dev = &rmi_dev->dev;
303 mutex_lock(&data->irq_mutex);
304 bitmap_or(data->new_irq_mask,
305 data->current_irq_mask, mask, data->irq_count);
307 error = rmi_write_block(rmi_dev,
308 data->f01_container->fd.control_base_addr + 1,
309 data->new_irq_mask, data->num_of_irq_regs);
311 dev_err(dev, "%s: Failed to change enabled interrupts!",
315 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
316 data->num_of_irq_regs);
319 mutex_unlock(&data->irq_mutex);
323 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
327 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
328 struct device *dev = &rmi_dev->dev;
330 mutex_lock(&data->irq_mutex);
331 bitmap_andnot(data->new_irq_mask,
332 data->current_irq_mask, mask, data->irq_count);
334 error = rmi_write_block(rmi_dev,
335 data->f01_container->fd.control_base_addr + 1,
336 data->new_irq_mask, data->num_of_irq_regs);
338 dev_err(dev, "%s: Failed to change enabled interrupts!",
342 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
343 data->num_of_irq_regs);
346 mutex_unlock(&data->irq_mutex);
350 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
352 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
356 * Can get called before the driver is fully ready to deal with
359 if (!data || !data->f01_container) {
360 dev_warn(&rmi_dev->dev,
361 "Not ready to handle reset yet!\n");
365 error = rmi_read_block(rmi_dev,
366 data->f01_container->fd.control_base_addr + 1,
367 data->current_irq_mask, data->num_of_irq_regs);
369 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
374 error = rmi_driver_process_reset_requests(rmi_dev);
378 error = rmi_driver_process_config_requests(rmi_dev);
385 int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
388 u8 buf[RMI_PDT_ENTRY_SIZE];
391 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
393 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
398 entry->page_start = pdt_address & RMI4_PAGE_MASK;
399 entry->query_base_addr = buf[0];
400 entry->command_base_addr = buf[1];
401 entry->control_base_addr = buf[2];
402 entry->data_base_addr = buf[3];
403 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
404 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
405 entry->function_number = buf[5];
409 EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
411 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
412 struct rmi_function_descriptor *fd)
414 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
415 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
416 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
417 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
418 fd->function_number = pdt->function_number;
419 fd->interrupt_source_count = pdt->interrupt_source_count;
420 fd->function_version = pdt->function_version;
423 #define RMI_SCAN_CONTINUE 0
424 #define RMI_SCAN_DONE 1
426 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
430 int (*callback)(struct rmi_device *rmi_dev,
432 const struct pdt_entry *entry))
434 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
435 struct pdt_entry pdt_entry;
436 u16 page_start = RMI4_PAGE_SIZE * page;
437 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
438 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
443 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
444 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
448 if (RMI4_END_OF_PDT(pdt_entry.function_number))
451 retval = callback(rmi_dev, ctx, &pdt_entry);
452 if (retval != RMI_SCAN_CONTINUE)
457 * Count number of empty PDT pages. If a gap of two pages
458 * or more is found, stop scanning.
460 if (addr == pdt_start)
465 return (data->f01_bootloader_mode || *empty_pages >= 2) ?
466 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
469 static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
470 int (*callback)(struct rmi_device *rmi_dev,
472 const struct pdt_entry *entry))
476 int retval = RMI_SCAN_DONE;
478 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
479 retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
481 if (retval != RMI_SCAN_CONTINUE)
485 return retval < 0 ? retval : 0;
488 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
489 struct rmi_register_descriptor *rdesc)
492 u8 size_presence_reg;
494 int presense_offset = 1;
503 * The first register of the register descriptor is the size of
504 * the register descriptor's presense register.
506 ret = rmi_read(d, addr, &size_presence_reg);
511 if (size_presence_reg < 0 || size_presence_reg > 35)
514 memset(buf, 0, sizeof(buf));
517 * The presence register contains the size of the register structure
518 * and a bitmap which identified which packet registers are present
519 * for this particular register type (ie query, control, or data).
521 ret = rmi_read_block(d, addr, buf, size_presence_reg);
528 rdesc->struct_size = buf[1] | (buf[2] << 8);
530 rdesc->struct_size = buf[0];
533 for (i = presense_offset; i < size_presence_reg; i++) {
534 for (b = 0; b < 8; b++) {
535 if (buf[i] & (0x1 << b))
536 bitmap_set(rdesc->presense_map, map_offset, 1);
541 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
542 RMI_REG_DESC_PRESENSE_BITS);
544 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
545 sizeof(struct rmi_register_desc_item),
547 if (!rdesc->registers)
551 * Allocate a temporary buffer to hold the register structure.
552 * I'm not using devm_kzalloc here since it will not be retained
553 * after exiting this function
555 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
560 * The register structure contains information about every packet
561 * register of this type. This includes the size of the packet
562 * register and a bitmap of all subpackets contained in the packet
565 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
567 goto free_struct_buff;
569 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
570 for (i = 0; i < rdesc->num_registers; i++) {
571 struct rmi_register_desc_item *item = &rdesc->registers[i];
572 int reg_size = struct_buf[offset];
576 reg_size = struct_buf[offset] |
577 (struct_buf[offset + 1] << 8);
582 reg_size = struct_buf[offset] |
583 (struct_buf[offset + 1] << 8) |
584 (struct_buf[offset + 2] << 16) |
585 (struct_buf[offset + 3] << 24);
590 item->reg_size = reg_size;
595 for (b = 0; b < 7; b++) {
596 if (struct_buf[offset] & (0x1 << b))
597 bitmap_set(item->subpacket_map,
601 } while (struct_buf[offset++] & 0x80);
603 item->num_subpackets = bitmap_weight(item->subpacket_map,
604 RMI_REG_DESC_SUBPACKET_BITS);
606 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
607 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
608 item->reg, item->reg_size, item->num_subpackets);
610 reg = find_next_bit(rdesc->presense_map,
611 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
618 EXPORT_SYMBOL_GPL(rmi_read_register_desc);
620 const struct rmi_register_desc_item *rmi_get_register_desc_item(
621 struct rmi_register_descriptor *rdesc, u16 reg)
623 const struct rmi_register_desc_item *item;
626 for (i = 0; i < rdesc->num_registers; i++) {
627 item = &rdesc->registers[i];
628 if (item->reg == reg)
634 EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
636 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
638 const struct rmi_register_desc_item *item;
642 for (i = 0; i < rdesc->num_registers; i++) {
643 item = &rdesc->registers[i];
644 size += item->reg_size;
648 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
650 /* Compute the register offset relative to the base address */
651 int rmi_register_desc_calc_reg_offset(
652 struct rmi_register_descriptor *rdesc, u16 reg)
654 const struct rmi_register_desc_item *item;
658 for (i = 0; i < rdesc->num_registers; i++) {
659 item = &rdesc->registers[i];
660 if (item->reg == reg)
666 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
668 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
671 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
672 subpacket) == subpacket;
675 /* Indicates that flash programming is enabled (bootloader mode). */
676 #define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
679 * Given the PDT entry for F01, read the device status register to determine
680 * if we're stuck in bootloader mode or not.
683 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
684 const struct pdt_entry *pdt)
689 error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
692 dev_err(&rmi_dev->dev,
693 "Failed to read device status: %d.\n", error);
697 return RMI_F01_STATUS_BOOTLOADER(device_status);
700 static int rmi_count_irqs(struct rmi_device *rmi_dev,
701 void *ctx, const struct pdt_entry *pdt)
703 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
704 int *irq_count = ctx;
706 *irq_count += pdt->interrupt_source_count;
707 if (pdt->function_number == 0x01) {
708 data->f01_bootloader_mode =
709 rmi_check_bootloader_mode(rmi_dev, pdt);
710 if (data->f01_bootloader_mode)
711 dev_warn(&rmi_dev->dev,
712 "WARNING: RMI4 device is in bootloader mode!\n");
715 return RMI_SCAN_CONTINUE;
718 static int rmi_initial_reset(struct rmi_device *rmi_dev,
719 void *ctx, const struct pdt_entry *pdt)
723 if (pdt->function_number == 0x01) {
724 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
725 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
726 const struct rmi_device_platform_data *pdata =
727 rmi_get_platform_data(rmi_dev);
729 if (rmi_dev->xport->ops->reset) {
730 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
735 return RMI_SCAN_DONE;
738 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
739 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
741 dev_err(&rmi_dev->dev,
742 "Initial reset failed. Code = %d.\n", error);
746 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
748 return RMI_SCAN_DONE;
751 /* F01 should always be on page 0. If we don't find it there, fail. */
752 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
755 static int rmi_create_function(struct rmi_device *rmi_dev,
756 void *ctx, const struct pdt_entry *pdt)
758 struct device *dev = &rmi_dev->dev;
759 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
760 int *current_irq_count = ctx;
761 struct rmi_function *fn;
765 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
766 pdt->function_number);
768 fn = kzalloc(sizeof(struct rmi_function) +
769 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
772 dev_err(dev, "Failed to allocate memory for F%02X\n",
773 pdt->function_number);
777 INIT_LIST_HEAD(&fn->node);
778 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
780 fn->rmi_dev = rmi_dev;
782 fn->num_of_irqs = pdt->interrupt_source_count;
783 fn->irq_pos = *current_irq_count;
784 *current_irq_count += fn->num_of_irqs;
786 for (i = 0; i < fn->num_of_irqs; i++)
787 set_bit(fn->irq_pos + i, fn->irq_mask);
789 error = rmi_register_function(fn);
793 if (pdt->function_number == 0x01)
794 data->f01_container = fn;
796 list_add_tail(&fn->node, &data->function_list);
798 return RMI_SCAN_CONTINUE;
801 put_device(&fn->dev);
805 int rmi_driver_suspend(struct rmi_device *rmi_dev)
809 retval = rmi_suspend_functions(rmi_dev);
811 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
816 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
818 int rmi_driver_resume(struct rmi_device *rmi_dev)
822 retval = rmi_resume_functions(rmi_dev);
824 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
829 EXPORT_SYMBOL_GPL(rmi_driver_resume);
831 static int rmi_driver_remove(struct device *dev)
833 struct rmi_device *rmi_dev = to_rmi_device(dev);
835 rmi_free_function_list(rmi_dev);
841 static int rmi_driver_of_probe(struct device *dev,
842 struct rmi_device_platform_data *pdata)
846 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
847 "syna,reset-delay-ms", 1);
854 static inline int rmi_driver_of_probe(struct device *dev,
855 struct rmi_device_platform_data *pdata)
861 static int rmi_probe_interrupts(struct rmi_driver_data *data)
863 struct rmi_device *rmi_dev = data->rmi_dev;
864 struct device *dev = &rmi_dev->dev;
871 * We need to count the IRQs and allocate their storage before scanning
872 * the PDT and creating the function entries, because adding a new
873 * function can trigger events that result in the IRQ related storage
876 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
878 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
880 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
883 data->irq_count = irq_count;
884 data->num_of_irq_regs = (data->irq_count + 7) / 8;
886 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
887 irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
889 dev_err(dev, "Failed to allocate memory for irq masks.\n");
893 data->irq_status = irq_memory + size * 0;
894 data->fn_irq_bits = irq_memory + size * 1;
895 data->current_irq_mask = irq_memory + size * 2;
896 data->new_irq_mask = irq_memory + size * 3;
901 static int rmi_init_functions(struct rmi_driver_data *data)
903 struct rmi_device *rmi_dev = data->rmi_dev;
904 struct device *dev = &rmi_dev->dev;
909 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
910 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
912 dev_err(dev, "Function creation failed with code %d.\n",
914 goto err_destroy_functions;
917 if (!data->f01_container) {
918 dev_err(dev, "Missing F01 container!\n");
920 goto err_destroy_functions;
923 retval = rmi_read_block(rmi_dev,
924 data->f01_container->fd.control_base_addr + 1,
925 data->current_irq_mask, data->num_of_irq_regs);
927 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
929 goto err_destroy_functions;
934 err_destroy_functions:
935 rmi_free_function_list(rmi_dev);
939 static int rmi_driver_probe(struct device *dev)
941 struct rmi_driver *rmi_driver;
942 struct rmi_driver_data *data;
943 struct rmi_device_platform_data *pdata;
944 struct rmi_device *rmi_dev;
947 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
950 if (!rmi_is_physical_device(dev)) {
951 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
955 rmi_dev = to_rmi_device(dev);
956 rmi_driver = to_rmi_driver(dev->driver);
957 rmi_dev->driver = rmi_driver;
959 pdata = rmi_get_platform_data(rmi_dev);
961 if (rmi_dev->xport->dev->of_node) {
962 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
967 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
971 INIT_LIST_HEAD(&data->function_list);
972 data->rmi_dev = rmi_dev;
973 dev_set_drvdata(&rmi_dev->dev, data);
976 * Right before a warm boot, the sensor might be in some unusual state,
977 * such as F54 diagnostics, or F34 bootloader mode after a firmware
978 * or configuration update. In order to clear the sensor to a known
979 * state and/or apply any updates, we issue a initial reset to clear any
980 * previous settings and force it into normal operation.
982 * We have to do this before actually building the PDT because
983 * the reflash updates (if any) might cause various registers to move
986 * For a number of reasons, this initial reset may fail to return
987 * within the specified time, but we'll still be able to bring up the
988 * driver normally after that failure. This occurs most commonly in
989 * a cold boot situation (where then firmware takes longer to come up
990 * than from a warm boot) and the reset_delay_ms in the platform data
991 * has been set too short to accommodate that. Since the sensor will
992 * eventually come up and be usable, we don't want to just fail here
993 * and leave the customer's device unusable. So we warn them, and
994 * continue processing.
996 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
998 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1000 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1003 * we'll print out a warning and continue since
1004 * failure to get the PDT properties is not a cause to fail
1006 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1007 PDT_PROPERTIES_LOCATION, retval);
1010 mutex_init(&data->irq_mutex);
1012 retval = rmi_probe_interrupts(data);
1016 if (rmi_dev->xport->input) {
1018 * The transport driver already has an input device.
1019 * In some cases it is preferable to reuse the transport
1020 * devices input device instead of creating a new one here.
1021 * One example is some HID touchpads report "pass-through"
1022 * button events are not reported by rmi registers.
1024 data->input = rmi_dev->xport->input;
1026 data->input = devm_input_allocate_device(dev);
1028 dev_err(dev, "%s: Failed to allocate input device.\n",
1033 rmi_driver_set_input_params(rmi_dev, data->input);
1034 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1035 "%s/input0", dev_name(dev));
1038 retval = rmi_init_functions(data);
1043 rmi_driver_set_input_name(rmi_dev, data->input);
1044 if (!rmi_dev->xport->input) {
1045 if (input_register_device(data->input)) {
1046 dev_err(dev, "%s: Failed to register input device.\n",
1048 goto err_destroy_functions;
1053 if (data->f01_container->dev.driver)
1054 /* Driver already bound, so enable ATTN now. */
1055 return enable_sensor(rmi_dev);
1059 err_destroy_functions:
1060 rmi_free_function_list(rmi_dev);
1062 return retval < 0 ? retval : 0;
1065 static struct rmi_driver rmi_physical_driver = {
1067 .owner = THIS_MODULE,
1068 .name = "rmi4_physical",
1069 .bus = &rmi_bus_type,
1070 .probe = rmi_driver_probe,
1071 .remove = rmi_driver_remove,
1073 .reset_handler = rmi_driver_reset_handler,
1074 .clear_irq_bits = rmi_driver_clear_irq_bits,
1075 .set_irq_bits = rmi_driver_set_irq_bits,
1076 .set_input_params = rmi_driver_set_input_params,
1079 bool rmi_is_physical_driver(struct device_driver *drv)
1081 return drv == &rmi_physical_driver.driver;
1084 int __init rmi_register_physical_driver(void)
1088 error = driver_register(&rmi_physical_driver.driver);
1090 pr_err("%s: driver register failed, code=%d.\n", __func__,
1098 void __exit rmi_unregister_physical_driver(void)
1100 driver_unregister(&rmi_physical_driver.driver);