Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2011-2016 Synaptics Incorporated
0004  * Copyright (c) 2011 Unixphere
0005  *
0006  * This driver provides the core support for a single RMI4-based device.
0007  *
0008  * The RMI4 specification can be found here (URL split for line length):
0009  *
0010  * http://www.synaptics.com/sites/default/files/
0011  *      511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
0012  */
0013 
0014 #include <linux/bitmap.h>
0015 #include <linux/delay.h>
0016 #include <linux/fs.h>
0017 #include <linux/irq.h>
0018 #include <linux/pm.h>
0019 #include <linux/slab.h>
0020 #include <linux/of.h>
0021 #include <linux/irqdomain.h>
0022 #include <uapi/linux/input.h>
0023 #include <linux/rmi.h>
0024 #include "rmi_bus.h"
0025 #include "rmi_driver.h"
0026 
0027 #define HAS_NONSTANDARD_PDT_MASK 0x40
0028 #define RMI4_MAX_PAGE 0xff
0029 #define RMI4_PAGE_SIZE 0x100
0030 #define RMI4_PAGE_MASK 0xFF00
0031 
0032 #define RMI_DEVICE_RESET_CMD    0x01
0033 #define DEFAULT_RESET_DELAY_MS  100
0034 
0035 void rmi_free_function_list(struct rmi_device *rmi_dev)
0036 {
0037     struct rmi_function *fn, *tmp;
0038     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0039 
0040     rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
0041 
0042     /* Doing it in the reverse order so F01 will be removed last */
0043     list_for_each_entry_safe_reverse(fn, tmp,
0044                      &data->function_list, node) {
0045         list_del(&fn->node);
0046         rmi_unregister_function(fn);
0047     }
0048 
0049     devm_kfree(&rmi_dev->dev, data->irq_memory);
0050     data->irq_memory = NULL;
0051     data->irq_status = NULL;
0052     data->fn_irq_bits = NULL;
0053     data->current_irq_mask = NULL;
0054     data->new_irq_mask = NULL;
0055 
0056     data->f01_container = NULL;
0057     data->f34_container = NULL;
0058 }
0059 
0060 static int reset_one_function(struct rmi_function *fn)
0061 {
0062     struct rmi_function_handler *fh;
0063     int retval = 0;
0064 
0065     if (!fn || !fn->dev.driver)
0066         return 0;
0067 
0068     fh = to_rmi_function_handler(fn->dev.driver);
0069     if (fh->reset) {
0070         retval = fh->reset(fn);
0071         if (retval < 0)
0072             dev_err(&fn->dev, "Reset failed with code %d.\n",
0073                 retval);
0074     }
0075 
0076     return retval;
0077 }
0078 
0079 static int configure_one_function(struct rmi_function *fn)
0080 {
0081     struct rmi_function_handler *fh;
0082     int retval = 0;
0083 
0084     if (!fn || !fn->dev.driver)
0085         return 0;
0086 
0087     fh = to_rmi_function_handler(fn->dev.driver);
0088     if (fh->config) {
0089         retval = fh->config(fn);
0090         if (retval < 0)
0091             dev_err(&fn->dev, "Config failed with code %d.\n",
0092                 retval);
0093     }
0094 
0095     return retval;
0096 }
0097 
0098 static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
0099 {
0100     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0101     struct rmi_function *entry;
0102     int retval;
0103 
0104     list_for_each_entry(entry, &data->function_list, node) {
0105         retval = reset_one_function(entry);
0106         if (retval < 0)
0107             return retval;
0108     }
0109 
0110     return 0;
0111 }
0112 
0113 static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
0114 {
0115     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0116     struct rmi_function *entry;
0117     int retval;
0118 
0119     list_for_each_entry(entry, &data->function_list, node) {
0120         retval = configure_one_function(entry);
0121         if (retval < 0)
0122             return retval;
0123     }
0124 
0125     return 0;
0126 }
0127 
0128 static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
0129 {
0130     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0131     struct device *dev = &rmi_dev->dev;
0132     int i;
0133     int error;
0134 
0135     if (!data)
0136         return 0;
0137 
0138     if (!data->attn_data.data) {
0139         error = rmi_read_block(rmi_dev,
0140                 data->f01_container->fd.data_base_addr + 1,
0141                 data->irq_status, data->num_of_irq_regs);
0142         if (error < 0) {
0143             dev_err(dev, "Failed to read irqs, code=%d\n", error);
0144             return error;
0145         }
0146     }
0147 
0148     mutex_lock(&data->irq_mutex);
0149     bitmap_and(data->irq_status, data->irq_status, data->fn_irq_bits,
0150            data->irq_count);
0151     /*
0152      * At this point, irq_status has all bits that are set in the
0153      * interrupt status register and are enabled.
0154      */
0155     mutex_unlock(&data->irq_mutex);
0156 
0157     for_each_set_bit(i, data->irq_status, data->irq_count)
0158         handle_nested_irq(irq_find_mapping(data->irqdomain, i));
0159 
0160     if (data->input)
0161         input_sync(data->input);
0162 
0163     return 0;
0164 }
0165 
0166 void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
0167                void *data, size_t size)
0168 {
0169     struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
0170     struct rmi4_attn_data attn_data;
0171     void *fifo_data;
0172 
0173     if (!drvdata->enabled)
0174         return;
0175 
0176     fifo_data = kmemdup(data, size, GFP_ATOMIC);
0177     if (!fifo_data)
0178         return;
0179 
0180     attn_data.irq_status = irq_status;
0181     attn_data.size = size;
0182     attn_data.data = fifo_data;
0183 
0184     kfifo_put(&drvdata->attn_fifo, attn_data);
0185 }
0186 EXPORT_SYMBOL_GPL(rmi_set_attn_data);
0187 
0188 static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
0189 {
0190     struct rmi_device *rmi_dev = dev_id;
0191     struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
0192     struct rmi4_attn_data attn_data = {0};
0193     int ret, count;
0194 
0195     count = kfifo_get(&drvdata->attn_fifo, &attn_data);
0196     if (count) {
0197         *(drvdata->irq_status) = attn_data.irq_status;
0198         drvdata->attn_data = attn_data;
0199     }
0200 
0201     ret = rmi_process_interrupt_requests(rmi_dev);
0202     if (ret)
0203         rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
0204             "Failed to process interrupt request: %d\n", ret);
0205 
0206     if (count) {
0207         kfree(attn_data.data);
0208         drvdata->attn_data.data = NULL;
0209     }
0210 
0211     if (!kfifo_is_empty(&drvdata->attn_fifo))
0212         return rmi_irq_fn(irq, dev_id);
0213 
0214     return IRQ_HANDLED;
0215 }
0216 
0217 static int rmi_irq_init(struct rmi_device *rmi_dev)
0218 {
0219     struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
0220     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0221     int irq_flags = irq_get_trigger_type(pdata->irq);
0222     int ret;
0223 
0224     if (!irq_flags)
0225         irq_flags = IRQF_TRIGGER_LOW;
0226 
0227     ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
0228                     rmi_irq_fn, irq_flags | IRQF_ONESHOT,
0229                     dev_driver_string(rmi_dev->xport->dev),
0230                     rmi_dev);
0231     if (ret < 0) {
0232         dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
0233             pdata->irq);
0234 
0235         return ret;
0236     }
0237 
0238     data->enabled = true;
0239 
0240     return 0;
0241 }
0242 
0243 struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
0244 {
0245     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0246     struct rmi_function *entry;
0247 
0248     list_for_each_entry(entry, &data->function_list, node) {
0249         if (entry->fd.function_number == number)
0250             return entry;
0251     }
0252 
0253     return NULL;
0254 }
0255 
0256 static int suspend_one_function(struct rmi_function *fn)
0257 {
0258     struct rmi_function_handler *fh;
0259     int retval = 0;
0260 
0261     if (!fn || !fn->dev.driver)
0262         return 0;
0263 
0264     fh = to_rmi_function_handler(fn->dev.driver);
0265     if (fh->suspend) {
0266         retval = fh->suspend(fn);
0267         if (retval < 0)
0268             dev_err(&fn->dev, "Suspend failed with code %d.\n",
0269                 retval);
0270     }
0271 
0272     return retval;
0273 }
0274 
0275 static int rmi_suspend_functions(struct rmi_device *rmi_dev)
0276 {
0277     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0278     struct rmi_function *entry;
0279     int retval;
0280 
0281     list_for_each_entry(entry, &data->function_list, node) {
0282         retval = suspend_one_function(entry);
0283         if (retval < 0)
0284             return retval;
0285     }
0286 
0287     return 0;
0288 }
0289 
0290 static int resume_one_function(struct rmi_function *fn)
0291 {
0292     struct rmi_function_handler *fh;
0293     int retval = 0;
0294 
0295     if (!fn || !fn->dev.driver)
0296         return 0;
0297 
0298     fh = to_rmi_function_handler(fn->dev.driver);
0299     if (fh->resume) {
0300         retval = fh->resume(fn);
0301         if (retval < 0)
0302             dev_err(&fn->dev, "Resume failed with code %d.\n",
0303                 retval);
0304     }
0305 
0306     return retval;
0307 }
0308 
0309 static int rmi_resume_functions(struct rmi_device *rmi_dev)
0310 {
0311     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0312     struct rmi_function *entry;
0313     int retval;
0314 
0315     list_for_each_entry(entry, &data->function_list, node) {
0316         retval = resume_one_function(entry);
0317         if (retval < 0)
0318             return retval;
0319     }
0320 
0321     return 0;
0322 }
0323 
0324 int rmi_enable_sensor(struct rmi_device *rmi_dev)
0325 {
0326     int retval = 0;
0327 
0328     retval = rmi_driver_process_config_requests(rmi_dev);
0329     if (retval < 0)
0330         return retval;
0331 
0332     return rmi_process_interrupt_requests(rmi_dev);
0333 }
0334 
0335 /**
0336  * rmi_driver_set_input_params - set input device id and other data.
0337  *
0338  * @rmi_dev: Pointer to an RMI device
0339  * @input: Pointer to input device
0340  *
0341  */
0342 static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
0343                 struct input_dev *input)
0344 {
0345     input->name = SYNAPTICS_INPUT_DEVICE_NAME;
0346     input->id.vendor  = SYNAPTICS_VENDOR_ID;
0347     input->id.bustype = BUS_RMI;
0348     return 0;
0349 }
0350 
0351 static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
0352                 struct input_dev *input)
0353 {
0354     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0355     const char *device_name = rmi_f01_get_product_ID(data->f01_container);
0356     char *name;
0357 
0358     name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
0359                   "Synaptics %s", device_name);
0360     if (!name)
0361         return;
0362 
0363     input->name = name;
0364 }
0365 
0366 static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
0367                    unsigned long *mask)
0368 {
0369     int error = 0;
0370     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0371     struct device *dev = &rmi_dev->dev;
0372 
0373     mutex_lock(&data->irq_mutex);
0374     bitmap_or(data->new_irq_mask,
0375           data->current_irq_mask, mask, data->irq_count);
0376 
0377     error = rmi_write_block(rmi_dev,
0378             data->f01_container->fd.control_base_addr + 1,
0379             data->new_irq_mask, data->num_of_irq_regs);
0380     if (error < 0) {
0381         dev_err(dev, "%s: Failed to change enabled interrupts!",
0382                             __func__);
0383         goto error_unlock;
0384     }
0385     bitmap_copy(data->current_irq_mask, data->new_irq_mask,
0386             data->num_of_irq_regs);
0387 
0388     bitmap_or(data->fn_irq_bits, data->fn_irq_bits, mask, data->irq_count);
0389 
0390 error_unlock:
0391     mutex_unlock(&data->irq_mutex);
0392     return error;
0393 }
0394 
0395 static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
0396                      unsigned long *mask)
0397 {
0398     int error = 0;
0399     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0400     struct device *dev = &rmi_dev->dev;
0401 
0402     mutex_lock(&data->irq_mutex);
0403     bitmap_andnot(data->fn_irq_bits,
0404               data->fn_irq_bits, mask, data->irq_count);
0405     bitmap_andnot(data->new_irq_mask,
0406           data->current_irq_mask, mask, data->irq_count);
0407 
0408     error = rmi_write_block(rmi_dev,
0409             data->f01_container->fd.control_base_addr + 1,
0410             data->new_irq_mask, data->num_of_irq_regs);
0411     if (error < 0) {
0412         dev_err(dev, "%s: Failed to change enabled interrupts!",
0413                             __func__);
0414         goto error_unlock;
0415     }
0416     bitmap_copy(data->current_irq_mask, data->new_irq_mask,
0417             data->num_of_irq_regs);
0418 
0419 error_unlock:
0420     mutex_unlock(&data->irq_mutex);
0421     return error;
0422 }
0423 
0424 static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
0425 {
0426     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0427     int error;
0428 
0429     /*
0430      * Can get called before the driver is fully ready to deal with
0431      * this situation.
0432      */
0433     if (!data || !data->f01_container) {
0434         dev_warn(&rmi_dev->dev,
0435              "Not ready to handle reset yet!\n");
0436         return 0;
0437     }
0438 
0439     error = rmi_read_block(rmi_dev,
0440                    data->f01_container->fd.control_base_addr + 1,
0441                    data->current_irq_mask, data->num_of_irq_regs);
0442     if (error < 0) {
0443         dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
0444             __func__);
0445         return error;
0446     }
0447 
0448     error = rmi_driver_process_reset_requests(rmi_dev);
0449     if (error < 0)
0450         return error;
0451 
0452     error = rmi_driver_process_config_requests(rmi_dev);
0453     if (error < 0)
0454         return error;
0455 
0456     return 0;
0457 }
0458 
0459 static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
0460                   struct pdt_entry *entry, u16 pdt_address)
0461 {
0462     u8 buf[RMI_PDT_ENTRY_SIZE];
0463     int error;
0464 
0465     error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
0466     if (error) {
0467         dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
0468                 pdt_address, error);
0469         return error;
0470     }
0471 
0472     entry->page_start = pdt_address & RMI4_PAGE_MASK;
0473     entry->query_base_addr = buf[0];
0474     entry->command_base_addr = buf[1];
0475     entry->control_base_addr = buf[2];
0476     entry->data_base_addr = buf[3];
0477     entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
0478     entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
0479     entry->function_number = buf[5];
0480 
0481     return 0;
0482 }
0483 
0484 static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
0485                       struct rmi_function_descriptor *fd)
0486 {
0487     fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
0488     fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
0489     fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
0490     fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
0491     fd->function_number = pdt->function_number;
0492     fd->interrupt_source_count = pdt->interrupt_source_count;
0493     fd->function_version = pdt->function_version;
0494 }
0495 
0496 #define RMI_SCAN_CONTINUE   0
0497 #define RMI_SCAN_DONE       1
0498 
0499 static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
0500                  int page,
0501                  int *empty_pages,
0502                  void *ctx,
0503                  int (*callback)(struct rmi_device *rmi_dev,
0504                          void *ctx,
0505                          const struct pdt_entry *entry))
0506 {
0507     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0508     struct pdt_entry pdt_entry;
0509     u16 page_start = RMI4_PAGE_SIZE * page;
0510     u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
0511     u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
0512     u16 addr;
0513     int error;
0514     int retval;
0515 
0516     for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
0517         error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
0518         if (error)
0519             return error;
0520 
0521         if (RMI4_END_OF_PDT(pdt_entry.function_number))
0522             break;
0523 
0524         retval = callback(rmi_dev, ctx, &pdt_entry);
0525         if (retval != RMI_SCAN_CONTINUE)
0526             return retval;
0527     }
0528 
0529     /*
0530      * Count number of empty PDT pages. If a gap of two pages
0531      * or more is found, stop scanning.
0532      */
0533     if (addr == pdt_start)
0534         ++*empty_pages;
0535     else
0536         *empty_pages = 0;
0537 
0538     return (data->bootloader_mode || *empty_pages >= 2) ?
0539                     RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
0540 }
0541 
0542 int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
0543          int (*callback)(struct rmi_device *rmi_dev,
0544          void *ctx, const struct pdt_entry *entry))
0545 {
0546     int page;
0547     int empty_pages = 0;
0548     int retval = RMI_SCAN_DONE;
0549 
0550     for (page = 0; page <= RMI4_MAX_PAGE; page++) {
0551         retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
0552                        ctx, callback);
0553         if (retval != RMI_SCAN_CONTINUE)
0554             break;
0555     }
0556 
0557     return retval < 0 ? retval : 0;
0558 }
0559 
0560 int rmi_read_register_desc(struct rmi_device *d, u16 addr,
0561                 struct rmi_register_descriptor *rdesc)
0562 {
0563     int ret;
0564     u8 size_presence_reg;
0565     u8 buf[35];
0566     int presense_offset = 1;
0567     u8 *struct_buf;
0568     int reg;
0569     int offset = 0;
0570     int map_offset = 0;
0571     int i;
0572     int b;
0573 
0574     /*
0575      * The first register of the register descriptor is the size of
0576      * the register descriptor's presense register.
0577      */
0578     ret = rmi_read(d, addr, &size_presence_reg);
0579     if (ret)
0580         return ret;
0581     ++addr;
0582 
0583     if (size_presence_reg < 0 || size_presence_reg > 35)
0584         return -EIO;
0585 
0586     memset(buf, 0, sizeof(buf));
0587 
0588     /*
0589      * The presence register contains the size of the register structure
0590      * and a bitmap which identified which packet registers are present
0591      * for this particular register type (ie query, control, or data).
0592      */
0593     ret = rmi_read_block(d, addr, buf, size_presence_reg);
0594     if (ret)
0595         return ret;
0596     ++addr;
0597 
0598     if (buf[0] == 0) {
0599         presense_offset = 3;
0600         rdesc->struct_size = buf[1] | (buf[2] << 8);
0601     } else {
0602         rdesc->struct_size = buf[0];
0603     }
0604 
0605     for (i = presense_offset; i < size_presence_reg; i++) {
0606         for (b = 0; b < 8; b++) {
0607             if (buf[i] & (0x1 << b))
0608                 bitmap_set(rdesc->presense_map, map_offset, 1);
0609             ++map_offset;
0610         }
0611     }
0612 
0613     rdesc->num_registers = bitmap_weight(rdesc->presense_map,
0614                         RMI_REG_DESC_PRESENSE_BITS);
0615 
0616     rdesc->registers = devm_kcalloc(&d->dev,
0617                     rdesc->num_registers,
0618                     sizeof(struct rmi_register_desc_item),
0619                     GFP_KERNEL);
0620     if (!rdesc->registers)
0621         return -ENOMEM;
0622 
0623     /*
0624      * Allocate a temporary buffer to hold the register structure.
0625      * I'm not using devm_kzalloc here since it will not be retained
0626      * after exiting this function
0627      */
0628     struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
0629     if (!struct_buf)
0630         return -ENOMEM;
0631 
0632     /*
0633      * The register structure contains information about every packet
0634      * register of this type. This includes the size of the packet
0635      * register and a bitmap of all subpackets contained in the packet
0636      * register.
0637      */
0638     ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
0639     if (ret)
0640         goto free_struct_buff;
0641 
0642     reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
0643     for (i = 0; i < rdesc->num_registers; i++) {
0644         struct rmi_register_desc_item *item = &rdesc->registers[i];
0645         int reg_size = struct_buf[offset];
0646 
0647         ++offset;
0648         if (reg_size == 0) {
0649             reg_size = struct_buf[offset] |
0650                     (struct_buf[offset + 1] << 8);
0651             offset += 2;
0652         }
0653 
0654         if (reg_size == 0) {
0655             reg_size = struct_buf[offset] |
0656                     (struct_buf[offset + 1] << 8) |
0657                     (struct_buf[offset + 2] << 16) |
0658                     (struct_buf[offset + 3] << 24);
0659             offset += 4;
0660         }
0661 
0662         item->reg = reg;
0663         item->reg_size = reg_size;
0664 
0665         map_offset = 0;
0666 
0667         do {
0668             for (b = 0; b < 7; b++) {
0669                 if (struct_buf[offset] & (0x1 << b))
0670                     bitmap_set(item->subpacket_map,
0671                         map_offset, 1);
0672                 ++map_offset;
0673             }
0674         } while (struct_buf[offset++] & 0x80);
0675 
0676         item->num_subpackets = bitmap_weight(item->subpacket_map,
0677                         RMI_REG_DESC_SUBPACKET_BITS);
0678 
0679         rmi_dbg(RMI_DEBUG_CORE, &d->dev,
0680             "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
0681             item->reg, item->reg_size, item->num_subpackets);
0682 
0683         reg = find_next_bit(rdesc->presense_map,
0684                 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
0685     }
0686 
0687 free_struct_buff:
0688     kfree(struct_buf);
0689     return ret;
0690 }
0691 
0692 const struct rmi_register_desc_item *rmi_get_register_desc_item(
0693                 struct rmi_register_descriptor *rdesc, u16 reg)
0694 {
0695     const struct rmi_register_desc_item *item;
0696     int i;
0697 
0698     for (i = 0; i < rdesc->num_registers; i++) {
0699         item = &rdesc->registers[i];
0700         if (item->reg == reg)
0701             return item;
0702     }
0703 
0704     return NULL;
0705 }
0706 
0707 size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
0708 {
0709     const struct rmi_register_desc_item *item;
0710     int i;
0711     size_t size = 0;
0712 
0713     for (i = 0; i < rdesc->num_registers; i++) {
0714         item = &rdesc->registers[i];
0715         size += item->reg_size;
0716     }
0717     return size;
0718 }
0719 
0720 /* Compute the register offset relative to the base address */
0721 int rmi_register_desc_calc_reg_offset(
0722         struct rmi_register_descriptor *rdesc, u16 reg)
0723 {
0724     const struct rmi_register_desc_item *item;
0725     int offset = 0;
0726     int i;
0727 
0728     for (i = 0; i < rdesc->num_registers; i++) {
0729         item = &rdesc->registers[i];
0730         if (item->reg == reg)
0731             return offset;
0732         ++offset;
0733     }
0734     return -1;
0735 }
0736 
0737 bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
0738     u8 subpacket)
0739 {
0740     return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
0741                 subpacket) == subpacket;
0742 }
0743 
0744 static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
0745                      const struct pdt_entry *pdt)
0746 {
0747     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0748     int ret;
0749     u8 status;
0750 
0751     if (pdt->function_number == 0x34 && pdt->function_version > 1) {
0752         ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
0753         if (ret) {
0754             dev_err(&rmi_dev->dev,
0755                 "Failed to read F34 status: %d.\n", ret);
0756             return ret;
0757         }
0758 
0759         if (status & BIT(7))
0760             data->bootloader_mode = true;
0761     } else if (pdt->function_number == 0x01) {
0762         ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
0763         if (ret) {
0764             dev_err(&rmi_dev->dev,
0765                 "Failed to read F01 status: %d.\n", ret);
0766             return ret;
0767         }
0768 
0769         if (status & BIT(6))
0770             data->bootloader_mode = true;
0771     }
0772 
0773     return 0;
0774 }
0775 
0776 static int rmi_count_irqs(struct rmi_device *rmi_dev,
0777              void *ctx, const struct pdt_entry *pdt)
0778 {
0779     int *irq_count = ctx;
0780     int ret;
0781 
0782     *irq_count += pdt->interrupt_source_count;
0783 
0784     ret = rmi_check_bootloader_mode(rmi_dev, pdt);
0785     if (ret < 0)
0786         return ret;
0787 
0788     return RMI_SCAN_CONTINUE;
0789 }
0790 
0791 int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
0792               const struct pdt_entry *pdt)
0793 {
0794     int error;
0795 
0796     if (pdt->function_number == 0x01) {
0797         u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
0798         u8 cmd_buf = RMI_DEVICE_RESET_CMD;
0799         const struct rmi_device_platform_data *pdata =
0800                 rmi_get_platform_data(rmi_dev);
0801 
0802         if (rmi_dev->xport->ops->reset) {
0803             error = rmi_dev->xport->ops->reset(rmi_dev->xport,
0804                                 cmd_addr);
0805             if (error)
0806                 return error;
0807 
0808             return RMI_SCAN_DONE;
0809         }
0810 
0811         rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
0812         error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
0813         if (error) {
0814             dev_err(&rmi_dev->dev,
0815                 "Initial reset failed. Code = %d.\n", error);
0816             return error;
0817         }
0818 
0819         mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
0820 
0821         return RMI_SCAN_DONE;
0822     }
0823 
0824     /* F01 should always be on page 0. If we don't find it there, fail. */
0825     return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
0826 }
0827 
0828 static int rmi_create_function(struct rmi_device *rmi_dev,
0829                    void *ctx, const struct pdt_entry *pdt)
0830 {
0831     struct device *dev = &rmi_dev->dev;
0832     struct rmi_driver_data *data = dev_get_drvdata(dev);
0833     int *current_irq_count = ctx;
0834     struct rmi_function *fn;
0835     int i;
0836     int error;
0837 
0838     rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
0839             pdt->function_number);
0840 
0841     fn = kzalloc(sizeof(struct rmi_function) +
0842             BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
0843              GFP_KERNEL);
0844     if (!fn) {
0845         dev_err(dev, "Failed to allocate memory for F%02X\n",
0846             pdt->function_number);
0847         return -ENOMEM;
0848     }
0849 
0850     INIT_LIST_HEAD(&fn->node);
0851     rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
0852 
0853     fn->rmi_dev = rmi_dev;
0854 
0855     fn->num_of_irqs = pdt->interrupt_source_count;
0856     fn->irq_pos = *current_irq_count;
0857     *current_irq_count += fn->num_of_irqs;
0858 
0859     for (i = 0; i < fn->num_of_irqs; i++)
0860         set_bit(fn->irq_pos + i, fn->irq_mask);
0861 
0862     error = rmi_register_function(fn);
0863     if (error)
0864         return error;
0865 
0866     if (pdt->function_number == 0x01)
0867         data->f01_container = fn;
0868     else if (pdt->function_number == 0x34)
0869         data->f34_container = fn;
0870 
0871     list_add_tail(&fn->node, &data->function_list);
0872 
0873     return RMI_SCAN_CONTINUE;
0874 }
0875 
0876 void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
0877 {
0878     struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
0879     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0880     int irq = pdata->irq;
0881     int irq_flags;
0882     int retval;
0883 
0884     mutex_lock(&data->enabled_mutex);
0885 
0886     if (data->enabled)
0887         goto out;
0888 
0889     enable_irq(irq);
0890     data->enabled = true;
0891     if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
0892         retval = disable_irq_wake(irq);
0893         if (retval)
0894             dev_warn(&rmi_dev->dev,
0895                  "Failed to disable irq for wake: %d\n",
0896                  retval);
0897     }
0898 
0899     /*
0900      * Call rmi_process_interrupt_requests() after enabling irq,
0901      * otherwise we may lose interrupt on edge-triggered systems.
0902      */
0903     irq_flags = irq_get_trigger_type(pdata->irq);
0904     if (irq_flags & IRQ_TYPE_EDGE_BOTH)
0905         rmi_process_interrupt_requests(rmi_dev);
0906 
0907 out:
0908     mutex_unlock(&data->enabled_mutex);
0909 }
0910 
0911 void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
0912 {
0913     struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
0914     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0915     struct rmi4_attn_data attn_data = {0};
0916     int irq = pdata->irq;
0917     int retval, count;
0918 
0919     mutex_lock(&data->enabled_mutex);
0920 
0921     if (!data->enabled)
0922         goto out;
0923 
0924     data->enabled = false;
0925     disable_irq(irq);
0926     if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
0927         retval = enable_irq_wake(irq);
0928         if (retval)
0929             dev_warn(&rmi_dev->dev,
0930                  "Failed to enable irq for wake: %d\n",
0931                  retval);
0932     }
0933 
0934     /* make sure the fifo is clean */
0935     while (!kfifo_is_empty(&data->attn_fifo)) {
0936         count = kfifo_get(&data->attn_fifo, &attn_data);
0937         if (count)
0938             kfree(attn_data.data);
0939     }
0940 
0941 out:
0942     mutex_unlock(&data->enabled_mutex);
0943 }
0944 
0945 int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
0946 {
0947     int retval;
0948 
0949     retval = rmi_suspend_functions(rmi_dev);
0950     if (retval)
0951         dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
0952             retval);
0953 
0954     rmi_disable_irq(rmi_dev, enable_wake);
0955     return retval;
0956 }
0957 EXPORT_SYMBOL_GPL(rmi_driver_suspend);
0958 
0959 int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
0960 {
0961     int retval;
0962 
0963     rmi_enable_irq(rmi_dev, clear_wake);
0964 
0965     retval = rmi_resume_functions(rmi_dev);
0966     if (retval)
0967         dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
0968             retval);
0969 
0970     return retval;
0971 }
0972 EXPORT_SYMBOL_GPL(rmi_driver_resume);
0973 
0974 static int rmi_driver_remove(struct device *dev)
0975 {
0976     struct rmi_device *rmi_dev = to_rmi_device(dev);
0977     struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
0978 
0979     rmi_disable_irq(rmi_dev, false);
0980 
0981     irq_domain_remove(data->irqdomain);
0982     data->irqdomain = NULL;
0983 
0984     rmi_f34_remove_sysfs(rmi_dev);
0985     rmi_free_function_list(rmi_dev);
0986 
0987     return 0;
0988 }
0989 
0990 #ifdef CONFIG_OF
0991 static int rmi_driver_of_probe(struct device *dev,
0992                 struct rmi_device_platform_data *pdata)
0993 {
0994     int retval;
0995 
0996     retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
0997                     "syna,reset-delay-ms", 1);
0998     if (retval)
0999         return retval;
1000 
1001     return 0;
1002 }
1003 #else
1004 static inline int rmi_driver_of_probe(struct device *dev,
1005                     struct rmi_device_platform_data *pdata)
1006 {
1007     return -ENODEV;
1008 }
1009 #endif
1010 
1011 int rmi_probe_interrupts(struct rmi_driver_data *data)
1012 {
1013     struct rmi_device *rmi_dev = data->rmi_dev;
1014     struct device *dev = &rmi_dev->dev;
1015     struct fwnode_handle *fwnode = rmi_dev->xport->dev->fwnode;
1016     int irq_count = 0;
1017     size_t size;
1018     int retval;
1019 
1020     /*
1021      * We need to count the IRQs and allocate their storage before scanning
1022      * the PDT and creating the function entries, because adding a new
1023      * function can trigger events that result in the IRQ related storage
1024      * being accessed.
1025      */
1026     rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
1027     data->bootloader_mode = false;
1028 
1029     retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
1030     if (retval < 0) {
1031         dev_err(dev, "IRQ counting failed with code %d.\n", retval);
1032         return retval;
1033     }
1034 
1035     if (data->bootloader_mode)
1036         dev_warn(dev, "Device in bootloader mode.\n");
1037 
1038     /* Allocate and register a linear revmap irq_domain */
1039     data->irqdomain = irq_domain_create_linear(fwnode, irq_count,
1040                            &irq_domain_simple_ops,
1041                            data);
1042     if (!data->irqdomain) {
1043         dev_err(&rmi_dev->dev, "Failed to create IRQ domain\n");
1044         return -ENOMEM;
1045     }
1046 
1047     data->irq_count = irq_count;
1048     data->num_of_irq_regs = (data->irq_count + 7) / 8;
1049 
1050     size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1051     data->irq_memory = devm_kcalloc(dev, size, 4, GFP_KERNEL);
1052     if (!data->irq_memory) {
1053         dev_err(dev, "Failed to allocate memory for irq masks.\n");
1054         return -ENOMEM;
1055     }
1056 
1057     data->irq_status    = data->irq_memory + size * 0;
1058     data->fn_irq_bits   = data->irq_memory + size * 1;
1059     data->current_irq_mask  = data->irq_memory + size * 2;
1060     data->new_irq_mask  = data->irq_memory + size * 3;
1061 
1062     return retval;
1063 }
1064 
1065 int rmi_init_functions(struct rmi_driver_data *data)
1066 {
1067     struct rmi_device *rmi_dev = data->rmi_dev;
1068     struct device *dev = &rmi_dev->dev;
1069     int irq_count = 0;
1070     int retval;
1071 
1072     rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
1073     retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
1074     if (retval < 0) {
1075         dev_err(dev, "Function creation failed with code %d.\n",
1076             retval);
1077         goto err_destroy_functions;
1078     }
1079 
1080     if (!data->f01_container) {
1081         dev_err(dev, "Missing F01 container!\n");
1082         retval = -EINVAL;
1083         goto err_destroy_functions;
1084     }
1085 
1086     retval = rmi_read_block(rmi_dev,
1087                 data->f01_container->fd.control_base_addr + 1,
1088                 data->current_irq_mask, data->num_of_irq_regs);
1089     if (retval < 0) {
1090         dev_err(dev, "%s: Failed to read current IRQ mask.\n",
1091             __func__);
1092         goto err_destroy_functions;
1093     }
1094 
1095     return 0;
1096 
1097 err_destroy_functions:
1098     rmi_free_function_list(rmi_dev);
1099     return retval;
1100 }
1101 
1102 static int rmi_driver_probe(struct device *dev)
1103 {
1104     struct rmi_driver *rmi_driver;
1105     struct rmi_driver_data *data;
1106     struct rmi_device_platform_data *pdata;
1107     struct rmi_device *rmi_dev;
1108     int retval;
1109 
1110     rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
1111             __func__);
1112 
1113     if (!rmi_is_physical_device(dev)) {
1114         rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
1115         return -ENODEV;
1116     }
1117 
1118     rmi_dev = to_rmi_device(dev);
1119     rmi_driver = to_rmi_driver(dev->driver);
1120     rmi_dev->driver = rmi_driver;
1121 
1122     pdata = rmi_get_platform_data(rmi_dev);
1123 
1124     if (rmi_dev->xport->dev->of_node) {
1125         retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1126         if (retval)
1127             return retval;
1128     }
1129 
1130     data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
1131     if (!data)
1132         return -ENOMEM;
1133 
1134     INIT_LIST_HEAD(&data->function_list);
1135     data->rmi_dev = rmi_dev;
1136     dev_set_drvdata(&rmi_dev->dev, data);
1137 
1138     /*
1139      * Right before a warm boot, the sensor might be in some unusual state,
1140      * such as F54 diagnostics, or F34 bootloader mode after a firmware
1141      * or configuration update.  In order to clear the sensor to a known
1142      * state and/or apply any updates, we issue a initial reset to clear any
1143      * previous settings and force it into normal operation.
1144      *
1145      * We have to do this before actually building the PDT because
1146      * the reflash updates (if any) might cause various registers to move
1147      * around.
1148      *
1149      * For a number of reasons, this initial reset may fail to return
1150      * within the specified time, but we'll still be able to bring up the
1151      * driver normally after that failure.  This occurs most commonly in
1152      * a cold boot situation (where then firmware takes longer to come up
1153      * than from a warm boot) and the reset_delay_ms in the platform data
1154      * has been set too short to accommodate that.  Since the sensor will
1155      * eventually come up and be usable, we don't want to just fail here
1156      * and leave the customer's device unusable.  So we warn them, and
1157      * continue processing.
1158      */
1159     retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
1160     if (retval < 0)
1161         dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
1162 
1163     retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
1164     if (retval < 0) {
1165         /*
1166          * we'll print out a warning and continue since
1167          * failure to get the PDT properties is not a cause to fail
1168          */
1169         dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
1170              PDT_PROPERTIES_LOCATION, retval);
1171     }
1172 
1173     mutex_init(&data->irq_mutex);
1174     mutex_init(&data->enabled_mutex);
1175 
1176     retval = rmi_probe_interrupts(data);
1177     if (retval)
1178         goto err;
1179 
1180     if (rmi_dev->xport->input) {
1181         /*
1182          * The transport driver already has an input device.
1183          * In some cases it is preferable to reuse the transport
1184          * devices input device instead of creating a new one here.
1185          * One example is some HID touchpads report "pass-through"
1186          * button events are not reported by rmi registers.
1187          */
1188         data->input = rmi_dev->xport->input;
1189     } else {
1190         data->input = devm_input_allocate_device(dev);
1191         if (!data->input) {
1192             dev_err(dev, "%s: Failed to allocate input device.\n",
1193                 __func__);
1194             retval = -ENOMEM;
1195             goto err;
1196         }
1197         rmi_driver_set_input_params(rmi_dev, data->input);
1198         data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
1199                         "%s/input0", dev_name(dev));
1200     }
1201 
1202     retval = rmi_init_functions(data);
1203     if (retval)
1204         goto err;
1205 
1206     retval = rmi_f34_create_sysfs(rmi_dev);
1207     if (retval)
1208         goto err;
1209 
1210     if (data->input) {
1211         rmi_driver_set_input_name(rmi_dev, data->input);
1212         if (!rmi_dev->xport->input) {
1213             retval = input_register_device(data->input);
1214             if (retval) {
1215                 dev_err(dev, "%s: Failed to register input device.\n",
1216                     __func__);
1217                 goto err_destroy_functions;
1218             }
1219         }
1220     }
1221 
1222     retval = rmi_irq_init(rmi_dev);
1223     if (retval < 0)
1224         goto err_destroy_functions;
1225 
1226     if (data->f01_container->dev.driver) {
1227         /* Driver already bound, so enable ATTN now. */
1228         retval = rmi_enable_sensor(rmi_dev);
1229         if (retval)
1230             goto err_disable_irq;
1231     }
1232 
1233     return 0;
1234 
1235 err_disable_irq:
1236     rmi_disable_irq(rmi_dev, false);
1237 err_destroy_functions:
1238     rmi_free_function_list(rmi_dev);
1239 err:
1240     return retval;
1241 }
1242 
1243 static struct rmi_driver rmi_physical_driver = {
1244     .driver = {
1245         .owner  = THIS_MODULE,
1246         .name   = "rmi4_physical",
1247         .bus    = &rmi_bus_type,
1248         .probe = rmi_driver_probe,
1249         .remove = rmi_driver_remove,
1250     },
1251     .reset_handler = rmi_driver_reset_handler,
1252     .clear_irq_bits = rmi_driver_clear_irq_bits,
1253     .set_irq_bits = rmi_driver_set_irq_bits,
1254     .set_input_params = rmi_driver_set_input_params,
1255 };
1256 
1257 bool rmi_is_physical_driver(struct device_driver *drv)
1258 {
1259     return drv == &rmi_physical_driver.driver;
1260 }
1261 
1262 int __init rmi_register_physical_driver(void)
1263 {
1264     int error;
1265 
1266     error = driver_register(&rmi_physical_driver.driver);
1267     if (error) {
1268         pr_err("%s: driver register failed, code=%d.\n", __func__,
1269                error);
1270         return error;
1271     }
1272 
1273     return 0;
1274 }
1275 
1276 void __exit rmi_unregister_physical_driver(void)
1277 {
1278     driver_unregister(&rmi_physical_driver.driver);
1279 }