Skip to contentSkip to conversation

input: Synaptics RMI4 Touchscreen Driver

RFC PATCH In review Christopher Heiny 4 participants 44 messages Aug 17, 2012 – Sep 26, 2012 Page 1 of 2

Subsystems (3)
lore
Share & tools
lore
#20 lore raw

RFC PATCH 7/17 input: RMI4 F01 device control

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f01.c | 1331 ++++++++++++++++++++++++++++++++++++++++++
 drivers/input/rmi4/rmi_f01.h |   88 +++
 2 files changed, 1419 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f01.c new file +1331 -0
new file mode 100644
index 0000000..312ad13
@@ -0,0 +1,1331 @@
+/*
+ * Copyright (c) 2011-2012 Synaptics Incorporated
+ * Copyright (c) 2011 Unixphere
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+#include "rmi_f01.h"
+
+union f01_device_commands {
+	struct {
+		u8 reset:1;
+		u8 reserved:1;
+	};
+	u8 reg;
+};
+
+struct f01_device_control {
+	union f01_device_control_0 ctrl0;
+	u8 *interrupt_enable;
+	u8 doze_interval;
+	u8 wakeup_threshold;
+	u8 doze_holdoff;
+};
+
+union f01_query_42 {
+	struct {
+		u8 has_ds4_queries:1;
+		u8 has_multi_phy:1;
+		u8 has_guest:1;
+		u8 reserved:5;
+	};
+	u8 regs[1];
+};
+
+union f01_ds4_queries {
+	struct {
+		u8 length:4;
+		u8 reserved_1:4;
+
+		u8 has_package_id_query:1;
+		u8 has_packrat_query:1;
+		u8 has_reset_query:1;
+		u8 has_maskrev_query:1;
+		u8 reserved_2:4;
+
+		u8 has_i2c_control:1;
+		u8 has_spi_control:1;
+		u8 has_attn_control:1;
+		u8 reserved_3:5;
+
+		u8 reset_enabled:1;
+		u8 reset_polarity:1;
+		u8 pullup_enabled:1;
+		u8 reserved_4:1;
+		u8 reset_pin_number:4;
+	};
+	u8 regs[4];
+};
+
+struct f01_data {
+	struct f01_device_control device_control;
+	union f01_basic_queries basic_queries;
+	union f01_device_status device_status;
+	u8 product_id[RMI_PRODUCT_ID_LENGTH+1];
+
+	u16 interrupt_enable_addr;
+	u16 doze_interval_addr;
+	u16 wakeup_threshold_addr;
+	u16 doze_holdoff_addr;
+
+	int irq_count;
+	int num_of_irq_regs;
+
+#ifdef	CONFIG_PM
+	bool suspended;
+	bool old_nosleep;
+#endif
+};
+
+
+static ssize_t rmi_fn_01_productinfo_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf);
+
+static ssize_t rmi_fn_01_productid_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t rmi_fn_01_manufacturer_show(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf);
+
+static ssize_t rmi_fn_01_datecode_show(struct device *dev,
+				       struct device_attribute *attr,
+				       char *buf);
+
+static ssize_t rmi_fn_01_reportrate_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+
+static ssize_t rmi_fn_01_reportrate_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_interrupt_enable_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+
+static ssize_t rmi_fn_01_interrupt_enable_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_doze_interval_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+
+static ssize_t rmi_fn_01_doze_interval_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_wakeup_threshold_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+
+static ssize_t rmi_fn_01_wakeup_threshold_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_doze_holdoff_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+
+static ssize_t rmi_fn_01_doze_holdoff_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_reset_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_sleepmode_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t rmi_fn_01_sleepmode_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_nosleep_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static ssize_t rmi_fn_01_nosleep_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_chargerinput_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static ssize_t rmi_fn_01_chargerinput_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t count);
+
+static ssize_t rmi_fn_01_configured_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static ssize_t rmi_fn_01_unconfigured_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static ssize_t rmi_fn_01_flashprog_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static ssize_t rmi_fn_01_statuscode_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf);
+
+static int rmi_f01_alloc_memory(struct rmi_function_container *fc,
+					int num_of_irq_regs);
+
+static void rmi_f01_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f01_initialize(struct rmi_function_container *fc);
+
+static int rmi_f01_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f01_config(struct rmi_function_container *fc);
+
+static int rmi_f01_reset(struct rmi_function_container *fc);
+
+
+static struct device_attribute fn_01_attrs[] = {
+	__ATTR(productinfo, RMI_RO_ATTR,
+	       rmi_fn_01_productinfo_show, rmi_store_error),
+	__ATTR(productid, RMI_RO_ATTR,
+	       rmi_fn_01_productid_show, rmi_store_error),
+	__ATTR(manufacturer, RMI_RO_ATTR,
+	       rmi_fn_01_manufacturer_show, rmi_store_error),
+	__ATTR(datecode, RMI_RO_ATTR,
+	       rmi_fn_01_datecode_show, rmi_store_error),
+
+	/* control register access */
+	__ATTR(sleepmode, RMI_RW_ATTR,
+	       rmi_fn_01_sleepmode_show, rmi_fn_01_sleepmode_store),
+	__ATTR(nosleep, RMI_RW_ATTR,
+	       rmi_fn_01_nosleep_show, rmi_fn_01_nosleep_store),
+	__ATTR(chargerinput, RMI_RW_ATTR,
+	       rmi_fn_01_chargerinput_show, rmi_fn_01_chargerinput_store),
+	__ATTR(reportrate, RMI_RW_ATTR,
+	       rmi_fn_01_reportrate_show, rmi_fn_01_reportrate_store),
+	__ATTR(interrupt_enable, RMI_RW_ATTR,
+	       rmi_fn_01_interrupt_enable_show,
+		rmi_fn_01_interrupt_enable_store),
+	__ATTR(doze_interval, RMI_RW_ATTR,
+	       rmi_fn_01_doze_interval_show,
+		rmi_fn_01_doze_interval_store),
+	__ATTR(wakeup_threshold, RMI_RW_ATTR,
+	       rmi_fn_01_wakeup_threshold_show,
+		rmi_fn_01_wakeup_threshold_store),
+	__ATTR(doze_holdoff, RMI_RW_ATTR,
+	       rmi_fn_01_doze_holdoff_show,
+		rmi_fn_01_doze_holdoff_store),
+
+	/* We make report rate RO, since the driver uses that to look for
+	 * resets.  We don't want someone faking us out by changing that
+	 * bit.
+	 */
+	__ATTR(configured, RMI_RO_ATTR,
+	       rmi_fn_01_configured_show, rmi_store_error),
+
+	/* Command register access. */
+	__ATTR(reset, RMI_WO_ATTR,
+	       rmi_show_error, rmi_fn_01_reset_store),
+
+	/* STatus register access. */
+	__ATTR(unconfigured, RMI_RO_ATTR,
+	       rmi_fn_01_unconfigured_show, rmi_store_error),
+	__ATTR(flashprog, RMI_RO_ATTR,
+	       rmi_fn_01_flashprog_show, rmi_store_error),
+	__ATTR(statuscode, RMI_RO_ATTR,
+	       rmi_fn_01_statuscode_show, rmi_store_error),
+};
+
+/* Utility routine to set the value of a bit field in a register. */
+int rmi_set_bit_field(struct rmi_device *rmi_dev,
+		      unsigned short address,
+		      unsigned char field_mask,
+		      unsigned char bits)
+{
+	unsigned char reg_contents;
+	int retval;
+
+	retval = rmi_read(rmi_dev, address, &reg_contents);
+	if (retval)
+		return retval;
+	reg_contents = (reg_contents & ~field_mask) | bits;
+	retval = rmi_write(rmi_dev, address, reg_contents);
+	if (retval == 1)
+		return 0;
+	else if (retval == 0)
+		return -EIO;
+	return retval;
+}
+
+static ssize_t rmi_fn_01_productinfo_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
+			data->basic_queries.productinfo_1,
+			data->basic_queries.productinfo_2);
+}
+
+static ssize_t rmi_fn_01_productid_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", data->product_id);
+}
+
+static ssize_t rmi_fn_01_manufacturer_show(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "0x%02x\n",
+			data->basic_queries.manufacturer_id);
+}
+
+static ssize_t rmi_fn_01_datecode_show(struct device *dev,
+				       struct device_attribute *attr,
+				       char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "20%02u-%02u-%02u\n",
+			data->basic_queries.year,
+			data->basic_queries.month,
+			data->basic_queries.day);
+}
+
+static ssize_t rmi_fn_01_reset_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct rmi_function_container *fc = NULL;
+	unsigned int reset;
+	int retval = 0;
+	/* Command register always reads as 0, so we can just use a local. */
+	union f01_device_commands commands = {};
+
+	fc = to_rmi_function_container(dev);
+
+	if (sscanf(buf, "%u", &reset) != 1)
+		return -EINVAL;
+	if (reset < 0 || reset > 1)
+		return -EINVAL;
+
+	/* Per spec, 0 has no effect, so we skip it entirely. */
+	if (reset) {
+		commands.reset = 1;
+		retval = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
+				&commands.reg, sizeof(commands.reg));
+		if (retval < 0) {
+			dev_err(dev, "%s: failed to issue reset command, "
+				"error = %d.", __func__, retval);
+			return retval;
+		}
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_01_sleepmode_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE,
+			"%d\n", data->device_control.ctrl0.sleep_mode);
+}
+
+static ssize_t rmi_fn_01_sleepmode_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf,
+					 size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || !RMI_IS_VALID_SLEEPMODE(new_value)) {
+		dev_err(dev, "%s: Invalid sleep mode %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "Setting sleep mode to %ld.", new_value);
+	data->device_control.ctrl0.sleep_mode = new_value;
+	retval = rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write sleep mode, code %d.\n", retval);
+	return retval;
+}
+
+static ssize_t rmi_fn_01_nosleep_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+		data->device_control.ctrl0.nosleep);
+}
+
+static ssize_t rmi_fn_01_nosleep_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf,
+				       size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid nosleep bit %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.ctrl0.nosleep = new_value;
+	retval = rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write nosleep bit.\n");
+	return retval;
+}
+
+static ssize_t rmi_fn_01_chargerinput_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.ctrl0.charger_input);
+}
+
+static ssize_t rmi_fn_01_chargerinput_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf,
+				       size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid chargerinput bit %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.ctrl0.charger_input = new_value;
+	retval = rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write chargerinput bit.\n");
+	return retval;
+}
+
+static ssize_t rmi_fn_01_reportrate_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.ctrl0.report_rate);
+}
+
+static ssize_t rmi_fn_01_reportrate_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf,
+				       size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid reportrate bit %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.ctrl0.report_rate = new_value;
+	retval = rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write reportrate bit.\n");
+	return retval;
+}
+
+static ssize_t rmi_fn_01_interrupt_enable_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct rmi_function_container *fc;
+	struct f01_data *data;
+	int i, len, total_len = 0;
+	char *current_buf = buf;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	/* loop through each irq value and copy its
+	 * string representation into buf */
+	for (i = 0; i < data->irq_count; i++) {
+		int irq_reg;
+		int irq_shift;
+		int interrupt_enable;
+
+		irq_reg = i / 8;
+		irq_shift = i % 8;
+		interrupt_enable =
+		    ((data->device_control.interrupt_enable[irq_reg]
+			>> irq_shift) & 0x01);
+
+		/* get next irq value and write it to buf */
+		len = snprintf(current_buf, PAGE_SIZE - total_len,
+			"%u ", interrupt_enable);
+		/* bump up ptr to next location in buf if the
+		 * snprintf was valid.  Otherwise issue an error
+		 * and return. */
+		if (len > 0) {
+			current_buf += len;
+			total_len += len;
+		} else {
+			dev_err(dev, "%s: Failed to build interrupt_enable"
+				" buffer, code = %d.\n", __func__, len);
+			return snprintf(buf, PAGE_SIZE, "unknown\n");
+		}
+	}
+	len = snprintf(current_buf, PAGE_SIZE - total_len, "\n");
+	if (len > 0)
+		total_len += len;
+	else
+		dev_warn(dev, "%s: Failed to append carriage return.\n",
+			 __func__);
+	return total_len;
+
+}
+
+static ssize_t rmi_fn_01_interrupt_enable_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct f01_data *data;
+	int i;
+	int irq_count = 0;
+	int retval = count;
+	int irq_reg = 0;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	for (i = 0; i < data->irq_count && *buf != 0;
+	     i++, buf += 2) {
+		int irq_shift;
+		int interrupt_enable;
+		int result;
+
+		irq_reg = i / 8;
+		irq_shift = i % 8;
+
+		/* get next interrupt mapping value and store and bump up to
+		 * point to next item in buf */
+		result = sscanf(buf, "%u", &interrupt_enable);
+		if ((result != 1) ||
+			(interrupt_enable != 0 && interrupt_enable != 1)) {
+			dev_err(dev,
+				"%s: Error - interrupt enable[%d]"
+				" is not a valid value 0x%x.\n",
+				__func__, i, interrupt_enable);
+			return -EINVAL;
+		}
+		if (interrupt_enable == 0) {
+			data->device_control.interrupt_enable[irq_reg] &=
+				(1 << irq_shift) ^ 0xFF;
+		} else
+			data->device_control.interrupt_enable[irq_reg] |=
+				(1 << irq_shift);
+		irq_count++;
+	}
+
+	/* Make sure the irq count matches */
+	if (irq_count != data->irq_count) {
+		dev_err(dev,
+			"%s: Error - interrupt enable count of %d"
+			" doesn't match device count of %d.\n",
+			 __func__, irq_count, data->irq_count);
+		return -EINVAL;
+	}
+
+	/* write back to the control register */
+	retval = rmi_write_block(fc->rmi_dev, data->interrupt_enable_addr,
+			data->device_control.interrupt_enable,
+			sizeof(u8)*(data->num_of_irq_regs));
+	if (retval < 0) {
+		dev_err(dev, "%s : Could not write interrupt_enable_store"
+			" to 0x%x\n", __func__, data->interrupt_enable_addr);
+		return retval;
+	}
+
+	return count;
+
+}
+
+static ssize_t rmi_fn_01_doze_interval_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.doze_interval);
+
+}
+
+static ssize_t rmi_fn_01_doze_interval_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+	u16 ctrl_base_addr;
+
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 255) {
+		dev_err(dev, "%s: Invalid doze interval %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.doze_interval = new_value;
+	ctrl_base_addr = fc->fd.control_base_addr + sizeof(u8) +
+			(sizeof(u8)*(data->num_of_irq_regs));
+	dev_dbg(dev, "doze_interval store address %x, value %d",
+		ctrl_base_addr, data->device_control.doze_interval);
+
+	retval = rmi_write_block(fc->rmi_dev, data->doze_interval_addr,
+			&data->device_control.doze_interval,
+			sizeof(u8));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write doze interval.\n");
+	return retval;
+
+}
+
+static ssize_t rmi_fn_01_wakeup_threshold_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.wakeup_threshold);
+}
+
+static ssize_t rmi_fn_01_wakeup_threshold_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 255) {
+		dev_err(dev, "%s: Invalid wakeup threshold %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.doze_interval = new_value;
+	retval = rmi_write_block(fc->rmi_dev, data->wakeup_threshold_addr,
+			&data->device_control.wakeup_threshold,
+			sizeof(u8));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write wakeup threshold.\n");
+	return retval;
+
+}
+
+static ssize_t rmi_fn_01_doze_holdoff_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.doze_holdoff);
+
+}
+
+
+static ssize_t rmi_fn_01_doze_holdoff_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count)
+{
+	struct f01_data *data = NULL;
+	unsigned long new_value;
+	int retval;
+
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	retval = strict_strtoul(buf, 10, &new_value);
+	if (retval < 0 || new_value < 0 || new_value > 255) {
+		dev_err(dev, "%s: Invalid doze holdoff %s.", __func__, buf);
+		return -EINVAL;
+	}
+
+	data->device_control.doze_interval = new_value;
+	retval = rmi_write_block(fc->rmi_dev, data->doze_holdoff_addr,
+			&data->device_control.doze_holdoff,
+			sizeof(u8));
+	if (retval >= 0)
+		retval = count;
+	else
+		dev_err(dev, "Failed to write doze holdoff.\n");
+	return retval;
+
+}
+
+static ssize_t rmi_fn_01_configured_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_control.ctrl0.configured);
+}
+
+static ssize_t rmi_fn_01_unconfigured_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_status.unconfigured);
+}
+
+static ssize_t rmi_fn_01_flashprog_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n",
+			data->device_status.flash_prog);
+}
+
+static ssize_t rmi_fn_01_statuscode_show(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	struct f01_data *data = NULL;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "0x%02x\n",
+			data->device_status.status_code);
+}
+
+/* why is this not done in init? */
+int rmi_driver_f01_init(struct rmi_device *rmi_dev)
+{
+	struct rmi_driver_data *driver_data = rmi_get_driverdata(rmi_dev);
+	struct rmi_function_container *fc = driver_data->f01_container;
+	int error;
+
+	error = rmi_f01_alloc_memory(fc, driver_data->num_of_irq_regs);
+	if (error < 0)
+		goto error_exit;
+
+	error = rmi_f01_initialize(fc);
+	if (error < 0)
+		goto error_exit;
+
+	error = rmi_f01_create_sysfs(fc);
+	if (error < 0)
+		goto error_exit;
+
+	return 0;
+
+ error_exit:
+	rmi_f01_free_memory(fc);
+
+	return error;
+}
+
+static int rmi_f01_alloc_memory(struct rmi_function_container *fc,
+	int num_of_irq_regs)
+{
+	struct f01_data *f01;
+
+	f01 = kzalloc(sizeof(struct f01_data), GFP_KERNEL);
+	if (!f01) {
+		dev_err(&fc->dev, "Failed to allocate fn_01_data.\n");
+		return -ENOMEM;
+	}
+
+	f01->device_control.interrupt_enable =
+		kzalloc(sizeof(u8)*(num_of_irq_regs), GFP_KERNEL);
+	if (!f01->device_control.interrupt_enable) {
+		kfree(f01);
+		return -ENOMEM;
+	}
+	fc->data = f01;
+
+	return 0;
+}
+
+static void rmi_f01_free_memory(struct rmi_function_container *fc)
+{
+	struct f01_data *f01 = fc->data;
+	kfree(f01->device_control.interrupt_enable);
+	kfree(fc->data);
+	fc->data = NULL;
+}
+
+static int rmi_f01_initialize(struct rmi_function_container *fc)
+{
+	u8 temp;
+	int retval;
+	u16 ctrl_base_addr;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_driver_data *driver_data = rmi_get_driverdata(rmi_dev);
+	struct f01_data *data = fc->data;
+	struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
+
+	/* Set the configured bit and (optionally) other important stuff
+	 * in the device control register. */
+	ctrl_base_addr = fc->fd.control_base_addr;
+	retval = rmi_read_block(rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read F01 control.\n");
+		return retval;
+	}
+	switch (pdata->power_management.nosleep) {
+	case RMI_F01_NOSLEEP_DEFAULT:
+		break;
+	case RMI_F01_NOSLEEP_OFF:
+		data->device_control.ctrl0.nosleep = 0;
+		break;
+	case RMI_F01_NOSLEEP_ON:
+		data->device_control.ctrl0.nosleep = 1;
+		break;
+	}
+	/* Sleep mode might be set as a hangover from a system crash or
+	 * reboot without power cycle.  If so, clear it so the sensor
+	 * is certain to function.
+	 */
+	if (data->device_control.ctrl0.sleep_mode != RMI_SLEEP_MODE_NORMAL) {
+		dev_warn(&fc->dev,
+			 "WARNING: Non-zero sleep mode found. Clearing...\n");
+		data->device_control.ctrl0.sleep_mode = RMI_SLEEP_MODE_NORMAL;
+	}
+
+	data->device_control.ctrl0.configured = 1;
+	retval = rmi_write_block(rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to write F01 control.\n");
+		return retval;
+	}
+
+	data->irq_count = driver_data->irq_count;
+	data->num_of_irq_regs = driver_data->num_of_irq_regs;
+	ctrl_base_addr += sizeof(union f01_device_control_0);
+
+	data->interrupt_enable_addr = ctrl_base_addr;
+	retval = rmi_read_block(rmi_dev, ctrl_base_addr,
+			data->device_control.interrupt_enable,
+			sizeof(u8) *(driver_data->num_of_irq_regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read F01 control interrupt enable register.\n");
+		goto error_exit;
+	}
+	ctrl_base_addr += driver_data->num_of_irq_regs;
+
+	/* dummy read in order to clear irqs */
+	retval = rmi_read(rmi_dev, fc->fd.data_base_addr + 1, &temp);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read Interrupt Status.\n");
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, fc->fd.query_base_addr,
+				data->basic_queries.regs,
+				sizeof(data->basic_queries.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read device query registers.\n");
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev,
+		fc->fd.query_base_addr + sizeof(data->basic_queries.regs),
+		data->product_id, RMI_PRODUCT_ID_LENGTH);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read product ID.\n");
+		return retval;
+	}
+	data->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
+	dev_info(&fc->dev, "found RMI device, manufacturer: %s, product: %s\n",
+		 data->basic_queries.manufacturer_id == 1 ?
+							"synaptics" : "unknown",
+		 data->product_id);
+
+	/* read control register */
+	if (data->basic_queries.has_adjustable_doze) {
+		data->doze_interval_addr = ctrl_base_addr;
+		ctrl_base_addr++;
+
+		if (pdata->power_management.doze_interval) {
+			data->device_control.doze_interval =
+				pdata->power_management.doze_interval;
+			retval = rmi_write(rmi_dev, data->doze_interval_addr,
+					data->device_control.doze_interval);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to configure F01 doze interval register.\n");
+				goto error_exit;
+			}
+		} else {
+			retval = rmi_read(rmi_dev, data->doze_interval_addr,
+					&data->device_control.doze_interval);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to read F01 doze interval register.\n");
+				goto error_exit;
+			}
+		}
+
+		data->wakeup_threshold_addr = ctrl_base_addr;
+		ctrl_base_addr++;
+
+		if (pdata->power_management.wakeup_threshold) {
+			data->device_control.wakeup_threshold =
+				pdata->power_management.wakeup_threshold;
+			retval = rmi_write(rmi_dev, data->wakeup_threshold_addr,
+					data->device_control.wakeup_threshold);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to configure F01 wakeup threshold register.\n");
+				goto error_exit;
+			}
+		} else {
+			retval = rmi_read(rmi_dev, data->wakeup_threshold_addr,
+					&data->device_control.wakeup_threshold);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to read F01 wakeup threshold register.\n");
+				goto error_exit;
+			}
+		}
+	}
+
+	if (data->basic_queries.has_adjustable_doze_holdoff) {
+		data->doze_holdoff_addr = ctrl_base_addr;
+		ctrl_base_addr++;
+
+		if (pdata->power_management.doze_holdoff) {
+			data->device_control.doze_holdoff =
+				pdata->power_management.doze_holdoff;
+			retval = rmi_write(rmi_dev, data->doze_holdoff_addr,
+					data->device_control.doze_holdoff);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to configure F01 "
+					"doze holdoff register.\n");
+				goto error_exit;
+			}
+		} else {
+			retval = rmi_read(rmi_dev, data->doze_holdoff_addr,
+					&data->device_control.doze_holdoff);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to read F01 doze"
+					" holdoff register.\n");
+				goto error_exit;
+			}
+		}
+	}
+
+	retval = rmi_read_block(rmi_dev, fc->fd.data_base_addr,
+		data->device_status.regs, ARRAY_SIZE(data->device_status.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read device status.\n");
+		goto error_exit;
+	}
+
+	if (data->device_status.unconfigured) {
+		dev_err(&fc->dev,
+			"Device reset during configuration process, status: "
+			"%#02x!\n", data->device_status.status_code);
+		retval = -EINVAL;
+		goto error_exit;
+	}
+
+	return retval;
+
+ error_exit:
+	kfree(data);
+	return retval;
+}
+
+static int rmi_f01_create_sysfs(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	int retval = 0;
+	struct f01_data *data = fc->data;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+	for (attr_count = 0; attr_count < ARRAY_SIZE(fn_01_attrs);
+			attr_count++) {
+		if (!strcmp("doze_interval", fn_01_attrs[attr_count].attr.name)
+			&& !data->basic_queries.has_lts) {
+			continue;
+		}
+		if (!strcmp("wakeup_threshold",
+			fn_01_attrs[attr_count].attr.name)
+			&& !data->basic_queries.has_adjustable_doze) {
+			continue;
+		}
+		if (!strcmp("doze_holdoff", fn_01_attrs[attr_count].attr.name)
+			&& !data->basic_queries.has_adjustable_doze_holdoff) {
+			continue;
+		}
+		retval = sysfs_create_file(&fc->dev.kobj,
+				      &fn_01_attrs[attr_count].attr);
+		if (retval < 0) {
+			dev_err(&fc->dev, "Failed to create sysfs file for %s.",
+			       fn_01_attrs[attr_count].attr.name);
+			goto err_remove_sysfs;
+		}
+	}
+
+	return 0;
+
+err_remove_sysfs:
+	for (attr_count--; attr_count >= 0; attr_count--)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &fn_01_attrs[attr_count].attr);
+
+	return retval;
+}
+
+static int rmi_f01_config(struct rmi_function_container *fc)
+{
+	struct f01_data *data = fc->data;
+	int retval;
+
+	retval = rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to write device_control.reg.\n");
+		return retval;
+	}
+
+	retval = rmi_write_block(fc->rmi_dev, data->interrupt_enable_addr,
+			data->device_control.interrupt_enable,
+			sizeof(u8)*(data->num_of_irq_regs));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to write interrupt enable.\n");
+		return retval;
+	}
+	if (data->basic_queries.has_lts) {
+		retval = rmi_write_block(fc->rmi_dev, data->doze_interval_addr,
+				&data->device_control.doze_interval,
+				sizeof(u8));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Failed to write doze interval.\n");
+			return retval;
+		}
+	}
+
+	if (data->basic_queries.has_adjustable_doze) {
+		retval = rmi_write_block(
+				fc->rmi_dev, data->wakeup_threshold_addr,
+				&data->device_control.wakeup_threshold,
+				sizeof(u8));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Failed to write wakeup threshold.\n");
+			return retval;
+		}
+	}
+
+	if (data->basic_queries.has_adjustable_doze_holdoff) {
+		retval = rmi_write_block(fc->rmi_dev, data->doze_holdoff_addr,
+				&data->device_control.doze_holdoff,
+				sizeof(u8));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Failed to write doze holdoff.\n");
+			return retval;
+		}
+	}
+	return 0;
+}
+
+static int rmi_f01_reset(struct rmi_function_container *fc)
+{
+	/*do nothing here */
+	return 0;
+}
+
+
+#ifdef CONFIG_PM
+static int rmi_f01_suspend(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_driver_data *driver_data = rmi_get_driverdata(rmi_dev);
+	struct f01_data *data = driver_data->f01_container->data;
+	int retval = 0;
+
+	if (data->suspended)
+		return 0;
+
+	data->old_nosleep = data->device_control.ctrl0.nosleep;
+	data->device_control.ctrl0.nosleep = 0;
+	data->device_control.ctrl0.sleep_mode = RMI_SLEEP_MODE_SENSOR_SLEEP;
+
+	retval = rmi_write_block(rmi_dev,
+			driver_data->f01_container->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to write sleep mode. Code: %d.\n",
+			retval);
+		data->device_control.ctrl0.nosleep = data->old_nosleep;
+		data->device_control.ctrl0.sleep_mode = RMI_SLEEP_MODE_NORMAL;
+	} else {
+		data->suspended = true;
+		retval = 0;
+	}
+
+	return retval;
+}
+
+static int rmi_f01_resume(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_driver_data *driver_data = rmi_get_driverdata(rmi_dev);
+	struct f01_data *data = driver_data->f01_container->data;
+	int retval = 0;
+
+	if (!data->suspended)
+		return 0;
+
+	data->device_control.ctrl0.nosleep = data->old_nosleep;
+	data->device_control.ctrl0.sleep_mode = RMI_SLEEP_MODE_NORMAL;
+
+	retval = rmi_write_block(rmi_dev,
+			driver_data->f01_container->fd.control_base_addr,
+			data->device_control.ctrl0.regs,
+			ARRAY_SIZE(data->device_control.ctrl0.regs));
+	if (retval < 0)
+		dev_err(&fc->dev,
+			"Failed to restore normal operation. Code: %d.\n",
+			retval);
+	else {
+		data->suspended = false;
+		retval = 0;
+	}
+
+	return retval;
+}
+#endif /* CONFIG_PM */
+
+static int rmi_f01_init(struct rmi_function_container *fc)
+{
+	return 0;
+}
+
+static void rmi_f01_remove(struct rmi_function_container *fc)
+{
+	int attr_count;
+
+	for (attr_count = 0; attr_count < ARRAY_SIZE(fn_01_attrs);
+			attr_count++) {
+		sysfs_remove_file(&fc->dev.kobj, &fn_01_attrs[attr_count].attr);
+	}
+
+	rmi_f01_free_memory(fc);
+}
+
+static int rmi_f01_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f01_data *data = fc->data;
+	int retval;
+
+	retval = rmi_read_block(rmi_dev, fc->fd.data_base_addr,
+		data->device_status.regs, ARRAY_SIZE(data->device_status.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read device status, code: %d.\n",
+			retval);
+		return retval;
+	}
+	if (data->device_status.unconfigured) {
+		dev_warn(&fc->dev, "Device reset detected.\n");
+		retval = rmi_dev->driver->reset_handler(rmi_dev);
+		if (retval < 0)
+			return retval;
+	}
+	return 0;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x01,
+	.init = rmi_f01_init,
+	.config = rmi_f01_config,
+	.reset = rmi_f01_reset,
+	.attention = rmi_f01_attention,
+
+#ifdef	CONFIG_PM
+#if defined(CONFIG_HAS_EARLYSUSPEND) && \
+			!defined(CONFIG_RMI4_SPECIAL_EARLYSUSPEND)
+	.early_suspend = rmi_f01_suspend,
+	.late_resume = rmi_f01_resume,
+#else
+	.suspend = rmi_f01_suspend,
+	.resume = rmi_f01_resume,
+#endif  /* defined(CONFIG_HAS_EARLYSUSPEND) && !def... */
+#endif  /* CONFIG_PM */
+	.remove = rmi_f01_remove,
+};
+
+static int __init rmi_f01_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void __exit rmi_f01_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+module_init(rmi_f01_module_init);
+module_exit(rmi_f01_module_exit);
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]>");
+MODULE_DESCRIPTION("RMI F01 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
drivers/input/rmi4/rmi_f01.h new file +88 -0

88 lines not on this page · raw message

#28 lore raw

RFC PATCH 11/17 input: RMI4 F19 capacitive buttons

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f19.c |  869 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 869 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f19.c new file +869 -0
new file mode 100644
index 0000000..9e9ea93
@@ -0,0 +1,869 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#define FUNCTION_DATA f19_data
+#define FNUM 19
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+#define QUERY_BASE_INDEX 1
+#define MAX_LEN 256
+#define MAX_BUFFER_LEN 80
+
+#define SENSOR_MAP_MIN			0
+#define SENSOR_MAP_MAX			127
+#define SENSITIVITY_ADJ_MIN		0
+#define SENSITIVITY_ADJ_MAX		31
+#define HYSTERESIS_THRESHOLD_MIN	0
+#define HYSTERESIS_THRESHOLD_MAX	15
+
+union f19_0d_query {
+	struct {
+		u8 configurable:1;
+		u8 has_sensitivity_adjust:1;
+		u8 has_hysteresis_threshold:1;
+		u8 reserved_1:5;
+
+		u8 button_count:5;
+		u8 reserved_2:3;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f19_0d_control_0 {
+	struct {
+		u8 button_usage:2;
+		u8 filter_mode:2;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+/* rewrite control regs */
+struct f19_0d_control_1n {
+	u8 int_enabled_button;
+};
+
+struct f19_0d_control_1 {
+	struct f19_0d_control_1n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f19_0d_control_2n {
+	u8 single_button;
+};
+
+struct f19_0d_control_2 {
+	struct f19_0d_control_2n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f19_0d_control_3n {
+	u8 sensor_map_button:7;
+};
+
+struct f19_0d_control_3 {
+	struct f19_0d_control_3n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f19_0d_control_4n {
+	u8 sensitivity_button:7;
+};
+
+struct f19_0d_control_4 {
+	struct f19_0d_control_4n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+union f19_0d_control_5 {
+	struct {
+		u8 sensitivity_adj:5;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f19_0d_control_6 {
+	struct {
+		u8 hysteresis_threshold:4;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f19_0d_control {
+	union f19_0d_control_0 *reg_0;
+	struct f19_0d_control_1 *reg_1;
+	struct f19_0d_control_2 *reg_2;
+	struct f19_0d_control_3 *reg_3;
+	struct f19_0d_control_4 *reg_4;
+	union f19_0d_control_5 *reg_5;
+	union f19_0d_control_6 *reg_6;
+};
+
+/* data specific to fn $19 that needs to be kept around */
+struct f19_data {
+	struct f19_0d_control control;
+	union f19_0d_query query;
+	u8 button_rezero;
+	unsigned char button_count;
+	unsigned char button_bitmask_size;
+	unsigned char *button_data_buffer;
+	unsigned short *button_map;
+	char input_name[MAX_LEN];
+	char input_phys[MAX_LEN];
+	struct input_dev *input;
+	struct mutex control_mutex;
+	struct mutex data_mutex;
+};
+
+
+static int rmi_f19_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f19_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f19_initialize(struct rmi_function_container *fc);
+
+static int rmi_f19_register_device(struct rmi_function_container *fc);
+
+static int rmi_f19_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f19_config(struct rmi_function_container *fc);
+
+static int rmi_f19_reset(struct rmi_function_container *fc);
+
+/* Query sysfs files */
+show_union_struct_prototype(configurable)
+show_union_struct_prototype(has_sensitivity_adjust)
+show_union_struct_prototype(has_hysteresis_threshold)
+show_union_struct_prototype(button_count)
+
+static struct attribute *attrs[] = {
+	attrify(configurable),
+	attrify(has_sensitivity_adjust),
+	attrify(has_hysteresis_threshold),
+	attrify(button_count),
+	NULL
+};
+
+static struct attribute_group attrs_query = GROUP(attrs);
+/* Control sysfs files */
+show_store_union_struct_prototype(button_usage)
+show_store_union_struct_prototype(filter_mode)
+show_store_union_struct_prototype(int_enabled_button)
+show_store_union_struct_prototype(single_button)
+show_store_union_struct_prototype(sensitivity_button)
+show_store_union_struct_prototype(sensitivity_adj)
+show_store_union_struct_prototype(hysteresis_threshold)
+
+
+static struct attribute *attrsCtrl[] = {
+	attrify(button_usage),
+	attrify(filter_mode),
+	attrify(int_enabled_button),
+	attrify(single_button),
+	attrify(sensitivity_button),
+	NULL
+};
+static struct attribute_group attrs_control = GROUP(attrsCtrl);
+
+static ssize_t rmi_fn_19_sensor_map_button_show(struct device *dev,
+				     struct device_attribute *attr, char *buf);
+static ssize_t rmi_fn_19_sensor_map_button_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+static struct device_attribute sensor_map_attr = __ATTR(sensor_map_button,
+			RMI_RW_ATTR, rmi_fn_19_sensor_map_button_show,
+			rmi_fn_19_sensor_map_button_store);
+
+static struct device_attribute sensor_map_ro_attr =  __ATTR(sensor_map_button,
+			RMI_RO_ATTR, rmi_fn_19_sensor_map_button_show,
+			 rmi_store_error);
+
+/* Command sysfs files */
+static ssize_t f19_rezero_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count);
+static DEVICE_ATTR(rezero, RMI_WO_ATTR,
+		rmi_show_error,
+		f19_rezero_store);
+
+static struct attribute *attrsCommand[] = {
+	attrify(rezero),
+	NULL
+};
+static struct attribute_group attrs_command = GROUP(attrsCommand);
+
+int rmi_f19_read_control_parameters(struct rmi_device *rmi_dev,
+	struct f19_data *f19)
+{
+	int retval = 0;
+	union f19_0d_query *query = &f19->query;
+	struct f19_0d_control *control = &f19->control;
+
+
+	retval = rmi_read_block(rmi_dev, control->reg_0->address,
+			(u8 *)control->reg_0->regs,
+			sizeof(control->reg_0->regs));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Could not read control reg0 to %#06x.\n",
+				control->reg_0->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_1->address,
+			(u8 *)control->reg_1->regs, f19->button_bitmask_size);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Could not read control reg1 to %#06x.\n",
+			 control->reg_1->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_2->address,
+			(u8 *)control->reg_2->regs, f19->button_bitmask_size);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Could not read control reg2 to %#06x.\n",
+			 control->reg_2->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_3->address,
+			(u8 *)control->reg_3->regs, f19->button_count);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Could not read control reg3 to %#06x.\n",
+			 control->reg_3->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_4->address,
+			(u8 *)control->reg_4->regs, f19->button_count);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Could not read control reg3 to %#06x.\n",
+			 control->reg_4->address);
+		return retval;
+	}
+
+	if (query->has_sensitivity_adjust) {
+		retval = rmi_read_block(rmi_dev, control->reg_5->address,
+				(u8 *)control->reg_5->regs,
+				sizeof(control->reg_5->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg5 to %#06x.\n",
+				control->reg_5->address);
+			return retval;
+		}
+	}
+	if (query->has_hysteresis_threshold) {
+		retval = rmi_read_block(rmi_dev, control->reg_6->address,
+				(u8 *)control->reg_6->regs,
+				sizeof(control->reg_6->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg6 to %#06x.\n",
+				control->reg_6->address);
+			return retval;
+		}
+	}
+	return 0;
+}
+static int rmi_f19_init(struct rmi_function_container *fc)
+{
+	int rc = rmi_f19_alloc_memory(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f19_initialize(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f19_register_device(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f19_create_sysfs(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	return 0;
+
+err_free_data:
+	rmi_f19_free_memory(fc);
+	return rc;
+}
+
+
+static int rmi_f19_alloc_memory(struct rmi_function_container *fc)
+{
+	struct f19_data *f19;
+	int rc;
+	u16 ctrl_base_addr;
+
+	/* allow memory for fn19 data */
+	f19 = kzalloc(sizeof(struct f19_data), GFP_KERNEL);
+	if (!f19) {
+		dev_err(&fc->dev, "Failed to allocate function data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f19;
+	rc = rmi_read_block(fc->rmi_dev, fc->fd.query_base_addr,
+				(u8 *)&f19->query,
+				sizeof(f19->query.regs));
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to read query register.\n");
+		return rc;
+	}
+	f19->query.address = fc->fd.query_base_addr;
+
+	f19->button_bitmask_size = sizeof(u8)*(f19->query.button_count + 7) / 8;
+	f19->button_data_buffer = kcalloc(f19->button_bitmask_size,
+		    sizeof(unsigned char), GFP_KERNEL);
+	if (!f19->button_data_buffer) {
+		dev_err(&fc->dev, "Failed to allocate button data buffer.\n");
+		return -ENOMEM;
+	}
+
+	f19->button_map = kcalloc(f19->query.button_count,
+				sizeof(unsigned short), GFP_KERNEL);
+	if (!f19->button_map) {
+		dev_err(&fc->dev, "Failed to allocate button map.\n");
+		return -ENOMEM;
+	}
+
+	/* allocate memory for control reg */
+	/* reg 0 */
+	ctrl_base_addr = fc->fd.control_base_addr;
+	f19->control.reg_0 =
+			kzalloc(sizeof(f19->control.reg_0->regs), GFP_KERNEL);
+	if (!f19->control.reg_0) {
+		dev_err(&fc->dev, "Failed to allocate reg_0 control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_0->address = ctrl_base_addr;
+	ctrl_base_addr += sizeof(f19->control.reg_0->regs);
+
+	/* reg 1 */
+	f19->control.reg_1 =
+			kzalloc(sizeof(struct f19_0d_control_1), GFP_KERNEL);
+	if (!f19->control.reg_1) {
+		dev_err(&fc->dev, "Failed to allocate reg_1 control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_1->regs =
+			kzalloc(f19->button_bitmask_size, GFP_KERNEL);
+	if (!f19->control.reg_1->regs) {
+		dev_err(&fc->dev, "Failed to allocate reg_1->regs control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_1->address = ctrl_base_addr;
+	f19->control.reg_1->length = f19->button_bitmask_size;
+	ctrl_base_addr += f19->button_bitmask_size;
+
+	/* reg 2 */
+	f19->control.reg_2 =
+			kzalloc(sizeof(struct f19_0d_control_2), GFP_KERNEL);
+	if (!f19->control.reg_2) {
+		dev_err(&fc->dev, "Failed to allocate reg_2 control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_2->regs =
+			kzalloc(f19->button_bitmask_size, GFP_KERNEL);
+	if (!f19->control.reg_2->regs) {
+		dev_err(&fc->dev, "Failed to allocate reg_2->regs control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_2->address = ctrl_base_addr;
+	f19->control.reg_2->length = f19->button_bitmask_size;
+	ctrl_base_addr += f19->button_bitmask_size;
+
+	/* reg 3 */
+	f19->control.reg_3 =
+			kzalloc(sizeof(struct f19_0d_control_3), GFP_KERNEL);
+	if (!f19->control.reg_3) {
+		dev_err(&fc->dev, "Failed to allocate reg_3 control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_3->regs =
+			kzalloc(f19->query.button_count, GFP_KERNEL);
+	if (!f19->control.reg_3->regs) {
+		dev_err(&fc->dev, "Failed to allocate reg_3->regs control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_3->address = ctrl_base_addr;
+	f19->control.reg_3->length = f19->query.button_count;
+	ctrl_base_addr += f19->query.button_count;
+
+	/* reg 4 */
+	f19->control.reg_4 =
+			kzalloc(sizeof(struct f19_0d_control_4), GFP_KERNEL);
+	if (!f19->control.reg_4) {
+		dev_err(&fc->dev, "Failed to allocate reg_3 control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_4->regs =
+			kzalloc(f19->query.button_count, GFP_KERNEL);
+	if (!f19->control.reg_4->regs) {
+		dev_err(&fc->dev, "Failed to allocate reg_3->regs control registers.");
+		return -ENOMEM;
+	}
+	f19->control.reg_4->address = ctrl_base_addr;
+	f19->control.reg_4->length = f19->query.button_count;
+	ctrl_base_addr += f19->query.button_count;
+
+	/* reg 5 */
+	if (f19->query.has_sensitivity_adjust) {
+		f19->control.reg_5 =
+			kzalloc(sizeof(f19->control.reg_5->regs), GFP_KERNEL);
+		if (!f19->control.reg_5) {
+			dev_err(&fc->dev, "Failed to allocate reg_5 control registers.");
+			return -ENOMEM;
+		}
+		f19->control.reg_5->address = ctrl_base_addr;
+		ctrl_base_addr += sizeof(f19->control.reg_5->regs);
+	}
+	/* reg 6 */
+	if (f19->query.has_hysteresis_threshold) {
+		f19->control.reg_6 =
+			kzalloc(sizeof(f19->control.reg_6->regs), GFP_KERNEL);
+		if (!f19->control.reg_6) {
+			dev_err(&fc->dev, "Failed to allocate reg_6 control registers.");
+			return -ENOMEM;
+		}
+		f19->control.reg_6->address = ctrl_base_addr;
+	}
+	return 0;
+}
+
+
+
+static void rmi_f19_free_memory(struct rmi_function_container *fc)
+{
+	union f19_0d_query *query;
+	struct f19_data *f19 = fc->data;
+
+	query = &f19->query;
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+	sysfs_remove_group(&fc->dev.kobj, &attrs_control);
+	if (query->has_sensitivity_adjust)
+		sysfs_remove_file(&fc->dev.kobj, attrify(sensitivity_adj));
+
+	if (query->has_hysteresis_threshold)
+		sysfs_remove_file(&fc->dev.kobj,
+				attrify(hysteresis_threshold));
+	sysfs_remove_group(&fc->dev.kobj, &attrs_command);
+	if (query->configurable)
+		sysfs_remove_file(&fc->dev.kobj, &sensor_map_attr.attr);
+	else
+		sysfs_remove_file(&fc->dev.kobj, &sensor_map_ro_attr.attr);
+
+	if (f19) {
+		kfree(f19->button_data_buffer);
+		kfree(f19->button_map);
+		kfree(f19->control.reg_0);
+		kfree(f19->control.reg_1);
+		kfree(f19->control.reg_1->regs);
+		kfree(f19->control.reg_2);
+		kfree(f19->control.reg_2->regs);
+		kfree(f19->control.reg_3);
+		kfree(f19->control.reg_3->regs);
+		kfree(f19->control.reg_5);
+		kfree(f19->control.reg_6);
+		kfree(f19);
+		fc->data = NULL;
+	}
+}
+
+static int rmi_f19_initialize(struct rmi_function_container *fc)
+{
+	int i;
+	int rc;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_device_platform_data *pdata;
+	struct f19_data *f19 = fc->data;
+
+	/* initial all default values for f19 data here */
+	rc = rmi_read(rmi_dev, fc->fd.command_base_addr,
+		(u8 *)&f19->button_rezero);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to read command register.\n");
+		return rc;
+	}
+	f19->button_rezero = f19->button_rezero & 1;
+
+	pdata = to_rmi_platform_data(rmi_dev);
+	if (pdata) {
+		if (!pdata->f19_button_map)
+			dev_warn(&fc->dev, "F19 button_map is NULL");
+		else if (!pdata->f19_button_map->map)
+			dev_warn(&fc->dev,
+				 "Platformdata button map is missing!\n");
+		else {
+			if (pdata->f19_button_map->nbuttons !=
+						f19->query.button_count)
+				dev_warn(&fc->dev, "Platformdata button map size (%d) != number of buttons on device (%d) - ignored.\n",
+					pdata->f19_button_map->nbuttons,
+					f19->query.button_count);
+			f19->button_count = min(pdata->f19_button_map->nbuttons,
+					 (u8) f19->query.button_count);
+			for (i = 0; i < f19->button_count; i++)
+				f19->button_map[i] =
+					pdata->f19_button_map->map[i];
+		}
+	}
+	rc = rmi_f19_read_control_parameters(rmi_dev, f19);
+	if (rc < 0) {
+		dev_err(&fc->dev,
+			"Failed to initialize F19 control params.\n");
+		return rc;
+	}
+
+	mutex_init(&f19->control_mutex);
+	mutex_init(&f19->data_mutex);
+	return 0;
+}
+
+static int rmi_f19_register_device(struct rmi_function_container *fc)
+{
+	int i;
+	int rc;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f19_data *f19 = fc->data;
+	struct input_dev *input_dev = input_allocate_device();
+
+	if (!input_dev) {
+		dev_err(&fc->dev, "Failed to allocate input device.\n");
+		return -ENOMEM;
+	}
+
+	f19->input = input_dev;
+	snprintf(f19->input_name, MAX_LEN, "%sfn%02x", dev_name(&rmi_dev->dev),
+		fc->fd.function_number);
+	input_dev->name = f19->input_name;
+	snprintf(f19->input_phys, MAX_LEN, "%s/input0", input_dev->name);
+	input_dev->phys = f19->input_phys;
+	input_dev->dev.parent = &rmi_dev->dev;
+	input_set_drvdata(input_dev, f19);
+
+	/* Set up any input events. */
+	set_bit(EV_SYN, input_dev->evbit);
+	set_bit(EV_KEY, input_dev->evbit);
+
+	/* manage button map using input subsystem */
+	input_dev->keycode = f19->button_map;
+	input_dev->keycodesize = sizeof(f19->button_map);
+	input_dev->keycodemax = f19->button_count;
+
+	/* set bits for each button... */
+	for (i = 0; i < f19->button_count; i++)
+		set_bit(f19->button_map[i], input_dev->keybit);
+	rc = input_register_device(input_dev);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to register input device.\n");
+		goto error_free_device;
+	}
+
+	return 0;
+
+error_free_device:
+	input_free_device(input_dev);
+
+	return rc;
+}
+
+
+static int rmi_f19_create_sysfs(struct rmi_function_container *fc)
+{
+	struct f19_data *f19 = fc->data;
+	union f19_0d_query *query = &f19->query;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.\n");
+
+	/* Set up sysfs device attributes. */
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_control) < 0) {
+		dev_err(&fc->dev, "Failed to create control sysfs files.");
+		return -ENODEV;
+	}
+	if (query->has_sensitivity_adjust) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(sensitivity_adj)) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+	if (query->has_hysteresis_threshold) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(hysteresis_threshold)) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_command) < 0) {
+		dev_err(&fc->dev, "Failed to create command sysfs files.");
+		return -ENODEV;
+	}
+	if (query->configurable) {
+		if (sysfs_create_file(&fc->dev.kobj,
+					&sensor_map_attr.attr) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	} else {
+		if (sysfs_create_file(&fc->dev.kobj,
+					&sensor_map_ro_attr.attr) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+	return 0;
+}
+
+static int rmi_f19_config(struct rmi_function_container *fc)
+{
+	struct f19_data *f19 = fc->data;
+	int retval = 0;
+	union f19_0d_query *query = &f19->query;
+	struct f19_0d_control *control = &f19->control;
+
+	retval = rmi_write_block(fc->rmi_dev, control->reg_0->address,
+			(u8 *)control->reg_0->regs,
+			sizeof(u8));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write control reg0 to %#06x.\n",
+				control->reg_0->address);
+		return retval;
+	}
+
+	retval = rmi_write_block(fc->rmi_dev, control->reg_1->address,
+			(u8 *)control->reg_1->regs, f19->button_bitmask_size);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write control reg1 to %#06x.\n",
+			 control->reg_1->address);
+		return retval;
+	}
+
+	retval = rmi_write_block(fc->rmi_dev, control->reg_2->address,
+			(u8 *)control->reg_2->regs, f19->button_bitmask_size);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write control reg2 to %#06x.\n",
+			 control->reg_2->address);
+		return retval;
+	}
+
+	retval = rmi_write_block(fc->rmi_dev, control->reg_3->address,
+			(u8 *)control->reg_3->regs, query->button_count);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write control reg3 to %#06x.\n",
+			 control->reg_3->address);
+		return retval;
+	}
+
+	retval = rmi_write_block(fc->rmi_dev, control->reg_4->address,
+			(u8 *)control->reg_4->regs, query->button_count);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write control reg3 to %#06x.\n",
+			 control->reg_4->address);
+		return retval;
+	}
+
+	if (query->has_sensitivity_adjust) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_5->address,
+				(u8 *)control->reg_5->regs,
+				sizeof(control->reg_5->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev,
+				"Could not write control reg5 to %#06x.\n",
+				control->reg_5->address);
+			return retval;
+		}
+	}
+	if (query->has_hysteresis_threshold) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_6->address,
+				(u8 *)control->reg_6->regs,
+				sizeof(control->reg_6->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev,
+				"Could not write control reg6 to %#06x.\n",
+				control->reg_6->address);
+			return retval;
+		}
+	}
+	return 0;
+}
+
+
+static int rmi_f19_reset(struct rmi_function_container *fc)
+{
+	/* we do nnothing here */
+	return 0;
+}
+
+
+static void rmi_f19_remove(struct rmi_function_container *fc)
+{
+	struct f19_data *f19 = fc->data;
+
+	input_unregister_device(f19->input);
+	rmi_f19_free_memory(fc);
+}
+
+static int rmi_f19_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	int error;
+	int button;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f19_data *f19 = fc->data;
+	u16 data_base_addr = fc->fd.data_base_addr;
+
+	/* Read the button data. */
+	error = rmi_read_block(rmi_dev, data_base_addr, f19->button_data_buffer,
+			f19->button_bitmask_size);
+	if (error < 0) {
+		dev_err(&fc->dev, "%s: Failed to read button data registers.\n",
+			__func__);
+		return error;
+	}
+
+	/* Generate events for buttons. */
+	for (button = 0; button < f19->button_count; button++) {
+		int button_reg;
+		int button_shift;
+		bool button_status;
+
+		/* determine which data byte the button status is in */
+		button_reg = button / 8;
+		/* bit shift to get button's status */
+		button_shift = button % 8;
+		button_status =
+		    ((f19->button_data_buffer[button_reg] >> button_shift)
+			& 0x01) != 0;
+		/* Generate an event here. */
+		input_report_key(f19->input, f19->button_map[button],
+				 button_status);
+	}
+
+	input_sync(f19->input); /* sync after groups of events */
+	return 0;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x19,
+	.init = rmi_f19_init,
+	.config = rmi_f19_config,
+	.reset = rmi_f19_reset,
+	.attention = rmi_f19_attention,
+	.remove = rmi_f19_remove
+};
+
+static int __init rmi_f19_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void rmi_f19_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+/* sysfs functions */
+/* Query */
+simple_show_union_struct_unsigned(query, configurable)
+simple_show_union_struct_unsigned(query, has_sensitivity_adjust)
+simple_show_union_struct_unsigned(query, has_hysteresis_threshold)
+simple_show_union_struct_unsigned(query, button_count)
+
+/* Control */
+show_store_union_struct_unsigned(control, reg_0, button_usage)
+show_store_union_struct_unsigned(control, reg_0, filter_mode)
+show_store_union_struct_unsigned(control, reg_5, sensitivity_adj)
+show_store_union_struct_unsigned(control, reg_6, hysteresis_threshold)
+
+/* repeated register functions */
+show_store_repeated_union_struct_unsigned(control, reg_1, int_enabled_button)
+show_store_repeated_union_struct_unsigned(control, reg_2, single_button)
+show_store_repeated_union_struct_unsigned(control, reg_3, sensor_map_button)
+show_store_repeated_union_struct_unsigned(control, reg_4, sensitivity_button)
+
+/* command */
+static ssize_t f19_rezero_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf,
+					 size_t count)
+{
+	unsigned int new_value;
+	int len;
+	struct f19_data *f19;
+	struct rmi_function_container *fc = to_rmi_function_container(dev);
+
+	f19 = fc->data;
+	len = sscanf(buf, "%u", &new_value);
+	if (new_value != 0 && new_value != 1) {
+		dev_err(dev, "%s: Error - rezero is not a valid value 0x%x.\n",
+			__func__, new_value);
+		return -EINVAL;
+	}
+	f19->button_rezero = new_value & 1;
+	len = rmi_write(fc->rmi_dev, fc->fd.command_base_addr,
+		f19->button_rezero);
+
+	if (len < 0) {
+		dev_err(dev, "%s : Could not write rezero to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return -EINVAL;
+	}
+	return count;
+}
+
+
+
+module_init(rmi_f19_module_init);
+module_exit(rmi_f19_module_exit);
+
+MODULE_AUTHOR("Vivian Ly <[email protected]>");
+MODULE_DESCRIPTION("RMI F19 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#30 lore raw

RFC PATCH 13/17 input: RMI4 F21 Force sensing

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f21.c |  832 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 832 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f21.c new file +832 -0
new file mode 100644
index 0000000..2e37c75
@@ -0,0 +1,832 @@
+/*
+ * Copyright (c) 2012-2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define FUNCTION_DATA rmi_fn_21_data
+#define FNUM 21
+
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+union f21_2df_query {
+	struct {
+		/* Query 0 */
+		u8 max_force_sensor_count:3;
+		u8 f21_2df_query0_b3__6:4;
+		u8 has_high_resolution:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f21_2df_control_0__3 {
+	struct {
+		/* Control 0 */
+		u8 reporting_mode:3;
+		u8 no_rezero:1;
+		u8 ctl0_reserved:4;
+
+		/* Control 1 */
+		u8 force_click_threshold:8;
+
+		/* Control 2 */
+		u8 int_en_force_0:1;
+		u8 int_en_force_1:1;
+		u8 int_en_force_2:1;
+		u8 int_en_force_3:1;
+		u8 int_en_force_4:1;
+		u8 int_en_force_5:1;
+		u8 int_en_force_6:1;
+		u8 int_en_click:1;
+
+		/* Control 3 */
+		u8 force_interrupt_threshold:8;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[4];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f21_2df_control_4n {
+	/*Control 4.* */
+	u8 one_newton_linear_calibration:7;
+	u8 use_cfg_cal:1;
+} __attribute__((__packed__));
+
+struct f21_2df_control_4 {
+		struct f21_2df_control_4n *regs;
+		u16 address;
+		u8 length;
+} __attribute__((__packed__));
+
+struct f21_2df_control_5n {
+	/* Control 5 */
+	u8 one_newton_nonlinear_calibration;
+};
+
+struct f21_2df_control_5 {
+		struct f21_2df_control_5n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f21_2df_control_6n {
+	/*Control 6.* */
+	u8 x_location;
+};
+
+struct f21_2df_control_6 {
+		struct f21_2df_control_6n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f21_2df_control_7n {
+	/*Control 7.* */
+	u8 y_location;
+};
+
+struct f21_2df_control_7 {
+		struct f21_2df_control_7n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f21_2df_control_8n {
+	/*Control 8.* */
+	u8 transmitter_force_sensor;
+};
+
+struct f21_2df_control_8 {
+		struct f21_2df_control_8n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f21_2df_control_9n {
+	/*Control 9.* */
+	u8 receiver_force_sensor;
+};
+
+struct f21_2df_control_9 {
+		struct f21_2df_control_9n *regs;
+		u16 address;
+		u8 length;
+};
+
+
+#define RMI_F21_NUM_CTRL_REGS 8
+struct f21_2df_control {
+	/* Control 0-3 */
+	union f21_2df_control_0__3 *reg_0__3;
+
+	/* Control 4 */
+	struct f21_2df_control_4 *reg_4;
+
+	/* Control 5 */
+	struct f21_2df_control_5 *reg_5;
+
+	/* Control 6 */
+	struct f21_2df_control_6 *reg_6;
+
+	/* Control 7 */
+	struct f21_2df_control_7 *reg_7;
+
+	/* Control 8 */
+	struct f21_2df_control_8 *reg_8;
+
+	/* Control 9 */
+	struct f21_2df_control_9 *reg_9;
+};
+
+union f21_2df_data_2 {
+	struct {
+		/* Data 2 */
+		u8 force_click:1;
+		u8 f21_2df_control0_b2__7:7;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f21_2df_data {
+	/* Data 0 */
+	struct {
+		u8 *force_hi_lo;
+		u16 address;
+	} reg_0__1;
+
+	/* Data 2 */
+	union f21_2df_data_2 *reg_2;
+};
+
+#define F21_REZERO_CMD 0x01
+
+/* data specific to fn $21 that needs to be kept around */
+struct rmi_fn_21_data {
+	union f21_2df_query query;
+	struct f21_2df_control control;
+	struct f21_2df_data data;
+
+	struct mutex control_mutex;
+	struct mutex data_mutex;
+};
+
+static int rmi_f21_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f21_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f21_initialize(struct rmi_function_container *fc);
+
+static int rmi_f21_config(struct rmi_function_container *fc);
+
+static int rmi_f21_create_sysfs(struct rmi_function_container *fc);
+
+/* Sysfs files */
+
+/* Query sysfs files */
+
+
+show_union_struct_prototype(max_force_sensor_count)
+show_union_struct_prototype(has_high_resolution)
+
+static struct attribute *attrs[] = {
+	attrify(max_force_sensor_count),
+	attrify(has_high_resolution),
+	NULL
+};
+static struct attribute_group attrs_query = GROUP(attrs);
+/* Control sysfs files */
+
+show_store_union_struct_prototype(reporting_mode)
+show_store_union_struct_prototype(no_rezero)
+show_store_union_struct_prototype(force_click_threshold)
+show_store_union_struct_prototype(int_en_force_0)
+show_store_union_struct_prototype(int_en_force_1)
+show_store_union_struct_prototype(int_en_force_2)
+show_store_union_struct_prototype(int_en_force_3)
+show_store_union_struct_prototype(int_en_force_4)
+show_store_union_struct_prototype(int_en_force_5)
+show_store_union_struct_prototype(int_en_force_6)
+show_store_union_struct_prototype(int_en_click)
+show_store_union_struct_prototype(force_interrupt_threshold)
+
+show_store_union_struct_prototype(use_cfg_cal)
+show_store_union_struct_prototype(one_newton_linear_calibration)
+show_store_union_struct_prototype(one_newton_nonlinear_calibration)
+show_store_union_struct_prototype(x_location)
+show_store_union_struct_prototype(y_location)
+show_store_union_struct_prototype(transmitter_force_sensor)
+show_store_union_struct_prototype(receiver_force_sensor)
+
+
+static struct attribute *attrs2[] = {
+	attrify(reporting_mode),
+	attrify(no_rezero),
+	attrify(force_click_threshold),
+	attrify(int_en_click),
+	attrify(force_interrupt_threshold),
+	attrify(use_cfg_cal),
+	attrify(one_newton_linear_calibration),
+	attrify(one_newton_nonlinear_calibration),
+	attrify(x_location),
+	attrify(y_location),
+	attrify(transmitter_force_sensor),
+	attrify(receiver_force_sensor),
+	NULL
+};
+static struct attribute_group attrs_control = GROUP(attrs2);
+
+/* Data sysfs files */
+show_union_struct_prototype(force)
+show_union_struct_prototype(force_click)
+
+static struct attribute *attrs3[] = {
+	attrify(force),
+	attrify(force_click),
+	NULL
+};
+static struct attribute_group attrs_data = GROUP(attrs3);
+
+/* Command sysfs files */
+
+static ssize_t rmi_fn_21_rezero_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count);
+DEVICE_ATTR(rezero, RMI_WO_ATTR,
+		rmi_show_error,
+		rmi_fn_21_rezero_store);
+
+static struct attribute *attrs4[] = {
+	attrify(rezero),
+	NULL
+};
+static struct attribute_group attrs_command = GROUP(attrs4);
+
+static int rmi_f21_init(struct rmi_function_container *fc)
+{
+	int retval = 0;
+
+	dev_info(&fc->dev, "Intializing F21.");
+
+	retval = rmi_f21_alloc_memory(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f21_initialize(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f21_create_sysfs(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	return retval;
+
+error_exit:
+	rmi_f21_free_memory(fc);
+
+	return retval;
+}
+
+static int rmi_f21_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_21_data *f21;
+
+	f21 = kzalloc(sizeof(struct rmi_fn_21_data), GFP_KERNEL);
+	if (!f21) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_21_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f21;
+
+	return 0;
+}
+
+
+static void rmi_f21_free_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_21_data *f21 = fc->data;
+	u8 int_num = f21->query.max_force_sensor_count;
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+	sysfs_remove_group(&fc->dev.kobj, &attrs_control);
+	switch (int_num) {
+	case 7:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_6));
+	case 6:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_5));
+	case 5:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_4));
+	case 4:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_3));
+	case 3:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_2));
+	case 2:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_1));
+	case 1:
+		sysfs_remove_file(&fc->dev.kobj, attrify(int_en_force_0));
+	default:
+		break;
+	}
+	sysfs_remove_group(&fc->dev.kobj, &attrs_data);
+	sysfs_remove_group(&fc->dev.kobj, &attrs_command);
+	if (f21) {
+		kfree(f21->control.reg_0__3);
+		kfree(f21->control.reg_4->regs);
+		kfree(f21->control.reg_4);
+		kfree(f21->control.reg_5->regs);
+		kfree(f21->control.reg_5);
+		kfree(f21->control.reg_6->regs);
+		kfree(f21->control.reg_6);
+		kfree(f21->control.reg_7->regs);
+		kfree(f21->control.reg_7);
+		kfree(f21->control.reg_8->regs);
+		kfree(f21->control.reg_8);
+		kfree(f21);
+		fc->data = NULL;
+	}
+}
+
+
+static int rmi_f21_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_fn_21_data *f21 = fc->data;
+	int retval = 0;
+	u16 next_loc;
+
+	/* Read F21 Query Data */
+	f21->query.address = fc->fd.query_base_addr;
+	retval = rmi_read_block(fc->rmi_dev, f21->query.address,
+		(u8 *)&f21->query, sizeof(f21->query.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read query registers from %#06x.\n",
+				f21->query.address);
+		return retval;
+	}
+
+	/* Initialize Control Data */
+	next_loc = fc->fd.control_base_addr;
+
+	f21->control.reg_0__3 =
+			kzalloc(sizeof(union f21_2df_control_0__3), GFP_KERNEL);
+	if (!f21->control.reg_0__3) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_0__3->address = next_loc;
+	next_loc += sizeof(f21->control.reg_0__3->regs);
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_0__3->address,
+			f21->control.reg_0__3->regs,
+			sizeof(f21->control.reg_0__3->regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read control registers 0-3 from %#06x.\n",
+			f21->control.reg_0__3->address);
+		return retval;
+	}
+
+	f21->control.reg_4 =
+			kzalloc(sizeof(struct f21_2df_control_4), GFP_KERNEL);
+	if (!f21->control.reg_4) {
+		dev_err(&fc->dev, "Failed to allocate control register.");
+		return -ENOMEM;
+	}
+	f21->control.reg_4->length = f21->query.max_force_sensor_count;
+	f21->control.reg_4->regs = kzalloc(sizeof(struct f21_2df_control_4n) *
+			f21->control.reg_4->length, GFP_KERNEL);
+	if (!f21->control.reg_4->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_4->address = next_loc;
+	next_loc += f21->control.reg_4->length;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_4->address,
+		(u8 *) f21->control.reg_4->regs, f21->control.reg_4->length);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read Control register 4 from %#06x.\n",
+			f21->control.reg_4->address);
+		return retval;
+	}
+
+
+	f21->control.reg_5 =
+			kzalloc(sizeof(struct f21_2df_control_5), GFP_KERNEL);
+	if (!f21->control.reg_5) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_5->length = f21->query.max_force_sensor_count;
+	f21->control.reg_5->regs = kzalloc(sizeof(struct f21_2df_control_5n) *
+				f21->control.reg_5->length, GFP_KERNEL);
+	if (!f21->control.reg_5->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_5->address = next_loc;
+	next_loc += f21->control.reg_5->length;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_5->address,
+				(u8 *) f21->control.reg_5->regs,
+				f21->control.reg_5->length);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read Control register 5 from %#06x.\n",
+				f21->control.reg_5->address);
+		return retval;
+	}
+
+	f21->control.reg_6 =
+			kzalloc(sizeof(struct f21_2df_control_6), GFP_KERNEL);
+	if (!f21->control.reg_6) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_6->length = f21->query.max_force_sensor_count;
+	f21->control.reg_6->regs = kzalloc(sizeof(struct f21_2df_control_6n) *
+				f21->control.reg_6->length, GFP_KERNEL);
+	if (!f21->control.reg_6->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_6->address = next_loc;
+	next_loc += f21->control.reg_6->length;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_6->address,
+			(u8 *) f21->control.reg_6->regs,
+				sizeof(f21->control.reg_6->regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read control register 6 from %#06x.\n",
+					f21->control.reg_6->address);
+		return retval;
+	}
+
+	f21->control.reg_7 =
+			kzalloc(sizeof(struct f21_2df_control_7), GFP_KERNEL);
+	if (!f21->control.reg_7) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_7->length = f21->query.max_force_sensor_count;
+	f21->control.reg_7->regs = kzalloc(sizeof(struct f21_2df_control_7n) *
+				f21->control.reg_7->length, GFP_KERNEL);
+	if (!f21->control.reg_7->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_7->address = next_loc;
+	next_loc += f21->control.reg_7->length;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_7->address,
+		(u8 *) f21->control.reg_7->regs, f21->control.reg_7->length);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read control register 7 from %#06x.\n",
+				f21->control.reg_7->address);
+		return retval;
+	}
+
+	f21->control.reg_8 =
+			kzalloc(sizeof(struct f21_2df_control_8), GFP_KERNEL);
+	if (!f21->control.reg_8) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_8->length = f21->query.max_force_sensor_count;
+	f21->control.reg_8->regs = kzalloc(sizeof(struct f21_2df_control_8n) *
+					f21->control.reg_8->length, GFP_KERNEL);
+	if (!f21->control.reg_8->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_8->address = next_loc;
+	next_loc += f21->control.reg_8->length;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_8->address,
+		(u8 *) f21->control.reg_8->regs, f21->control.reg_8->length);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read control register 8 from %#06x.\n",
+					f21->control.reg_8->address);
+		return retval;
+	}
+
+	f21->control.reg_9 =
+			kzalloc(sizeof(struct f21_2df_control_9), GFP_KERNEL);
+	if (!f21->control.reg_9) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_9->length = f21->query.max_force_sensor_count;
+	f21->control.reg_9->regs =
+			kzalloc(sizeof(struct f21_2df_control_9n)
+			*f21->control.reg_9->length, GFP_KERNEL);
+	if (!f21->control.reg_9->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_9->address = next_loc;
+	retval = rmi_read_block(fc->rmi_dev, f21->control.reg_9->address,
+		(u8 *) f21->control.reg_9->regs, f21->control.reg_9->length);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read control register 9 from %#06x.\n",
+					f21->control.reg_9->address);
+		return retval;
+	}
+
+
+	/* initialize data registers */
+	next_loc = fc->fd.data_base_addr;
+
+	f21->data.reg_0__1.force_hi_lo = kzalloc(
+			2 * f21->query.max_force_sensor_count * sizeof(u8),
+			GFP_KERNEL);
+	if (!f21->data.reg_0__1.force_hi_lo) {
+		dev_err(&fc->dev, "Failed to allocate data registers.");
+		return -ENOMEM;
+	}
+	f21->data.reg_0__1.address = next_loc;
+	next_loc += 2 * f21->query.max_force_sensor_count;
+
+	f21->data.reg_2 =
+			kzalloc(sizeof(union f21_2df_data_2), GFP_KERNEL);
+	if (!f21->data.reg_2) {
+		dev_err(&fc->dev, "Failed to allocate data registers.");
+		return -ENOMEM;
+	}
+	f21->control.reg_0__3->address = next_loc;
+
+	mutex_init(&f21->control_mutex);
+	return 0;
+}
+
+
+static int rmi_f21_create_sysfs(struct rmi_function_container *fc)
+{
+	struct rmi_fn_21_data *f21 = fc->data;
+	u8 int_num = f21->query.max_force_sensor_count;
+	int rc;
+
+	if (int_num > 7)
+		int_num = 7;
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+
+	/* Set up sysfs device attributes. */
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_control) < 0) {
+		dev_err(&fc->dev, "Failed to create control sysfs files.");
+		return -ENODEV;
+	}
+	switch (int_num) {
+	case 7:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_6));
+		break;
+	case 6:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_5));
+		break;
+	case 5:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_4));
+		break;
+	case 4:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_3));
+		break;
+	case 3:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_2));
+		break;
+	case 2:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_1));
+		break;
+	case 1:
+		rc = sysfs_create_file(&fc->dev.kobj, attrify(int_en_force_0));
+		break;
+	default:
+		rc = 0;
+		break;
+	}
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to create control sysfs files.\n");
+		return rc;
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_data) < 0) {
+		dev_err(&fc->dev, "Failed to create data sysfs files.\n");
+		return -ENODEV;
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_command) < 0) {
+		dev_err(&fc->dev, "Failed to create command sysfs files.\n");
+		return -ENODEV;
+	}
+	return 0;
+}
+
+
+static int rmi_f21_config(struct rmi_function_container *fc)
+{
+	struct rmi_fn_21_data *data = fc->data;
+/* repeated register functions */
+
+	/* Write Control Register values back to device */
+	rmi_write_block(fc->rmi_dev, data->control.reg_0__3->address,
+				(u8 *)data->control.reg_0__3,
+				sizeof(data->control.reg_0__3->regs));
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_4->address,
+			(u8 *) data->control.reg_4->regs,
+			data->control.reg_4->length);
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_5->address,
+			(u8 *) data->control.reg_5->regs,
+			data->control.reg_5->length);
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_6->address,
+			(u8 *) data->control.reg_6->regs,
+			data->control.reg_6->length);
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_7->address,
+			(u8 *) data->control.reg_7->regs,
+			data->control.reg_7->length);
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_8->address,
+			(u8 *) data->control.reg_8->regs,
+			data->control.reg_8->length);
+
+	rmi_write_block(fc->rmi_dev, data->control.reg_9->address,
+			(u8 *) data->control.reg_9->regs,
+			data->control.reg_9->length);
+
+	return 0;
+}
+
+static void rmi_f21_remove(struct rmi_function_container *fc)
+{
+
+	dev_info(&fc->dev, "Removing F21.");
+	rmi_f21_free_memory(fc);
+}
+
+/* sysfs functions */
+/* Query */
+simple_show_union_struct_unsigned(query, max_force_sensor_count)
+simple_show_union_struct_unsigned(query, has_high_resolution)
+
+/* Control */
+show_store_union_struct_unsigned(control, reg_0__3, reporting_mode)
+show_store_union_struct_unsigned(control, reg_0__3, no_rezero)
+
+show_store_union_struct_unsigned(control, reg_0__3, force_click_threshold)
+
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_0)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_1)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_2)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_3)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_4)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_5)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_force_6)
+show_store_union_struct_unsigned(control, reg_0__3, int_en_click)
+show_store_union_struct_unsigned(control, reg_0__3, force_interrupt_threshold)
+
+
+/* repeated register functions */
+show_store_repeated_union_struct_unsigned(control, reg_4, use_cfg_cal)
+show_store_repeated_union_struct_unsigned(control, reg_4,
+					  one_newton_linear_calibration)
+show_store_repeated_union_struct_unsigned(control, reg_5,
+					  one_newton_nonlinear_calibration)
+show_store_repeated_union_struct_unsigned(control, reg_6, x_location)
+show_store_repeated_union_struct_unsigned(control, reg_7, y_location)
+show_store_repeated_union_struct_unsigned(control, reg_8,
+					  transmitter_force_sensor)
+show_store_repeated_union_struct_unsigned(control, reg_9,
+					  receiver_force_sensor)
+
+/* Data */
+static ssize_t rmi_fn_21_force_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf) {
+	struct rmi_function_container *fc;
+	struct FUNCTION_DATA *data;
+	int reg_length;
+	int result, size = 0;
+	char *temp;
+	int i;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+
+	/* Read current regtype values */
+	reg_length = data->query.max_force_sensor_count;
+	result = rmi_read_block(fc->rmi_dev, data->data.reg_0__1.address,
+			data->data.reg_0__1.force_hi_lo,
+			2 * reg_length * sizeof(u8));
+
+	if (result < 0) {
+		dev_err(dev, "Could not read regtype at %#06x. Data may be outdated.",
+					data->data.reg_0__1.address);
+	}
+	temp = buf;
+	for (i = 0; i < reg_length; i++) {
+		result = snprintf(temp, PAGE_SIZE - size, "%d ",
+			data->data.reg_0__1.force_hi_lo[i] * (2 << 3)
+			+ data->data.reg_0__1.force_hi_lo[i + reg_length]);
+		if (result < 0) {
+			dev_err(dev, "%s : Could not write output.", __func__);
+			return result;
+		}
+		size += result;
+		temp += result;
+	}
+	result = snprintf(temp, PAGE_SIZE - size, "\n");
+	if (result < 0) {
+			dev_err(dev, "%s : Could not write output.", __func__);
+			return result;
+	}
+	return size + result;
+}
+
+show_union_struct_unsigned(data, reg_2, force_click)
+
+/* Command */
+
+static ssize_t rmi_fn_21_rezero_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count) {
+	unsigned long val;
+	int error, result;
+	struct rmi_function_container *fc;
+	struct rmi_fn_21_data *f21;
+	struct rmi_driver *driver;
+	u8 command;
+
+	fc = to_rmi_function_container(dev);
+	f21 = fc->data;
+	driver = fc->rmi_dev->driver;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+	if (error)
+		return error;
+	/* Do nothing if not set to 1. This prevents accidental commands. */
+	if (val != 1)
+		return count;
+
+	command = (unsigned char)F21_REZERO_CMD;
+
+	/* Write the command to the command register */
+	result = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
+						&command, 1);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write command to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return result;
+	}
+	return count;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x21,
+	.init = rmi_f21_init,
+	.config = rmi_f21_config,
+	.reset = NULL,
+	.attention = NULL,
+	.remove = rmi_f21_remove
+};
+
+static int __init rmi_f21_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+	return 0;
+}
+
+static void rmi_f21_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+module_init(rmi_f21_module_init);
+module_exit(rmi_f21_module_exit);
+
+MODULE_AUTHOR("Daniel Rosenberg <[email protected]>");
+MODULE_DESCRIPTION("RMI F21 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#36 lore raw

RFC PATCH 17/17 input: RMI4 F54 analog data reporting

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f54.c | 2247 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 2247 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f54.c new file +2247 -0
new file mode 100644
index 0000000..9c19799
@@ -0,0 +1,2247 @@
+/*
+ * Copyright (c) 2011-2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <linux/hrtimer.h>
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/slab.h>
+#include <linux/version.h>
+#include <linux/delay.h>
+
+#define FUNCTION_DATA rmi_fn_54_data
+#define FNUM 54
+
+#include "rmi_driver.h"
+
+/* Set this to 1 for raw hex dump of returned data. */
+#define RAW_HEX 0
+/* Set this to 1 for human readable dump of returned data. */
+#define HUMAN_READABLE 0
+/* The watchdog timer can be useful when debugging certain firmware related
+ * issues.
+ */
+#define F54_WATCHDOG 1
+
+/* define fn $54 commands */
+#define GET_REPORT                1
+#define FORCE_CAL                 2
+
+#define NO_AUTO_CAL_MASK 1
+/* status */
+#define BUSY 1
+#define IDLE 0
+
+/* Offsets for data */
+#define RMI_F54_REPORT_DATA_OFFSET	3
+#define RMI_F54_FIFO_OFFSET		1
+#define RMI_F54_NUM_TX_OFFSET		1
+#define RMI_F54_NUM_RX_OFFSET		0
+
+/* Fixed sizes of reports */
+#define RMI_54_FULL_RAW_CAP_MIN_MAX_SIZE 4
+#define RMI_54_HIGH_RESISTANCE_SIZE 6
+
+/* definitions for F54 Query Registers */
+union f54_ad_query {
+	struct {
+		/* query 0 */
+		u8 num_of_rx_electrodes;
+
+		/* query 1 */
+		u8 num_of_tx_electrodes;
+
+		/* query2 */
+		u8 f54_ad_query2_b0__1:2;
+		u8 has_baseline:1;
+		u8 has_image8:1;
+		u8 f54_ad_query2_b4__5:2;
+		u8 has_image16:1;
+		u8 f54_ad_query2_b7:1;
+
+		/* query 3.0 and 3.1 */
+		u16 clock_rate;
+
+		/* query 4 */
+		u8 touch_controller_family;
+
+		/* query 5 */
+		u8 has_pixel_touch_threshold_adjustment:1;
+		u8 f54_ad_query5_b1__7:7;
+
+		/* query 6 */
+		u8 has_sensor_assignment:1;
+		u8 has_interference_metric:1;
+		u8 has_sense_frequency_control:1;
+		u8 has_firmware_noise_mitigation:1;
+		u8 f54_ad_query6_b4:1;
+		u8 has_two_byte_report_rate:1;
+		u8 has_one_byte_report_rate:1;
+		u8 has_relaxation_control:1;
+
+		/* query 7 */
+		u8 curve_compensation_mode:2;
+		u8 f54_ad_query7_b2__7:6;
+
+		/* query 8 */
+		u8 f54_ad_query2_b0:1;
+		u8 has_iir_filter:1;
+		u8 has_cmn_removal:1;
+		u8 has_cmn_maximum:1;
+		u8 has_touch_hysteresis:1;
+		u8 has_edge_compensation:1;
+		u8 has_per_frequency_noise_control:1;
+		u8 f54_ad_query8_b7:1;
+
+		u8 f54_ad_query9;
+		u8 f54_ad_query10;
+
+		/* query 11 */
+		u8 f54_ad_query11_b0__6:7;
+		u8 has_query_15:1;
+
+		/* query 12 */
+		u8 number_of_sensing_frequencies:4;
+		u8 f54_ad_query12_b4__7:4;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[14];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+/* And now for the very large amount of control registers */
+
+/* Ctrl registers */
+
+union f54_ad_control_0 {
+	/* control 0 */
+	struct {
+		u8 no_relax:1;
+		u8 no_scan:1;
+		u8 f54_ad_ctrl0_b2__7:6;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_1 {
+	/* control 1 */
+	struct {
+		/* control 1 */
+		u8 bursts_per_cluster:4;
+		u8 f54_ad_ctrl1_b4__7:4;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_2 {
+	/* control 2 */
+	struct {
+		u16 saturation_cap;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_3 {
+	/* control 3 */
+	struct {
+		u16 pixel_touch_threshold;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_4__6 {
+	struct {
+		/* control 4 */
+		u8 rx_feedback_cap:2;
+		u8 f54_ad_ctrl4_b2__7:6;
+
+		/* control 5 */
+		u8 low_ref_cap:2;
+		u8 low_ref_feedback_cap:2;
+		u8 low_ref_polarity:1;
+		u8 f54_ad_ctrl5_b5__7:3;
+
+		/* control 6 */
+		u8 high_ref_cap:2;
+		u8 high_ref_feedback_cap:2;
+		u8 high_ref_polarity:1;
+		u8 f54_ad_ctrl6_b5__7:3;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[3];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_7 {
+	struct {
+		/* control 7 */
+		u8 cbc_cap:2;
+		u8 cbc_polarity:2;
+		u8 cbc_tx_carrier_selection:1;
+		u8 f54_ad_ctrl6_b5__7:3;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_8__9 {
+	struct {
+		/* control 8 */
+		u16 integration_duration:10;
+		u16 f54_ad_ctrl8_b10__15:6;
+		/* control 9 */
+		u8 reset_duration;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[3];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_10 {
+	struct {
+		/* control 10 */
+		u8 noise_sensing_bursts_per_image:4;
+		u8 f54_ad_ctrl10_b4__7:4;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_11 {
+	struct {
+		/* control 11 */
+		u8 reserved;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_12__13 {
+	struct {
+		/* control 12 */
+		u8 slow_relaxation_rate;
+
+		/* control 13 */
+		u8 fast_relaxation_rate;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_14 {
+	struct {
+		/* control 14 */
+			u8 rxs_on_xaxis:1;
+			u8 curve_comp_on_txs:1;
+			u8 f54_ad_ctrl14b2__7:6;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f54_ad_control_15n {
+	/*Control 15.* */
+	u8 sensor_rx_assignment;
+};
+
+struct f54_ad_control_15 {
+		struct f54_ad_control_15n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_16n {
+	/*Control 16.* */
+	u8 sensor_tx_assignment;
+};
+
+struct f54_ad_control_16 {
+		struct f54_ad_control_16n *regs;
+		u16 address;
+		u8 length;
+};
+
+
+/* control 17 */
+struct f54_ad_control_17n {
+	u8 burst_countb10__8:3;
+	u8 disable:1;
+	u8 f54_ad_ctrlb4:1;
+	u8 filter_bandwidth:3;
+};
+
+struct f54_ad_control_17 {
+		struct f54_ad_control_17n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_18n {
+	/*Control 18.* */
+	u8 burst_countb7__0n;
+};
+
+struct f54_ad_control_18 {
+		struct f54_ad_control_18n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_19n {
+	/*Control 19.* */
+	u8 stretch_duration;
+};
+
+struct f54_ad_control_19 {
+		struct f54_ad_control_19n *regs;
+		u16 address;
+		u8 length;
+};
+
+union f54_ad_control_20 {
+	struct {
+		/* control 20 */
+		u8 disable_noise_mitigation:1;
+		u8 f54_ad_ctrl20b2__7:7;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_21 {
+	struct {
+		/* control 21 */
+		u16 freq_shift_noise_threshold;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_22__26 {
+	struct {
+		/* control 22 */
+		/* u8 noise_density_threshold; */
+		u8 f54_ad_ctrl22;
+
+		/* control 23 */
+		u16 medium_noise_threshold;
+
+		/* control 24 */
+		u16 high_noise_threshold;
+
+		/* control 25 */
+		u8 noise_density;
+
+		/* control 26 */
+		u8 frame_count;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[7];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_27 {
+	struct {
+		/* control 27 */
+		u8 iir_filter_coef;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_28 {
+	struct {
+		/* control 28 */
+		u16 quiet_threshold;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+
+union f54_ad_control_29 {
+	struct {
+		/* control 29 */
+		u8 f54_ad_ctrl20b0__6:7;
+		u8 cmn_filter_disable:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_30 {
+	struct {
+		/* control 30 */
+		u8 cmn_filter_max;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_31 {
+	struct {
+		/* control 31 */
+		u8 touch_hysteresis;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f54_ad_control_32__35 {
+	struct {
+		/* control 32 */
+		u16 rx_low_edge_comp;
+
+		/* control 33 */
+		u16 rx_high_edge_comp;
+
+		/* control 34 */
+		u16 tx_low_edge_comp;
+
+		/* control 35 */
+		u16 tx_high_edge_comp;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[8];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f54_ad_control_36n {
+	/*Control 36.* */
+	u8 axis1_comp;
+};
+
+struct f54_ad_control_36 {
+		struct f54_ad_control_36n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_37n {
+	/*Control 37.* */
+	u8 axis2_comp;
+};
+
+struct f54_ad_control_37 {
+		struct f54_ad_control_37n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_38n {
+	/*Control 38.* */
+	u8 noise_control_1;
+};
+
+struct f54_ad_control_38 {
+		struct f54_ad_control_38n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_39n {
+	/*Control 39.* */
+	u8 noise_control_2;
+};
+
+struct f54_ad_control_39 {
+		struct f54_ad_control_39n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control_40n {
+	/*Control 40.* */
+	u8 noise_control_3;
+};
+
+struct f54_ad_control_40 {
+		struct f54_ad_control_40n *regs;
+		u16 address;
+		u8 length;
+};
+
+struct f54_ad_control {
+	union f54_ad_control_0 *reg_0;
+	union f54_ad_control_1 *reg_1;
+	union f54_ad_control_2 *reg_2;
+	union f54_ad_control_3 *reg_3;
+	union f54_ad_control_4__6 *reg_4__6;
+	union f54_ad_control_7 *reg_7;
+	union f54_ad_control_8__9 *reg_8__9;
+	union f54_ad_control_10 *reg_10;
+	union f54_ad_control_11 *reg_11;
+	union f54_ad_control_12__13 *reg_12__13;
+	union f54_ad_control_14 *reg_14;
+	/* control 15 */
+	struct f54_ad_control_15 *reg_15;
+	/* control 16 */
+	struct f54_ad_control_16 *reg_16;
+
+	/* This register is n repetitions of f54_ad_control_17 */
+	struct f54_ad_control_17 *reg_17;
+
+	/* control 18 */
+	struct f54_ad_control_18 *reg_18;
+
+	/* control 19 */
+	struct f54_ad_control_19 *reg_19;
+
+	union f54_ad_control_20 *reg_20;
+	union f54_ad_control_21 *reg_21;
+	union f54_ad_control_22__26 *reg_22__26;
+	union f54_ad_control_27 *reg_27;
+	union f54_ad_control_28 *reg_28;
+	union f54_ad_control_29 *reg_29;
+	union f54_ad_control_30 *reg_30;
+	union f54_ad_control_31 *reg_31;
+	union f54_ad_control_32__35 *reg_32__35;
+	/* control 36 */
+	struct f54_ad_control_36 *reg_36;
+
+	/* control 37 */
+	struct f54_ad_control_37 *reg_37;
+
+	/* control 38 */
+	struct f54_ad_control_38 *reg_38;
+
+	/* control 39 */
+	struct f54_ad_control_39 *reg_39;
+
+	/* control 40 */
+	struct f54_ad_control_40 *reg_40;
+};
+
+/* define report types */
+enum f54_report_types {
+	F54_8BIT_IMAGE = 1,
+	F54_16BIT_IMAGE = 2,
+	F54_RAW_16BIT_IMAGE = 3,
+	F54_HIGH_RESISTANCE = 4,
+	F54_TX_TO_TX_SHORT = 5,
+	F54_RX_TO_RX1 = 7,
+	F54_TRUE_BASELINE = 9,
+	F54_FULL_RAW_CAP_MIN_MAX = 13,
+	F54_RX_OPENS1 = 14,
+	F54_TX_OPEN = 15,
+	F54_TX_TO_GROUND = 16,
+	F54_RX_TO_RX2 = 17,
+	F54_RX_OPENS2 = 18,
+	F54_FULL_RAW_CAP = 19,
+	F54_FULL_RAW_CAP_RX_COUPLING_COMP = 20
+};
+
+/* sysfs functions */
+show_store_union_struct_prototype(report_type)
+
+store_union_struct_prototype(get_report)
+
+store_union_struct_prototype(force_cal)
+
+show_union_struct_prototype(status)
+
+static ssize_t rmi_fn_54_data_read(struct file *data_file, struct kobject *kobj,
+					struct bin_attribute *attributes,
+					char *buf, loff_t pos, size_t count);
+
+show_union_struct_prototype(num_of_rx_electrodes)
+show_union_struct_prototype(num_of_tx_electrodes)
+show_union_struct_prototype(has_image16)
+show_union_struct_prototype(has_image8)
+show_union_struct_prototype(has_baseline)
+show_union_struct_prototype(clock_rate)
+show_union_struct_prototype(touch_controller_family)
+show_union_struct_prototype(has_pixel_touch_threshold_adjustment)
+show_union_struct_prototype(has_sensor_assignment)
+show_union_struct_prototype(has_interference_metric)
+show_union_struct_prototype(has_sense_frequency_control)
+show_union_struct_prototype(has_firmware_noise_mitigation)
+show_union_struct_prototype(has_two_byte_report_rate)
+show_union_struct_prototype(has_one_byte_report_rate)
+show_union_struct_prototype(has_relaxation_control)
+show_union_struct_prototype(curve_compensation_mode)
+show_union_struct_prototype(has_iir_filter)
+show_union_struct_prototype(has_cmn_removal)
+show_union_struct_prototype(has_cmn_maximum)
+show_union_struct_prototype(has_touch_hysteresis)
+show_union_struct_prototype(has_edge_compensation)
+show_union_struct_prototype(has_per_frequency_noise_control)
+show_union_struct_prototype(number_of_sensing_frequencies)
+show_store_union_struct_prototype(no_auto_cal)
+show_store_union_struct_prototype(fifoindex)
+
+/* Repeated Control Registers */
+show_union_struct_prototype(sensor_rx_assignment)
+show_union_struct_prototype(sensor_tx_assignment)
+show_union_struct_prototype(filter_bandwidth)
+show_union_struct_prototype(disable)
+show_union_struct_prototype(burst_count)
+show_union_struct_prototype(stretch_duration)
+show_store_union_struct_prototype(axis1_comp)
+show_store_union_struct_prototype(axis2_comp)
+show_union_struct_prototype(noise_control_1)
+show_union_struct_prototype(noise_control_2)
+show_union_struct_prototype(noise_control_3)
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+show_store_union_struct_prototype(no_relax)
+show_store_union_struct_prototype(no_scan)
+show_store_union_struct_prototype(bursts_per_cluster)
+show_store_union_struct_prototype(saturation_cap)
+show_store_union_struct_prototype(pixel_touch_threshold)
+show_store_union_struct_prototype(rx_feedback_cap)
+show_store_union_struct_prototype(low_ref_cap)
+show_store_union_struct_prototype(low_ref_feedback_cap)
+show_store_union_struct_prototype(low_ref_polarity)
+show_store_union_struct_prototype(high_ref_cap)
+show_store_union_struct_prototype(high_ref_feedback_cap)
+show_store_union_struct_prototype(high_ref_polarity)
+show_store_union_struct_prototype(cbc_cap)
+show_store_union_struct_prototype(cbc_polarity)
+show_store_union_struct_prototype(cbc_tx_carrier_selection)
+show_store_union_struct_prototype(integration_duration)
+show_store_union_struct_prototype(reset_duration)
+show_store_union_struct_prototype(noise_sensing_bursts_per_image)
+show_store_union_struct_prototype(slow_relaxation_rate)
+show_store_union_struct_prototype(fast_relaxation_rate)
+show_store_union_struct_prototype(rxs_on_xaxis)
+show_store_union_struct_prototype(curve_comp_on_txs)
+show_store_union_struct_prototype(disable_noise_mitigation)
+show_store_union_struct_prototype(freq_shift_noise_threshold)
+/*show_store_union_struct_prototype(noise_density_threshold)*/
+show_store_union_struct_prototype(medium_noise_threshold)
+show_store_union_struct_prototype(high_noise_threshold)
+show_store_union_struct_prototype(noise_density)
+show_store_union_struct_prototype(frame_count)
+show_store_union_struct_prototype(iir_filter_coef)
+show_store_union_struct_prototype(quiet_threshold)
+show_store_union_struct_prototype(cmn_filter_disable)
+show_store_union_struct_prototype(cmn_filter_max)
+show_store_union_struct_prototype(touch_hysteresis)
+show_store_union_struct_prototype(rx_low_edge_comp)
+show_store_union_struct_prototype(rx_high_edge_comp)
+show_store_union_struct_prototype(tx_low_edge_comp)
+show_store_union_struct_prototype(tx_high_edge_comp)
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
+static struct attribute *attrs[] = {
+	attrify(report_type),
+	attrify(get_report),
+	attrify(force_cal),
+	attrify(status),
+	attrify(num_of_rx_electrodes),
+	attrify(num_of_tx_electrodes),
+	attrify(has_image16),
+	attrify(has_image8),
+	attrify(has_baseline),
+	attrify(clock_rate),
+	attrify(touch_controller_family),
+	attrify(has_pixel_touch_threshold_adjustment),
+	attrify(has_sensor_assignment),
+	attrify(has_interference_metric),
+	attrify(has_sense_frequency_control),
+	attrify(has_firmware_noise_mitigation),
+	attrify(has_two_byte_report_rate),
+	attrify(has_one_byte_report_rate),
+	attrify(has_relaxation_control),
+	attrify(curve_compensation_mode),
+	attrify(has_iir_filter),
+	attrify(has_cmn_removal),
+	attrify(has_cmn_maximum),
+	attrify(has_touch_hysteresis),
+	attrify(has_edge_compensation),
+	attrify(has_per_frequency_noise_control),
+	attrify(number_of_sensing_frequencies),
+	attrify(no_auto_cal),
+	attrify(fifoindex),
+	NULL
+};
+
+static struct attribute_group attrs_query = GROUP(attrs);
+
+struct bin_attribute dev_rep_data = {
+	.attr = {
+		 .name = "rep_data",
+		 .mode = RMI_RO_ATTR},
+	.size = 0,
+	.read = rmi_fn_54_data_read,
+};
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
+static struct attribute *attrs_reg_0[] = {
+	attrify(no_relax),
+	attrify(no_scan),
+	NULL
+};
+
+static struct attribute *attrs_reg_1[] = {
+	attrify(bursts_per_cluster),
+	NULL
+};
+
+static struct attribute *attrs_reg_2[] = {
+	attrify(saturation_cap),
+	NULL
+};
+
+static struct attribute *attrs_reg_3[] = {
+	attrify(pixel_touch_threshold),
+	NULL
+};
+
+static struct attribute *attrs_reg_4__6[] = {
+	attrify(rx_feedback_cap),
+	attrify(low_ref_cap),
+	attrify(low_ref_feedback_cap),
+	attrify(low_ref_polarity),
+	attrify(high_ref_cap),
+	attrify(high_ref_feedback_cap),
+	attrify(high_ref_polarity),
+	NULL
+};
+
+static struct attribute *attrs_reg_7[] = {
+	attrify(cbc_cap),
+	attrify(cbc_polarity),
+	attrify(cbc_tx_carrier_selection),
+	NULL
+};
+
+static struct attribute *attrs_reg_8__9[] = {
+	attrify(integration_duration),
+	attrify(reset_duration),
+	NULL
+};
+
+static struct attribute *attrs_reg_10[] = {
+	attrify(noise_sensing_bursts_per_image),
+	NULL
+};
+
+static struct attribute *attrs_reg_12__13[] = {
+	attrify(slow_relaxation_rate),
+	attrify(fast_relaxation_rate),
+	NULL
+};
+
+static struct attribute *attrs_reg_14__16[] = {
+	attrify(rxs_on_xaxis),
+	attrify(curve_comp_on_txs),
+	attrify(sensor_rx_assignment),
+	attrify(sensor_tx_assignment),
+	NULL
+};
+
+static struct attribute *attrs_reg_17__19[] = {
+	attrify(filter_bandwidth),
+	attrify(disable),
+	attrify(burst_count),
+	attrify(stretch_duration),
+	NULL
+};
+
+static struct attribute *attrs_reg_20[] = {
+	attrify(disable_noise_mitigation),
+	NULL
+};
+
+static struct attribute *attrs_reg_21[] = {
+	attrify(freq_shift_noise_threshold),
+	NULL
+};
+
+static struct attribute *attrs_reg_22__26[] = {
+	/*attrify(noise_density_threshold),*/
+	attrify(medium_noise_threshold),
+	attrify(high_noise_threshold),
+	attrify(noise_density),
+	attrify(frame_count),
+	NULL
+};
+
+static struct attribute *attrs_reg_27[] = {
+	attrify(iir_filter_coef),
+	NULL
+};
+
+static struct attribute *attrs_reg_28[] = {
+	attrify(quiet_threshold),
+	NULL
+};
+
+static struct attribute *attrs_reg_29[] = {
+	attrify(cmn_filter_disable),
+	NULL
+};
+
+static struct attribute *attrs_reg_30[] = {
+	attrify(cmn_filter_max),
+	NULL
+};
+
+static struct attribute *attrs_reg_31[] = {
+	attrify(touch_hysteresis),
+	NULL
+};
+
+static struct attribute *attrs_reg_32__35[] = {
+	attrify(rx_low_edge_comp),
+	attrify(rx_high_edge_comp),
+	attrify(tx_low_edge_comp),
+	attrify(tx_high_edge_comp),
+	NULL
+};
+
+static struct attribute *attrs_reg_36[] = {
+	attrify(axis1_comp),
+	NULL
+};
+
+static struct attribute *attrs_reg_37[] = {
+	attrify(axis2_comp),
+	NULL
+};
+
+static struct attribute *attrs_reg_38__40[] = {
+	attrify(noise_control_1),
+	attrify(noise_control_2),
+	attrify(noise_control_3),
+	NULL
+};
+
+static struct attribute_group attrs_ctrl_regs[] = {
+	GROUP(attrs_reg_0),
+	GROUP(attrs_reg_1),
+	GROUP(attrs_reg_2),
+	GROUP(attrs_reg_3),
+	GROUP(attrs_reg_4__6),
+	GROUP(attrs_reg_7),
+	GROUP(attrs_reg_8__9),
+	GROUP(attrs_reg_10),
+	GROUP(attrs_reg_12__13),
+	GROUP(attrs_reg_14__16),
+	GROUP(attrs_reg_17__19),
+	GROUP(attrs_reg_20),
+	GROUP(attrs_reg_21),
+	GROUP(attrs_reg_22__26),
+	GROUP(attrs_reg_27),
+	GROUP(attrs_reg_28),
+	GROUP(attrs_reg_29),
+	GROUP(attrs_reg_30),
+	GROUP(attrs_reg_31),
+	GROUP(attrs_reg_32__35),
+	GROUP(attrs_reg_36),
+	GROUP(attrs_reg_37),
+	GROUP(attrs_reg_38__40)
+};
+
+/* data specific to fn $54 that needs to be kept around */
+struct rmi_fn_54_data {
+	union f54_ad_query query;
+	struct f54_ad_control control;
+	bool attrs_ctrl_regs_exist[ARRAY_SIZE(attrs_ctrl_regs)];
+	enum f54_report_types report_type;
+	u16 fifoindex;
+	signed char status;
+	bool no_auto_cal;
+	unsigned int report_size;
+	unsigned char *report_data;
+	unsigned int bufsize;
+	struct mutex data_mutex;
+	struct mutex status_mutex;
+	struct mutex control_mutex;
+#if F54_WATCHDOG
+	struct hrtimer watchdog;
+#endif
+	struct rmi_function_container *fc;
+	struct work_struct work;
+};
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+#if F54_WATCHDOG
+static enum hrtimer_restart clear_status(struct hrtimer *timer);
+
+static void clear_status_worker(struct work_struct *work);
+#endif
+
+static int rmi_f54_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f54_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f54_initialize(struct rmi_function_container *fc);
+
+static int rmi_f54_reset(struct rmi_function_container *fc);
+
+static int rmi_f54_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f54_init(struct rmi_function_container *fc)
+{
+	int retval = 0;
+	struct rmi_fn_54_data *f54;
+
+	retval = rmi_f54_alloc_memory(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f54_initialize(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f54_create_sysfs(fc);
+	if (retval < 0)
+		goto error_exit;
+	f54 = fc->data;
+	f54->status = IDLE;
+	return retval;
+
+error_exit:
+	rmi_f54_free_memory(fc);
+
+	return retval;
+}
+
+static int rmi_f54_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_54_data *f54;
+
+	f54 = kzalloc(sizeof(struct rmi_fn_54_data), GFP_KERNEL);
+	if (!f54) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_54_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f54;
+	f54->fc = fc;
+
+
+	return 0;
+}
+
+static void rmi_f54_free_memory(struct rmi_function_container *fc)
+{
+	int reg_num;
+	struct rmi_fn_54_data *f54 = fc->data;
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+	for (reg_num = 0; reg_num < ARRAY_SIZE(attrs_ctrl_regs); reg_num++)
+		sysfs_remove_group(&fc->dev.kobj, &attrs_ctrl_regs[reg_num]);
+	sysfs_remove_bin_file(&fc->dev.kobj, &dev_rep_data);
+	if (f54) {
+		kfree(f54->report_data);
+		kfree(f54);
+		fc->data = NULL;
+	}
+}
+
+static int rmi_f54_reset(struct rmi_function_container *fc)
+{
+	struct rmi_fn_54_data *data = fc->data;
+	struct rmi_driver *driver = fc->rmi_dev->driver;
+
+#if F54_WATCHDOG
+	hrtimer_cancel(&data->watchdog);
+#endif
+
+	mutex_lock(&data->status_mutex);
+	if (driver->restore_irq_mask) {
+		dev_dbg(&fc->dev, "Restoring interupts!\n");
+		driver->restore_irq_mask(fc->rmi_dev);
+	} else {
+		dev_err(&fc->dev, "No way to restore interrupts!\n");
+	}
+	data->status = -ECONNRESET;
+	mutex_unlock(&data->status_mutex);
+
+	return 0;
+}
+
+static void rmi_f54_remove(struct rmi_function_container *fc)
+{
+	struct rmi_fn_54_data *data = fc->data;
+
+	dev_info(&fc->dev, "Removing F54.");
+
+	#if F54_WATCHDOG
+	/* Stop timer */
+	hrtimer_cancel(&data->watchdog);
+	#endif
+
+	rmi_f54_free_memory(fc);
+}
+
+static int rmi_f54_create_sysfs(struct rmi_function_container *fc)
+{
+	int reg_num;
+	int retval;
+	struct rmi_fn_54_data *f54 = fc->data;
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+	/* Set up sysfs device attributes. */
+
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+	for (reg_num = 0; reg_num < ARRAY_SIZE(attrs_ctrl_regs); reg_num++) {
+		if (f54->attrs_ctrl_regs_exist[reg_num]) {
+			retval = sysfs_create_group(&fc->dev.kobj,
+					&attrs_ctrl_regs[reg_num]);
+			if (retval < 0) {
+				dev_err(&fc->dev, "Failed to create sysfs file group for reg group %d, error = %d.",
+							reg_num, retval);
+				return -ENODEV;
+			}
+		}
+	}
+
+	/* Binary sysfs file to report the data back */
+	retval = sysfs_create_bin_file(&fc->dev.kobj, &dev_rep_data);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to create sysfs file for F54 data (error = %d).\n",
+				retval);
+		return -ENODEV;
+	}
+	return 0;
+}
+
+
+
+static int rmi_f54_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_fn_54_data *f54 = fc->data;
+	struct f54_ad_control *control;
+	int retval = 0;
+	u8 size = 0;
+	u16 next_loc;
+	u8 reg_num;
+
+#if F54_WATCHDOG
+	/* Set up watchdog timer to catch unanswered get_report commands */
+	hrtimer_init(&f54->watchdog, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	f54->watchdog.function = clear_status;
+
+	/* work function to do unlocking */
+	INIT_WORK(&f54->work, clear_status_worker);
+#endif
+
+	/* Read F54 Query Data */
+	retval = rmi_read_block(fc->rmi_dev, fc->fd.query_base_addr,
+		(u8 *)&f54->query, sizeof(f54->query));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read query registers from 0x%04x\n",
+				fc->fd.query_base_addr);
+		return retval;
+	}
+
+	/* Initialize the control registers */
+	next_loc = fc->fd.control_base_addr;
+	reg_num = 0;
+	control = &f54->control;
+
+	f54->attrs_ctrl_regs_exist[reg_num] = true;
+	reg_num++;
+	control->reg_0 = kzalloc(sizeof(union f54_ad_control_0), GFP_KERNEL);
+	if (!control->reg_0) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_0->address = next_loc;
+	next_loc += sizeof(control->reg_0->regs);
+
+	if (f54->query.touch_controller_family == 0
+			|| f54->query.touch_controller_family == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_1 = kzalloc(sizeof(union f54_ad_control_1),
+								GFP_KERNEL);
+		if (!control->reg_1) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_1->address = next_loc;
+		next_loc += sizeof(control->reg_1->regs);
+	}
+	reg_num++;
+
+	f54->attrs_ctrl_regs_exist[reg_num] = true;
+	reg_num++;
+	control->reg_2 = kzalloc(sizeof(union f54_ad_control_2), GFP_KERNEL);
+	if (!control->reg_2) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_2->address = next_loc;
+	next_loc += sizeof(control->reg_2->regs);
+
+	if (f54->query.has_pixel_touch_threshold_adjustment == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+
+		control->reg_3 = kzalloc(sizeof(union f54_ad_control_3),
+								GFP_KERNEL);
+		if (!control->reg_3) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_3->address = next_loc;
+		next_loc += sizeof(control->reg_3->regs);
+	}
+	reg_num++;
+
+	if (f54->query.touch_controller_family == 0
+		|| f54->query.touch_controller_family == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_4__6 = kzalloc(sizeof(union f54_ad_control_4__6),
+								GFP_KERNEL);
+		if (!control->reg_4__6) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_4__6->address = next_loc;
+		next_loc += sizeof(control->reg_4__6->regs);
+	}
+	reg_num++;
+
+	if (f54->query.touch_controller_family == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_7 = kzalloc(sizeof(union f54_ad_control_7),
+								GFP_KERNEL);
+		if (!control->reg_7) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_7->address = next_loc;
+		next_loc += sizeof(control->reg_7->regs);
+	}
+	reg_num++;
+
+	if (f54->query.touch_controller_family == 0
+		|| f54->query.touch_controller_family == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_8__9 = kzalloc(sizeof(union f54_ad_control_8__9),
+								GFP_KERNEL);
+		if (!control->reg_8__9) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_8__9->address = next_loc;
+		next_loc += sizeof(control->reg_8__9->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_interference_metric == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_10 = kzalloc(sizeof(union f54_ad_control_10),
+								GFP_KERNEL);
+		if (!control->reg_10) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_10->address = next_loc;
+		next_loc += sizeof(control->reg_10->regs);
+	}
+	reg_num++;
+
+	/* F54 Control Register 11 is reserved */
+	next_loc++;
+
+	if (f54->query.has_relaxation_control == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_12__13 = kzalloc(
+			sizeof(union f54_ad_control_12__13), GFP_KERNEL);
+		if (!control->reg_12__13) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_12__13->address = next_loc;
+		next_loc += sizeof(control->reg_12__13->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_sensor_assignment == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_14 = kzalloc(sizeof(union f54_ad_control_14),
+								GFP_KERNEL);
+		if (!control->reg_14) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_15 =
+			kzalloc(sizeof(struct f54_ad_control_15), GFP_KERNEL);
+		if (!control->reg_15) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_15->length = f54->query.num_of_rx_electrodes;
+		control->reg_15->regs =
+				kzalloc(control->reg_15->length *
+					sizeof(struct f54_ad_control_15n),
+					GFP_KERNEL);
+		if (!control->reg_15->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_16 =
+			kzalloc(sizeof(struct f54_ad_control_16), GFP_KERNEL);
+		if (!control->reg_16) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_16->length = f54->query.num_of_tx_electrodes;
+		control->reg_16->regs =
+				kzalloc(control->reg_16->length *
+					sizeof(struct f54_ad_control_16n),
+					GFP_KERNEL);
+		if (!control->reg_16->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_14->address = next_loc;
+		next_loc += sizeof(control->reg_14->regs);
+		control->reg_15->address = next_loc;
+		next_loc += control->reg_15->length;
+		control->reg_16->address = next_loc;
+		next_loc += control->reg_16->length;
+	}
+	reg_num++;
+
+	if (f54->query.has_sense_frequency_control == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		size = f54->query.number_of_sensing_frequencies;
+
+		control->reg_17 =
+			kzalloc(sizeof(struct f54_ad_control_17), GFP_KERNEL);
+		if (!control->reg_17) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_17->length = size;
+		control->reg_17->regs = kzalloc(size *
+				sizeof(struct f54_ad_control_17n), GFP_KERNEL);
+		if (!control->reg_17->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_18 =
+			kzalloc(sizeof(struct f54_ad_control_18), GFP_KERNEL);
+		if (!control->reg_18) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_18->length = size;
+		control->reg_18->regs = kzalloc(size *
+				sizeof(struct f54_ad_control_18n), GFP_KERNEL);
+		if (!control->reg_18->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_19 =
+			kzalloc(sizeof(struct f54_ad_control_19), GFP_KERNEL);
+		if (!control->reg_19) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_19->length = size;
+		control->reg_19->regs = kzalloc(size *
+				sizeof(struct f54_ad_control_19n), GFP_KERNEL);
+		if (!control->reg_19->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_17->address = next_loc;
+		next_loc += size;
+		control->reg_18->address = next_loc;
+		next_loc += size;
+		control->reg_19->address = next_loc;
+		next_loc += size;
+	}
+	reg_num++;
+
+	f54->attrs_ctrl_regs_exist[reg_num] = true;
+	control->reg_20 = kzalloc(sizeof(union f54_ad_control_20), GFP_KERNEL);
+	control->reg_20->address = next_loc;
+	next_loc += sizeof(control->reg_20->regs);
+	reg_num++;
+
+	if (f54->query.has_sense_frequency_control == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_21 = kzalloc(sizeof(union f54_ad_control_21),
+								GFP_KERNEL);
+		control->reg_21->address = next_loc;
+		next_loc += sizeof(control->reg_21->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_sense_frequency_control == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_22__26 = kzalloc(
+			sizeof(union f54_ad_control_22__26), GFP_KERNEL);
+		control->reg_22__26->address = next_loc;
+		next_loc += sizeof(control->reg_22__26->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_iir_filter == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_27 = kzalloc(sizeof(union f54_ad_control_27),
+								GFP_KERNEL);
+		control->reg_27->address = next_loc;
+		next_loc += sizeof(control->reg_27->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_firmware_noise_mitigation == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_28 = kzalloc(sizeof(union f54_ad_control_28),
+								GFP_KERNEL);
+		control->reg_28->address = next_loc;
+		next_loc += sizeof(control->reg_28->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_cmn_removal == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_29 = kzalloc(sizeof(union f54_ad_control_29),
+								GFP_KERNEL);
+		control->reg_29->address = next_loc;
+		next_loc += sizeof(control->reg_29->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_cmn_maximum == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_30 = kzalloc(sizeof(union f54_ad_control_30),
+								GFP_KERNEL);
+		control->reg_30->address = next_loc;
+		next_loc += sizeof(control->reg_30->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_touch_hysteresis == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_31 = kzalloc(sizeof(union f54_ad_control_31),
+								GFP_KERNEL);
+		control->reg_31->address = next_loc;
+		next_loc += sizeof(control->reg_31->regs);
+	}
+	reg_num++;
+
+	if (f54->query.has_interference_metric == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+		control->reg_32__35 = kzalloc(
+			sizeof(union f54_ad_control_32__35), GFP_KERNEL);
+		control->reg_32__35->address = next_loc;
+		next_loc += sizeof(control->reg_32__35->regs);
+	}
+	reg_num++;
+
+	if (f54->query.curve_compensation_mode == 1) {
+		size = max(f54->query.num_of_rx_electrodes,
+				f54->query.num_of_tx_electrodes);
+	}
+	if (f54->query.curve_compensation_mode == 2)
+		size = f54->query.num_of_rx_electrodes;
+	if (f54->query.curve_compensation_mode == 1
+			|| f54->query.curve_compensation_mode == 2) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+
+		control->reg_36 =
+			kzalloc(sizeof(struct f54_ad_control_36), GFP_KERNEL);
+		if (!control->reg_36) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_36->length = size;
+		control->reg_36->regs =
+			kzalloc(size * sizeof(struct f54_ad_control_36n),
+				GFP_KERNEL);
+		if (!control->reg_36->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_36->address = next_loc;
+		next_loc += size;
+	}
+	reg_num++;
+
+	if (f54->query.curve_compensation_mode == 2) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+
+		control->reg_37 =
+			kzalloc(sizeof(struct f54_ad_control_37), GFP_KERNEL);
+		if (!control->reg_37) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_37->length = f54->query.num_of_tx_electrodes;
+		control->reg_37->regs = kzalloc(control->reg_37->length *
+				sizeof(struct f54_ad_control_37n),
+				GFP_KERNEL);
+		if (!control->reg_37->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_37->address = next_loc;
+		next_loc += control->reg_37->length;
+	}
+	reg_num++;
+
+	if (f54->query.has_per_frequency_noise_control == 1) {
+		f54->attrs_ctrl_regs_exist[reg_num] = true;
+
+		control->reg_38 =
+			kzalloc(sizeof(struct f54_ad_control_38), GFP_KERNEL);
+		if (!control->reg_38) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_38->length =
+				f54->query.number_of_sensing_frequencies;
+		control->reg_38->regs = kzalloc(control->reg_38->length *
+					sizeof(struct f54_ad_control_38n),
+					GFP_KERNEL);
+		if (!control->reg_38->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_39 =
+			kzalloc(sizeof(struct f54_ad_control_39), GFP_KERNEL);
+		if (!control->reg_39) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_39->length =
+				f54->query.number_of_sensing_frequencies;
+		control->reg_39->regs = kzalloc(control->reg_39->length *
+					sizeof(struct f54_ad_control_39n),
+					GFP_KERNEL);
+		if (!control->reg_39->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_40 =
+			kzalloc(sizeof(struct f54_ad_control_40), GFP_KERNEL);
+		if (!control->reg_40) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		control->reg_40->length =
+				f54->query.number_of_sensing_frequencies;
+		control->reg_40->regs = kzalloc(control->reg_40->length *
+				sizeof(struct f54_ad_control_40n), GFP_KERNEL);
+		if (!control->reg_40->regs) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+
+		control->reg_38->address = next_loc;
+		next_loc += control->reg_38->length;
+		control->reg_39->address = next_loc;
+		next_loc += control->reg_39->length;
+		control->reg_40->address = next_loc;
+		next_loc += control->reg_40->length;
+	}
+	reg_num++;
+
+	mutex_init(&f54->data_mutex);
+
+	mutex_init(&f54->status_mutex);
+
+	mutex_init(&f54->control_mutex);
+
+	return retval;
+}
+
+static void set_report_size(struct rmi_fn_54_data *data)
+{
+	u8 rx = data->query.num_of_rx_electrodes;
+	u8 tx = data->query.num_of_tx_electrodes;
+	switch (data->report_type) {
+	case F54_8BIT_IMAGE:
+		data->report_size = rx * tx;
+		break;
+	case F54_16BIT_IMAGE:
+	case F54_RAW_16BIT_IMAGE:
+	case F54_TRUE_BASELINE:
+	case F54_FULL_RAW_CAP:
+	case F54_FULL_RAW_CAP_RX_COUPLING_COMP:
+		data->report_size = 2 * rx * tx;
+		break;
+	case F54_HIGH_RESISTANCE:
+		data->report_size = RMI_54_HIGH_RESISTANCE_SIZE;
+		break;
+	case F54_FULL_RAW_CAP_MIN_MAX:
+		data->report_size = RMI_54_FULL_RAW_CAP_MIN_MAX_SIZE;
+		break;
+	case F54_TX_TO_TX_SHORT:
+	case F54_TX_OPEN:
+	case F54_TX_TO_GROUND:
+		data->report_size =  (tx + 7) / 8;
+		break;
+	case F54_RX_TO_RX1:
+	case F54_RX_OPENS1:
+		if (rx < tx)
+			data->report_size = 2 * rx * rx;
+		else
+			data->report_size = 2 * rx * tx;
+		break;
+	case F54_RX_TO_RX2:
+	case F54_RX_OPENS2:
+		if (rx <= tx)
+			data->report_size = 0;
+		else
+			data->report_size = 2 * rx * (rx - tx);
+		break;
+	default:
+		data->report_size = 0;
+	}
+}
+
+int rmi_f54_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_driver *driver = fc->rmi_dev->driver;
+	char fifo[2];
+	struct rmi_fn_54_data *data = fc->data;
+	int error = 0;
+
+	set_report_size(data);
+	if (data->report_size == 0) {
+		dev_err(&fc->dev, "Invalid report type set in %s. "
+				"This should never happen.\n", __func__);
+		error = -EINVAL;
+		goto error_exit;
+	}
+	/*
+	 * We need to ensure the buffer is big enough. A Buffer size of 0 means
+	 * that the buffer has not been allocated.
+	 */
+	if (data->bufsize < data->report_size) {
+		mutex_lock(&data->data_mutex);
+		if (data->bufsize > 0)
+			kfree(data->report_data);
+		data->report_data = kzalloc(data->report_size, GFP_KERNEL);
+		if (!data->report_data) {
+			dev_err(&fc->dev, "Failed to allocate report_data.\n");
+			error = -ENOMEM;
+			data->bufsize = 0;
+			mutex_unlock(&data->data_mutex);
+			goto error_exit;
+		}
+		data->bufsize = data->report_size;
+		mutex_unlock(&data->data_mutex);
+	}
+	dev_vdbg(&fc->dev, "F54 Interrupt handler is running.\nSize: %d\n",
+		 data->report_size);
+	/* Write 0 to fifohi and fifolo. */
+	fifo[0] = 0;
+	fifo[1] = 0;
+	error = rmi_write_block(fc->rmi_dev, fc->fd.data_base_addr
+				+ RMI_F54_FIFO_OFFSET, fifo,	sizeof(fifo));
+	if (error < 0)
+		dev_err(&fc->dev, "Failed to write fifo to zero!\n");
+	else
+		error = rmi_read_block(fc->rmi_dev,
+			fc->fd.data_base_addr + RMI_F54_REPORT_DATA_OFFSET,
+			data->report_data, data->report_size);
+	if (error < 0)
+		dev_err(&fc->dev, "F54 data read failed. Code: %d.\n", error);
+	else if (error != data->report_size) {
+		error = -EINVAL;
+		goto error_exit;
+	}
+#if RAW_HEX
+	int l;
+	/* Debugging: Print out the file in hex. */
+	pr_info("Report data (raw hex):\n");
+	for (l = 0; l < data->report_size; l += 2) {
+		pr_info("%03d: 0x%02x%02x\n", l/2,
+			data->report_data[l+1], data->report_data[l]);
+	}
+#endif
+#if HUMAN_READABLE
+	/* Debugging: Print out file in human understandable image */
+	switch (data->report_type) {
+	case F54_16BIT_IMAGE:
+	case F54_RAW_16BIT_IMAGE:
+	case F54_TRUE_BASELINE:
+	case F54_FULL_RAW_CAP:
+	case F54_FULL_RAW_CAP_RX_COUPLING_COMP:
+		pr_info("Report data (Image):\n");
+		int i, j, k;
+		char c[2];
+		short s;
+		k = 0;
+		for (i = 0; i < data->query.num_of_tx_electrodes;
+									i++) {
+			for (j = 0; j <
+			     data->query.num_of_rx_electrodes; j++) {
+				c[0] = data->report_data[k];
+				c[1] = data->report_data[k+1];
+				memcpy(&s, &c, 2);
+				if (s < -64)
+					printk(".");
+				else if (s < 0)
+					printk("-");
+				else if (s > 64)
+					printk("*");
+				else if (s > 0)
+					printk("+");
+				else
+					printk("0");
+				k += 2;
+			}
+			pr_info("\n");
+		}
+		pr_info("EOF\n");
+		break;
+	default:
+		pr_info("Report type %d debug image not supported",
+							data->report_type);
+	}
+#endif
+	error = IDLE;
+error_exit:
+	mutex_lock(&data->status_mutex);
+	/* Turn back on other interupts, if it
+	 * appears that we turned them off. */
+	if (driver->restore_irq_mask) {
+		dev_dbg(&fc->dev, "Restoring interupts!\n");
+		driver->restore_irq_mask(fc->rmi_dev);
+	} else {
+		dev_err(&fc->dev, "No way to restore interrupts!\n");
+	}
+	data->status = error;
+	mutex_unlock(&data->status_mutex);
+	return data->status;
+}
+
+
+#if F54_WATCHDOG
+static void clear_status_worker(struct work_struct *work)
+{
+	struct rmi_fn_54_data *data = container_of(work,
+					struct rmi_fn_54_data, work);
+	struct rmi_function_container *fc = data->fc;
+	struct rmi_driver *driver = fc->rmi_dev->driver;
+	char command;
+	int result;
+
+	mutex_lock(&data->status_mutex);
+	if (data->status == BUSY) {
+		result = rmi_read_block(fc->rmi_dev, fc->fd.command_base_addr,
+								&command, 1);
+		if (result < 0) {
+			dev_err(&fc->dev, "Could not read get_report register from %#06x.\n",
+						fc->fd.command_base_addr);
+			data->status = -ETIMEDOUT;
+		} else {
+			if (command & GET_REPORT) {
+				dev_warn(&fc->dev, "Report type unsupported!");
+				data->status = -EINVAL;
+			} else {
+				data->status = -ETIMEDOUT;
+			}
+		}
+		if (driver->restore_irq_mask) {
+			dev_dbg(&fc->dev, "Restoring interupts!\n");
+			driver->restore_irq_mask(fc->rmi_dev);
+		} else {
+			dev_err(&fc->dev, "No way to restore interrupts!\n");
+		}
+	}
+	mutex_unlock(&data->status_mutex);
+}
+
+static enum hrtimer_restart clear_status(struct hrtimer *timer)
+{
+	struct rmi_fn_54_data *data = container_of(timer,
+					struct rmi_fn_54_data, watchdog);
+	schedule_work(&(data->work));
+	return HRTIMER_NORESTART;
+}
+#endif
+
+/* Check if report_type is valid */
+static bool is_report_type_valid(enum f54_report_types reptype)
+{
+	/* Basic checks on report_type to ensure we write a valid type
+	 * to the sensor.
+	 * TODO: Check Query3 to see if some specific reports are
+	 * available. This is currently listed as a reserved register.
+	 */
+	switch (reptype) {
+	case F54_8BIT_IMAGE:
+	case F54_16BIT_IMAGE:
+	case F54_RAW_16BIT_IMAGE:
+	case F54_HIGH_RESISTANCE:
+	case F54_TX_TO_TX_SHORT:
+	case F54_RX_TO_RX1:
+	case F54_TRUE_BASELINE:
+	case F54_FULL_RAW_CAP_MIN_MAX:
+	case F54_RX_OPENS1:
+	case F54_TX_OPEN:
+	case F54_TX_TO_GROUND:
+	case F54_RX_TO_RX2:
+	case F54_RX_OPENS2:
+	case F54_FULL_RAW_CAP:
+	case F54_FULL_RAW_CAP_RX_COUPLING_COMP:
+		return true;
+		break;
+	default:
+		return false;
+	}
+}
+
+/* SYSFS file show/store functions */
+static ssize_t rmi_fn_54_report_type_show(struct device *dev,
+				struct device_attribute *attr, char *buf) {
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *f54;
+
+	fc = to_rmi_function_container(dev);
+	f54 = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", f54->report_type);
+}
+
+static ssize_t rmi_fn_54_report_type_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count) {
+	int result;
+	unsigned long val;
+	unsigned char data;
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	/* need to convert the string data to an actual value */
+	result = strict_strtoul(buf, 10, &val);
+	if (result)
+		return result;
+	if (!is_report_type_valid(val)) {
+		dev_err(dev, "%s : Report type %d is invalid.\n",
+					__func__, (u8) val);
+		return -EINVAL;
+	}
+	mutex_lock(&instance_data->status_mutex);
+	if (instance_data->status != BUSY) {
+		instance_data->report_type = (enum f54_report_types)val;
+		data = (char)val;
+		/* Write the Report Type back to the first Block
+		 * Data registers (F54_AD_Data0). */
+		result =
+		    rmi_write_block(fc->rmi_dev, fc->fd.data_base_addr,
+								&data, 1);
+		mutex_unlock(&instance_data->status_mutex);
+		if (result < 0) {
+			dev_err(dev, "%s : Could not write report type to"
+				" 0x%x\n", __func__, fc->fd.data_base_addr);
+			return result;
+		}
+		return count;
+	} else {
+		dev_err(dev, "%s : Report type cannot be changed in the middle"
+				" of command.\n", __func__);
+		mutex_unlock(&instance_data->status_mutex);
+		return -EINVAL;
+	}
+}
+
+static ssize_t rmi_fn_54_get_report_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count) {
+	unsigned long val;
+	int error, result;
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+	struct rmi_driver *driver;
+	u8 command;
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	driver = fc->rmi_dev->driver;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+	if (error)
+		return error;
+	/* Do nothing if not set to 1. This prevents accidental commands. */
+	if (val != 1)
+		return count;
+	command = (unsigned char)GET_REPORT;
+	/* Basic checks on report_type to ensure we write a valid type
+	 * to the sensor.
+	 * TODO: Check Query3 to see if some specific reports are
+	 * available. This is currently listed as a reserved register.
+	 */
+	if (!is_report_type_valid(instance_data->report_type)) {
+		dev_err(dev, "%s : Report type %d is invalid.\n",
+				__func__, instance_data->report_type);
+		return -EINVAL;
+	}
+	mutex_lock(&instance_data->status_mutex);
+	if (instance_data->status != IDLE) {
+		if (instance_data->status != BUSY) {
+			dev_err(dev, "F54 status is in an abnormal state: 0x%x",
+							instance_data->status);
+		}
+		mutex_unlock(&instance_data->status_mutex);
+		return count;
+	}
+	/* Store interrupts */
+	/* Do not exit if we fail to turn off interupts. We are likely
+	 * to still get useful data. The report data can, however, be
+	 * corrupted, and there may be unexpected behavior.
+	 */
+	dev_dbg(dev, "Storing and overriding interupts\n");
+	if (driver->store_irq_mask)
+		driver->store_irq_mask(fc->rmi_dev,
+					fc->irq_mask);
+	else
+		dev_err(dev, "No way to store interupts!\n");
+	instance_data->status = BUSY;
+
+	/* small delay to avoid race condition in firmare. This value is a bit
+	 * higher than absolutely necessary. Should be removed once issue is
+	 * resolved in firmware. */
+
+	mdelay(2);
+
+	/* Write the command to the command register */
+	result = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
+						&command, 1);
+	mutex_unlock(&instance_data->status_mutex);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write command to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return result;
+	}
+#if F54_WATCHDOG
+	/* start watchdog timer */
+	hrtimer_start(&instance_data->watchdog, ktime_set(1, 0),
+							HRTIMER_MODE_REL);
+#endif
+	return count;
+}
+
+static ssize_t rmi_fn_54_force_cal_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count) {
+	unsigned long val;
+	int error, result;
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+	struct rmi_driver *driver;
+	u8 command;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	driver = fc->rmi_dev->driver;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+	if (error)
+		return error;
+	/* Do nothing if not set to 1. This prevents accidental commands. */
+	if (val != 1)
+		return count;
+
+	command = (unsigned char)FORCE_CAL;
+
+	if (instance_data->status == BUSY)
+		return -EBUSY;
+	/* Write the command to the command register */
+	result = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
+						&command, 1);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write command to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return result;
+	}
+	return count;
+}
+
+static ssize_t rmi_fn_54_status_show(struct device *dev,
+				struct device_attribute *attr, char *buf) {
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n", instance_data->status);
+}
+
+simple_show_union_struct_unsigned(query, num_of_rx_electrodes)
+simple_show_union_struct_unsigned(query, num_of_tx_electrodes)
+simple_show_union_struct_unsigned(query, has_image16)
+simple_show_union_struct_unsigned(query, has_image8)
+simple_show_union_struct_unsigned(query, has_baseline)
+simple_show_union_struct_unsigned(query, clock_rate)
+simple_show_union_struct_unsigned(query, touch_controller_family)
+simple_show_union_struct_unsigned(query, has_pixel_touch_threshold_adjustment)
+simple_show_union_struct_unsigned(query, has_sensor_assignment)
+simple_show_union_struct_unsigned(query, has_interference_metric)
+simple_show_union_struct_unsigned(query, has_sense_frequency_control)
+simple_show_union_struct_unsigned(query, has_firmware_noise_mitigation)
+simple_show_union_struct_unsigned(query, has_two_byte_report_rate)
+simple_show_union_struct_unsigned(query, has_one_byte_report_rate)
+simple_show_union_struct_unsigned(query, has_relaxation_control)
+simple_show_union_struct_unsigned(query, curve_compensation_mode)
+simple_show_union_struct_unsigned(query, has_iir_filter)
+simple_show_union_struct_unsigned(query, has_cmn_removal)
+simple_show_union_struct_unsigned(query, has_cmn_maximum)
+simple_show_union_struct_unsigned(query, has_touch_hysteresis)
+simple_show_union_struct_unsigned(query, has_edge_compensation)
+simple_show_union_struct_unsigned(query, has_per_frequency_noise_control)
+simple_show_union_struct_unsigned(query, number_of_sensing_frequencies)
+
+static ssize_t rmi_fn_54_no_auto_cal_show(struct device *dev,
+				struct device_attribute *attr, char *buf) {
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+				data->no_auto_cal ? 1 : 0);
+}
+
+static ssize_t rmi_fn_54_no_auto_cal_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count) {
+	int result;
+	unsigned long val;
+	unsigned char data;
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	/* need to convert the string data to an actual value */
+	result = strict_strtoul(buf, 10, &val);
+
+	/* if an error occured, return it */
+	if (result)
+		return result;
+	/* Do nothing if not 0 or 1. This prevents accidental commands. */
+	if (val > 1)
+		return -EINVAL;
+	/* Read current control values */
+	result =
+	    rmi_read_block(fc->rmi_dev, fc->fd.control_base_addr, &data, 1);
+
+	/* if the current control registers are already set as we want them, do
+	 * nothing to them */
+	if ((data & NO_AUTO_CAL_MASK) == val)
+		return count;
+	/* Write the control back to the control register (F54_AD_Ctrl0)
+	 * Ignores everything but bit 0 */
+	data = (data & ~NO_AUTO_CAL_MASK) | (val & NO_AUTO_CAL_MASK);
+	result =
+	    rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr, &data, 1);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write control to 0x%x\n",
+		       __func__, fc->fd.control_base_addr);
+		return result;
+	}
+	/* update our internal representation iff the write succeeds */
+	instance_data->no_auto_cal = (val == 1);
+	return count;
+}
+
+static ssize_t rmi_fn_54_fifoindex_show(struct device *dev,
+				  struct device_attribute *attr, char *buf) {
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+	struct rmi_driver *driver;
+	unsigned char temp_buf[2];
+	int retval;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	driver = fc->rmi_dev->driver;
+
+	/* Read fifoindex from device */
+	retval = rmi_read_block(fc->rmi_dev,
+				fc->fd.data_base_addr + RMI_F54_FIFO_OFFSET,
+				temp_buf, ARRAY_SIZE(temp_buf));
+
+	if (retval < 0) {
+		dev_err(dev, "Could not read fifoindex from 0x%04x\n",
+		       fc->fd.data_base_addr + RMI_F54_FIFO_OFFSET);
+		return retval;
+	}
+	batohs(&instance_data->fifoindex, temp_buf);
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->fifoindex);
+}
+static ssize_t rmi_fn_54_fifoindex_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf,
+					size_t count)
+{
+	int error;
+	unsigned long val;
+	unsigned char data[2];
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+
+	if (error)
+		return error;
+
+	instance_data->fifoindex = val;
+
+	/* Write the FifoIndex back to the first data registers. */
+	hstoba(data, (unsigned short)val);
+
+	error = rmi_write_block(fc->rmi_dev,
+				fc->fd.data_base_addr + RMI_F54_FIFO_OFFSET,
+				data,
+				ARRAY_SIZE(data));
+
+	if (error < 0) {
+		dev_err(dev, "%s : Could not write fifoindex to 0x%x\n",
+		       __func__, fc->fd.data_base_addr + RMI_F54_FIFO_OFFSET);
+		return error;
+	}
+	return count;
+}
+
+/* Provide access to last report */
+static ssize_t rmi_fn_54_data_read(struct file *data_file, struct kobject *kobj,
+				struct bin_attribute *attributes,
+				char *buf, loff_t pos, size_t count)
+{
+	struct device *dev;
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *instance_data;
+
+	dev = container_of(kobj, struct device, kobj);
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	mutex_lock(&instance_data->data_mutex);
+	if (count < instance_data->report_size) {
+		dev_err(dev,
+			"%s: F54 report size too large for buffer: %d."
+				" Need at least: %d for Report type: %d.\n",
+			__func__, count, instance_data->report_size,
+			instance_data->report_type);
+		mutex_unlock(&instance_data->data_mutex);
+		return -EINVAL;
+	}
+	if (instance_data->report_data) {
+		/* Copy data from instance_data to buffer */
+		memcpy(buf, instance_data->report_data,
+					instance_data->report_size);
+		mutex_unlock(&instance_data->data_mutex);
+		dev_dbg(dev, "%s: Presumably successful.", __func__);
+		return instance_data->report_size;
+	} else {
+		dev_err(dev, "%s: F54 report_data does not exist!\n", __func__);
+		mutex_unlock(&instance_data->data_mutex);
+		return -EINVAL;
+	}
+}
+
+/* Repeated Register sysfs functions */
+show_repeated_union_struct_unsigned(control, reg_15, sensor_rx_assignment)
+show_repeated_union_struct_unsigned(control, reg_16, sensor_tx_assignment)
+
+show_repeated_union_struct_unsigned(control, reg_17, filter_bandwidth)
+show_repeated_union_struct_unsigned(control, reg_17, disable)
+
+
+static ssize_t rmi_fn_54_burst_count_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf) {
+	struct rmi_function_container *fc;
+	struct rmi_fn_54_data *data;
+	int result, size = 0;
+	char *temp;
+	int i;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	mutex_lock(&data->control_mutex);
+	/* Read current control values */
+	result = rmi_read_block(fc->rmi_dev, data->control.reg_17->address,
+			(u8 *) data->control.reg_17->regs,
+			data->control.reg_17->length * sizeof(u8));
+	if (result < 0) {
+		dev_err(dev, "%s : Could not read control at 0x%x\n"
+					"Data may be outdated.", __func__,
+					data->control.reg_17->address);
+	}
+
+	result = rmi_read_block(fc->rmi_dev, data->control.reg_18->address,
+			(u8 *)data->control.reg_18->regs,
+			data->control.reg_18->length * sizeof(u8));
+	if (result < 0) {
+		dev_err(dev, "%s : Could not read control at 0x%x\n"
+					"Data may be outdated.", __func__,
+					data->control.reg_18->address);
+	}
+	mutex_unlock(&data->control_mutex);
+	temp = buf;
+	for (i = 0; i < data->control.reg_17->length; i++) {
+		result = snprintf(temp, PAGE_SIZE - size, "%u ", (1<<8) *
+			data->control.reg_17->regs[i].burst_countb10__8 +
+			data->control.reg_18->regs[i].burst_countb7__0n);
+		size += result;
+		temp += result;
+	}
+	return size + snprintf(temp, PAGE_SIZE - size, "\n");
+}
+
+show_repeated_union_struct_unsigned(control, reg_19, stretch_duration)
+show_store_repeated_union_struct_unsigned(control, reg_36, axis1_comp)
+show_store_repeated_union_struct_unsigned(control, reg_37, axis2_comp)
+
+show_repeated_union_struct_unsigned(control, reg_38, noise_control_1)
+show_repeated_union_struct_unsigned(control, reg_39, noise_control_2)
+show_repeated_union_struct_unsigned(control, reg_40, noise_control_3)
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
+show_store_union_struct_unsigned(control, reg_0, no_relax)
+show_store_union_struct_unsigned(control, reg_0, no_scan)
+show_store_union_struct_unsigned(control, reg_1, bursts_per_cluster)
+show_store_union_struct_unsigned(control, reg_2, saturation_cap)
+show_store_union_struct_unsigned(control, reg_3, pixel_touch_threshold)
+show_store_union_struct_unsigned(control, reg_4__6, rx_feedback_cap)
+show_store_union_struct_unsigned(control, reg_4__6, low_ref_cap)
+show_store_union_struct_unsigned(control, reg_4__6, low_ref_feedback_cap)
+show_store_union_struct_unsigned(control, reg_4__6, low_ref_polarity)
+show_store_union_struct_unsigned(control, reg_4__6, high_ref_cap)
+show_store_union_struct_unsigned(control, reg_4__6, high_ref_feedback_cap)
+show_store_union_struct_unsigned(control, reg_4__6, high_ref_polarity)
+show_store_union_struct_unsigned(control, reg_7, cbc_cap)
+show_store_union_struct_unsigned(control, reg_7, cbc_polarity)
+show_store_union_struct_unsigned(control, reg_7, cbc_tx_carrier_selection)
+show_store_union_struct_unsigned(control, reg_8__9, integration_duration)
+show_store_union_struct_unsigned(control, reg_8__9, reset_duration)
+show_store_union_struct_unsigned(control, reg_10,
+						noise_sensing_bursts_per_image)
+show_store_union_struct_unsigned(control, reg_12__13, slow_relaxation_rate)
+show_store_union_struct_unsigned(control, reg_12__13, fast_relaxation_rate)
+show_store_union_struct_unsigned(control, reg_14, rxs_on_xaxis)
+show_store_union_struct_unsigned(control, reg_14, curve_comp_on_txs)
+show_store_union_struct_unsigned(control, reg_20, disable_noise_mitigation)
+show_store_union_struct_unsigned(control, reg_21, freq_shift_noise_threshold)
+show_store_union_struct_unsigned(control, reg_22__26, medium_noise_threshold)
+show_store_union_struct_unsigned(control, reg_22__26, high_noise_threshold)
+show_store_union_struct_unsigned(control, reg_22__26, noise_density)
+show_store_union_struct_unsigned(control, reg_22__26, frame_count)
+show_store_union_struct_unsigned(control, reg_27, iir_filter_coef)
+show_store_union_struct_unsigned(control, reg_28, quiet_threshold)
+show_store_union_struct_unsigned(control, reg_29, cmn_filter_disable)
+show_store_union_struct_unsigned(control, reg_30, cmn_filter_max)
+show_store_union_struct_unsigned(control, reg_31, touch_hysteresis)
+show_store_union_struct_unsigned(control, reg_32__35, rx_low_edge_comp)
+show_store_union_struct_unsigned(control, reg_32__35, rx_high_edge_comp)
+show_store_union_struct_unsigned(control, reg_32__35, tx_low_edge_comp)
+show_store_union_struct_unsigned(control, reg_32__35, tx_high_edge_comp)
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+static struct rmi_function_handler function_handler = {
+	.func = 0x54,
+	.init = rmi_f54_init,
+	.config = NULL,
+	.reset = rmi_f54_reset,
+	.attention = rmi_f54_attention,
+	.remove = rmi_f54_remove
+};
+
+static int __init rmi_f54_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+	return 0;
+}
+
+static void rmi_f54_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+module_init(rmi_f54_module_init);
+module_exit(rmi_f54_module_exit);
+
+MODULE_AUTHOR("Daniel Rosenberg <[email protected]>");
+MODULE_DESCRIPTION("RMI F54 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#0 lore raw

cover input: Synaptics RMI4 Touchscreen Driver

This patch implements a driver supporting Synaptics ClearPad and other touchscreen sensors that use the RMI4 protocol, as defined here:

    http://www.synaptics.com/sites/default/files/511-000136-01-Rev-E-RMI4%20Intrfacing%20Guide.pdf

as well as successor documents that haven't made their way through to publication yet.

This code supersedes the patch submitted on 2011-12-21. We believe that all outstanding issues arising from the previous submissions have been addressed, except certain issues relating to the use of device_type and the binding of the individual function drivers (see below).

This patch is against the v2.6.38 tag of Linus' kernel tree, commit 521cb40b0c44418a4fd36dc633f575813d59a43d. This will be our last patch against such an old kernel version, future patches will be against 3.x kernels.

Included in this patch are:
- full support for an RMI virtual bus as a standard kernel bus

- physical layer implementations for I2C and SPI

- function implementations for the following RMI4 functions:
* F01 device control
* F09 self test (aka BIST)
* F11 multifinger pointing
* F17 pointing sticks
* F19 capacitive buttons
* F1A simple capacitive buttons
* F21 force sensing
* F30 GPIO and LED controls
* F34 device reflash
* F41 active pen pointing
* F54 analog data reporting

- character device access to the RMI4 register map via rmidev

- firmware reflash using the request_firmware mechanism

The driver supports a system having one or more RMI sensors attached to it. Most devices have just a single touch sensor, but some have more than one. An example is the Fuse concept phone, which has 4 RMI sensors in it.

Each sensor is presented as a device on the RMI logical bus (/sys/bus/rmi). Devices are named/numbered in the order they are discovered on the bus, starting with /sys/bus/rmi/devices/sensor00 for the first once, .../sensor01 for the second one, and so on.

Individual RMI functions are presented as child devices of the sensor device. For example, sensor00/fn01, sensor00/fn11, and so on. Control of an RMI function's operating parameters is implemented via sysfs.

For diagnostic purposes, the rmidev interface is provided. This implements a character device (/dev/rmi0, and so on) that provides flat file access to the RMI4 register map. For more information, see the file Documentation/input/rmidev.txt.

Note regarding device_type and functional driver matching.

Feedback on our patch submission from 2011-12 requested that we use device_type to help match drivers that worked directly with the sensor (such as rmi_dev.c) and drivers that work with individual functions (such as rmi_f11.c). We spent some effort on this request, but in the meantime the original customer requirements that led to the differentiation between the two types of driver no longer exist. As a result, we have rolled back that work, and plan to merge the rmi_dev functionality into the core of the driver, eliminating the need to support two device_types and their drivers. This will be included in our next patch submission, along with changes to how function drivers are matched functions.

We considered postponing this patch until that was done, but it's been a very long time since the last one, and we wanted to make the other work available.

Due to the large amount of code, we've broken this patch into 17 parts, as follows:
01 - public header files and documentation
02 - core sensor and bus implementation
03 - physical layer drivers
04 - Kconfigs and Makefiles
05 - rmidev
06 - firmware update
07..17 - drivers for individual RMI functions

Comments and other feedback on this driver are welcomed.

Christopher Heiny and the Synaptics RMI4 driver team

Cc Jean Delvare <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

#12 lore raw

RFC PATCH 4/17 input: RMI4 configs and makefiles

Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/Kconfig       |    2 +
 drivers/input/Makefile      |    4 +-
 drivers/input/rmi4/Kconfig  |  262 +++++++++++++++++++++++++++++++++++++++++++
 drivers/input/rmi4/Makefile |   18 +++
 4 files changed, 284 insertions(+), 2 deletions(-)
drivers/input/Kconfig +2 -0
index 1903c0f..0cdd8a5 100644
@@ -185,6 +185,8 @@ source "drivers/input/touchscreen/Kconfig"
 
 source "drivers/input/misc/Kconfig"
 
+source "drivers/input/rmi4/Kconfig"
+
 endif
 
 menu "Hardware I/O ports"
drivers/input/Makefile +2 -2
index 09614ce..cfb383e 100644
@@ -23,6 +23,6 @@ obj-$(CONFIG_INPUT_TABLET)	+= tablet/
 obj-$(CONFIG_INPUT_TOUCHSCREEN)	+= touchscreen/
 obj-$(CONFIG_INPUT_MISC)	+= misc/
 
-obj-$(CONFIG_INPUT_APMPOWER)	+= apm-power.o
+obj-$(CONFIG_INPUT_RMI4) += rmi4/
 
-obj-$(CONFIG_XEN_KBDDEV_FRONTEND)	+= xen-kbdfront.o
+obj-$(CONFIG_INPUT_APMPOWER)	+= apm-power.o
drivers/input/rmi4/Kconfig new file +262 -0

262 lines not on this page · raw message

drivers/input/rmi4/Makefile new file +18 -0
new file mode 100644
index 0000000..4a14a21
@@ -0,0 +1,18 @@
+obj-$(CONFIG_RMI4_BUS) += rmi_bus.o
+obj-$(CONFIG_RMI4_I2C) += rmi_i2c.o
+obj-$(CONFIG_RMI4_SPI) += rmi_spi.o
+obj-$(CONFIG_RMI4_SMB) += rmi_smbus.o
+obj-$(CONFIG_RMI4_GENERIC) += rmi_driver.o rmi_f01.o
+obj-$(CONFIG_RMI4_FWLIB) += rmi_fw_update.o
+obj-$(CONFIG_RMI4_F09) += rmi_f09.o
+obj-$(CONFIG_RMI4_F1A) += rmi_f1a.o
+obj-$(CONFIG_RMI4_F11) += rmi_f11.o
+obj-$(CONFIG_RMI4_F17) += rmi_f17.o
+obj-$(CONFIG_RMI4_F19) += rmi_f19.o
+obj-$(CONFIG_RMI4_F21) += rmi_f21.o
+obj-$(CONFIG_RMI4_F30) += rmi_f30.o
+obj-$(CONFIG_RMI4_F34) += rmi_f34.o
+obj-$(CONFIG_RMI4_F41) += rmi_f41.o
+obj-$(CONFIG_RMI4_F54) += rmi_f54.o
+obj-$(CONFIG_RMI4_DEV) += rmi_dev.o
+
#14 lore raw

RFC PATCH 5/17 input: rmidev character driver for RMI4 sensors

In reply to Christopher Heiny

Driver for Synaptics touchscreens using RMI4 protocol.

Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_dev.c |  448 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 448 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_dev.c new file +448 -0
new file mode 100644
index 0000000..080db55
@@ -0,0 +1,448 @@
+/*
+ * Copyright (c) 2011 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/syscalls.h>
+
+#include <linux/rmi.h>
+#include "rmi_driver.h"
+
+#define CHAR_DEVICE_NAME "rmi"
+#define DEVICE_CLASS_NAME "rmidev"
+
+#define RMI_CHAR_DEV_TMPBUF_SZ 128
+#define RMI_REG_ADDR_PAGE_SELECT 0xFF
+#define REG_ADDR_LIMIT 0xFFFF
+
+struct rmidev_data {
+	/* mutex for file operation*/
+	struct mutex file_mutex;
+	/* main char dev structure */
+	struct cdev main_dev;
+
+	/* pointer to the corresponding RMI4 device.  We use this to do */
+	/* read, write, etc. */
+	struct rmi_device *rmi_dev;
+	/* reference count */
+	int ref_count;
+
+	struct class *device_class;
+};
+
+/*store dynamically allocated major number of char device*/
+static int rmidev_major_num;
+
+
+static struct class *rmidev_device_class;
+
+
+/* file operations for RMI char device */
+
+/*
+ * rmidev_llseek: - use to setup register address
+ *
+ * @filp: file structure for seek
+ * @off: offset
+ *       if whence == SEEK_SET,
+ *       high 16 bits: page address
+ *       low 16 bits: register address
+ *
+ *       if whence == SEEK_CUR,
+ *       offset from current position
+ *
+ *       if whence == SEEK_END,
+ *       offset from END(0xFFFF)
+ *
+ * @whence: SEEK_SET , SEEK_CUR or SEEK_END
+ */
+static loff_t rmidev_llseek(struct file *filp, loff_t off, int whence)
+{
+	loff_t newpos;
+	struct rmidev_data *data = filp->private_data;
+
+	if (IS_ERR(data)) {
+		pr_err("%s: pointer of char device is invalid", __func__);
+		return -EBADF;
+	}
+
+	mutex_lock(&(data->file_mutex));
+
+	switch (whence) {
+	case SEEK_SET:
+		newpos = off;
+		break;
+
+	case SEEK_CUR:
+		newpos = filp->f_pos + off;
+		break;
+
+	case SEEK_END:
+		newpos = REG_ADDR_LIMIT + off;
+		break;
+
+	default:		/* can't happen */
+		newpos = -EINVAL;
+		goto clean_up;
+	}
+
+	if (newpos < 0 || newpos > REG_ADDR_LIMIT) {
+		dev_err(&data->rmi_dev->dev, "newpos 0x%04x is invalid.\n",
+			(unsigned int)newpos);
+		newpos = -EINVAL;
+		goto clean_up;
+	}
+
+	filp->f_pos = newpos;
+
+clean_up:
+	mutex_unlock(&(data->file_mutex));
+	return newpos;
+}
+
+/*
+ *  rmidev_read: - use to read data from RMI stream
+ *
+ *  @filp: file structure for read
+ *  @buf: user-level buffer pointer
+ *
+ *  @count: number of byte read
+ *  @f_pos: offset (starting register address)
+ *
+ *	@return number of bytes read into user buffer (buf) if succeeds
+ *          negative number if error occurs.
+ */
+static ssize_t rmidev_read(struct file *filp, char __user *buf,
+		size_t count, loff_t *f_pos)
+{
+	struct rmidev_data *data = filp->private_data;
+	ssize_t retval  = 0;
+	unsigned char tmpbuf[count+1];
+
+	/* limit offset to REG_ADDR_LIMIT-1 */
+	if (count > (REG_ADDR_LIMIT - *f_pos))
+		count = REG_ADDR_LIMIT - *f_pos;
+
+	if (count == 0)
+		return 0;
+
+	if (IS_ERR(data)) {
+		pr_err("%s: pointer of char device is invalid", __func__);
+		return -EBADF;
+	}
+
+	mutex_lock(&(data->file_mutex));
+
+	retval = rmi_read_block(data->rmi_dev, *f_pos, tmpbuf, count);
+
+	if (retval < 0)
+		goto clean_up;
+
+	if (copy_to_user(buf, tmpbuf, count))
+		retval = -EFAULT;
+	else
+		*f_pos += retval;
+
+clean_up:
+
+	mutex_unlock(&(data->file_mutex));
+
+	return retval;
+}
+
+/*
+ * rmidev_write: - use to write data into RMI stream
+ *
+ * @filep : file structure for write
+ * @buf: user-level buffer pointer contains data to be written
+ * @count: number of byte be be written
+ * @f_pos: offset (starting register address)
+ *
+ * @return number of bytes written from user buffer (buf) if succeeds
+ *         negative number if error occurs.
+ */
+static ssize_t rmidev_write(struct file *filp, const char __user *buf,
+		size_t count, loff_t *f_pos)
+{
+	struct rmidev_data *data = filp->private_data;
+	ssize_t retval  = 0;
+	unsigned char tmpbuf[count+1];
+
+	/* limit offset to REG_ADDR_LIMIT-1 */
+	if (count > (REG_ADDR_LIMIT - *f_pos))
+		count = REG_ADDR_LIMIT - *f_pos;
+
+	if (count == 0)
+		return 0;
+
+	if (IS_ERR(data)) {
+		pr_err("%s: pointer of char device is invalid", __func__);
+		return -EBADF;
+	}
+
+	if (copy_from_user(tmpbuf, buf, count))
+		return -EFAULT;
+
+	mutex_lock(&(data->file_mutex));
+
+	retval = rmi_write_block(data->rmi_dev, *f_pos, tmpbuf, count);
+
+	if (retval >= 0)
+		*f_pos += count;
+
+	mutex_unlock(&(data->file_mutex));
+
+	return retval;
+}
+
+/*
+ * rmidev_open: - get a new handle for from RMI stream
+ * @inp : inode struture
+ * @filp: file structure for read/write
+ *
+ * @return 0 if succeeds
+ */
+static int rmidev_open(struct inode *inp, struct file *filp)
+{
+	struct rmidev_data *data = container_of(inp->i_cdev,
+			struct rmidev_data, main_dev);
+	int retval = 0;
+
+	filp->private_data = data;
+
+	if (!data->rmi_dev)
+		return -EACCES;
+
+	mutex_lock(&(data->file_mutex));
+	if (data->ref_count < 1)
+		data->ref_count++;
+	else
+		retval = -EACCES;
+
+	mutex_unlock(&(data->file_mutex));
+
+	return retval;
+}
+
+/*
+ *  rmidev_release: - release an existing handle
+ *  @inp: inode structure
+ *  @filp: file structure for read/write
+ *
+ *  @return 0 if succeeds
+ */
+static int rmidev_release(struct inode *inp, struct file *filp)
+{
+	struct rmidev_data *data = container_of(inp->i_cdev,
+			struct rmidev_data, main_dev);
+
+	if (!data->rmi_dev)
+		return -EACCES;
+
+	mutex_lock(&(data->file_mutex));
+
+	data->ref_count--;
+	if (data->ref_count < 0)
+		data->ref_count = 0;
+
+	mutex_unlock(&(data->file_mutex));
+
+	return 0;
+}
+
+static const struct file_operations rmidev_fops = {
+	.owner =    THIS_MODULE,
+	.llseek =   rmidev_llseek,
+	.read =     rmidev_read,
+	.write =    rmidev_write,
+	.open =     rmidev_open,
+	.release =  rmidev_release,
+};
+
+/*
+ * rmidev_device_cleanup - release memory or unregister driver
+ * @rmidev_data: instance data for a particular device.
+ *
+ */
+static void rmidev_device_cleanup(struct rmidev_data *data)
+{
+	dev_t devno;
+
+	/* Get rid of our char dev entries */
+	if (data) {
+		devno = data->main_dev.dev;
+
+		if (data->device_class)
+			device_destroy(data->device_class, devno);
+
+		cdev_del(&data->main_dev);
+		kfree(data);
+
+		/* cleanup_module is never called if registering failed */
+		unregister_chrdev_region(devno, 1);
+		pr_debug("%s: rmidev device is removed\n", __func__);
+	}
+}
+
+/*
+ * rmi_char_devnode - return device permission
+ *
+ * @dev: char device structure
+ * @mode: file permission
+ *
+ */
+static char *rmi_char_devnode(struct device *dev, mode_t *mode)
+{
+	if (!mode)
+		return NULL;
+	/**mode = 0666*/
+	*mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
+
+	return kasprintf(GFP_KERNEL, "rmi/%s", dev_name(dev));
+}
+
+static int rmidev_init_device(struct rmi_char_device *cd)
+{
+	struct rmi_device *rmi_dev = cd->rmi_dev;
+	struct rmidev_data *data;
+	dev_t dev_no;
+	int retval;
+	struct device *device_ptr;
+
+	if (rmidev_major_num) {
+		dev_no = MKDEV(rmidev_major_num, cd->rmi_dev->number);
+		retval = register_chrdev_region(dev_no, 1, CHAR_DEVICE_NAME);
+	} else {
+		retval = alloc_chrdev_region(&dev_no, 0, 1, CHAR_DEVICE_NAME);
+		/* let kernel allocate a major for us */
+		rmidev_major_num = MAJOR(dev_no);
+		dev_info(&rmi_dev->dev, "Major number of rmidev: %d\n",
+				 rmidev_major_num);
+	}
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"Failed to get minor dev number %d, code %d.\n",
+			cd->rmi_dev->number, retval);
+		return retval;
+	} else
+		dev_info(&rmi_dev->dev, "Allocated rmidev %d %d.\n",
+			 MAJOR(dev_no), MINOR(dev_no));
+
+	data = kzalloc(sizeof(struct rmidev_data), GFP_KERNEL);
+	if (!data) {
+		dev_err(&rmi_dev->dev, "Failed to allocate rmidev_data.\n");
+		/* unregister the char device region */
+		__unregister_chrdev(rmidev_major_num, MINOR(dev_no), 1,
+				CHAR_DEVICE_NAME);
+		return -ENOMEM;
+	}
+
+	mutex_init(&data->file_mutex);
+
+	data->rmi_dev = cd->rmi_dev;
+	cd->data = data;
+
+	cdev_init(&data->main_dev, &rmidev_fops);
+
+	retval = cdev_add(&data->main_dev, dev_no, 1);
+	if (retval) {
+		dev_err(&cd->rmi_dev->dev, "Error %d adding rmi_char_dev.\n",
+			retval);
+		rmidev_device_cleanup(data);
+		return retval;
+	}
+
+	dev_set_name(&cd->dev, "rmidev%d", MINOR(dev_no));
+	data->device_class = rmidev_device_class;
+	device_ptr = device_create(
+			data->device_class,
+			NULL, dev_no, NULL,
+			CHAR_DEVICE_NAME"%d",
+			MINOR(dev_no));
+
+	if (IS_ERR(device_ptr)) {
+		dev_err(&cd->rmi_dev->dev, "Failed to create rmi device.\n");
+		rmidev_device_cleanup(data);
+		return -ENODEV;
+	}
+
+	return 0;
+}
+
+static void rmidev_remove_device(struct rmi_char_device *cd)
+{
+	struct rmidev_data *data;
+
+	dev_dbg(&cd->dev, "%s: removing an rmidev device.\n", __func__);
+	if (!cd)
+		return;
+
+	data = cd->data;
+	if (data)
+		rmidev_device_cleanup(data);
+}
+
+static struct rmi_char_driver rmidev_driver = {
+	.driver = {
+		.name = "rmidev",
+		.owner = THIS_MODULE,
+	},
+
+	.init = rmidev_init_device,
+	.remove = rmidev_remove_device,
+};
+
+static int __init rmidev_init(void)
+{
+	int error = 0;
+	pr_debug("%s: rmi_dev initialization.\n", __func__);
+
+	/* create device node */
+	rmidev_device_class = class_create(THIS_MODULE, DEVICE_CLASS_NAME);
+
+	if (IS_ERR(rmidev_device_class)) {
+		pr_err("%s: ERROR - Failed to create /dev/%s.\n", __func__,
+			CHAR_DEVICE_NAME);
+		return -ENODEV;
+	}
+	/* setup permission */
+	rmidev_device_class->devnode = rmi_char_devnode;
+
+	error = rmi_register_character_driver(&rmidev_driver);
+	if (error)
+		class_destroy(rmidev_device_class);
+	return error;
+}
+
+static void __exit rmidev_exit(void)
+{
+	pr_debug("%s: exiting.\n", __func__);
+	rmi_unregister_character_driver(&rmidev_driver);
+	class_destroy(rmidev_device_class);
+}
+
+module_init(rmidev_init);
+module_exit(rmidev_exit);
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]>");
+MODULE_DESCRIPTION("RMI4 Char Device");
+MODULE_LICENSE("GPL");
#27 lore raw

RFC PATCH 10/17 input: RM4 F17 Pointing sticks

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f17.c |  713 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 713 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f17.c new file +713 -0
new file mode 100644
index 0000000..006a716
@@ -0,0 +1,713 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+#define QUERY_BASE_INDEX 1
+#define MAX_NAME_LENGTH 256
+
+union f17_device_query {
+	struct {
+		u8 number_of_sticks:3;
+	};
+	u8 regs[1];
+};
+
+#define F17_MANUFACTURER_SYNAPTICS 0
+#define F17_MANUFACTURER_NMB 1
+#define F17_MANUFACTURER_ALPS 2
+
+struct f17_stick_query {
+	union {
+		struct {
+			u8 manufacturer:4;
+			u8 resistive:1;
+			u8 ballistics:1;
+			u8 reserved1:2;
+			u8 has_relative:1;
+			u8 has_absolute:1;
+			u8 has_gestures:1;
+			u8 has_dribble:1;
+			u8 reserved2:4;
+		};
+		u8 regs[2];
+	} general;
+
+	union {
+		struct {
+			u8 has_single_tap:1;
+			u8 has_tap_and_hold:1;
+			u8 has_double_tap:1;
+			u8 has_early_tap:1;
+			u8 has_press:1;
+		};
+		u8 regs[1];
+	} gestures;
+};
+
+union f17_device_controls {
+	struct {
+		u8 reporting_mode:3;
+		u8 dribble:1;
+	};
+	u8 regs[1];
+};
+
+struct f17_stick_controls {
+	union {
+		struct {
+			u8 z_force_threshold;
+			u8 radial_force_threshold;
+		};
+		u8 regs[3];
+	} general;
+
+	union {
+		struct {
+			u8 motion_sensitivity:4;
+			u8 antijitter:1;
+		};
+		u8 regs[1];
+	} relative;
+
+	union {
+		struct {
+			u8 single_tap:1;
+			u8 tap_and_hold:1;
+
+			u8 double_tap:1;
+			u8 early_tap:1;
+			u8 press:1;
+		};
+		u8 regs[1];
+	} enable;
+
+	u8 maximum_tap_time;
+	u8 minimum_press_time;
+	u8 maximum_radial_force;
+};
+
+
+union f17_device_commands {
+	struct {
+		u8 rezero:1;
+	};
+	u8 regs[1];
+};
+
+struct f17_stick_data {
+	union {
+		struct {
+			u8 x_force_high:8;
+			u8 y_force_high:8;
+			u8 y_force_low:4;
+			u8 x_force_low:4;
+			u8 z_force:8;
+		} __attribute__((__packed__));
+		struct {
+			u8 regs[4];
+			u16 address;
+		};
+	} abs;
+	union {
+		struct {
+			s8 x_delta:8;
+			s8 y_delta:8;
+		} __attribute__((__packed__));
+		struct {
+			u8 regs[2];
+			u16 address;
+		};
+	} rel;
+	union {
+		struct {
+			u8 single_tap:1;
+			u8 tap_and_hold:1;
+			u8 double_tap:1;
+			u8 early_tap:1;
+			u8 press:1;
+			u8 reserved:3;
+		} __attribute__((__packed__));
+		struct {
+			u8 regs[1];
+			u16 address;
+		};
+	} gestures;
+};
+
+
+/* data specific to f17 that needs to be kept around */
+
+struct rmi_f17_stick_data {
+	struct f17_stick_query query;
+	struct f17_stick_controls controls;
+	struct f17_stick_data data;
+
+	u16 control_address;
+
+	int index;
+
+	char input_name[MAX_NAME_LENGTH];
+	char input_phys[MAX_NAME_LENGTH];
+	struct input_dev *input;
+	char mouse_name[MAX_NAME_LENGTH];
+	char mouse_phys[MAX_NAME_LENGTH];
+	struct input_dev *mouse;
+};
+
+struct rmi_f17_device_data {
+	u16 control_address;
+
+	union f17_device_query query;
+	union f17_device_commands commands;
+	union f17_device_controls controls;
+
+	struct rmi_f17_stick_data *sticks;
+
+};
+
+static ssize_t f17_rezero_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf);
+static ssize_t f17_rezero_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+
+
+
+static int f17_alloc_memory(struct rmi_function_container *fc);
+
+static void f17_free_memory(struct rmi_function_container *fc);
+
+static int f17_initialize(struct rmi_function_container *fc);
+
+static int f17_register_devices(struct rmi_function_container *fc);
+
+static int f17_create_sysfs(struct rmi_function_container *fc);
+
+static int f17_config(struct rmi_function_container *fc);
+
+
+static struct device_attribute attrs[] = {
+	__ATTR(rezero, RMI_RW_ATTR,
+		f17_rezero_show, f17_rezero_store),
+};
+
+
+int f17_read_control_parameters(struct rmi_device *rmi_dev,
+	struct rmi_f17_device_data *f17)
+{
+	int retval = 0;
+
+	/* TODO: read this or delete the function */
+
+	return retval;
+}
+
+
+static int f17_init(struct rmi_function_container *fc)
+{
+	int retval;
+
+	retval = f17_alloc_memory(fc);
+	if (retval < 0)
+		goto err_free_data;
+
+	retval = f17_initialize(fc);
+	if (retval < 0)
+		goto err_free_data;
+
+	retval = f17_register_devices(fc);
+	if (retval < 0)
+		goto err_free_data;
+
+	retval = f17_create_sysfs(fc);
+	if (retval < 0)
+		goto err_free_data;
+
+	return 0;
+
+err_free_data:
+	f17_free_memory(fc);
+
+	return retval;
+}
+
+static int f17_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_f17_device_data *f17;
+	int retval;
+
+	f17 = kzalloc(sizeof(struct rmi_f17_device_data), GFP_KERNEL);
+	if (!f17) {
+		dev_err(&fc->dev, "Failed to allocate function data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f17;
+
+	retval = rmi_read_block(fc->rmi_dev, fc->fd.query_base_addr,
+				f17->query.regs, sizeof(f17->query.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read query register.\n");
+		goto error_exit;
+	}
+
+	f17->sticks = kcalloc(f17->query.number_of_sticks + 1,
+			sizeof(struct rmi_f17_stick_data), GFP_KERNEL);
+	if (!f17->sticks) {
+		dev_err(&fc->dev, "Failed to allocate per stick data.\n");
+		return -ENOMEM;
+	}
+
+	return 0;
+
+error_exit:
+	kfree(f17);
+	fc->data = NULL;
+	return retval;
+}
+
+static void f17_free_memory(struct rmi_function_container *fc)
+{
+	struct rmi_f17_device_data *f17 = fc->data;
+
+	if (f17) {
+		kfree(f17->sticks);
+		f17->sticks = NULL;
+	}
+	kfree(f17);
+	fc->data = NULL;
+}
+
+static int f17_init_stick(struct rmi_device *rmi_dev,
+			  struct rmi_f17_stick_data *stick,
+			  u16 *next_query_reg, u16 *next_data_reg,
+			  u16 *next_control_reg) {
+	int retval = 0;
+
+	retval = rmi_read_block(rmi_dev, *next_query_reg,
+		stick->query.general.regs,
+		sizeof(stick->query.general.regs));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "Failed to read stick general query.\n");
+		return retval;
+	}
+	*next_query_reg += sizeof(stick->query.general.regs);
+
+	dev_dbg(&rmi_dev->dev, "Stick %d found\n", stick->index);
+	dev_dbg(&rmi_dev->dev, "    Manufacturer: %d.\n",
+				stick->query.general.manufacturer);
+	dev_dbg(&rmi_dev->dev, "    Resistive:    %d.\n",
+				stick->query.general.resistive);
+	dev_dbg(&rmi_dev->dev, "    Ballistics:   %d.\n",
+				stick->query.general.ballistics);
+	dev_dbg(&rmi_dev->dev, "    Manufacturer: %d.\n",
+				stick->query.general.ballistics);
+	dev_dbg(&rmi_dev->dev, "    Has relative: %d.\n",
+				stick->query.general.has_relative);
+	dev_dbg(&rmi_dev->dev, "    Has absolute: %d.\n",
+				stick->query.general.has_absolute);
+	dev_dbg(&rmi_dev->dev, "    Had dribble:  %d.\n",
+				stick->query.general.has_dribble);
+	dev_dbg(&rmi_dev->dev, "    Has gestures: %d.\n",
+				stick->query.general.has_gestures);
+
+	if (stick->query.general.has_gestures) {
+		retval = rmi_read_block(rmi_dev, *next_query_reg,
+			stick->query.gestures.regs,
+			sizeof(stick->query.gestures.regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F17 gestures query, code %d.\n",
+				retval);
+			return retval;
+		}
+		*next_query_reg += sizeof(stick->query.gestures.regs);
+		dev_dbg(&rmi_dev->dev, "        single tap: %d.\n",
+					stick->query.gestures.has_single_tap);
+		dev_dbg(&rmi_dev->dev, "        tap & hold: %d.\n",
+					stick->query.gestures.has_tap_and_hold);
+		dev_dbg(&rmi_dev->dev, "        double tap: %d.\n",
+					stick->query.gestures.has_double_tap);
+		dev_dbg(&rmi_dev->dev, "        early tap:  %d.\n",
+					stick->query.gestures.has_early_tap);
+		dev_dbg(&rmi_dev->dev, "        press:      %d.\n",
+					stick->query.gestures.has_press);
+	}
+	if (stick->query.general.has_absolute) {
+		stick->data.abs.address = *next_data_reg;
+		*next_data_reg += sizeof(stick->data.abs.regs);
+	}
+	if (stick->query.general.has_relative) {
+		stick->data.rel.address = *next_data_reg;
+		*next_data_reg += sizeof(stick->data.rel.regs);
+	}
+	if (stick->query.general.has_gestures) {
+		stick->data.gestures.address = *next_data_reg;
+		*next_data_reg += sizeof(stick->data.gestures.regs);
+	}
+
+	return retval;
+}
+
+static int f17_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_f17_device_data *f17 = fc->data;
+	int i;
+	int retval;
+	u16 next_query_reg = fc->fd.query_base_addr;
+	u16 next_data_reg = fc->fd.data_base_addr;
+	u16 next_control_reg = fc->fd.control_base_addr;
+
+	retval = rmi_read_block(fc->rmi_dev, fc->fd.query_base_addr,
+				f17->query.regs, sizeof(f17->query.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read query register.\n");
+		return retval;
+	}
+	dev_info(&fc->dev, "Found F17 with %d sticks.\n",
+		 f17->query.number_of_sticks + 1);
+	next_query_reg += sizeof(f17->query.regs);
+
+	retval = rmi_read_block(rmi_dev, fc->fd.command_base_addr,
+		f17->commands.regs, sizeof(f17->commands.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read command register.\n");
+		return retval;
+	}
+
+	f17->control_address = fc->fd.control_base_addr;
+	retval = f17_read_control_parameters(rmi_dev, f17);
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to initialize F17 control params.\n");
+		return retval;
+	}
+
+	for (i = 0; i < f17->query.number_of_sticks + 1; i++) {
+		f17->sticks[i].index = i;
+		retval = f17_init_stick(rmi_dev, &f17->sticks[i],
+					&next_query_reg, &next_data_reg,
+					&next_control_reg);
+		if (!retval) {
+			dev_err(&fc->dev, "Failed to init stick %d.\n", i);
+			return retval;
+		}
+	}
+
+	return retval;
+}
+
+static int f17_register_stick(struct rmi_function_container *fc,
+			      struct rmi_f17_stick_data *stick) {
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	int retval = 0;
+
+	if (stick->query.general.has_absolute) {
+		struct input_dev *input_dev;
+		input_dev = input_allocate_device();
+		if (!input_dev) {
+			dev_err(&rmi_dev->dev, "Failed to allocate stick device %d.\n",
+				stick->index);
+			return -ENOMEM;
+		}
+
+		snprintf(stick->input_name, sizeof(stick->input_name),
+			"RMI F%02x Stick %d", 0x17, stick->index);
+		snprintf(stick->input_phys, sizeof(stick->input_phys),
+			 "sensor00fn%02x/stick%d", 0x17, stick->index);
+		input_dev->name = stick->input_name;
+		input_dev->phys = stick->input_phys;
+		input_dev->dev.parent = &fc->dev;
+		input_set_drvdata(input_dev, stick);
+
+		retval = input_register_device(input_dev);
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Failed to register stick device %d.\n",
+				stick->index);
+			goto error_free_device;
+		}
+		stick->input = input_dev;
+	}
+
+	if (stick->query.general.has_relative) {
+		struct input_dev *input_dev_mouse;
+		/*create input device for mouse events  */
+		input_dev_mouse = input_allocate_device();
+		if (!input_dev_mouse) {
+			retval = -ENOMEM;
+			goto error_free_device;
+		}
+
+		snprintf(stick->mouse_name, sizeof(stick->mouse_name),
+			"RMI F%02x Mouse %d", 0x17, stick->index);
+		snprintf(stick->mouse_phys, sizeof(stick->mouse_name),
+			 "sensor00fn%02x/mouse%d", 0x17, stick->index);
+		input_dev_mouse->name = stick->mouse_name;
+		input_dev_mouse->phys = stick->mouse_phys;
+		input_dev_mouse->dev.parent = &fc->dev;
+
+		input_dev_mouse->id.vendor  = 0x18d1;
+		input_dev_mouse->id.product = 0x0210;
+		input_dev_mouse->id.version = 0x0100;
+
+		set_bit(EV_REL, input_dev_mouse->evbit);
+		set_bit(REL_X, input_dev_mouse->relbit);
+		set_bit(REL_Y, input_dev_mouse->relbit);
+
+		set_bit(BTN_MOUSE, input_dev_mouse->evbit);
+		/* Register device's buttons and keys */
+		set_bit(EV_KEY, input_dev_mouse->evbit);
+		set_bit(BTN_LEFT, input_dev_mouse->keybit);
+		set_bit(BTN_MIDDLE, input_dev_mouse->keybit);
+		set_bit(BTN_RIGHT, input_dev_mouse->keybit);
+
+		retval = input_register_device(input_dev_mouse);
+		if (retval < 0)
+			goto error_free_device;
+		stick->mouse = input_dev_mouse;
+	}
+
+	return 0;
+
+error_free_device:
+	if (stick->input) {
+		input_free_device(stick->input);
+		stick->input = NULL;
+	}
+	if (stick->mouse) {
+		input_free_device(stick->mouse);
+		stick->mouse = NULL;
+	}
+	return retval;
+}
+
+static int f17_register_devices(struct rmi_function_container *fc)
+{
+	struct rmi_f17_device_data *f17 = fc->data;
+	int i;
+	int retval = 0;
+
+	for (i = 0; i < f17->query.number_of_sticks + 1 && !retval; i++)
+		retval = f17_register_stick(fc, &f17->sticks[i]);
+
+	return retval;
+}
+
+static int f17_create_sysfs(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	int rc;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.\n");
+	/* Set up sysfs device attributes. */
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
+		if (sysfs_create_file
+		    (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create sysfs file for %s.",
+				attrs[attr_count].attr.name);
+			rc = -ENODEV;
+			goto err_remove_sysfs;
+		}
+	}
+
+	return 0;
+
+err_remove_sysfs:
+	for (attr_count--; attr_count >= 0; attr_count--)
+		sysfs_remove_file(&fc->dev.kobj, &attrs[attr_count].attr);
+	return rc;
+
+}
+
+static int f17_config(struct rmi_function_container *fc)
+{
+	struct rmi_f17_device_data *f17 = fc->data;
+	int retval;
+
+	retval = rmi_write_block(fc->rmi_dev, f17->control_address,
+		f17->controls.regs, sizeof(f17->controls.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not write stick controls to 0x%04x\n",
+				f17->control_address);
+		return retval;
+	}
+
+	return retval;
+}
+
+static void f17_remove(struct rmi_function_container *fc)
+{
+	struct rmi_f17_device_data *f17 = fc->data;
+	int i = 0;
+
+	for (i = 0; i < ARRAY_SIZE(attrs); i++)
+		sysfs_remove_file(&fc->dev.kobj, &attrs[i].attr);
+
+	for (i = 0; i < f17->query.number_of_sticks + 1; i++)
+		input_unregister_device(f17->sticks[i].input);
+
+	f17_free_memory(fc);
+}
+
+static int f17_process_stick(struct rmi_device *rmi_dev,
+			     struct rmi_f17_stick_data *stick) {
+	int retval = 0;
+
+	if (stick->query.general.has_absolute) {
+		retval = rmi_read_block(rmi_dev, stick->data.abs.address,
+			stick->data.abs.regs, sizeof(stick->data.abs.regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read abs data for stick %d, code %d.\n",
+				stick->index, retval);
+			goto error_exit;
+		}
+	}
+	if (stick->query.general.has_relative) {
+		retval = rmi_read_block(rmi_dev, stick->data.rel.address,
+			stick->data.rel.regs, sizeof(stick->data.rel.regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read rel data for stick %d, code %d.\n",
+				stick->index, retval);
+			goto error_exit;
+		}
+		dev_dbg(&rmi_dev->dev, "Reporting dX: %d, dy: %d\n",
+			stick->data.rel.x_delta, stick->data.rel.y_delta);
+		input_report_rel(stick->mouse, REL_X, stick->data.rel.x_delta);
+		input_report_rel(stick->mouse, REL_Y, stick->data.rel.y_delta);
+	}
+	if (stick->query.general.has_gestures) {
+		retval = rmi_read_block(rmi_dev, stick->data.gestures.address,
+			stick->data.gestures.regs,
+			sizeof(stick->data.gestures.regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read gestures for stick %d, code %d.\n",
+				stick->index, retval);
+			goto error_exit;
+		}
+	}
+	retval = 0;
+
+error_exit:
+	if (stick->input)
+		input_sync(stick->input);
+	if (stick->mouse)
+		input_sync(stick->mouse);
+	return retval;
+}
+
+static int f17_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_f17_device_data *f17 = fc->data;
+	int i;
+	int retval = 0;
+
+	for (i = 0; i < f17->query.number_of_sticks + 1 && !retval; i++)
+		retval = f17_process_stick(rmi_dev, &f17->sticks[i]);
+
+	return retval;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x17,
+	.init = f17_init,
+	.config = f17_config,
+	.attention = f17_attention,
+	.remove = f17_remove
+};
+
+static int __init f17_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void f17_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+
+static ssize_t f17_rezero_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_f17_device_data *f17;
+
+	fc = to_rmi_function_container(dev);
+	f17 = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			f17->commands.rezero);
+
+}
+
+static ssize_t f17_rezero_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf,
+					 size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_f17_device_data *data;
+	unsigned int new_value;
+	int len;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	len = sscanf(buf, "%u", &new_value);
+	if (new_value != 0 && new_value != 1) {
+		dev_err(dev,
+			"%s: Error - rezero is not a valid value 0x%x.\n",
+			__func__, new_value);
+		return -EINVAL;
+	}
+	data->commands.rezero = new_value;
+	len = rmi_write(fc->rmi_dev, fc->fd.command_base_addr,
+		data->commands.rezero);
+
+	if (len < 0) {
+		dev_err(dev, "%s : Could not write rezero to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return -EINVAL;
+	}
+	return count;
+}
+
+module_init(f17_module_init);
+module_exit(f17_module_exit);
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]>");
+MODULE_DESCRIPTION("RMI F17 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#18 lore raw

RFC PATCH 6/17 input: RMI4 firmware update

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_fw_update.c |  724 ++++++++++++++++++++++++++++++++++++
 1 files changed, 724 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_fw_update.c new file +724 -0
new file mode 100644
index 0000000..7f6c315
@@ -0,0 +1,724 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define DEBUG
+
+#define RIM_HACK 1
+
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/ihex.h>
+#include <linux/kernel.h>
+#include<linux/moduleparam.h>
+#include <linux/rmi.h>
+#include <linux/time.h>
+#include "rmi_driver.h"
+#include "rmi_f01.h"
+#include "rmi_f34.h"
+
+#define HAS_BSR_MASK 0x20
+
+#define CHECKSUM_OFFSET 0
+#define BOOTLOADER_VERSION_OFFSET 0x07
+#define IMAGE_SIZE_OFFSET 0x08
+#define CONFIG_SIZE_OFFSET 0x0C
+#define PRODUCT_ID_OFFSET 0x10
+#define PRODUCT_ID_SIZE 10
+#define PRODUCT_INFO_OFFSET 0x1E
+#define PRODUCT_INFO_SIZE 2
+
+#define F01_RESET_MASK 0x01
+
+#define ENABLE_WAIT_US (300 * 1000)
+
+/** Image file V5, Option 0
+ */
+struct image_header {
+	u32 checksum;
+	unsigned int image_size;
+	unsigned int config_size;
+	unsigned char options;
+	unsigned char bootloader_version;
+	u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
+	unsigned char product_info[PRODUCT_INFO_SIZE];
+};
+
+static u32 extract_u32(const u8 *ptr)
+{
+	return (u32)ptr[0] +
+		(u32)ptr[1] * 0x100 +
+		(u32)ptr[2] * 0x10000 +
+		(u32)ptr[3] * 0x1000000;
+}
+
+struct reflash_data {
+	struct rmi_device *rmi_dev;
+	struct pdt_entry *f01_pdt;
+	union f01_basic_queries f01_queries;
+	union f01_device_control_0 f01_controls;
+	char product_id[RMI_PRODUCT_ID_LENGTH+1];
+	struct pdt_entry *f34_pdt;
+	u8 bootloader_id[2];
+	union f34_query_regs f34_queries;
+	union f34_control_status f34_controls;
+	const u8 *firmware_data;
+	const u8 *config_data;
+};
+
+/* If this parameter is true, we will update the firmware regardless of
+ * the versioning info.
+ */
+static bool force = 1;
+module_param(force, bool, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(param, "Force reflash of RMI4 devices");
+
+/* If this parameter is not NULL, we'll use that name for the firmware image,
+ * instead of getting it from the F01 queries.
+ */
+static char *img_name;
+module_param(img_name, charp, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(param, "Name of the RMI4 firmware image");
+
+#define RMI4_IMAGE_FILE_REV1_OFFSET 30
+#define RMI4_IMAGE_FILE_REV2_OFFSET 31
+#define IMAGE_FILE_CHECKSUM_SIZE 4
+#define FIRMWARE_IMAGE_AREA_OFFSET 0x100
+
+static void extract_header(const u8 *data, int pos, struct image_header *header)
+{
+	header->checksum = extract_u32(&data[pos + CHECKSUM_OFFSET]);
+	header->bootloader_version = data[pos + BOOTLOADER_VERSION_OFFSET];
+	header->image_size = extract_u32(&data[pos + IMAGE_SIZE_OFFSET]);
+	header->config_size = extract_u32(&data[pos + CONFIG_SIZE_OFFSET]);
+	memcpy(header->product_id, &data[pos + PRODUCT_ID_OFFSET],
+	       RMI_PRODUCT_ID_LENGTH);
+	header->product_id[PRODUCT_ID_SIZE] = 0;
+	memcpy(header->product_info, &data[pos + PRODUCT_INFO_OFFSET],
+	       RMI_PRODUCT_ID_LENGTH);
+}
+
+static int rescan_pdt(struct reflash_data *data)
+{
+	int retval;
+	bool f01_found;
+	bool f34_found;
+	struct pdt_entry pdt_entry;
+	int i;
+	struct rmi_device *rmi_dev = data->rmi_dev;
+	struct pdt_entry *f34_pdt = data->f34_pdt;
+	struct pdt_entry *f01_pdt = data->f01_pdt;
+
+	/* Per spec, once we're in reflash we only need to look at the first
+	 * PDT page for potentially changed F01 and F34 information.
+	 */
+	for (i = PDT_START_SCAN_LOCATION; i >= PDT_END_SCAN_LOCATION;
+			i -= sizeof(pdt_entry)) {
+		retval = rmi_read_block(rmi_dev, i, (u8 *)&pdt_entry,
+					sizeof(pdt_entry));
+		if (retval != sizeof(pdt_entry)) {
+			dev_err(&rmi_dev->dev,
+				"Read PDT entry at %#06x failed: %d.\n",
+				i, retval);
+			return retval;
+		}
+
+		if (RMI4_END_OF_PDT(pdt_entry.function_number))
+			break;
+
+		if (pdt_entry.function_number == 0x01) {
+			memcpy(f01_pdt, &pdt_entry, sizeof(pdt_entry));
+			f01_found = true;
+		} else if (pdt_entry.function_number == 0x34) {
+			memcpy(f34_pdt, &pdt_entry, sizeof(pdt_entry));
+			f34_found = true;
+		}
+	}
+
+	if (!f01_found) {
+		dev_err(&rmi_dev->dev, "Failed to find F01 PDT entry.\n");
+		retval = -ENODEV;
+	} else if (!f34_found) {
+		dev_err(&rmi_dev->dev, "Failed to find F34 PDT entry.\n");
+		retval = -ENODEV;
+	} else
+		retval = 0;
+
+	return retval;
+}
+
+static int read_f34_controls(struct reflash_data *data)
+{
+	int retval;
+
+	retval = rmi_read(data->rmi_dev, data->f34_controls.address,
+			  data->f34_controls.regs);
+	if (retval < 0)
+		return retval;
+
+	return 0;
+}
+
+static int read_f01_status(struct reflash_data *data,
+			   union f01_device_status *device_status)
+{
+	int retval;
+
+	retval = rmi_read(data->rmi_dev, data->f01_pdt->data_base_addr,
+			  device_status->regs);
+	if (retval < 0)
+		return retval;
+
+	return 0;
+}
+
+static int read_f01_controls(struct reflash_data *data)
+{
+	int retval;
+
+	retval = rmi_read(data->rmi_dev, data->f01_pdt->control_base_addr,
+			  data->f01_controls.regs);
+	if (retval < 0)
+		return retval;
+	return 0;
+}
+
+static int write_f01_controls(struct reflash_data *data)
+{
+	int retval;
+
+	retval = rmi_write(data->rmi_dev, data->f01_pdt->control_base_addr,
+			  data->f01_controls.regs[0]);
+	if (retval < 0)
+		return retval;
+	return 0;
+}
+
+#define MIN_SLEEP_TIME_US 50
+#define MAX_SLEEP_TIME_US 100
+
+/* Wait until the status is idle and we're ready to continue */
+static int wait_for_idle(struct reflash_data *data, int timeout_ms)
+{
+	int timeout_count = ((timeout_ms * 1000) / MAX_SLEEP_TIME_US) + 1;
+	int count = 0;
+	union f34_control_status *controls = &data->f34_controls;
+	int retval;
+
+	do {
+		if (count || timeout_count == 1)
+			usleep_range(MIN_SLEEP_TIME_US, MAX_SLEEP_TIME_US);
+		retval = read_f34_controls(data);
+		count++;
+		if (retval < 0)
+			continue;
+		else if (IS_IDLE(controls)) {
+			if (!data->f34_controls.program_enabled) {
+				/** This works around a bug in certain device
+				 * firmwares, where the idle state is reached,
+				 * but the program_enabled bit is not yet set.
+				 */
+				dev_warn(&data->rmi_dev->dev, "Yikes!  We're not enabled!\n");
+				msleep(1000);
+				read_f34_controls(data);
+			}
+			return 0;
+		}
+	} while (count < timeout_count);
+
+	dev_err(&data->rmi_dev->dev,
+		"ERROR: Timeout waiting for idle status, last status: %#04x.\n",
+		controls->regs[0]);
+	dev_err(&data->rmi_dev->dev, "Command: %#04x\n", controls->command);
+	dev_err(&data->rmi_dev->dev, "Status:  %#04x\n", controls->status);
+	dev_err(&data->rmi_dev->dev, "Enabled: %d\n",
+			controls->program_enabled);
+	dev_err(&data->rmi_dev->dev, "Idle:    %d\n", IS_IDLE(controls));
+	return -ETIMEDOUT;
+}
+
+
+static int read_f01_queries(struct reflash_data *data)
+{
+	int retval;
+	u16 addr = data->f01_pdt->query_base_addr;
+
+	retval = rmi_read_block(data->rmi_dev, addr, data->f01_queries.regs,
+				ARRAY_SIZE(data->f01_queries.regs));
+	if (retval < 0) {
+		dev_err(&data->rmi_dev->dev,
+			"Failed to read F34 queries (code %d).\n", retval);
+		return retval;
+	}
+	addr += ARRAY_SIZE(data->f01_queries.regs);
+
+	retval = rmi_read_block(data->rmi_dev, addr, data->product_id,
+				RMI_PRODUCT_ID_LENGTH);
+	if (retval < 0) {
+		dev_err(&data->rmi_dev->dev,
+			"Failed to read product ID (code %d).\n", retval);
+		return retval;
+	}
+	data->product_id[RMI_PRODUCT_ID_LENGTH] = 0;
+	dev_info(&data->rmi_dev->dev, "F01 Product id:   %s\n",
+			data->product_id);
+	dev_info(&data->rmi_dev->dev, "F01 product info: %#04x %#04x\n",
+			data->f01_queries.productinfo_1,
+			data->f01_queries.productinfo_2);
+
+	return 0;
+}
+
+static int read_f34_queries(struct reflash_data *data)
+{
+	int retval;
+	u8 id_str[3];
+
+	retval = rmi_read_block(data->rmi_dev, data->f34_pdt->query_base_addr,
+				data->bootloader_id, 2);
+	if (retval < 0) {
+		dev_err(&data->rmi_dev->dev,
+			"Failed to read F34 bootloader_id (code %d).\n",
+			retval);
+		return retval;
+	}
+	retval = rmi_read_block(data->rmi_dev, data->f34_pdt->query_base_addr+2,
+			data->f34_queries.regs,
+			ARRAY_SIZE(data->f34_queries.regs));
+	if (retval < 0) {
+		dev_err(&data->rmi_dev->dev,
+			"Failed to read F34 queries (code %d).\n", retval);
+		return retval;
+	}
+	data->f34_queries.block_size =
+			le16_to_cpu(data->f34_queries.block_size);
+	data->f34_queries.fw_block_count =
+			le16_to_cpu(data->f34_queries.fw_block_count);
+	data->f34_queries.config_block_count =
+			le16_to_cpu(data->f34_queries.config_block_count);
+	id_str[0] = data->bootloader_id[0];
+	id_str[1] = data->bootloader_id[1];
+	id_str[2] = 0;
+#ifdef DEBUG
+	dev_info(&data->rmi_dev->dev, "Got F34 data->f34_queries.\n");
+	dev_info(&data->rmi_dev->dev, "F34 bootloader id: %s (%#04x %#04x)\n",
+		 id_str, data->bootloader_id[0], data->bootloader_id[1]);
+	dev_info(&data->rmi_dev->dev, "F34 has config id: %d\n",
+		 data->f34_queries.has_config_id);
+	dev_info(&data->rmi_dev->dev, "F34 unlocked:      %d\n",
+		 data->f34_queries.unlocked);
+	dev_info(&data->rmi_dev->dev, "F34 regMap:        %d\n",
+		 data->f34_queries.reg_map);
+	dev_info(&data->rmi_dev->dev, "F34 block size:    %d\n",
+		 data->f34_queries.block_size);
+	dev_info(&data->rmi_dev->dev, "F34 fw blocks:     %d\n",
+		 data->f34_queries.fw_block_count);
+	dev_info(&data->rmi_dev->dev, "F34 config blocks: %d\n",
+		 data->f34_queries.config_block_count);
+#endif
+
+	data->f34_controls.address = data->f34_pdt->data_base_addr +
+			F34_BLOCK_DATA_OFFSET + data->f34_queries.block_size;
+
+	return 0;
+}
+
+static int write_bootloader_id(struct reflash_data *data)
+{
+	int retval;
+	struct rmi_device *rmi_dev = data->rmi_dev;
+	struct pdt_entry *f34_pdt = data->f34_pdt;
+
+	retval = rmi_write_block(rmi_dev,
+			f34_pdt->data_base_addr + F34_BLOCK_DATA_OFFSET,
+			data->bootloader_id, ARRAY_SIZE(data->bootloader_id));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"Failed to write bootloader ID. Code: %d.\n", retval);
+		return retval;
+	}
+
+	return 0;
+}
+
+static int write_f34_command(struct reflash_data *data, u8 command)
+{
+	int retval;
+	struct rmi_device *rmi_dev = data->rmi_dev;
+
+	retval = rmi_write(rmi_dev, data->f34_controls.address, command);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"Failed to write F34 command %#04x. Code: %d.\n",
+			command, retval);
+		return retval;
+	}
+
+	return 0;
+}
+
+static int enter_flash_programming(struct reflash_data *data)
+{
+	int retval;
+	union f01_device_status device_status;
+	struct rmi_device *rmi_dev = data->rmi_dev;
+
+	retval = write_bootloader_id(data);
+	if (retval < 0)
+		return retval;
+
+	dev_info(&rmi_dev->dev, "Enabling flash programming.\n");
+	retval = write_f34_command(data, F34_ENABLE_FLASH_PROG);
+	if (retval < 0)
+		return retval;
+
+#if	RIM_HACK
+	data->f01_controls.nosleep = true;
+	retval = write_f01_controls(data);
+	if (retval < 0)
+		return retval;
+#endif
+
+	retval = wait_for_idle(data, F34_ENABLE_WAIT_MS);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "Did not reach idle state after %d ms. Code: %d.\n",
+			F34_ENABLE_WAIT_MS, retval);
+		return retval;
+	}
+	if (!data->f34_controls.program_enabled) {
+		dev_err(&rmi_dev->dev, "Reached idle, but programming not enabled (current status register: %#04x).\n",
+					data->f34_controls.regs[0]);
+		return -EINVAL;
+	}
+	dev_info(&rmi_dev->dev, "HOORAY! Programming is enabled!\n");
+
+	retval = rescan_pdt(data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "Failed to rescan pdt.  Code: %d.\n",
+			retval);
+		return retval;
+	}
+
+	retval = read_f01_status(data, &device_status);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "Failed to read F01 status after enabling reflash. Code: %d.\n",
+			retval);
+		return retval;
+	}
+	if (!(device_status.flash_prog)) {
+		dev_err(&rmi_dev->dev, "Device reports as not in flash programming mode.\n");
+		return -EINVAL;
+	}
+
+	retval = read_f34_queries(data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "F34 queries failed, code = %d.\n",
+			retval);
+		return retval;
+	}
+
+	retval = read_f01_controls(data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "F01 controls read failed, code = %d.\n",
+			retval);
+		return retval;
+	}
+	data->f01_controls.nosleep = true;
+	data->f01_controls.sleep_mode = RMI_SLEEP_MODE_NORMAL;
+
+	retval = write_f01_controls(data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "F01 controls write failed, code = %d.\n",
+			retval);
+		return retval;
+	}
+
+	return retval;
+}
+
+static void reset_device(struct reflash_data *data)
+{
+	int retval;
+	struct rmi_device_platform_data *pdata =
+		to_rmi_platform_data(data->rmi_dev);
+
+	dev_info(&data->rmi_dev->dev, "Resetting...\n");
+	retval = rmi_write(data->rmi_dev, data->f01_pdt->command_base_addr,
+			   F01_RESET_MASK);
+	if (retval < 0)
+		dev_warn(&data->rmi_dev->dev,
+			 "WARNING - post-flash reset failed, code: %d.\n",
+			 retval);
+	msleep(pdata->reset_delay_ms);
+	dev_info(&data->rmi_dev->dev, "Reset completed.\n");
+}
+
+/*
+ * Send data to the device one block at a time.
+ */
+static int write_blocks(struct reflash_data *data, u8 *block_ptr,
+			u16 block_count, u8 cmd)
+{
+	int block_num;
+	u8 zeros[] = {0, 0};
+	int retval;
+	u16 addr = data->f34_pdt->data_base_addr + F34_BLOCK_DATA_OFFSET;
+
+	retval = rmi_write_block(data->rmi_dev, data->f34_pdt->data_base_addr,
+				 zeros, ARRAY_SIZE(zeros));
+	if (retval < 0) {
+		dev_err(&data->rmi_dev->dev, "Failed to write initial zeros. Code=%d.\n",
+			retval);
+		return retval;
+	}
+
+	for (block_num = 0; block_num < block_count; ++block_num) {
+		retval = rmi_write_block(data->rmi_dev, addr, block_ptr,
+					 data->f34_queries.block_size);
+		if (retval < 0) {
+			dev_err(&data->rmi_dev->dev, "Failed to write block %d. Code=%d.\n",
+				block_num, retval);
+			return retval;
+		}
+
+		retval = write_f34_command(data, cmd);
+		if (retval) {
+			dev_err(&data->rmi_dev->dev, "Failed to write command for block %d. Code=%d.\n",
+				block_num, retval);
+			return retval;
+		}
+
+
+		retval = wait_for_idle(data, F34_IDLE_WAIT_MS);
+		if (retval) {
+			dev_err(&data->rmi_dev->dev, "Failed to go idle after writing block %d. Code=%d.\n",
+				block_num, retval);
+			return retval;
+		}
+
+		block_ptr += data->f34_queries.block_size;
+	}
+
+	return 0;
+}
+
+static int write_firmware(struct reflash_data *data)
+{
+	return write_blocks(data, (u8 *) data->firmware_data,
+		data->f34_queries.fw_block_count, F34_WRITE_FW_BLOCK);
+}
+
+static int write_configuration(struct reflash_data *data)
+{
+	return write_blocks(data, (u8 *) data->config_data,
+		data->f34_queries.config_block_count, F34_WRITE_CONFIG_BLOCK);
+}
+
+static void reflash_firmware(struct reflash_data *data)
+{
+#ifdef DEBUG
+	struct timespec start;
+	struct timespec end;
+	s64 duration_ns;
+#endif
+	int retval = 0;
+
+	retval = enter_flash_programming(data);
+	if (retval)
+		return;
+
+	retval = write_bootloader_id(data);
+	if (retval)
+		return;
+
+#ifdef	DEBUG
+	dev_info(&data->rmi_dev->dev, "Erasing FW...\n");
+	getnstimeofday(&start);
+#endif
+	retval = write_f34_command(data, F34_ERASE_ALL);
+	if (retval)
+		return;
+
+	retval = wait_for_idle(data, F34_ERASE_WAIT_MS);
+	if (retval) {
+		dev_err(&data->rmi_dev->dev,
+			"Failed to reach idle state. Code: %d.\n", retval);
+		return;
+	}
+#ifdef	DEBUG
+	getnstimeofday(&end);
+	duration_ns = timespec_to_ns(&end) - timespec_to_ns(&start);
+	dev_info(&data->rmi_dev->dev,
+		 "Erase complete, time: %lld ns.\n", duration_ns);
+#endif
+
+	if (data->firmware_data) {
+#ifdef	DEBUG
+		dev_info(&data->rmi_dev->dev, "Writing firmware...\n");
+		getnstimeofday(&start);
+#endif
+		retval = write_firmware(data);
+		if (retval)
+			return;
+#ifdef	DEBUG
+		getnstimeofday(&end);
+		duration_ns = timespec_to_ns(&end) - timespec_to_ns(&start);
+		dev_info(&data->rmi_dev->dev,
+			 "Done writing FW, time: %lld ns.\n", duration_ns);
+#endif
+	}
+
+	if (data->config_data) {
+#ifdef	DEBUG
+		dev_info(&data->rmi_dev->dev, "Writing configuration...\n");
+		getnstimeofday(&start);
+#endif
+		retval = write_configuration(data);
+		if (retval)
+			return;
+#ifdef	DEBUG
+		getnstimeofday(&end);
+		duration_ns = timespec_to_ns(&end) - timespec_to_ns(&start);
+		dev_info(&data->rmi_dev->dev,
+			 "Done writing config, time: %lld ns.\n", duration_ns);
+#endif
+	}
+}
+
+/* Returns false if the firmware should not be reflashed.
+ */
+static bool go_nogo(struct reflash_data *data, struct image_header *header)
+{
+	union f01_device_status device_status;
+	int retval;
+
+	if (data->f01_queries.productinfo_1 < header->product_info[0] ||
+		data->f01_queries.productinfo_2 < header->product_info[1]) {
+		dev_info(&data->rmi_dev->dev,
+			 "FW product ID is older than image product ID.\n");
+		return true;
+	}
+
+	retval = read_f01_status(data, &device_status);
+	if (retval)
+		dev_err(&data->rmi_dev->dev,
+			"Failed to read F01 status. Code: %d.\n", retval);
+
+	return device_status.flash_prog || force;
+}
+
+void rmi4_fw_update(struct rmi_device *rmi_dev,
+		struct pdt_entry *f01_pdt, struct pdt_entry *f34_pdt)
+{
+#ifdef DEBUG
+	struct timespec start;
+	struct timespec end;
+	s64 duration_ns;
+#endif
+	char firmware_name[RMI_PRODUCT_ID_LENGTH + 12];
+	const struct firmware *fw_entry = NULL;
+	int retval;
+	struct image_header header;
+	union pdt_properties pdt_props;
+	struct reflash_data data = {
+		.rmi_dev = rmi_dev,
+		.f01_pdt = f01_pdt,
+		.f34_pdt = f34_pdt,
+	};
+	struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
+
+	dev_info(&rmi_dev->dev, "%s called.\n", __func__);
+#ifdef	DEBUG
+	getnstimeofday(&start);
+#endif
+
+	retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, pdt_props.regs);
+	if (retval < 0) {
+		dev_warn(&rmi_dev->dev,
+			 "Failed to read PDT props at %#06x (code %d). Assuming 0x00.\n",
+			 PDT_PROPERTIES_LOCATION, retval);
+	}
+	if (pdt_props.has_bsr) {
+		dev_warn(&rmi_dev->dev,
+			 "Firmware update for LTS not currently supported.\n");
+		return;
+	}
+
+	retval = read_f01_queries(&data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "F01 queries failed, code = %d.\n",
+			retval);
+		return;
+	}
+	retval = read_f34_queries(&data);
+	if (retval) {
+		dev_err(&rmi_dev->dev, "F34 queries failed, code = %d.\n",
+			retval);
+		return;
+	}
+	if (pdata->firmware_name && strlen(pdata->firmware_name))
+		snprintf(firmware_name, sizeof(firmware_name), "rmi4/%s.img",
+			pdata->firmware_name);
+	else
+		snprintf(firmware_name, sizeof(firmware_name), "rmi4/%s.img",
+			(img_name && strlen(img_name))
+				? img_name : data.product_id);
+	dev_info(&rmi_dev->dev, "Requesting %s.\n", firmware_name);
+	retval = request_firmware(&fw_entry, firmware_name, &rmi_dev->dev);
+	if (retval != 0) {
+		dev_err(&rmi_dev->dev, "Firmware %s not available, code = %d\n",
+			firmware_name, retval);
+		return;
+	}
+
+#ifdef	DEBUG
+	dev_info(&rmi_dev->dev, "Got firmware, size: %d.\n", fw_entry->size);
+	extract_header(fw_entry->data, 0, &header);
+	dev_info(&rmi_dev->dev, "Img checksum:           %#08X\n",
+		 header.checksum);
+	dev_info(&rmi_dev->dev, "Img image size:         %d\n",
+		 header.image_size);
+	dev_info(&rmi_dev->dev, "Img config size:        %d\n",
+		 header.config_size);
+	dev_info(&rmi_dev->dev, "Img bootloader version: %d\n",
+		 header.bootloader_version);
+	dev_info(&rmi_dev->dev, "Img product id:         %s\n",
+		 header.product_id);
+	dev_info(&rmi_dev->dev, "Img product info:       %#04x %#04x\n",
+		 header.product_info[0], header.product_info[1]);
+#endif
+
+	if (header.image_size)
+		data.firmware_data = fw_entry->data + F34_FW_IMAGE_OFFSET;
+	if (header.config_size)
+		data.config_data = fw_entry->data + F34_FW_IMAGE_OFFSET +
+			header.image_size;
+
+	if (go_nogo(&data, &header)) {
+		reflash_firmware(&data);
+		reset_device(&data);
+	} else
+		dev_info(&rmi_dev->dev, "Go/NoGo said don't reflash.\n");
+
+	if (fw_entry)
+		release_firmware(fw_entry);
+#ifdef	DEBUG
+	getnstimeofday(&end);
+	duration_ns = timespec_to_ns(&end) - timespec_to_ns(&start);
+	dev_info(&rmi_dev->dev, "Time to reflash: %lld ns.\n", duration_ns);
+#endif
+}
#22 lore raw

RFC PATCH 8/17 input: RMI4 F09 Built-In Self Test

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f09.c |  772 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 772 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f09.c new file +772 -0
new file mode 100644
index 0000000..ee3058e
@@ -0,0 +1,772 @@
+/*
+ * Copyright (c) 2011, 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+#define QUERY_BASE_INDEX 1
+
+/* data specific to fn $09 that needs to be kept around */
+struct f09_query {
+	u8 limit_register_count;
+	union {
+		struct {
+			u8 result_register_count:3;
+			u8 reserved:3;
+			u8 internal_limits:1;
+			u8 host_test_enable:1;
+		};
+		u8 f09_bist_query1;
+	};
+};
+
+struct f09_control {
+	union {
+		struct {
+			u8 test1_limit_low:8;
+			u8 test1_limit_high:8;
+			u8 test1_limit_diff:8;
+		};
+		u8 f09_control_test1[3];
+	};
+	union {
+		struct {
+			u8 test2_limit_low:8;
+			u8 test2_limit_high:8;
+			u8 test2_limit_diff:8;
+		};
+		u8 f09_control_test2[3];
+	};
+};
+
+struct f09_data {
+	u8 test_number_control;
+	u8 overall_bist_result;
+	u8 test_result1;
+	u8 test_result2;
+	u8 transmitter_number;
+
+	union {
+		struct {
+			u8 receiver_number:6;
+			u8 limit_failure_code:2;
+		};
+		u8 f09_bist_data2;
+	};
+};
+
+struct f09_cmd {
+	union {
+		struct {
+			u8 run_bist:1;
+		};
+		u8 f09_bist_cmd0;
+	};
+};
+
+struct rmi_fn_09_data {
+	struct f09_query query;
+	struct f09_data data;
+	struct f09_cmd cmd;
+	struct f09_control control;
+	signed char status;
+};
+
+
+static ssize_t rmi_f09_status_show(struct device *dev,
+				     struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_status_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count);
+
+static ssize_t rmi_f09_limit_register_count_show(struct device *dev,
+				struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_host_test_enable_show(struct device *dev,
+				struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_host_test_enable_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count);
+
+static ssize_t rmi_f09_internal_limits_show(struct device *dev,
+				struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_result_register_count_show(struct device *dev,
+				struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_overall_bist_result_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_test_number_control_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_test_number_control_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count);
+
+static ssize_t rmi_f09_run_bist_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_run_bist_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count);
+
+static ssize_t rmi_f09_test_result1_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_test_result2_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_control_test1_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_control_test1_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count);
+
+static ssize_t rmi_f09_control_test2_show(struct device *dev,
+				      struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_f09_control_test2_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count);
+
+static int rmi_f09_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f09_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f09_initialize(struct rmi_function_container *fc);
+
+static int rmi_f09_config(struct rmi_function_container *fc);
+
+static int rmi_f09_reset(struct rmi_function_container *fc);
+
+static int rmi_f09_create_sysfs(struct rmi_function_container *fc);
+
+
+static struct device_attribute attrs[] = {
+	__ATTR(status, RMI_RW_ATTR,
+		   rmi_f09_status_show, rmi_f09_status_store),
+	__ATTR(limitRegisterCount, RMI_RO_ATTR,
+	       rmi_f09_limit_register_count_show, rmi_store_error),
+	__ATTR(hostTestEnable, RMI_RW_ATTR,
+	       rmi_f09_host_test_enable_show, rmi_f09_host_test_enable_store),
+	__ATTR(internalLimits, RMI_RO_ATTR,
+	       rmi_f09_internal_limits_show, rmi_store_error),
+	__ATTR(resultRegisterCount, RMI_RO_ATTR,
+	       rmi_f09_result_register_count_show, rmi_store_error),
+	__ATTR(overall_bist_result, RMI_RO_ATTR,
+	       rmi_f09_overall_bist_result_show, rmi_store_error),
+	__ATTR(test_number_control, RMI_RW_ATTR,
+	       rmi_f09_test_number_control_show,
+	       rmi_f09_test_number_control_store),
+	__ATTR(test_result1, RMI_RO_ATTR,
+	       rmi_f09_test_result1_show, rmi_store_error),
+	__ATTR(test_result2, RMI_RO_ATTR,
+	       rmi_f09_test_result2_show, rmi_store_error),
+	__ATTR(run_bist, RMI_RW_ATTR,
+	       rmi_f09_run_bist_show, rmi_f09_run_bist_store),
+	__ATTR(f09_control_test1, RMI_RW_ATTR,
+	       rmi_f09_control_test1_show, rmi_f09_control_test1_store),
+	__ATTR(f09_control_test2, RMI_RW_ATTR,
+	       rmi_f09_control_test2_show, rmi_f09_control_test2_store),
+};
+
+static int rmi_f09_init(struct rmi_function_container *fc)
+{
+	int rc;
+
+	dev_info(&fc->dev, "Intializing F09 values.");
+
+	rc = rmi_f09_alloc_memory(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	rc = rmi_f09_initialize(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	rc = rmi_f09_create_sysfs(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	return 0;
+
+error_exit:
+	rmi_f09_free_memory(fc);
+
+	return rc;
+}
+
+static int rmi_f09_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_09_data *f09;
+
+	f09 = kzalloc(sizeof(struct rmi_fn_09_data), GFP_KERNEL);
+	if (!f09) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_09_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f09;
+
+	return 0;
+}
+
+static void rmi_f09_free_memory(struct rmi_function_container *fc)
+{
+	kfree(fc->data);
+	fc->data = NULL;
+}
+
+static int rmi_f09_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_device_platform_data *pdata;
+	struct rmi_fn_09_data *f09 = fc->data;
+	u16 query_base_addr;
+	int rc;
+
+
+	pdata = to_rmi_platform_data(rmi_dev);
+	query_base_addr = fc->fd.query_base_addr;
+
+	/* initial all default values for f09 query here */
+	rc = rmi_read_block(rmi_dev, query_base_addr,
+		(u8 *)&f09->query, sizeof(f09->query));
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to read query register."
+			" from 0x%04x\n", query_base_addr);
+		return rc;
+	}
+
+	return 0;
+}
+
+static int rmi_f09_create_sysfs(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	int rc;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+	/* Set up sysfs device attributes. */
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
+		if (sysfs_create_file
+		    (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
+			dev_err(&fc->dev, "Failed to create sysfs file for %s.",
+			     attrs[attr_count].attr.name);
+			rc = -ENODEV;
+			goto err_remove_sysfs;
+		}
+	}
+
+	return 0;
+
+err_remove_sysfs:
+	for (attr_count--; attr_count >= 0; attr_count--)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &attrs[attr_count].attr);
+
+	return rc;
+}
+
+static int rmi_f09_config(struct rmi_function_container *fc)
+{
+	/*we do nothing here. instead reset should notify the user.*/
+	return 0;
+}
+
+static int rmi_f09_reset(struct rmi_function_container *fc)
+{
+	struct  rmi_fn_09_data  *instance_data = fc->data;
+
+	instance_data->status = -ECONNRESET;
+
+	return 0;
+}
+
+static void rmi_f09_remove(struct rmi_function_container *fc)
+{
+	int attr_count;
+
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &attrs[attr_count].attr);
+
+	rmi_f09_free_memory(fc);
+}
+
+static int rmi_f09_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_fn_09_data *data = fc->data;
+	int error;
+	error = rmi_read_block(rmi_dev, fc->fd.command_base_addr,
+			(u8 *)&data->cmd, sizeof(data->cmd));
+
+	if (error < 0) {
+		dev_err(&fc->dev, "Failed to read command register.\n");
+		return error;
+	}
+	/* If the command register is cleared, meaning the value is 0 */
+	if (data->status == ECONNRESET) {
+		dev_warn(&rmi_dev->dev, "RESET occured: %#04x.\n",
+			data->status);
+	} else if (data->cmd.run_bist) {
+		dev_warn(&rmi_dev->dev,
+			"Command register not cleared: %#04x.\n",
+			data->cmd.run_bist);
+	}
+	return 0;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x09,
+	.init = rmi_f09_init,
+	.config = rmi_f09_config,
+	.attention = rmi_f09_attention,
+	.reset = rmi_f09_reset,
+	.remove = rmi_f09_remove
+};
+
+static int __init rmi_f09_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void rmi_f09_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+static ssize_t rmi_f09_status_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%d\n", instance_data->status);
+}
+
+static ssize_t rmi_f09_status_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	/* any write to status resets 1 */
+	instance_data->status = 0;
+
+	return 0;
+}
+
+static ssize_t rmi_f09_limit_register_count_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->query.limit_register_count);
+}
+
+static ssize_t rmi_f09_host_test_enable_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->query.host_test_enable);
+}
+
+static ssize_t rmi_f09_host_test_enable_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+	unsigned int new_value;
+	int result;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	if (sscanf(buf, "%u", &new_value) != 1) {
+		dev_err(dev,
+			"%s: Error - hostTestEnable has an invalid length.\n",
+			__func__);
+		return -EINVAL;
+	}
+
+	if (new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid hostTestEnable bit %s.",
+			__func__, buf);
+		return -EINVAL;
+	}
+	data->query.host_test_enable = new_value;
+	result = rmi_write(fc->rmi_dev, fc->fd.query_base_addr,
+		data->query.host_test_enable);
+	if (result < 0) {
+		dev_err(dev, "%s: Could not write hostTestEnable to 0x%x\n",
+				__func__, fc->fd.query_base_addr);
+		return result;
+	}
+
+	return count;
+
+}
+
+static ssize_t rmi_f09_internal_limits_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->query.internal_limits);
+}
+
+static ssize_t rmi_f09_result_register_count_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->query.result_register_count);
+}
+
+static ssize_t rmi_f09_overall_bist_result_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->data.overall_bist_result);
+}
+
+static ssize_t rmi_f09_test_result1_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->data.test_result1);
+}
+
+static ssize_t rmi_f09_test_result2_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->data.test_result2);
+}
+
+static ssize_t rmi_f09_run_bist_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->cmd.run_bist);
+}
+
+static ssize_t rmi_f09_run_bist_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+	unsigned int new_value;
+	int result;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	if (sscanf(buf, "%u", &new_value) != 1) {
+		dev_err(dev,
+		"%s: Error - run_bist_store has an "
+		"invalid len.\n",
+		__func__);
+		return -EINVAL;
+	}
+
+	if (new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid run_bist bit %s.", __func__, buf);
+		return -EINVAL;
+	}
+	data->cmd.run_bist = new_value;
+	result = rmi_write(fc->rmi_dev, fc->fd.command_base_addr,
+		data->cmd.run_bist);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write run_bist_store to 0x%x\n",
+				__func__, fc->fd.command_base_addr);
+		return result;
+	}
+
+	return count;
+
+}
+
+static ssize_t rmi_f09_control_test1_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u %u %u\n",
+			data->control.test1_limit_low,
+			data->control.test1_limit_high,
+			data->control.test1_limit_diff);
+}
+
+static ssize_t rmi_f09_control_test1_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+	unsigned int new_low, new_high, new_diff;
+	int result;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	if (sscanf(buf, "%u %u %u", &new_low, &new_high, &new_diff) != 3) {
+		dev_err(dev,
+		"%s: Error - f09_control_test1_store has an "
+		"invalid len.\n",
+		__func__);
+		return -EINVAL;
+	}
+
+	if (new_low < 0 || new_low > 1 || new_high < 0 || new_high
+			|| new_diff < 0 || new_diff) {
+		dev_err(dev, "%s: Invalid f09_control_test1_diff bit %s.",
+			__func__, buf);
+		return -EINVAL;
+	}
+	data->control.test1_limit_low = new_low;
+	data->control.test1_limit_high = new_high;
+	data->control.test1_limit_diff = new_diff;
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test1_limit_low);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test1_limit_low to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test1_limit_high);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test1_limit_high to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test1_limit_diff);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test1_limit_diff to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	return count;
+
+}
+
+static ssize_t rmi_f09_control_test2_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u %u %u\n",
+			data->control.test2_limit_low,
+			data->control.test2_limit_high,
+			data->control.test2_limit_diff);
+}
+
+static ssize_t rmi_f09_control_test2_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+	unsigned int new_low, new_high, new_diff;
+	int result;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	if (sscanf(buf, "%u %u %u", &new_low, &new_high, &new_diff) != 3) {
+		dev_err(dev,
+		"%s: Error - f09_control_test1_store has an "
+		"invalid len.\n",
+		__func__);
+		return -EINVAL;
+	}
+
+	if (new_low < 0 || new_low > 1 || new_high < 0 || new_high > 1 ||
+			new_diff < 0 || new_diff > 1) {
+		dev_err(dev, "%s: Invalid f09_control_test2_diff bit %s.",
+			__func__, buf);
+		return -EINVAL;
+	}
+	data->control.test2_limit_low = new_low;
+	data->control.test2_limit_high = new_high;
+	data->control.test2_limit_diff = new_diff;
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test2_limit_low);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test2_limit_low to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test2_limit_high);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test2_limit_high to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->control.test2_limit_diff);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write f09_control_test2_limit_diff to 0x%x\n",
+				__func__, fc->fd.control_base_addr);
+		return result;
+	}
+
+	return count;
+
+}
+
+
+static ssize_t rmi_f09_test_number_control_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			data->data.test_number_control);
+}
+
+static ssize_t rmi_f09_test_number_control_store(struct device *dev,
+				      struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_09_data *data;
+	unsigned int new_value;
+	int result;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	if (sscanf(buf, "%u", &new_value) != 1) {
+		dev_err(dev,
+		"%s: Error - test_number_control_store has an "
+		"invalid len.\n",
+		__func__);
+		return -EINVAL;
+	}
+
+	if (new_value < 0 || new_value > 1) {
+		dev_err(dev, "%s: Invalid test_number_control bit %s.",
+			__func__, buf);
+		return -EINVAL;
+	}
+	data->data.test_number_control = new_value;
+	result = rmi_write(fc->rmi_dev, fc->fd.control_base_addr,
+		data->data.test_number_control);
+	if (result < 0) {
+		dev_err(dev, "%s : Could not write "
+				"test_number_control_store to 0x%x\n",
+				__func__, fc->fd.data_base_addr);
+		return result;
+	}
+
+	return count;
+}
+
+module_init(rmi_f09_module_init);
+module_exit(rmi_f09_module_exit);
+
+MODULE_AUTHOR("Allie Xiong <[email protected]>");
+MODULE_DESCRIPTION("RMI F09 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#34 lore raw

RFC PATCH 15/17 input: RMI4 F34 device reflash

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f34.c |  917 ++++++++++++++++++++++++++++++++++++++++++
 drivers/input/rmi4/rmi_f34.h |   74 ++++
 2 files changed, 991 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f34.c new file +917 -0
new file mode 100644
index 0000000..a07f7f9
@@ -0,0 +1,917 @@
+/*
+ * Copyright (c) 2011, 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <linux/kernel.h>
+#include <linux/mutex.h>
+#include <linux/rmi.h>
+#include <linux/slab.h>
+#include <linux/version.h>
+#include "rmi_driver.h"
+#include "rmi_f34.h"
+
+#define BLK_SZ_OFF	3
+#define IMG_BLK_CNT_OFF	5
+#define CFG_BLK_CNT_OFF	7
+
+#define BLK_NUM_OFF 2
+
+/* data specific to fn $34 that needs to be kept around */
+struct rmi_fn_34_data {
+	unsigned char status;
+	unsigned char cmd;
+	unsigned short bootloaderid;
+	unsigned short blocksize;
+	unsigned short imageblockcount;
+	unsigned short configblockcount;
+	unsigned short blocknum;
+	bool inflashprogmode;
+	struct mutex attn_mutex;
+};
+
+static ssize_t rmi_fn_34_status_show(struct device *dev,
+				     struct device_attribute *attr, char *buf);
+
+
+static ssize_t rmi_fn_34_status_store(struct device *dev,
+				  struct device_attribute *attr,
+				  const char *buf, size_t count);
+
+static ssize_t rmi_fn_34_cmd_show(struct device *dev,
+				  struct device_attribute *attr, char *buf);
+
+static ssize_t rmi_fn_34_cmd_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count);
+
+static ssize_t rmi_fn_34_data_read(struct file *data_file, struct kobject *kobj,
+				   struct bin_attribute *attributes,
+				   char *buf, loff_t pos, size_t count);
+
+static ssize_t rmi_fn_34_data_write(struct file *data_file,
+				    struct kobject *kobj,
+				    struct bin_attribute *attributes, char *buf,
+				    loff_t pos, size_t count);
+
+static ssize_t rmi_fn_34_bootloaderid_show(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf);
+
+static ssize_t rmi_fn_34_bootloaderid_store(struct device *dev,
+					    struct device_attribute *attr,
+					    const char *buf, size_t count);
+
+static ssize_t rmi_fn_34_blocksize_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t rmi_fn_34_imageblockcount_show(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf);
+
+static ssize_t rmi_fn_34_configblockcount_show(struct device *dev,
+					       struct device_attribute *attr,
+					       char *buf);
+
+static ssize_t rmi_fn_34_blocknum_show(struct device *dev,
+				       struct device_attribute *attr,
+				       char *buf);
+
+static ssize_t rmi_fn_34_blocknum_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count);
+
+static ssize_t rmi_fn_34_rescanPDT_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count);
+
+
+static int rmi_f34_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f34_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f34_initialize(struct rmi_function_container *fc);
+
+static int rmi_f34_config(struct rmi_function_container *fc);
+
+static int rmi_f34_reset(struct rmi_function_container *fc);
+
+static int rmi_f34_create_sysfs(struct rmi_function_container *fc);
+
+
+
+static struct device_attribute attrs[] = {
+	__ATTR(status, RMI_RW_ATTR,
+	       rmi_fn_34_status_show, rmi_fn_34_status_store),
+
+	/* Also, sysfs will need to have a file set up to distinguish
+	 * between commands - like Config write/read, Image write/verify. */
+	__ATTR(cmd, RMI_RW_ATTR,
+	       rmi_fn_34_cmd_show, rmi_fn_34_cmd_store),
+	__ATTR(bootloaderid, RMI_RW_ATTR,
+	       rmi_fn_34_bootloaderid_show, rmi_fn_34_bootloaderid_store),
+	__ATTR(blocksize, RMI_RO_ATTR,
+	       rmi_fn_34_blocksize_show, rmi_store_error),
+	__ATTR(imageblockcount, RMI_RO_ATTR,
+	       rmi_fn_34_imageblockcount_show, rmi_store_error),
+	__ATTR(configblockcount, RMI_RO_ATTR,
+	       rmi_fn_34_configblockcount_show, rmi_store_error),
+	__ATTR(blocknum, RMI_RW_ATTR,
+	       rmi_fn_34_blocknum_show, rmi_fn_34_blocknum_store),
+	__ATTR(rescanPDT, RMI_WO_ATTR,
+	       rmi_show_error, rmi_fn_34_rescanPDT_store)
+};
+
+struct bin_attribute dev_attr_data = {
+	.attr = {
+		 .name = "data",
+		 .mode = 0666},
+	.size = 0,
+	.read = rmi_fn_34_data_read,
+	.write = rmi_fn_34_data_write,
+};
+
+
+static int rmi_f34_init(struct rmi_function_container *fc)
+{
+	int retval;
+
+	dev_info(&fc->dev, "Intializing f34 values.");
+
+	/* init instance data, fill in values and create any sysfs files */
+	retval = rmi_f34_alloc_memory(fc);
+	if (retval < 0)
+		goto exit_free_data;
+
+	retval = rmi_f34_initialize(fc);
+	if (retval < 0)
+		goto exit_free_data;
+
+	retval = rmi_f34_create_sysfs(fc);
+	if (retval < 0)
+		goto exit_free_data;
+
+	return 0;
+
+exit_free_data:
+	rmi_f34_free_memory(fc);
+
+	return retval;
+}
+
+static int rmi_f34_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_34_data *f34;
+
+	f34 = kzalloc(sizeof(struct rmi_fn_34_data), GFP_KERNEL);
+	if (!f34) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_34_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f34;
+
+	return 0;
+}
+
+static void rmi_f34_free_memory(struct rmi_function_container *fc)
+{
+	kfree(fc->data);
+	fc->data = NULL;
+}
+
+static int rmi_f34_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_device_platform_data *pdata;
+	int retval = 0;
+	struct rmi_fn_34_data *f34 = fc->data;
+	u16 query_base_addr;
+	u16 control_base_addr;
+	unsigned char buf[2];
+
+	pdata = to_rmi_platform_data(rmi_dev);
+	dev_dbg(&fc->dev, "Initializing F34 values for %s.\n",
+		pdata->sensor_name);
+
+	mutex_init(&f34->attn_mutex);
+
+	/* get the Bootloader ID and Block Size. */
+	query_base_addr = fc->fd.query_base_addr;
+	control_base_addr = fc->fd.control_base_addr;
+
+	retval = rmi_read_block(fc->rmi_dev, query_base_addr, buf,
+			ARRAY_SIZE(buf));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read bootloaderid from 0x%04x.\n",
+			query_base_addr);
+		return retval;
+	}
+
+	batohs(&f34->bootloaderid, buf);
+
+	retval = rmi_read_block(fc->rmi_dev, query_base_addr + BLK_SZ_OFF, buf,
+			ARRAY_SIZE(buf));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read block size from 0x%04x, "
+			"error=%d.\n", query_base_addr + BLK_SZ_OFF, retval);
+		return retval;
+	}
+	batohs(&f34->blocksize, buf);
+
+	/* Get firmware image block count and store it in the instance data */
+	retval = rmi_read_block(fc->rmi_dev, query_base_addr + IMG_BLK_CNT_OFF,
+			buf, ARRAY_SIZE(buf));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Couldn't read image block count from 0x%x, "
+			"error=%d.\n", query_base_addr + IMG_BLK_CNT_OFF,
+			retval);
+		return retval;
+	}
+	batohs(&f34->imageblockcount, buf);
+
+	/* Get config block count and store it in the instance data */
+	retval = rmi_read_block(fc->rmi_dev, query_base_addr + 7, buf,
+			ARRAY_SIZE(buf));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Couldn't read config block count from 0x%x, "
+			"error=%d.\n", query_base_addr + CFG_BLK_CNT_OFF,
+			retval);
+		return retval;
+	}
+	batohs(&f34->configblockcount, buf);
+
+	return 0;
+}
+
+static int rmi_f34_create_sysfs(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	int rc;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+
+	/* We need a sysfs file for the image/config block to write or read.
+	 * Set up sysfs bin file for binary data block. Since the image is
+	 * already in our format there is no need to convert the data for
+	 * endianess. */
+	rc = sysfs_create_bin_file(&fc->dev.kobj,
+				&dev_attr_data);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to create sysfs file for F34 data "
+		     "(error = %d).\n", rc);
+		return -ENODEV;
+	}
+
+	/* Set up sysfs device attributes. */
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
+		if (sysfs_create_file
+		    (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
+			dev_err(&fc->dev, "Failed to create sysfs file for %s.",
+			     attrs[attr_count].attr.name);
+			rc = -ENODEV;
+			goto err_remove_sysfs;
+		}
+	}
+
+	return 0;
+
+err_remove_sysfs:
+	sysfs_remove_bin_file(&fc->dev.kobj, &dev_attr_data);
+
+	for (attr_count--; attr_count >= 0; attr_count--)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &attrs[attr_count].attr);
+	return rc;
+}
+
+static int rmi_f34_config(struct rmi_function_container *fc)
+{
+	/* for this function we should do nothing here */
+	return 0;
+}
+
+
+static int rmi_f34_reset(struct rmi_function_container *fc)
+{
+	struct  rmi_fn_34_data  *instance_data = fc->data;
+
+	instance_data->status = ECONNRESET;
+
+	return 0;
+}
+
+static void rmi_f34_remove(struct rmi_function_container *fc)
+{
+	int attr_count;
+
+	sysfs_remove_bin_file(&fc->dev.kobj,
+						  &dev_attr_data);
+
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &attrs[attr_count].attr);
+
+	rmi_f34_free_memory(fc);
+}
+
+static int f34_read_status(struct rmi_function_container *fc)
+{
+	struct rmi_fn_34_data *instance_data = fc->data;
+	u16 data_base_addr = fc->fd.data_base_addr;
+	u8 status;
+	int retval;
+
+	if (instance_data->status == ECONNRESET)
+		return instance_data->status;
+
+	/* Read the Fn $34 status from F34_Flash_Data3 to see the previous
+	 * commands status. F34_Flash_Data3 will be the address after the
+	 * 2 block number registers plus blocksize Data registers.
+	 *  inform user space - through a sysfs param. */
+	retval = rmi_read(fc->rmi_dev,
+			  data_base_addr + instance_data->blocksize +
+			  BLK_NUM_OFF, &status);
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read status from 0x%x\n",
+		       data_base_addr + instance_data->blocksize + BLK_NUM_OFF);
+		status = 0xff;	/* failure */
+	}
+
+	/* set a sysfs value that the user mode can read - only
+	 * upper 4 bits are the status. successful is $80, anything
+	 * else is failure */
+	instance_data->status = status & 0xf0;
+
+	/* put mode into Flash Prog Mode when we successfully do
+	 * an Enable Flash Prog cmd. */
+	if ((instance_data->status == F34_STATUS_IDLE) &&
+		(instance_data->cmd == F34_ENABLE_FLASH_PROG))
+		instance_data->inflashprogmode = true;
+
+	return retval;
+}
+
+int rmi_f34_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	int retval;
+	struct rmi_fn_34_data *data = fc->data;
+
+	mutex_lock(&data->attn_mutex);
+	retval = f34_read_status(fc);
+	mutex_unlock(&data->attn_mutex);
+	return retval;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x34,
+	.init = rmi_f34_init,
+	.config = rmi_f34_config,
+	.reset = rmi_f34_reset,
+	.attention = rmi_f34_attention,
+	.remove = rmi_f34_remove
+};
+
+static ssize_t rmi_fn_34_bootloaderid_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->bootloaderid);
+}
+
+static ssize_t rmi_fn_34_bootloaderid_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf,
+					size_t count)
+{
+	int error;
+	unsigned long val;
+	unsigned char data[2];
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	u16 data_base_addr;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+
+	if (error)
+		return error;
+
+	instance_data->bootloaderid = val;
+
+	/* Write the Bootloader ID key data back to the first two Block
+	 * Data registers (F34_Flash_Data2.0 and F34_Flash_Data2.1). */
+	hstoba(data, (unsigned short)val);
+	data_base_addr = fc->fd.data_base_addr;
+
+	error = rmi_write_block(fc->rmi_dev,
+				data_base_addr + BLK_NUM_OFF,
+				data,
+				ARRAY_SIZE(data));
+
+	if (error < 0) {
+		dev_err(dev, "%s : Could not write bootloader id to 0x%x\n",
+		       __func__, data_base_addr + BLK_NUM_OFF);
+		return error;
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_34_blocksize_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->blocksize);
+}
+
+static ssize_t rmi_fn_34_imageblockcount_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			instance_data->imageblockcount);
+}
+
+static ssize_t rmi_fn_34_configblockcount_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			instance_data->configblockcount);
+}
+
+static ssize_t rmi_fn_34_status_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	int retval;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	mutex_lock(&instance_data->attn_mutex);
+	retval = f34_read_status(fc);
+	mutex_unlock(&instance_data->attn_mutex);
+
+	if (retval < 0)
+		return retval;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->status);
+}
+
+
+static ssize_t rmi_fn_34_status_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	instance_data->status = 0;
+
+	return 0;
+}
+
+
+static ssize_t rmi_fn_34_cmd_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->cmd);
+}
+
+static ssize_t rmi_fn_34_cmd_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf,
+				size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	unsigned long val;
+	u16 data_base_addr;
+	int error;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	data_base_addr = fc->fd.data_base_addr;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+	if (error)
+		return error;
+
+	/* make sure we are in Flash Prog mode for all cmds except the
+	 * Enable Flash Programming cmd - otherwise we are in error */
+	if ((val != F34_ENABLE_FLASH_PROG) && !instance_data->inflashprogmode) {
+		dev_err(dev, "%s: CANNOT SEND CMD %d TO SENSOR - "
+			"NOT IN FLASH PROG MODE\n"
+			, __func__, data_base_addr);
+		return -EINVAL;
+	}
+
+	instance_data->cmd = val;
+
+	/* Validate command value and (if necessary) write it to the command
+	 * register.
+	 */
+	switch (instance_data->cmd) {
+	case F34_ENABLE_FLASH_PROG:
+	case F34_ERASE_ALL:
+	case F34_ERASE_CONFIG:
+	case F34_WRITE_FW_BLOCK:
+	case F34_READ_CONFIG_BLOCK:
+	case F34_WRITE_CONFIG_BLOCK:
+		/* Reset the status to indicate we are in progress on a cmd. */
+		/* The status will change when the ATTN interrupt happens
+		   and the status of the cmd that was issued is read from
+		   the F34_Flash_Data3 register - result should be 0x80 for
+		   success - any other value indicates an error */
+
+		/* Issue the command to the device. */
+		error = rmi_write(fc->rmi_dev,
+				data_base_addr + instance_data->blocksize +
+				BLK_NUM_OFF, instance_data->cmd);
+
+		if (error < 0) {
+			dev_err(dev, "%s: Could not write command 0x%02x "
+				"to 0x%04x\n", __func__, instance_data->cmd,
+				data_base_addr + instance_data->blocksize +
+				BLK_NUM_OFF);
+			return error;
+		}
+
+		if (instance_data->cmd == F34_ENABLE_FLASH_PROG)
+			instance_data->inflashprogmode = true;
+
+		/* set status to indicate we are in progress */
+		instance_data->status = F34_STATUS_IN_PROGRESS;
+		break;
+	default:
+		dev_dbg(dev, "%s: RMI4 function $34 - "
+				"unknown command 0x%02lx.\n", __func__, val);
+		count = -EINVAL;
+		break;
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_34_blocknum_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->blocknum);
+}
+
+static ssize_t rmi_fn_34_blocknum_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf,
+					size_t count)
+{
+	int error;
+	unsigned long val;
+	unsigned char data[2];
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	u16 data_base_addr;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	data_base_addr = fc->fd.data_base_addr;
+
+	/* need to convert the string data to an actual value */
+	error = strict_strtoul(buf, 10, &val);
+
+	if (error)
+		return error;
+
+	instance_data->blocknum = val;
+
+	/* Write the Block Number data back to the first two Block
+	 * Data registers (F34_Flash_Data_0 and F34_Flash_Data_1). */
+	hstoba(data, (unsigned short)val);
+
+	error = rmi_write_block(fc->rmi_dev, data_base_addr,
+				data, ARRAY_SIZE(data));
+
+	if (error < 0) {
+		dev_err(dev, "%s : Could not write block number %u to 0x%x\n",
+		       __func__, instance_data->blocknum, data_base_addr);
+		return error;
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_34_rescanPDT_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	struct rmi_device *rmi_dev;
+	struct rmi_driver_data *driver_data;
+	struct pdt_entry pdt_entry;
+	bool fn01found = false;
+	bool fn34found = false;
+	unsigned int rescan;
+	int irq_count = 0;
+	int retval = 0;
+	int i;
+
+	/* Rescan of the PDT is needed since issuing the Flash Enable cmd
+	 * the device registers for Fn$01 and Fn$34 moving around because
+	 * of the change from Bootloader mode to Flash Programming mode
+	 * may change to a different PDT with only Fn$01 and Fn$34 that
+	 * could have addresses for query, control, data, command registers
+	 * that differ from the PDT scan done at device initialization. */
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+	rmi_dev = fc->rmi_dev;
+	driver_data = rmi_get_driverdata(rmi_dev);
+
+	/* Make sure we are only in Flash Programming mode  - DON'T
+	 * ALLOW THIS IN UI MODE. */
+	if (instance_data->cmd != F34_ENABLE_FLASH_PROG) {
+		dev_err(dev, "%s: NOT IN FLASH PROG MODE - CAN'T RESCAN PDT.\n"
+				, __func__);
+		return -EINVAL;
+	}
+
+	/* The only good value to write to this is 1, we allow 0, but with
+	 * no effect (this is consistent with the way the command bit works. */
+	if (sscanf(buf, "%u", &rescan) != 1)
+		return -EINVAL;
+	if (rescan < 0 || rescan > 1)
+		return -EINVAL;
+
+	/* 0 has no effect, so we skip it entirely. */
+	if (rescan) {
+		/* rescan the PDT - filling in Fn01 and Fn34 addresses -
+		 * this is only temporary - the device will need to be reset
+		 * to return the PDT to the normal values. */
+
+		/* mini-parse the PDT - we only have to get Fn$01 and Fn$34 and
+		   since we are Flash Programming mode we only have page 0. */
+		for (i = PDT_START_SCAN_LOCATION; i >= PDT_END_SCAN_LOCATION;
+			i -= sizeof(pdt_entry)) {
+			retval = rmi_read_block(rmi_dev, i, (u8 *)&pdt_entry,
+					       sizeof(pdt_entry));
+			if (retval != sizeof(pdt_entry)) {
+				dev_err(dev, "%s: err frm rmi_read_block pdt "
+					"entry data from PDT, "
+					"error = %d.", __func__, retval);
+				return retval;
+			}
+
+			if ((pdt_entry.function_number == 0x00) ||
+				(pdt_entry.function_number == 0xff))
+				break;
+
+			dev_dbg(dev, "%s: Found F%.2X\n",
+				__func__, pdt_entry.function_number);
+
+			/* f01 found - just fill in the new addresses in
+			 * the existing fc. */
+			if (pdt_entry.function_number == 0x01) {
+				struct rmi_function_container *f01_fc =
+					driver_data->f01_container;
+				fn01found = true;
+				f01_fc->fd.query_base_addr =
+					pdt_entry.query_base_addr;
+				f01_fc->fd.command_base_addr =
+				  pdt_entry.command_base_addr;
+				f01_fc->fd.control_base_addr =
+				  pdt_entry.control_base_addr;
+				f01_fc->fd.data_base_addr =
+				  pdt_entry.data_base_addr;
+				f01_fc->fd.function_number =
+				  pdt_entry.function_number;
+				f01_fc->fd.interrupt_source_count =
+				  pdt_entry.interrupt_source_count;
+				f01_fc->num_of_irqs =
+				  pdt_entry.interrupt_source_count;
+				f01_fc->irq_pos = irq_count;
+
+				irq_count += f01_fc->num_of_irqs;
+
+				if (fn34found)
+					break;
+			}
+
+			/* f34 found - just fill in the new addresses in
+			 * the existing fc. */
+			if (pdt_entry.function_number == 0x34) {
+				fn34found = true;
+				fc->fd.query_base_addr =
+				  pdt_entry.query_base_addr;
+				fc->fd.command_base_addr =
+				  pdt_entry.command_base_addr;
+				fc->fd.control_base_addr =
+				  pdt_entry.control_base_addr;
+				fc->fd.data_base_addr =
+				  pdt_entry.data_base_addr;
+				fc->fd.function_number =
+				  pdt_entry.function_number;
+				fc->fd.interrupt_source_count =
+				  pdt_entry.interrupt_source_count;
+				fc->num_of_irqs =
+				  pdt_entry.interrupt_source_count;
+				fc->irq_pos = irq_count;
+
+				irq_count += fc->num_of_irqs;
+
+				if (fn01found)
+					break;
+			}
+
+		}
+
+		if (!fn01found || !fn34found) {
+			dev_err(dev, "%s: failed to find fn$01 or fn$34 trying "
+				"to do rescan PDT.\n"
+				, __func__);
+			return -EINVAL;
+		}
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_34_data_read(struct file *data_file,
+				struct kobject *kobj,
+				struct bin_attribute *attributes,
+				char *buf,
+				loff_t pos,
+				size_t count)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	u16 data_base_addr;
+	int error;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	data_base_addr = fc->fd.data_base_addr;
+
+	if (count != instance_data->blocksize) {
+		dev_err(dev,
+			"%s : Incorrect F34 block size %d. "
+			"Expected size %d.\n",
+			__func__, count, instance_data->blocksize);
+		return -EINVAL;
+	}
+
+	/* Read the data from flash into buf.  The app layer will be blocked
+	 * at reading from the sysfs file.  When we return the count (or
+	 * error if we fail) the app will resume. */
+	error = rmi_read_block(fc->rmi_dev, data_base_addr + BLK_NUM_OFF,
+			(unsigned char *)buf, count);
+
+	if (error < 0) {
+		dev_err(dev, "%s : Could not read data from 0x%04x\n",
+		       __func__, data_base_addr + BLK_NUM_OFF);
+		return error;
+	}
+
+	return count;
+}
+
+static ssize_t rmi_fn_34_data_write(struct file *data_file,
+				struct kobject *kobj,
+				struct bin_attribute *attributes,
+				char *buf,
+				loff_t pos,
+				size_t count)
+{
+	struct device *dev = container_of(kobj, struct device, kobj);
+	struct rmi_function_container *fc;
+	struct rmi_fn_34_data *instance_data;
+	u16 data_base_addr;
+	int error;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	data_base_addr = fc->fd.data_base_addr;
+
+	/* Write the data from buf to flash. The app layer will be
+	 * blocked at writing to the sysfs file.  When we return the
+	 * count (or error if we fail) the app will resume. */
+
+	if (count != instance_data->blocksize) {
+		dev_err(dev,
+			"%s : Incorrect F34 block size %d. "
+			"Expected size %d.\n",
+			__func__, count, instance_data->blocksize);
+		return -EINVAL;
+	}
+
+	/* Write the data block - only if the count is non-zero  */
+	if (count) {
+		error = rmi_write_block(fc->rmi_dev,
+				data_base_addr + BLK_NUM_OFF,
+				(unsigned char *)buf,
+				count);
+
+		if (error < 0) {
+			dev_err(dev, "%s : Could not write block data "
+				"to 0x%x\n", __func__,
+				data_base_addr + BLK_NUM_OFF);
+			return error;
+		}
+	}
+
+	return count;
+}
+
+static int __init rmi_f34_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s : register failed !\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void rmi_f34_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+module_init(rmi_f34_module_init);
+module_exit(rmi_f34_module_exit);
+
+MODULE_AUTHOR("William Manson <[email protected]");
+MODULE_DESCRIPTION("RMI F34 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
drivers/input/rmi4/rmi_f34.h new file +74 -0

74 lines not on this page · raw message

#35 lore raw

RFC PATCH 16/17 input: RMI4 F41 Active pen 2D input

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f41.c | 1020 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1020 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f41.c new file +1020 -0
new file mode 100644
index 0000000..1bac4e3
@@ -0,0 +1,1020 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define FUNCTION_DATA rmi_fn_41_data
+#define FNUM 41
+
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+#define MAX_STR_LEN 256
+
+union f41_ap_query {
+	struct {
+		/* Query 0 */
+		u8 has_reduced_reporting:1;
+		u8 has_modal_control:1;
+		u8 has_force:1;
+		u8 has_orientation:1;
+		u8 has_serial_number:1;
+		u8 has_battery:1;
+		u8 has_z:1;
+		u8 has_single_tap:1;
+
+		/* Query 1 */
+		u8 number_of_buttons:3;
+		u8 f41_ap_query1_b3__7:5;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+
+union f41_ap_control_0 {
+	struct {
+		/* control 0 */
+		u8 reporting_mode:2;
+		u8 modal_ctrl:1;
+		u8 f41_ap_control0_b3__4:2;
+		u8 single_tap_interrupt_enable:1;
+		u8 in_range_interrupt_enable:1;
+		u8 new_sn_interrupt_enable:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f41_ap_control_1 {
+	struct {
+		/* control 1 */
+		u8 button_1_interrupt_enable:1;
+		u8 button_2_interrupt_enable:1;
+		u8 button_3_interrupt_enable:1;
+		u8 button_4_interrupt_enable:1;
+		u8 button_5_interrupt_enable:1;
+		u8 button_6_interrupt_enable:1;
+		u8 button_7_interrupt_enable:1;
+		u8 f41_ap_control1_b7:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f41_ap_control_2__5 {
+	struct {
+		u16 max_x_position;
+		u16 max_y_position;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[4];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f41_ap_control_6__9 {
+	struct {
+		u16 x_reduced_reporting_distance;
+		u16 y_reduced_reporting_distance;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[4];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f41_ap_control_10 {
+	struct {
+		u8 max_tap_time;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f41_ap_control {
+	union f41_ap_control_0 *reg_0;
+	union f41_ap_control_1 *reg_1;
+	union f41_ap_control_2__5 *reg_2__5;
+	union f41_ap_control_6__9 *reg_6__9;
+	union f41_ap_control_10 *reg_10;
+};
+
+
+union f41_ap_data_0__3 {
+	struct {
+		/* data 0-1 */
+		u16 x_position;
+
+		/* data 2-3 */
+		u16 y_position;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[4];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_4 {
+	struct {
+		/* data 4 */
+		u8 force;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_5 {
+	struct {
+		/* data 5 */
+		u8 button_1_state:1;
+		u8 button_2_state:1;
+		u8 button_3_state:1;
+		u8 button_4_state:1;
+		u8 button_5_state:1;
+		u8 button_6_state:1;
+		u8 button_7_state:1;
+		u8 f41_ap_data_5_b7:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_6 {
+	struct {
+		/* data 6 */
+		u8 single_tap:1;
+		u8 in_range:1;
+		u8 new_serial_number:1;
+		u8 f41_ap_data_6_b3__7:5;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_7__8 {
+	struct {
+		/* data 7 */
+		u8 azimuth;
+
+		/* data 8 */
+		u8 altitude;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_9 {
+	struct {
+		/* data 9 */
+		u8 z_position;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_10 {
+	struct {
+		/* data 10 */
+		u8 battery_level;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+	} __attribute__((__packed__));
+};
+
+union f41_ap_data_11__14 {
+	struct {
+		/* data 11-14 */
+		u32 pen_serial_number;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[4];
+	} __attribute__((__packed__));
+};
+
+struct f41_ap_data {
+	union f41_ap_data_0__3 *reg_0__3;
+	union f41_ap_data_4 *reg_4;
+	union f41_ap_data_5 *reg_5;
+	union f41_ap_data_6 *reg_6;
+	union f41_ap_data_7__8 *reg_7__8;
+	union f41_ap_data_9 *reg_9;
+	union f41_ap_data_10 *reg_10;
+	union f41_ap_data_11__14 *reg_11__14;
+};
+
+/* Query */
+show_union_struct_prototype(has_reduced_reporting)
+show_union_struct_prototype(has_modal_control)
+show_union_struct_prototype(has_force)
+show_union_struct_prototype(has_orientation)
+show_union_struct_prototype(has_serial_number)
+show_union_struct_prototype(has_battery)
+show_union_struct_prototype(has_z)
+show_union_struct_prototype(has_single_tap)
+show_union_struct_prototype(number_of_buttons)
+
+static struct attribute *attrs_q[] = {
+	attrify(has_reduced_reporting),
+	attrify(has_modal_control),
+	attrify(has_force),
+	attrify(has_orientation),
+	attrify(has_serial_number),
+	attrify(has_battery),
+	attrify(has_z),
+	attrify(has_single_tap),
+	attrify(number_of_buttons),
+	NULL
+};
+static struct attribute_group attrs_query = GROUP(attrs_q);
+
+/* control 0 */
+show_store_union_struct_prototype(reporting_mode)
+show_store_union_struct_prototype(modal_ctrl)
+show_store_union_struct_prototype(single_tap_interrupt_enable)
+show_store_union_struct_prototype(in_range_interrupt_enable)
+show_store_union_struct_prototype(new_sn_interrupt_enable)
+
+static struct attribute *attrs_reg_0a[] = {
+	attrify(reporting_mode),
+	NULL
+};
+static struct attribute *attrs_reg_0b[] = {
+	attrify(modal_ctrl),
+	NULL
+};
+static struct attribute *attrs_reg_0c[] = {
+	attrify(single_tap_interrupt_enable),
+	NULL
+};
+static struct attribute *attrs_reg_0d[] = {
+	attrify(in_range_interrupt_enable),
+	NULL
+};
+static struct attribute *attrs_reg_0e[] = {
+	attrify(new_sn_interrupt_enable),
+	NULL
+};
+
+/* control 1 */
+show_store_union_struct_prototype(button_1_interrupt_enable)
+show_store_union_struct_prototype(button_2_interrupt_enable)
+show_store_union_struct_prototype(button_3_interrupt_enable)
+show_store_union_struct_prototype(button_4_interrupt_enable)
+show_store_union_struct_prototype(button_5_interrupt_enable)
+show_store_union_struct_prototype(button_6_interrupt_enable)
+show_store_union_struct_prototype(button_7_interrupt_enable)
+static struct attribute *attrs_reg_1[] = {
+	attrify(button_1_interrupt_enable),
+	attrify(button_2_interrupt_enable),
+	attrify(button_3_interrupt_enable),
+	attrify(button_4_interrupt_enable),
+	attrify(button_5_interrupt_enable),
+	attrify(button_6_interrupt_enable),
+	attrify(button_7_interrupt_enable),
+	NULL
+};
+
+/* control 2-5 */
+show_store_union_struct_prototype(max_x_position)
+show_store_union_struct_prototype(max_y_position)
+static struct attribute *attrs_reg_2__5[] = {
+	attrify(max_x_position),
+	attrify(max_y_position),
+	NULL
+};
+
+/* control 6-9 */
+show_store_union_struct_prototype(x_reduced_reporting_distance)
+show_store_union_struct_prototype(y_reduced_reporting_distance)
+static struct attribute *attrs_reg_6__9[] = {
+	attrify(x_reduced_reporting_distance),
+	attrify(y_reduced_reporting_distance),
+	NULL
+};
+
+/* control 10 */
+show_store_union_struct_prototype(max_tap_time)
+static struct attribute *attrs_reg_10[] = {
+	attrify(max_tap_time),
+	NULL
+};
+
+
+static struct attribute_group attrs_ctrl[] = {
+	GROUP(attrs_reg_0a),
+	GROUP(attrs_reg_0b),
+	GROUP(attrs_reg_0c),
+	GROUP(attrs_reg_0d),
+	GROUP(attrs_reg_0e),
+	GROUP(attrs_reg_1),
+	GROUP(attrs_reg_2__5),
+	GROUP(attrs_reg_6__9),
+	GROUP(attrs_reg_10)
+};
+
+enum ctrl_reg_group {
+	f41_ctrl_reg_0a = 0,
+	f41_ctrl_reg_0b = 1,
+	f41_ctrl_reg_0c = 2,
+	f41_ctrl_reg_0d = 3,
+	f41_ctrl_reg_0e = 4,
+	f41_ctrl_reg_1 = 5,
+	f41_ctrl_reg_2__5 = 6,
+	f41_ctrl_reg_6__9 = 7,
+	f41_ctrl_reg_10 = 8
+};
+
+/* data 0-3 */
+show_union_struct_prototype(x_position)
+show_union_struct_prototype(y_position)
+static struct attribute *attrs_data_reg_0__3[] = {
+	attrify(x_position),
+	attrify(y_position),
+	NULL
+};
+
+/* data 4 */
+show_union_struct_prototype(force)
+static struct attribute *attrs_data_reg_4[] = {
+	attrify(force),
+	NULL
+};
+
+/* data 5 */
+/* Handled in button map */
+
+/* data 6 */
+show_union_struct_prototype(single_tap)
+show_union_struct_prototype(in_range)
+show_union_struct_prototype(new_serial_number)
+static struct attribute *attrs_data_reg_6a[] = {
+	attrify(single_tap),
+	NULL
+};
+static struct attribute *attrs_data_reg_6b[] = {
+	attrify(in_range),
+	NULL
+};
+static struct attribute *attrs_data_reg_6c[] = {
+	attrify(new_serial_number),
+	NULL
+};
+
+/* data 7-8 */
+show_union_struct_prototype(azimuth)
+show_union_struct_prototype(altitude)
+static struct attribute *attrs_data_reg_7__8[] = {
+	attrify(azimuth),
+	attrify(altitude),
+	NULL
+};
+
+/* data 9 */
+show_union_struct_prototype(z_position)
+static struct attribute *attrs_data_reg_9[] = {
+	attrify(z_position),
+	NULL
+};
+
+/* data 10 */
+show_union_struct_prototype(battery_level)
+static struct attribute *attrs_data_reg_10[] = {
+	attrify(battery_level),
+	NULL
+};
+
+/* data 11-14 */
+show_union_struct_prototype(pen_serial_number)
+static struct attribute *attrs_data_reg_11__14[] = {
+	attrify(pen_serial_number),
+	NULL
+};
+
+static struct attribute_group attrs_data[] = {
+	GROUP(attrs_data_reg_0__3),
+	GROUP(attrs_data_reg_4),
+	GROUP(attrs_data_reg_6a),
+	GROUP(attrs_data_reg_6b),
+	GROUP(attrs_data_reg_6c),
+	GROUP(attrs_data_reg_7__8),
+	GROUP(attrs_data_reg_9),
+	GROUP(attrs_data_reg_10),
+	GROUP(attrs_data_reg_11__14)
+};
+
+enum data_reg_group {
+	f41_data_reg_0__3 = 0,
+	f41_data_reg_4 = 1,
+	f41_data_reg_6a = 2,
+	f41_data_reg_6b = 3,
+	f41_data_reg_6c = 4,
+	f41_data_reg_7__8 = 5,
+	f41_data_reg_9 = 6,
+	f41_data_reg_10 = 7,
+	f41_data_reg_11__14 = 8
+};
+
+/* data specific to fn $41 that needs to be kept around */
+struct rmi_fn_41_data {
+	union f41_ap_query query;
+	struct f41_ap_control control;
+	struct f41_ap_data data;
+	u8 *data_block;
+	u8 data_block_size;
+
+	bool attrs_ctrl_regs_exist[ARRAY_SIZE(attrs_ctrl)];
+	bool attrs_data_regs_exist[ARRAY_SIZE(attrs_data)];
+
+	struct mutex control_mutex;
+	struct mutex data_mutex;
+
+	unsigned char button_count;
+	unsigned short *button_map;
+	char input_name[MAX_STR_LEN];
+	char input_phys[MAX_STR_LEN];
+	struct input_dev *input;
+};
+
+static void rmi_f41_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f41_initialize(struct rmi_function_container *fc);
+
+static int rmi_f41_register_device(struct rmi_function_container *fc);
+
+static int rmi_f41_config(struct rmi_function_container *fc);
+
+static int rmi_f41_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f41_init(struct rmi_function_container *fc)
+{
+	int retval = 0;
+
+	dev_dbg(&fc->dev, "Intializing F41.");
+
+	retval = rmi_f41_initialize(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f41_register_device(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	retval = rmi_f41_create_sysfs(fc);
+	if (retval < 0)
+		goto error_exit;
+
+	return retval;
+
+error_exit:
+	rmi_f41_free_memory(fc);
+
+	return retval;
+}
+
+
+static void rmi_f41_free_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_41_data *f41 = fc->data;
+	int i;
+
+	/* query */
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+	/* control */
+	for (i = 0; i < ARRAY_SIZE(attrs_ctrl); i++) {
+		if (f41->attrs_ctrl_regs_exist[i])
+			sysfs_remove_group(&fc->dev.kobj, &attrs_ctrl[i]);
+	}
+	for (i = 0; i < ARRAY_SIZE(attrs_data); i++) {
+		if (f41->attrs_data_regs_exist[i])
+			sysfs_remove_group(&fc->dev.kobj, &attrs_data[i]);
+	}
+	if (f41) {
+		kfree(f41->control.reg_0);
+		kfree(f41->control.reg_1);
+		kfree(f41->control.reg_2__5);
+		kfree(f41->control.reg_6__9);
+		kfree(f41->control.reg_10);
+
+		kfree(f41->data_block);
+		kfree(f41->button_map);
+		kfree(f41);
+		fc->data = NULL;
+	}
+}
+
+
+static int rmi_f41_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_fn_41_data *f41;
+	struct rmi_device_platform_data *pdata;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	int retval = 0;
+	u16 next_loc;
+	int i;
+	u8 *data_loc;
+
+	f41 = kzalloc(sizeof(struct rmi_fn_41_data), GFP_KERNEL);
+	if (!f41) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_41_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f41;
+
+	/* Read F41 Query Data */
+	f41->query.address = fc->fd.query_base_addr;
+	retval = rmi_read_block(fc->rmi_dev, f41->query.address,
+		(u8 *)&f41->query, sizeof(f41->query.regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read query registers from 0x%04x\n",
+				f41->query.address);
+		return retval;
+	}
+
+	/* Initialize Control Data */
+	next_loc = fc->fd.control_base_addr;
+
+	if (f41->query.has_reduced_reporting == 1 ||
+			f41->query.has_modal_control == 1 ||
+			f41->query.has_single_tap == 1 ||
+			f41->query.has_z == 1 ||
+			f41->query.has_serial_number == 1) {
+		f41->control.reg_0 = kzalloc(sizeof(union f41_ap_control_0),
+				 GFP_KERNEL);
+		if (!f41->control.reg_0) {
+			dev_err(&fc->dev, "Failed to allocate control register 0.");
+			return -ENOMEM;
+		}
+		f41->control.reg_0->address = next_loc;
+		next_loc += sizeof(f41->control.reg_0->regs);
+		retval = rmi_read_block(fc->rmi_dev,
+				f41->control.reg_0->address,
+				f41->control.reg_0->regs,
+				sizeof(f41->control.reg_0->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Could not read Control register 0 from 0x%04x\n",
+					f41->control.reg_0->address);
+			return retval;
+		}
+	}
+
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0a] =
+					f41->query.has_reduced_reporting == 1;
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0b] =
+					f41->query.has_modal_control == 1;
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0c] =
+					f41->query.has_single_tap == 1;
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0d] =
+					f41->query.has_z == 1;
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0e] =
+					f41->query.has_serial_number == 1;
+
+	if (f41->query.number_of_buttons > 0) {
+		f41->control.reg_1 = kzalloc(sizeof(union f41_ap_control_1),
+							GFP_KERNEL);
+		if (!f41->control.reg_1) {
+			dev_err(&fc->dev, "Failed to allocate control registers.");
+			return -ENOMEM;
+		}
+		f41->control.reg_1->address = next_loc;
+		next_loc += sizeof(f41->control.reg_1->regs);
+		retval = rmi_read_block(fc->rmi_dev,
+				f41->control.reg_1->address,
+				f41->control.reg_1->regs,
+				sizeof(f41->control.reg_1->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Could not read Control register 1 from 0x%04x\n",
+						f41->control.reg_1->address);
+			return retval;
+		}
+		f41->attrs_ctrl_regs_exist[f41_ctrl_reg_1] = true;
+	}
+
+	f41->control.reg_2__5 =	kzalloc(sizeof(union f41_ap_control_2__5),
+					GFP_KERNEL);
+	if (!f41->control.reg_2__5) {
+		dev_err(&fc->dev, "Failed to allocate control registers 2-5.");
+		return -ENOMEM;
+	}
+	f41->control.reg_2__5->address = next_loc;
+	next_loc += sizeof(f41->control.reg_2__5->regs);
+	retval = rmi_read_block(fc->rmi_dev, f41->control.reg_2__5->address,
+				f41->control.reg_2__5->regs,
+				sizeof(f41->control.reg_2__5->regs));
+	if (retval < 0) {
+		dev_err(&fc->dev, "Could not read Control registers 2-5 from 0x%04x\n",
+				f41->control.reg_2__5->address);
+		return retval;
+	}
+	f41->attrs_ctrl_regs_exist[f41_ctrl_reg_2__5] = true;
+
+	if (f41->query.has_reduced_reporting == 1) {
+		f41->control.reg_6__9 =
+			kzalloc(sizeof(union f41_ap_control_6__9), GFP_KERNEL);
+		if (!f41->control.reg_6__9) {
+			dev_err(&fc->dev, "Failed to allocate control registers 6-9.");
+			return -ENOMEM;
+		}
+		f41->control.reg_6__9->address = next_loc;
+		next_loc += sizeof(f41->control.reg_6__9->regs);
+		retval = rmi_read_block(fc->rmi_dev,
+				f41->control.reg_6__9->address,
+				f41->control.reg_6__9->regs,
+				sizeof(f41->control.reg_6__9->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Could not read Control registers 6-9 from 0x%04x\n",
+						f41->control.reg_6__9->address);
+			return retval;
+		}
+		f41->attrs_ctrl_regs_exist[f41_ctrl_reg_6__9] = true;
+	}
+
+	if (f41->query.has_single_tap == 1) {
+		f41->control.reg_10 = kzalloc(sizeof(union f41_ap_control_10),
+							GFP_KERNEL);
+		if (!f41->control.reg_10) {
+			dev_err(&fc->dev, "Failed to allocate control register 10.");
+			return -ENOMEM;
+		}
+		f41->control.reg_10->address = next_loc;
+		next_loc += sizeof(f41->control.reg_10->regs);
+		retval = rmi_read_block(fc->rmi_dev,
+					f41->control.reg_10->address,
+					f41->control.reg_10->regs,
+					sizeof(f41->control.reg_10->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "Could not read Control register 10 from 0x%04x\n",
+						f41->control.reg_10->address);
+			return retval;
+		}
+		f41->attrs_ctrl_regs_exist[f41_ctrl_reg_10] = true;
+	}
+
+
+	/* initialize data registers */
+
+	f41->data_block = kzalloc(sizeof(union f41_ap_data_0__3)
+					+ sizeof(union f41_ap_data_4)
+					+ sizeof(union f41_ap_data_5)
+					+ sizeof(union f41_ap_data_6)
+					+ sizeof(union f41_ap_data_7__8)
+					+ sizeof(union f41_ap_data_9)
+					+ sizeof(union f41_ap_data_10)
+					+ sizeof(union f41_ap_data_11__14),
+				GFP_KERNEL);
+	if (!f41->data_block) {
+		dev_err(&fc->dev, "Failed to allocate data registers.");
+			return -ENOMEM;
+	}
+	data_loc = f41->data_block;
+	/* data 0-3 */
+
+	f41->data.reg_0__3 = (union f41_ap_data_0__3 *) data_loc;
+	data_loc += sizeof(union f41_ap_data_0__3);
+	f41->attrs_data_regs_exist[f41_data_reg_0__3] = true;
+
+	/* data 4 */
+	if (f41->query.has_force == 1) {
+		f41->data.reg_4 = (union f41_ap_data_4 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_4);
+		f41->attrs_data_regs_exist[f41_data_reg_4] = true;
+	}
+
+	/* data 5 */
+	/* button map */
+	f41->button_map = kcalloc(f41->query.number_of_buttons,
+					sizeof(unsigned short), GFP_KERNEL);
+	if (!f41->button_map) {
+		dev_err(&fc->dev, "Failed to allocate button map.\n");
+		return -ENOMEM;
+	}
+	if (f41->query.number_of_buttons > 0) {
+		f41->data.reg_5 = (union f41_ap_data_5 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_5);
+	}
+	pdata = to_rmi_platform_data(rmi_dev);
+	if (pdata) {
+		if (!pdata->f41_button_map)
+			dev_warn(&fc->dev, "%s - button_map is NULL", __func__);
+		else if (!pdata->f41_button_map->map)
+			dev_warn(&fc->dev,
+				 "Platformdata button map is missing!\n");
+		else {
+			if (pdata->f41_button_map->nbuttons !=
+						f41->query.number_of_buttons)
+				dev_warn(&fc->dev,
+					"Platformdata button map size (%d) != number of buttons on device (%d).\n",
+					pdata->f41_button_map->nbuttons,
+					f41->query.number_of_buttons);
+			f41->button_count = min(f41->query.number_of_buttons,
+					 pdata->f41_button_map->nbuttons);
+			for (i = 0; i < f41->button_count; i++)
+				f41->button_map[i] =
+					pdata->f41_button_map->map[i];
+		}
+	}
+
+	/* data 6 */
+	if (f41->query.has_z == 1 || f41->query.has_single_tap == 1
+					|| f41->query.has_serial_number == 1) {
+		f41->data.reg_6 = (union f41_ap_data_6 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_6);
+	}
+	f41->attrs_data_regs_exist[f41_data_reg_6a] =
+					f41->query.has_single_tap == 1;
+	f41->attrs_data_regs_exist[f41_data_reg_6b] = f41->query.has_z == 1;
+	f41->attrs_data_regs_exist[f41_data_reg_6c] =
+					f41->query.has_serial_number == 1;
+
+	/* data 7-8 */
+	if (f41->query.has_orientation == 1) {
+		f41->data.reg_7__8 = (union f41_ap_data_7__8 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_7__8);
+		f41->attrs_data_regs_exist[f41_data_reg_7__8] = true;
+	}
+
+	/* data 9 */
+	if (f41->query.has_z == 1) {
+		f41->data.reg_9 = (union f41_ap_data_9 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_9);
+		f41->attrs_data_regs_exist[f41_data_reg_9] = true;
+	}
+
+	/* data 10 */
+	if (f41->query.has_battery == 1) {
+		f41->data.reg_10 = (union f41_ap_data_10 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_10);
+		f41->attrs_data_regs_exist[f41_data_reg_10] = true;
+	}
+
+	/* data 11-14 */
+	if (f41->query.has_serial_number > 0) {
+		f41->data.reg_11__14 = (union f41_ap_data_11__14 *) data_loc;
+		data_loc += sizeof(union f41_ap_data_11__14);
+		f41->attrs_data_regs_exist[f41_data_reg_11__14] = true;
+	}
+
+	f41->data_block_size = data_loc - f41->data_block;
+	mutex_init(&f41->control_mutex);
+	mutex_init(&f41->data_mutex);
+	return 0;
+}
+
+
+static int rmi_f41_create_sysfs(struct rmi_function_container *fc)
+{
+	u8 attr_num;
+	struct rmi_fn_41_data *f41 = fc->data;
+	dev_dbg(&fc->dev, "Creating F41 sysfs files.");
+
+	/* Set up sysfs device attributes. */
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+	for (attr_num = 0; attr_num < ARRAY_SIZE(attrs_ctrl); attr_num++) {
+		if (f41->attrs_ctrl_regs_exist[attr_num]) {
+			if (sysfs_create_group(&fc->dev.kobj,
+					&attrs_ctrl[attr_num]) < 0) {
+				dev_err(&fc->dev, "Failed to create sysfs file group for reg group %d.",
+								attr_num);
+				return -ENODEV;
+			}
+		}
+	}
+	for (attr_num = 0; attr_num < ARRAY_SIZE(attrs_data); attr_num++) {
+		if (f41->attrs_data_regs_exist[attr_num]) {
+			if (sysfs_create_group(&fc->dev.kobj,
+					&attrs_data[attr_num]) < 0) {
+				dev_err(&fc->dev, "Failed to create sysfs file group for reg group %d.",
+								attr_num);
+				return -ENODEV;
+			}
+		}
+	}
+	return 0;
+}
+
+
+static int rmi_f41_config(struct rmi_function_container *fc)
+{
+	struct rmi_fn_41_data *f41 = fc->data;
+
+	/* Write Control Register values back to device */
+	if (f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0a]
+		|| f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0b]
+		|| f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0c]
+		|| f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0d]
+		|| f41->attrs_ctrl_regs_exist[f41_ctrl_reg_0e])
+		rmi_write_block(fc->rmi_dev, f41->control.reg_0->address,
+					(u8 *)f41->control.reg_0,
+					sizeof(f41->control.reg_0->regs));
+	if (f41->attrs_ctrl_regs_exist[f41_ctrl_reg_1])
+		rmi_write_block(fc->rmi_dev, f41->control.reg_1->address,
+					(u8 *)f41->control.reg_1,
+					sizeof(f41->control.reg_1->regs));
+	if (f41->attrs_ctrl_regs_exist[f41_ctrl_reg_2__5])
+		rmi_write_block(fc->rmi_dev, f41->control.reg_2__5->address,
+					(u8 *)f41->control.reg_2__5,
+					sizeof(f41->control.reg_2__5->regs));
+	if (f41->attrs_ctrl_regs_exist[f41_ctrl_reg_6__9])
+		rmi_write_block(fc->rmi_dev, f41->control.reg_6__9->address,
+					(u8 *)f41->control.reg_6__9,
+					sizeof(f41->control.reg_6__9->regs));
+	if (f41->attrs_ctrl_regs_exist[f41_ctrl_reg_10])
+		rmi_write_block(fc->rmi_dev, f41->control.reg_10->address,
+					(u8 *)f41->control.reg_10,
+					sizeof(f41->control.reg_10->regs));
+
+	return 0;
+}
+
+static void rmi_f41_remove(struct rmi_function_container *fc)
+{
+	struct rmi_fn_41_data *f41 = fc->data;
+	dev_dbg(&fc->dev, "Removing F41.");
+	input_unregister_device(f41->input);
+	rmi_f41_free_memory(fc);
+}
+
+/* sysfs functions */
+/* Query */
+simple_show_union_struct_unsigned(query, has_reduced_reporting)
+simple_show_union_struct_unsigned(query, has_modal_control)
+simple_show_union_struct_unsigned(query, has_force)
+simple_show_union_struct_unsigned(query, has_orientation)
+simple_show_union_struct_unsigned(query, has_serial_number)
+simple_show_union_struct_unsigned(query, has_battery)
+simple_show_union_struct_unsigned(query, has_z)
+simple_show_union_struct_unsigned(query, has_single_tap)
+simple_show_union_struct_unsigned(query, number_of_buttons)
+
+/* Control */
+show_store_union_struct_unsigned(control, reg_0, reporting_mode)
+show_store_union_struct_unsigned(control, reg_0, modal_ctrl)
+show_store_union_struct_unsigned(control, reg_0, single_tap_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_0, in_range_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_0, new_sn_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_1_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_2_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_3_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_4_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_5_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_6_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_1, button_7_interrupt_enable)
+show_store_union_struct_unsigned(control, reg_2__5, max_x_position)
+show_store_union_struct_unsigned(control, reg_2__5, max_y_position)
+show_store_union_struct_unsigned(control,
+					reg_6__9, x_reduced_reporting_distance)
+show_store_union_struct_unsigned(control,
+					reg_6__9, y_reduced_reporting_distance)
+show_store_union_struct_unsigned(control, reg_10, max_tap_time)
+
+
+/* Data */
+simple_show_union_struct_unsigned2(data, reg_0__3, x_position)
+simple_show_union_struct_unsigned2(data, reg_0__3, y_position)
+simple_show_union_struct_unsigned2(data, reg_4, force)
+simple_show_union_struct_unsigned2(data, reg_6, single_tap)
+simple_show_union_struct_unsigned2(data, reg_6, in_range)
+simple_show_union_struct_unsigned2(data, reg_6, new_serial_number)
+simple_show_union_struct_unsigned2(data, reg_7__8, azimuth)
+simple_show_union_struct_unsigned2(data, reg_7__8, altitude)
+simple_show_union_struct_unsigned2(data, reg_9, z_position)
+simple_show_union_struct_unsigned2(data, reg_10, battery_level)
+simple_show_union_struct_unsigned2(data, reg_11__14, pen_serial_number)
+
+static int rmi_f41_register_device(struct rmi_function_container *fc)
+{
+	int i;
+	int rc;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_fn_41_data *f41 = fc->data;
+	struct input_dev *input_dev = input_allocate_device();
+
+	if (!input_dev) {
+		dev_err(&fc->dev, "Failed to allocate input device.\n");
+		return -ENOMEM;
+	}
+
+	f41->input = input_dev;
+	snprintf(f41->input_name, MAX_STR_LEN, "%sfn%02x",
+			dev_name(&rmi_dev->dev), fc->fd.function_number);
+	input_dev->name = f41->input_name;
+	snprintf(f41->input_phys, MAX_STR_LEN, "%s/input0", input_dev->name);
+	input_dev->phys = f41->input_phys;
+	input_dev->dev.parent = &rmi_dev->dev;
+	input_set_drvdata(input_dev, f41);
+
+	/* Set up any input events. */
+	set_bit(EV_SYN, input_dev->evbit);
+	set_bit(EV_KEY, input_dev->evbit);
+	/* set bits for each button... */
+	for (i = 0; i < f41->button_count; i++)
+		set_bit(f41->button_map[i], input_dev->keybit);
+	rc = input_register_device(input_dev);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to register input device.\n");
+		goto error_free_device;
+	}
+
+	return 0;
+
+error_free_device:
+	input_free_device(input_dev);
+
+	return rc;
+}
+
+static int rmi_f41_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	int error;
+	int button;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_fn_41_data *f41 = fc->data;
+
+
+	/* Read all the data registers. */
+	error = rmi_read_block(rmi_dev, fc->fd.data_base_addr, f41->data_block,
+			f41->data_block_size);
+	if (error < 0) {
+		dev_err(&fc->dev, "Failed to read data registers.\n");
+		return error;
+	}
+
+	/* Generate events for buttons that change state. */
+	for (button = 0; button < f41->button_count; button++) {
+		bool button_status;
+		/* bit shift to get button's status */
+		button_status =
+			((f41->data.reg_5->regs[0] >> button) & 0x01) != 0;
+		/* Generate an event here. */
+		input_report_key(f41->input, f41->button_map[button],
+				 button_status);
+	}
+
+	input_sync(f41->input); /* sync after groups of events */
+	return 0;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x41,
+	.init = rmi_f41_init,
+	.config = rmi_f41_config,
+	.remove = rmi_f41_remove,
+	.attention = rmi_f41_attention
+};
+
+static int __init rmi_f41_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+	return 0;
+}
+
+static void rmi_f41_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+module_init(rmi_f41_module_init);
+module_exit(rmi_f41_module_exit);
+
+MODULE_AUTHOR("Daniel Rosenberg <[email protected]>");
+MODULE_DESCRIPTION("RMI F41 module");
+MODULE_LICENSE("GPL");
#29 lore raw

RFC PATCH 12/17 input: RMI4 F1A simple capacitive buttons

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f1a.c | 1025 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1025 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f1a.c new file +1025 -0
new file mode 100644
index 0000000..a7cccfa
@@ -0,0 +1,1025 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#define FUNCTION_DATA f1a_data
+#define FNUM 1a
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+#define QUERY_BASE_INDEX 1
+#define MAX_NAME_LEN 256
+#define MAX_BUFFER_LEN 80
+
+#define FILTER_MODE_MIN				0
+#define FILTER_MODE_MAX				3
+#define MULTI_BUTTON_REPORT_MIN			0
+#define MULTI_BUTTON_REPORT_MAX			3
+#define TX_RX_BUTTON_MIN			0
+#define TX_RX_BUTTON_MAX			255
+#define THREADHOLD_BUTTON_MIN			0
+#define THREADHOLD_BUTTON_MAX			255
+#define RELEASE_THREADHOLD_BUTTON_MIN		0
+#define RELEASE_THREADHOLD_BUTTON_MAX		255
+#define STRONGEST_BUTTON_HYSTERESIS_MIN		0
+#define STRONGEST_BUTTON_HYSTERESIS_MAX		255
+#define FILTER_STRENGTH_MIN			0
+#define FILTER_STRENGTH_MAX			255
+
+union f1a_0d_query {
+	struct {
+		u8 max_button_count:3;
+		u8 reserved:5;
+
+		u8 has_general_control:1;
+		u8 has_interrupt_enable:1;
+		u8 has_multibutton_select:1;
+		u8 has_tx_rx_map:1;
+		u8 has_perbutton_threshold:1;
+		u8 has_release_threshold:1;
+		u8 has_strongestbtn_hysteresis:1;
+		u8 has_filter_strength:1;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[2];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f1a_0d_control_0 {
+	struct {
+		u8 multibutton_report:2;
+		u8 filter_mode:2;
+	} __attribute__((__packed__));
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f1a_0d_control_1n {
+	u8 interrupt_enabled_button;
+};
+
+struct f1a_0d_control_1 {
+	struct f1a_0d_control_1n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f1a_0d_control_2n {
+	u8 multi_button;
+};
+
+struct f1a_0d_control_2 {
+	struct f1a_0d_control_2n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f1a_0d_control_3_4n {
+	u8 transmitterbtn;
+	u8 receiverbtn;
+} __attribute__((__packed__));
+
+struct f1a_0d_control_3_4 {
+	struct f1a_0d_control_3_4n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+struct f1a_0d_control_5n {
+	u8 threshold_button;
+};
+
+struct f1a_0d_control_5 {
+	struct f1a_0d_control_5n *regs;
+	u16 address;
+	u8 length;
+} __attribute__((__packed__));
+
+union f1a_0d_control_6 {
+	struct {
+		u8 button_release_threshold;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f1a_0d_control_7 {
+	struct {
+		u8 strongest_button_hysteresis;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+union f1a_0d_control_8 {
+	struct {
+		u8 filter_strength;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	} __attribute__((__packed__));
+};
+
+struct f1a_0d_control {
+	union f1a_0d_control_0 *reg_0;
+	struct f1a_0d_control_1 *reg_1;
+	struct f1a_0d_control_2 *reg_2;
+	struct f1a_0d_control_3_4 *reg_3_4;
+	struct f1a_0d_control_5 *reg_5;
+	union f1a_0d_control_6 *reg_6;
+	union f1a_0d_control_7 *reg_7;
+	union f1a_0d_control_8 *reg_8;
+};
+
+/* data specific to fn $1a that needs to be kept around */
+struct f1a_data {
+	struct f1a_0d_control control;
+	union f1a_0d_query query;
+	u8 sensor_button_count;
+	int button_bitmask_size;
+	u8 *button_data_buffer;
+	u8 button_map_size;
+	u8 *button_map;
+	char input_name[MAX_NAME_LEN];
+	char input_phys[MAX_NAME_LEN];
+	struct input_dev *input;
+	struct mutex control_mutex;
+	struct mutex data_mutex;
+};
+
+
+static int rmi_f1a_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f1a_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f1a_initialize(struct rmi_function_container *fc);
+
+static int rmi_f1a_register_device(struct rmi_function_container *fc);
+
+static int rmi_f1a_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f1a_config(struct rmi_function_container *fc);
+
+static int rmi_f1a_reset(struct rmi_function_container *fc);
+
+/* Query sysfs files */
+show_union_struct_prototype(max_button_count)
+show_union_struct_prototype(has_general_control)
+show_union_struct_prototype(has_interrupt_enable)
+show_union_struct_prototype(has_multibutton_select)
+show_union_struct_prototype(has_tx_rx_map)
+show_union_struct_prototype(has_perbutton_threshold)
+show_union_struct_prototype(has_release_threshold)
+show_union_struct_prototype(has_strongestbtn_hysteresis)
+show_union_struct_prototype(has_filter_strength)
+
+static struct attribute *attrs[] = {
+	attrify(max_button_count),
+	attrify(has_general_control),
+	attrify(has_interrupt_enable),
+	attrify(has_multibutton_select),
+	attrify(has_tx_rx_map),
+	attrify(has_perbutton_threshold),
+	attrify(has_release_threshold),
+	attrify(has_strongestbtn_hysteresis),
+	attrify(has_filter_strength),
+	NULL
+};
+
+static struct attribute_group attrs_query = GROUP(attrs);
+
+/* Control sysfs files */
+show_store_union_struct_prototype(multibutton_report)
+show_store_union_struct_prototype(filter_mode)
+show_store_union_struct_prototype(interrupt_enabled_button)
+show_store_union_struct_prototype(multi_button)
+show_union_struct_prototype(tx_rx_map)
+show_store_union_struct_prototype(threshold_button)
+show_store_union_struct_prototype(button_release_threshold)
+show_store_union_struct_prototype(strongest_button_hysteresis)
+show_store_union_struct_prototype(filter_strength)
+
+static int rmi_f1a_read_control_parameters(struct rmi_device *rmi_dev,
+	struct f1a_data *f1a)
+{
+	int retval = 0;
+	union f1a_0d_query *query = &f1a->query;
+	struct f1a_0d_control *control = &f1a->control;
+
+	if (query->has_general_control) {
+		retval = rmi_read_block(rmi_dev, control->reg_0->address,
+				(u8 *)control->reg_0->regs,
+				sizeof(control->reg_0->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg0 to %#06x.\n",
+					control->reg_0->address);
+			return retval;
+		}
+	}
+
+	if (query->has_interrupt_enable) {
+		retval = rmi_read_block(rmi_dev, control->reg_1->address,
+			(u8 *)control->reg_1->regs, f1a->button_bitmask_size);
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg1 to %#06x.\n",
+				 control->reg_1->address);
+			return retval;
+		}
+	}
+
+	if (query->has_multibutton_select) {
+		retval = rmi_read_block(rmi_dev, control->reg_2->address,
+			(u8 *)control->reg_2->regs, f1a->button_bitmask_size);
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg2 to %#06x.\n",
+				 control->reg_2->address);
+			return retval;
+		}
+	}
+
+	if (query->has_tx_rx_map) {
+		retval = rmi_read_block(rmi_dev, control->reg_3_4->address,
+			(u8 *)control->reg_3_4->regs,
+			sizeof(struct f1a_0d_control_3_4n) *
+					f1a->sensor_button_count);
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg 3_4 to %#06x.\n",
+				 control->reg_3_4->address);
+			return retval;
+		}
+	}
+
+	if (query->has_perbutton_threshold) {
+		retval = rmi_read_block(rmi_dev, control->reg_5->address,
+			(u8 *)control->reg_5->regs, f1a->sensor_button_count);
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg 5 to %#06x.\n",
+				 control->reg_5->address);
+			return retval;
+		}
+	}
+
+	if (query->has_release_threshold) {
+		retval = rmi_read_block(rmi_dev, control->reg_6->address,
+				(u8 *)control->reg_6->regs,
+				sizeof(control->reg_6->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg 6 to %#06x.\n",
+					control->reg_6->address);
+			return retval;
+		}
+	}
+
+	if (query->has_strongestbtn_hysteresis) {
+		retval = rmi_read_block(rmi_dev, control->reg_7->address,
+				(u8 *)control->reg_7->regs,
+				sizeof(control->reg_7->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg 7 to %#06x.\n",
+					control->reg_7->address);
+			return retval;
+		}
+	}
+
+	if (query->has_filter_strength) {
+		retval = rmi_read_block(rmi_dev, control->reg_8->address,
+				(u8 *)control->reg_8->regs,
+				sizeof(control->reg_8->regs));
+		if (retval < 0) {
+			dev_err(&rmi_dev->dev, "Could not read control reg 8 to %#06x.\n",
+					control->reg_8->address);
+			return retval;
+		}
+	}
+	return 0;
+}
+
+
+static int rmi_f1a_init(struct rmi_function_container *fc)
+{
+	int rc;
+
+	rc = rmi_f1a_alloc_memory(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f1a_initialize(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f1a_register_device(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f1a_create_sysfs(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	return 0;
+
+err_free_data:
+	rmi_f1a_free_memory(fc);
+
+	return rc;
+}
+
+
+static int rmi_f1a_alloc_memory(struct rmi_function_container *fc)
+{
+	struct f1a_data *f1a;
+	int rc;
+	int regSize;
+	u16 ctrl_base_addr;
+
+	f1a = kzalloc(sizeof(struct f1a_data), GFP_KERNEL);
+	if (!f1a) {
+		dev_err(&fc->dev, "Failed to allocate function data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f1a;
+
+	rc = rmi_read_block(fc->rmi_dev, fc->fd.query_base_addr,
+			f1a->query.regs, sizeof(f1a->query.regs));
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to read query register.\n");
+		return rc;
+	}
+
+	f1a->sensor_button_count = f1a->query.max_button_count+1;
+
+	f1a->button_bitmask_size =
+			sizeof(u8)*(f1a->sensor_button_count + 7) / 8;
+	f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
+			sizeof(u8), GFP_KERNEL);
+	if (!f1a->button_data_buffer) {
+		dev_err(&fc->dev, "Failed to allocate button data buffer.\n");
+		return -ENOMEM;
+	}
+
+	f1a->button_map = kcalloc(f1a->sensor_button_count,
+				sizeof(unsigned char), GFP_KERNEL);
+	if (!f1a->button_map) {
+		dev_err(&fc->dev, "Failed to allocate button map.\n");
+		return -ENOMEM;
+	}
+
+	/* allocate memory for control reg */
+	/* reg 0 */
+	ctrl_base_addr = fc->fd.control_base_addr;
+	if (f1a->query.has_general_control) {
+		f1a->control.reg_0 =
+			kzalloc(sizeof(f1a->control.reg_0->regs), GFP_KERNEL);
+		if (!f1a->control.reg_0) {
+			dev_err(&fc->dev, "Failed to allocate reg_0 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_0->address = ctrl_base_addr;
+		ctrl_base_addr += sizeof(f1a->control.reg_0->regs);
+	}
+	/* reg 1 */
+	if (f1a->query.has_interrupt_enable) {
+		f1a->control.reg_1 =
+			kzalloc(sizeof(struct f1a_0d_control_1), GFP_KERNEL);
+		if (!f1a->control.reg_1) {
+			dev_err(&fc->dev, "Failed to allocate reg_1 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_1->regs =
+				kzalloc(f1a->button_bitmask_size, GFP_KERNEL);
+		if (!f1a->control.reg_1->regs) {
+			dev_err(&fc->dev, "Failed to allocate reg_1->regs control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_1->address = ctrl_base_addr;
+		f1a->control.reg_1->length = f1a->button_bitmask_size;
+		ctrl_base_addr += f1a->button_bitmask_size;
+	}
+
+	/* reg 2 */
+	if (f1a->query.has_multibutton_select) {
+		f1a->control.reg_2 =
+			kzalloc(sizeof(struct f1a_0d_control_2), GFP_KERNEL);
+		if (!f1a->control.reg_2) {
+			dev_err(&fc->dev, "Failed to allocate reg_2 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_2->regs =
+				kzalloc(f1a->button_bitmask_size, GFP_KERNEL);
+		if (!f1a->control.reg_2->regs) {
+			dev_err(&fc->dev, "Failed to allocate reg_2->regs control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_2->address = ctrl_base_addr;
+		f1a->control.reg_2->length = f1a->button_bitmask_size;
+		ctrl_base_addr += f1a->button_bitmask_size;
+	}
+
+	/* reg 3_4*/
+	if (f1a->query.has_tx_rx_map) {
+		f1a->control.reg_3_4 =
+			kzalloc(sizeof(struct f1a_0d_control_3_4), GFP_KERNEL);
+		if (!f1a->control.reg_3_4) {
+			dev_err(&fc->dev, "Failed to allocate reg_3_4 control registers.");
+			return -ENOMEM;
+		}
+		regSize = sizeof(struct f1a_0d_control_3_4n);
+		f1a->control.reg_3_4->regs =
+			kzalloc(regSize * f1a->sensor_button_count, GFP_KERNEL);
+		if (!f1a->control.reg_3_4->regs) {
+			dev_err(&fc->dev, "Failed to allocate reg_3_4->regs control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_3_4->address = ctrl_base_addr;
+		f1a->control.reg_3_4->length =
+					regSize * f1a->sensor_button_count;
+		ctrl_base_addr += regSize * f1a->sensor_button_count;
+	}
+
+	/* reg 5 */
+	if (f1a->query.has_perbutton_threshold) {
+		f1a->control.reg_5 =
+			kzalloc(sizeof(struct f1a_0d_control_5), GFP_KERNEL);
+		if (!f1a->control.reg_5) {
+			dev_err(&fc->dev, "Failed to allocate reg_5 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_5->regs =
+				kzalloc(f1a->sensor_button_count, GFP_KERNEL);
+		if (!f1a->control.reg_5->regs) {
+			dev_err(&fc->dev, "Failed to allocate reg_5->regs control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_5->address = ctrl_base_addr;
+		f1a->control.reg_5->length = f1a->sensor_button_count;
+		ctrl_base_addr += f1a->sensor_button_count;
+	}
+
+	/* reg 6 */
+	if (f1a->query.has_release_threshold) {
+		f1a->control.reg_6 =
+			kzalloc(sizeof(f1a->control.reg_6->regs), GFP_KERNEL);
+		if (!f1a->control.reg_6) {
+			dev_err(&fc->dev, "Failed to allocate reg_6 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_6->address = ctrl_base_addr;
+		ctrl_base_addr += sizeof(f1a->control.reg_6->regs);
+	}
+	/* reg 7 */
+	if (f1a->query.has_strongestbtn_hysteresis) {
+		f1a->control.reg_7 =
+			kzalloc(sizeof(f1a->control.reg_7->regs), GFP_KERNEL);
+		if (!f1a->control.reg_7) {
+			dev_err(&fc->dev, "Failed to allocate reg_7 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_7->address = ctrl_base_addr;
+		ctrl_base_addr += sizeof(f1a->control.reg_7->regs);
+	}
+	/* reg 8 */
+	if (f1a->query.has_filter_strength) {
+		f1a->control.reg_8 =
+			kzalloc(sizeof(f1a->control.reg_8->regs), GFP_KERNEL);
+		if (!f1a->control.reg_8) {
+			dev_err(&fc->dev, "Failed to allocate reg_8 control registers.");
+			return -ENOMEM;
+		}
+		f1a->control.reg_8->address = ctrl_base_addr;
+		ctrl_base_addr += sizeof(f1a->control.reg_8->regs);
+	}
+	return 0;
+}
+
+
+
+static void rmi_f1a_free_memory(struct rmi_function_container *fc)
+{
+	union f1a_0d_query *query;
+	struct f1a_data *f1a = fc->data;
+
+	query = &f1a->query;
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+
+	if (query->has_general_control) {
+		sysfs_remove_file(&fc->dev.kobj, attrify(multibutton_report));
+		sysfs_remove_file(&fc->dev.kobj, attrify(filter_mode));
+	}
+
+	if (query->has_interrupt_enable)
+		sysfs_remove_file(&fc->dev.kobj,
+			attrify(interrupt_enabled_button));
+
+	if (query->has_multibutton_select)
+		sysfs_remove_file(&fc->dev.kobj, attrify(multi_button));
+
+	if (query->has_tx_rx_map)
+		sysfs_remove_file(&fc->dev.kobj, attrify(tx_rx_map));
+
+	if (query->has_perbutton_threshold)
+		sysfs_remove_file(&fc->dev.kobj, attrify(threshold_button));
+
+	if (query->has_release_threshold)
+		sysfs_remove_file(&fc->dev.kobj,
+				attrify(button_release_threshold));
+
+	if (query->has_strongestbtn_hysteresis)
+		sysfs_remove_file(&fc->dev.kobj,
+				attrify(strongest_button_hysteresis));
+
+	if (query->has_filter_strength)
+		sysfs_remove_file(&fc->dev.kobj, attrify(filter_strength));
+
+	if (f1a) {
+		kfree(f1a->button_data_buffer);
+		kfree(f1a->button_map);
+		kfree(f1a->control.reg_0);
+		if (f1a->control.reg_1) {
+			kfree(f1a->control.reg_1->regs);
+			kfree(f1a->control.reg_1);
+		}
+		if (f1a->control.reg_2) {
+			kfree(f1a->control.reg_2->regs);
+			kfree(f1a->control.reg_2);
+		}
+		if (f1a->control.reg_3_4) {
+			kfree(f1a->control.reg_3_4->regs);
+			kfree(f1a->control.reg_3_4);
+		}
+		if (f1a->control.reg_5) {
+			kfree(f1a->control.reg_5->regs);
+			kfree(f1a->control.reg_5);
+		}
+		kfree(f1a->control.reg_5);
+		kfree(f1a->control.reg_6);
+		kfree(f1a->control.reg_7);
+		kfree(f1a->control.reg_8);
+		kfree(f1a);
+		fc->data = NULL;
+	}
+}
+
+
+static int rmi_f1a_initialize(struct rmi_function_container *fc)
+{
+	int i;
+	int retval;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_device_platform_data *pdata;
+	struct f1a_data *f1a = fc->data;
+
+	dev_dbg(&fc->dev, "Intializing F1A values.");
+
+	/* initial all default values for f1a data here */
+	pdata = to_rmi_platform_data(rmi_dev);
+	if (pdata) {
+		if (!pdata->f1a_button_map)
+			dev_warn(&fc->dev, "%s - button_map is NULL", __func__);
+		else if (!pdata->f1a_button_map->map)
+			dev_warn(&fc->dev,
+				 "Platformdata button map is missing!\n");
+		else {
+			if (pdata->f1a_button_map->nbuttons !=
+					f1a->sensor_button_count)
+				dev_warn(&fc->dev,
+					"Platform data buttonmap size(%d) != number buttons on device(%d)\n",
+					pdata->f1a_button_map->nbuttons,
+					f1a->sensor_button_count);
+			f1a->button_map_size = min(f1a->sensor_button_count,
+					 pdata->f1a_button_map->nbuttons);
+			for (i = 0; i < f1a->button_map_size; i++)
+				f1a->button_map[i] =
+					pdata->f1a_button_map->map[i];
+		}
+	}
+
+	retval = rmi_f1a_read_control_parameters(rmi_dev, f1a);
+	if (retval < 0) {
+		dev_err(&fc->dev,
+			"Failed to initialize F1a control params.\n");
+		return retval;
+	}
+
+	mutex_init(&f1a->control_mutex);
+	mutex_init(&f1a->data_mutex);
+	return 0;
+}
+
+
+
+static int rmi_f1a_register_device(struct rmi_function_container *fc)
+{
+	int i;
+	int rc;
+	struct input_dev *input_dev;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f1a_data *f1a = fc->data;
+
+	input_dev = input_allocate_device();
+	if (!input_dev) {
+		dev_err(&fc->dev, "Failed to allocate input device.\n");
+		return -ENOMEM;
+	}
+
+	f1a->input = input_dev;
+	snprintf(f1a->input_name, MAX_NAME_LEN, "%sfn%02x",
+			dev_name(&rmi_dev->dev), fc->fd.function_number);
+	input_dev->name = f1a->input_name;
+	snprintf(f1a->input_phys, MAX_NAME_LEN, "%s/input0", input_dev->name);
+	input_dev->phys = f1a->input_phys;
+	input_dev->dev.parent = &rmi_dev->dev;
+	input_set_drvdata(input_dev, f1a);
+
+	/* Set up any input events. */
+	set_bit(EV_SYN, input_dev->evbit);
+	set_bit(EV_KEY, input_dev->evbit);
+
+	/* manage button map using input subsystem */
+	input_dev->keycode = f1a->button_map;
+	input_dev->keycodesize = sizeof(f1a->button_map);
+	input_dev->keycodemax = f1a->button_map_size;
+
+	/* set bits for each button. */
+	for (i = 0; i < f1a->button_map_size; i++) {
+		set_bit(f1a->button_map[i], input_dev->keybit);
+		input_set_capability(input_dev, EV_KEY, f1a->button_map[i]);
+	}
+
+	rc = input_register_device(input_dev);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to register input device.\n");
+		goto error_free_device;
+	}
+
+	return 0;
+
+error_free_device:
+	input_free_device(input_dev);
+
+	return rc;
+}
+
+
+static int rmi_f1a_create_sysfs(struct rmi_function_container *fc)
+{
+	struct f1a_data *f19 = fc->data;
+	union f1a_0d_query *query = &f19->query;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.\n");
+	/* Set up sysfs device attributes. */
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+
+	if (query->has_general_control) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(multibutton_report)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(filter_mode)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_interrupt_enable) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(interrupt_enabled_button)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_multibutton_select) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(multi_button)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_tx_rx_map) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(tx_rx_map)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_perbutton_threshold) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(threshold_button)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_release_threshold) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(button_release_threshold)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_strongestbtn_hysteresis) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(strongest_button_hysteresis)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	if (query->has_filter_strength) {
+		if (sysfs_create_file(&fc->dev.kobj,
+				attrify(filter_strength)) < 0) {
+			dev_err(&fc->dev, "Failed to create control sysfs files.");
+			return -ENODEV;
+		}
+	}
+	return 0;
+}
+
+
+
+static int rmi_f1a_config(struct rmi_function_container *fc)
+{
+	int retval;
+	struct f1a_data *f1a = fc->data;
+	union f1a_0d_query *query = &f1a->query;
+	struct f1a_0d_control *control = &f1a->control;
+
+	if (query->has_general_control) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_0->address,
+			(u8 *)control->reg_0->regs,
+			sizeof(control->reg_0->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg0 to 0x%x\n",
+					__func__, control->reg_0->address);
+			return retval;
+		}
+	}
+	if (query->has_interrupt_enable) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_1->address,
+				(u8 *)control->reg_1->regs,
+				f1a->button_bitmask_size);
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg 1 to 0x%x\n",
+				__func__, control->reg_1->address);
+			return retval;
+		}
+	}
+	if (query->has_multibutton_select) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_2->address,
+				(u8 *)control->reg_2->regs,
+				f1a->button_bitmask_size);
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg 2 to 0x%x\n",
+				__func__, control->reg_2->address);
+			return -EINVAL;
+		}
+	}
+	if (query->has_tx_rx_map) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_3_4->address,
+				(u8 *)control->reg_3_4->regs,
+				f1a->sensor_button_count);
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg_3_4 to 0x%x\n",
+					__func__, control->reg_3_4->address);
+			return -EINVAL;
+		}
+	}
+	if (query->has_perbutton_threshold) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_5->address,
+				(u8 *)control->reg_5->regs,
+				f1a->sensor_button_count);
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write to reg 5 to 0x%x\n",
+				__func__, control->reg_5->address);
+			return retval;
+		}
+	}
+	if (query->has_release_threshold) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_6->address,
+			(u8 *)control->reg_6->regs,
+			sizeof(control->reg_6->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write  reg 6 to 0x%x\n",
+				__func__, control->reg_6->address);
+			return -EINVAL;
+		}
+	}
+	if (query->has_strongestbtn_hysteresis) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_7->address,
+			(u8 *)control->reg_7->regs,
+			sizeof(control->reg_7->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg 7 to 0x%x\n",
+				__func__, control->reg_7->address);
+			return -EINVAL;
+		}
+	}
+	if (query->has_filter_strength) {
+		retval = rmi_write_block(fc->rmi_dev, control->reg_8->address,
+			(u8 *)control->reg_8->regs,
+			sizeof(control->reg_8->regs));
+		if (retval < 0) {
+			dev_err(&fc->dev, "%s : Could not write reg 8 to 0x%x\n",
+				__func__, control->reg_8->address);
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+
+
+static int rmi_f1a_reset(struct rmi_function_container *fc)
+{
+	/* we do nnothing here */
+	return 0;
+}
+
+
+static void rmi_f1a_remove(struct rmi_function_container *fc)
+{
+	struct f1a_data *f1a = fc->data;
+
+	input_unregister_device(f1a->input);
+
+	rmi_f1a_free_memory(fc);
+}
+
+static int rmi_f1a_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	int error;
+	int button;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f1a_data *f1a = fc->data;
+	u16 data_base_addr = fc->fd.data_base_addr;
+
+	/* Read the button data. */
+	error = rmi_read_block(rmi_dev, data_base_addr, f1a->button_data_buffer,
+			f1a->button_bitmask_size);
+	if (error < 0) {
+		dev_err(&fc->dev, "%s: Failed to read button data registers.\n",
+			__func__);
+		return error;
+	}
+
+	/* Generate events for buttons that change state. */
+	for (button = 0; button < f1a->sensor_button_count; button++) {
+		int button_reg;
+		int button_shift;
+		bool button_status;
+
+		/* determine which data byte the button status is in */
+		button_reg = button / 8;
+		/* bit shift to get button's status */
+		button_shift = button % 8;
+		button_status =
+		    ((f1a->button_data_buffer[button_reg] >> button_shift)
+			& 0x01) != 0;
+
+		dev_dbg(&fc->dev, "%s: Button %d (code %d) -> %d.\n",
+			__func__, button, f1a->button_map[button],
+				button_status);
+		/* Generate an event here. */
+		input_report_key(f1a->input, f1a->button_map[button],
+					button_status);
+	}
+
+	input_sync(f1a->input); /* sync after groups of events */
+	return 0;
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x1a,
+	.init = rmi_f1a_init,
+	.config = rmi_f1a_config,
+	.reset = rmi_f1a_reset,
+	.attention = rmi_f1a_attention,
+	.remove = rmi_f1a_remove
+};
+
+static int __init rmi_f1a_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void rmi_f1a_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+/* sysfs functions */
+/* Query */
+simple_show_union_struct_unsigned(query, max_button_count)
+simple_show_union_struct_unsigned(query, has_general_control)
+simple_show_union_struct_unsigned(query, has_interrupt_enable)
+simple_show_union_struct_unsigned(query, has_multibutton_select)
+simple_show_union_struct_unsigned(query, has_tx_rx_map)
+simple_show_union_struct_unsigned(query, has_perbutton_threshold)
+simple_show_union_struct_unsigned(query, has_release_threshold)
+simple_show_union_struct_unsigned(query, has_strongestbtn_hysteresis)
+simple_show_union_struct_unsigned(query, has_filter_strength)
+
+/* Control */
+show_store_union_struct_unsigned(control, reg_0, multibutton_report)
+show_store_union_struct_unsigned(control, reg_0, filter_mode)
+show_store_union_struct_unsigned(control, reg_6, button_release_threshold)
+show_store_union_struct_unsigned(control, reg_7, strongest_button_hysteresis)
+show_store_union_struct_unsigned(control, reg_8, filter_strength)
+
+/* repeated register functions */
+show_store_repeated_union_struct_unsigned(control, reg_1,
+						interrupt_enabled_button)
+show_store_repeated_union_struct_unsigned(control, reg_2, multi_button)
+show_store_repeated_union_struct_unsigned(control, reg_5, threshold_button)
+
+static ssize_t rmi_fn_1a_tx_rx_map_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	struct rmi_function_container *fc;
+	struct FUNCTION_DATA *data;
+	struct f1a_0d_control *control;
+	int reg_length;
+	int result, size = 0;
+	char *temp;
+	int i;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+	control = &data->control;
+	/* Read current regtype values */
+	reg_length = control->reg_3_4->length;
+	result = rmi_read_block(fc->rmi_dev, control->reg_3_4->address,
+			(u8 *)control->reg_3_4->regs,
+			reg_length * sizeof(u8));
+
+	if (result < 0) {
+		dev_dbg(dev, "%s : Could not read regtype at 0x%x\n Data may be outdated.",
+				__func__, control->reg_3_4->address);
+	}
+	temp = buf;
+	for (i = 0; i < reg_length/2; i++) {
+		result = snprintf(temp, PAGE_SIZE - size, "%u-%u ",
+				control->reg_3_4->regs[i].transmitterbtn,
+				control->reg_3_4->regs[i].receiverbtn);
+		if (result < 0) {
+			dev_err(dev, "%s : Could not write output.", __func__);
+			return result;
+		}
+		size += result;
+		temp += result;
+	}
+	result = snprintf(temp, PAGE_SIZE - size, "\n");
+	if (result < 0) {
+			dev_err(dev, "%s : Could not write output.", __func__);
+			return result;
+	}
+	return size + result;
+}
+
+module_init(rmi_f1a_module_init);
+module_exit(rmi_f1a_module_exit);
+
+MODULE_AUTHOR("Vivian Ly <[email protected]>");
+MODULE_DESCRIPTION("RMI F1a module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#31 lore raw

RFC PATCH 14/17 input: RMI4 F30 GPIO/LED control

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f30.c | 1247 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1247 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f30.c new file +1247 -0
new file mode 100644
index 0000000..4bc2932
@@ -0,0 +1,1247 @@
+/*
+ * Copyright (c) 2012 Synaptics Incorporated
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#define FUNCTION_DATA rmi_fn_30_data
+#define FNUM 30
+
+#include <linux/kernel.h>
+#include <linux/rmi.h>
+#include <linux/input.h>
+#include <linux/slab.h>
+#include "rmi_driver.h"
+
+
+#define MAX_LEN 256
+
+/* data specific to fn $30 that needs to be kept around */
+union f30_query {
+	struct {
+		u8 extended_patterns:1;
+		u8 has_mappable_buttons:1;
+		u8 has_led:1;
+		u8 has_gpio:1;
+		u8 has_haptic:1;
+		u8 has_gpio_driver_control:1;
+		u8 gpio_led_count:5;
+	};
+	struct {
+		u8 regs[2];
+		u16 address;
+	};
+};
+
+struct f30_gpio_ctrl_0n {
+	u8 led_sel;
+};
+
+struct f30_gpio_ctrl_0 {
+	struct f30_gpio_ctrl_0n *regs;
+	u16 address;
+	u8 length;
+};
+
+union f30_gpio_ctrl_1 {
+	struct {
+		u8 gpio_debounce:1;
+		u8 reserved:3;
+		u8 halt:1;
+		u8 halted:1;
+		u8 reserved2:2;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	};
+};
+
+struct f30_gpio_ctrl_2n {
+	u8 dir;
+};
+
+struct f30_gpio_ctrl_2 {
+	struct f30_gpio_ctrl_2n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_gpio_ctrl_3n {
+	u8 gpiodata;
+};
+
+struct f30_gpio_ctrl_3 {
+	struct f30_gpio_ctrl_3n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_gpio_ctrl_4n {
+	u8 led_act;
+};
+
+struct f30_gpio_ctrl_4 {
+	struct f30_gpio_ctrl_4n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_gpio_ctrl_5n {
+	u8 ramp_period_a;
+	u8 ramp_period_b;
+};
+
+struct f30_gpio_ctrl_5 {
+	struct f30_gpio_ctrl_5n *regs;
+	u16 address;
+	u8 length;
+};
+
+union f30_gpio_ctrl_6n {
+	struct {
+		u8 reserved:1;
+		u8 SPCTRL:3;
+		u8 STRPD:1;
+		u8 reserved2:1;
+		u8 STRPU:1;
+		u8 reserved3:1;
+	};
+	struct {
+		u8 brightness:4;
+		u8 pattern:4;
+	};
+};
+
+struct f30_gpio_ctrl_6 {
+	union f30_gpio_ctrl_6n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_gpio_ctrl_7n {
+	u8 capacity_btn_nbr:5;
+	u8 valid:1;
+	u8 invert:1;
+	u8 open_drain:1;
+};
+
+struct f30_gpio_ctrl_7 {
+	struct f30_gpio_ctrl_7n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_gpio_ctrl_8n {
+	u8 gpio_ctrl8_0;
+	u8 gpio_ctrl8_1;
+};
+
+struct f30_gpio_ctrl_8 {
+	struct f30_gpio_ctrl_8n *regs;
+	u16 address;
+	u8 length;
+};
+
+union f30_gpio_ctrl_9 {
+	struct {
+		u8 haptic_duration;
+	};
+	struct {
+		u8 regs[1];
+		u16 address;
+	};
+};
+
+struct f30_control {
+	struct f30_gpio_ctrl_0 *reg_0;
+	union f30_gpio_ctrl_1 *reg_1;
+	struct f30_gpio_ctrl_2 *reg_2;
+	struct f30_gpio_ctrl_3 *reg_3;
+	struct f30_gpio_ctrl_4 *reg_4;
+	struct f30_gpio_ctrl_5 *reg_5;
+	struct f30_gpio_ctrl_6 *reg_6;
+	struct f30_gpio_ctrl_7 *reg_7;
+	struct f30_gpio_ctrl_8 *reg_8;
+	union f30_gpio_ctrl_9 *reg_9;
+};
+
+struct f30_data_0n {
+	u8 gpi_led_data:1;
+};
+
+struct f30_data_0 {
+	struct f30_data_0n *regs;
+	u16 address;
+	u8 length;
+};
+
+struct f30_data {
+	struct f30_data_0 *datareg_0;
+	u16 address;
+	u8 length;
+};
+
+struct rmi_fn_30_data {
+	union f30_query query;
+	struct f30_data data;
+	struct f30_control control;
+	unsigned char gpioled_count;
+	unsigned char gpioled_bitmask_size;
+	unsigned char gpioled_byte_size;
+	unsigned char *button_data_buffer;
+	unsigned char button_bitmask_size;
+	unsigned short *gpioled_map;
+	char input_name[MAX_LEN];
+	char input_phys[MAX_LEN];
+	struct input_dev *input;
+	struct mutex control_mutex;
+	struct mutex data_mutex;
+};
+
+static int rmi_f30_alloc_memory(struct rmi_function_container *fc);
+
+static void rmi_f30_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f30_initialize(struct rmi_function_container *fc);
+
+static int rmi_f30_register_device(struct rmi_function_container *fc);
+
+static int rmi_f30_config(struct rmi_function_container *fc);
+
+static int rmi_f30_create_sysfs(struct rmi_function_container *fc);
+
+
+/* Query sysfs files */
+
+show_union_struct_prototype(extended_patterns)
+show_union_struct_prototype(has_mappable_buttons)
+show_union_struct_prototype(has_led)
+show_union_struct_prototype(has_gpio)
+show_union_struct_prototype(has_haptic)
+show_union_struct_prototype(has_gpio_driver_control)
+show_union_struct_prototype(gpio_led_count)
+
+static struct attribute *attrs1[] = {
+	attrify(extended_patterns),
+	attrify(has_mappable_buttons),
+	attrify(has_led),
+	attrify(has_gpio),
+	attrify(has_haptic),
+	attrify(has_gpio_driver_control),
+	attrify(gpio_led_count),
+	NULL
+};
+
+static struct attribute_group attrs_query = GROUP(attrs1);
+
+/* Control sysfs files */
+
+show_store_union_struct_prototype(led_sel)
+show_store_union_struct_prototype(gpio_debounce)
+show_store_union_struct_prototype(halt)
+show_store_union_struct_prototype(halted)
+show_store_union_struct_prototype(dir)
+show_store_union_struct_prototype(gpiodata)
+show_store_union_struct_prototype(led_act)
+show_store_union_struct_prototype(ramp_period_a)
+show_store_union_struct_prototype(ramp_period_b)
+show_store_union_struct_prototype(SPCTRL)
+show_store_union_struct_prototype(STRPD)
+show_store_union_struct_prototype(STRPU)
+show_store_union_struct_prototype(brightness)
+show_store_union_struct_prototype(pattern)
+
+show_store_union_struct_prototype(capacity_btn_nbr)
+show_store_union_struct_prototype(valid)
+show_store_union_struct_prototype(invert)
+show_store_union_struct_prototype(open_drain)
+show_store_union_struct_prototype(gpio_ctrl8_0)
+show_store_union_struct_prototype(gpio_ctrl8_1)
+show_store_union_struct_prototype(haptic_duration)
+
+/* Data sysfs files */
+show_store_union_struct_prototype(gpi_led_data)
+
+static struct attribute *attrs_ctrl_reg_0[] = {
+	attrify(led_sel),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_1[] = {
+	attrify(gpio_debounce),
+	attrify(halt),
+	attrify(halted),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_2[] = {
+	attrify(dir),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_3[] = {
+	attrify(gpiodata),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_4[] = {
+	attrify(led_act),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_5[] = {
+	attrify(ramp_period_a),
+	attrify(ramp_period_b),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_6_gpio[] = {
+	attrify(SPCTRL),
+	attrify(STRPD),
+	attrify(STRPU),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_6_led[] = {
+	attrify(brightness),
+	attrify(pattern),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_7[] = {
+	attrify(capacity_btn_nbr),
+	attrify(valid),
+	attrify(invert),
+	attrify(open_drain),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_8[] = {
+	attrify(gpio_ctrl8_0),
+	attrify(gpio_ctrl8_1),
+	NULL
+};
+
+static struct attribute *attrs_ctrl_reg_9[] = {
+	attrify(haptic_duration),
+	NULL
+};
+
+static struct attribute_group attrs_ctrl_regs[] = {
+	GROUP(attrs_ctrl_reg_0),
+	GROUP(attrs_ctrl_reg_1),
+	GROUP(attrs_ctrl_reg_2),
+	GROUP(attrs_ctrl_reg_3),
+	GROUP(attrs_ctrl_reg_4),
+	GROUP(attrs_ctrl_reg_5),
+	GROUP(attrs_ctrl_reg_6_gpio),
+	GROUP(attrs_ctrl_reg_6_led),
+	GROUP(attrs_ctrl_reg_7),
+	GROUP(attrs_ctrl_reg_8),
+	GROUP(attrs_ctrl_reg_9),
+};
+
+bool f30_attrs_regs_exist[ARRAY_SIZE(attrs_ctrl_regs)];
+
+static struct attribute *attrs_gpileddata[] = {
+	attrify(gpi_led_data),
+	NULL
+};
+
+static struct attribute_group attrs_data = GROUP(attrs_gpileddata);
+
+int rmi_f30_read_control_parameters(struct rmi_device *rmi_dev,
+	struct rmi_fn_30_data *f30)
+{
+	int retval = 0;
+	struct f30_control *control = &f30->control;
+
+	retval = rmi_read_block(rmi_dev, control->reg_0->address,
+			(u8 *)control->reg_0->regs,
+			control->reg_0->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg0 to 0x%x\n",
+			__func__, control->reg_0->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_1->address,
+			(u8 *)control->reg_1->regs,
+			sizeof(control->reg_1->regs));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg1 to 0x%x\n",
+			 __func__, control->reg_1->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_2->address,
+			(u8 *)control->reg_2->regs, control->reg_2->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg_2 to 0x%x\n",
+			 __func__, control->reg_2->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_3->address,
+			(u8 *)control->reg_3->regs, control->reg_3->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg_3 to 0x%x\n",
+			 __func__, control->reg_3->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_4->address,
+			(u8 *)control->reg_4->regs, control->reg_4->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg4 to 0x%x\n",
+			 __func__, control->reg_4->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_5->address,
+			(u8 *)control->reg_5->regs,
+			control->reg_5->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg5 to 0x%x\n", __func__,
+			control->reg_5->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_6->address,
+			(u8 *)control->reg_6->regs,
+			control->reg_6->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg6 to 0x%x\n", __func__,
+		control->reg_6->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_7->address,
+		(u8 *)control->reg_7->regs,
+		control->reg_7->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg7 to 0x%x\n", __func__,
+		control->reg_7->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_8->address,
+		(u8 *)control->reg_8->regs,
+		control->reg_8->length);
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg8 to 0x%x\n", __func__,
+		control->reg_8->address);
+		return retval;
+	}
+
+	retval = rmi_read_block(rmi_dev, control->reg_9->address,
+		(u8 *)control->reg_9->regs,
+		sizeof(control->reg_9->regs));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev,
+			"%s : Could not read control reg9 to 0x%x\n", __func__,
+		control->reg_9->address);
+		return retval;
+	}
+	return 0;
+}
+
+static int rmi_f30_init(struct rmi_function_container *fc)
+{
+	int rc;
+	struct rmi_fn_30_data *f30 = fc->data;
+
+	rc = rmi_f30_alloc_memory(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	rc = rmi_f30_initialize(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	rc = rmi_f30_register_device(fc);
+	if (rc < 0)
+		goto error_exit;
+
+	rc = rmi_f30_create_sysfs(fc);
+	if (rc < 0)
+		goto error_uregister_exit;
+	return 0;
+
+error_uregister_exit:
+	input_unregister_device(f30->input);
+
+error_exit:
+	rmi_f30_free_memory(fc);
+
+	return rc;
+
+}
+
+static inline int rmi_f30_alloc_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_30_data *f30;
+	int retval;
+
+	f30 = kzalloc(sizeof(struct rmi_fn_30_data), GFP_KERNEL);
+	if (!f30) {
+		dev_err(&fc->dev, "Failed to allocate rmi_fn_30_data.\n");
+		return -ENOMEM;
+	}
+	fc->data = f30;
+
+	retval = rmi_read_block(fc->rmi_dev,
+						fc->fd.query_base_addr,
+						f30->query.regs,
+					ARRAY_SIZE(f30->query.regs));
+
+	if (retval < 0) {
+		dev_err(&fc->dev, "Failed to read query register.\n");
+		return retval;
+	}
+
+	f30->gpioled_count = f30->query.gpio_led_count;
+	f30->button_bitmask_size = sizeof(u8)*(f30->gpioled_count + 7) / 8;
+	f30->button_data_buffer =
+	    kcalloc(f30->button_bitmask_size,
+		    sizeof(unsigned char), GFP_KERNEL);
+	if (!f30->button_data_buffer) {
+		dev_err(&fc->dev, "Failed to allocate button data buffer.\n");
+		return -ENOMEM;
+	}
+
+	f30->gpioled_map = kcalloc(f30->gpioled_count,
+				sizeof(unsigned short), GFP_KERNEL);
+	if (!f30->gpioled_map) {
+		dev_err(&fc->dev, "Failed to allocate button map.\n");
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static inline void rmi_f30_free_memory(struct rmi_function_container *fc)
+{
+	struct rmi_fn_30_data *f30 = fc->data;
+	u8 reg_num = 0;
+	sysfs_remove_group(&fc->dev.kobj, &attrs_query);
+	sysfs_remove_group(&fc->dev.kobj, &attrs_data);
+	for (reg_num = 0; reg_num < ARRAY_SIZE(attrs_ctrl_regs); reg_num++)
+		sysfs_remove_group(&fc->dev.kobj, &attrs_ctrl_regs[reg_num]);
+	if (f30) {
+		if (f30->control.reg_0)
+			kfree(f30->control.reg_0->regs);
+		kfree(f30->control.reg_0);
+
+		if (f30->control.reg_1)
+			kfree(f30->control.reg_1->regs);
+		kfree(f30->control.reg_1);
+
+		if (f30->control.reg_2)
+			kfree(f30->control.reg_2->regs);
+		kfree(f30->control.reg_2);
+
+		if (f30->control.reg_3)
+			kfree(f30->control.reg_3->regs);
+		kfree(f30->control.reg_3);
+
+		if (f30->control.reg_4)
+			kfree(f30->control.reg_4->regs);
+		kfree(f30->control.reg_4);
+
+		if (f30->control.reg_5)
+			kfree(f30->control.reg_5->regs);
+		kfree(f30->control.reg_5);
+
+		if (f30->control.reg_6)
+			kfree(f30->control.reg_6->regs);
+		kfree(f30->control.reg_6);
+
+		if (f30->control.reg_7)
+			kfree(f30->control.reg_7->regs);
+		kfree(f30->control.reg_7);
+
+		if (f30->control.reg_8)
+			kfree(f30->control.reg_8->regs);
+		kfree(f30->control.reg_8);
+
+		if (f30->control.reg_9)
+			kfree(f30->control.reg_9->regs);
+		kfree(f30->control.reg_9);
+
+		if (!f30->data.datareg_0)
+			kfree(f30->data.datareg_0->regs);
+		kfree(f30->data.datareg_0);
+
+		kfree(f30->button_data_buffer);
+		kfree(f30->gpioled_map);
+		kfree(f30);
+		fc->data = NULL;
+	}
+}
+
+int rmi_f30_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	int data_base_addr = fc->fd.data_base_addr;
+	struct rmi_fn_30_data *f30 = fc->data;
+	int error;
+	int gpiled;
+	bool gpiled_status = false;
+	int status = 0;
+
+	/* Read the button data. */
+
+	error = rmi_read_block(rmi_dev, data_base_addr,
+			f30->button_data_buffer,
+			f30->button_bitmask_size);
+	if (error < 0) {
+		dev_err(&fc->dev,
+			"%s: Failed to read button data registers.\n",
+			__func__);
+		return error;
+	}
+
+	/* Read the gpi led data. */
+	f30->data.address = fc->fd.data_base_addr;
+	error = rmi_read_block(fc->rmi_dev, f30->data.address,
+		(u8 *)&f30->data, f30->gpioled_count);
+
+	if (error < 0) {
+		dev_err(&fc->dev, "%s: Failed to read f30 data registers.\n",
+			__func__);
+		return error;
+	}
+	/* Generate events for buttons that change state. */
+	for (gpiled = 0; gpiled < f30->gpioled_count; gpiled++) {
+		status = f30->data.datareg_0->regs[gpiled].gpi_led_data;
+		dev_warn(&fc->dev,
+			"rmi_f30 attention gpiled=%d data status=%d\n",
+			gpiled,
+			f30->data.datareg_0->regs[gpiled].gpi_led_data);
+		/* check if gpio */
+		if (!(f30->control.reg_0->regs[gpiled].led_sel)) {
+			if (f30->control.reg_2->regs[gpiled].dir == 0) {
+				gpiled_status = status != 0;
+
+		/* if the gpiled data state changed from the
+		* last time report it and store the new state */
+		/* Generate an event here. */
+			dev_warn(&fc->dev,
+			"rmi_f30 attention call input_report_key\n");
+			input_report_key(f30->input,
+				f30->data.datareg_0->regs[gpiled].gpi_led_data,
+				gpiled_status);
+			}
+		}
+	}
+	input_sync(f30->input); /* sync after groups of events */
+	return 0;
+}
+
+static int rmi_f30_register_device(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct input_dev *input_dev;
+	struct rmi_fn_30_data *f30 = fc->data;
+	int i;
+	int rc;
+
+	input_dev = input_allocate_device();
+	if (!input_dev) {
+		dev_err(&fc->dev, "Failed to allocate input device.\n");
+		return -ENOMEM;
+	}
+
+	f30->input = input_dev;
+	snprintf(f30->input_name, MAX_LEN, "%sfn%02x", dev_name(&rmi_dev->dev),
+		fc->fd.function_number);
+	input_dev->name = f30->input_name;
+	snprintf(f30->input_phys, MAX_LEN, "%s/input0", input_dev->name);
+	input_dev->phys = f30->input_phys;
+	input_dev->dev.parent = &rmi_dev->dev;
+	input_set_drvdata(input_dev, f30);
+
+	/* Set up any input events. */
+	set_bit(EV_SYN, input_dev->evbit);
+	set_bit(EV_KEY, input_dev->evbit);
+	input_dev->keycode = f30->gpioled_map;
+	input_dev->keycodesize = 1;
+	input_dev->keycodemax = f30->gpioled_count;
+	/* set bits for each qpio led pin... */
+	for (i = 0; i < f30->gpioled_count; i++) {
+		set_bit(f30->gpioled_map[i], input_dev->keybit);
+		input_set_capability(input_dev, EV_KEY, f30->gpioled_map[i]);
+	}
+
+	rc = input_register_device(input_dev);
+	if (rc < 0) {
+		dev_err(&fc->dev, "Failed to register input device.\n");
+		goto error_free_device;
+	}
+	return 0;
+
+error_free_device:
+	input_free_device(input_dev);
+
+	return rc;
+}
+
+
+static int rmi_f30_config(struct rmi_function_container *fc)
+{
+	struct rmi_fn_30_data *data = fc->data;
+	int gpio_led_cnt = data->query.gpio_led_count;
+	int bytecnt = gpio_led_cnt / 7 + 1;
+	int regs_size = 0;
+	int rc;
+	/* repeated register functions */
+
+	/* Write Control Register values back to device */
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_0->address,
+				(u8 *)data->control.reg_0,
+				bytecnt * sizeof(struct f30_gpio_ctrl_0n));
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 0 to 0x%x\n",
+				__func__, rc, data->control.reg_0->address);
+		return rc;
+	}
+
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_1->address,
+			(u8 *) data->control.reg_1->regs,
+			sizeof(union f30_gpio_ctrl_1));
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 1 to 0x%x\n",
+				__func__, rc, data->control.reg_1->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_2->length;
+
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_2->address,
+			(u8 *) data->control.reg_2->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 2 to 0x%x\n",
+				__func__, rc, data->control.reg_2->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_3->length;
+
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_3->address,
+			(u8 *) data->control.reg_3->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 3 to 0x%x\n",
+				__func__, rc, data->control.reg_3->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_4->length;
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_4->address,
+			(u8 *) data->control.reg_4->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 4 to 0x%x\n",
+				__func__, rc, data->control.reg_4->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_5->length;
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_5->address,
+			(u8 *) data->control.reg_5->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 5 to 0x%x\n",
+				__func__, rc, data->control.reg_5->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_6->length;
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_6->address,
+			(u8 *) data->control.reg_6->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 6 to 0x%x\n",
+				__func__, rc, data->control.reg_6->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_7->length;
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_7->address,
+			(u8 *) data->control.reg_7->regs,
+			regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 7 to 0x%x\n",
+			__func__, rc, data->control.reg_7->address);
+		return rc;
+	}
+
+	regs_size = data->control.reg_8->length;
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_8->address,
+			(u8 *) data->control.reg_8->regs, regs_size);
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 9 to 0x%x\n",
+			__func__, rc, data->control.reg_8->address);
+		return rc;
+	}
+
+	rc = rmi_write_block(fc->rmi_dev, data->control.reg_9->address,
+			(u8 *) data->control.reg_9->regs,
+			sizeof(union f30_gpio_ctrl_9));
+	if (rc < 0) {
+		dev_err(&fc->dev, "%s error %d: Could not write control 9 to 0x%x\n",
+				__func__, rc, data->control.reg_9->address);
+		return rc;
+	}
+
+	return 0;
+}
+
+static inline int rmi_f30_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct rmi_device_platform_data *pdata;
+	struct rmi_fn_30_data *instance_data = fc->data;
+
+	int retval = 0;
+	u16 next_loc;
+	int  gpio_led_cnt = 0;
+	int regs_size = 0;
+	u8 reg_num = 0;
+	int reg_flg;
+	int hasgpio, hasled, hasmbtn, hashaptic;
+	struct f30_control *control = &instance_data->control;
+
+	/* Read F30 Query Data */
+	instance_data->query.address = fc->fd.query_base_addr;
+	retval = rmi_read_block(fc->rmi_dev, instance_data->query.address,
+		(u8 *)&instance_data->query, sizeof(instance_data->query));
+	if (retval < 0) {
+		dev_err(&fc->dev,
+		"Could not read query registers from 0x%04x\n",
+		instance_data->query.address);
+		return retval;
+	}
+
+	/* initialize gpioled_map data */
+	hasgpio = instance_data->query.has_gpio;
+	hasled = instance_data->query.has_led;
+	hasmbtn = instance_data->query.has_mappable_buttons;
+	hashaptic = instance_data->query.has_haptic ;
+	gpio_led_cnt = instance_data->query.gpio_led_count;
+
+	pdata = to_rmi_platform_data(rmi_dev);
+	if (pdata) {
+		if (!pdata->gpioled_map) {
+			dev_warn(&fc->dev,
+			"%s - gpioled_map is NULL", __func__);
+		} else if (pdata->gpioled_map->ngpioleds != gpio_led_cnt) {
+			dev_warn(&fc->dev,
+				"Platformdata gpioled map size (%d) != number "
+				"of buttons on device (%d) - ignored.\n",
+				pdata->gpioled_map->ngpioleds,
+				gpio_led_cnt);
+		} else if (!pdata->gpioled_map->map) {
+			dev_warn(&fc->dev,
+				 "Platformdata button map is missing!\n");
+		} else {
+			int i;
+			for (i = 0; i < pdata->gpioled_map->ngpioleds; i++)
+				instance_data->gpioled_map[i] =
+					pdata->gpioled_map->map[i];
+		}
+	}
+
+	/* Initialize Control Data */
+
+	next_loc = fc->fd.control_base_addr;
+
+	/* calculate reg size */
+
+	instance_data->gpioled_bitmask_size = sizeof(u8)*(gpio_led_cnt + 7) / 8;
+	instance_data->gpioled_byte_size = sizeof(u8)*gpio_led_cnt;
+
+	/* reg_0 */
+	control->reg_0 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_0), GFP_KERNEL);
+	if (!control->reg_0) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	if (hasgpio && hasled)
+		reg_flg = 1;
+
+	f30_attrs_regs_exist[reg_num] = true;
+	regs_size = max(sizeof(struct f30_gpio_ctrl_0n) * reg_flg *
+					instance_data->gpioled_bitmask_size, 1);
+	control->reg_0->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+
+	if (!control->reg_0->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_0->address = next_loc;
+	control->reg_0->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_1 */
+	control->reg_1 =
+			kzalloc(sizeof(union f30_gpio_ctrl_1), GFP_KERNEL);
+	if (!control->reg_1) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	f30_attrs_regs_exist[reg_num] = true;
+	reg_num++;
+	instance_data->control.reg_1->address = next_loc;
+	next_loc += regs_size;
+
+	/* reg_2 */
+	instance_data->control.reg_2 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_2), GFP_KERNEL);
+	if (!control->reg_2) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	reg_flg = hasgpio;
+	f30_attrs_regs_exist[reg_num] = true;
+	regs_size = max(sizeof(struct f30_gpio_ctrl_2n)*reg_flg*
+					instance_data->gpioled_bitmask_size, 1);
+	control->reg_2->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+
+	if (!control->reg_2->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_2->address = next_loc;
+	control->reg_2->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_3 */
+	instance_data->control.reg_3 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_3), GFP_KERNEL);
+	if (!control->reg_3) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	reg_flg = hasgpio;
+	f30_attrs_regs_exist[reg_num] = true;
+	regs_size = max(sizeof(struct f30_gpio_ctrl_3n) * reg_flg *
+					instance_data->gpioled_bitmask_size, 1);
+	control->reg_3->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+
+	if (!control->reg_3->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_3->address = next_loc;
+	control->reg_3->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_4 */
+	control->reg_4 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_4), GFP_KERNEL);
+	if (!control->reg_4) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	reg_flg = hasled;
+	f30_attrs_regs_exist[reg_num] = true;
+	regs_size = max(sizeof(struct f30_gpio_ctrl_4n)*reg_flg*
+					instance_data->gpioled_bitmask_size,
+					sizeof(struct f30_gpio_ctrl_4n));
+	control->reg_4->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+	if (!control->reg_4->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_4->address = next_loc;
+	control->reg_4->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_5 */
+	control->reg_5 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_5), GFP_KERNEL);
+	if (!control->reg_5) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	reg_flg = hasled;
+	f30_attrs_regs_exist[reg_num] = true;
+	regs_size = max(6 * reg_flg, 2);
+	control->reg_5->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+	if (!control->reg_5->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_5->address = next_loc;
+	control->reg_5->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_6 */
+	control->reg_6 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_6), GFP_KERNEL);
+	if (!control->reg_6) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	reg_flg = hasled || (!hasled
+		&& instance_data->query.has_gpio_driver_control);
+
+	regs_size = max(sizeof(union f30_gpio_ctrl_6n)*reg_flg*gpio_led_cnt,
+					sizeof(union f30_gpio_ctrl_6n));
+	if (!hasled
+		&& instance_data->query.has_gpio_driver_control)
+		f30_attrs_regs_exist[reg_num] = true;
+
+	reg_num++;
+	if (hasled)
+		f30_attrs_regs_exist[reg_num] = true;
+
+	reg_num++;
+
+	control->reg_6->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+	if (!control->reg_6->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_6->address = next_loc;
+	control->reg_6->length = regs_size;
+	next_loc += regs_size;
+
+	/* reg_7 */
+	reg_flg = hasmbtn;
+	control->reg_7 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_7), GFP_KERNEL);
+	if (!control->reg_7) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	if (hasmbtn)
+		regs_size = sizeof(struct f30_gpio_ctrl_7n)*gpio_led_cnt;
+	else
+		regs_size = sizeof(struct f30_gpio_ctrl_7n);
+
+	regs_size = max(sizeof(struct f30_gpio_ctrl_7n)*reg_flg*
+					gpio_led_cnt,
+					sizeof(struct f30_gpio_ctrl_7n));
+	f30_attrs_regs_exist[reg_num] = true;
+	control->reg_7->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+	if (!control->reg_7->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_7->address = next_loc;
+	control->reg_7->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_8 */
+	control->reg_8 =
+			kzalloc(sizeof(struct f30_gpio_ctrl_8), GFP_KERNEL);
+	if (!control->reg_8) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+
+	regs_size = max(sizeof(struct f30_gpio_ctrl_8n)*hashaptic*
+					gpio_led_cnt,
+					sizeof(struct f30_gpio_ctrl_8n));
+	f30_attrs_regs_exist[reg_num] = true;
+	control->reg_8->regs =
+			kzalloc(regs_size, GFP_KERNEL);
+	if (!control->reg_8->regs) {
+		dev_err(&fc->dev, "Failed to allocate control registers.");
+		return -ENOMEM;
+	}
+	control->reg_8->address = next_loc;
+	control->reg_8->length = regs_size;
+	next_loc += regs_size;
+	reg_num++;
+
+	/* reg_9 */
+	control->reg_9 =
+			kzalloc(sizeof(union f30_gpio_ctrl_9), GFP_KERNEL);
+	if (!control->reg_9) {
+		dev_err(&fc->dev, "Failed to allocate control register.");
+		return -ENOMEM;
+	}
+	if (instance_data->query.has_haptic)
+		f30_attrs_regs_exist[reg_num] = true;
+	control->reg_9->address = next_loc;
+	next_loc += sizeof(control->reg_9->regs);
+
+	/* data reg_0 */
+	instance_data->data.datareg_0 =
+			kzalloc(sizeof(struct f30_data_0), GFP_KERNEL);
+	if (!instance_data->data.datareg_0) {
+		dev_err(&fc->dev, "Failed to allocate control register.");
+		return -ENOMEM;
+	}
+
+	regs_size = sizeof(struct f30_data_0n)*
+				instance_data->gpioled_byte_size;
+	instance_data->data.datareg_0->address = fc->fd.data_base_addr;
+	next_loc += sizeof(instance_data->data.datareg_0->regs);
+
+	retval = rmi_f30_read_control_parameters(rmi_dev, instance_data);
+	if (retval < 0) {
+		dev_err(&fc->dev,
+			"Failed to initialize F19 control params.\n");
+		return retval;
+	}
+
+	mutex_init(&instance_data->control_mutex);
+	mutex_init(&instance_data->data_mutex);
+	return 0;
+}
+
+static int rmi_f30_create_sysfs(struct rmi_function_container *fc)
+{
+	u8 reg_num;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.");
+
+	/* Set up sysfs device attributes. */
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_query) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.");
+		return -ENODEV;
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_data) < 0) {
+		dev_err(&fc->dev, "Failed to create data sysfs files.");
+		return -ENODEV;
+	}
+
+	for (reg_num = 0; reg_num < ARRAY_SIZE(attrs_ctrl_regs);
+		reg_num++) {
+		if (f30_attrs_regs_exist[reg_num]) {
+			if (sysfs_create_group(&fc->dev.kobj,
+					&attrs_ctrl_regs[reg_num]) < 0) {
+				dev_err(&fc->dev,
+					"Failed to create "
+					"sysfs file group for reg"
+					"group %d.",
+					reg_num);
+				return -ENODEV;
+			}
+
+		}
+	}
+
+	return 0;
+}
+
+static void rmi_f30_remove(struct rmi_function_container *fc)
+{
+	dev_info(&fc->dev, "Removing F30.");
+	rmi_f30_free_memory(fc);
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x30,
+	.init = rmi_f30_init,
+	.config = rmi_f30_config,
+	.attention = rmi_f30_attention,
+	.remove = rmi_f30_remove
+};
+
+static int __init rmi_f30_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+	return 0;
+}
+
+static void rmi_f30_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+/* sysfs functions */
+/* Query */
+simple_show_union_struct_unsigned(query, extended_patterns)
+simple_show_union_struct_unsigned(query, has_mappable_buttons)
+simple_show_union_struct_unsigned(query, has_led)
+simple_show_union_struct_unsigned(query, has_gpio)
+simple_show_union_struct_unsigned(query, has_haptic)
+simple_show_union_struct_unsigned(query, has_gpio_driver_control)
+simple_show_union_struct_unsigned(query, gpio_led_count)
+
+/* Control */
+show_store_union_struct_unsigned(control, reg_1, gpio_debounce)
+show_store_union_struct_unsigned(control, reg_1, halt)
+show_store_union_struct_unsigned(control, reg_1, halted)
+show_store_union_struct_unsigned(control, reg_9, haptic_duration)
+
+/* repeated register functions */
+show_store_repeated_union_struct_unsigned(control, reg_0, led_sel)
+show_store_repeated_union_struct_unsigned(control, reg_2, dir)
+show_store_repeated_union_struct_unsigned(control, reg_3, gpiodata)
+show_store_repeated_union_struct_unsigned(control, reg_4, led_act)
+show_store_repeated_union_struct_unsigned(control, reg_5, ramp_period_a)
+show_store_repeated_union_struct_unsigned(control, reg_5, ramp_period_b)
+show_store_repeated_union_struct_unsigned(control, reg_6, SPCTRL)
+show_store_repeated_union_struct_unsigned(control, reg_6, STRPD)
+show_store_repeated_union_struct_unsigned(control, reg_6, STRPU)
+show_store_repeated_union_struct_unsigned(control, reg_6, brightness)
+show_store_repeated_union_struct_unsigned(control, reg_6, pattern)
+show_store_repeated_union_struct_unsigned(control, reg_7, capacity_btn_nbr)
+show_store_repeated_union_struct_unsigned(control, reg_7, valid)
+show_store_repeated_union_struct_unsigned(control, reg_7, invert)
+show_store_repeated_union_struct_unsigned(control, reg_7, open_drain)
+show_store_repeated_union_struct_unsigned(control, reg_8, gpio_ctrl8_0)
+show_store_repeated_union_struct_unsigned(control, reg_8, gpio_ctrl8_1)
+
+/* Data */
+show_store_repeated_union_struct_unsigned(data, datareg_0, gpi_led_data)
+
+module_init(rmi_f30_module_init);
+module_exit(rmi_f30_module_exit);
+
+MODULE_AUTHOR("Allie Xiong <[email protected]>");
+MODULE_DESCRIPTION("RMI f30 module");
+MODULE_LICENSE("GPL");
#10 lore raw

RFC PATCH 3/17 input: RMI4 physical layer drivers for I2C and SPI

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_i2c.c |  452 +++++++++++++++++++++
 drivers/input/rmi4/rmi_spi.c |  911 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1363 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_i2c.c new file +452 -0
new file mode 100644
index 0000000..1da8d96
@@ -0,0 +1,452 @@
+/*
+ * Copyright (c) 2011, 2012 Synaptics Incorporated
+ * Copyright (c) 2011 Unixphere
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define COMMS_DEBUG 0
+
+#define IRQ_DEBUG 0
+
+#if COMMS_DEBUG || IRQ_DEBUG
+#define DEBUG
+#endif
+
+#include <linux/kernel.h>
+#include <linux/lockdep.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/pm.h>
+#include <linux/gpio.h>
+#include <linux/rmi.h>
+#include "rmi_driver.h"
+
+#define RMI_PAGE_SELECT_REGISTER 0xff
+#define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
+
+static char *phys_proto_name = "i2c";
+
+struct rmi_i2c_data {
+	struct mutex page_mutex;
+	int page;
+	int enabled;
+	int irq;
+	int irq_flags;
+	struct rmi_phys_device *phys;
+};
+
+static irqreturn_t rmi_i2c_irq_thread(int irq, void *p)
+{
+	struct rmi_phys_device *phys = p;
+	struct rmi_device *rmi_dev = phys->rmi_dev;
+	struct rmi_driver *driver = rmi_dev->driver;
+	struct rmi_device_platform_data *pdata = phys->dev->platform_data;
+
+#if IRQ_DEBUG
+	dev_dbg(phys->dev, "ATTN gpio, value: %d.\n",
+			gpio_get_value(pdata->attn_gpio));
+#endif
+	if (gpio_get_value(pdata->attn_gpio) == pdata->attn_polarity) {
+		phys->info.attn_count++;
+		if (driver && driver->irq_handler && rmi_dev)
+			driver->irq_handler(rmi_dev, irq);
+	}
+
+	return IRQ_HANDLED;
+}
+
+/*
+ * rmi_set_page - Set RMI page
+ * @phys: The pointer to the rmi_phys_device struct
+ * @page: The new page address.
+ *
+ * RMI devices have 16-bit addressing, but some of the physical
+ * implementations (like SMBus) only have 8-bit addressing. So RMI implements
+ * a page address at 0xff of every page so we can reliable page addresses
+ * every 256 registers.
+ *
+ * The page_mutex lock must be held when this function is entered.
+ *
+ * Returns zero on success, non-zero on failure.
+ */
+static int rmi_set_page(struct rmi_phys_device *phys, unsigned int page)
+{
+	struct i2c_client *client = to_i2c_client(phys->dev);
+	struct rmi_i2c_data *data = phys->data;
+	char txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page};
+	int retval;
+
+#if COMMS_DEBUG
+	dev_dbg(&client->dev, "RMI4 I2C writes 3 bytes: %02x %02x\n",
+		txbuf[0], txbuf[1]);
+#endif
+	phys->info.tx_count++;
+	phys->info.tx_bytes += sizeof(txbuf);
+	retval = i2c_master_send(client, txbuf, sizeof(txbuf));
+	if (retval != sizeof(txbuf)) {
+		phys->info.tx_errs++;
+		dev_err(&client->dev,
+			"%s: set page failed: %d.", __func__, retval);
+		return (retval < 0) ? retval : -EIO;
+	}
+	data->page = page;
+	return 0;
+}
+
+static int rmi_i2c_write_block(struct rmi_phys_device *phys, u16 addr, u8 *buf,
+			       int len)
+{
+	struct i2c_client *client = to_i2c_client(phys->dev);
+	struct rmi_i2c_data *data = phys->data;
+	u8 txbuf[len + 1];
+	int retval;
+#if	COMMS_DEBUG
+	char debug_buf[len*3 + 1];
+	int i, n;
+#endif
+
+	txbuf[0] = addr & 0xff;
+	memcpy(txbuf + 1, buf, len);
+
+	mutex_lock(&data->page_mutex);
+
+	if (RMI_I2C_PAGE(addr) != data->page) {
+		retval = rmi_set_page(phys, RMI_I2C_PAGE(addr));
+		if (retval < 0)
+			goto exit;
+	}
+
+#if COMMS_DEBUG
+	n = 0;
+	for (i = 0; i < len; i++)
+		n = snprintf(debug_buf+n, 4, "%02x ", buf[i]);
+	dev_dbg(&client->dev, "RMI4 I2C writes %d bytes at %#06x: %s\n",
+		len, addr, debug_buf);
+#endif
+
+	phys->info.tx_count++;
+	phys->info.tx_bytes += sizeof(txbuf);
+	retval = i2c_master_send(client, txbuf, sizeof(txbuf));
+	if (retval < 0)
+		phys->info.tx_errs++;
+	else
+		retval--; /* don't count the address byte */
+
+exit:
+	mutex_unlock(&data->page_mutex);
+	return retval;
+}
+
+static int rmi_i2c_write(struct rmi_phys_device *phys, u16 addr, u8 data)
+{
+	int retval = rmi_i2c_write_block(phys, addr, &data, 1);
+	return (retval < 0) ? retval : 0;
+}
+
+static int rmi_i2c_read_block(struct rmi_phys_device *phys, u16 addr, u8 *buf,
+			      int len)
+{
+	struct i2c_client *client = to_i2c_client(phys->dev);
+	struct rmi_i2c_data *data = phys->data;
+	u8 txbuf[1] = {addr & 0xff};
+	int retval;
+#if	COMMS_DEBUG
+	char debug_buf[len*3 + 1];
+	char *temp = debug_buf;
+	int i, n;
+#endif
+
+	mutex_lock(&data->page_mutex);
+
+	if (RMI_I2C_PAGE(addr) != data->page) {
+		retval = rmi_set_page(phys, RMI_I2C_PAGE(addr));
+		if (retval < 0)
+			goto exit;
+	}
+
+#if COMMS_DEBUG
+	dev_dbg(&client->dev, "RMI4 I2C writes 1 bytes: %02x\n", txbuf[0]);
+#endif
+	phys->info.tx_count++;
+	phys->info.tx_bytes += sizeof(txbuf);
+	retval = i2c_master_send(client, txbuf, sizeof(txbuf));
+	if (retval != sizeof(txbuf)) {
+		phys->info.tx_errs++;
+		retval = (retval < 0) ? retval : -EIO;
+		goto exit;
+	}
+
+	retval = i2c_master_recv(client, buf, len);
+
+	phys->info.rx_count++;
+	phys->info.rx_bytes += len;
+	if (retval < 0)
+		phys->info.rx_errs++;
+#if COMMS_DEBUG
+	else {
+		n = 0;
+		for (i = 0; i < len; i++) {
+			n = sprintf(temp, " %02x", buf[i]);
+			temp += n;
+		}
+		dev_dbg(&client->dev, "RMI4 I2C read %d bytes at %#06x:%s\n",
+			len, addr, debug_buf);
+	}
+#endif
+
+exit:
+	mutex_unlock(&data->page_mutex);
+	return retval;
+}
+
+static int rmi_i2c_read(struct rmi_phys_device *phys, u16 addr, u8 *buf)
+{
+	int retval = rmi_i2c_read_block(phys, addr, buf, 1);
+	return (retval < 0) ? retval : 0;
+}
+
+static int acquire_attn_irq(struct rmi_i2c_data *data)
+{
+	return request_threaded_irq(data->irq, NULL, rmi_i2c_irq_thread,
+			data->irq_flags, dev_name(data->phys->dev), data->phys);
+}
+
+static int enable_device(struct rmi_phys_device *phys)
+{
+	int retval = 0;
+
+	struct rmi_i2c_data *data = phys->data;
+
+	if (data->enabled)
+		return 0;
+
+	retval = acquire_attn_irq(data);
+	if (retval)
+		goto error_exit;
+
+	data->enabled = true;
+	dev_dbg(phys->dev, "Physical device enabled.\n");
+	return 0;
+
+error_exit:
+	dev_err(phys->dev, "Failed to enable physical device. Code=%d.\n",
+		retval);
+	return retval;
+}
+
+static void disable_device(struct rmi_phys_device *phys)
+{
+	struct rmi_i2c_data *data = phys->data;
+
+	if (!data->enabled)
+		return;
+
+	disable_irq(data->irq);
+	free_irq(data->irq, data->phys);
+
+	dev_dbg(phys->dev, "Physical device disabled.\n");
+	data->enabled = false;
+}
+
+static int __devinit rmi_i2c_probe(struct i2c_client *client,
+				  const struct i2c_device_id *id)
+{
+	struct rmi_phys_device *rmi_phys;
+	struct rmi_i2c_data *data;
+	struct rmi_device_platform_data *pdata = client->dev.platform_data;
+	int error;
+
+	if (!pdata) {
+		dev_err(&client->dev, "no platform data\n");
+		return -EINVAL;
+	}
+	dev_info(&client->dev, "Probing %s at %#02x (IRQ %d).\n",
+		pdata->sensor_name ? pdata->sensor_name : "-no name-",
+		client->addr, pdata->attn_gpio);
+
+	if (pdata->gpio_config) {
+		dev_info(&client->dev, "Configuring GPIOs.\n");
+		error = pdata->gpio_config(pdata->gpio_data, true);
+		if (error < 0) {
+			dev_err(&client->dev, "Failed to configure GPIOs, code: %d.\n",
+				error);
+			return error;
+		}
+		dev_info(&client->dev, "Done with GPIO configuration.\n");
+	}
+
+	error = i2c_check_functionality(client->adapter, I2C_FUNC_I2C);
+	if (!error) {
+		dev_err(&client->dev, "i2c_check_functionality error %d.\n",
+			error);
+		return error;
+	}
+
+	rmi_phys = kzalloc(sizeof(struct rmi_phys_device), GFP_KERNEL);
+	if (!rmi_phys)
+		return -ENOMEM;
+
+	data = kzalloc(sizeof(struct rmi_i2c_data), GFP_KERNEL);
+	if (!data) {
+		error = -ENOMEM;
+		goto err_phys;
+	}
+
+	data->enabled = true;	/* We plan to come up enabled. */
+	data->irq = gpio_to_irq(pdata->attn_gpio);
+	if (pdata->level_triggered) {
+		data->irq_flags = IRQF_ONESHOT |
+			((pdata->attn_polarity == RMI_ATTN_ACTIVE_HIGH) ?
+			IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW);
+	} else {
+		data->irq_flags =
+			(pdata->attn_polarity == RMI_ATTN_ACTIVE_HIGH) ?
+			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
+	}
+	data->phys = rmi_phys;
+
+	rmi_phys->data = data;
+	rmi_phys->dev = &client->dev;
+
+	rmi_phys->write = rmi_i2c_write;
+	rmi_phys->write_block = rmi_i2c_write_block;
+	rmi_phys->read = rmi_i2c_read;
+	rmi_phys->read_block = rmi_i2c_read_block;
+	rmi_phys->enable_device = enable_device;
+	rmi_phys->disable_device = disable_device;
+
+	rmi_phys->info.proto = phys_proto_name;
+
+	mutex_init(&data->page_mutex);
+
+	/* Setting the page to zero will (a) make sure the PSR is in a
+	 * known state, and (b) make sure we can talk to the device.
+	 */
+	error = rmi_set_page(rmi_phys, 0);
+	if (error) {
+		dev_err(&client->dev, "Failed to set page select to 0.\n");
+		goto err_data;
+	}
+
+	error = rmi_register_phys_device(rmi_phys);
+	if (error) {
+		dev_err(&client->dev,
+			"failed to register physical driver at 0x%.2X.\n",
+			client->addr);
+		goto err_gpio;
+	}
+	i2c_set_clientdata(client, rmi_phys);
+
+	if (pdata->attn_gpio > 0) {
+		error = acquire_attn_irq(data);
+		if (error < 0) {
+			dev_err(&client->dev,
+				"request_threaded_irq failed %d\n",
+				pdata->attn_gpio);
+			goto err_unregister;
+		}
+	}
+
+#if defined(CONFIG_RMI4_DEV)
+	error = gpio_export(pdata->attn_gpio, false);
+	if (error) {
+		dev_warn(&client->dev,
+			 "WARNING: Failed to export ATTN gpio!\n");
+		error = 0;
+	} else {
+		error = gpio_export_link(&(rmi_phys->rmi_dev->dev), "attn",
+					pdata->attn_gpio);
+		if (error) {
+			dev_warn(&(rmi_phys->rmi_dev->dev),
+				 "WARNING: Failed to symlink ATTN gpio!\n");
+			error = 0;
+		} else {
+			dev_info(&(rmi_phys->rmi_dev->dev),
+				"%s: Exported ATTN GPIO %d.", __func__,
+				pdata->attn_gpio);
+		}
+	}
+#endif /* CONFIG_RMI4_DEV */
+
+	dev_info(&client->dev, "registered rmi i2c driver at %#04x.\n",
+			client->addr);
+	return 0;
+
+err_unregister:
+	rmi_unregister_phys_device(rmi_phys);
+err_gpio:
+	if (pdata->gpio_config)
+		pdata->gpio_config(pdata->gpio_data, false);
+err_data:
+	kfree(data);
+err_phys:
+	kfree(rmi_phys);
+	return error;
+}
+
+static int __devexit rmi_i2c_remove(struct i2c_client *client)
+{
+	struct rmi_phys_device *phys = i2c_get_clientdata(client);
+	struct rmi_device_platform_data *pd = client->dev.platform_data;
+
+	disable_device(phys);
+	rmi_unregister_phys_device(phys);
+	kfree(phys->data);
+	kfree(phys);
+
+	if (pd->gpio_config)
+		pd->gpio_config(&pd->gpio_data, false);
+
+	return 0;
+}
+
+static const struct i2c_device_id rmi_id[] = {
+	{ "rmi", 0 },
+	{ "rmi_i2c", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, rmi_id);
+
+static struct i2c_driver rmi_i2c_driver = {
+	.driver = {
+		.owner	= THIS_MODULE,
+		.name	= "rmi_i2c"
+	},
+	.id_table	= rmi_id,
+	.probe		= rmi_i2c_probe,
+	.remove		= __devexit_p(rmi_i2c_remove),
+};
+
+static int __init rmi_i2c_init(void)
+{
+	return i2c_add_driver(&rmi_i2c_driver);
+}
+
+static void __exit rmi_i2c_exit(void)
+{
+	i2c_del_driver(&rmi_i2c_driver);
+}
+
+module_init(rmi_i2c_init);
+module_exit(rmi_i2c_exit);
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]>");
+MODULE_DESCRIPTION("RMI I2C driver");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
drivers/input/rmi4/rmi_spi.c new file +911 -0

911 lines not on this page · raw message

#1 lore raw

RFC PATCH 1/17 input: RMI4 public header file and documentation.

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 Documentation/input/rmi4.txt     |   25 ++
 Documentation/input/rmidev.txt   |  144 +++++++++
 Documentation/input/rmisysfs.txt |  200 ++++++++++++
 include/linux/rmi.h              |  652 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 1021 insertions(+), 0 deletions(-)
Documentation/input/rmi4.txt new file +25 -0
new file mode 100644
index 0000000..4e033ff
@@ -0,0 +1,25 @@
+RMI4 Touchscreen Driver
+=======================
+
+TBD
+
+Firmware Update Function
+========================
+
+The RMI4 driver uses the kernel's request_firmware() feature to obtain
+firmware for the touch sensor.  The firmware is expected to live in
+the file firmware/rmi4/<productid>.img, where <productid> is the Product ID
+found in the F01 query registers (F01_RMI_QUERY11 through F01_RMI_QUERY20).
+
+To prepare Synaptics provided .img file for reflashing, convert it to .ihex
+format using the following command:
+
+    objcopy -I binary -O ihex <productid>.img firmware/rmi4/<productid>.img.ihex
+
+Then make sure to add the image file name to the CONFIG_RMI4_FWLIB entry in
+firmware/Makefile.  If you don't do this, the image file won't be included, and
+the firmware loader class will delay for 60 seconds waiting for a non-existent
+userspace response to the firmware load request.
+
+Firmware updates for multichip solutions (aka LTS) are not supported.
+
Documentation/input/rmidev.txt new file +144 -0

144 lines not on this page · raw message

Documentation/input/rmisysfs.txt new file +200 -0

200 lines not on this page · raw message

include/linux/rmi.h new file +652 -0

652 lines not on this page · raw message

#25 lore raw

RFC PATCH 9/17 input: RMI4 F11 multitouch sensing

In reply to Christopher Heiny
Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_f11.c | 2439 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 2439 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_f11.c new file +2439 -0
new file mode 100644
index 0000000..86d7693
@@ -0,0 +1,2439 @@
+/*
+ * Copyright (c) 2011,2012 Synaptics Incorporated
+ * Copyright (c) 2011 Unixphere
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define FUNCTION_DATA f11_data
+#define FNUM 11
+
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/input.h>
+#ifdef CONFIG_RMI4_F11_TYPEB
+#include <linux/mt.h>
+#endif
+#include <linux/rmi.h>
+#include "rmi_driver.h"
+
+#ifdef CONFIG_RMI4_DEBUG
+#include <linux/debugfs.h>
+#include <linux/fs.h>
+/*#include <asm/uaccess.h> */
+#include <linux/uaccess.h>
+#endif
+
+#define RESUME_REZERO (1 && defined(CONFIG_PM))
+#if RESUME_REZERO
+#include <linux/delay.h>
+#define DEFAULT_REZERO_WAIT_MS	40
+#endif
+
+#ifndef MT_TOOL_MAX
+#define MT_TOOL_MAX MT_TOOL_PEN
+#endif
+
+#define F11_MAX_NUM_OF_SENSORS		8
+#define F11_MAX_NUM_OF_FINGERS		10
+#define F11_MAX_NUM_OF_TOUCH_SHAPES	16
+
+#define F11_REL_POS_MIN		-128
+#define F11_REL_POS_MAX		127
+
+#define FINGER_STATE_MASK	0x03
+#define GET_FINGER_STATE(f_states, i) \
+	((f_states[i / 4] >> (2 * (i % 4))) & FINGER_STATE_MASK)
+
+#define F11_CTRL_SENSOR_MAX_X_POS_OFFSET	6
+#define F11_CTRL_SENSOR_MAX_Y_POS_OFFSET	8
+
+#define F11_CEIL(x, y) (((x) + ((y)-1)) / (y))
+#define INBOX(x, y, box) (x >= box.x && x < (box.x + box.width) \
+			&& y >= box.y && y < (box.y + box.height))
+
+#define DEFAULT_XY_MAX 9999
+#define DEFAULT_MAX_ABS_MT_PRESSURE 255
+#define DEFAULT_MAX_ABS_MT_TOUCH 15
+#define DEFAULT_MAX_ABS_MT_ORIENTATION 1
+#define DEFAULT_MIN_ABS_MT_TRACKING_ID 1
+#define DEFAULT_MAX_ABS_MT_TRACKING_ID 10
+#define MAX_NAME_LENGTH 256
+
+/* Adding debugfs for flip, clip, offset and swap */
+#ifdef CONFIG_RMI4_DEBUG
+
+static int setup_debugfs(struct rmi_device *rmi_dev);
+static void teardown_debugfs(struct rmi_device *rmi_dev);
+#endif
+/* End adding debugfs */
+
+static ssize_t f11_relreport_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t f11_relreport_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+
+static ssize_t f11_maxPos_show(struct device *dev,
+				     struct device_attribute *attr, char *buf);
+
+static ssize_t f11_rezero_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+
+#if RESUME_REZERO
+static ssize_t f11_rezeroOnResume_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t f11_rezeroOnResume_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+static ssize_t f11_rezeroWait_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf);
+
+static ssize_t f11_rezeroWait_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t count);
+#endif
+
+static void rmi_f11_free_memory(struct rmi_function_container *fc);
+
+static int rmi_f11_initialize(struct rmi_function_container *fc);
+
+static int rmi_f11_create_sysfs(struct rmi_function_container *fc);
+
+static int rmi_f11_config(struct rmi_function_container *fc);
+
+static int rmi_f11_reset(struct rmi_function_container *fc);
+
+static int rmi_f11_register_devices(struct rmi_function_container *fc);
+
+static void rmi_f11_free_devices(struct rmi_function_container *fc);
+
+static void f11_set_abs_params(struct rmi_function_container *fc, int index);
+
+static struct device_attribute attrs[] = {
+	__ATTR(relreport, RMI_RW_ATTR, f11_relreport_show, f11_relreport_store),
+	__ATTR(maxPos, RMI_RO_ATTR, f11_maxPos_show, rmi_store_error),
+#if RESUME_REZERO
+	__ATTR(rezeroOnResume, RMI_RW_ATTR, f11_rezeroOnResume_show,
+		f11_rezeroOnResume_store),
+	__ATTR(rezeroWait, RMI_RW_ATTR, f11_rezeroWait_show,
+		f11_rezeroWait_store),
+#endif
+	__ATTR(rezero, RMI_WO_ATTR, rmi_show_error, f11_rezero_store)
+};
+
+
+union f11_2d_commands {
+	struct {
+		u8 rezero:1;
+	};
+	u8 reg;
+};
+
+struct f11_2d_device_query {
+	union {
+		struct {
+			u8 nbr_of_sensors:3;
+			u8 has_query9:1;
+			u8 has_query11:1;
+		};
+		u8 f11_2d_query0;
+	};
+
+	union {
+		struct {
+			u8 has_z_tuning:1;
+			u8 has_pos_interpolation_tuning:1;
+			u8 has_w_tuning:1;
+			u8 has_pitch_info:1;
+			u8 has_default_finger_width:1;
+			u8 has_segmentation_aggressiveness:1;
+			u8 has_tx_rw_clip:1;
+			u8 has_drumming_correction:1;
+		};
+		u8 f11_2d_query11;
+	};
+};
+
+union f11_2d_query9 {
+	struct {
+		u8 has_pen:1;
+		u8 has_proximity:1;
+		u8 has_palm_det_sensitivity:1;
+		u8 has_suppress_on_palm_detect:1;
+		u8 has_two_pen_thresholds:1;
+		u8 has_contact_geometry:1;
+	};
+	u8 reg;
+};
+
+struct f11_2d_sensor_query {
+	union {
+		struct {
+			/* query1 */
+			u8 number_of_fingers:3;
+			u8 has_rel:1;
+			u8 has_abs:1;
+			u8 has_gestures:1;
+			u8 has_sensitivity_adjust:1;
+			u8 configurable:1;
+			/* query2 */
+			u8 num_of_x_electrodes:7;
+			/* query3 */
+			u8 num_of_y_electrodes:7;
+			/* query4 */
+			u8 max_electrodes:7;
+		};
+		u8 f11_2d_query1__4[4];
+	};
+
+	union {
+		struct {
+			u8 abs_data_size:3;
+			u8 has_anchored_finger:1;
+			u8 has_adj_hyst:1;
+			u8 has_dribble:1;
+		};
+		u8 f11_2d_query5;
+	};
+
+	u8 f11_2d_query6;
+
+	union {
+		struct {
+			u8 has_single_tap:1;
+			u8 has_tap_n_hold:1;
+			u8 has_double_tap:1;
+			u8 has_early_tap:1;
+			u8 has_flick:1;
+			u8 has_press:1;
+			u8 has_pinch:1;
+			u8 padding:1;
+
+			u8 has_palm_det:1;
+			u8 has_rotate:1;
+			u8 has_touch_shapes:1;
+			u8 has_scroll_zones:1;
+			u8 has_individual_scroll_zones:1;
+			u8 has_multi_finger_scroll:1;
+		};
+		u8 f11_2d_query7__8[2];
+	};
+
+	union f11_2d_query9 query9;
+
+	union {
+		struct {
+			u8 nbr_touch_shapes:5;
+		};
+		u8 f11_2d_query10;
+	};
+};
+union f11_2d_ctrl0_9 {
+	struct {
+		/* F11_2D_Ctrl0 */
+		u8 reporting_mode:3;
+		u8 abs_pos_filt:1;
+		u8 rel_pos_filt:1;
+		u8 rel_ballistics:1;
+		u8 dribble:1;
+		u8 report_beyond_clip:1;
+		/* F11_2D_Ctrl1 */
+		u8 palm_detect_thres:4;
+		u8 motion_sensitivity:2;
+		u8 man_track_en:1;
+		u8 man_tracked_finger:1;
+		/* F11_2D_Ctrl2 and 3 */
+		u8 delta_x_threshold:8;
+		u8 delta_y_threshold:8;
+		/* F11_2D_Ctrl4 and 5 */
+		u8 velocity:8;
+		u8 acceleration:8;
+		/* F11_2D_Ctrl6 thru 9 */
+		u16 sensor_max_x_pos:12;
+		u8 ctrl7_reserved:4;
+		u16 sensor_max_y_pos:12;
+		u8 ctrl9_reserved:4;
+	};
+	struct {
+		u8 regs[10];
+		u16 address;
+	};
+};
+union f11_2d_ctrl10 {
+	struct {
+		u8 single_tap_int_enable:1;
+		u8 tap_n_hold_int_enable:1;
+		u8 double_tap_int_enable:1;
+		u8 early_tap_int_enable:1;
+		u8 flick_int_enable:1;
+		u8 press_int_enable:1;
+		u8 pinch_int_enable:1;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl11 {
+	struct {
+		u8 palm_detect_int_enable:1;
+		u8 rotate_int_enable:1;
+		u8 touch_shape_int_enable:1;
+		u8 scroll_zone_int_enable:1;
+		u8 multi_finger_scroll_int_enable:1;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl12 {
+	struct {
+		u8 sensor_map:7;
+		u8 xy_sel:1;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl14 {
+	struct {
+		u8 sens_adjustment:5;
+		u8 hyst_adjustment:3;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl15 {
+	struct {
+		u8 max_tap_time:8;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl16 {
+	struct {
+		u8 min_press_time:8;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl17 {
+	struct {
+		u8 max_tap_distance:8;
+	};
+	u8 reg;
+};
+
+union f11_2d_ctrl18_19 {
+	struct {
+		u8 min_flick_distance:8;
+		u8 min_flick_speed:8;
+	};
+	u8 reg[2];
+};
+
+union f11_2d_ctrl20_21 {
+	struct {
+		u8 pen_detect_enable:1;
+		u8 pen_jitter_filter_enable:1;
+		u8 ctrl20_reserved:6;
+		u8 pen_z_threshold:8;
+	};
+	u8 reg[2];
+};
+
+/* These are not accessible through sysfs yet. */
+union f11_2d_ctrl22_26 {
+	struct {
+		/* control 22 */
+		u8 proximity_detect_int_en:1;
+		u8 proximity_jitter_filter_en:1;
+		u8 f11_2d_ctrl6_b3__7:6;
+
+		/* control 23 */
+		u8 proximity_detection_z_threshold;
+
+		/* control 24 */
+		u8 proximity_delta_x_threshold;
+
+		/* control 25 */
+		u8 proximity_delta_y_threshold;
+
+		/* control 26 */
+		u8 proximity_delta_z_threshold;
+	};
+	u8 regs[5];
+};
+
+/* control 27 - haspalmdetectsensitivity or has suppressonpalmdetect */
+union f11_2d_ctrl27 {
+	struct {
+		u8 palm_detecy_sensitivity:4;
+		u8 suppress_on_palm_detect:1;
+		u8 f11_2d_ctrl27_b5__7:3;
+	};
+	u8 regs[1];
+};
+
+/* control 28 - has_multifingerscroll */
+union f11_2d_ctrl28 {
+	struct {
+		u8 multi_finger_scroll_mode:2;
+		u8 edge_motion_en:1;
+		u8 f11_2d_ctrl28b_3:1;
+		u8 multi_finger_scroll_momentum:4;
+	};
+	u8 regs[1];
+};
+
+/* control 29 & 30 - hasztuning */
+union f11_2d_ctrl29_30 {
+	struct {
+		u8 z_touch_threshold;
+		u8 z_touch_hysteresis;
+	};
+	struct {
+		u8 regs[2];
+		u16 address;
+	};
+};
+
+
+struct  f11_2d_ctrl {
+	union f11_2d_ctrl0_9 *ctrl0_9;
+	union f11_2d_ctrl10		*ctrl10;
+	union f11_2d_ctrl11		*ctrl11;
+	union f11_2d_ctrl12		*ctrl12;
+	u8				ctrl12_size;
+	union f11_2d_ctrl14		*ctrl14;
+	union f11_2d_ctrl15		*ctrl15;
+	union f11_2d_ctrl16		*ctrl16;
+	union f11_2d_ctrl17		*ctrl17;
+	union f11_2d_ctrl18_19		*ctrl18_19;
+	union f11_2d_ctrl20_21		*ctrl20_21;
+	union f11_2d_ctrl22_26 *ctrl22_26;
+	union f11_2d_ctrl27 *ctrl27;
+	union f11_2d_ctrl28 *ctrl28;
+	union f11_2d_ctrl29_30 *ctrl29_30;
+};
+
+struct f11_2d_data_1_5 {
+	u8 x_msb;
+	u8 y_msb;
+	u8 x_lsb:4;
+	u8 y_lsb:4;
+	u8 w_y:4;
+	u8 w_x:4;
+	u8 z;
+};
+
+struct f11_2d_data_6_7 {
+	s8 delta_x;
+	s8 delta_y;
+};
+
+struct f11_2d_data_8 {
+	u8 single_tap:1;
+	u8 tap_and_hold:1;
+	u8 double_tap:1;
+	u8 early_tap:1;
+	u8 flick:1;
+	u8 press:1;
+	u8 pinch:1;
+};
+
+struct f11_2d_data_9 {
+	u8 palm_detect:1;
+	u8 rotate:1;
+	u8 shape:1;
+	u8 scrollzone:1;
+	u8 finger_count:3;
+};
+
+struct f11_2d_data_10 {
+	u8 pinch_motion;
+};
+
+struct f11_2d_data_10_12 {
+	u8 x_flick_dist;
+	u8 y_flick_dist;
+	u8 flick_time;
+};
+
+struct f11_2d_data_11_12 {
+	u8 motion;
+	u8 finger_separation;
+};
+
+struct f11_2d_data_13 {
+	u8 shape_n;
+};
+
+struct f11_2d_data_14_15 {
+	u8 horizontal;
+	u8 vertical;
+};
+
+struct f11_2d_data_14_17 {
+	u8 x_low;
+	u8 y_right;
+	u8 x_upper;
+	u8 y_left;
+};
+
+struct f11_2d_data {
+	u8				*f_state;
+	const struct f11_2d_data_1_5	*abs_pos;
+	const struct f11_2d_data_6_7	*rel_pos;
+	const struct f11_2d_data_8	*gest_1;
+	const struct f11_2d_data_9	*gest_2;
+	const struct f11_2d_data_10	*pinch;
+	const struct f11_2d_data_10_12	*flick;
+	const struct f11_2d_data_11_12	*rotate;
+	const struct f11_2d_data_13	*shapes;
+	const struct f11_2d_data_14_15	*multi_scroll;
+	const struct f11_2d_data_14_17	*scroll_zones;
+};
+
+struct f11_2d_sensor {
+	struct rmi_f11_2d_axis_alignment axis_align;
+	struct f11_2d_sensor_query sens_query;
+	struct f11_2d_data data;
+	u16 max_x;
+	u16 max_y;
+	u8 nbr_fingers;
+	u8 finger_tracker[F11_MAX_NUM_OF_FINGERS];
+	u8 *data_pkt;
+	int pkt_size;
+	u8 sensor_index;
+	struct rmi_button_map virtualbutton_map;
+	char input_name[MAX_NAME_LENGTH];
+	char input_phys[MAX_NAME_LENGTH];
+	struct input_dev *input;
+	struct input_dev *mouse_input;
+};
+
+struct f11_data {
+	struct f11_2d_device_query dev_query;
+	struct f11_2d_ctrl dev_controls;
+	struct mutex dev_controls_mutex;
+#if	RESUME_REZERO
+	u16 rezero_wait_ms;
+	bool rezero_on_resume;
+#endif
+	struct f11_2d_sensor sensors[F11_MAX_NUM_OF_SENSORS];
+	bool type_b;
+};
+
+enum finger_state_values {
+	F11_NO_FINGER	= 0x00,
+	F11_PRESENT	= 0x01,
+	F11_INACCURATE	= 0x02,
+	F11_RESERVED	= 0x03
+};
+
+/* ctrl sysfs files */
+show_store_union_struct_prototype(abs_pos_filt)
+show_store_union_struct_prototype(z_touch_threshold)
+show_store_union_struct_prototype(z_touch_hysteresis)
+
+/* Adding debugfs for flip, clip, offset and swap */
+#ifdef CONFIG_RMI4_DEBUG
+
+struct f11_debugfs_data {
+	bool done;
+	struct rmi_device *rmi_dev;
+};
+
+static int debug_open(struct inode *inodep, struct file *filp)
+{
+	struct f11_debugfs_data *data;
+
+	data = kzalloc(sizeof(struct f11_debugfs_data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->rmi_dev = inodep->i_private;
+	filp->private_data = data;
+	return 0;
+}
+
+static int debug_release(struct inode *inodep, struct file *filp)
+{
+	kfree(filp->private_data);
+	return 0;
+}
+
+#define FLIP_NAME "flip"
+#define CLIP_NAME "clip"
+#define OFFSET_NAME "offset"
+#define SWAP_NAME "swap"
+
+
+static ssize_t flip_read(struct file *filp, char __user *buffer, size_t size,
+		    loff_t *offset) {
+	struct f11_debugfs_data *dfs;
+	int retval;
+	char local_buf[size];
+	struct rmi_device_platform_data *data;
+	dfs = filp->private_data;
+	data = dfs->rmi_dev->phys->dev->platform_data;
+
+	if (dfs->done)
+		return 0;
+
+	dfs->done = 1;
+
+	retval = snprintf(local_buf, PAGE_SIZE, "%u %u\n",
+			data->axis_align.flip_x,
+			data->axis_align.flip_y);
+
+	if (retval <= 0 || copy_to_user(buffer, local_buf, retval))
+		return -EFAULT;
+
+	return retval;
+}
+
+static ssize_t flip_write(struct file *filp, const char __user *buffer,
+			   size_t size, loff_t *offset) {
+
+	struct f11_debugfs_data *dfs = filp->private_data;
+	int retval;
+	char local_buf[size];
+	unsigned int new_X;
+	unsigned int new_Y;
+	struct rmi_device_platform_data *data =
+			dfs->rmi_dev->phys->dev->platform_data;
+
+	retval = copy_from_user(local_buf, buffer, size);
+	if (retval)
+		return -EFAULT;
+
+	retval = sscanf(local_buf, "%u %u", &new_X, &new_Y);
+	if (retval != 2) {
+		dev_err(&dfs->rmi_dev->dev,
+			"Incorrect number of values provided for flip.");
+		return -EINVAL;
+	}
+	if (new_X < 0 || new_X > 1 || new_Y < 0 || new_Y > 1)
+		return -EINVAL;
+
+	dev_dbg(&dfs->rmi_dev->dev,
+		 "Setting flip to %u %u.\n", new_X, new_Y);
+	data->axis_align.flip_x = new_X;
+	data->axis_align.flip_y = new_Y;
+
+	return size;
+}
+
+static const struct file_operations flip_fops = {
+	.owner = THIS_MODULE,
+	.open = debug_open,
+	.release = debug_release,
+	.read = flip_read,
+	.write = flip_write,
+};
+
+
+static ssize_t offset_read(struct file *filp, char __user *buffer, size_t size,
+		    loff_t *offset) {
+
+	struct f11_debugfs_data *dfs;
+	int retval;
+	char local_buf[size];
+	struct rmi_device_platform_data *data;
+	dfs = filp->private_data;
+	data = dfs->rmi_dev->phys->dev->platform_data;
+
+	if (dfs->done)
+		return 0;
+
+	dfs->done = 1;
+	retval = snprintf(local_buf, PAGE_SIZE, "%u %u\n",
+			data->axis_align.offset_X,
+			data->axis_align.offset_Y);
+
+	if (retval <= 0 || copy_to_user(buffer, local_buf, retval))
+		return -EFAULT;
+
+	return retval;
+}
+
+static ssize_t offset_write(struct file *filp, const char __user *buffer,
+			   size_t size, loff_t *offset)
+{
+	struct f11_debugfs_data *dfs = filp->private_data;
+	int retval;
+	char local_buf[size];
+	int new_X;
+	int new_Y;
+	struct rmi_device_platform_data *data =
+			dfs->rmi_dev->phys->dev->platform_data;
+
+	retval = copy_from_user(local_buf, buffer, size);
+	if (retval)
+		return -EFAULT;
+	retval = sscanf(local_buf, "%u %u", &new_X, &new_Y);
+	if (retval != 2) {
+		dev_err(&dfs->rmi_dev->dev,
+			"Incorrect number of values provided for offset.");
+		return -EINVAL;
+	}
+
+	dev_dbg(&dfs->rmi_dev->dev,
+		 "Setting offset to %u %u.\n", new_X, new_Y);
+	data->axis_align.offset_X = new_X;
+	data->axis_align.offset_Y = new_Y;
+
+	return size;
+}
+
+static const struct file_operations offset_fops = {
+	.owner = THIS_MODULE,
+	.open = debug_open,
+	.release = debug_release,
+	.read = offset_read,
+	.write = offset_write,
+};
+
+static ssize_t clip_read(struct file *filp, char __user *buffer, size_t size,
+		    loff_t *offset) {
+
+	struct f11_debugfs_data *dfs;
+	int retval;
+	char local_buf[size];
+	struct rmi_device_platform_data *data;
+	dfs = filp->private_data;
+	data = dfs->rmi_dev->phys->dev->platform_data;
+
+	if (dfs->done)
+		return 0;
+
+	dfs->done = 1;
+
+	retval = snprintf(local_buf, PAGE_SIZE, "%u %u %u %u\n",
+			data->axis_align.clip_X_low,
+			data->axis_align.clip_X_high,
+			data->axis_align.clip_Y_low,
+			data->axis_align.clip_Y_high);
+
+	if (retval <= 0 || copy_to_user(buffer, local_buf, retval))
+		return -EFAULT;
+
+	return retval;
+}
+
+static ssize_t clip_write(struct file *filp, const char __user *buffer,
+			   size_t size, loff_t *offset)
+{
+	struct f11_debugfs_data *dfs = filp->private_data;
+	int retval;
+	char local_buf[size];
+	unsigned int new_X_low, new_X_high, new_Y_low, new_Y_high;
+
+	struct rmi_device_platform_data *data =
+			dfs->rmi_dev->phys->dev->platform_data;
+
+	retval = copy_from_user(local_buf, buffer, size);
+	if (retval)
+		return -EFAULT;
+
+	retval = sscanf(local_buf, "%u %u %u %u",
+		&new_X_low, &new_X_high, &new_Y_low, &new_Y_high);
+	if (retval != 4) {
+		dev_err(&dfs->rmi_dev->dev,
+			"Incorrect number of values provided for clip.");
+		return -EINVAL;
+	}
+
+	if (new_X_low < 0 || new_X_low >= new_X_high || new_Y_low < 0
+	    || new_Y_low >= new_Y_high)
+		return -EINVAL;
+
+	dev_dbg(&dfs->rmi_dev->dev,
+		 "Setting clip to %u %u %u %u.\n", new_X_low, new_X_high,
+			new_Y_low, new_Y_high);
+
+	data->axis_align.clip_X_low = new_X_low;
+	data->axis_align.clip_X_high = new_X_high;
+	data->axis_align.clip_Y_low = new_Y_low;
+	data->axis_align.clip_Y_high = new_Y_high;
+
+	return size;
+}
+
+static const struct file_operations clip_fops = {
+	.owner = THIS_MODULE,
+	.open = debug_open,
+	.release = debug_release,
+	.read = clip_read,
+	.write = clip_write,
+};
+
+static ssize_t swap_read(struct file *filp, char __user *buffer, size_t size,
+		    loff_t *offset) {
+
+	struct f11_debugfs_data *dfs;
+	int retval;
+	char local_buf[size];
+	struct rmi_device_platform_data *data;
+	dfs = filp->private_data;
+	data = dfs->rmi_dev->phys->dev->platform_data;
+
+	if (dfs->done)
+		return 0;
+
+	dfs->done = 1;
+
+	retval = snprintf(local_buf, PAGE_SIZE, "%u\n",
+			data->axis_align.swap_axes);
+
+	if (retval <= 0 || copy_to_user(buffer, local_buf, retval))
+		return -EFAULT;
+
+	return retval;
+}
+
+static ssize_t swap_write(struct file *filp, const char __user *buffer,
+			   size_t size, loff_t *offset)
+{
+	struct f11_debugfs_data *dfs = filp->private_data;
+	struct rmi_device_platform_data *data =
+			dfs->rmi_dev->phys->dev->platform_data;
+
+	int retval;
+	char local_buf[size];
+	int newSwap;
+
+	if (!dfs->rmi_dev->debugfs_root)
+		return -ENODEV;
+
+	retval = copy_from_user(local_buf, buffer, size);
+	if (retval)
+		return -EFAULT;
+	retval = sscanf(local_buf, "%u", &newSwap);
+	if (retval != 1) {
+		dev_err(&dfs->rmi_dev->dev,
+			"Incorrect number of values provided for swap.");
+		return -EINVAL;
+	}
+	if (newSwap < 0 || newSwap > 1)
+		return -EINVAL;
+
+	data->axis_align.swap_axes = newSwap;
+	return size;
+}
+
+static const struct file_operations swap_fops = {
+	.owner = THIS_MODULE,
+	.open = debug_open,
+	.release = debug_release,
+	.read = swap_read,
+	.write = swap_write,
+};
+
+static int setup_debugfs(struct rmi_device *rmi_dev)
+{
+	int retval = 0;
+	struct rmi_device_platform_data *data;
+	data = rmi_dev->phys->dev->platform_data;
+
+	if (!rmi_dev->debugfs_root)
+		return -ENODEV;
+
+	data->axis_align.debugfs_flip
+		= debugfs_create_file(FLIP_NAME, RMI_RW_ATTR,
+				rmi_dev->debugfs_root, rmi_dev, &flip_fops);
+	if (!data->axis_align.debugfs_flip
+		|| IS_ERR(data->axis_align.debugfs_flip)) {
+		dev_warn(&rmi_dev->dev, "Failed to create debugfs flip.\n");
+		data->axis_align.debugfs_flip = NULL;
+	}
+
+	data->axis_align.debugfs_clip
+		= debugfs_create_file(CLIP_NAME, RMI_RW_ATTR,
+				rmi_dev->debugfs_root, rmi_dev, &clip_fops);
+	if (!data->axis_align.debugfs_clip
+		|| IS_ERR(data->axis_align.debugfs_clip)) {
+		dev_warn(&rmi_dev->dev, "Failed to create debugfs clip.\n");
+		data->axis_align.debugfs_clip = NULL;
+	}
+
+	data->axis_align.debugfs_offset
+		= debugfs_create_file(OFFSET_NAME, RMI_RW_ATTR,
+				rmi_dev->debugfs_root, rmi_dev, &offset_fops);
+	if (!data->axis_align.debugfs_offset
+		|| IS_ERR(data->axis_align.debugfs_offset)) {
+		dev_warn(&rmi_dev->dev, "Failed to create debugfs offset.\n");
+		data->axis_align.debugfs_offset = NULL;
+	}
+
+	data->axis_align.debugfs_swap
+		= debugfs_create_file(SWAP_NAME, RMI_RW_ATTR,
+				rmi_dev->debugfs_root, rmi_dev, &swap_fops);
+	if (!data->axis_align.debugfs_swap
+		|| IS_ERR(data->axis_align.debugfs_swap)) {
+		dev_warn(&rmi_dev->dev, "Failed to create debugfs swap.\n");
+		data->axis_align.debugfs_swap = NULL;
+	}
+
+	return retval;
+}
+
+static void teardown_debugfs(struct rmi_device *rmi_dev)
+{
+	struct rmi_device_platform_data *data;
+	data = rmi_dev->phys->dev->platform_data;
+
+	if (!data->axis_align.debugfs_flip)
+		debugfs_remove(data->axis_align.debugfs_flip);
+
+	if (!data->axis_align.debugfs_clip)
+		debugfs_remove(data->axis_align.debugfs_clip);
+
+	if (!data->axis_align.debugfs_offset)
+		debugfs_remove(data->axis_align.debugfs_offset);
+
+	if (!data->axis_align.debugfs_swap)
+		debugfs_remove(data->axis_align.debugfs_swap);
+}
+
+#endif
+/* End adding debugfs */
+
+/* This is a group in case we add the other ctrls. */
+static struct attribute *attrs_ctrl0[] = {
+	attrify(abs_pos_filt),
+	NULL
+};
+static struct attribute_group attrs_control0 = GROUP(attrs_ctrl0);
+
+static struct attribute *attrs_ctrl29_30[] = {
+	attrify(z_touch_threshold),
+	attrify(z_touch_hysteresis),
+	NULL
+};
+static struct attribute_group attrs_control29_30 = GROUP(attrs_ctrl29_30);
+
+/** F11_INACCURATE state is overloaded to indicate pen present. */
+#define F11_PEN F11_INACCURATE
+
+static int get_tool_type(struct f11_2d_sensor *sensor, u8 finger_state)
+{
+#ifdef	CONFIG_RMI4_F11_PEN
+	if (sensor->sens_query.query9.has_pen && finger_state == F11_PEN)
+		return MT_TOOL_PEN;
+#endif
+	return MT_TOOL_FINGER;
+}
+
+static void rmi_f11_rel_pos_report(struct f11_2d_sensor *sensor, u8 n_finger)
+{
+	struct f11_2d_data *data = &sensor->data;
+	struct rmi_f11_2d_axis_alignment *axis_align = &sensor->axis_align;
+	s8 x, y;
+	s8 temp;
+
+	x = data->rel_pos[n_finger].delta_x;
+	y = data->rel_pos[n_finger].delta_y;
+
+	x = min(F11_REL_POS_MAX, max(F11_REL_POS_MIN, (int)x));
+	y = min(F11_REL_POS_MAX, max(F11_REL_POS_MIN, (int)y));
+
+	if (axis_align->swap_axes) {
+		temp = x;
+		x = y;
+		y = temp;
+	}
+	if (axis_align->flip_x)
+		x = min(F11_REL_POS_MAX, -x);
+	if (axis_align->flip_y)
+		y = min(F11_REL_POS_MAX, -y);
+
+	if (x || y) {
+		input_report_rel(sensor->input, REL_X, x);
+		input_report_rel(sensor->input, REL_Y, y);
+		input_report_rel(sensor->mouse_input, REL_X, x);
+		input_report_rel(sensor->mouse_input, REL_Y, y);
+	}
+	input_sync(sensor->mouse_input);
+}
+
+static void rmi_f11_abs_pos_report(struct f11_data *f11,
+				   struct f11_2d_sensor *sensor,
+				   u8 finger_state, u8 n_finger)
+{
+	struct f11_2d_data *data = &sensor->data;
+	struct rmi_f11_2d_axis_alignment *axis_align = &sensor->axis_align;
+	int prev_state = sensor->finger_tracker[n_finger];
+	int x, y, z;
+	int w_x, w_y, w_max, w_min, orient;
+	int temp;
+
+	if (prev_state && !finger_state) {
+		/* this is a release */
+		x = y = z = w_max = w_min = orient = 0;
+	} else if (!prev_state && !finger_state) {
+		/* nothing to report */
+		return;
+	} else {
+		x = ((data->abs_pos[n_finger].x_msb << 4) |
+			data->abs_pos[n_finger].x_lsb);
+		y = ((data->abs_pos[n_finger].y_msb << 4) |
+			data->abs_pos[n_finger].y_lsb);
+		z = data->abs_pos[n_finger].z;
+		w_x = data->abs_pos[n_finger].w_x;
+		w_y = data->abs_pos[n_finger].w_y;
+		w_max = max(w_x, w_y);
+		w_min = min(w_x, w_y);
+
+		if (axis_align->swap_axes) {
+			temp = x;
+			x = y;
+			y = temp;
+			temp = w_x;
+			w_x = w_y;
+			w_y = temp;
+		}
+
+		orient = w_x > w_y ? 1 : 0;
+
+		if (axis_align->flip_x)
+			x = max(sensor->max_x - x, 0);
+
+		if (axis_align->flip_y)
+			y = max(sensor->max_y - y, 0);
+
+		/*
+		** here checking if X offset or y offset are specified is
+		**  redundant.  We just add the offsets or, clip the values
+		**
+		** note: offsets need to be done before clipping occurs,
+		** or we could get funny values that are outside
+		** clipping boundaries.
+		*/
+		x += axis_align->offset_X;
+		y += axis_align->offset_Y;
+		x =  max(axis_align->clip_X_low, x);
+		y =  max(axis_align->clip_Y_low, y);
+		if (axis_align->clip_X_high)
+			x = min(axis_align->clip_X_high, x);
+		if (axis_align->clip_Y_high)
+			y =  min(axis_align->clip_Y_high, y);
+
+	}
+
+	pr_debug("%s: f_state[%d]:%d - x:%d y:%d z:%d w_max:%d w_min:%d\n",
+		__func__, n_finger, finger_state, x, y, z, w_max, w_min);
+#ifndef CONFIG_RMI4_F11_PEN
+	/* Some UIs ignore W of zero, so we fudge it to 1 for pens. */
+	if (get_tool_type(sensor, finger_state) == MT_TOOL_PEN) {
+		w_max = max(1, w_max);
+		w_min = max(1, w_min);
+	}
+#endif
+
+#ifdef CONFIG_RMI4_F11_TYPEB
+	if (f11->type_b) {
+		input_mt_slot(sensor->input, n_finger);
+		input_mt_report_slot_state(sensor->input,
+					   get_tool_type(sensor, finger_state),
+					   finger_state);
+	} else
+		input_report_abs(sensor->input, ABS_MT_TOOL_TYPE,
+				 get_tool_type(sensor, finger_state));
+#else
+	input_report_abs(sensor->input, ABS_MT_TOOL_TYPE,
+				get_tool_type(sensor, finger_state));
+#endif
+
+#ifdef ABS_MT_PRESSURE
+	input_report_abs(sensor->input, ABS_MT_PRESSURE, z);
+#endif
+	input_report_abs(sensor->input, ABS_MT_TOUCH_MAJOR, w_max);
+	input_report_abs(sensor->input, ABS_MT_TOUCH_MINOR, w_min);
+	input_report_abs(sensor->input, ABS_MT_ORIENTATION, orient);
+	input_report_abs(sensor->input, ABS_MT_POSITION_X, x);
+	input_report_abs(sensor->input, ABS_MT_POSITION_Y, y);
+	input_report_abs(sensor->input, ABS_MT_TRACKING_ID, n_finger);
+	input_report_abs(sensor->input, ABS_MT_TOOL_TYPE,
+				get_tool_type(sensor, finger_state));
+
+	/* MT sync between fingers */
+#ifdef CONFIG_RMI4_F11_TYPEB
+	if (!f11->type_b)
+		input_mt_sync(sensor->input);
+#else
+	input_mt_sync(sensor->input);
+#endif
+	sensor->finger_tracker[n_finger] = finger_state;
+}
+
+#ifdef CONFIG_RMI4_VIRTUAL_BUTTON
+static int rmi_f11_virtual_button_handler(struct f11_2d_sensor *sensor)
+{
+	int i;
+	int x;
+	int y;
+	struct rmi_button_map *virtualbutton_map;
+
+	if (sensor->sens_query.has_gestures &&
+		sensor->data.gest_1->single_tap) {
+		virtualbutton_map = &sensor->virtualbutton_map;
+		x = ((sensor->data.abs_pos[0].x_msb << 4) |
+			sensor->data.abs_pos[0].x_lsb);
+		y = ((sensor->data.abs_pos[0].y_msb << 4) |
+			sensor->data.abs_pos[0].y_lsb);
+		for (i = 0; i < virtualbutton_map->buttons; i++) {
+			if (INBOX(x, y, virtualbutton_map->map[i])) {
+				input_report_key(sensor->input,
+					virtualbutton_map->map[i].code, 1);
+				input_report_key(sensor->input,
+					virtualbutton_map->map[i].code, 0);
+				input_sync(sensor->input);
+				return 0;
+			}
+		}
+	}
+	return 0;
+}
+#else
+#define rmi_f11_virtual_button_handler(sensor)
+#endif
+static void rmi_f11_finger_handler(struct f11_data *f11,
+				   struct f11_2d_sensor *sensor)
+{
+	const u8 *f_state = sensor->data.f_state;
+	u8 finger_state;
+	u8 finger_pressed_count;
+	u8 i;
+
+	for (i = 0, finger_pressed_count = 0; i < sensor->nbr_fingers; i++) {
+		/* Possible of having 4 fingers per f_statet register */
+		finger_state = GET_FINGER_STATE(f_state, i);
+
+		if (finger_state == F11_RESERVED) {
+			pr_err("%s: Invalid finger state[%d]:0x%02x.", __func__,
+					i, finger_state);
+			continue;
+		} else if ((finger_state == F11_PRESENT) ||
+				(finger_state == F11_INACCURATE)) {
+			finger_pressed_count++;
+		}
+
+		if (sensor->data.abs_pos)
+			rmi_f11_abs_pos_report(f11, sensor, finger_state, i);
+
+		if (sensor->data.rel_pos)
+			rmi_f11_rel_pos_report(sensor, i);
+	}
+	input_report_key(sensor->input, BTN_TOUCH, finger_pressed_count);
+	input_sync(sensor->input);
+}
+
+static int f11_2d_construct_data(struct f11_2d_sensor *sensor)
+{
+	struct f11_2d_sensor_query *query = &sensor->sens_query;
+	struct f11_2d_data *data = &sensor->data;
+	int i;
+
+	sensor->nbr_fingers = (query->number_of_fingers == 5 ? 10 :
+				query->number_of_fingers + 1);
+
+	sensor->pkt_size = F11_CEIL(sensor->nbr_fingers, 4);
+
+	if (query->has_abs)
+		sensor->pkt_size += (sensor->nbr_fingers * 5);
+
+	if (query->has_rel)
+		sensor->pkt_size +=  (sensor->nbr_fingers * 2);
+
+	/* Check if F11_2D_Query7 is non-zero */
+	if (query->f11_2d_query7__8[0])
+		sensor->pkt_size += sizeof(u8);
+
+	/* Check if F11_2D_Query7 or F11_2D_Query8 is non-zero */
+	if (query->f11_2d_query7__8[0] || query->f11_2d_query7__8[1])
+		sensor->pkt_size += sizeof(u8);
+
+	if (query->has_pinch || query->has_flick || query->has_rotate) {
+		sensor->pkt_size += 3;
+		if (!query->has_flick)
+			sensor->pkt_size--;
+		if (!query->has_rotate)
+			sensor->pkt_size--;
+	}
+
+	if (query->has_touch_shapes)
+		sensor->pkt_size += F11_CEIL(query->nbr_touch_shapes + 1, 8);
+
+	sensor->data_pkt = kzalloc(sensor->pkt_size, GFP_KERNEL);
+	if (!sensor->data_pkt)
+		return -ENOMEM;
+
+	data->f_state = sensor->data_pkt;
+	i = F11_CEIL(sensor->nbr_fingers, 4);
+
+	if (query->has_abs) {
+		data->abs_pos = (struct f11_2d_data_1_5 *)
+				&sensor->data_pkt[i];
+		i += (sensor->nbr_fingers * 5);
+	}
+
+	if (query->has_rel) {
+		data->rel_pos = (struct f11_2d_data_6_7 *)
+				&sensor->data_pkt[i];
+		i += (sensor->nbr_fingers * 2);
+	}
+
+	if (query->f11_2d_query7__8[0]) {
+		data->gest_1 = (struct f11_2d_data_8 *)&sensor->data_pkt[i];
+		i++;
+	}
+
+	if (query->f11_2d_query7__8[0] || query->f11_2d_query7__8[1]) {
+		data->gest_2 = (struct f11_2d_data_9 *)&sensor->data_pkt[i];
+		i++;
+	}
+
+	if (query->has_pinch) {
+		data->pinch = (struct f11_2d_data_10 *)&sensor->data_pkt[i];
+		i++;
+	}
+
+	if (query->has_flick) {
+		if (query->has_pinch) {
+			data->flick = (struct f11_2d_data_10_12 *)data->pinch;
+			i += 2;
+		} else {
+			data->flick = (struct f11_2d_data_10_12 *)
+					&sensor->data_pkt[i];
+			i += 3;
+		}
+	}
+
+	if (query->has_rotate) {
+		if (query->has_flick) {
+			data->rotate = (struct f11_2d_data_11_12 *)
+					(data->flick + 1);
+		} else {
+			data->rotate = (struct f11_2d_data_11_12 *)
+					&sensor->data_pkt[i];
+			i += 2;
+		}
+	}
+
+	if (query->has_touch_shapes)
+		data->shapes = (struct f11_2d_data_13 *)&sensor->data_pkt[i];
+
+	return 0;
+}
+
+static void f11_free_control_regs(struct f11_2d_ctrl *ctrl)
+{
+	kfree(ctrl->ctrl10);
+	kfree(ctrl->ctrl11);
+	kfree(ctrl->ctrl14);
+	kfree(ctrl->ctrl15);
+	kfree(ctrl->ctrl16);
+	kfree(ctrl->ctrl17);
+	kfree(ctrl->ctrl18_19);
+	kfree(ctrl->ctrl20_21);
+	kfree(ctrl->ctrl22_26);
+	kfree(ctrl->ctrl27);
+	kfree(ctrl->ctrl28);
+	kfree(ctrl->ctrl29_30);
+	ctrl->ctrl10 = NULL;
+	ctrl->ctrl11 = NULL;
+	ctrl->ctrl14 = NULL;
+	ctrl->ctrl15 = NULL;
+	ctrl->ctrl16 = NULL;
+	ctrl->ctrl17 = NULL;
+	ctrl->ctrl18_19 = NULL;
+	ctrl->ctrl20_21 = NULL;
+	ctrl->ctrl22_26 = NULL;
+	ctrl->ctrl27 = NULL;
+	ctrl->ctrl28 = NULL;
+	ctrl->ctrl29_30 = NULL;
+}
+
+static int f11_read_control_regs(struct rmi_device *rmi_dev,
+					   struct f11_2d_ctrl *ctrl,
+					   u16 ctrl_base_addr) {
+	u16 read_address = ctrl_base_addr;
+	int error = 0;
+
+	ctrl->ctrl0_9->address = read_address;
+	error = rmi_read_block(rmi_dev, read_address, ctrl->ctrl0_9->regs,
+		sizeof(ctrl->ctrl0_9->regs));
+	if (error < 0) {
+		dev_err(&rmi_dev->dev,
+			"Failed to read F11 ctrl0, code: %d.\n", error);
+		return error;
+	}
+	read_address = read_address + sizeof(ctrl->ctrl0_9->regs);
+
+	if (ctrl->ctrl10) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl10->reg, sizeof(union f11_2d_ctrl10));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl10, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl10);
+	}
+
+	if (ctrl->ctrl11) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl11->reg, sizeof(union f11_2d_ctrl11));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl11, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl11);
+	}
+
+	if (ctrl->ctrl14) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl14->reg, sizeof(union f11_2d_ctrl14));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl14, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl14);
+	}
+
+	if (ctrl->ctrl15) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl15->reg, sizeof(union f11_2d_ctrl15));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl15, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl15);
+	}
+
+	if (ctrl->ctrl16) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl16->reg, sizeof(union f11_2d_ctrl16));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl16, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl16);
+	}
+
+	if (ctrl->ctrl17) {
+		error = rmi_read_block(rmi_dev, read_address,
+			&ctrl->ctrl17->reg, sizeof(union f11_2d_ctrl17));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl17, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl17);
+	}
+
+	if (ctrl->ctrl18_19) {
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl18_19->reg, sizeof(union f11_2d_ctrl18_19));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl18_19, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl18_19);
+	}
+
+	if (ctrl->ctrl20_21) {
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl20_21->reg, sizeof(union f11_2d_ctrl20_21));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl20_21, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl20_21);
+	}
+
+	if (ctrl->ctrl22_26) {
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl22_26->regs, sizeof(union f11_2d_ctrl22_26));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl22_26, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl22_26);
+	}
+
+	if (ctrl->ctrl27) {
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl27->regs, sizeof(union f11_2d_ctrl27));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl27, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl27);
+	}
+
+	if (ctrl->ctrl28) {
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl28->regs, sizeof(union f11_2d_ctrl28));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl28, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(union f11_2d_ctrl28);
+	}
+
+	if (ctrl->ctrl29_30) {
+		ctrl->ctrl29_30->address = read_address;
+		error = rmi_read_block(rmi_dev, read_address,
+			ctrl->ctrl29_30->regs, sizeof(ctrl->ctrl29_30->regs));
+		if (error < 0) {
+			dev_err(&rmi_dev->dev,
+				"Failed to read F11 ctrl29_30, code: %d.\n",
+				error);
+			return error;
+		}
+		read_address = read_address + sizeof(ctrl->ctrl29_30->regs);
+	}
+	return 0;
+}
+
+static int f11_allocate_control_regs(struct rmi_device *rmi_dev,
+				struct f11_2d_device_query *device_query,
+				struct f11_2d_sensor_query *sensor_query,
+				struct f11_2d_ctrl *ctrl,
+				u16 ctrl_base_addr) {
+
+	int error = 0;
+	ctrl->ctrl0_9 = kzalloc(sizeof(union f11_2d_ctrl0_9),
+				       GFP_KERNEL);
+	if (!ctrl->ctrl0_9) {
+		error = -ENOMEM;
+		goto error_exit;
+	}
+	if (sensor_query->f11_2d_query7__8[0]) {
+		ctrl->ctrl10 = kzalloc(sizeof(union f11_2d_ctrl10),
+				       GFP_KERNEL);
+		if (!ctrl->ctrl10) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (sensor_query->f11_2d_query7__8[1]) {
+		ctrl->ctrl11 = kzalloc(sizeof(union f11_2d_ctrl11),
+				       GFP_KERNEL);
+		if (!ctrl->ctrl11) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (device_query->has_query9 && sensor_query->query9.has_pen) {
+		ctrl->ctrl20_21 = kzalloc(sizeof(union f11_2d_ctrl20_21),
+					  GFP_KERNEL);
+		if (!ctrl->ctrl20_21) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (device_query->has_query9 && sensor_query->query9.has_proximity) {
+		ctrl->ctrl22_26 = kzalloc(sizeof(union f11_2d_ctrl22_26),
+					  GFP_KERNEL);
+		if (!ctrl->ctrl22_26) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (device_query->has_query9 &&
+		(sensor_query->query9.has_palm_det_sensitivity ||
+		sensor_query->query9.has_suppress_on_palm_detect)) {
+		ctrl->ctrl27 = kzalloc(sizeof(union f11_2d_ctrl27),
+					  GFP_KERNEL);
+		if (!ctrl->ctrl27) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (sensor_query->has_multi_finger_scroll) {
+		ctrl->ctrl28 = kzalloc(sizeof(union f11_2d_ctrl28),
+					  GFP_KERNEL);
+		if (!ctrl->ctrl28) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	if (device_query->has_query11 && device_query->has_z_tuning) {
+		ctrl->ctrl29_30 = kzalloc(sizeof(union f11_2d_ctrl29_30),
+					  GFP_KERNEL);
+		if (!ctrl->ctrl29_30) {
+			error = -ENOMEM;
+			goto error_exit;
+		}
+	}
+
+	return f11_read_control_regs(rmi_dev, ctrl, ctrl_base_addr);
+
+error_exit:
+	f11_free_control_regs(ctrl);
+	return error;
+}
+
+static int f11_write_control_regs(struct rmi_device *rmi_dev,
+					struct f11_2d_sensor_query *query,
+					struct f11_2d_ctrl *ctrl,
+					u16 ctrl_base_addr)
+{
+	u16 write_address = ctrl_base_addr;
+	int error;
+
+	error = rmi_write_block(rmi_dev, write_address,
+				ctrl->ctrl0_9->regs,
+				 sizeof(ctrl->ctrl0_9->regs));
+	if (error < 0)
+		return error;
+	write_address += sizeof(ctrl->ctrl0_9);
+
+	if (ctrl->ctrl10) {
+		error = rmi_write_block(rmi_dev, write_address,
+					&ctrl->ctrl10->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl11) {
+		error = rmi_write_block(rmi_dev, write_address,
+					&ctrl->ctrl11->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl12 && ctrl->ctrl12_size && query->configurable) {
+		if (ctrl->ctrl12_size > query->max_electrodes) {
+			dev_err(&rmi_dev->dev,
+				"%s: invalid cfg size:%d, should be < %d.\n",
+				__func__, ctrl->ctrl12_size,
+				query->max_electrodes);
+			return -EINVAL;
+		}
+		error = rmi_write_block(rmi_dev, write_address,
+						&ctrl->ctrl12->reg,
+						ctrl->ctrl12_size);
+		if (error < 0)
+			return error;
+		write_address += ctrl->ctrl12_size;
+	}
+
+	if (ctrl->ctrl14) {
+		error = rmi_write_block(rmi_dev, write_address,
+				&ctrl->ctrl14->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl15) {
+		error = rmi_write_block(rmi_dev, write_address,
+				&ctrl->ctrl15->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl16) {
+		error = rmi_write_block(rmi_dev, write_address,
+				&ctrl->ctrl16->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl17) {
+		error = rmi_write_block(rmi_dev, write_address,
+				&ctrl->ctrl17->reg, 1);
+		if (error < 0)
+			return error;
+		write_address++;
+	}
+
+	if (ctrl->ctrl18_19) {
+		error = rmi_write_block(rmi_dev, write_address,
+			ctrl->ctrl18_19->reg, sizeof(union f11_2d_ctrl18_19));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl18_19);
+	}
+
+	if (ctrl->ctrl20_21) {
+		error = rmi_write_block(rmi_dev, write_address,
+					ctrl->ctrl20_21->reg,
+					sizeof(union f11_2d_ctrl20_21));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl20_21);
+	}
+
+	if (ctrl->ctrl22_26) {
+		error = rmi_write_block(rmi_dev, write_address,
+					ctrl->ctrl22_26->regs,
+					sizeof(union f11_2d_ctrl22_26));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl22_26);
+	}
+
+	if (ctrl->ctrl27) {
+		error = rmi_write_block(rmi_dev, write_address,
+					ctrl->ctrl27->regs,
+					sizeof(union f11_2d_ctrl27));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl27);
+	}
+
+	if (ctrl->ctrl28) {
+		error = rmi_write_block(rmi_dev, write_address,
+					ctrl->ctrl28->regs,
+					sizeof(union f11_2d_ctrl28));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl28);
+	}
+
+	if (ctrl->ctrl29_30) {
+		error = rmi_write_block(rmi_dev, write_address,
+					ctrl->ctrl29_30->regs,
+					sizeof(union f11_2d_ctrl29_30));
+		if (error < 0)
+			return error;
+		write_address += sizeof(union f11_2d_ctrl29_30);
+	}
+
+	return 0;
+}
+
+static int rmi_f11_get_query_parameters(struct rmi_device *rmi_dev,
+			struct f11_2d_sensor_query *query, u16 query_base_addr)
+{
+	int query_size;
+	int rc;
+
+	rc = rmi_read_block(rmi_dev, query_base_addr, query->f11_2d_query1__4,
+					sizeof(query->f11_2d_query1__4));
+	if (rc < 0)
+		return rc;
+	query_size = rc;
+
+	if (query->has_abs) {
+		rc = rmi_read(rmi_dev, query_base_addr + query_size,
+					&query->f11_2d_query5);
+		if (rc < 0)
+			return rc;
+		query_size++;
+	}
+
+	if (query->has_rel) {
+		rc = rmi_read(rmi_dev, query_base_addr + query_size,
+					&query->f11_2d_query6);
+		if (rc < 0)
+			return rc;
+		query_size++;
+	}
+
+	if (query->has_gestures) {
+		rc = rmi_read_block(rmi_dev, query_base_addr + query_size,
+					query->f11_2d_query7__8,
+					sizeof(query->f11_2d_query7__8));
+		if (rc < 0)
+			return rc;
+		query_size += sizeof(query->f11_2d_query7__8);
+	}
+
+	if (query->has_touch_shapes) {
+		rc = rmi_read(rmi_dev, query_base_addr + query_size,
+					&query->f11_2d_query10);
+		if (rc < 0)
+			return rc;
+		query_size++;
+	}
+
+	return query_size;
+}
+
+/* This operation is done in a number of places, so we have a handy routine
+ * for it.
+ */
+static void f11_set_abs_params(struct rmi_function_container *fc, int index)
+{
+	struct f11_data *f11 = fc->data;
+	struct f11_2d_sensor *sensor = &f11->sensors[index];
+	struct input_dev *input = sensor->input;
+	int device_x_max =
+		f11->dev_controls.ctrl0_9->sensor_max_x_pos;
+	int device_y_max =
+		f11->dev_controls.ctrl0_9->sensor_max_y_pos;
+	int x_min, x_max, y_min, y_max;
+	if (sensor->axis_align.swap_axes) {
+		int temp = device_x_max;
+		device_x_max = device_y_max;
+		device_y_max = temp;
+	}
+	/* Use the max X and max Y read from the device, or the clip values,
+	 * whichever is stricter.
+	 */
+	x_min = sensor->axis_align.clip_X_low;
+	if (sensor->axis_align.clip_X_high)
+		x_max = min((int) device_x_max,
+			sensor->axis_align.clip_X_high);
+	else
+		x_max = device_x_max;
+
+	y_min = sensor->axis_align.clip_Y_low;
+	if (sensor->axis_align.clip_Y_high)
+		y_max = min((int) device_y_max,
+			sensor->axis_align.clip_Y_high);
+	else
+		y_max = device_y_max;
+
+	dev_dbg(&fc->dev, "Set ranges X=[%d..%d] Y=[%d..%d].",
+			x_min, x_max, y_min, y_max);
+
+#ifdef ABS_MT_PRESSURE
+	input_set_abs_params(input, ABS_MT_PRESSURE, 0,
+			DEFAULT_MAX_ABS_MT_PRESSURE, 0, 0);
+#endif
+	input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
+			0, DEFAULT_MAX_ABS_MT_TOUCH, 0, 0);
+	input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
+			0, DEFAULT_MAX_ABS_MT_TOUCH, 0, 0);
+	input_set_abs_params(input, ABS_MT_ORIENTATION,
+			0, DEFAULT_MAX_ABS_MT_ORIENTATION, 0, 0);
+	input_set_abs_params(input, ABS_MT_TRACKING_ID,
+			DEFAULT_MIN_ABS_MT_TRACKING_ID,
+			DEFAULT_MAX_ABS_MT_TRACKING_ID, 0, 0);
+	/* TODO get max_x_pos (and y) from control registers. */
+	input_set_abs_params(input, ABS_MT_POSITION_X,
+			x_min, x_max, 0, 0);
+	input_set_abs_params(input, ABS_MT_POSITION_Y,
+			y_min, y_max, 0, 0);
+#ifdef CONFIG_RMI4_F11_TYPEB
+	if (f11->type_b)
+		input_mt_init_slots(input, sensor->nbr_fingers);
+#endif
+#ifdef	CONFIG_RMI4_F11_PEN
+	if (sensor->sens_query.query9.has_pen)
+		input_set_abs_params(input, ABS_MT_TOOL_TYPE,
+				     0, MT_TOOL_MAX, 0, 0);
+	else
+		input_set_abs_params(input, ABS_MT_TOOL_TYPE,
+				     0, MT_TOOL_MAX, 0, 0);
+#else
+	input_set_abs_params(input, ABS_MT_TOOL_TYPE, 0, MT_TOOL_FINGER, 0, 0);
+#endif
+}
+
+static int rmi_f11_init(struct rmi_function_container *fc)
+{
+	int rc;
+
+	rc = rmi_f11_initialize(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f11_register_devices(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	rc = rmi_f11_create_sysfs(fc);
+	if (rc < 0)
+		goto err_free_data;
+
+	return 0;
+
+err_free_data:
+	rmi_f11_free_memory(fc);
+
+	return rc;
+}
+
+static void rmi_f11_free_memory(struct rmi_function_container *fc)
+{
+	struct f11_data *f11 = fc->data;
+	int i;
+
+	if (f11) {
+		f11_free_control_regs(&f11->dev_controls);
+		for (i = 0; i < f11->dev_query.nbr_of_sensors + 1; i++)
+			kfree(f11->sensors[i].virtualbutton_map.map);
+		kfree(f11);
+		fc->data = NULL;
+	}
+}
+
+
+static int rmi_f11_initialize(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f11_data *f11;
+	u8 query_offset;
+	u16 query_base_addr;
+	u16 control_base_addr;
+	u16 max_x_pos, max_y_pos, temp;
+	int rc;
+	int i;
+	struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
+
+	dev_dbg(&fc->dev, "Initializing F11 values for %s.\n",
+		 pdata->sensor_name);
+
+	/*
+	** init instance data, fill in values and create any sysfs files
+	*/
+	f11 = kzalloc(sizeof(struct f11_data), GFP_KERNEL);
+	if (!f11)
+		return -ENOMEM;
+
+	fc->data = f11;
+#if	RESUME_REZERO
+	f11->rezero_on_resume = true;
+	f11->rezero_wait_ms = DEFAULT_REZERO_WAIT_MS;
+#endif
+	#ifdef CONFIG_RMI4_F11_TYPEB
+	f11->type_b = pdata->f11_type_b;
+#endif
+
+	query_base_addr = fc->fd.query_base_addr;
+	control_base_addr = fc->fd.control_base_addr;
+
+	rc = rmi_read(rmi_dev, query_base_addr, &f11->dev_query.f11_2d_query0);
+	if (rc < 0)
+		return rc;
+
+	query_offset = (query_base_addr + 1);
+	/* Increase with one since number of sensors is zero based */
+	for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
+		f11->sensors[i].sensor_index = i;
+
+		rc = rmi_f11_get_query_parameters(rmi_dev,
+					&f11->sensors[i].sens_query,
+					query_offset);
+		if (rc < 0)
+			return rc;
+		query_offset += rc;
+
+		if (f11->dev_query.has_query9) {
+			rc = rmi_read(rmi_dev, query_offset,
+				      &f11->sensors[i].sens_query.query9.reg);
+			if (rc < 0) {
+				dev_err(&fc->dev, "Failed to read query 9.\n");
+				return rc;
+			}
+			query_offset += rc;
+		}
+
+		rc = f11_allocate_control_regs(rmi_dev,
+				&f11->dev_query, &f11->sensors[i].sens_query,
+				&f11->dev_controls, control_base_addr);
+		if (rc < 0) {
+			dev_err(&fc->dev,
+				"Failed to initialize F11 control params.\n");
+			return rc;
+		}
+
+		f11->sensors[i].axis_align = pdata->axis_align;
+
+		rc = rmi_read_block(rmi_dev,
+			control_base_addr + F11_CTRL_SENSOR_MAX_X_POS_OFFSET,
+			(u8 *)&max_x_pos, sizeof(max_x_pos));
+		if (rc < 0)
+			return rc;
+
+		rc = rmi_read_block(rmi_dev,
+			control_base_addr + F11_CTRL_SENSOR_MAX_Y_POS_OFFSET,
+			(u8 *)&max_y_pos, sizeof(max_y_pos));
+		if (rc < 0)
+			return rc;
+
+		if (pdata->axis_align.swap_axes) {
+			temp = max_x_pos;
+			max_x_pos = max_y_pos;
+			max_y_pos = temp;
+		}
+		f11->sensors[i].max_x = max_x_pos;
+		f11->sensors[i].max_y = max_y_pos;
+
+		rc = f11_2d_construct_data(&f11->sensors[i]);
+		if (rc < 0)
+			return rc;
+	}
+#ifdef CONFIG_RMI4_DEBUG
+	rc = setup_debugfs(rmi_dev);
+	if (rc < 0)
+		dev_warn(&fc->dev, "Failed to setup debugfs. Code: %d.\n",
+			 rc);
+#endif
+	mutex_init(&f11->dev_controls_mutex);
+	return 0;
+}
+
+static int rmi_f11_register_devices(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f11_data *f11 = fc->data;
+	struct input_dev *input_dev;
+	struct input_dev *input_dev_mouse;
+	int sensors_itertd = 0;
+	int i;
+	int rc;
+#ifdef CONFIG_RMI4_VIRTUAL_BUTTON
+	struct rmi_button_map *vm_sensor;
+	struct rmi_button_map *vm_pdata;
+	struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
+#endif
+
+	for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
+		sensors_itertd = i;
+		input_dev = input_allocate_device();
+		if (!input_dev) {
+			rc = -ENOMEM;
+			goto error_unregister;
+		}
+
+		f11->sensors[i].input = input_dev;
+		/* TODO how to modify the dev name and
+		* phys name for input device */
+		sprintf(f11->sensors[i].input_name, "%sfn%02x",
+			dev_name(&rmi_dev->dev), fc->fd.function_number);
+		input_dev->name = f11->sensors[i].input_name;
+		sprintf(f11->sensors[i].input_phys, "%s/input0",
+			input_dev->name);
+		input_dev->phys = f11->sensors[i].input_phys;
+		input_dev->dev.parent = &rmi_dev->dev;
+		input_set_drvdata(input_dev, f11);
+
+		set_bit(EV_SYN, input_dev->evbit);
+		set_bit(EV_KEY, input_dev->evbit);
+		set_bit(EV_ABS, input_dev->evbit);
+#ifdef INPUT_PROP_DIRECT
+		set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
+#endif
+
+		f11_set_abs_params(fc, i);
+
+		dev_dbg(&fc->dev, "%s: Sensor %d hasRel %d.\n",
+			__func__, i, f11->sensors[i].sens_query.has_rel);
+		if (f11->sensors[i].sens_query.has_rel) {
+			set_bit(EV_REL, input_dev->evbit);
+			set_bit(REL_X, input_dev->relbit);
+			set_bit(REL_Y, input_dev->relbit);
+		}
+		rc = input_register_device(input_dev);
+		if (rc < 0) {
+			input_free_device(input_dev);
+			f11->sensors[i].input = NULL;
+			goto error_unregister;
+		}
+
+		/* how to register the virtualbutton device */
+#ifdef CONFIG_RMI4_VIRTUAL_BUTTON
+		if (f11->sensors[i].sens_query.has_gestures) {
+			int j;
+
+			vm_sensor = &f11->sensors[i].virtualbutton_map;
+			vm_pdata = pdata->f11_button_map;
+			if (!vm_pdata) {
+				dev_err(&fc->dev, "Failed to get the pdata virtualbutton map.\n");
+				goto error_unregister;
+			}
+			vm_sensor->buttons = vm_pdata->buttons;
+			vm_sensor->map = kcalloc(vm_pdata->buttons,
+					sizeof(struct virtualbutton_map),
+					GFP_KERNEL);
+			if (!vm_sensor->map) {
+				dev_err(&fc->dev, "Failed to allocate the virtualbutton map.\n");
+				rc = -ENOMEM;
+				goto error_unregister;
+			}
+
+			/* manage button map using input subsystem */
+			input_dev->keycode = vm_sensor->map;
+			input_dev->keycodesize =
+					sizeof(struct virtualbutton_map);
+			input_dev->keycodemax = vm_pdata->buttons;
+
+			/* set bits for each button... */
+			for (j = 0; j < vm_pdata->buttons; j++) {
+				memcpy(&vm_sensor->map[j], &vm_pdata->map[j],
+					sizeof(struct virtualbutton_map));
+				set_bit(vm_sensor->map[j].code,
+					f11->sensors[i].input->keybit);
+			}
+		}
+
+#endif
+
+		if (f11->sensors[i].sens_query.has_rel) {
+			/*create input device for mouse events  */
+			input_dev_mouse = input_allocate_device();
+			if (!input_dev_mouse) {
+				rc = -ENOMEM;
+				goto error_unregister;
+			}
+
+			f11->sensors[i].mouse_input = input_dev_mouse;
+			input_dev_mouse->name = "rmi_mouse";
+			input_dev_mouse->phys = "rmi_f11/input0";
+
+			input_dev_mouse->id.vendor  = 0x18d1;
+			input_dev_mouse->id.product = 0x0210;
+			input_dev_mouse->id.version = 0x0100;
+
+			set_bit(EV_REL, input_dev_mouse->evbit);
+			set_bit(REL_X, input_dev_mouse->relbit);
+			set_bit(REL_Y, input_dev_mouse->relbit);
+
+			set_bit(BTN_MOUSE, input_dev_mouse->evbit);
+			/* Register device's buttons and keys */
+			set_bit(EV_KEY, input_dev_mouse->evbit);
+			set_bit(BTN_LEFT, input_dev_mouse->keybit);
+			set_bit(BTN_MIDDLE, input_dev_mouse->keybit);
+			set_bit(BTN_RIGHT, input_dev_mouse->keybit);
+
+			rc = input_register_device(input_dev_mouse);
+			if (rc < 0) {
+				input_free_device(input_dev_mouse);
+				f11->sensors[i].mouse_input = NULL;
+				goto error_unregister;
+			}
+
+			set_bit(BTN_RIGHT, input_dev_mouse->keybit);
+		}
+
+	}
+
+	return 0;
+
+error_unregister:
+	for (; sensors_itertd > 0; sensors_itertd--) {
+		if (f11->sensors[sensors_itertd].input) {
+			if (f11->sensors[sensors_itertd].mouse_input) {
+				input_unregister_device(
+				   f11->sensors[sensors_itertd].mouse_input);
+				f11->sensors[sensors_itertd].mouse_input = NULL;
+			}
+			input_unregister_device(f11->sensors[i].input);
+			f11->sensors[i].input = NULL;
+		}
+		kfree(f11->sensors[i].virtualbutton_map.map);
+	}
+
+	return rc;
+}
+
+static void rmi_f11_free_devices(struct rmi_function_container *fc)
+{
+	struct f11_data *f11 = fc->data;
+	int i;
+
+	for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
+		if (f11->sensors[i].input)
+			input_unregister_device(f11->sensors[i].input);
+		if (f11->sensors[i].sens_query.has_rel &&
+				f11->sensors[i].mouse_input)
+			input_unregister_device(f11->sensors[i].mouse_input);
+	}
+}
+
+static int rmi_f11_create_sysfs(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	int rc;
+	struct f11_data *f11 = fc->data;
+
+	dev_dbg(&fc->dev, "Creating sysfs files.\n");
+	/* Set up sysfs device attributes. */
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
+		if (sysfs_create_file
+		    (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create sysfs file for %s.",
+				attrs[attr_count].attr.name);
+			rc = -ENODEV;
+			goto err_remove_sysfs;
+		}
+	}
+	if (sysfs_create_group(&fc->dev.kobj, &attrs_control0) < 0) {
+		dev_err(&fc->dev, "Failed to create query sysfs files.\n");
+		return -ENODEV;
+	}
+	if (f11->dev_controls.ctrl29_30) {
+		if (sysfs_create_group(&fc->dev.kobj,
+			&attrs_control29_30) < 0) {
+			dev_err(&fc->dev,
+				"Failed to create query sysfs files.");
+			return -ENODEV;
+		}
+	}
+
+	return 0;
+
+err_remove_sysfs:
+	for (attr_count--; attr_count >= 0; attr_count--)
+		sysfs_remove_file(&fc->dev.kobj, &attrs[attr_count].attr);
+	sysfs_remove_group(&fc->dev.kobj, &attrs_control0);
+	if (f11->dev_controls.ctrl29_30)
+		sysfs_remove_group(&fc->dev.kobj, &attrs_control29_30);
+	return rc;
+}
+
+static int rmi_f11_config(struct rmi_function_container *fc)
+{
+	struct f11_data *f11 = fc->data;
+	int i;
+	int rc;
+
+	for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
+		rc = f11_write_control_regs(fc->rmi_dev,
+				   &f11->sensors[i].sens_query,
+				   &f11->dev_controls,
+				   fc->fd.query_base_addr);
+		if (rc < 0)
+			return rc;
+	}
+
+	return 0;
+}
+
+static int rmi_f11_reset(struct rmi_function_container *fc)
+{
+	/* we do nothing here */
+	return 0;
+}
+
+int rmi_f11_attention(struct rmi_function_container *fc, u8 *irq_bits)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f11_data *f11 = fc->data;
+	u16 data_base_addr = fc->fd.data_base_addr;
+	u16 data_base_addr_offset = 0;
+	int error;
+	int i;
+
+	for (i = 0; i < f11->dev_query.nbr_of_sensors + 1; i++) {
+		error = rmi_read_block(rmi_dev,
+				data_base_addr + data_base_addr_offset,
+				f11->sensors[i].data_pkt,
+				f11->sensors[i].pkt_size);
+		if (error < 0)
+			return error;
+
+		rmi_f11_finger_handler(f11, &f11->sensors[i]);
+		rmi_f11_virtual_button_handler(&f11->sensors[i]);
+		data_base_addr_offset += f11->sensors[i].pkt_size;
+	}
+
+	return 0;
+}
+
+#if RESUME_REZERO
+static int rmi_f11_resume(struct rmi_function_container *fc)
+{
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+	struct f11_data *data = fc->data;
+	/* Command register always reads as 0, so we can just use a local. */
+	union f11_2d_commands commands = {};
+	int retval = 0;
+
+	dev_dbg(&fc->dev, "Resuming...\n");
+	if (!data->rezero_on_resume)
+		return 0;
+
+	if (data->rezero_wait_ms)
+		mdelay(data->rezero_wait_ms);
+
+	commands.rezero = 1;
+	retval = rmi_write_block(rmi_dev, fc->fd.command_base_addr,
+			&commands.reg, sizeof(commands.reg));
+	if (retval < 0) {
+		dev_err(&rmi_dev->dev, "%s: failed to issue rezero command, error = %d.",
+			__func__, retval);
+		return retval;
+	}
+
+	return retval;
+}
+#endif /* RESUME_REZERO */
+
+static void rmi_f11_remove(struct rmi_function_container *fc)
+{
+	int attr_count = 0;
+	struct f11_data *f11 = fc->data;
+	struct rmi_device *rmi_dev = fc->rmi_dev;
+#ifdef	CONFIG_RMI4_DEBUG
+	teardown_debugfs(rmi_dev);
+#endif
+	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
+		sysfs_remove_file(&fc->dev.kobj,
+				  &attrs[attr_count].attr);
+
+	sysfs_remove_group(&fc->dev.kobj, &attrs_control0);
+	if (f11->dev_controls.ctrl29_30)
+		sysfs_remove_group(&fc->dev.kobj, &attrs_control29_30);
+
+	rmi_f11_free_devices(fc);
+
+	rmi_f11_free_memory(fc);
+
+}
+
+static struct rmi_function_handler function_handler = {
+	.func = 0x11,
+	.init = rmi_f11_init,
+	.config = rmi_f11_config,
+	.reset = rmi_f11_reset,
+	.attention = rmi_f11_attention,
+	.remove = rmi_f11_remove,
+#if	RESUME_REZERO
+#if defined(CONFIG_HAS_EARLYSUSPEND) && \
+			!defined(CONFIG_RMI4_SPECIAL_EARLYSUSPEND)
+	.late_resume = rmi_f11_resume
+#else
+	.resume = rmi_f11_resume
+#endif  /* defined(CONFIG_HAS_EARLYSUSPEND) && !def... */
+#endif
+};
+
+static int __init rmi_f11_module_init(void)
+{
+	int error;
+
+	error = rmi_register_function_driver(&function_handler);
+	if (error < 0) {
+		pr_err("%s: register failed!\n", __func__);
+		return error;
+	}
+
+	return 0;
+}
+
+static void __exit rmi_f11_module_exit(void)
+{
+	rmi_unregister_function_driver(&function_handler);
+}
+
+static ssize_t f11_maxPos_show(struct device *dev,
+				     struct device_attribute *attr,
+				     char *buf)
+{
+	struct rmi_function_container *fc;
+	struct f11_data *data;
+
+	fc = to_rmi_function_container(dev);
+	data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u %u\n",
+			data->sensors[0].max_x, data->sensors[0].max_y);
+}
+
+static ssize_t f11_relreport_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct f11_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			instance_data->
+			sensors[0].axis_align.rel_report_enabled);
+}
+
+static ssize_t f11_relreport_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf,
+					 size_t count)
+{
+	struct rmi_function_container *fc;
+	struct f11_data *instance_data;
+	unsigned int new_value;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+
+	if (sscanf(buf, "%u", &new_value) != 1)
+		return -EINVAL;
+	if (new_value < 0 || new_value > 1)
+		return -EINVAL;
+	instance_data->sensors[0].axis_align.rel_report_enabled = new_value;
+
+	return count;
+}
+
+static ssize_t f11_rezero_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct rmi_function_container *fc = NULL;
+	unsigned int rezero;
+	int retval = 0;
+	/* Command register always reads as 0, so we can just use a local. */
+	union f11_2d_commands commands = {};
+
+	fc = to_rmi_function_container(dev);
+
+	if (sscanf(buf, "%u", &rezero) != 1)
+		return -EINVAL;
+	if (rezero < 0 || rezero > 1)
+		return -EINVAL;
+
+	/* Per spec, 0 has no effect, so we skip it entirely. */
+	if (rezero) {
+		commands.rezero = 1;
+		retval = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
+				&commands.reg, sizeof(commands.reg));
+		if (retval < 0) {
+			dev_err(dev, "%s: failed to issue rezero command, error = %d.",
+				__func__, retval);
+			return retval;
+		}
+	}
+
+	return count;
+}
+
+#if RESUME_REZERO
+static ssize_t f11_rezeroOnResume_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct rmi_function_container *fc = NULL;
+	unsigned int newValue;
+	struct f11_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	if (sscanf(buf, "%u", &newValue) != 1)
+		return -EINVAL;
+	if (newValue < 0 || newValue > 1) {
+		dev_err(dev, "rezeroOnResume must be either 1 or 0.\n");
+		return -EINVAL;
+	}
+
+	instance_data->rezero_on_resume = (newValue != 0);
+
+	return count;
+}
+
+static ssize_t f11_rezeroOnResume_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct f11_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			instance_data->rezero_on_resume);
+}
+
+static ssize_t f11_rezeroWait_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct rmi_function_container *fc = NULL;
+	unsigned int newValue;
+	struct f11_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	if (sscanf(buf, "%u", &newValue) != 1)
+		return -EINVAL;
+	if (newValue < 0) {
+		dev_err(dev, "rezeroWait must be 0 or greater.\n");
+		return -EINVAL;
+	}
+
+	instance_data->rezero_wait_ms = (newValue != 0);
+
+	return count;
+}
+
+static ssize_t f11_rezeroWait_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct rmi_function_container *fc;
+	struct f11_data *instance_data;
+
+	fc = to_rmi_function_container(dev);
+	instance_data = fc->data;
+
+	return snprintf(buf, PAGE_SIZE, "%u\n",
+			instance_data->rezero_wait_ms);
+}
+
+#endif
+
+/* Control sysfs files */
+show_store_union_struct_unsigned(dev_controls, ctrl0_9, abs_pos_filt)
+show_store_union_struct_unsigned(dev_controls, ctrl29_30, z_touch_threshold)
+show_store_union_struct_unsigned(dev_controls, ctrl29_30, z_touch_hysteresis)
+module_init(rmi_f11_module_init);
+module_exit(rmi_f11_module_exit);
+
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]");
+MODULE_DESCRIPTION("RMI F11 module");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
#6 lore raw

RFC PATCH 2/17 input: RMI4 core bus and sensor drivers.

In reply to Christopher Heiny

Driver for Synaptics touchscreens using RMI4 protocol.

Cc Dmitry Torokhov <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Naveen Kumar Gaddipati <[email protected]>
Cc Joeri de Gram <[email protected]>

---

 drivers/input/rmi4/rmi_bus.c    |  504 ++++++++++++
 drivers/input/rmi4/rmi_driver.c | 1716 +++++++++++++++++++++++++++++++++++++++
 drivers/input/rmi4/rmi_driver.h |  440 ++++++++++
 3 files changed, 2660 insertions(+), 0 deletions(-)
drivers/input/rmi4/rmi_bus.c new file +504 -0
new file mode 100644
index 0000000..0f29abb
@@ -0,0 +1,504 @@
+/*
+ * Copyright (c) 2011, 2012 Synaptics Incorporated
+ * Copyright (c) 2011 Unixphere
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/pm.h>
+#include <linux/slab.h>
+#include <linux/list.h>
+#include <linux/rmi.h>
+#include <linux/types.h>
+#ifdef CONFIG_RMI4_DEBUG
+#include <linux/debugfs.h>
+#endif
+#include "rmi_driver.h"
+
+DEFINE_MUTEX(rmi_bus_mutex);
+
+static struct rmi_function_list {
+	struct list_head list;
+	struct rmi_function_handler *fh;
+} rmi_supported_functions;
+
+static struct rmi_character_driver_list {
+	struct list_head list;
+	struct rmi_char_driver *cd;
+} rmi_character_drivers;
+
+static atomic_t physical_device_count/* = ATOMIC_INIT(0) ?*/;
+
+#ifdef CONFIG_RMI4_DEBUG
+static struct dentry *rmi_debugfs_root;
+#endif
+
+static int rmi_bus_match(struct device *dev, struct device_driver *driver)
+{
+	struct rmi_driver *rmi_driver;
+	struct rmi_device *rmi_dev;
+	struct rmi_device_platform_data *pdata;
+
+	rmi_driver = to_rmi_driver(driver);
+	rmi_dev = to_rmi_device(dev);
+	pdata = to_rmi_platform_data(rmi_dev);
+	dev_dbg(dev, "%s: Matching %s.\n", __func__, pdata->sensor_name);
+
+	if (!strcmp(pdata->driver_name, rmi_driver->driver.name)) {
+		rmi_dev->driver = rmi_driver;
+		dev_dbg(dev, "%s: Match %s to %s succeeded.\n", __func__,
+			pdata->driver_name, rmi_driver->driver.name);
+		return 1;
+	}
+
+	dev_vdbg(dev, "%s: Match %s to %s failed.\n", __func__,
+		pdata->driver_name, rmi_driver->driver.name);
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int rmi_bus_suspend(struct device *dev)
+{
+	struct device_driver *driver = dev->driver;
+#ifdef GENERIC_SUBSYS_PM_OPS
+	const struct dev_pm_ops *pm;
+#endif
+
+	if (!driver)
+		return 0;
+
+#ifdef GENERIC_SUBSYS_PM_OPS
+	pm = driver->pm;
+	if (pm && pm->suspend)
+		return pm->suspend(dev);
+#endif
+	if (driver->suspend)
+		return driver->suspend(dev, PMSG_SUSPEND);
+
+	return 0;
+}
+
+static int rmi_bus_resume(struct device *dev)
+{
+	struct device_driver *driver = dev->driver;
+#ifdef GENERIC_SUBSYS_PM_OPS
+	const struct dev_pm_ops *pm;
+#endif
+
+	if (!driver)
+		return 0;
+
+#ifdef GENERIC_SUBSYS_PM_OPS
+	pm = driver->pm;
+	if (pm && pm->resume)
+		return pm->resume(dev);
+#endif
+	if (driver->resume)
+		return driver->resume(dev);
+
+	return 0;
+}
+#endif
+
+static int rmi_bus_probe(struct device *dev)
+{
+	struct rmi_driver *driver;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	driver = rmi_dev->driver;
+	if (driver && driver->probe)
+		return driver->probe(rmi_dev);
+
+	return 0;
+}
+
+static int rmi_bus_remove(struct device *dev)
+{
+	struct rmi_driver *driver;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	driver = rmi_dev->driver;
+	if (driver && driver->remove)
+		return driver->remove(rmi_dev);
+
+	return 0;
+}
+
+static void rmi_bus_shutdown(struct device *dev)
+{
+	struct rmi_driver *driver;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	driver = rmi_dev->driver;
+	if (driver && driver->shutdown)
+		driver->shutdown(rmi_dev);
+}
+
+static SIMPLE_DEV_PM_OPS(rmi_bus_pm_ops,
+			 rmi_bus_suspend, rmi_bus_resume);
+
+struct bus_type rmi_bus_type = {
+	.name		= "rmi",
+	.match		= rmi_bus_match,
+	.probe		= rmi_bus_probe,
+	.remove		= rmi_bus_remove,
+	.shutdown	= rmi_bus_shutdown,
+	.pm		= &rmi_bus_pm_ops
+};
+
+static void release_rmidev_device(struct device *dev)
+{
+	device_unregister(dev);
+}
+
+int rmi_register_phys_device(struct rmi_phys_device *phys)
+{
+	struct rmi_device_platform_data *pdata = phys->dev->platform_data;
+	struct rmi_device *rmi_dev;
+
+	if (!pdata) {
+		dev_err(phys->dev, "no platform data!\n");
+		return -EINVAL;
+	}
+
+	rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
+	if (!rmi_dev)
+		return -ENOMEM;
+
+	rmi_dev->phys = phys;
+	rmi_dev->dev.bus = &rmi_bus_type;
+
+	rmi_dev->number = atomic_inc_return(&physical_device_count) - 1;
+	rmi_dev->dev.release = release_rmidev_device;
+
+	dev_set_name(&rmi_dev->dev, "sensor%02d", rmi_dev->number);
+	dev_dbg(phys->dev, "%s: Registered %s as %s.\n", __func__,
+		pdata->sensor_name, dev_name(&rmi_dev->dev));
+
+#ifdef CONFIG_RMI4_DEBUG
+	if (rmi_debugfs_root) {
+		rmi_dev->debugfs_root = debugfs_create_dir(
+			dev_name(&rmi_dev->dev), rmi_debugfs_root);
+		if (!rmi_dev->debugfs_root)
+			dev_err(&rmi_dev->dev, "Failed to create debugfs root.\n");
+	}
+#endif
+	phys->rmi_dev = rmi_dev;
+	return device_register(&rmi_dev->dev);
+}
+EXPORT_SYMBOL(rmi_register_phys_device);
+
+void rmi_unregister_phys_device(struct rmi_phys_device *phys)
+{
+	struct rmi_device *rmi_dev = phys->rmi_dev;
+
+#ifdef CONFIG_RMI4_DEBUG
+	if (rmi_dev->debugfs_root)
+		debugfs_remove(rmi_dev->debugfs_root);
+#endif
+
+	kfree(rmi_dev);
+}
+EXPORT_SYMBOL(rmi_unregister_phys_device);
+
+int rmi_register_driver(struct rmi_driver *driver)
+{
+	driver->driver.bus = &rmi_bus_type;
+	return driver_register(&driver->driver);
+}
+EXPORT_SYMBOL(rmi_register_driver);
+
+static int __rmi_driver_remove(struct device *dev, void *data)
+{
+	struct rmi_driver *driver = data;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	if (rmi_dev->driver == driver)
+		rmi_dev->driver = NULL;
+
+	return 0;
+}
+
+void rmi_unregister_driver(struct rmi_driver *driver)
+{
+	bus_for_each_dev(&rmi_bus_type, NULL, driver, __rmi_driver_remove);
+	driver_unregister(&driver->driver);
+}
+EXPORT_SYMBOL(rmi_unregister_driver);
+
+static int rmi_bus_add_function_handler(struct device *dev, void *data)
+{
+	struct rmi_driver *driver;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	driver = rmi_dev->driver;
+	if (driver && driver->fh_add)
+		driver->fh_add(rmi_dev, data);
+
+	return 0;
+}
+
+int rmi_register_function_driver(struct rmi_function_handler *fh)
+{
+	struct rmi_function_list *entry;
+	struct rmi_function_handler *fh_dup;
+	int retval = 0;
+
+	mutex_lock(&rmi_bus_mutex);
+	fh_dup = rmi_get_function_handler(fh->func);
+	if (fh_dup) {
+		pr_err("%s: function F%02x already registered!\n", __func__,
+			fh->func);
+		retval = -EINVAL;
+		goto error_exit;
+	}
+
+	entry = kzalloc(sizeof(struct rmi_function_list), GFP_KERNEL);
+	if (!entry) {
+		retval = -ENOMEM;
+		goto error_exit;
+	}
+
+	entry->fh = fh;
+	INIT_LIST_HEAD(&entry->list);
+	list_add_tail(&entry->list, &rmi_supported_functions.list);
+
+	/* notify devices of the new function handler */
+	bus_for_each_dev(&rmi_bus_type, NULL, fh, rmi_bus_add_function_handler);
+
+error_exit:
+	mutex_unlock(&rmi_bus_mutex);
+	return retval;
+}
+EXPORT_SYMBOL(rmi_register_function_driver);
+
+static int __rmi_bus_fh_remove(struct device *dev, void *data)
+{
+	struct rmi_driver *driver;
+	struct rmi_device *rmi_dev = to_rmi_device(dev);
+
+	driver = rmi_dev->driver;
+	if (driver && driver->fh_remove)
+		driver->fh_remove(rmi_dev, data);
+
+	return 0;
+}
+
+void rmi_unregister_function_driver(struct rmi_function_handler *fh)
+{
+	struct rmi_function_list *entry, *n;
+
+	/* notify devices of the removal of the function handler */
+	bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_remove);
+
+	if (list_empty(&rmi_supported_functions.list))
+		return;
+
+	list_for_each_entry_safe(entry, n, &rmi_supported_functions.list,
+									list) {
+		if (entry->fh->func == fh->func) {
+			list_del(&entry->list);
+			kfree(entry);
+		}
+	}
+
+}
+EXPORT_SYMBOL(rmi_unregister_function_driver);
+
+struct rmi_function_handler *rmi_get_function_handler(int id)
+{
+	struct rmi_function_list *entry;
+
+	if (list_empty(&rmi_supported_functions.list))
+		return NULL;
+
+	list_for_each_entry(entry, &rmi_supported_functions.list, list)
+		if (entry->fh->func == id)
+			return entry->fh;
+
+	return NULL;
+}
+EXPORT_SYMBOL(rmi_get_function_handler);
+
+static void rmi_release_character_device(struct device *dev)
+{
+	dev_dbg(dev, "%s: Called.\n", __func__);
+	return;
+}
+
+static int rmi_register_character_device(struct device *dev, void *data)
+{
+	struct rmi_device *rmi_dev;
+	struct rmi_char_driver *char_driver = data;
+	struct rmi_char_device *char_dev;
+	int retval;
+
+	dev_dbg(dev, "Attaching character device.\n");
+	rmi_dev = to_rmi_device(dev);
+	if (char_driver->match && !char_driver->match(rmi_dev))
+		return 0;
+
+	if (!char_driver->init) {
+		dev_err(dev, "ERROR: No init() function in %s.\n", __func__);
+		return -EINVAL;
+	}
+
+	char_dev = kzalloc(sizeof(struct rmi_char_device), GFP_KERNEL);
+	if (!char_dev)
+		return -ENOMEM;
+
+	char_dev->rmi_dev = rmi_dev;
+	char_dev->driver = char_driver;
+
+	char_dev->dev.parent = dev;
+	char_dev->dev.release = rmi_release_character_device;
+	char_dev->dev.driver = &char_driver->driver;
+	retval = device_register(&char_dev->dev);
+	if (!retval) {
+		dev_err(dev, "Failed to register character device.\n");
+		goto error_exit;
+	}
+
+	retval = char_driver->init(char_dev);
+	if (retval) {
+		dev_err(dev, "Failed to initialize character device.\n");
+		goto error_exit;
+	}
+
+	mutex_lock(&rmi_bus_mutex);
+	list_add_tail(&char_dev->list, &char_driver->devices);
+	mutex_unlock(&rmi_bus_mutex);
+	dev_info(&char_dev->dev, "Registered a device.\n");
+	return retval;
+
+error_exit:
+	kfree(char_dev);
+	return retval;
+}
+
+int rmi_register_character_driver(struct rmi_char_driver *char_driver)
+{
+	struct rmi_character_driver_list *entry;
+	int retval;
+
+	pr_debug("%s: Registering character driver %s.\n", __func__,
+		char_driver->driver.name);
+
+	char_driver->driver.bus = &rmi_bus_type;
+	INIT_LIST_HEAD(&char_driver->devices);
+	retval = driver_register(&char_driver->driver);
+	if (retval) {
+		pr_err("%s: Failed to register %s, code: %d.\n", __func__,
+		       char_driver->driver.name, retval);
+		return retval;
+	}
+
+	entry = kzalloc(sizeof(struct rmi_character_driver_list), GFP_KERNEL);
+	if (!entry)
+		return -ENOMEM;
+	entry->cd = char_driver;
+
+	mutex_lock(&rmi_bus_mutex);
+	list_add_tail(&entry->list, &rmi_character_drivers.list);
+	mutex_unlock(&rmi_bus_mutex);
+
+	/* notify devices of the removal of the function handler */
+	bus_for_each_dev(&rmi_bus_type, NULL, char_driver,
+			 rmi_register_character_device);
+
+	return 0;
+}
+EXPORT_SYMBOL(rmi_register_character_driver);
+
+
+int rmi_unregister_character_driver(struct rmi_char_driver *char_driver)
+{
+	struct rmi_character_driver_list *entry, *n;
+	struct rmi_char_device *char_dev, *m;
+	pr_debug("%s: Unregistering character driver %s.\n", __func__,
+		char_driver->driver.name);
+
+	mutex_lock(&rmi_bus_mutex);
+	list_for_each_entry_safe(char_dev, m, &char_driver->devices,
+				 list) {
+		list_del(&char_dev->list);
+		char_dev->driver->remove(char_dev);
+	}
+	list_for_each_entry_safe(entry, n, &rmi_character_drivers.list,
+				 list) {
+		if (entry->cd == char_driver) {
+			list_del(&entry->list);
+			kfree(entry);
+		}
+	}
+	mutex_unlock(&rmi_bus_mutex);
+
+	driver_unregister(&char_driver->driver);
+
+	return 0;
+}
+EXPORT_SYMBOL(rmi_unregister_character_driver);
+
+static int __init rmi_bus_init(void)
+{
+	int error;
+
+	mutex_init(&rmi_bus_mutex);
+	INIT_LIST_HEAD(&rmi_supported_functions.list);
+	INIT_LIST_HEAD(&rmi_character_drivers.list);
+
+#ifdef CONFIG_RMI4_DEBUG
+	rmi_debugfs_root = debugfs_create_dir(rmi_bus_type.name, NULL);
+	if (!rmi_debugfs_root)
+		pr_err("%s: Failed to create debugfs root.\n", __func__);
+	else if (IS_ERR(rmi_debugfs_root)) {
+		pr_err("%s: Kernel may not contain debugfs support, code=%ld\n",
+		       __func__, PTR_ERR(rmi_debugfs_root));
+		rmi_debugfs_root = NULL;
+	}
+#endif
+
+	error = bus_register(&rmi_bus_type);
+	if (error < 0) {
+		pr_err("%s: error registering the RMI bus: %d\n", __func__,
+		       error);
+		return error;
+	}
+	pr_debug("%s: successfully registered RMI bus.\n", __func__);
+
+	return 0;
+}
+
+static void __exit rmi_bus_exit(void)
+{
+	/* We should only ever get here if all drivers are unloaded, so
+	 * all we have to do at this point is unregister ourselves.
+	 */
+#ifdef CONFIG_RMI4_DEBUG
+	if (rmi_debugfs_root)
+		debugfs_remove(rmi_debugfs_root);
+#endif
+	bus_unregister(&rmi_bus_type);
+}
+
+module_init(rmi_bus_init);
+module_exit(rmi_bus_exit);
+
+MODULE_AUTHOR("Christopher Heiny <[email protected]");
+MODULE_DESCRIPTION("RMI bus");
+MODULE_LICENSE("GPL");
+MODULE_VERSION(RMI_DRIVER_VERSION);
drivers/input/rmi4/rmi_driver.c new file +1716 -0

1,716 lines not on this page · raw message

drivers/input/rmi4/rmi_driver.h new file +440 -0

440 lines not on this page · raw message

#39 lore raw
In reply to Christopher Heiny
Quoted 2 lines · Christopher Heiny

This patch implements a driver supporting Synaptics ClearPad and other touchscreen sensors that use the RMI4 protocol, as defined here:

Nice!

Quoted 3 lines

This patch is against the v2.6.38 tag of Linus' kernel tree, commit 521cb40b0c44418a4fd36dc633f575813d59a43d. This will be our last patch against such an old kernel version, future patches will be against 3.x kernels.

Please use the head of the subsystem tree to do this work. In this case, use Dmitry's git.

Currently I just cannot test this because we have no such old codebase around.

But I will attempt to review the patch set!

Yours,
Linus Walleij

#2 lore raw

Re: [RFC PATCH 1/17] input: RMI4 public header file and documentation.

In reply to Christopher Heiny

On Sat, Aug 18, 2012 at 12:17 AM, Christopher Heiny <[email protected]> wrote:

(...)

include/linux/rmi.h line 39

Quoted 3 lines
include/linux/rmi.h new file +3 -0
+#ifdef CONFIG_HAS_EARLYSUSPEND
+#include <linux/earlysuspend.h>
+#endif

This does not exist in the mainline kernel, I can see you're having fun with Android ... check with Rafael on how to really handle this in a contemporary kernel.

Quoted 5 lines

+/**
+ * struct rmi_f11_axis_alignment - target axis alignment
+ * @swap_axes: set to TRUE if desired to swap x- and y-axis
+ * @flip_x: set to TRUE if desired to flip direction on x-axis
+ * @flip_y: set to TRUE if desired to flip direction on y-axis

This kerneldoc is quite incomplete. There is a *lot* of members below that are not documented.

Quoted 22 lines

+ */

include/linux/rmi.h new file +21 -0
+struct rmi_f11_2d_axis_alignment {
+       bool swap_axes;
+       bool flip_x;
+       bool flip_y;
+       int clip_X_low;
+       int clip_Y_low;
+       int clip_X_high;
+       int clip_Y_high;
+       int offset_X;
+       int offset_Y;
+       int rel_report_enabled;
+
+#ifdef CONFIG_RMI4_DEBUG
+       struct dentry *debugfs_flip;
+       struct dentry *debugfs_clip;
+       struct dentry *debugfs_offset;
+       struct dentry *debugfs_swap;
+       u16 reg_debug_addr;
+       u8 reg_debug_size;
+#endif
+};

(...)

include/linux/rmi.h line 123

Quoted 4 lines
include/linux/rmi.h new file +4 -0
+struct rmi_f11_virtualbutton_map {
+       u8 buttons;
+       struct virtualbutton_map *map;
+};

<newline> (nitpick)

Quoted 25 lines
include/linux/rmi.h new file +25 -0
+struct rmi_device_platform_data_spi {
+       int block_delay_us;
+       int split_read_block_delay_us;
+       int read_delay_us;
+       int write_delay_us;
+       int split_read_byte_delay_us;
+       int pre_delay_us;
+       int post_delay_us;
+
+       void *cs_assert_data;
+       int (*cs_assert) (const void *cs_assert_data, const bool assert);
+};
+
+/**
+ * struct rmi_device_platform_data - system specific configuration info.
+ *
+ * @firmware_name - if specified will override default firmware name,
+ * for reflashing.
+ *
+ * @f11_type_b - Force F11 to treat the sensor as a multitouch Type B sensor.
+ * This is useful for older Type B RMI4 sensors that can't be queried for this
+ * information, but will cause problems if you force a Type A sensor to be
+ * treated as type_b.
+ *
+ * @pre_suspend - this will be called before any other suspend operations are

include/linux/rmi.h line 158

Quoted 10 lines
include/linux/rmi.h new file +10 -0
+ * done.
+ * @post_suspend - this will be called after all suspend operations are
+ * completed.  This is the ONLY safe place to power off an RMI sensor
+ * during the suspend process.
+ * @pre_resume - this is called before any other resume operations.  If you
+ * powered off the RMI4 sensor in post_suspend(), then you MUST power it back
+ * here, and you MUST wait an appropriate time for the ASIC to come up
+ * (100ms to 200ms, depending on the sensor) before returning.
+ * @pm_data - this will be passed to the various (pre|post)_(suspend/resume)
+ * functions.

This kerneldoc is incomplete, maybe intentional, I don't know...

(...)

include/linux/rmi.h line 248

Quoted 11 lines
include/linux/rmi.h new file +11 -0
+struct rmi_function_handler {
+       int func;
+       int (*init)(struct rmi_function_container *fc);
+       int (*config)(struct rmi_function_container *fc);
+       int (*reset)(struct rmi_function_container *fc);
+       int (*attention)(struct rmi_function_container *fc, u8 *irq_bits);
+#ifdef CONFIG_PM
+       int (*suspend)(struct rmi_function_container *fc);
+       int (*resume)(struct rmi_function_container *fc);
+#if defined(CONFIG_HAS_EARLYSUSPEND) && \
+               !defined(CONFIG_RMI4_SPECIAL_EARLYSUSPEND)

There is this earlysuspend again, now with some turbo-version which is "special" as well :-)

Quoted 6 lines
include/linux/rmi.h new file +6 -0
+       int (*early_suspend)(struct rmi_function_container *fc);
+       int (*late_resume)(struct rmi_function_container *fc);
+#endif
+#endif
+       void (*remove)(struct rmi_function_container *fc);
+};

(...)

Quoted 12 lines
include/linux/rmi.h new file +12 -0
+/**
+ * struct rmi_device - represents an RMI device
+ * @dev: The device created for the RMI bus
+ * @number: Unique number for the device on the bus.
+ * @driver: Pointer to associated driver
+ * @phys: Pointer to the physical interface
+ * @early_suspend_handler: Pointers to early_suspend and late_resume, if
+ * configured.
+ *
+ * This structs represent an RMI device.
+ *
+ */

include/linux/rmi.h line 403

Quoted 10 lines
include/linux/rmi.h new file +10 -0
+struct rmi_device {
+       struct device dev;
+       int number;
+
+       struct rmi_driver *driver;
+       struct rmi_phys_device *phys;
+
+#ifdef CONFIG_HAS_EARLYSUSPEND
+       struct early_suspend early_suspend_handler;
+#endif

...and here...

Quoted 4 lines
include/linux/rmi.h new file +4 -0
+#ifdef CONFIG_RMI4_DEBUG
+       struct dentry *debugfs_root;
+#endif
+};

(...)

include/linux/rmi.h line 600

Quoted 7 lines
include/linux/rmi.h new file +7 -0
+/* Helper fn to convert a byte array representing a short in the RMI
+ * endian-ness to a short in the native processor's specific endianness.
+ * We don't use ntohs/htons here because, well, we're not dealing with
+ * a pair of shorts. And casting dest to short* wouldn't work, because
+ * that would imply knowing the byte order of short in the first place.
+ */
+static inline void batohs(unsigned short *dest, unsigned char *src)

Change this prototype to use kernel-internal types, and what is wron with using the return value??

static inline void batohs(unsigned short *dest, unsigned char *src)

include/linux/rmi.h line 603

Quoted 3 lines
include/linux/rmi.h new file +3 -0
+{
+       *dest = src[1] * 0x100 + src[0];
+}

Doing this instead of casting is good style. But I would have done like this:

static inline u16 batohs(u8 *src)
{
return src[1] << 8 | src[0];
}

Which the compiler *hopufully will emit, but since we're doing arithmetic, not maths, please use shifts and OR:s.

And at the call sites, instead of

unsigned short mr16;
unsigned char mr_arrary[2];
batohs(&mr16, &mr_array[0]);

Just:

u16 mr16;
u8 mr_array[2];

mr16 = batohs(&mr_array[0]);

include/linux/rmi.h line 609

Quoted 5 lines
include/linux/rmi.h new file +5 -0
+/* Helper function to convert a short (in host processor endianess) to
+ * a byte array in the RMI endianess for shorts.  See above comment for
+ * why we dont us htons or something like that.
+ */
+static inline void hstoba(unsigned char *dest, unsigned short src)

Here yuou need to pass a pointer to the destination for sure.

include/linux/rmi.h line 613

Quoted 4 lines
include/linux/rmi.h new file +4 -0
+{
+       dest[0] = src % 0x100;
+       dest[1] = src / 0x100;
+}

I would have done the above like this:

static inline void hstoba(u8 *dest, u16 src)
{
dest[0] = src & 0xFF;
dest[1] = src >> 8;
}

(...)

Quoted 1 line

+/* utility function for bit access of u8*'s */

No, do not reinvent the wheel. Use
#include <linux/bitmap.h>
(which in turn will include <linux/bitops.h> for you) Those bitmaps use unsigned int but that does not really matter, they should work just fine using the lower 8 bits.

Quoted 1 line

+void u8_set_bit(u8 *target, int pos);

Use set_bit() from <linux/bitops.h>

Quoted 1 line

+void u8_clear_bit(u8 *target, int pos);

Use clear_bit() from <linux/bitops.h>

Quoted 1 line

+bool u8_is_set(u8 *target, int pos);

Use test_bit() from <linux/bitops.h>

Quoted 1 line

+bool u8_is_any_set(u8 *target, int size);

Use find_first_bit(), if this returns 0 none is set, !find_first_bit() for example serves the same purpose.

Quoted 1 line

+void u8_or(u8 *dest, u8* target1, u8* target2, int size);

Use bitmap_or()

Quoted 1 line

+void u8_and(u8 *dest, u8* target1, u8* target2, int size);

Use bitmap_and()

Yours,
Linus Walleij

#40 lore raw
In reply to Linus Walleij
Quoted 12 lines · Linus Walleij
Quoted 2 lines · Christopher Heiny

This patch implements a driver supporting Synaptics ClearPad and other touchscreen sensors that use the RMI4 protocol, as defined here:

Nice!

Quoted 3 lines

This patch is against the v2.6.38 tag of Linus' kernel tree, commit 521cb40b0c44418a4fd36dc633f575813d59a43d. This will be our last patch against such an old kernel version, future patches will be against 3.x kernels.

Quoted 5 lines

Please use the head of the subsystem tree to do this work. In this case, use Dmitry's git.

Currently I just cannot test this because we have no such old codebase around.

Yeah, we've been in a bind in that regard, mostly due to customer requirements to support Android Gingerbread. At least we were able to drop Froyo support this time around. :-P

Going forward, though, we'll be tracking much newer kernels for our development, and hopefully getting patches out more frequently as well.

Quoted 1 line

But I will attempt to review the patch set!

Thanks! We look forward to your input.

Chris

maintainer
#3 lore raw

Re: [RFC PATCH 1/17] input: RMI4 public header file and documentation.

In reply to Linus Walleij
Quoted 5 lines · Linus Walleij
Quoted 2 lines · Christopher Heiny

+/* Helper fn to convert a byte array representing a short in the RMI
+ * endian-ness to a short in the native processor's specific endianness.

Is RMI-endiannes different from BE and LE (joking)? Please simply state what on-wire endianness is and use xx_to_cpu() or cpu_to_xx() as needed. I think I mentioned it about a year ago already...

Thanks.

Signature · 1 line
Dmitry
#5 lore raw

Re: [RFC PATCH 1/17] input: RMI4 public header file and documentation.

In reply to Linus Walleij
Quoted 11 lines · Linus Walleij

On Sat, Aug 18, 2012 at 12:17 AM, Christopher Heiny <[email protected]> wrote:

(...)

include/linux/rmi.h line 39

Quoted 3 lines
include/linux/rmi.h new file +3 -0
+#ifdef CONFIG_HAS_EARLYSUSPEND
+#include <linux/earlysuspend.h>
+#endif

This does not exist in the mainline kernel, I can see you're having fun with Android ... check with Rafael on how to really handle this in a contemporary kernel.

You can't. Early suspend is not going to be merged.

Thanks,
Rafael

#4 lore raw

Re: [RFC PATCH 1/17] input: RMI4 public header file and documentation.

In reply to Dmitry Torokhov
Quoted 9 lines · Dmitry Torokhov
Quoted 5 lines · Linus Walleij
Quoted 5 lines
Quoted 2 lines · Christopher Heiny
Quoted 2 lines

+/* Helper fn to convert a byte array representing a short in the RMI
+ * endian-ness to a short in the native processor's specific endianness.

Is RMI-endiannes different from BE and LE (joking)? Please simply state what on-wire endianness is and use xx_to_cpu() or cpu_to_xx() as needed. I think I mentioned it about a year ago already...

Gronk. I thought I'd killed that. But obviously didn't do a thorough job.

[Slinks off in embarrassment.]