Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2011-2016 Synaptics Incorporated
   4 * Copyright (c) 2011 Unixphere
   5 *
   6 * This driver provides the core support for a single RMI4-based device.
   7 *
   8 * The RMI4 specification can be found here (URL split for line length):
   9 *
  10 * http://www.synaptics.com/sites/default/files/
  11 *      511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
 
 
 
 
  12 */
  13
  14#include <linux/bitmap.h>
  15#include <linux/delay.h>
  16#include <linux/fs.h>
  17#include <linux/irq.h>
  18#include <linux/pm.h>
  19#include <linux/slab.h>
  20#include <linux/of.h>
  21#include <linux/irqdomain.h>
  22#include <uapi/linux/input.h>
  23#include <linux/rmi.h>
  24#include "rmi_bus.h"
  25#include "rmi_driver.h"
  26
  27#define HAS_NONSTANDARD_PDT_MASK 0x40
  28#define RMI4_MAX_PAGE 0xff
  29#define RMI4_PAGE_SIZE 0x100
  30#define RMI4_PAGE_MASK 0xFF00
  31
  32#define RMI_DEVICE_RESET_CMD	0x01
  33#define DEFAULT_RESET_DELAY_MS	100
  34
  35void rmi_free_function_list(struct rmi_device *rmi_dev)
  36{
  37	struct rmi_function *fn, *tmp;
  38	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  39
  40	rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
  41
  42	/* Doing it in the reverse order so F01 will be removed last */
  43	list_for_each_entry_safe_reverse(fn, tmp,
  44					 &data->function_list, node) {
  45		list_del(&fn->node);
  46		rmi_unregister_function(fn);
  47	}
  48
  49	devm_kfree(&rmi_dev->dev, data->irq_memory);
  50	data->irq_memory = NULL;
  51	data->irq_status = NULL;
  52	data->fn_irq_bits = NULL;
  53	data->current_irq_mask = NULL;
  54	data->new_irq_mask = NULL;
  55
  56	data->f01_container = NULL;
  57	data->f34_container = NULL;
  58}
  59
  60static int reset_one_function(struct rmi_function *fn)
  61{
  62	struct rmi_function_handler *fh;
  63	int retval = 0;
  64
  65	if (!fn || !fn->dev.driver)
  66		return 0;
  67
  68	fh = to_rmi_function_handler(fn->dev.driver);
  69	if (fh->reset) {
  70		retval = fh->reset(fn);
  71		if (retval < 0)
  72			dev_err(&fn->dev, "Reset failed with code %d.\n",
  73				retval);
  74	}
  75
  76	return retval;
  77}
  78
  79static int configure_one_function(struct rmi_function *fn)
  80{
  81	struct rmi_function_handler *fh;
  82	int retval = 0;
  83
  84	if (!fn || !fn->dev.driver)
  85		return 0;
  86
  87	fh = to_rmi_function_handler(fn->dev.driver);
  88	if (fh->config) {
  89		retval = fh->config(fn);
  90		if (retval < 0)
  91			dev_err(&fn->dev, "Config failed with code %d.\n",
  92				retval);
  93	}
  94
  95	return retval;
  96}
  97
  98static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
  99{
 100	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 101	struct rmi_function *entry;
 102	int retval;
 103
 104	list_for_each_entry(entry, &data->function_list, node) {
 105		retval = reset_one_function(entry);
 106		if (retval < 0)
 107			return retval;
 108	}
 109
 110	return 0;
 111}
 112
 113static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
 114{
 115	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 116	struct rmi_function *entry;
 117	int retval;
 118
 119	list_for_each_entry(entry, &data->function_list, node) {
 120		retval = configure_one_function(entry);
 121		if (retval < 0)
 122			return retval;
 123	}
 124
 125	return 0;
 126}
 127
 128static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 129{
 130	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 131	struct device *dev = &rmi_dev->dev;
 132	int i;
 133	int error;
 134
 135	if (!data)
 136		return 0;
 137
 138	if (!data->attn_data.data) {
 139		error = rmi_read_block(rmi_dev,
 140				data->f01_container->fd.data_base_addr + 1,
 141				data->irq_status, data->num_of_irq_regs);
 142		if (error < 0) {
 143			dev_err(dev, "Failed to read irqs, code=%d\n", error);
 144			return error;
 145		}
 146	}
 147
 148	mutex_lock(&data->irq_mutex);
 149	bitmap_and(data->irq_status, data->irq_status, data->fn_irq_bits,
 150	       data->irq_count);
 151	/*
 152	 * At this point, irq_status has all bits that are set in the
 153	 * interrupt status register and are enabled.
 154	 */
 155	mutex_unlock(&data->irq_mutex);
 156
 157	for_each_set_bit(i, data->irq_status, data->irq_count)
 158		handle_nested_irq(irq_find_mapping(data->irqdomain, i));
 
 
 
 
 
 
 
 
 159
 160	if (data->input)
 161		input_sync(data->input);
 162
 163	return 0;
 164}
 165
 166void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
 167		       void *data, size_t size)
 168{
 169	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
 170	struct rmi4_attn_data attn_data;
 171	void *fifo_data;
 172
 173	if (!drvdata->enabled)
 174		return;
 175
 176	fifo_data = kmemdup(data, size, GFP_ATOMIC);
 177	if (!fifo_data)
 178		return;
 179
 180	attn_data.irq_status = irq_status;
 181	attn_data.size = size;
 182	attn_data.data = fifo_data;
 183
 184	kfifo_put(&drvdata->attn_fifo, attn_data);
 185}
 186EXPORT_SYMBOL_GPL(rmi_set_attn_data);
 187
 188static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
 189{
 190	struct rmi_device *rmi_dev = dev_id;
 191	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
 192	struct rmi4_attn_data attn_data = {0};
 193	int ret, count;
 194
 195	count = kfifo_get(&drvdata->attn_fifo, &attn_data);
 196	if (count) {
 197		*(drvdata->irq_status) = attn_data.irq_status;
 198		drvdata->attn_data = attn_data;
 199	}
 200
 201	ret = rmi_process_interrupt_requests(rmi_dev);
 202	if (ret)
 203		rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
 204			"Failed to process interrupt request: %d\n", ret);
 205
 206	if (count) {
 207		kfree(attn_data.data);
 208		drvdata->attn_data.data = NULL;
 209	}
 210
 211	if (!kfifo_is_empty(&drvdata->attn_fifo))
 212		return rmi_irq_fn(irq, dev_id);
 213
 214	return IRQ_HANDLED;
 215}
 216
 217static int rmi_irq_init(struct rmi_device *rmi_dev)
 218{
 219	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
 220	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 221	int irq_flags = irq_get_trigger_type(pdata->irq);
 222	int ret;
 223
 224	if (!irq_flags)
 225		irq_flags = IRQF_TRIGGER_LOW;
 226
 227	ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
 228					rmi_irq_fn, irq_flags | IRQF_ONESHOT,
 229					dev_driver_string(rmi_dev->xport->dev),
 230					rmi_dev);
 231	if (ret < 0) {
 232		dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
 233			pdata->irq);
 234
 235		return ret;
 236	}
 237
 238	data->enabled = true;
 239
 240	return 0;
 241}
 242
 243struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
 244{
 245	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 246	struct rmi_function *entry;
 247
 248	list_for_each_entry(entry, &data->function_list, node) {
 249		if (entry->fd.function_number == number)
 250			return entry;
 251	}
 252
 253	return NULL;
 254}
 255
 256static int suspend_one_function(struct rmi_function *fn)
 257{
 258	struct rmi_function_handler *fh;
 259	int retval = 0;
 260
 261	if (!fn || !fn->dev.driver)
 262		return 0;
 263
 264	fh = to_rmi_function_handler(fn->dev.driver);
 265	if (fh->suspend) {
 266		retval = fh->suspend(fn);
 267		if (retval < 0)
 268			dev_err(&fn->dev, "Suspend failed with code %d.\n",
 269				retval);
 270	}
 271
 272	return retval;
 273}
 274
 275static int rmi_suspend_functions(struct rmi_device *rmi_dev)
 276{
 277	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 278	struct rmi_function *entry;
 279	int retval;
 280
 281	list_for_each_entry(entry, &data->function_list, node) {
 282		retval = suspend_one_function(entry);
 283		if (retval < 0)
 284			return retval;
 285	}
 286
 287	return 0;
 288}
 289
 290static int resume_one_function(struct rmi_function *fn)
 291{
 292	struct rmi_function_handler *fh;
 293	int retval = 0;
 294
 295	if (!fn || !fn->dev.driver)
 296		return 0;
 297
 298	fh = to_rmi_function_handler(fn->dev.driver);
 299	if (fh->resume) {
 300		retval = fh->resume(fn);
 301		if (retval < 0)
 302			dev_err(&fn->dev, "Resume failed with code %d.\n",
 303				retval);
 304	}
 305
 306	return retval;
 307}
 308
 309static int rmi_resume_functions(struct rmi_device *rmi_dev)
 310{
 311	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 312	struct rmi_function *entry;
 313	int retval;
 314
 315	list_for_each_entry(entry, &data->function_list, node) {
 316		retval = resume_one_function(entry);
 317		if (retval < 0)
 318			return retval;
 319	}
 320
 321	return 0;
 322}
 323
 324int rmi_enable_sensor(struct rmi_device *rmi_dev)
 325{
 326	int retval = 0;
 327
 328	retval = rmi_driver_process_config_requests(rmi_dev);
 329	if (retval < 0)
 330		return retval;
 331
 332	return rmi_process_interrupt_requests(rmi_dev);
 333}
 334
 335/**
 336 * rmi_driver_set_input_params - set input device id and other data.
 337 *
 338 * @rmi_dev: Pointer to an RMI device
 339 * @input: Pointer to input device
 340 *
 341 */
 342static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
 343				struct input_dev *input)
 344{
 345	input->name = SYNAPTICS_INPUT_DEVICE_NAME;
 346	input->id.vendor  = SYNAPTICS_VENDOR_ID;
 347	input->id.bustype = BUS_RMI;
 348	return 0;
 349}
 350
 351static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
 352				struct input_dev *input)
 353{
 354	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 355	const char *device_name = rmi_f01_get_product_ID(data->f01_container);
 356	char *name;
 357
 358	name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
 359			      "Synaptics %s", device_name);
 360	if (!name)
 361		return;
 362
 363	input->name = name;
 364}
 365
 366static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
 367				   unsigned long *mask)
 368{
 369	int error = 0;
 370	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 371	struct device *dev = &rmi_dev->dev;
 372
 373	mutex_lock(&data->irq_mutex);
 374	bitmap_or(data->new_irq_mask,
 375		  data->current_irq_mask, mask, data->irq_count);
 376
 377	error = rmi_write_block(rmi_dev,
 378			data->f01_container->fd.control_base_addr + 1,
 379			data->new_irq_mask, data->num_of_irq_regs);
 380	if (error < 0) {
 381		dev_err(dev, "%s: Failed to change enabled interrupts!",
 382							__func__);
 383		goto error_unlock;
 384	}
 385	bitmap_copy(data->current_irq_mask, data->new_irq_mask,
 386		    data->num_of_irq_regs);
 387
 388	bitmap_or(data->fn_irq_bits, data->fn_irq_bits, mask, data->irq_count);
 389
 390error_unlock:
 391	mutex_unlock(&data->irq_mutex);
 392	return error;
 393}
 394
 395static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
 396				     unsigned long *mask)
 397{
 398	int error = 0;
 399	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 400	struct device *dev = &rmi_dev->dev;
 401
 402	mutex_lock(&data->irq_mutex);
 403	bitmap_andnot(data->fn_irq_bits,
 404		      data->fn_irq_bits, mask, data->irq_count);
 405	bitmap_andnot(data->new_irq_mask,
 406		  data->current_irq_mask, mask, data->irq_count);
 407
 408	error = rmi_write_block(rmi_dev,
 409			data->f01_container->fd.control_base_addr + 1,
 410			data->new_irq_mask, data->num_of_irq_regs);
 411	if (error < 0) {
 412		dev_err(dev, "%s: Failed to change enabled interrupts!",
 413							__func__);
 414		goto error_unlock;
 415	}
 416	bitmap_copy(data->current_irq_mask, data->new_irq_mask,
 417		    data->num_of_irq_regs);
 418
 419error_unlock:
 420	mutex_unlock(&data->irq_mutex);
 421	return error;
 422}
 423
 424static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
 425{
 426	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 427	int error;
 428
 429	/*
 430	 * Can get called before the driver is fully ready to deal with
 431	 * this situation.
 432	 */
 433	if (!data || !data->f01_container) {
 434		dev_warn(&rmi_dev->dev,
 435			 "Not ready to handle reset yet!\n");
 436		return 0;
 437	}
 438
 439	error = rmi_read_block(rmi_dev,
 440			       data->f01_container->fd.control_base_addr + 1,
 441			       data->current_irq_mask, data->num_of_irq_regs);
 442	if (error < 0) {
 443		dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
 444			__func__);
 445		return error;
 446	}
 447
 448	error = rmi_driver_process_reset_requests(rmi_dev);
 449	if (error < 0)
 450		return error;
 451
 452	error = rmi_driver_process_config_requests(rmi_dev);
 453	if (error < 0)
 454		return error;
 455
 456	return 0;
 457}
 458
 459static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
 460			      struct pdt_entry *entry, u16 pdt_address)
 461{
 462	u8 buf[RMI_PDT_ENTRY_SIZE];
 463	int error;
 464
 465	error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
 466	if (error) {
 467		dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
 468				pdt_address, error);
 469		return error;
 470	}
 471
 472	entry->page_start = pdt_address & RMI4_PAGE_MASK;
 473	entry->query_base_addr = buf[0];
 474	entry->command_base_addr = buf[1];
 475	entry->control_base_addr = buf[2];
 476	entry->data_base_addr = buf[3];
 477	entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
 478	entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
 479	entry->function_number = buf[5];
 480
 481	return 0;
 482}
 
 483
 484static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
 485				      struct rmi_function_descriptor *fd)
 486{
 487	fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
 488	fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
 489	fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
 490	fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
 491	fd->function_number = pdt->function_number;
 492	fd->interrupt_source_count = pdt->interrupt_source_count;
 493	fd->function_version = pdt->function_version;
 494}
 495
 496#define RMI_SCAN_CONTINUE	0
 497#define RMI_SCAN_DONE		1
 498
 499static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
 500			     int page,
 501			     int *empty_pages,
 502			     void *ctx,
 503			     int (*callback)(struct rmi_device *rmi_dev,
 504					     void *ctx,
 505					     const struct pdt_entry *entry))
 506{
 507	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 508	struct pdt_entry pdt_entry;
 509	u16 page_start = RMI4_PAGE_SIZE * page;
 510	u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
 511	u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
 512	u16 addr;
 513	int error;
 514	int retval;
 515
 516	for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
 517		error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
 518		if (error)
 519			return error;
 520
 521		if (RMI4_END_OF_PDT(pdt_entry.function_number))
 522			break;
 523
 524		retval = callback(rmi_dev, ctx, &pdt_entry);
 525		if (retval != RMI_SCAN_CONTINUE)
 526			return retval;
 527	}
 528
 529	/*
 530	 * Count number of empty PDT pages. If a gap of two pages
 531	 * or more is found, stop scanning.
 532	 */
 533	if (addr == pdt_start)
 534		++*empty_pages;
 535	else
 536		*empty_pages = 0;
 537
 538	return (data->bootloader_mode || *empty_pages >= 2) ?
 539					RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
 540}
 541
 542int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
 543		 int (*callback)(struct rmi_device *rmi_dev,
 544		 void *ctx, const struct pdt_entry *entry))
 
 545{
 546	int page;
 547	int empty_pages = 0;
 548	int retval = RMI_SCAN_DONE;
 549
 550	for (page = 0; page <= RMI4_MAX_PAGE; page++) {
 551		retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
 552					   ctx, callback);
 553		if (retval != RMI_SCAN_CONTINUE)
 554			break;
 555	}
 556
 557	return retval < 0 ? retval : 0;
 558}
 559
 560int rmi_read_register_desc(struct rmi_device *d, u16 addr,
 561				struct rmi_register_descriptor *rdesc)
 562{
 563	int ret;
 564	u8 size_presence_reg;
 565	u8 buf[35];
 566	int presense_offset = 1;
 567	u8 *struct_buf;
 568	int reg;
 569	int offset = 0;
 570	int map_offset = 0;
 571	int i;
 572	int b;
 573
 574	/*
 575	 * The first register of the register descriptor is the size of
 576	 * the register descriptor's presense register.
 577	 */
 578	ret = rmi_read(d, addr, &size_presence_reg);
 579	if (ret)
 580		return ret;
 581	++addr;
 582
 583	if (size_presence_reg < 0 || size_presence_reg > 35)
 584		return -EIO;
 585
 586	memset(buf, 0, sizeof(buf));
 587
 588	/*
 589	 * The presence register contains the size of the register structure
 590	 * and a bitmap which identified which packet registers are present
 591	 * for this particular register type (ie query, control, or data).
 592	 */
 593	ret = rmi_read_block(d, addr, buf, size_presence_reg);
 594	if (ret)
 595		return ret;
 596	++addr;
 597
 598	if (buf[0] == 0) {
 599		presense_offset = 3;
 600		rdesc->struct_size = buf[1] | (buf[2] << 8);
 601	} else {
 602		rdesc->struct_size = buf[0];
 603	}
 604
 605	for (i = presense_offset; i < size_presence_reg; i++) {
 606		for (b = 0; b < 8; b++) {
 607			if (buf[i] & (0x1 << b))
 608				bitmap_set(rdesc->presense_map, map_offset, 1);
 609			++map_offset;
 610		}
 611	}
 612
 613	rdesc->num_registers = bitmap_weight(rdesc->presense_map,
 614						RMI_REG_DESC_PRESENSE_BITS);
 615
 616	rdesc->registers = devm_kcalloc(&d->dev,
 617					rdesc->num_registers,
 618					sizeof(struct rmi_register_desc_item),
 619					GFP_KERNEL);
 620	if (!rdesc->registers)
 621		return -ENOMEM;
 622
 623	/*
 624	 * Allocate a temporary buffer to hold the register structure.
 625	 * I'm not using devm_kzalloc here since it will not be retained
 626	 * after exiting this function
 627	 */
 628	struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
 629	if (!struct_buf)
 630		return -ENOMEM;
 631
 632	/*
 633	 * The register structure contains information about every packet
 634	 * register of this type. This includes the size of the packet
 635	 * register and a bitmap of all subpackets contained in the packet
 636	 * register.
 637	 */
 638	ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
 639	if (ret)
 640		goto free_struct_buff;
 641
 642	reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
 
 643	for (i = 0; i < rdesc->num_registers; i++) {
 644		struct rmi_register_desc_item *item = &rdesc->registers[i];
 645		int reg_size = struct_buf[offset];
 646
 647		++offset;
 648		if (reg_size == 0) {
 649			reg_size = struct_buf[offset] |
 650					(struct_buf[offset + 1] << 8);
 651			offset += 2;
 652		}
 653
 654		if (reg_size == 0) {
 655			reg_size = struct_buf[offset] |
 656					(struct_buf[offset + 1] << 8) |
 657					(struct_buf[offset + 2] << 16) |
 658					(struct_buf[offset + 3] << 24);
 659			offset += 4;
 660		}
 661
 662		item->reg = reg;
 663		item->reg_size = reg_size;
 664
 665		map_offset = 0;
 666
 667		do {
 668			for (b = 0; b < 7; b++) {
 669				if (struct_buf[offset] & (0x1 << b))
 670					bitmap_set(item->subpacket_map,
 671						map_offset, 1);
 672				++map_offset;
 673			}
 674		} while (struct_buf[offset++] & 0x80);
 675
 676		item->num_subpackets = bitmap_weight(item->subpacket_map,
 677						RMI_REG_DESC_SUBPACKET_BITS);
 678
 679		rmi_dbg(RMI_DEBUG_CORE, &d->dev,
 680			"%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
 681			item->reg, item->reg_size, item->num_subpackets);
 682
 683		reg = find_next_bit(rdesc->presense_map,
 684				RMI_REG_DESC_PRESENSE_BITS, reg + 1);
 685	}
 686
 687free_struct_buff:
 688	kfree(struct_buf);
 689	return ret;
 690}
 
 691
 692const struct rmi_register_desc_item *rmi_get_register_desc_item(
 693				struct rmi_register_descriptor *rdesc, u16 reg)
 694{
 695	const struct rmi_register_desc_item *item;
 696	int i;
 697
 698	for (i = 0; i < rdesc->num_registers; i++) {
 699		item = &rdesc->registers[i];
 700		if (item->reg == reg)
 701			return item;
 702	}
 703
 704	return NULL;
 705}
 
 706
 707size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
 708{
 709	const struct rmi_register_desc_item *item;
 710	int i;
 711	size_t size = 0;
 712
 713	for (i = 0; i < rdesc->num_registers; i++) {
 714		item = &rdesc->registers[i];
 715		size += item->reg_size;
 716	}
 717	return size;
 718}
 
 719
 720/* Compute the register offset relative to the base address */
 721int rmi_register_desc_calc_reg_offset(
 722		struct rmi_register_descriptor *rdesc, u16 reg)
 723{
 724	const struct rmi_register_desc_item *item;
 725	int offset = 0;
 726	int i;
 727
 728	for (i = 0; i < rdesc->num_registers; i++) {
 729		item = &rdesc->registers[i];
 730		if (item->reg == reg)
 731			return offset;
 732		++offset;
 733	}
 734	return -1;
 735}
 
 736
 737bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
 738	u8 subpacket)
 739{
 740	return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
 741				subpacket) == subpacket;
 742}
 743
 
 
 
 
 
 
 
 
 744static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
 745				     const struct pdt_entry *pdt)
 746{
 747	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 748	int ret;
 749	u8 status;
 750
 751	if (pdt->function_number == 0x34 && pdt->function_version > 1) {
 752		ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
 753		if (ret) {
 754			dev_err(&rmi_dev->dev,
 755				"Failed to read F34 status: %d.\n", ret);
 756			return ret;
 757		}
 758
 759		if (status & BIT(7))
 760			data->bootloader_mode = true;
 761	} else if (pdt->function_number == 0x01) {
 762		ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
 763		if (ret) {
 764			dev_err(&rmi_dev->dev,
 765				"Failed to read F01 status: %d.\n", ret);
 766			return ret;
 767		}
 768
 769		if (status & BIT(6))
 770			data->bootloader_mode = true;
 
 
 
 
 771	}
 772
 773	return 0;
 774}
 775
 776static int rmi_count_irqs(struct rmi_device *rmi_dev,
 777			 void *ctx, const struct pdt_entry *pdt)
 778{
 
 779	int *irq_count = ctx;
 780	int ret;
 781
 782	*irq_count += pdt->interrupt_source_count;
 783
 784	ret = rmi_check_bootloader_mode(rmi_dev, pdt);
 785	if (ret < 0)
 786		return ret;
 
 
 
 787
 788	return RMI_SCAN_CONTINUE;
 789}
 790
 791int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
 792		      const struct pdt_entry *pdt)
 793{
 794	int error;
 795
 796	if (pdt->function_number == 0x01) {
 797		u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
 798		u8 cmd_buf = RMI_DEVICE_RESET_CMD;
 799		const struct rmi_device_platform_data *pdata =
 800				rmi_get_platform_data(rmi_dev);
 801
 802		if (rmi_dev->xport->ops->reset) {
 803			error = rmi_dev->xport->ops->reset(rmi_dev->xport,
 804								cmd_addr);
 805			if (error)
 806				return error;
 807
 808			return RMI_SCAN_DONE;
 809		}
 810
 811		rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
 812		error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
 813		if (error) {
 814			dev_err(&rmi_dev->dev,
 815				"Initial reset failed. Code = %d.\n", error);
 816			return error;
 817		}
 818
 819		mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
 820
 821		return RMI_SCAN_DONE;
 822	}
 823
 824	/* F01 should always be on page 0. If we don't find it there, fail. */
 825	return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
 826}
 827
 828static int rmi_create_function(struct rmi_device *rmi_dev,
 829			       void *ctx, const struct pdt_entry *pdt)
 830{
 831	struct device *dev = &rmi_dev->dev;
 832	struct rmi_driver_data *data = dev_get_drvdata(dev);
 833	int *current_irq_count = ctx;
 834	struct rmi_function *fn;
 835	int i;
 836	int error;
 837
 838	rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
 839			pdt->function_number);
 840
 841	fn = kzalloc(sizeof(struct rmi_function) +
 842			BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
 843		     GFP_KERNEL);
 844	if (!fn) {
 845		dev_err(dev, "Failed to allocate memory for F%02X\n",
 846			pdt->function_number);
 847		return -ENOMEM;
 848	}
 849
 850	INIT_LIST_HEAD(&fn->node);
 851	rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
 852
 853	fn->rmi_dev = rmi_dev;
 854
 855	fn->num_of_irqs = pdt->interrupt_source_count;
 856	fn->irq_pos = *current_irq_count;
 857	*current_irq_count += fn->num_of_irqs;
 858
 859	for (i = 0; i < fn->num_of_irqs; i++)
 860		set_bit(fn->irq_pos + i, fn->irq_mask);
 861
 862	error = rmi_register_function(fn);
 863	if (error)
 864		return error;
 865
 866	if (pdt->function_number == 0x01)
 867		data->f01_container = fn;
 868	else if (pdt->function_number == 0x34)
 869		data->f34_container = fn;
 870
 871	list_add_tail(&fn->node, &data->function_list);
 872
 873	return RMI_SCAN_CONTINUE;
 874}
 875
 876void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
 877{
 878	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
 879	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 880	int irq = pdata->irq;
 881	int irq_flags;
 882	int retval;
 883
 884	mutex_lock(&data->enabled_mutex);
 885
 886	if (data->enabled)
 887		goto out;
 888
 889	enable_irq(irq);
 890	data->enabled = true;
 891	if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
 892		retval = disable_irq_wake(irq);
 893		if (retval)
 894			dev_warn(&rmi_dev->dev,
 895				 "Failed to disable irq for wake: %d\n",
 896				 retval);
 897	}
 898
 899	/*
 900	 * Call rmi_process_interrupt_requests() after enabling irq,
 901	 * otherwise we may lose interrupt on edge-triggered systems.
 902	 */
 903	irq_flags = irq_get_trigger_type(pdata->irq);
 904	if (irq_flags & IRQ_TYPE_EDGE_BOTH)
 905		rmi_process_interrupt_requests(rmi_dev);
 906
 907out:
 908	mutex_unlock(&data->enabled_mutex);
 909}
 910
 911void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
 912{
 913	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
 914	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 915	struct rmi4_attn_data attn_data = {0};
 916	int irq = pdata->irq;
 917	int retval, count;
 918
 919	mutex_lock(&data->enabled_mutex);
 920
 921	if (!data->enabled)
 922		goto out;
 923
 924	data->enabled = false;
 925	disable_irq(irq);
 926	if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
 927		retval = enable_irq_wake(irq);
 928		if (retval)
 929			dev_warn(&rmi_dev->dev,
 930				 "Failed to enable irq for wake: %d\n",
 931				 retval);
 932	}
 933
 934	/* make sure the fifo is clean */
 935	while (!kfifo_is_empty(&data->attn_fifo)) {
 936		count = kfifo_get(&data->attn_fifo, &attn_data);
 937		if (count)
 938			kfree(attn_data.data);
 939	}
 940
 941out:
 942	mutex_unlock(&data->enabled_mutex);
 943}
 944
 945int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
 946{
 947	int retval;
 948
 949	retval = rmi_suspend_functions(rmi_dev);
 950	if (retval)
 951		dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
 952			retval);
 953
 954	rmi_disable_irq(rmi_dev, enable_wake);
 955	return retval;
 956}
 957EXPORT_SYMBOL_GPL(rmi_driver_suspend);
 958
 959int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
 960{
 961	int retval;
 962
 963	rmi_enable_irq(rmi_dev, clear_wake);
 964
 965	retval = rmi_resume_functions(rmi_dev);
 966	if (retval)
 967		dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
 968			retval);
 969
 970	return retval;
 971}
 972EXPORT_SYMBOL_GPL(rmi_driver_resume);
 973
 974static int rmi_driver_remove(struct device *dev)
 975{
 976	struct rmi_device *rmi_dev = to_rmi_device(dev);
 977	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 978
 979	rmi_disable_irq(rmi_dev, false);
 980
 981	rmi_f34_remove_sysfs(rmi_dev);
 982	rmi_free_function_list(rmi_dev);
 983
 984	irq_domain_remove(data->irqdomain);
 985	data->irqdomain = NULL;
 986
 987	return 0;
 988}
 989
 990#ifdef CONFIG_OF
 991static int rmi_driver_of_probe(struct device *dev,
 992				struct rmi_device_platform_data *pdata)
 993{
 994	int retval;
 995
 996	retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
 997					"syna,reset-delay-ms", 1);
 998	if (retval)
 999		return retval;
1000
1001	return 0;
1002}
1003#else
1004static inline int rmi_driver_of_probe(struct device *dev,
1005					struct rmi_device_platform_data *pdata)
1006{
1007	return -ENODEV;
1008}
1009#endif
1010
1011int 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
1065int 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
1097err_destroy_functions:
1098	rmi_free_function_list(rmi_dev);
1099	return retval;
1100}
1101
1102static 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
1235err_disable_irq:
1236	rmi_disable_irq(rmi_dev, false);
1237err_destroy_functions:
1238	rmi_free_function_list(rmi_dev);
1239err:
1240	return retval;
1241}
1242
1243static 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
1257bool rmi_is_physical_driver(struct device_driver *drv)
1258{
1259	return drv == &rmi_physical_driver.driver;
1260}
1261
1262int __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
1276void __exit rmi_unregister_physical_driver(void)
1277{
1278	driver_unregister(&rmi_physical_driver.driver);
1279}
v4.6
 
   1/*
   2 * Copyright (c) 2011-2016 Synaptics Incorporated
   3 * Copyright (c) 2011 Unixphere
   4 *
   5 * This driver provides the core support for a single RMI4-based device.
   6 *
   7 * The RMI4 specification can be found here (URL split for line length):
   8 *
   9 * http://www.synaptics.com/sites/default/files/
  10 *      511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
  11 *
  12 * This program is free software; you can redistribute it and/or modify it
  13 * under the terms of the GNU General Public License version 2 as published by
  14 * the Free Software Foundation.
  15 */
  16
  17#include <linux/bitmap.h>
  18#include <linux/delay.h>
  19#include <linux/fs.h>
  20#include <linux/kconfig.h>
  21#include <linux/pm.h>
  22#include <linux/slab.h>
  23#include <linux/of.h>
 
  24#include <uapi/linux/input.h>
  25#include <linux/rmi.h>
  26#include "rmi_bus.h"
  27#include "rmi_driver.h"
  28
  29#define HAS_NONSTANDARD_PDT_MASK 0x40
  30#define RMI4_MAX_PAGE 0xff
  31#define RMI4_PAGE_SIZE 0x100
  32#define RMI4_PAGE_MASK 0xFF00
  33
  34#define RMI_DEVICE_RESET_CMD	0x01
  35#define DEFAULT_RESET_DELAY_MS	100
  36
  37static void rmi_free_function_list(struct rmi_device *rmi_dev)
  38{
  39	struct rmi_function *fn, *tmp;
  40	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  41
  42	data->f01_container = NULL;
  43
  44	/* Doing it in the reverse order so F01 will be removed last */
  45	list_for_each_entry_safe_reverse(fn, tmp,
  46					 &data->function_list, node) {
  47		list_del(&fn->node);
  48		rmi_unregister_function(fn);
  49	}
 
 
 
 
 
 
 
 
 
 
  50}
  51
  52static int reset_one_function(struct rmi_function *fn)
  53{
  54	struct rmi_function_handler *fh;
  55	int retval = 0;
  56
  57	if (!fn || !fn->dev.driver)
  58		return 0;
  59
  60	fh = to_rmi_function_handler(fn->dev.driver);
  61	if (fh->reset) {
  62		retval = fh->reset(fn);
  63		if (retval < 0)
  64			dev_err(&fn->dev, "Reset failed with code %d.\n",
  65				retval);
  66	}
  67
  68	return retval;
  69}
  70
  71static int configure_one_function(struct rmi_function *fn)
  72{
  73	struct rmi_function_handler *fh;
  74	int retval = 0;
  75
  76	if (!fn || !fn->dev.driver)
  77		return 0;
  78
  79	fh = to_rmi_function_handler(fn->dev.driver);
  80	if (fh->config) {
  81		retval = fh->config(fn);
  82		if (retval < 0)
  83			dev_err(&fn->dev, "Config failed with code %d.\n",
  84				retval);
  85	}
  86
  87	return retval;
  88}
  89
  90static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
  91{
  92	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  93	struct rmi_function *entry;
  94	int retval;
  95
  96	list_for_each_entry(entry, &data->function_list, node) {
  97		retval = reset_one_function(entry);
  98		if (retval < 0)
  99			return retval;
 100	}
 101
 102	return 0;
 103}
 104
 105static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
 106{
 107	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 108	struct rmi_function *entry;
 109	int retval;
 110
 111	list_for_each_entry(entry, &data->function_list, node) {
 112		retval = configure_one_function(entry);
 113		if (retval < 0)
 114			return retval;
 115	}
 116
 117	return 0;
 118}
 119
 120static void process_one_interrupt(struct rmi_driver_data *data,
 121				  struct rmi_function *fn)
 122{
 123	struct rmi_function_handler *fh;
 124
 125	if (!fn || !fn->dev.driver)
 126		return;
 127
 128	fh = to_rmi_function_handler(fn->dev.driver);
 129	if (fh->attention) {
 130		bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
 131				data->irq_count);
 132		if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
 133			fh->attention(fn, data->fn_irq_bits);
 134	}
 135}
 136
 137int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
 138{
 139	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 140	struct device *dev = &rmi_dev->dev;
 141	struct rmi_function *entry;
 142	int error;
 143
 144	if (!data)
 145		return 0;
 146
 147	if (!rmi_dev->xport->attn_data) {
 148		error = rmi_read_block(rmi_dev,
 149				data->f01_container->fd.data_base_addr + 1,
 150				data->irq_status, data->num_of_irq_regs);
 151		if (error < 0) {
 152			dev_err(dev, "Failed to read irqs, code=%d\n", error);
 153			return error;
 154		}
 155	}
 156
 157	mutex_lock(&data->irq_mutex);
 158	bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
 159	       data->irq_count);
 160	/*
 161	 * At this point, irq_status has all bits that are set in the
 162	 * interrupt status register and are enabled.
 163	 */
 164	mutex_unlock(&data->irq_mutex);
 165
 166	/*
 167	 * It would be nice to be able to use irq_chip to handle these
 168	 * nested IRQs.  Unfortunately, most of the current customers for
 169	 * this driver are using older kernels (3.0.x) that don't support
 170	 * the features required for that.  Once they've shifted to more
 171	 * recent kernels (say, 3.3 and higher), this should be switched to
 172	 * use irq_chip.
 173	 */
 174	list_for_each_entry(entry, &data->function_list, node)
 175		process_one_interrupt(data, entry);
 176
 177	if (data->input)
 178		input_sync(data->input);
 179
 180	return 0;
 181}
 182EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 183
 184static int suspend_one_function(struct rmi_function *fn)
 185{
 186	struct rmi_function_handler *fh;
 187	int retval = 0;
 188
 189	if (!fn || !fn->dev.driver)
 190		return 0;
 191
 192	fh = to_rmi_function_handler(fn->dev.driver);
 193	if (fh->suspend) {
 194		retval = fh->suspend(fn);
 195		if (retval < 0)
 196			dev_err(&fn->dev, "Suspend failed with code %d.\n",
 197				retval);
 198	}
 199
 200	return retval;
 201}
 202
 203static int rmi_suspend_functions(struct rmi_device *rmi_dev)
 204{
 205	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 206	struct rmi_function *entry;
 207	int retval;
 208
 209	list_for_each_entry(entry, &data->function_list, node) {
 210		retval = suspend_one_function(entry);
 211		if (retval < 0)
 212			return retval;
 213	}
 214
 215	return 0;
 216}
 217
 218static int resume_one_function(struct rmi_function *fn)
 219{
 220	struct rmi_function_handler *fh;
 221	int retval = 0;
 222
 223	if (!fn || !fn->dev.driver)
 224		return 0;
 225
 226	fh = to_rmi_function_handler(fn->dev.driver);
 227	if (fh->resume) {
 228		retval = fh->resume(fn);
 229		if (retval < 0)
 230			dev_err(&fn->dev, "Resume failed with code %d.\n",
 231				retval);
 232	}
 233
 234	return retval;
 235}
 236
 237static int rmi_resume_functions(struct rmi_device *rmi_dev)
 238{
 239	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 240	struct rmi_function *entry;
 241	int retval;
 242
 243	list_for_each_entry(entry, &data->function_list, node) {
 244		retval = resume_one_function(entry);
 245		if (retval < 0)
 246			return retval;
 247	}
 248
 249	return 0;
 250}
 251
 252static int enable_sensor(struct rmi_device *rmi_dev)
 253{
 254	int retval = 0;
 255
 256	retval = rmi_driver_process_config_requests(rmi_dev);
 257	if (retval < 0)
 258		return retval;
 259
 260	return rmi_process_interrupt_requests(rmi_dev);
 261}
 262
 263/**
 264 * rmi_driver_set_input_params - set input device id and other data.
 265 *
 266 * @rmi_dev: Pointer to an RMI device
 267 * @input: Pointer to input device
 268 *
 269 */
 270static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
 271				struct input_dev *input)
 272{
 273	input->name = SYNAPTICS_INPUT_DEVICE_NAME;
 274	input->id.vendor  = SYNAPTICS_VENDOR_ID;
 275	input->id.bustype = BUS_RMI;
 276	return 0;
 277}
 278
 279static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
 280				struct input_dev *input)
 281{
 282	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 283	char *device_name = rmi_f01_get_product_ID(data->f01_container);
 284	char *name;
 285
 286	name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
 287			      "Synaptics %s", device_name);
 288	if (!name)
 289		return;
 290
 291	input->name = name;
 292}
 293
 294static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
 295				   unsigned long *mask)
 296{
 297	int error = 0;
 298	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 299	struct device *dev = &rmi_dev->dev;
 300
 301	mutex_lock(&data->irq_mutex);
 302	bitmap_or(data->new_irq_mask,
 303		  data->current_irq_mask, mask, data->irq_count);
 304
 305	error = rmi_write_block(rmi_dev,
 306			data->f01_container->fd.control_base_addr + 1,
 307			data->new_irq_mask, data->num_of_irq_regs);
 308	if (error < 0) {
 309		dev_err(dev, "%s: Failed to change enabled interrupts!",
 310							__func__);
 311		goto error_unlock;
 312	}
 313	bitmap_copy(data->current_irq_mask, data->new_irq_mask,
 314		    data->num_of_irq_regs);
 315
 
 
 316error_unlock:
 317	mutex_unlock(&data->irq_mutex);
 318	return error;
 319}
 320
 321static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
 322				     unsigned long *mask)
 323{
 324	int error = 0;
 325	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 326	struct device *dev = &rmi_dev->dev;
 327
 328	mutex_lock(&data->irq_mutex);
 
 
 329	bitmap_andnot(data->new_irq_mask,
 330		  data->current_irq_mask, mask, data->irq_count);
 331
 332	error = rmi_write_block(rmi_dev,
 333			data->f01_container->fd.control_base_addr + 1,
 334			data->new_irq_mask, data->num_of_irq_regs);
 335	if (error < 0) {
 336		dev_err(dev, "%s: Failed to change enabled interrupts!",
 337							__func__);
 338		goto error_unlock;
 339	}
 340	bitmap_copy(data->current_irq_mask, data->new_irq_mask,
 341		    data->num_of_irq_regs);
 342
 343error_unlock:
 344	mutex_unlock(&data->irq_mutex);
 345	return error;
 346}
 347
 348static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
 349{
 350	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 351	int error;
 352
 353	/*
 354	 * Can get called before the driver is fully ready to deal with
 355	 * this situation.
 356	 */
 357	if (!data || !data->f01_container) {
 358		dev_warn(&rmi_dev->dev,
 359			 "Not ready to handle reset yet!\n");
 360		return 0;
 361	}
 362
 363	error = rmi_read_block(rmi_dev,
 364			       data->f01_container->fd.control_base_addr + 1,
 365			       data->current_irq_mask, data->num_of_irq_regs);
 366	if (error < 0) {
 367		dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
 368			__func__);
 369		return error;
 370	}
 371
 372	error = rmi_driver_process_reset_requests(rmi_dev);
 373	if (error < 0)
 374		return error;
 375
 376	error = rmi_driver_process_config_requests(rmi_dev);
 377	if (error < 0)
 378		return error;
 379
 380	return 0;
 381}
 382
 383int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
 384			u16 pdt_address)
 385{
 386	u8 buf[RMI_PDT_ENTRY_SIZE];
 387	int error;
 388
 389	error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
 390	if (error) {
 391		dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
 392				pdt_address, error);
 393		return error;
 394	}
 395
 396	entry->page_start = pdt_address & RMI4_PAGE_MASK;
 397	entry->query_base_addr = buf[0];
 398	entry->command_base_addr = buf[1];
 399	entry->control_base_addr = buf[2];
 400	entry->data_base_addr = buf[3];
 401	entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
 402	entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
 403	entry->function_number = buf[5];
 404
 405	return 0;
 406}
 407EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
 408
 409static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
 410				      struct rmi_function_descriptor *fd)
 411{
 412	fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
 413	fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
 414	fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
 415	fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
 416	fd->function_number = pdt->function_number;
 417	fd->interrupt_source_count = pdt->interrupt_source_count;
 418	fd->function_version = pdt->function_version;
 419}
 420
 421#define RMI_SCAN_CONTINUE	0
 422#define RMI_SCAN_DONE		1
 423
 424static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
 425			     int page,
 
 426			     void *ctx,
 427			     int (*callback)(struct rmi_device *rmi_dev,
 428					     void *ctx,
 429					     const struct pdt_entry *entry))
 430{
 431	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 432	struct pdt_entry pdt_entry;
 433	u16 page_start = RMI4_PAGE_SIZE * page;
 434	u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
 435	u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
 436	u16 addr;
 437	int error;
 438	int retval;
 439
 440	for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
 441		error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
 442		if (error)
 443			return error;
 444
 445		if (RMI4_END_OF_PDT(pdt_entry.function_number))
 446			break;
 447
 448		retval = callback(rmi_dev, ctx, &pdt_entry);
 449		if (retval != RMI_SCAN_CONTINUE)
 450			return retval;
 451	}
 452
 453	return (data->f01_bootloader_mode || addr == pdt_start) ?
 
 
 
 
 
 
 
 
 
 454					RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
 455}
 456
 457static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
 458			int (*callback)(struct rmi_device *rmi_dev,
 459					void *ctx,
 460					const struct pdt_entry *entry))
 461{
 462	int page;
 
 463	int retval = RMI_SCAN_DONE;
 464
 465	for (page = 0; page <= RMI4_MAX_PAGE; page++) {
 466		retval = rmi_scan_pdt_page(rmi_dev, page, ctx, callback);
 
 467		if (retval != RMI_SCAN_CONTINUE)
 468			break;
 469	}
 470
 471	return retval < 0 ? retval : 0;
 472}
 473
 474int rmi_read_register_desc(struct rmi_device *d, u16 addr,
 475				struct rmi_register_descriptor *rdesc)
 476{
 477	int ret;
 478	u8 size_presence_reg;
 479	u8 buf[35];
 480	int presense_offset = 1;
 481	u8 *struct_buf;
 482	int reg;
 483	int offset = 0;
 484	int map_offset = 0;
 485	int i;
 486	int b;
 487
 488	/*
 489	 * The first register of the register descriptor is the size of
 490	 * the register descriptor's presense register.
 491	 */
 492	ret = rmi_read(d, addr, &size_presence_reg);
 493	if (ret)
 494		return ret;
 495	++addr;
 496
 497	if (size_presence_reg < 0 || size_presence_reg > 35)
 498		return -EIO;
 499
 500	memset(buf, 0, sizeof(buf));
 501
 502	/*
 503	 * The presence register contains the size of the register structure
 504	 * and a bitmap which identified which packet registers are present
 505	 * for this particular register type (ie query, control, or data).
 506	 */
 507	ret = rmi_read_block(d, addr, buf, size_presence_reg);
 508	if (ret)
 509		return ret;
 510	++addr;
 511
 512	if (buf[0] == 0) {
 513		presense_offset = 3;
 514		rdesc->struct_size = buf[1] | (buf[2] << 8);
 515	} else {
 516		rdesc->struct_size = buf[0];
 517	}
 518
 519	for (i = presense_offset; i < size_presence_reg; i++) {
 520		for (b = 0; b < 8; b++) {
 521			if (buf[i] & (0x1 << b))
 522				bitmap_set(rdesc->presense_map, map_offset, 1);
 523			++map_offset;
 524		}
 525	}
 526
 527	rdesc->num_registers = bitmap_weight(rdesc->presense_map,
 528						RMI_REG_DESC_PRESENSE_BITS);
 529
 530	rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
 531				sizeof(struct rmi_register_desc_item),
 532				GFP_KERNEL);
 
 533	if (!rdesc->registers)
 534		return -ENOMEM;
 535
 536	/*
 537	 * Allocate a temporary buffer to hold the register structure.
 538	 * I'm not using devm_kzalloc here since it will not be retained
 539	 * after exiting this function
 540	 */
 541	struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
 542	if (!struct_buf)
 543		return -ENOMEM;
 544
 545	/*
 546	 * The register structure contains information about every packet
 547	 * register of this type. This includes the size of the packet
 548	 * register and a bitmap of all subpackets contained in the packet
 549	 * register.
 550	 */
 551	ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
 552	if (ret)
 553		goto free_struct_buff;
 554
 555	reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
 556	map_offset = 0;
 557	for (i = 0; i < rdesc->num_registers; i++) {
 558		struct rmi_register_desc_item *item = &rdesc->registers[i];
 559		int reg_size = struct_buf[offset];
 560
 561		++offset;
 562		if (reg_size == 0) {
 563			reg_size = struct_buf[offset] |
 564					(struct_buf[offset + 1] << 8);
 565			offset += 2;
 566		}
 567
 568		if (reg_size == 0) {
 569			reg_size = struct_buf[offset] |
 570					(struct_buf[offset + 1] << 8) |
 571					(struct_buf[offset + 2] << 16) |
 572					(struct_buf[offset + 3] << 24);
 573			offset += 4;
 574		}
 575
 576		item->reg = reg;
 577		item->reg_size = reg_size;
 578
 
 
 579		do {
 580			for (b = 0; b < 7; b++) {
 581				if (struct_buf[offset] & (0x1 << b))
 582					bitmap_set(item->subpacket_map,
 583						map_offset, 1);
 584				++map_offset;
 585			}
 586		} while (struct_buf[offset++] & 0x80);
 587
 588		item->num_subpackets = bitmap_weight(item->subpacket_map,
 589						RMI_REG_DESC_SUBPACKET_BITS);
 590
 591		rmi_dbg(RMI_DEBUG_CORE, &d->dev,
 592			"%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
 593			item->reg, item->reg_size, item->num_subpackets);
 594
 595		reg = find_next_bit(rdesc->presense_map,
 596				RMI_REG_DESC_PRESENSE_BITS, reg + 1);
 597	}
 598
 599free_struct_buff:
 600	kfree(struct_buf);
 601	return ret;
 602}
 603EXPORT_SYMBOL_GPL(rmi_read_register_desc);
 604
 605const struct rmi_register_desc_item *rmi_get_register_desc_item(
 606				struct rmi_register_descriptor *rdesc, u16 reg)
 607{
 608	const struct rmi_register_desc_item *item;
 609	int i;
 610
 611	for (i = 0; i < rdesc->num_registers; i++) {
 612		item = &rdesc->registers[i];
 613		if (item->reg == reg)
 614			return item;
 615	}
 616
 617	return NULL;
 618}
 619EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
 620
 621size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
 622{
 623	const struct rmi_register_desc_item *item;
 624	int i;
 625	size_t size = 0;
 626
 627	for (i = 0; i < rdesc->num_registers; i++) {
 628		item = &rdesc->registers[i];
 629		size += item->reg_size;
 630	}
 631	return size;
 632}
 633EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
 634
 635/* Compute the register offset relative to the base address */
 636int rmi_register_desc_calc_reg_offset(
 637		struct rmi_register_descriptor *rdesc, u16 reg)
 638{
 639	const struct rmi_register_desc_item *item;
 640	int offset = 0;
 641	int i;
 642
 643	for (i = 0; i < rdesc->num_registers; i++) {
 644		item = &rdesc->registers[i];
 645		if (item->reg == reg)
 646			return offset;
 647		++offset;
 648	}
 649	return -1;
 650}
 651EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
 652
 653bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
 654	u8 subpacket)
 655{
 656	return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
 657				subpacket) == subpacket;
 658}
 659
 660/* Indicates that flash programming is enabled (bootloader mode). */
 661#define RMI_F01_STATUS_BOOTLOADER(status)	(!!((status) & 0x40))
 662
 663/*
 664 * Given the PDT entry for F01, read the device status register to determine
 665 * if we're stuck in bootloader mode or not.
 666 *
 667 */
 668static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
 669				     const struct pdt_entry *pdt)
 670{
 671	int error;
 672	u8 device_status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 673
 674	error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
 675			 &device_status);
 676	if (error) {
 677		dev_err(&rmi_dev->dev,
 678			"Failed to read device status: %d.\n", error);
 679		return error;
 680	}
 681
 682	return RMI_F01_STATUS_BOOTLOADER(device_status);
 683}
 684
 685static int rmi_count_irqs(struct rmi_device *rmi_dev,
 686			 void *ctx, const struct pdt_entry *pdt)
 687{
 688	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 689	int *irq_count = ctx;
 
 690
 691	*irq_count += pdt->interrupt_source_count;
 692	if (pdt->function_number == 0x01) {
 693		data->f01_bootloader_mode =
 694			rmi_check_bootloader_mode(rmi_dev, pdt);
 695		if (data->f01_bootloader_mode)
 696			dev_warn(&rmi_dev->dev,
 697				"WARNING: RMI4 device is in bootloader mode!\n");
 698	}
 699
 700	return RMI_SCAN_CONTINUE;
 701}
 702
 703static int rmi_initial_reset(struct rmi_device *rmi_dev,
 704			     void *ctx, const struct pdt_entry *pdt)
 705{
 706	int error;
 707
 708	if (pdt->function_number == 0x01) {
 709		u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
 710		u8 cmd_buf = RMI_DEVICE_RESET_CMD;
 711		const struct rmi_device_platform_data *pdata =
 712				rmi_get_platform_data(rmi_dev);
 713
 714		if (rmi_dev->xport->ops->reset) {
 715			error = rmi_dev->xport->ops->reset(rmi_dev->xport,
 716								cmd_addr);
 717			if (error)
 718				return error;
 719
 720			return RMI_SCAN_DONE;
 721		}
 722
 
 723		error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
 724		if (error) {
 725			dev_err(&rmi_dev->dev,
 726				"Initial reset failed. Code = %d.\n", error);
 727			return error;
 728		}
 729
 730		mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
 731
 732		return RMI_SCAN_DONE;
 733	}
 734
 735	/* F01 should always be on page 0. If we don't find it there, fail. */
 736	return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
 737}
 738
 739static int rmi_create_function(struct rmi_device *rmi_dev,
 740			       void *ctx, const struct pdt_entry *pdt)
 741{
 742	struct device *dev = &rmi_dev->dev;
 743	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
 744	int *current_irq_count = ctx;
 745	struct rmi_function *fn;
 746	int i;
 747	int error;
 748
 749	rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
 750			pdt->function_number);
 751
 752	fn = kzalloc(sizeof(struct rmi_function) +
 753			BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
 754		     GFP_KERNEL);
 755	if (!fn) {
 756		dev_err(dev, "Failed to allocate memory for F%02X\n",
 757			pdt->function_number);
 758		return -ENOMEM;
 759	}
 760
 761	INIT_LIST_HEAD(&fn->node);
 762	rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
 763
 764	fn->rmi_dev = rmi_dev;
 765
 766	fn->num_of_irqs = pdt->interrupt_source_count;
 767	fn->irq_pos = *current_irq_count;
 768	*current_irq_count += fn->num_of_irqs;
 769
 770	for (i = 0; i < fn->num_of_irqs; i++)
 771		set_bit(fn->irq_pos + i, fn->irq_mask);
 772
 773	error = rmi_register_function(fn);
 774	if (error)
 775		goto err_put_fn;
 776
 777	if (pdt->function_number == 0x01)
 778		data->f01_container = fn;
 
 
 779
 780	list_add_tail(&fn->node, &data->function_list);
 781
 782	return RMI_SCAN_CONTINUE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 783
 784err_put_fn:
 785	put_device(&fn->dev);
 786	return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 787}
 788
 789int rmi_driver_suspend(struct rmi_device *rmi_dev)
 790{
 791	int retval = 0;
 792
 793	retval = rmi_suspend_functions(rmi_dev);
 794	if (retval)
 795		dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
 796			retval);
 797
 
 798	return retval;
 799}
 800EXPORT_SYMBOL_GPL(rmi_driver_suspend);
 801
 802int rmi_driver_resume(struct rmi_device *rmi_dev)
 803{
 804	int retval;
 805
 
 
 806	retval = rmi_resume_functions(rmi_dev);
 807	if (retval)
 808		dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
 809			retval);
 810
 811	return retval;
 812}
 813EXPORT_SYMBOL_GPL(rmi_driver_resume);
 814
 815static int rmi_driver_remove(struct device *dev)
 816{
 817	struct rmi_device *rmi_dev = to_rmi_device(dev);
 
 818
 
 
 
 819	rmi_free_function_list(rmi_dev);
 820
 
 
 
 821	return 0;
 822}
 823
 824#ifdef CONFIG_OF
 825static int rmi_driver_of_probe(struct device *dev,
 826				struct rmi_device_platform_data *pdata)
 827{
 828	int retval;
 829
 830	retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
 831					"syna,reset-delay-ms", 1);
 832	if (retval)
 833		return retval;
 834
 835	return 0;
 836}
 837#else
 838static inline int rmi_driver_of_probe(struct device *dev,
 839					struct rmi_device_platform_data *pdata)
 840{
 841	return -ENODEV;
 842}
 843#endif
 844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 845static int rmi_driver_probe(struct device *dev)
 846{
 847	struct rmi_driver *rmi_driver;
 848	struct rmi_driver_data *data;
 849	struct rmi_device_platform_data *pdata;
 850	struct rmi_device *rmi_dev;
 851	size_t size;
 852	void *irq_memory;
 853	int irq_count;
 854	int retval;
 855
 856	rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
 857			__func__);
 858
 859	if (!rmi_is_physical_device(dev)) {
 860		rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
 861		return -ENODEV;
 862	}
 863
 864	rmi_dev = to_rmi_device(dev);
 865	rmi_driver = to_rmi_driver(dev->driver);
 866	rmi_dev->driver = rmi_driver;
 867
 868	pdata = rmi_get_platform_data(rmi_dev);
 869
 870	if (rmi_dev->xport->dev->of_node) {
 871		retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
 872		if (retval)
 873			return retval;
 874	}
 875
 876	data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
 877	if (!data)
 878		return -ENOMEM;
 879
 880	INIT_LIST_HEAD(&data->function_list);
 881	data->rmi_dev = rmi_dev;
 882	dev_set_drvdata(&rmi_dev->dev, data);
 883
 884	/*
 885	 * Right before a warm boot, the sensor might be in some unusual state,
 886	 * such as F54 diagnostics, or F34 bootloader mode after a firmware
 887	 * or configuration update.  In order to clear the sensor to a known
 888	 * state and/or apply any updates, we issue a initial reset to clear any
 889	 * previous settings and force it into normal operation.
 890	 *
 891	 * We have to do this before actually building the PDT because
 892	 * the reflash updates (if any) might cause various registers to move
 893	 * around.
 894	 *
 895	 * For a number of reasons, this initial reset may fail to return
 896	 * within the specified time, but we'll still be able to bring up the
 897	 * driver normally after that failure.  This occurs most commonly in
 898	 * a cold boot situation (where then firmware takes longer to come up
 899	 * than from a warm boot) and the reset_delay_ms in the platform data
 900	 * has been set too short to accommodate that.  Since the sensor will
 901	 * eventually come up and be usable, we don't want to just fail here
 902	 * and leave the customer's device unusable.  So we warn them, and
 903	 * continue processing.
 904	 */
 905	retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
 906	if (retval < 0)
 907		dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
 908
 909	retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
 910	if (retval < 0) {
 911		/*
 912		 * we'll print out a warning and continue since
 913		 * failure to get the PDT properties is not a cause to fail
 914		 */
 915		dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
 916			 PDT_PROPERTIES_LOCATION, retval);
 917	}
 918
 919	/*
 920	 * We need to count the IRQs and allocate their storage before scanning
 921	 * the PDT and creating the function entries, because adding a new
 922	 * function can trigger events that result in the IRQ related storage
 923	 * being accessed.
 924	 */
 925	rmi_dbg(RMI_DEBUG_CORE, dev, "Counting IRQs.\n");
 926	irq_count = 0;
 927	retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
 928	if (retval < 0) {
 929		dev_err(dev, "IRQ counting failed with code %d.\n", retval);
 930		goto err;
 931	}
 932	data->irq_count = irq_count;
 933	data->num_of_irq_regs = (data->irq_count + 7) / 8;
 934
 935	mutex_init(&data->irq_mutex);
 
 936
 937	size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
 938	irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
 939	if (!irq_memory) {
 940		dev_err(dev, "Failed to allocate memory for irq masks.\n");
 941		goto err;
 942	}
 943
 944	data->irq_status	= irq_memory + size * 0;
 945	data->fn_irq_bits	= irq_memory + size * 1;
 946	data->current_irq_mask	= irq_memory + size * 2;
 947	data->new_irq_mask	= irq_memory + size * 3;
 948
 949	if (rmi_dev->xport->input) {
 950		/*
 951		 * The transport driver already has an input device.
 952		 * In some cases it is preferable to reuse the transport
 953		 * devices input device instead of creating a new one here.
 954		 * One example is some HID touchpads report "pass-through"
 955		 * button events are not reported by rmi registers.
 956		 */
 957		data->input = rmi_dev->xport->input;
 958	} else {
 959		data->input = devm_input_allocate_device(dev);
 960		if (!data->input) {
 961			dev_err(dev, "%s: Failed to allocate input device.\n",
 962				__func__);
 963			retval = -ENOMEM;
 964			goto err_destroy_functions;
 965		}
 966		rmi_driver_set_input_params(rmi_dev, data->input);
 967		data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
 968						"%s/input0", dev_name(dev));
 969	}
 970
 971	irq_count = 0;
 972	rmi_dbg(RMI_DEBUG_CORE, dev, "Creating functions.");
 973	retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
 974	if (retval < 0) {
 975		dev_err(dev, "Function creation failed with code %d.\n",
 976			retval);
 977		goto err_destroy_functions;
 978	}
 979
 980	if (!data->f01_container) {
 981		dev_err(dev, "Missing F01 container!\n");
 982		retval = -EINVAL;
 983		goto err_destroy_functions;
 984	}
 985
 986	retval = rmi_read_block(rmi_dev,
 987				data->f01_container->fd.control_base_addr + 1,
 988				data->current_irq_mask, data->num_of_irq_regs);
 989	if (retval < 0) {
 990		dev_err(dev, "%s: Failed to read current IRQ mask.\n",
 991			__func__);
 992		goto err_destroy_functions;
 993	}
 994
 995	if (data->input) {
 996		rmi_driver_set_input_name(rmi_dev, data->input);
 997		if (!rmi_dev->xport->input) {
 998			if (input_register_device(data->input)) {
 
 999				dev_err(dev, "%s: Failed to register input device.\n",
1000					__func__);
1001				goto err_destroy_functions;
1002			}
1003		}
1004	}
1005
1006	if (data->f01_container->dev.driver)
 
 
 
 
1007		/* Driver already bound, so enable ATTN now. */
1008		return enable_sensor(rmi_dev);
 
 
 
1009
1010	return 0;
1011
 
 
1012err_destroy_functions:
1013	rmi_free_function_list(rmi_dev);
1014err:
1015	return retval < 0 ? retval : 0;
1016}
1017
1018static struct rmi_driver rmi_physical_driver = {
1019	.driver = {
1020		.owner	= THIS_MODULE,
1021		.name	= "rmi4_physical",
1022		.bus	= &rmi_bus_type,
1023		.probe = rmi_driver_probe,
1024		.remove = rmi_driver_remove,
1025	},
1026	.reset_handler = rmi_driver_reset_handler,
1027	.clear_irq_bits = rmi_driver_clear_irq_bits,
1028	.set_irq_bits = rmi_driver_set_irq_bits,
1029	.set_input_params = rmi_driver_set_input_params,
1030};
1031
1032bool rmi_is_physical_driver(struct device_driver *drv)
1033{
1034	return drv == &rmi_physical_driver.driver;
1035}
1036
1037int __init rmi_register_physical_driver(void)
1038{
1039	int error;
1040
1041	error = driver_register(&rmi_physical_driver.driver);
1042	if (error) {
1043		pr_err("%s: driver register failed, code=%d.\n", __func__,
1044		       error);
1045		return error;
1046	}
1047
1048	return 0;
1049}
1050
1051void __exit rmi_unregister_physical_driver(void)
1052{
1053	driver_unregister(&rmi_physical_driver.driver);
1054}