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>
21 #include <linux/slab.h>
23 #include <uapi/linux/input.h>
24 #include <linux/rmi.h>
26 #include "rmi_driver.h"
28 #define HAS_NONSTANDARD_PDT_MASK 0x40
29 #define RMI4_MAX_PAGE 0xff
30 #define RMI4_PAGE_SIZE 0x100
31 #define RMI4_PAGE_MASK 0xFF00
33 #define RMI_DEVICE_RESET_CMD 0x01
34 #define DEFAULT_RESET_DELAY_MS 100
36 static void rmi_free_function_list(struct rmi_device *rmi_dev)
38 struct rmi_function *fn, *tmp;
39 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
41 data->f01_container = NULL;
43 /* Doing it in the reverse order so F01 will be removed last */
44 list_for_each_entry_safe_reverse(fn, tmp,
45 &data->function_list, node) {
47 rmi_unregister_function(fn);
51 static int reset_one_function(struct rmi_function *fn)
53 struct rmi_function_handler *fh;
56 if (!fn || !fn->dev.driver)
59 fh = to_rmi_function_handler(fn->dev.driver);
61 retval = fh->reset(fn);
63 dev_err(&fn->dev, "Reset failed with code %d.\n",
70 static int configure_one_function(struct rmi_function *fn)
72 struct rmi_function_handler *fh;
75 if (!fn || !fn->dev.driver)
78 fh = to_rmi_function_handler(fn->dev.driver);
80 retval = fh->config(fn);
82 dev_err(&fn->dev, "Config failed with code %d.\n",
89 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
91 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
92 struct rmi_function *entry;
95 list_for_each_entry(entry, &data->function_list, node) {
96 retval = reset_one_function(entry);
104 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
106 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
107 struct rmi_function *entry;
110 list_for_each_entry(entry, &data->function_list, node) {
111 retval = configure_one_function(entry);
119 static void process_one_interrupt(struct rmi_driver_data *data,
120 struct rmi_function *fn)
122 struct rmi_function_handler *fh;
124 if (!fn || !fn->dev.driver)
127 fh = to_rmi_function_handler(fn->dev.driver);
129 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
131 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
132 fh->attention(fn, data->fn_irq_bits);
136 int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
138 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
139 struct device *dev = &rmi_dev->dev;
140 struct rmi_function *entry;
146 if (!rmi_dev->xport->attn_data) {
147 error = rmi_read_block(rmi_dev,
148 data->f01_container->fd.data_base_addr + 1,
149 data->irq_status, data->num_of_irq_regs);
151 dev_err(dev, "Failed to read irqs, code=%d\n", error);
156 mutex_lock(&data->irq_mutex);
157 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
160 * At this point, irq_status has all bits that are set in the
161 * interrupt status register and are enabled.
163 mutex_unlock(&data->irq_mutex);
166 * It would be nice to be able to use irq_chip to handle these
167 * nested IRQs. Unfortunately, most of the current customers for
168 * this driver are using older kernels (3.0.x) that don't support
169 * the features required for that. Once they've shifted to more
170 * recent kernels (say, 3.3 and higher), this should be switched to
173 list_for_each_entry(entry, &data->function_list, node)
174 process_one_interrupt(data, entry);
177 input_sync(data->input);
181 EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
183 static int suspend_one_function(struct rmi_function *fn)
185 struct rmi_function_handler *fh;
188 if (!fn || !fn->dev.driver)
191 fh = to_rmi_function_handler(fn->dev.driver);
193 retval = fh->suspend(fn);
195 dev_err(&fn->dev, "Suspend failed with code %d.\n",
202 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
204 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
205 struct rmi_function *entry;
208 list_for_each_entry(entry, &data->function_list, node) {
209 retval = suspend_one_function(entry);
217 static int resume_one_function(struct rmi_function *fn)
219 struct rmi_function_handler *fh;
222 if (!fn || !fn->dev.driver)
225 fh = to_rmi_function_handler(fn->dev.driver);
227 retval = fh->resume(fn);
229 dev_err(&fn->dev, "Resume failed with code %d.\n",
236 static int rmi_resume_functions(struct rmi_device *rmi_dev)
238 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
239 struct rmi_function *entry;
242 list_for_each_entry(entry, &data->function_list, node) {
243 retval = resume_one_function(entry);
251 static int enable_sensor(struct rmi_device *rmi_dev)
255 retval = rmi_driver_process_config_requests(rmi_dev);
259 return rmi_process_interrupt_requests(rmi_dev);
263 * rmi_driver_set_input_params - set input device id and other data.
265 * @rmi_dev: Pointer to an RMI device
266 * @input: Pointer to input device
269 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
270 struct input_dev *input)
272 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
273 input->id.vendor = SYNAPTICS_VENDOR_ID;
274 input->id.bustype = BUS_RMI;
278 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
279 struct input_dev *input)
281 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
282 char *device_name = rmi_f01_get_product_ID(data->f01_container);
285 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
286 "Synaptics %s", device_name);
293 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
297 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
298 struct device *dev = &rmi_dev->dev;
300 mutex_lock(&data->irq_mutex);
301 bitmap_or(data->new_irq_mask,
302 data->current_irq_mask, mask, data->irq_count);
304 error = rmi_write_block(rmi_dev,
305 data->f01_container->fd.control_base_addr + 1,
306 data->new_irq_mask, data->num_of_irq_regs);
308 dev_err(dev, "%s: Failed to change enabled interrupts!",
312 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
313 data->num_of_irq_regs);
316 mutex_unlock(&data->irq_mutex);
320 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
324 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
325 struct device *dev = &rmi_dev->dev;
327 mutex_lock(&data->irq_mutex);
328 bitmap_andnot(data->new_irq_mask,
329 data->current_irq_mask, mask, data->irq_count);
331 error = rmi_write_block(rmi_dev,
332 data->f01_container->fd.control_base_addr + 1,
333 data->new_irq_mask, data->num_of_irq_regs);
335 dev_err(dev, "%s: Failed to change enabled interrupts!",
339 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
340 data->num_of_irq_regs);
343 mutex_unlock(&data->irq_mutex);
347 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
349 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
353 * Can get called before the driver is fully ready to deal with
356 if (!data || !data->f01_container) {
357 dev_warn(&rmi_dev->dev,
358 "Not ready to handle reset yet!\n");
362 error = rmi_read_block(rmi_dev,
363 data->f01_container->fd.control_base_addr + 1,
364 data->current_irq_mask, data->num_of_irq_regs);
366 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
371 error = rmi_driver_process_reset_requests(rmi_dev);
375 error = rmi_driver_process_config_requests(rmi_dev);
382 int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
385 u8 buf[RMI_PDT_ENTRY_SIZE];
388 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
390 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
395 entry->page_start = pdt_address & RMI4_PAGE_MASK;
396 entry->query_base_addr = buf[0];
397 entry->command_base_addr = buf[1];
398 entry->control_base_addr = buf[2];
399 entry->data_base_addr = buf[3];
400 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
401 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
402 entry->function_number = buf[5];
406 EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
408 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
409 struct rmi_function_descriptor *fd)
411 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
412 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
413 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
414 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
415 fd->function_number = pdt->function_number;
416 fd->interrupt_source_count = pdt->interrupt_source_count;
417 fd->function_version = pdt->function_version;
420 #define RMI_SCAN_CONTINUE 0
421 #define RMI_SCAN_DONE 1
423 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
426 int (*callback)(struct rmi_device *rmi_dev,
428 const struct pdt_entry *entry))
430 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
431 struct pdt_entry pdt_entry;
432 u16 page_start = RMI4_PAGE_SIZE * page;
433 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
434 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
439 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
440 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
444 if (RMI4_END_OF_PDT(pdt_entry.function_number))
447 retval = callback(rmi_dev, ctx, &pdt_entry);
448 if (retval != RMI_SCAN_CONTINUE)
452 return (data->f01_bootloader_mode || addr == pdt_start) ?
453 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
456 static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
457 int (*callback)(struct rmi_device *rmi_dev,
459 const struct pdt_entry *entry))
462 int retval = RMI_SCAN_DONE;
464 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
465 retval = rmi_scan_pdt_page(rmi_dev, page, ctx, callback);
466 if (retval != RMI_SCAN_CONTINUE)
470 return retval < 0 ? retval : 0;
473 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
474 struct rmi_register_descriptor *rdesc)
477 u8 size_presence_reg;
479 int presense_offset = 1;
488 * The first register of the register descriptor is the size of
489 * the register descriptor's presense register.
491 ret = rmi_read(d, addr, &size_presence_reg);
496 if (size_presence_reg < 0 || size_presence_reg > 35)
499 memset(buf, 0, sizeof(buf));
502 * The presence register contains the size of the register structure
503 * and a bitmap which identified which packet registers are present
504 * for this particular register type (ie query, control, or data).
506 ret = rmi_read_block(d, addr, buf, size_presence_reg);
513 rdesc->struct_size = buf[1] | (buf[2] << 8);
515 rdesc->struct_size = buf[0];
518 for (i = presense_offset; i < size_presence_reg; i++) {
519 for (b = 0; b < 8; b++) {
520 if (buf[i] & (0x1 << b))
521 bitmap_set(rdesc->presense_map, map_offset, 1);
526 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
527 RMI_REG_DESC_PRESENSE_BITS);
529 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
530 sizeof(struct rmi_register_desc_item),
532 if (!rdesc->registers)
536 * Allocate a temporary buffer to hold the register structure.
537 * I'm not using devm_kzalloc here since it will not be retained
538 * after exiting this function
540 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
545 * The register structure contains information about every packet
546 * register of this type. This includes the size of the packet
547 * register and a bitmap of all subpackets contained in the packet
550 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
552 goto free_struct_buff;
554 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
555 for (i = 0; i < rdesc->num_registers; i++) {
556 struct rmi_register_desc_item *item = &rdesc->registers[i];
557 int reg_size = struct_buf[offset];
561 reg_size = struct_buf[offset] |
562 (struct_buf[offset + 1] << 8);
567 reg_size = struct_buf[offset] |
568 (struct_buf[offset + 1] << 8) |
569 (struct_buf[offset + 2] << 16) |
570 (struct_buf[offset + 3] << 24);
575 item->reg_size = reg_size;
580 for (b = 0; b < 7; b++) {
581 if (struct_buf[offset] & (0x1 << b))
582 bitmap_set(item->subpacket_map,
586 } while (struct_buf[offset++] & 0x80);
588 item->num_subpackets = bitmap_weight(item->subpacket_map,
589 RMI_REG_DESC_SUBPACKET_BITS);
591 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
592 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
593 item->reg, item->reg_size, item->num_subpackets);
595 reg = find_next_bit(rdesc->presense_map,
596 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
603 EXPORT_SYMBOL_GPL(rmi_read_register_desc);
605 const struct rmi_register_desc_item *rmi_get_register_desc_item(
606 struct rmi_register_descriptor *rdesc, u16 reg)
608 const struct rmi_register_desc_item *item;
611 for (i = 0; i < rdesc->num_registers; i++) {
612 item = &rdesc->registers[i];
613 if (item->reg == reg)
619 EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
621 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
623 const struct rmi_register_desc_item *item;
627 for (i = 0; i < rdesc->num_registers; i++) {
628 item = &rdesc->registers[i];
629 size += item->reg_size;
633 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
635 /* Compute the register offset relative to the base address */
636 int rmi_register_desc_calc_reg_offset(
637 struct rmi_register_descriptor *rdesc, u16 reg)
639 const struct rmi_register_desc_item *item;
643 for (i = 0; i < rdesc->num_registers; i++) {
644 item = &rdesc->registers[i];
645 if (item->reg == reg)
651 EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
653 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
656 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
657 subpacket) == subpacket;
660 /* Indicates that flash programming is enabled (bootloader mode). */
661 #define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
664 * Given the PDT entry for F01, read the device status register to determine
665 * if we're stuck in bootloader mode or not.
668 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
669 const struct pdt_entry *pdt)
674 error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
677 dev_err(&rmi_dev->dev,
678 "Failed to read device status: %d.\n", error);
682 return RMI_F01_STATUS_BOOTLOADER(device_status);
685 static int rmi_count_irqs(struct rmi_device *rmi_dev,
686 void *ctx, const struct pdt_entry *pdt)
688 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
689 int *irq_count = ctx;
691 *irq_count += pdt->interrupt_source_count;
692 if (pdt->function_number == 0x01) {
693 data->f01_bootloader_mode =
694 rmi_check_bootloader_mode(rmi_dev, pdt);
695 if (data->f01_bootloader_mode)
696 dev_warn(&rmi_dev->dev,
697 "WARNING: RMI4 device is in bootloader mode!\n");
700 return RMI_SCAN_CONTINUE;
703 static int rmi_initial_reset(struct rmi_device *rmi_dev,
704 void *ctx, const struct pdt_entry *pdt)
708 if (pdt->function_number == 0x01) {
709 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
710 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
711 const struct rmi_device_platform_data *pdata =
712 rmi_get_platform_data(rmi_dev);
714 if (rmi_dev->xport->ops->reset) {
715 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
720 return RMI_SCAN_DONE;
723 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
725 dev_err(&rmi_dev->dev,
726 "Initial reset failed. Code = %d.\n", error);
730 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
732 return RMI_SCAN_DONE;
735 /* F01 should always be on page 0. If we don't find it there, fail. */
736 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
739 static int rmi_create_function(struct rmi_device *rmi_dev,
740 void *ctx, const struct pdt_entry *pdt)
742 struct device *dev = &rmi_dev->dev;
743 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
744 int *current_irq_count = ctx;
745 struct rmi_function *fn;
749 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
750 pdt->function_number);
752 fn = kzalloc(sizeof(struct rmi_function) +
753 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
756 dev_err(dev, "Failed to allocate memory for F%02X\n",
757 pdt->function_number);
761 INIT_LIST_HEAD(&fn->node);
762 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
764 fn->rmi_dev = rmi_dev;
766 fn->num_of_irqs = pdt->interrupt_source_count;
767 fn->irq_pos = *current_irq_count;
768 *current_irq_count += fn->num_of_irqs;
770 for (i = 0; i < fn->num_of_irqs; i++)
771 set_bit(fn->irq_pos + i, fn->irq_mask);
773 error = rmi_register_function(fn);
777 if (pdt->function_number == 0x01)
778 data->f01_container = fn;
780 list_add_tail(&fn->node, &data->function_list);
782 return RMI_SCAN_CONTINUE;
785 int rmi_driver_suspend(struct rmi_device *rmi_dev)
789 retval = rmi_suspend_functions(rmi_dev);
791 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
796 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
798 int rmi_driver_resume(struct rmi_device *rmi_dev)
802 retval = rmi_resume_functions(rmi_dev);
804 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
809 EXPORT_SYMBOL_GPL(rmi_driver_resume);
811 static int rmi_driver_remove(struct device *dev)
813 struct rmi_device *rmi_dev = to_rmi_device(dev);
815 rmi_free_function_list(rmi_dev);
821 static int rmi_driver_of_probe(struct device *dev,
822 struct rmi_device_platform_data *pdata)
826 retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
827 "syna,reset-delay-ms", 1);
834 static inline int rmi_driver_of_probe(struct device *dev,
835 struct rmi_device_platform_data *pdata)
841 static int rmi_driver_probe(struct device *dev)
843 struct rmi_driver *rmi_driver;
844 struct rmi_driver_data *data;
845 struct rmi_device_platform_data *pdata;
846 struct rmi_device *rmi_dev;
852 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
855 if (!rmi_is_physical_device(dev)) {
856 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
860 rmi_dev = to_rmi_device(dev);
861 rmi_driver = to_rmi_driver(dev->driver);
862 rmi_dev->driver = rmi_driver;
864 pdata = rmi_get_platform_data(rmi_dev);
866 if (rmi_dev->xport->dev->of_node) {
867 retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
872 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
876 INIT_LIST_HEAD(&data->function_list);
877 data->rmi_dev = rmi_dev;
878 dev_set_drvdata(&rmi_dev->dev, data);
881 * Right before a warm boot, the sensor might be in some unusual state,
882 * such as F54 diagnostics, or F34 bootloader mode after a firmware
883 * or configuration update. In order to clear the sensor to a known
884 * state and/or apply any updates, we issue a initial reset to clear any
885 * previous settings and force it into normal operation.
887 * We have to do this before actually building the PDT because
888 * the reflash updates (if any) might cause various registers to move
891 * For a number of reasons, this initial reset may fail to return
892 * within the specified time, but we'll still be able to bring up the
893 * driver normally after that failure. This occurs most commonly in
894 * a cold boot situation (where then firmware takes longer to come up
895 * than from a warm boot) and the reset_delay_ms in the platform data
896 * has been set too short to accommodate that. Since the sensor will
897 * eventually come up and be usable, we don't want to just fail here
898 * and leave the customer's device unusable. So we warn them, and
899 * continue processing.
901 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
903 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
905 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
908 * we'll print out a warning and continue since
909 * failure to get the PDT properties is not a cause to fail
911 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
912 PDT_PROPERTIES_LOCATION, retval);
916 * We need to count the IRQs and allocate their storage before scanning
917 * the PDT and creating the function entries, because adding a new
918 * function can trigger events that result in the IRQ related storage
921 rmi_dbg(RMI_DEBUG_CORE, dev, "Counting IRQs.\n");
923 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
925 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
928 data->irq_count = irq_count;
929 data->num_of_irq_regs = (data->irq_count + 7) / 8;
931 mutex_init(&data->irq_mutex);
933 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
934 irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
936 dev_err(dev, "Failed to allocate memory for irq masks.\n");
940 data->irq_status = irq_memory + size * 0;
941 data->fn_irq_bits = irq_memory + size * 1;
942 data->current_irq_mask = irq_memory + size * 2;
943 data->new_irq_mask = irq_memory + size * 3;
945 if (rmi_dev->xport->input) {
947 * The transport driver already has an input device.
948 * In some cases it is preferable to reuse the transport
949 * devices input device instead of creating a new one here.
950 * One example is some HID touchpads report "pass-through"
951 * button events are not reported by rmi registers.
953 data->input = rmi_dev->xport->input;
955 data->input = devm_input_allocate_device(dev);
957 dev_err(dev, "%s: Failed to allocate input device.\n",
960 goto err_destroy_functions;
962 rmi_driver_set_input_params(rmi_dev, data->input);
963 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
964 "%s/input0", dev_name(dev));
968 rmi_dbg(RMI_DEBUG_CORE, dev, "Creating functions.");
969 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
971 dev_err(dev, "Function creation failed with code %d.\n",
973 goto err_destroy_functions;
976 if (!data->f01_container) {
977 dev_err(dev, "Missing F01 container!\n");
979 goto err_destroy_functions;
982 retval = rmi_read_block(rmi_dev,
983 data->f01_container->fd.control_base_addr + 1,
984 data->current_irq_mask, data->num_of_irq_regs);
986 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
988 goto err_destroy_functions;
992 rmi_driver_set_input_name(rmi_dev, data->input);
993 if (!rmi_dev->xport->input) {
994 retval = input_register_device(data->input);
996 dev_err(dev, "%s: Failed to register input device.\n",
998 goto err_destroy_functions;
1003 if (data->f01_container->dev.driver)
1004 /* Driver already bound, so enable ATTN now. */
1005 return enable_sensor(rmi_dev);
1009 err_destroy_functions:
1010 rmi_free_function_list(rmi_dev);
1012 return retval < 0 ? retval : 0;
1015 static struct rmi_driver rmi_physical_driver = {
1017 .owner = THIS_MODULE,
1018 .name = "rmi4_physical",
1019 .bus = &rmi_bus_type,
1020 .probe = rmi_driver_probe,
1021 .remove = rmi_driver_remove,
1023 .reset_handler = rmi_driver_reset_handler,
1024 .clear_irq_bits = rmi_driver_clear_irq_bits,
1025 .set_irq_bits = rmi_driver_set_irq_bits,
1026 .set_input_params = rmi_driver_set_input_params,
1029 bool rmi_is_physical_driver(struct device_driver *drv)
1031 return drv == &rmi_physical_driver.driver;
1034 int __init rmi_register_physical_driver(void)
1038 error = driver_register(&rmi_physical_driver.driver);
1040 pr_err("%s: driver register failed, code=%d.\n", __func__,
1048 void __exit rmi_unregister_physical_driver(void)
1050 driver_unregister(&rmi_physical_driver.driver);