1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22b6a321dSAndrew Duggan /*
32b6a321dSAndrew Duggan * Copyright (c) 2011-2016 Synaptics Incorporated
42b6a321dSAndrew Duggan * Copyright (c) 2011 Unixphere
52b6a321dSAndrew Duggan *
62b6a321dSAndrew Duggan * This driver provides the core support for a single RMI4-based device.
72b6a321dSAndrew Duggan *
82b6a321dSAndrew Duggan * The RMI4 specification can be found here (URL split for line length):
92b6a321dSAndrew Duggan *
102b6a321dSAndrew Duggan * http://www.synaptics.com/sites/default/files/
112b6a321dSAndrew Duggan * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
122b6a321dSAndrew Duggan */
132b6a321dSAndrew Duggan
142b6a321dSAndrew Duggan #include <linux/bitmap.h>
152b6a321dSAndrew Duggan #include <linux/delay.h>
162b6a321dSAndrew Duggan #include <linux/fs.h>
173aeed5b5SBjorn Andersson #include <linux/irq.h>
182b6a321dSAndrew Duggan #include <linux/pm.h>
192b6a321dSAndrew Duggan #include <linux/slab.h>
20d8a8b3edSAndrew Duggan #include <linux/of.h>
2124d28e4fSNick Dyer #include <linux/irqdomain.h>
222b6a321dSAndrew Duggan #include <uapi/linux/input.h>
232b6a321dSAndrew Duggan #include <linux/rmi.h>
242b6a321dSAndrew Duggan #include "rmi_bus.h"
252b6a321dSAndrew Duggan #include "rmi_driver.h"
262b6a321dSAndrew Duggan
272b6a321dSAndrew Duggan #define HAS_NONSTANDARD_PDT_MASK 0x40
282b6a321dSAndrew Duggan #define RMI4_MAX_PAGE 0xff
292b6a321dSAndrew Duggan #define RMI4_PAGE_SIZE 0x100
302b6a321dSAndrew Duggan #define RMI4_PAGE_MASK 0xFF00
312b6a321dSAndrew Duggan
322b6a321dSAndrew Duggan #define RMI_DEVICE_RESET_CMD 0x01
332b6a321dSAndrew Duggan #define DEFAULT_RESET_DELAY_MS 100
342b6a321dSAndrew Duggan
rmi_free_function_list(struct rmi_device * rmi_dev)3529fd0ec2SNick Dyer void rmi_free_function_list(struct rmi_device *rmi_dev)
362b6a321dSAndrew Duggan {
372b6a321dSAndrew Duggan struct rmi_function *fn, *tmp;
382b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
392b6a321dSAndrew Duggan
406bd0dcfaSNick Dyer rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
416bd0dcfaSNick Dyer
42a1ab6902SDmitry Torokhov /* Doing it in the reverse order so F01 will be removed last */
43a1ab6902SDmitry Torokhov list_for_each_entry_safe_reverse(fn, tmp,
44a1ab6902SDmitry Torokhov &data->function_list, node) {
45a1ab6902SDmitry Torokhov list_del(&fn->node);
46a1ab6902SDmitry Torokhov rmi_unregister_function(fn);
47a1ab6902SDmitry Torokhov }
48a1ab6902SDmitry Torokhov
4929fd0ec2SNick Dyer devm_kfree(&rmi_dev->dev, data->irq_memory);
5029fd0ec2SNick Dyer data->irq_memory = NULL;
5129fd0ec2SNick Dyer data->irq_status = NULL;
5229fd0ec2SNick Dyer data->fn_irq_bits = NULL;
5329fd0ec2SNick Dyer data->current_irq_mask = NULL;
5429fd0ec2SNick Dyer data->new_irq_mask = NULL;
5529fd0ec2SNick Dyer
562b6a321dSAndrew Duggan data->f01_container = NULL;
5729fd0ec2SNick Dyer data->f34_container = NULL;
582b6a321dSAndrew Duggan }
592b6a321dSAndrew Duggan
reset_one_function(struct rmi_function * fn)602b6a321dSAndrew Duggan static int reset_one_function(struct rmi_function *fn)
612b6a321dSAndrew Duggan {
622b6a321dSAndrew Duggan struct rmi_function_handler *fh;
632b6a321dSAndrew Duggan int retval = 0;
642b6a321dSAndrew Duggan
652b6a321dSAndrew Duggan if (!fn || !fn->dev.driver)
662b6a321dSAndrew Duggan return 0;
672b6a321dSAndrew Duggan
682b6a321dSAndrew Duggan fh = to_rmi_function_handler(fn->dev.driver);
692b6a321dSAndrew Duggan if (fh->reset) {
702b6a321dSAndrew Duggan retval = fh->reset(fn);
712b6a321dSAndrew Duggan if (retval < 0)
722b6a321dSAndrew Duggan dev_err(&fn->dev, "Reset failed with code %d.\n",
732b6a321dSAndrew Duggan retval);
742b6a321dSAndrew Duggan }
752b6a321dSAndrew Duggan
762b6a321dSAndrew Duggan return retval;
772b6a321dSAndrew Duggan }
782b6a321dSAndrew Duggan
configure_one_function(struct rmi_function * fn)792b6a321dSAndrew Duggan static int configure_one_function(struct rmi_function *fn)
802b6a321dSAndrew Duggan {
812b6a321dSAndrew Duggan struct rmi_function_handler *fh;
822b6a321dSAndrew Duggan int retval = 0;
832b6a321dSAndrew Duggan
842b6a321dSAndrew Duggan if (!fn || !fn->dev.driver)
852b6a321dSAndrew Duggan return 0;
862b6a321dSAndrew Duggan
872b6a321dSAndrew Duggan fh = to_rmi_function_handler(fn->dev.driver);
882b6a321dSAndrew Duggan if (fh->config) {
892b6a321dSAndrew Duggan retval = fh->config(fn);
902b6a321dSAndrew Duggan if (retval < 0)
912b6a321dSAndrew Duggan dev_err(&fn->dev, "Config failed with code %d.\n",
922b6a321dSAndrew Duggan retval);
932b6a321dSAndrew Duggan }
942b6a321dSAndrew Duggan
952b6a321dSAndrew Duggan return retval;
962b6a321dSAndrew Duggan }
972b6a321dSAndrew Duggan
rmi_driver_process_reset_requests(struct rmi_device * rmi_dev)982b6a321dSAndrew Duggan static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
992b6a321dSAndrew Duggan {
1002b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
1012b6a321dSAndrew Duggan struct rmi_function *entry;
1022b6a321dSAndrew Duggan int retval;
1032b6a321dSAndrew Duggan
1042b6a321dSAndrew Duggan list_for_each_entry(entry, &data->function_list, node) {
1052b6a321dSAndrew Duggan retval = reset_one_function(entry);
1062b6a321dSAndrew Duggan if (retval < 0)
1072b6a321dSAndrew Duggan return retval;
1082b6a321dSAndrew Duggan }
1092b6a321dSAndrew Duggan
1102b6a321dSAndrew Duggan return 0;
1112b6a321dSAndrew Duggan }
1122b6a321dSAndrew Duggan
rmi_driver_process_config_requests(struct rmi_device * rmi_dev)1132b6a321dSAndrew Duggan static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
1142b6a321dSAndrew Duggan {
1152b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
1162b6a321dSAndrew Duggan struct rmi_function *entry;
1172b6a321dSAndrew Duggan int retval;
1182b6a321dSAndrew Duggan
1192b6a321dSAndrew Duggan list_for_each_entry(entry, &data->function_list, node) {
1202b6a321dSAndrew Duggan retval = configure_one_function(entry);
1212b6a321dSAndrew Duggan if (retval < 0)
1222b6a321dSAndrew Duggan return retval;
1232b6a321dSAndrew Duggan }
1242b6a321dSAndrew Duggan
1252b6a321dSAndrew Duggan return 0;
1262b6a321dSAndrew Duggan }
1272b6a321dSAndrew Duggan
rmi_process_interrupt_requests(struct rmi_device * rmi_dev)1283aeed5b5SBjorn Andersson static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
1292b6a321dSAndrew Duggan {
1302b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
1312b6a321dSAndrew Duggan struct device *dev = &rmi_dev->dev;
13224d28e4fSNick Dyer int i;
1332b6a321dSAndrew Duggan int error;
1342b6a321dSAndrew Duggan
1352b6a321dSAndrew Duggan if (!data)
1362b6a321dSAndrew Duggan return 0;
1372b6a321dSAndrew Duggan
138ae9979c3SBenjamin Tissoires if (!data->attn_data.data) {
1392b6a321dSAndrew Duggan error = rmi_read_block(rmi_dev,
1402b6a321dSAndrew Duggan data->f01_container->fd.data_base_addr + 1,
1412b6a321dSAndrew Duggan data->irq_status, data->num_of_irq_regs);
1422b6a321dSAndrew Duggan if (error < 0) {
1432b6a321dSAndrew Duggan dev_err(dev, "Failed to read irqs, code=%d\n", error);
1442b6a321dSAndrew Duggan return error;
1452b6a321dSAndrew Duggan }
1462b6a321dSAndrew Duggan }
1472b6a321dSAndrew Duggan
1482b6a321dSAndrew Duggan mutex_lock(&data->irq_mutex);
149363c5387SEvan Green bitmap_and(data->irq_status, data->irq_status, data->fn_irq_bits,
1502b6a321dSAndrew Duggan data->irq_count);
1512b6a321dSAndrew Duggan /*
1522b6a321dSAndrew Duggan * At this point, irq_status has all bits that are set in the
1532b6a321dSAndrew Duggan * interrupt status register and are enabled.
1542b6a321dSAndrew Duggan */
1552b6a321dSAndrew Duggan mutex_unlock(&data->irq_mutex);
1562b6a321dSAndrew Duggan
15724d28e4fSNick Dyer for_each_set_bit(i, data->irq_status, data->irq_count)
15824d28e4fSNick Dyer handle_nested_irq(irq_find_mapping(data->irqdomain, i));
1592b6a321dSAndrew Duggan
1602b6a321dSAndrew Duggan if (data->input)
1612b6a321dSAndrew Duggan input_sync(data->input);
1622b6a321dSAndrew Duggan
1632b6a321dSAndrew Duggan return 0;
1642b6a321dSAndrew Duggan }
1653aeed5b5SBjorn Andersson
rmi_set_attn_data(struct rmi_device * rmi_dev,unsigned long irq_status,void * data,size_t size)166b908d3cdSBenjamin Tissoires void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
167b908d3cdSBenjamin Tissoires void *data, size_t size)
168b908d3cdSBenjamin Tissoires {
169b908d3cdSBenjamin Tissoires struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
170b908d3cdSBenjamin Tissoires struct rmi4_attn_data attn_data;
171b908d3cdSBenjamin Tissoires void *fifo_data;
172b908d3cdSBenjamin Tissoires
173b908d3cdSBenjamin Tissoires if (!drvdata->enabled)
174b908d3cdSBenjamin Tissoires return;
175b908d3cdSBenjamin Tissoires
176b908d3cdSBenjamin Tissoires fifo_data = kmemdup(data, size, GFP_ATOMIC);
177b908d3cdSBenjamin Tissoires if (!fifo_data)
178b908d3cdSBenjamin Tissoires return;
179b908d3cdSBenjamin Tissoires
180b908d3cdSBenjamin Tissoires attn_data.irq_status = irq_status;
181b908d3cdSBenjamin Tissoires attn_data.size = size;
182b908d3cdSBenjamin Tissoires attn_data.data = fifo_data;
183b908d3cdSBenjamin Tissoires
184b908d3cdSBenjamin Tissoires kfifo_put(&drvdata->attn_fifo, attn_data);
185b908d3cdSBenjamin Tissoires }
186b908d3cdSBenjamin Tissoires EXPORT_SYMBOL_GPL(rmi_set_attn_data);
187b908d3cdSBenjamin Tissoires
rmi_irq_fn(int irq,void * dev_id)1883aeed5b5SBjorn Andersson static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
1893aeed5b5SBjorn Andersson {
1903aeed5b5SBjorn Andersson struct rmi_device *rmi_dev = dev_id;
191b908d3cdSBenjamin Tissoires struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
192b908d3cdSBenjamin Tissoires struct rmi4_attn_data attn_data = {0};
193b908d3cdSBenjamin Tissoires int ret, count;
194b908d3cdSBenjamin Tissoires
195b908d3cdSBenjamin Tissoires count = kfifo_get(&drvdata->attn_fifo, &attn_data);
196b908d3cdSBenjamin Tissoires if (count) {
197b908d3cdSBenjamin Tissoires *(drvdata->irq_status) = attn_data.irq_status;
198ae9979c3SBenjamin Tissoires drvdata->attn_data = attn_data;
199b908d3cdSBenjamin Tissoires }
2003aeed5b5SBjorn Andersson
2013aeed5b5SBjorn Andersson ret = rmi_process_interrupt_requests(rmi_dev);
2023aeed5b5SBjorn Andersson if (ret)
2033aeed5b5SBjorn Andersson rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
2043aeed5b5SBjorn Andersson "Failed to process interrupt request: %d\n", ret);
2053aeed5b5SBjorn Andersson
20655edde9fSNick Desaulniers if (count) {
207b908d3cdSBenjamin Tissoires kfree(attn_data.data);
208d5a5e5b5SEvan Green drvdata->attn_data.data = NULL;
20955edde9fSNick Desaulniers }
210b908d3cdSBenjamin Tissoires
211b908d3cdSBenjamin Tissoires if (!kfifo_is_empty(&drvdata->attn_fifo))
212b908d3cdSBenjamin Tissoires return rmi_irq_fn(irq, dev_id);
213b908d3cdSBenjamin Tissoires
2143aeed5b5SBjorn Andersson return IRQ_HANDLED;
2153aeed5b5SBjorn Andersson }
2163aeed5b5SBjorn Andersson
rmi_irq_init(struct rmi_device * rmi_dev)2173aeed5b5SBjorn Andersson static int rmi_irq_init(struct rmi_device *rmi_dev)
2183aeed5b5SBjorn Andersson {
2193aeed5b5SBjorn Andersson struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
220a64ea311SBenjamin Tissoires struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
2213aeed5b5SBjorn Andersson int irq_flags = irq_get_trigger_type(pdata->irq);
2223aeed5b5SBjorn Andersson int ret;
2233aeed5b5SBjorn Andersson
2243aeed5b5SBjorn Andersson if (!irq_flags)
2253aeed5b5SBjorn Andersson irq_flags = IRQF_TRIGGER_LOW;
2263aeed5b5SBjorn Andersson
2273aeed5b5SBjorn Andersson ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
2283aeed5b5SBjorn Andersson rmi_irq_fn, irq_flags | IRQF_ONESHOT,
22972fe3870SNick Dyer dev_driver_string(rmi_dev->xport->dev),
2303aeed5b5SBjorn Andersson rmi_dev);
2313aeed5b5SBjorn Andersson if (ret < 0) {
2323aeed5b5SBjorn Andersson dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
2333aeed5b5SBjorn Andersson pdata->irq);
2343aeed5b5SBjorn Andersson
2353aeed5b5SBjorn Andersson return ret;
2363aeed5b5SBjorn Andersson }
2373aeed5b5SBjorn Andersson
238a64ea311SBenjamin Tissoires data->enabled = true;
239a64ea311SBenjamin Tissoires
2403aeed5b5SBjorn Andersson return 0;
2413aeed5b5SBjorn Andersson }
2422b6a321dSAndrew Duggan
rmi_find_function(struct rmi_device * rmi_dev,u8 number)243f32361b7SBenjamin Tissoires struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
244f32361b7SBenjamin Tissoires {
245f32361b7SBenjamin Tissoires struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
246f32361b7SBenjamin Tissoires struct rmi_function *entry;
247f32361b7SBenjamin Tissoires
248f32361b7SBenjamin Tissoires list_for_each_entry(entry, &data->function_list, node) {
249f32361b7SBenjamin Tissoires if (entry->fd.function_number == number)
250f32361b7SBenjamin Tissoires return entry;
251f32361b7SBenjamin Tissoires }
252f32361b7SBenjamin Tissoires
253f32361b7SBenjamin Tissoires return NULL;
254f32361b7SBenjamin Tissoires }
255f32361b7SBenjamin Tissoires
suspend_one_function(struct rmi_function * fn)2562b6a321dSAndrew Duggan static int suspend_one_function(struct rmi_function *fn)
2572b6a321dSAndrew Duggan {
2582b6a321dSAndrew Duggan struct rmi_function_handler *fh;
2592b6a321dSAndrew Duggan int retval = 0;
2602b6a321dSAndrew Duggan
2612b6a321dSAndrew Duggan if (!fn || !fn->dev.driver)
2622b6a321dSAndrew Duggan return 0;
2632b6a321dSAndrew Duggan
2642b6a321dSAndrew Duggan fh = to_rmi_function_handler(fn->dev.driver);
2652b6a321dSAndrew Duggan if (fh->suspend) {
2662b6a321dSAndrew Duggan retval = fh->suspend(fn);
2672b6a321dSAndrew Duggan if (retval < 0)
2682b6a321dSAndrew Duggan dev_err(&fn->dev, "Suspend failed with code %d.\n",
2692b6a321dSAndrew Duggan retval);
2702b6a321dSAndrew Duggan }
2712b6a321dSAndrew Duggan
2722b6a321dSAndrew Duggan return retval;
2732b6a321dSAndrew Duggan }
2742b6a321dSAndrew Duggan
rmi_suspend_functions(struct rmi_device * rmi_dev)2752b6a321dSAndrew Duggan static int rmi_suspend_functions(struct rmi_device *rmi_dev)
2762b6a321dSAndrew Duggan {
2772b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
2782b6a321dSAndrew Duggan struct rmi_function *entry;
2792b6a321dSAndrew Duggan int retval;
2802b6a321dSAndrew Duggan
2812b6a321dSAndrew Duggan list_for_each_entry(entry, &data->function_list, node) {
2822b6a321dSAndrew Duggan retval = suspend_one_function(entry);
2832b6a321dSAndrew Duggan if (retval < 0)
2842b6a321dSAndrew Duggan return retval;
2852b6a321dSAndrew Duggan }
2862b6a321dSAndrew Duggan
2872b6a321dSAndrew Duggan return 0;
2882b6a321dSAndrew Duggan }
2892b6a321dSAndrew Duggan
resume_one_function(struct rmi_function * fn)2902b6a321dSAndrew Duggan static int resume_one_function(struct rmi_function *fn)
2912b6a321dSAndrew Duggan {
2922b6a321dSAndrew Duggan struct rmi_function_handler *fh;
2932b6a321dSAndrew Duggan int retval = 0;
2942b6a321dSAndrew Duggan
2952b6a321dSAndrew Duggan if (!fn || !fn->dev.driver)
2962b6a321dSAndrew Duggan return 0;
2972b6a321dSAndrew Duggan
2982b6a321dSAndrew Duggan fh = to_rmi_function_handler(fn->dev.driver);
2992b6a321dSAndrew Duggan if (fh->resume) {
3002b6a321dSAndrew Duggan retval = fh->resume(fn);
3012b6a321dSAndrew Duggan if (retval < 0)
3022b6a321dSAndrew Duggan dev_err(&fn->dev, "Resume failed with code %d.\n",
3032b6a321dSAndrew Duggan retval);
3042b6a321dSAndrew Duggan }
3052b6a321dSAndrew Duggan
3062b6a321dSAndrew Duggan return retval;
3072b6a321dSAndrew Duggan }
3082b6a321dSAndrew Duggan
rmi_resume_functions(struct rmi_device * rmi_dev)3092b6a321dSAndrew Duggan static int rmi_resume_functions(struct rmi_device *rmi_dev)
3102b6a321dSAndrew Duggan {
3112b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
3122b6a321dSAndrew Duggan struct rmi_function *entry;
3132b6a321dSAndrew Duggan int retval;
3142b6a321dSAndrew Duggan
3152b6a321dSAndrew Duggan list_for_each_entry(entry, &data->function_list, node) {
3162b6a321dSAndrew Duggan retval = resume_one_function(entry);
3172b6a321dSAndrew Duggan if (retval < 0)
3182b6a321dSAndrew Duggan return retval;
3192b6a321dSAndrew Duggan }
3202b6a321dSAndrew Duggan
3212b6a321dSAndrew Duggan return 0;
3222b6a321dSAndrew Duggan }
3232b6a321dSAndrew Duggan
rmi_enable_sensor(struct rmi_device * rmi_dev)32429fd0ec2SNick Dyer int rmi_enable_sensor(struct rmi_device *rmi_dev)
3252b6a321dSAndrew Duggan {
3262b6a321dSAndrew Duggan int retval = 0;
3272b6a321dSAndrew Duggan
3282b6a321dSAndrew Duggan retval = rmi_driver_process_config_requests(rmi_dev);
3292b6a321dSAndrew Duggan if (retval < 0)
3302b6a321dSAndrew Duggan return retval;
3312b6a321dSAndrew Duggan
3322b6a321dSAndrew Duggan return rmi_process_interrupt_requests(rmi_dev);
3332b6a321dSAndrew Duggan }
3342b6a321dSAndrew Duggan
3352b6a321dSAndrew Duggan /**
3362b6a321dSAndrew Duggan * rmi_driver_set_input_params - set input device id and other data.
3372b6a321dSAndrew Duggan *
3382b6a321dSAndrew Duggan * @rmi_dev: Pointer to an RMI device
3392b6a321dSAndrew Duggan * @input: Pointer to input device
3402b6a321dSAndrew Duggan *
3412b6a321dSAndrew Duggan */
rmi_driver_set_input_params(struct rmi_device * rmi_dev,struct input_dev * input)3422b6a321dSAndrew Duggan static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
3432b6a321dSAndrew Duggan struct input_dev *input)
3442b6a321dSAndrew Duggan {
3452b6a321dSAndrew Duggan input->name = SYNAPTICS_INPUT_DEVICE_NAME;
3462b6a321dSAndrew Duggan input->id.vendor = SYNAPTICS_VENDOR_ID;
3472b6a321dSAndrew Duggan input->id.bustype = BUS_RMI;
3482b6a321dSAndrew Duggan return 0;
3492b6a321dSAndrew Duggan }
3502b6a321dSAndrew Duggan
rmi_driver_set_input_name(struct rmi_device * rmi_dev,struct input_dev * input)3512b6a321dSAndrew Duggan static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
3522b6a321dSAndrew Duggan struct input_dev *input)
3532b6a321dSAndrew Duggan {
3542b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
355ce363f0dSNick Dyer const char *device_name = rmi_f01_get_product_ID(data->f01_container);
3562b6a321dSAndrew Duggan char *name;
3572b6a321dSAndrew Duggan
3582b6a321dSAndrew Duggan name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
3592b6a321dSAndrew Duggan "Synaptics %s", device_name);
3602b6a321dSAndrew Duggan if (!name)
3612b6a321dSAndrew Duggan return;
3622b6a321dSAndrew Duggan
3632b6a321dSAndrew Duggan input->name = name;
3642b6a321dSAndrew Duggan }
3652b6a321dSAndrew Duggan
rmi_driver_set_irq_bits(struct rmi_device * rmi_dev,unsigned long * mask)3662b6a321dSAndrew Duggan static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
3672b6a321dSAndrew Duggan unsigned long *mask)
3682b6a321dSAndrew Duggan {
3692b6a321dSAndrew Duggan int error = 0;
3702b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
3712b6a321dSAndrew Duggan struct device *dev = &rmi_dev->dev;
3722b6a321dSAndrew Duggan
3732b6a321dSAndrew Duggan mutex_lock(&data->irq_mutex);
3742b6a321dSAndrew Duggan bitmap_or(data->new_irq_mask,
3752b6a321dSAndrew Duggan data->current_irq_mask, mask, data->irq_count);
3762b6a321dSAndrew Duggan
3772b6a321dSAndrew Duggan error = rmi_write_block(rmi_dev,
3782b6a321dSAndrew Duggan data->f01_container->fd.control_base_addr + 1,
3792b6a321dSAndrew Duggan data->new_irq_mask, data->num_of_irq_regs);
3802b6a321dSAndrew Duggan if (error < 0) {
3812b6a321dSAndrew Duggan dev_err(dev, "%s: Failed to change enabled interrupts!",
3822b6a321dSAndrew Duggan __func__);
3832b6a321dSAndrew Duggan goto error_unlock;
3842b6a321dSAndrew Duggan }
3852b6a321dSAndrew Duggan bitmap_copy(data->current_irq_mask, data->new_irq_mask,
3862b6a321dSAndrew Duggan data->num_of_irq_regs);
3872b6a321dSAndrew Duggan
388363c5387SEvan Green bitmap_or(data->fn_irq_bits, data->fn_irq_bits, mask, data->irq_count);
389363c5387SEvan Green
3902b6a321dSAndrew Duggan error_unlock:
3912b6a321dSAndrew Duggan mutex_unlock(&data->irq_mutex);
3922b6a321dSAndrew Duggan return error;
3932b6a321dSAndrew Duggan }
3942b6a321dSAndrew Duggan
rmi_driver_clear_irq_bits(struct rmi_device * rmi_dev,unsigned long * mask)3952b6a321dSAndrew Duggan static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
3962b6a321dSAndrew Duggan unsigned long *mask)
3972b6a321dSAndrew Duggan {
3982b6a321dSAndrew Duggan int error = 0;
3992b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
4002b6a321dSAndrew Duggan struct device *dev = &rmi_dev->dev;
4012b6a321dSAndrew Duggan
4022b6a321dSAndrew Duggan mutex_lock(&data->irq_mutex);
403363c5387SEvan Green bitmap_andnot(data->fn_irq_bits,
404363c5387SEvan Green data->fn_irq_bits, mask, data->irq_count);
4052b6a321dSAndrew Duggan bitmap_andnot(data->new_irq_mask,
4062b6a321dSAndrew Duggan data->current_irq_mask, mask, data->irq_count);
4072b6a321dSAndrew Duggan
4082b6a321dSAndrew Duggan error = rmi_write_block(rmi_dev,
4092b6a321dSAndrew Duggan data->f01_container->fd.control_base_addr + 1,
4102b6a321dSAndrew Duggan data->new_irq_mask, data->num_of_irq_regs);
4112b6a321dSAndrew Duggan if (error < 0) {
4122b6a321dSAndrew Duggan dev_err(dev, "%s: Failed to change enabled interrupts!",
4132b6a321dSAndrew Duggan __func__);
4142b6a321dSAndrew Duggan goto error_unlock;
4152b6a321dSAndrew Duggan }
4162b6a321dSAndrew Duggan bitmap_copy(data->current_irq_mask, data->new_irq_mask,
4172b6a321dSAndrew Duggan data->num_of_irq_regs);
4182b6a321dSAndrew Duggan
4192b6a321dSAndrew Duggan error_unlock:
4202b6a321dSAndrew Duggan mutex_unlock(&data->irq_mutex);
4212b6a321dSAndrew Duggan return error;
4222b6a321dSAndrew Duggan }
4232b6a321dSAndrew Duggan
rmi_driver_reset_handler(struct rmi_device * rmi_dev)4242b6a321dSAndrew Duggan static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
4252b6a321dSAndrew Duggan {
4262b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
4272b6a321dSAndrew Duggan int error;
4282b6a321dSAndrew Duggan
4292b6a321dSAndrew Duggan /*
4302b6a321dSAndrew Duggan * Can get called before the driver is fully ready to deal with
4312b6a321dSAndrew Duggan * this situation.
4322b6a321dSAndrew Duggan */
4332b6a321dSAndrew Duggan if (!data || !data->f01_container) {
4342b6a321dSAndrew Duggan dev_warn(&rmi_dev->dev,
4352b6a321dSAndrew Duggan "Not ready to handle reset yet!\n");
4362b6a321dSAndrew Duggan return 0;
4372b6a321dSAndrew Duggan }
4382b6a321dSAndrew Duggan
4392b6a321dSAndrew Duggan error = rmi_read_block(rmi_dev,
4402b6a321dSAndrew Duggan data->f01_container->fd.control_base_addr + 1,
4412b6a321dSAndrew Duggan data->current_irq_mask, data->num_of_irq_regs);
4422b6a321dSAndrew Duggan if (error < 0) {
4432b6a321dSAndrew Duggan dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
4442b6a321dSAndrew Duggan __func__);
4452b6a321dSAndrew Duggan return error;
4462b6a321dSAndrew Duggan }
4472b6a321dSAndrew Duggan
4482b6a321dSAndrew Duggan error = rmi_driver_process_reset_requests(rmi_dev);
4492b6a321dSAndrew Duggan if (error < 0)
4502b6a321dSAndrew Duggan return error;
4512b6a321dSAndrew Duggan
4522b6a321dSAndrew Duggan error = rmi_driver_process_config_requests(rmi_dev);
4532b6a321dSAndrew Duggan if (error < 0)
4542b6a321dSAndrew Duggan return error;
4552b6a321dSAndrew Duggan
4562b6a321dSAndrew Duggan return 0;
4572b6a321dSAndrew Duggan }
4582b6a321dSAndrew Duggan
rmi_read_pdt_entry(struct rmi_device * rmi_dev,struct pdt_entry * entry,u16 pdt_address)459e9dade41SBenjamin Tissoires static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
460e9dade41SBenjamin Tissoires struct pdt_entry *entry, u16 pdt_address)
4612b6a321dSAndrew Duggan {
4622b6a321dSAndrew Duggan u8 buf[RMI_PDT_ENTRY_SIZE];
4632b6a321dSAndrew Duggan int error;
4642b6a321dSAndrew Duggan
4652b6a321dSAndrew Duggan error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
4662b6a321dSAndrew Duggan if (error) {
4672b6a321dSAndrew Duggan dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
4682b6a321dSAndrew Duggan pdt_address, error);
4692b6a321dSAndrew Duggan return error;
4702b6a321dSAndrew Duggan }
4712b6a321dSAndrew Duggan
4722b6a321dSAndrew Duggan entry->page_start = pdt_address & RMI4_PAGE_MASK;
4732b6a321dSAndrew Duggan entry->query_base_addr = buf[0];
4742b6a321dSAndrew Duggan entry->command_base_addr = buf[1];
4752b6a321dSAndrew Duggan entry->control_base_addr = buf[2];
4762b6a321dSAndrew Duggan entry->data_base_addr = buf[3];
4772b6a321dSAndrew Duggan entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
4782b6a321dSAndrew Duggan entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
4792b6a321dSAndrew Duggan entry->function_number = buf[5];
4802b6a321dSAndrew Duggan
4812b6a321dSAndrew Duggan return 0;
4822b6a321dSAndrew Duggan }
4832b6a321dSAndrew Duggan
rmi_driver_copy_pdt_to_fd(const struct pdt_entry * pdt,struct rmi_function_descriptor * fd)4842b6a321dSAndrew Duggan static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
4852b6a321dSAndrew Duggan struct rmi_function_descriptor *fd)
4862b6a321dSAndrew Duggan {
4872b6a321dSAndrew Duggan fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
4882b6a321dSAndrew Duggan fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
4892b6a321dSAndrew Duggan fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
4902b6a321dSAndrew Duggan fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
4912b6a321dSAndrew Duggan fd->function_number = pdt->function_number;
4922b6a321dSAndrew Duggan fd->interrupt_source_count = pdt->interrupt_source_count;
4932b6a321dSAndrew Duggan fd->function_version = pdt->function_version;
4942b6a321dSAndrew Duggan }
4952b6a321dSAndrew Duggan
4962b6a321dSAndrew Duggan #define RMI_SCAN_CONTINUE 0
4972b6a321dSAndrew Duggan #define RMI_SCAN_DONE 1
4982b6a321dSAndrew Duggan
rmi_scan_pdt_page(struct rmi_device * rmi_dev,int page,int * empty_pages,void * ctx,int (* callback)(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * entry))4992b6a321dSAndrew Duggan static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
5002b6a321dSAndrew Duggan int page,
501ad338e8bSNick Dyer int *empty_pages,
5022b6a321dSAndrew Duggan void *ctx,
5032b6a321dSAndrew Duggan int (*callback)(struct rmi_device *rmi_dev,
5042b6a321dSAndrew Duggan void *ctx,
5052b6a321dSAndrew Duggan const struct pdt_entry *entry))
5062b6a321dSAndrew Duggan {
5072b6a321dSAndrew Duggan struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
5082b6a321dSAndrew Duggan struct pdt_entry pdt_entry;
5092b6a321dSAndrew Duggan u16 page_start = RMI4_PAGE_SIZE * page;
5102b6a321dSAndrew Duggan u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
5112b6a321dSAndrew Duggan u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
5122b6a321dSAndrew Duggan u16 addr;
5132b6a321dSAndrew Duggan int error;
5142b6a321dSAndrew Duggan int retval;
5152b6a321dSAndrew Duggan
5162b6a321dSAndrew Duggan for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
5172b6a321dSAndrew Duggan error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
5182b6a321dSAndrew Duggan if (error)
5192b6a321dSAndrew Duggan return error;
5202b6a321dSAndrew Duggan
5212b6a321dSAndrew Duggan if (RMI4_END_OF_PDT(pdt_entry.function_number))
5222b6a321dSAndrew Duggan break;
5232b6a321dSAndrew Duggan
5242b6a321dSAndrew Duggan retval = callback(rmi_dev, ctx, &pdt_entry);
5252b6a321dSAndrew Duggan if (retval != RMI_SCAN_CONTINUE)
5262b6a321dSAndrew Duggan return retval;
5272b6a321dSAndrew Duggan }
5282b6a321dSAndrew Duggan
529ad338e8bSNick Dyer /*
530ad338e8bSNick Dyer * Count number of empty PDT pages. If a gap of two pages
531ad338e8bSNick Dyer * or more is found, stop scanning.
532ad338e8bSNick Dyer */
533ad338e8bSNick Dyer if (addr == pdt_start)
534ad338e8bSNick Dyer ++*empty_pages;
535ad338e8bSNick Dyer else
536ad338e8bSNick Dyer *empty_pages = 0;
537ad338e8bSNick Dyer
5385191d88aSNick Dyer return (data->bootloader_mode || *empty_pages >= 2) ?
5392b6a321dSAndrew Duggan RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
5402b6a321dSAndrew Duggan }
5412b6a321dSAndrew Duggan
rmi_scan_pdt(struct rmi_device * rmi_dev,void * ctx,int (* callback)(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * entry))54229fd0ec2SNick Dyer int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
5432b6a321dSAndrew Duggan int (*callback)(struct rmi_device *rmi_dev,
54429fd0ec2SNick Dyer void *ctx, const struct pdt_entry *entry))
5452b6a321dSAndrew Duggan {
5462b6a321dSAndrew Duggan int page;
547ad338e8bSNick Dyer int empty_pages = 0;
5482b6a321dSAndrew Duggan int retval = RMI_SCAN_DONE;
5492b6a321dSAndrew Duggan
5502b6a321dSAndrew Duggan for (page = 0; page <= RMI4_MAX_PAGE; page++) {
551ad338e8bSNick Dyer retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
552ad338e8bSNick Dyer ctx, callback);
5532b6a321dSAndrew Duggan if (retval != RMI_SCAN_CONTINUE)
5542b6a321dSAndrew Duggan break;
5552b6a321dSAndrew Duggan }
5562b6a321dSAndrew Duggan
5572b6a321dSAndrew Duggan return retval < 0 ? retval : 0;
5582b6a321dSAndrew Duggan }
5592b6a321dSAndrew Duggan
rmi_read_register_desc(struct rmi_device * d,u16 addr,struct rmi_register_descriptor * rdesc)5602b6a321dSAndrew Duggan int rmi_read_register_desc(struct rmi_device *d, u16 addr,
5612b6a321dSAndrew Duggan struct rmi_register_descriptor *rdesc)
5622b6a321dSAndrew Duggan {
5632b6a321dSAndrew Duggan int ret;
5642b6a321dSAndrew Duggan u8 size_presence_reg;
5652b6a321dSAndrew Duggan u8 buf[35];
5662b6a321dSAndrew Duggan int presense_offset = 1;
5672b6a321dSAndrew Duggan u8 *struct_buf;
5682b6a321dSAndrew Duggan int reg;
5692b6a321dSAndrew Duggan int offset = 0;
5702b6a321dSAndrew Duggan int map_offset = 0;
5712b6a321dSAndrew Duggan int i;
5722b6a321dSAndrew Duggan int b;
5732b6a321dSAndrew Duggan
5742b6a321dSAndrew Duggan /*
5752b6a321dSAndrew Duggan * The first register of the register descriptor is the size of
5762b6a321dSAndrew Duggan * the register descriptor's presense register.
5772b6a321dSAndrew Duggan */
5782b6a321dSAndrew Duggan ret = rmi_read(d, addr, &size_presence_reg);
5792b6a321dSAndrew Duggan if (ret)
5802b6a321dSAndrew Duggan return ret;
5812b6a321dSAndrew Duggan ++addr;
5822b6a321dSAndrew Duggan
5832b6a321dSAndrew Duggan if (size_presence_reg < 0 || size_presence_reg > 35)
5842b6a321dSAndrew Duggan return -EIO;
5852b6a321dSAndrew Duggan
5862b6a321dSAndrew Duggan memset(buf, 0, sizeof(buf));
5872b6a321dSAndrew Duggan
5882b6a321dSAndrew Duggan /*
5892b6a321dSAndrew Duggan * The presence register contains the size of the register structure
5902b6a321dSAndrew Duggan * and a bitmap which identified which packet registers are present
5912b6a321dSAndrew Duggan * for this particular register type (ie query, control, or data).
5922b6a321dSAndrew Duggan */
5932b6a321dSAndrew Duggan ret = rmi_read_block(d, addr, buf, size_presence_reg);
5942b6a321dSAndrew Duggan if (ret)
5952b6a321dSAndrew Duggan return ret;
5962b6a321dSAndrew Duggan ++addr;
5972b6a321dSAndrew Duggan
5982b6a321dSAndrew Duggan if (buf[0] == 0) {
5992b6a321dSAndrew Duggan presense_offset = 3;
6002b6a321dSAndrew Duggan rdesc->struct_size = buf[1] | (buf[2] << 8);
6012b6a321dSAndrew Duggan } else {
6022b6a321dSAndrew Duggan rdesc->struct_size = buf[0];
6032b6a321dSAndrew Duggan }
6042b6a321dSAndrew Duggan
6052b6a321dSAndrew Duggan for (i = presense_offset; i < size_presence_reg; i++) {
6062b6a321dSAndrew Duggan for (b = 0; b < 8; b++) {
6072b6a321dSAndrew Duggan if (buf[i] & (0x1 << b))
6082b6a321dSAndrew Duggan bitmap_set(rdesc->presense_map, map_offset, 1);
6092b6a321dSAndrew Duggan ++map_offset;
6102b6a321dSAndrew Duggan }
6112b6a321dSAndrew Duggan }
6122b6a321dSAndrew Duggan
6132b6a321dSAndrew Duggan rdesc->num_registers = bitmap_weight(rdesc->presense_map,
6142b6a321dSAndrew Duggan RMI_REG_DESC_PRESENSE_BITS);
6152b6a321dSAndrew Duggan
616a86854d0SKees Cook rdesc->registers = devm_kcalloc(&d->dev,
617a86854d0SKees Cook rdesc->num_registers,
6182b6a321dSAndrew Duggan sizeof(struct rmi_register_desc_item),
6192b6a321dSAndrew Duggan GFP_KERNEL);
6202b6a321dSAndrew Duggan if (!rdesc->registers)
6212b6a321dSAndrew Duggan return -ENOMEM;
6222b6a321dSAndrew Duggan
6232b6a321dSAndrew Duggan /*
6242b6a321dSAndrew Duggan * Allocate a temporary buffer to hold the register structure.
6252b6a321dSAndrew Duggan * I'm not using devm_kzalloc here since it will not be retained
6262b6a321dSAndrew Duggan * after exiting this function
6272b6a321dSAndrew Duggan */
6282b6a321dSAndrew Duggan struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
6292b6a321dSAndrew Duggan if (!struct_buf)
6302b6a321dSAndrew Duggan return -ENOMEM;
6312b6a321dSAndrew Duggan
6322b6a321dSAndrew Duggan /*
6332b6a321dSAndrew Duggan * The register structure contains information about every packet
6342b6a321dSAndrew Duggan * register of this type. This includes the size of the packet
6352b6a321dSAndrew Duggan * register and a bitmap of all subpackets contained in the packet
6362b6a321dSAndrew Duggan * register.
6372b6a321dSAndrew Duggan */
6382b6a321dSAndrew Duggan ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
6392b6a321dSAndrew Duggan if (ret)
6402b6a321dSAndrew Duggan goto free_struct_buff;
6412b6a321dSAndrew Duggan
6422b6a321dSAndrew Duggan reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
6432b6a321dSAndrew Duggan for (i = 0; i < rdesc->num_registers; i++) {
6442b6a321dSAndrew Duggan struct rmi_register_desc_item *item = &rdesc->registers[i];
6452b6a321dSAndrew Duggan int reg_size = struct_buf[offset];
6462b6a321dSAndrew Duggan
6472b6a321dSAndrew Duggan ++offset;
6482b6a321dSAndrew Duggan if (reg_size == 0) {
6492b6a321dSAndrew Duggan reg_size = struct_buf[offset] |
6502b6a321dSAndrew Duggan (struct_buf[offset + 1] << 8);
6512b6a321dSAndrew Duggan offset += 2;
6522b6a321dSAndrew Duggan }
6532b6a321dSAndrew Duggan
6542b6a321dSAndrew Duggan if (reg_size == 0) {
6552b6a321dSAndrew Duggan reg_size = struct_buf[offset] |
6562b6a321dSAndrew Duggan (struct_buf[offset + 1] << 8) |
6572b6a321dSAndrew Duggan (struct_buf[offset + 2] << 16) |
6582b6a321dSAndrew Duggan (struct_buf[offset + 3] << 24);
6592b6a321dSAndrew Duggan offset += 4;
6602b6a321dSAndrew Duggan }
6612b6a321dSAndrew Duggan
6622b6a321dSAndrew Duggan item->reg = reg;
6632b6a321dSAndrew Duggan item->reg_size = reg_size;
6642b6a321dSAndrew Duggan
6653e29d6bbSAndrew Duggan map_offset = 0;
6663e29d6bbSAndrew Duggan
6672b6a321dSAndrew Duggan do {
6682b6a321dSAndrew Duggan for (b = 0; b < 7; b++) {
6692b6a321dSAndrew Duggan if (struct_buf[offset] & (0x1 << b))
6702b6a321dSAndrew Duggan bitmap_set(item->subpacket_map,
6712b6a321dSAndrew Duggan map_offset, 1);
6722b6a321dSAndrew Duggan ++map_offset;
6732b6a321dSAndrew Duggan }
6742b6a321dSAndrew Duggan } while (struct_buf[offset++] & 0x80);
6752b6a321dSAndrew Duggan
6762b6a321dSAndrew Duggan item->num_subpackets = bitmap_weight(item->subpacket_map,
6772b6a321dSAndrew Duggan RMI_REG_DESC_SUBPACKET_BITS);
6782b6a321dSAndrew Duggan
6792b6a321dSAndrew Duggan rmi_dbg(RMI_DEBUG_CORE, &d->dev,
6802b6a321dSAndrew Duggan "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
6812b6a321dSAndrew Duggan item->reg, item->reg_size, item->num_subpackets);
6822b6a321dSAndrew Duggan
6832b6a321dSAndrew Duggan reg = find_next_bit(rdesc->presense_map,
6842b6a321dSAndrew Duggan RMI_REG_DESC_PRESENSE_BITS, reg + 1);
6852b6a321dSAndrew Duggan }
6862b6a321dSAndrew Duggan
6872b6a321dSAndrew Duggan free_struct_buff:
6882b6a321dSAndrew Duggan kfree(struct_buf);
6892b6a321dSAndrew Duggan return ret;
6902b6a321dSAndrew Duggan }
6912b6a321dSAndrew Duggan
rmi_get_register_desc_item(struct rmi_register_descriptor * rdesc,u16 reg)6922b6a321dSAndrew Duggan const struct rmi_register_desc_item *rmi_get_register_desc_item(
6932b6a321dSAndrew Duggan struct rmi_register_descriptor *rdesc, u16 reg)
6942b6a321dSAndrew Duggan {
6952b6a321dSAndrew Duggan const struct rmi_register_desc_item *item;
6962b6a321dSAndrew Duggan int i;
6972b6a321dSAndrew Duggan
6982b6a321dSAndrew Duggan for (i = 0; i < rdesc->num_registers; i++) {
6992b6a321dSAndrew Duggan item = &rdesc->registers[i];
7002b6a321dSAndrew Duggan if (item->reg == reg)
7012b6a321dSAndrew Duggan return item;
7022b6a321dSAndrew Duggan }
7032b6a321dSAndrew Duggan
7042b6a321dSAndrew Duggan return NULL;
7052b6a321dSAndrew Duggan }
7062b6a321dSAndrew Duggan
rmi_register_desc_calc_size(struct rmi_register_descriptor * rdesc)7072b6a321dSAndrew Duggan size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
7082b6a321dSAndrew Duggan {
7092b6a321dSAndrew Duggan const struct rmi_register_desc_item *item;
7102b6a321dSAndrew Duggan int i;
7112b6a321dSAndrew Duggan size_t size = 0;
7122b6a321dSAndrew Duggan
7132b6a321dSAndrew Duggan for (i = 0; i < rdesc->num_registers; i++) {
7142b6a321dSAndrew Duggan item = &rdesc->registers[i];
7152b6a321dSAndrew Duggan size += item->reg_size;
7162b6a321dSAndrew Duggan }
7172b6a321dSAndrew Duggan return size;
7182b6a321dSAndrew Duggan }
7192b6a321dSAndrew Duggan
7202b6a321dSAndrew Duggan /* Compute the register offset relative to the base address */
rmi_register_desc_calc_reg_offset(struct rmi_register_descriptor * rdesc,u16 reg)7212b6a321dSAndrew Duggan int rmi_register_desc_calc_reg_offset(
7222b6a321dSAndrew Duggan struct rmi_register_descriptor *rdesc, u16 reg)
7232b6a321dSAndrew Duggan {
7242b6a321dSAndrew Duggan const struct rmi_register_desc_item *item;
7252b6a321dSAndrew Duggan int offset = 0;
7262b6a321dSAndrew Duggan int i;
7272b6a321dSAndrew Duggan
7282b6a321dSAndrew Duggan for (i = 0; i < rdesc->num_registers; i++) {
7292b6a321dSAndrew Duggan item = &rdesc->registers[i];
7302b6a321dSAndrew Duggan if (item->reg == reg)
7312b6a321dSAndrew Duggan return offset;
7322b6a321dSAndrew Duggan ++offset;
7332b6a321dSAndrew Duggan }
7342b6a321dSAndrew Duggan return -1;
7352b6a321dSAndrew Duggan }
7362b6a321dSAndrew Duggan
rmi_register_desc_has_subpacket(const struct rmi_register_desc_item * item,u8 subpacket)7372b6a321dSAndrew Duggan bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
7382b6a321dSAndrew Duggan u8 subpacket)
7392b6a321dSAndrew Duggan {
7402b6a321dSAndrew Duggan return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
7412b6a321dSAndrew Duggan subpacket) == subpacket;
7422b6a321dSAndrew Duggan }
7432b6a321dSAndrew Duggan
rmi_check_bootloader_mode(struct rmi_device * rmi_dev,const struct pdt_entry * pdt)7442b6a321dSAndrew Duggan static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
7452b6a321dSAndrew Duggan const struct pdt_entry *pdt)
7462b6a321dSAndrew Duggan {
7475191d88aSNick Dyer struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
7485191d88aSNick Dyer int ret;
7495191d88aSNick Dyer u8 status;
7502b6a321dSAndrew Duggan
7515191d88aSNick Dyer if (pdt->function_number == 0x34 && pdt->function_version > 1) {
7525191d88aSNick Dyer ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
7535191d88aSNick Dyer if (ret) {
7542b6a321dSAndrew Duggan dev_err(&rmi_dev->dev,
7555191d88aSNick Dyer "Failed to read F34 status: %d.\n", ret);
7565191d88aSNick Dyer return ret;
7572b6a321dSAndrew Duggan }
7582b6a321dSAndrew Duggan
7595191d88aSNick Dyer if (status & BIT(7))
7605191d88aSNick Dyer data->bootloader_mode = true;
7615191d88aSNick Dyer } else if (pdt->function_number == 0x01) {
7625191d88aSNick Dyer ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
7635191d88aSNick Dyer if (ret) {
7645191d88aSNick Dyer dev_err(&rmi_dev->dev,
7655191d88aSNick Dyer "Failed to read F01 status: %d.\n", ret);
7665191d88aSNick Dyer return ret;
7675191d88aSNick Dyer }
7685191d88aSNick Dyer
7695191d88aSNick Dyer if (status & BIT(6))
7705191d88aSNick Dyer data->bootloader_mode = true;
7715191d88aSNick Dyer }
7725191d88aSNick Dyer
7735191d88aSNick Dyer return 0;
7742b6a321dSAndrew Duggan }
7752b6a321dSAndrew Duggan
rmi_count_irqs(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)7762b6a321dSAndrew Duggan static int rmi_count_irqs(struct rmi_device *rmi_dev,
7772b6a321dSAndrew Duggan void *ctx, const struct pdt_entry *pdt)
7782b6a321dSAndrew Duggan {
7792b6a321dSAndrew Duggan int *irq_count = ctx;
7805191d88aSNick Dyer int ret;
7812b6a321dSAndrew Duggan
7822b6a321dSAndrew Duggan *irq_count += pdt->interrupt_source_count;
7835191d88aSNick Dyer
7845191d88aSNick Dyer ret = rmi_check_bootloader_mode(rmi_dev, pdt);
7855191d88aSNick Dyer if (ret < 0)
7865191d88aSNick Dyer return ret;
7872b6a321dSAndrew Duggan
7882b6a321dSAndrew Duggan return RMI_SCAN_CONTINUE;
7892b6a321dSAndrew Duggan }
7902b6a321dSAndrew Duggan
rmi_initial_reset(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)79129fd0ec2SNick Dyer int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
79229fd0ec2SNick Dyer const struct pdt_entry *pdt)
7932b6a321dSAndrew Duggan {
7942b6a321dSAndrew Duggan int error;
7952b6a321dSAndrew Duggan
7962b6a321dSAndrew Duggan if (pdt->function_number == 0x01) {
7972b6a321dSAndrew Duggan u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
7982b6a321dSAndrew Duggan u8 cmd_buf = RMI_DEVICE_RESET_CMD;
7992b6a321dSAndrew Duggan const struct rmi_device_platform_data *pdata =
8002b6a321dSAndrew Duggan rmi_get_platform_data(rmi_dev);
8012b6a321dSAndrew Duggan
8022b6a321dSAndrew Duggan if (rmi_dev->xport->ops->reset) {
8032b6a321dSAndrew Duggan error = rmi_dev->xport->ops->reset(rmi_dev->xport,
8042b6a321dSAndrew Duggan cmd_addr);
8052b6a321dSAndrew Duggan if (error)
8062b6a321dSAndrew Duggan return error;
8072b6a321dSAndrew Duggan
8082b6a321dSAndrew Duggan return RMI_SCAN_DONE;
8092b6a321dSAndrew Duggan }
8102b6a321dSAndrew Duggan
8118029a283SNick Dyer rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
8122b6a321dSAndrew Duggan error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
8132b6a321dSAndrew Duggan if (error) {
8142b6a321dSAndrew Duggan dev_err(&rmi_dev->dev,
8152b6a321dSAndrew Duggan "Initial reset failed. Code = %d.\n", error);
8162b6a321dSAndrew Duggan return error;
8172b6a321dSAndrew Duggan }
8182b6a321dSAndrew Duggan
8192b6a321dSAndrew Duggan mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
8202b6a321dSAndrew Duggan
8212b6a321dSAndrew Duggan return RMI_SCAN_DONE;
8222b6a321dSAndrew Duggan }
8232b6a321dSAndrew Duggan
8242b6a321dSAndrew Duggan /* F01 should always be on page 0. If we don't find it there, fail. */
8252b6a321dSAndrew Duggan return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
8262b6a321dSAndrew Duggan }
8272b6a321dSAndrew Duggan
rmi_create_function(struct rmi_device * rmi_dev,void * ctx,const struct pdt_entry * pdt)8282b6a321dSAndrew Duggan static int rmi_create_function(struct rmi_device *rmi_dev,
8292b6a321dSAndrew Duggan void *ctx, const struct pdt_entry *pdt)
8302b6a321dSAndrew Duggan {
8312b6a321dSAndrew Duggan struct device *dev = &rmi_dev->dev;
832ed77bdf4SGuenter Roeck struct rmi_driver_data *data = dev_get_drvdata(dev);
8332b6a321dSAndrew Duggan int *current_irq_count = ctx;
8342b6a321dSAndrew Duggan struct rmi_function *fn;
8352b6a321dSAndrew Duggan int i;
8362b6a321dSAndrew Duggan int error;
8372b6a321dSAndrew Duggan
8382b6a321dSAndrew Duggan rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
8392b6a321dSAndrew Duggan pdt->function_number);
8402b6a321dSAndrew Duggan
8412b6a321dSAndrew Duggan fn = kzalloc(sizeof(struct rmi_function) +
8422b6a321dSAndrew Duggan BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
8432b6a321dSAndrew Duggan GFP_KERNEL);
8442b6a321dSAndrew Duggan if (!fn) {
8452b6a321dSAndrew Duggan dev_err(dev, "Failed to allocate memory for F%02X\n",
8462b6a321dSAndrew Duggan pdt->function_number);
8472b6a321dSAndrew Duggan return -ENOMEM;
8482b6a321dSAndrew Duggan }
8492b6a321dSAndrew Duggan
8502b6a321dSAndrew Duggan INIT_LIST_HEAD(&fn->node);
8512b6a321dSAndrew Duggan rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
8522b6a321dSAndrew Duggan
8532b6a321dSAndrew Duggan fn->rmi_dev = rmi_dev;
8542b6a321dSAndrew Duggan
8552b6a321dSAndrew Duggan fn->num_of_irqs = pdt->interrupt_source_count;
8562b6a321dSAndrew Duggan fn->irq_pos = *current_irq_count;
8572b6a321dSAndrew Duggan *current_irq_count += fn->num_of_irqs;
8582b6a321dSAndrew Duggan
8592b6a321dSAndrew Duggan for (i = 0; i < fn->num_of_irqs; i++)
8602b6a321dSAndrew Duggan set_bit(fn->irq_pos + i, fn->irq_mask);
8612b6a321dSAndrew Duggan
8622b6a321dSAndrew Duggan error = rmi_register_function(fn);
8632b6a321dSAndrew Duggan if (error)
864bce1a784SPan Bian return error;
8652b6a321dSAndrew Duggan
8662b6a321dSAndrew Duggan if (pdt->function_number == 0x01)
8672b6a321dSAndrew Duggan data->f01_container = fn;
86829fd0ec2SNick Dyer else if (pdt->function_number == 0x34)
86929fd0ec2SNick Dyer data->f34_container = fn;
8702b6a321dSAndrew Duggan
8712b6a321dSAndrew Duggan list_add_tail(&fn->node, &data->function_list);
8722b6a321dSAndrew Duggan
8732b6a321dSAndrew Duggan return RMI_SCAN_CONTINUE;
8742b6a321dSAndrew Duggan }
8752b6a321dSAndrew Duggan
rmi_enable_irq(struct rmi_device * rmi_dev,bool clear_wake)876a64ea311SBenjamin Tissoires void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
8772b6a321dSAndrew Duggan {
8783aeed5b5SBjorn Andersson struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
879a64ea311SBenjamin Tissoires struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
8803aeed5b5SBjorn Andersson int irq = pdata->irq;
881a64ea311SBenjamin Tissoires int irq_flags;
882a64ea311SBenjamin Tissoires int retval;
8832b6a321dSAndrew Duggan
884a64ea311SBenjamin Tissoires mutex_lock(&data->enabled_mutex);
885a64ea311SBenjamin Tissoires
886a64ea311SBenjamin Tissoires if (data->enabled)
887a64ea311SBenjamin Tissoires goto out;
888a64ea311SBenjamin Tissoires
889a64ea311SBenjamin Tissoires enable_irq(irq);
890a64ea311SBenjamin Tissoires data->enabled = true;
891a64ea311SBenjamin Tissoires if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
892a64ea311SBenjamin Tissoires retval = disable_irq_wake(irq);
89305e0be7cSChristophe JAILLET if (retval)
894a64ea311SBenjamin Tissoires dev_warn(&rmi_dev->dev,
895a64ea311SBenjamin Tissoires "Failed to disable irq for wake: %d\n",
8962b6a321dSAndrew Duggan retval);
897a64ea311SBenjamin Tissoires }
8982b6a321dSAndrew Duggan
899a64ea311SBenjamin Tissoires /*
900a64ea311SBenjamin Tissoires * Call rmi_process_interrupt_requests() after enabling irq,
901a64ea311SBenjamin Tissoires * otherwise we may lose interrupt on edge-triggered systems.
902a64ea311SBenjamin Tissoires */
903a64ea311SBenjamin Tissoires irq_flags = irq_get_trigger_type(pdata->irq);
904a64ea311SBenjamin Tissoires if (irq_flags & IRQ_TYPE_EDGE_BOTH)
905a64ea311SBenjamin Tissoires rmi_process_interrupt_requests(rmi_dev);
906a64ea311SBenjamin Tissoires
907a64ea311SBenjamin Tissoires out:
908a64ea311SBenjamin Tissoires mutex_unlock(&data->enabled_mutex);
909a64ea311SBenjamin Tissoires }
910a64ea311SBenjamin Tissoires
rmi_disable_irq(struct rmi_device * rmi_dev,bool enable_wake)911a64ea311SBenjamin Tissoires void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
912a64ea311SBenjamin Tissoires {
913a64ea311SBenjamin Tissoires struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
914a64ea311SBenjamin Tissoires struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
915b908d3cdSBenjamin Tissoires struct rmi4_attn_data attn_data = {0};
916a64ea311SBenjamin Tissoires int irq = pdata->irq;
917b908d3cdSBenjamin Tissoires int retval, count;
918a64ea311SBenjamin Tissoires
919a64ea311SBenjamin Tissoires mutex_lock(&data->enabled_mutex);
920a64ea311SBenjamin Tissoires
921a64ea311SBenjamin Tissoires if (!data->enabled)
922a64ea311SBenjamin Tissoires goto out;
923a64ea311SBenjamin Tissoires
924a64ea311SBenjamin Tissoires data->enabled = false;
9253aeed5b5SBjorn Andersson disable_irq(irq);
9263aeed5b5SBjorn Andersson if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
9273aeed5b5SBjorn Andersson retval = enable_irq_wake(irq);
92805e0be7cSChristophe JAILLET if (retval)
9293aeed5b5SBjorn Andersson dev_warn(&rmi_dev->dev,
9303aeed5b5SBjorn Andersson "Failed to enable irq for wake: %d\n",
9313aeed5b5SBjorn Andersson retval);
9323aeed5b5SBjorn Andersson }
933a64ea311SBenjamin Tissoires
934b908d3cdSBenjamin Tissoires /* make sure the fifo is clean */
935b908d3cdSBenjamin Tissoires while (!kfifo_is_empty(&data->attn_fifo)) {
936b908d3cdSBenjamin Tissoires count = kfifo_get(&data->attn_fifo, &attn_data);
937b908d3cdSBenjamin Tissoires if (count)
938b908d3cdSBenjamin Tissoires kfree(attn_data.data);
939b908d3cdSBenjamin Tissoires }
940b908d3cdSBenjamin Tissoires
941a64ea311SBenjamin Tissoires out:
942a64ea311SBenjamin Tissoires mutex_unlock(&data->enabled_mutex);
943a64ea311SBenjamin Tissoires }
944a64ea311SBenjamin Tissoires
rmi_driver_suspend(struct rmi_device * rmi_dev,bool enable_wake)945a64ea311SBenjamin Tissoires int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
946a64ea311SBenjamin Tissoires {
947a64ea311SBenjamin Tissoires int retval;
9482b6a321dSAndrew Duggan
9492b6a321dSAndrew Duggan retval = rmi_suspend_functions(rmi_dev);
9502b6a321dSAndrew Duggan if (retval)
9512b6a321dSAndrew Duggan dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
9522b6a321dSAndrew Duggan retval);
9532b6a321dSAndrew Duggan
954a64ea311SBenjamin Tissoires rmi_disable_irq(rmi_dev, enable_wake);
9552b6a321dSAndrew Duggan return retval;
9562b6a321dSAndrew Duggan }
9572b6a321dSAndrew Duggan EXPORT_SYMBOL_GPL(rmi_driver_suspend);
9582b6a321dSAndrew Duggan
rmi_driver_resume(struct rmi_device * rmi_dev,bool clear_wake)9593aeed5b5SBjorn Andersson int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
9602b6a321dSAndrew Duggan {
9612b6a321dSAndrew Duggan int retval;
9622b6a321dSAndrew Duggan
963a64ea311SBenjamin Tissoires rmi_enable_irq(rmi_dev, clear_wake);
9643aeed5b5SBjorn Andersson
9652b6a321dSAndrew Duggan retval = rmi_resume_functions(rmi_dev);
9662b6a321dSAndrew Duggan if (retval)
9672b6a321dSAndrew Duggan dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
9682b6a321dSAndrew Duggan retval);
9692b6a321dSAndrew Duggan
9702b6a321dSAndrew Duggan return retval;
9712b6a321dSAndrew Duggan }
9722b6a321dSAndrew Duggan EXPORT_SYMBOL_GPL(rmi_driver_resume);
9732b6a321dSAndrew Duggan
rmi_driver_remove(struct device * dev)9742b6a321dSAndrew Duggan static int rmi_driver_remove(struct device *dev)
9752b6a321dSAndrew Duggan {
9762b6a321dSAndrew Duggan struct rmi_device *rmi_dev = to_rmi_device(dev);
97724d28e4fSNick Dyer struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
9782b6a321dSAndrew Duggan
979a64ea311SBenjamin Tissoires rmi_disable_irq(rmi_dev, false);
9802b6a321dSAndrew Duggan
98129fd0ec2SNick Dyer rmi_f34_remove_sysfs(rmi_dev);
9822b6a321dSAndrew Duggan rmi_free_function_list(rmi_dev);
9832b6a321dSAndrew Duggan
984fbf8d717SMathias Krause irq_domain_remove(data->irqdomain);
985fbf8d717SMathias Krause data->irqdomain = NULL;
986fbf8d717SMathias Krause
9872b6a321dSAndrew Duggan return 0;
9882b6a321dSAndrew Duggan }
9892b6a321dSAndrew Duggan
990d8a8b3edSAndrew Duggan #ifdef CONFIG_OF
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)991d8a8b3edSAndrew Duggan static int rmi_driver_of_probe(struct device *dev,
992d8a8b3edSAndrew Duggan struct rmi_device_platform_data *pdata)
993d8a8b3edSAndrew Duggan {
994d8a8b3edSAndrew Duggan int retval;
995d8a8b3edSAndrew Duggan
996d8a8b3edSAndrew Duggan retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
997d8a8b3edSAndrew Duggan "syna,reset-delay-ms", 1);
998d8a8b3edSAndrew Duggan if (retval)
999d8a8b3edSAndrew Duggan return retval;
1000d8a8b3edSAndrew Duggan
1001d8a8b3edSAndrew Duggan return 0;
1002d8a8b3edSAndrew Duggan }
1003d8a8b3edSAndrew Duggan #else
rmi_driver_of_probe(struct device * dev,struct rmi_device_platform_data * pdata)1004d8a8b3edSAndrew Duggan static inline int rmi_driver_of_probe(struct device *dev,
1005d8a8b3edSAndrew Duggan struct rmi_device_platform_data *pdata)
1006d8a8b3edSAndrew Duggan {
1007d8a8b3edSAndrew Duggan return -ENODEV;
1008d8a8b3edSAndrew Duggan }
1009d8a8b3edSAndrew Duggan #endif
1010d8a8b3edSAndrew Duggan
rmi_probe_interrupts(struct rmi_driver_data * data)101129fd0ec2SNick Dyer int rmi_probe_interrupts(struct rmi_driver_data *data)
10126bd0dcfaSNick Dyer {
10136bd0dcfaSNick Dyer struct rmi_device *rmi_dev = data->rmi_dev;
10146bd0dcfaSNick Dyer struct device *dev = &rmi_dev->dev;
101524d28e4fSNick Dyer struct fwnode_handle *fwnode = rmi_dev->xport->dev->fwnode;
101624d28e4fSNick Dyer int irq_count = 0;
10176bd0dcfaSNick Dyer size_t size;
10186bd0dcfaSNick Dyer int retval;
10196bd0dcfaSNick Dyer
10206bd0dcfaSNick Dyer /*
10216bd0dcfaSNick Dyer * We need to count the IRQs and allocate their storage before scanning
10226bd0dcfaSNick Dyer * the PDT and creating the function entries, because adding a new
10236bd0dcfaSNick Dyer * function can trigger events that result in the IRQ related storage
10246bd0dcfaSNick Dyer * being accessed.
10256bd0dcfaSNick Dyer */
10266bd0dcfaSNick Dyer rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
10275191d88aSNick Dyer data->bootloader_mode = false;
10285191d88aSNick Dyer
10296bd0dcfaSNick Dyer retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
10306bd0dcfaSNick Dyer if (retval < 0) {
10316bd0dcfaSNick Dyer dev_err(dev, "IRQ counting failed with code %d.\n", retval);
10326bd0dcfaSNick Dyer return retval;
10336bd0dcfaSNick Dyer }
103429fd0ec2SNick Dyer
10355191d88aSNick Dyer if (data->bootloader_mode)
1036ed77bdf4SGuenter Roeck dev_warn(dev, "Device in bootloader mode.\n");
103729fd0ec2SNick Dyer
103824d28e4fSNick Dyer /* Allocate and register a linear revmap irq_domain */
103924d28e4fSNick Dyer data->irqdomain = irq_domain_create_linear(fwnode, irq_count,
104024d28e4fSNick Dyer &irq_domain_simple_ops,
104124d28e4fSNick Dyer data);
104224d28e4fSNick Dyer if (!data->irqdomain) {
104324d28e4fSNick Dyer dev_err(&rmi_dev->dev, "Failed to create IRQ domain\n");
1044fd629294SWei Yongjun return -ENOMEM;
104524d28e4fSNick Dyer }
104624d28e4fSNick Dyer
10476bd0dcfaSNick Dyer data->irq_count = irq_count;
10486bd0dcfaSNick Dyer data->num_of_irq_regs = (data->irq_count + 7) / 8;
10496bd0dcfaSNick Dyer
10506bd0dcfaSNick Dyer size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
1051a86854d0SKees Cook data->irq_memory = devm_kcalloc(dev, size, 4, GFP_KERNEL);
105229fd0ec2SNick Dyer if (!data->irq_memory) {
10536bd0dcfaSNick Dyer dev_err(dev, "Failed to allocate memory for irq masks.\n");
1054e7839533SWei Yongjun return -ENOMEM;
10556bd0dcfaSNick Dyer }
10566bd0dcfaSNick Dyer
105729fd0ec2SNick Dyer data->irq_status = data->irq_memory + size * 0;
105829fd0ec2SNick Dyer data->fn_irq_bits = data->irq_memory + size * 1;
105929fd0ec2SNick Dyer data->current_irq_mask = data->irq_memory + size * 2;
106029fd0ec2SNick Dyer data->new_irq_mask = data->irq_memory + size * 3;
10616bd0dcfaSNick Dyer
10626bd0dcfaSNick Dyer return retval;
10636bd0dcfaSNick Dyer }
10646bd0dcfaSNick Dyer
rmi_init_functions(struct rmi_driver_data * data)106529fd0ec2SNick Dyer int rmi_init_functions(struct rmi_driver_data *data)
10666bd0dcfaSNick Dyer {
10676bd0dcfaSNick Dyer struct rmi_device *rmi_dev = data->rmi_dev;
10686bd0dcfaSNick Dyer struct device *dev = &rmi_dev->dev;
106924d28e4fSNick Dyer int irq_count = 0;
10706bd0dcfaSNick Dyer int retval;
10716bd0dcfaSNick Dyer
10726bd0dcfaSNick Dyer rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
10736bd0dcfaSNick Dyer retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
10746bd0dcfaSNick Dyer if (retval < 0) {
10756bd0dcfaSNick Dyer dev_err(dev, "Function creation failed with code %d.\n",
10766bd0dcfaSNick Dyer retval);
10776bd0dcfaSNick Dyer goto err_destroy_functions;
10786bd0dcfaSNick Dyer }
10796bd0dcfaSNick Dyer
10806bd0dcfaSNick Dyer if (!data->f01_container) {
10816bd0dcfaSNick Dyer dev_err(dev, "Missing F01 container!\n");
10826bd0dcfaSNick Dyer retval = -EINVAL;
10836bd0dcfaSNick Dyer goto err_destroy_functions;
10846bd0dcfaSNick Dyer }
10856bd0dcfaSNick Dyer
10866bd0dcfaSNick Dyer retval = rmi_read_block(rmi_dev,
10876bd0dcfaSNick Dyer data->f01_container->fd.control_base_addr + 1,
10886bd0dcfaSNick Dyer data->current_irq_mask, data->num_of_irq_regs);
10896bd0dcfaSNick Dyer if (retval < 0) {
10906bd0dcfaSNick Dyer dev_err(dev, "%s: Failed to read current IRQ mask.\n",
10916bd0dcfaSNick Dyer __func__);
10926bd0dcfaSNick Dyer goto err_destroy_functions;
10936bd0dcfaSNick Dyer }
10946bd0dcfaSNick Dyer
10956bd0dcfaSNick Dyer return 0;
10966bd0dcfaSNick Dyer
10976bd0dcfaSNick Dyer err_destroy_functions:
10986bd0dcfaSNick Dyer rmi_free_function_list(rmi_dev);
10996bd0dcfaSNick Dyer return retval;
11006bd0dcfaSNick Dyer }
11016bd0dcfaSNick Dyer
rmi_driver_probe(struct device * dev)11022b6a321dSAndrew Duggan static int rmi_driver_probe(struct device *dev)
11032b6a321dSAndrew Duggan {
11042b6a321dSAndrew Duggan struct rmi_driver *rmi_driver;
11052b6a321dSAndrew Duggan struct rmi_driver_data *data;
11062b6a321dSAndrew Duggan struct rmi_device_platform_data *pdata;
11072b6a321dSAndrew Duggan struct rmi_device *rmi_dev;
11082b6a321dSAndrew Duggan int retval;
11092b6a321dSAndrew Duggan
11102b6a321dSAndrew Duggan rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
11112b6a321dSAndrew Duggan __func__);
11122b6a321dSAndrew Duggan
11132b6a321dSAndrew Duggan if (!rmi_is_physical_device(dev)) {
11142b6a321dSAndrew Duggan rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
11152b6a321dSAndrew Duggan return -ENODEV;
11162b6a321dSAndrew Duggan }
11172b6a321dSAndrew Duggan
11182b6a321dSAndrew Duggan rmi_dev = to_rmi_device(dev);
11192b6a321dSAndrew Duggan rmi_driver = to_rmi_driver(dev->driver);
11202b6a321dSAndrew Duggan rmi_dev->driver = rmi_driver;
11212b6a321dSAndrew Duggan
11222b6a321dSAndrew Duggan pdata = rmi_get_platform_data(rmi_dev);
11232b6a321dSAndrew Duggan
1124d8a8b3edSAndrew Duggan if (rmi_dev->xport->dev->of_node) {
1125d8a8b3edSAndrew Duggan retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
1126d8a8b3edSAndrew Duggan if (retval)
1127d8a8b3edSAndrew Duggan return retval;
1128d8a8b3edSAndrew Duggan }
1129d8a8b3edSAndrew Duggan
11302b6a321dSAndrew Duggan data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
11312b6a321dSAndrew Duggan if (!data)
11322b6a321dSAndrew Duggan return -ENOMEM;
11332b6a321dSAndrew Duggan
11342b6a321dSAndrew Duggan INIT_LIST_HEAD(&data->function_list);
11352b6a321dSAndrew Duggan data->rmi_dev = rmi_dev;
11362b6a321dSAndrew Duggan dev_set_drvdata(&rmi_dev->dev, data);
11372b6a321dSAndrew Duggan
11382b6a321dSAndrew Duggan /*
11392b6a321dSAndrew Duggan * Right before a warm boot, the sensor might be in some unusual state,
11402b6a321dSAndrew Duggan * such as F54 diagnostics, or F34 bootloader mode after a firmware
11412b6a321dSAndrew Duggan * or configuration update. In order to clear the sensor to a known
11422b6a321dSAndrew Duggan * state and/or apply any updates, we issue a initial reset to clear any
11432b6a321dSAndrew Duggan * previous settings and force it into normal operation.
11442b6a321dSAndrew Duggan *
11452b6a321dSAndrew Duggan * We have to do this before actually building the PDT because
11462b6a321dSAndrew Duggan * the reflash updates (if any) might cause various registers to move
11472b6a321dSAndrew Duggan * around.
11482b6a321dSAndrew Duggan *
11492b6a321dSAndrew Duggan * For a number of reasons, this initial reset may fail to return
11502b6a321dSAndrew Duggan * within the specified time, but we'll still be able to bring up the
11512b6a321dSAndrew Duggan * driver normally after that failure. This occurs most commonly in
11522b6a321dSAndrew Duggan * a cold boot situation (where then firmware takes longer to come up
11532b6a321dSAndrew Duggan * than from a warm boot) and the reset_delay_ms in the platform data
11542b6a321dSAndrew Duggan * has been set too short to accommodate that. Since the sensor will
11552b6a321dSAndrew Duggan * eventually come up and be usable, we don't want to just fail here
11562b6a321dSAndrew Duggan * and leave the customer's device unusable. So we warn them, and
11572b6a321dSAndrew Duggan * continue processing.
11582b6a321dSAndrew Duggan */
11592b6a321dSAndrew Duggan retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
11602b6a321dSAndrew Duggan if (retval < 0)
11612b6a321dSAndrew Duggan dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
11622b6a321dSAndrew Duggan
11632b6a321dSAndrew Duggan retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
11642b6a321dSAndrew Duggan if (retval < 0) {
11652b6a321dSAndrew Duggan /*
11662b6a321dSAndrew Duggan * we'll print out a warning and continue since
11672b6a321dSAndrew Duggan * failure to get the PDT properties is not a cause to fail
11682b6a321dSAndrew Duggan */
11692b6a321dSAndrew Duggan dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
11702b6a321dSAndrew Duggan PDT_PROPERTIES_LOCATION, retval);
11712b6a321dSAndrew Duggan }
11722b6a321dSAndrew Duggan
11732b6a321dSAndrew Duggan mutex_init(&data->irq_mutex);
1174a64ea311SBenjamin Tissoires mutex_init(&data->enabled_mutex);
11752b6a321dSAndrew Duggan
11766bd0dcfaSNick Dyer retval = rmi_probe_interrupts(data);
11776bd0dcfaSNick Dyer if (retval)
11782b6a321dSAndrew Duggan goto err;
11792b6a321dSAndrew Duggan
11802b6a321dSAndrew Duggan if (rmi_dev->xport->input) {
11812b6a321dSAndrew Duggan /*
11822b6a321dSAndrew Duggan * The transport driver already has an input device.
11832b6a321dSAndrew Duggan * In some cases it is preferable to reuse the transport
11842b6a321dSAndrew Duggan * devices input device instead of creating a new one here.
11852b6a321dSAndrew Duggan * One example is some HID touchpads report "pass-through"
11862b6a321dSAndrew Duggan * button events are not reported by rmi registers.
11872b6a321dSAndrew Duggan */
11882b6a321dSAndrew Duggan data->input = rmi_dev->xport->input;
11892b6a321dSAndrew Duggan } else {
11902b6a321dSAndrew Duggan data->input = devm_input_allocate_device(dev);
11912b6a321dSAndrew Duggan if (!data->input) {
11922b6a321dSAndrew Duggan dev_err(dev, "%s: Failed to allocate input device.\n",
11932b6a321dSAndrew Duggan __func__);
11942b6a321dSAndrew Duggan retval = -ENOMEM;
11956bd0dcfaSNick Dyer goto err;
11962b6a321dSAndrew Duggan }
11972b6a321dSAndrew Duggan rmi_driver_set_input_params(rmi_dev, data->input);
11982b6a321dSAndrew Duggan data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
11992b6a321dSAndrew Duggan "%s/input0", dev_name(dev));
1200bc499618SKunwu Chan if (!data->input->phys) {
1201bc499618SKunwu Chan retval = -ENOMEM;
1202bc499618SKunwu Chan goto err;
1203bc499618SKunwu Chan }
12042b6a321dSAndrew Duggan }
12052b6a321dSAndrew Duggan
12066bd0dcfaSNick Dyer retval = rmi_init_functions(data);
12076bd0dcfaSNick Dyer if (retval)
12086bd0dcfaSNick Dyer goto err;
12092b6a321dSAndrew Duggan
121029fd0ec2SNick Dyer retval = rmi_f34_create_sysfs(rmi_dev);
121129fd0ec2SNick Dyer if (retval)
121229fd0ec2SNick Dyer goto err;
12132b6a321dSAndrew Duggan
12142b6a321dSAndrew Duggan if (data->input) {
12152b6a321dSAndrew Duggan rmi_driver_set_input_name(rmi_dev, data->input);
12162b6a321dSAndrew Duggan if (!rmi_dev->xport->input) {
12175caab2daSWei Yongjun retval = input_register_device(data->input);
12185caab2daSWei Yongjun if (retval) {
12192b6a321dSAndrew Duggan dev_err(dev, "%s: Failed to register input device.\n",
12202b6a321dSAndrew Duggan __func__);
12212b6a321dSAndrew Duggan goto err_destroy_functions;
12222b6a321dSAndrew Duggan }
12232b6a321dSAndrew Duggan }
12242b6a321dSAndrew Duggan }
12252b6a321dSAndrew Duggan
12263aeed5b5SBjorn Andersson retval = rmi_irq_init(rmi_dev);
12273aeed5b5SBjorn Andersson if (retval < 0)
12283aeed5b5SBjorn Andersson goto err_destroy_functions;
12293aeed5b5SBjorn Andersson
12308cf0adf2SDmitry Torokhov if (data->f01_container->dev.driver) {
12312b6a321dSAndrew Duggan /* Driver already bound, so enable ATTN now. */
12328cf0adf2SDmitry Torokhov retval = rmi_enable_sensor(rmi_dev);
12338cf0adf2SDmitry Torokhov if (retval)
12348cf0adf2SDmitry Torokhov goto err_disable_irq;
12358cf0adf2SDmitry Torokhov }
12362b6a321dSAndrew Duggan
12372b6a321dSAndrew Duggan return 0;
12382b6a321dSAndrew Duggan
12398cf0adf2SDmitry Torokhov err_disable_irq:
12408cf0adf2SDmitry Torokhov rmi_disable_irq(rmi_dev, false);
12412b6a321dSAndrew Duggan err_destroy_functions:
12422b6a321dSAndrew Duggan rmi_free_function_list(rmi_dev);
12432b6a321dSAndrew Duggan err:
12448cf0adf2SDmitry Torokhov return retval;
12452b6a321dSAndrew Duggan }
12462b6a321dSAndrew Duggan
12472b6a321dSAndrew Duggan static struct rmi_driver rmi_physical_driver = {
12482b6a321dSAndrew Duggan .driver = {
12492b6a321dSAndrew Duggan .owner = THIS_MODULE,
12502b6a321dSAndrew Duggan .name = "rmi4_physical",
12512b6a321dSAndrew Duggan .bus = &rmi_bus_type,
12522b6a321dSAndrew Duggan .probe = rmi_driver_probe,
12532b6a321dSAndrew Duggan .remove = rmi_driver_remove,
12542b6a321dSAndrew Duggan },
12552b6a321dSAndrew Duggan .reset_handler = rmi_driver_reset_handler,
12562b6a321dSAndrew Duggan .clear_irq_bits = rmi_driver_clear_irq_bits,
12572b6a321dSAndrew Duggan .set_irq_bits = rmi_driver_set_irq_bits,
12582b6a321dSAndrew Duggan .set_input_params = rmi_driver_set_input_params,
12592b6a321dSAndrew Duggan };
12602b6a321dSAndrew Duggan
rmi_is_physical_driver(const struct device_driver * drv)1261*d69d8048SGreg Kroah-Hartman bool rmi_is_physical_driver(const struct device_driver *drv)
12622b6a321dSAndrew Duggan {
12632b6a321dSAndrew Duggan return drv == &rmi_physical_driver.driver;
12642b6a321dSAndrew Duggan }
12652b6a321dSAndrew Duggan
rmi_register_physical_driver(void)12662b6a321dSAndrew Duggan int __init rmi_register_physical_driver(void)
12672b6a321dSAndrew Duggan {
12682b6a321dSAndrew Duggan int error;
12692b6a321dSAndrew Duggan
12702b6a321dSAndrew Duggan error = driver_register(&rmi_physical_driver.driver);
12712b6a321dSAndrew Duggan if (error) {
12722b6a321dSAndrew Duggan pr_err("%s: driver register failed, code=%d.\n", __func__,
12732b6a321dSAndrew Duggan error);
12742b6a321dSAndrew Duggan return error;
12752b6a321dSAndrew Duggan }
12762b6a321dSAndrew Duggan
12772b6a321dSAndrew Duggan return 0;
12782b6a321dSAndrew Duggan }
12792b6a321dSAndrew Duggan
rmi_unregister_physical_driver(void)12802b6a321dSAndrew Duggan void __exit rmi_unregister_physical_driver(void)
12812b6a321dSAndrew Duggan {
12822b6a321dSAndrew Duggan driver_unregister(&rmi_physical_driver.driver);
12832b6a321dSAndrew Duggan }
1284