All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Add driver for Domintech DMARD06 accelerometer
@ 2016-07-18 12:48 Aleksei Mamlin
  2016-07-18 12:48 ` [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd Aleksei Mamlin
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-18 12:48 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, devicetree, linux-kernel, Aleksei Mamlin

This patch series adds driver for Domintech DMARD06 Low-G Tri-Axial
digital accelerometer and vendor prefix for Domintech Co., Ltd.

Aleksei Mamlin (2):
  devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
  input: misc: Add support for Domintech DMARD06 accelerometer

 .../devicetree/bindings/input/dmard06.txt          |  24 ++
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/input/misc/Kconfig                         |  12 +
 drivers/input/misc/Makefile                        |   1 +
 drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
 5 files changed, 480 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
 create mode 100644 drivers/input/misc/dmard06.c

-- 
2.7.3

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
  2016-07-18 12:48 [PATCH 0/2] Add driver for Domintech DMARD06 accelerometer Aleksei Mamlin
@ 2016-07-18 12:48 ` Aleksei Mamlin
  2016-07-20  1:09   ` Rob Herring
  2016-07-18 12:48   ` Aleksei Mamlin
  2016-07-21 20:32   ` Aleksei Mamlin
  2 siblings, 1 reply; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-18 12:48 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, devicetree, linux-kernel, Aleksei Mamlin

Add vendor-prefix for Domintech Co., Ltd.

Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index d2bce22..0c12f2d 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -75,6 +75,7 @@ digilent	Diglent, Inc.
 dlg	Dialog Semiconductor
 dlink	D-Link Corporation
 dmo	Data Modul AG
+domintech	Domintech Co., Ltd.
 dptechnics	DPTechnics
 dragino	Dragino Technology Co., Limited
 ea	Embedded Artists AB
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer
@ 2016-07-18 12:48   ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-18 12:48 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, devicetree, linux-kernel, Aleksei Mamlin

This patch add support for Domintech DMARD06 accelerometer.

Domintech DMARD06 is a low-g tri-axial digital accelerometer with
special power saving modes suitable for consumer mobile application.

Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
---
 .../devicetree/bindings/input/dmard06.txt          |  24 ++
 drivers/input/misc/Kconfig                         |  12 +
 drivers/input/misc/Makefile                        |   1 +
 drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
 4 files changed, 479 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
 create mode 100644 drivers/input/misc/dmard06.c

diff --git a/Documentation/devicetree/bindings/input/dmard06.txt b/Documentation/devicetree/bindings/input/dmard06.txt
new file mode 100644
index 0000000..528c318
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/dmard06.txt
@@ -0,0 +1,24 @@
+Device tree bindings for Domintech DMARD06 acceletometer
+
+Required properties:
+ - compatible		: Should be "domintech,dmard06"
+ - reg			: I2C address of the chip. Should be 0x1c
+
+Optional properties:
+ - interrupt-parent	: Interrupt controller to which the chip is connected
+ - interrupts		: Interrupt to which the chip is connected
+
+
+Example:
+	&i2c1 {
+		/* ... */
+
+		accelerometer@1c {
+			compatible = "domintech,dmard06";
+			reg = <0x1c>;
+			interrupt-parent = <&gpio>;
+			interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
+		};
+
+		/* ... */
+	};
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index efb0ca8..b95e0e7 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -107,6 +107,18 @@ config INPUT_BMA150
 	  To compile this driver as a module, choose M here: the
 	  module will be called bma150.
 
+config INPUT_DMARD06
+	tristate "Domintech DMARD06 Digital Accelerometer support"
+	depends on OF || COMPILE_TEST
+	depends on I2C
+	select INPUT_POLLDEV
+	help
+	  Say Y to enable support for the Domintech DMARD06 Low-G Tri-Axial
+	  digital accelerometer connected via an I2C bus.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called dmard06.
+
 config INPUT_E3X0_BUTTON
 	tristate "NI Ettus Research USRP E3xx Button support."
 	default n
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 6a1e5e2..d83866f 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_DA9052_ONKEY)	+= da9052_onkey.o
 obj-$(CONFIG_INPUT_DA9055_ONKEY)	+= da9055_onkey.o
 obj-$(CONFIG_INPUT_DA9063_ONKEY)	+= da9063_onkey.o
 obj-$(CONFIG_INPUT_DM355EVM)		+= dm355evm_keys.o
+obj-$(CONFIG_INPUT_DMARD06)		+= dmard06.o
 obj-$(CONFIG_INPUT_E3X0_BUTTON)		+= e3x0-button.o
 obj-$(CONFIG_INPUT_DRV260X_HAPTICS)	+= drv260x.o
 obj-$(CONFIG_INPUT_DRV2665_HAPTICS)	+= drv2665.o
diff --git a/drivers/input/misc/dmard06.c b/drivers/input/misc/dmard06.c
new file mode 100644
index 0000000..cc1e5a2
--- /dev/null
+++ b/drivers/input/misc/dmard06.c
@@ -0,0 +1,442 @@
+/*
+ *  Driver for Domintech DMARD06 accelerometer
+ *
+ *  Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
+ *  Copyright (c) 2012 Domintech Technology Co., Ltd
+ *
+ * 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; version 2 of the License.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+
+/* Device data registers */
+#define DMARD06_CHIP_ID_REG		0x0f
+#define DMARD06_TOUT_REG		0x40
+#define DMARD06_XOUT_REG		0x41
+#define DMARD06_YOUT_REG		0x42
+#define DMARD06_ZOUT_REG		0x43
+
+/* Device control registers */
+#define DMARD06_CTRL_1_REG		0x44
+#define DMARD06_CTRL_2_REG		0x45
+#define DMARD06_CTRL_3_REG		0x46
+#define DMARD06_CTRL_4_REG		0x47
+#define DMARD06_CTRL_5_REG		0x48
+#define DMARD06_STATUS_REG		0x49
+#define DMARD06_INT_CFG_REG		0x4a
+#define DMARD06_FILTER_RESET_REG	0x52
+#define DMARD06_CHIP_RESET_REG		0x53
+
+/* Device ID value */
+#define DMARD06_CHIP_ID			0x06
+
+/* Device max value */
+#define DMARD06_MAX_VALUE		0x3f
+
+/* Each axis is represented by a 1-byte data word */
+#define DMARD06_XYZ_DATA_SIZE		3
+
+/* Interrupt sources */
+#define DMARD06_INT_SRC1		0x04
+#define DMARD06_INT_SRC_BOTH		0x08
+#define DMARD06_INT_MOV_DETECT		0x40
+#define DMARD06_INT_POS_DETECT		0xc0
+
+/* Device modes */
+#define DMARD06_MODE_NORMAL		0x27
+#define DMARD06_MODE_POWERDOWN		0x00
+
+/* Input poll interval in milliseconds */
+#define DMARD06_POLL_INTERVAL		10
+#define DMARD06_POLL_MAX		100
+#define DMARD06_POLL_MIN		0
+
+struct dmard06_data {
+	struct i2c_client *client;
+	struct input_dev *input;
+	struct input_polled_dev *input_poll;
+	unsigned long irq_flags;
+	int range;
+	u8 chip_id;
+	u8 mode;
+};
+
+static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
+{
+	int error;
+
+	error = i2c_master_send(client, &reg, 1);
+	if (unlikely(error < 0))
+		return error;
+
+	error = i2c_master_recv(client, buf, 1);
+	if (unlikely(error < 0))
+		return error;
+
+	return 0;
+}
+
+static int dmard06_i2c_write(struct i2c_client *client, u8 reg, u8 val)
+{
+	int error;
+	u8 buf[2] = {reg, val};
+
+	error = i2c_master_send(client, buf, 2);
+	if (unlikely(error < 0))
+		return error;
+
+	return 0;
+}
+
+static int dmard06_reset(struct i2c_client *client)
+{
+	u8 buf;
+	int error;
+
+	error = dmard06_i2c_read(client, DMARD06_CHIP_RESET_REG, &buf);
+	if (error) {
+		dev_err(&client->dev, "Failed to reset controller: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
+static int dmard06_set_mode(struct dmard06_data *dmard06, u8 mode)
+{
+	int error;
+
+	error = dmard06_i2c_write(dmard06->client, DMARD06_CTRL_1_REG, mode);
+	if (error) {
+		dev_err(&dmard06->client->dev, "Failed to set mode: %d",
+			error);
+		return error;
+	}
+
+	dmard06->mode = mode;
+
+	return 0;
+}
+
+static int dmard06_init_irq(struct i2c_client *client)
+{
+	int error;
+
+	error = dmard06_i2c_write(client, DMARD06_CTRL_4_REG,
+				  DMARD06_INT_SRC1);
+	if (error) {
+		dev_err(&client->dev, "Failed to set interrupt source: %d",
+			error);
+		return error;
+	}
+
+	error = dmard06_i2c_write(client, DMARD06_INT_CFG_REG,
+				  DMARD06_INT_POS_DETECT);
+	if (error) {
+		dev_err(&client->dev, "Failed to set interrupt event: %d",
+			error);
+		return error;
+	}
+
+	return 0;
+}
+
+static int dmard06_open(struct dmard06_data *dmard06)
+{
+	int error;
+
+	if (dmard06->mode != DMARD06_MODE_NORMAL) {
+		error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static void dmard06_close(struct dmard06_data *dmard06)
+{
+	if (dmard06->mode != DMARD06_MODE_POWERDOWN)
+		dmard06_set_mode(dmard06, DMARD06_MODE_POWERDOWN);
+}
+
+static int dmard06_irq_open(struct input_dev *input)
+{
+	struct dmard06_data *dmard06 = input_get_drvdata(input);
+
+	return dmard06_open(dmard06);
+}
+
+static void dmard06_irq_close(struct input_dev *input)
+{
+	struct dmard06_data *dmard06 = input_get_drvdata(input);
+
+	dmard06_close(dmard06);
+}
+
+static void dmard06_poll_open(struct input_polled_dev *input_poll)
+{
+	struct dmard06_data *dmard06 = input_poll->private;
+
+	dmard06_open(dmard06);
+}
+
+static void dmard06_poll_close(struct input_polled_dev *input_poll)
+{
+	struct dmard06_data *dmard06 = input_poll->private;
+
+	dmard06_close(dmard06);
+}
+
+static int dmard06_read_chip_id(struct dmard06_data *dmard06)
+{
+	u8 buf;
+	u8 chip_id;
+	int error;
+
+	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
+	if (error) {
+		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
+			error);
+		return error;
+	}
+
+	chip_id = (buf & 0x00ff);
+	if (chip_id != DMARD06_CHIP_ID) {
+		dev_err(&dmard06->client->dev, "Unknown chip id: %02x\n",
+			chip_id);
+		return -EINVAL;
+	}
+
+	dmard06->chip_id = chip_id;
+	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
+
+	return 0;
+}
+
+static void dmard06_report_xyz(struct dmard06_data *dmard06)
+{
+	u8 axis[DMARD06_XYZ_DATA_SIZE];
+	bool reversed;
+	int xyz[3];
+	int error;
+	int i;
+
+	error = i2c_smbus_read_i2c_block_data(dmard06->client,
+					      DMARD06_XOUT_REG,
+					      DMARD06_XYZ_DATA_SIZE, axis);
+	if (error != DMARD06_XYZ_DATA_SIZE)
+		return;
+
+	for (i = 0; i < DMARD06_XYZ_DATA_SIZE; i++) {
+		reversed = (axis[i] >> 7) & 1;
+		xyz[i] = (int)((axis[i] & 0x7f) >> 1);
+		if (reversed)
+			xyz[i] = xyz[i] - dmard06->range;
+	}
+
+	input_report_abs(dmard06->input, ABS_X, xyz[0]);
+	input_report_abs(dmard06->input, ABS_Y, xyz[1]);
+	input_report_abs(dmard06->input, ABS_Z, xyz[2]);
+	input_sync(dmard06->input);
+}
+
+static irqreturn_t dmard06_irq_handler(int irq, void *dev_id)
+{
+	struct dmard06_data *dmard06 = dev_id;
+
+	dmard06_report_xyz(dmard06);
+
+	return IRQ_HANDLED;
+}
+
+static void dmard06_poll(struct input_polled_dev *input_poll)
+{
+	dmard06_report_xyz(input_poll->private);
+}
+
+static void dmard06_init_input_device(struct dmard06_data *dmard06,
+				      struct input_dev *input)
+{
+	int range;
+
+	input->name = "dmard06";
+	input->id.bustype = BUS_I2C;
+	input->id.version = dmard06->chip_id;
+	input->dev.parent = &dmard06->client->dev;
+
+	set_bit(EV_ABS, input->evbit);
+
+	range = DMARD06_MAX_VALUE;
+	input_set_abs_params(input, ABS_X, -range, range, 0, 0);
+	input_set_abs_params(input, ABS_Y, -range, range, 0, 0);
+	input_set_abs_params(input, ABS_Z, -range, range, 0, 0);
+
+	dmard06->range = range;
+}
+
+static int dmard06_request_input_device(struct dmard06_data *dmard06)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&dmard06->client->dev);
+	if (!input) {
+		dev_err(&dmard06->client->dev,
+			"Failed to allocate input device\n");
+		return -ENOMEM;
+	}
+
+	dmard06_init_input_device(dmard06, input);
+
+	input->open = dmard06_irq_open;
+	input->close = dmard06_irq_close;
+	input_set_drvdata(input, dmard06);
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&dmard06->client->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	dmard06->input = input;
+
+	return 0;
+}
+
+static int dmard06_request_polled_device(struct dmard06_data *dmard06)
+{
+	struct input_polled_dev *input_poll;
+	int error;
+
+	input_poll = devm_input_allocate_polled_device(&dmard06->client->dev);
+	if (!input_poll) {
+		dev_err(&dmard06->client->dev,
+			"Failed to allocate polled input device\n");
+		return -ENOMEM;
+	}
+
+	input_poll->private = dmard06;
+	input_poll->open = dmard06_poll_open;
+	input_poll->close = dmard06_poll_close;
+	input_poll->poll = dmard06_poll;
+	input_poll->poll_interval = DMARD06_POLL_INTERVAL;
+	input_poll->poll_interval_min = DMARD06_POLL_MIN;
+	input_poll->poll_interval_max = DMARD06_POLL_MAX;
+
+	dmard06_init_input_device(dmard06, input_poll->input);
+
+	error = input_register_polled_device(input_poll);
+	if (error) {
+		dev_err(&dmard06->client->dev,
+			"Failed to register polled device: %d", error);
+		return error;
+	}
+
+	dmard06->input_poll = input_poll;
+	dmard06->input = input_poll->input;
+
+	return 0;
+}
+
+static int dmard06_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct dmard06_data *dmard06;
+	int error;
+
+	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		dev_err(&client->dev, "I2C check functionality failed\n");
+		return -ENXIO;
+	}
+
+	dmard06 = devm_kzalloc(&client->dev, sizeof(*dmard06), GFP_KERNEL);
+	if (!dmard06)
+		return -ENOMEM;
+
+	dmard06->client = client;
+	i2c_set_clientdata(client, dmard06);
+
+	error = dmard06_reset(dmard06->client);
+	if (error)
+		return error;
+
+	error = dmard06_read_chip_id(dmard06);
+	if (error)
+		return error;
+
+	error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
+	if (error)
+		return error;
+
+	if (dmard06->client->irq) {
+		error = dmard06_init_irq(dmard06->client);
+		if (error)
+			return error;
+
+		error = dmard06_request_input_device(dmard06);
+		if (error)
+			return error;
+
+		dmard06->irq_flags = IRQ_TYPE_LEVEL_LOW | IRQF_ONESHOT;
+		error = devm_request_threaded_irq(&dmard06->client->dev,
+						  dmard06->client->irq, NULL,
+						  dmard06_irq_handler,
+						  dmard06->irq_flags,
+						  dmard06->client->name,
+						  dmard06);
+		if (error) {
+			dev_err(&dmard06->client->dev,
+				"Request IRQ failed: %d\n", error);
+			return error;
+		}
+
+	} else {
+		error = dmard06_request_polled_device(dmard06);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static const struct i2c_device_id dmard06_id[] = {
+	{ "dmard06", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, dmard06_id);
+
+static const struct of_device_id dmard06_of_match[] = {
+	{ .compatible = "domintech,dmard06" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, dmard06_of_match);
+
+static struct i2c_driver dmard06_driver = {
+	.probe = dmard06_probe,
+	.class = I2C_CLASS_HWMON,
+	.id_table = dmard06_id,
+	.driver = {
+		.name = "dmard06",
+		.of_match_table = of_match_ptr(dmard06_of_match),
+	},
+};
+module_i2c_driver(dmard06_driver);
+
+MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
+MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
+MODULE_LICENSE("GPL v2");
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer
@ 2016-07-18 12:48   ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-18 12:48 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: linux-input-u79uwXL29TY76Z2rM5mHXA,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Aleksei Mamlin

This patch add support for Domintech DMARD06 accelerometer.

Domintech DMARD06 is a low-g tri-axial digital accelerometer with
special power saving modes suitable for consumer mobile application.

Signed-off-by: Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 .../devicetree/bindings/input/dmard06.txt          |  24 ++
 drivers/input/misc/Kconfig                         |  12 +
 drivers/input/misc/Makefile                        |   1 +
 drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
 4 files changed, 479 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
 create mode 100644 drivers/input/misc/dmard06.c

diff --git a/Documentation/devicetree/bindings/input/dmard06.txt b/Documentation/devicetree/bindings/input/dmard06.txt
new file mode 100644
index 0000000..528c318
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/dmard06.txt
@@ -0,0 +1,24 @@
+Device tree bindings for Domintech DMARD06 acceletometer
+
+Required properties:
+ - compatible		: Should be "domintech,dmard06"
+ - reg			: I2C address of the chip. Should be 0x1c
+
+Optional properties:
+ - interrupt-parent	: Interrupt controller to which the chip is connected
+ - interrupts		: Interrupt to which the chip is connected
+
+
+Example:
+	&i2c1 {
+		/* ... */
+
+		accelerometer@1c {
+			compatible = "domintech,dmard06";
+			reg = <0x1c>;
+			interrupt-parent = <&gpio>;
+			interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
+		};
+
+		/* ... */
+	};
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index efb0ca8..b95e0e7 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -107,6 +107,18 @@ config INPUT_BMA150
 	  To compile this driver as a module, choose M here: the
 	  module will be called bma150.
 
+config INPUT_DMARD06
+	tristate "Domintech DMARD06 Digital Accelerometer support"
+	depends on OF || COMPILE_TEST
+	depends on I2C
+	select INPUT_POLLDEV
+	help
+	  Say Y to enable support for the Domintech DMARD06 Low-G Tri-Axial
+	  digital accelerometer connected via an I2C bus.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called dmard06.
+
 config INPUT_E3X0_BUTTON
 	tristate "NI Ettus Research USRP E3xx Button support."
 	default n
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 6a1e5e2..d83866f 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_DA9052_ONKEY)	+= da9052_onkey.o
 obj-$(CONFIG_INPUT_DA9055_ONKEY)	+= da9055_onkey.o
 obj-$(CONFIG_INPUT_DA9063_ONKEY)	+= da9063_onkey.o
 obj-$(CONFIG_INPUT_DM355EVM)		+= dm355evm_keys.o
+obj-$(CONFIG_INPUT_DMARD06)		+= dmard06.o
 obj-$(CONFIG_INPUT_E3X0_BUTTON)		+= e3x0-button.o
 obj-$(CONFIG_INPUT_DRV260X_HAPTICS)	+= drv260x.o
 obj-$(CONFIG_INPUT_DRV2665_HAPTICS)	+= drv2665.o
diff --git a/drivers/input/misc/dmard06.c b/drivers/input/misc/dmard06.c
new file mode 100644
index 0000000..cc1e5a2
--- /dev/null
+++ b/drivers/input/misc/dmard06.c
@@ -0,0 +1,442 @@
+/*
+ *  Driver for Domintech DMARD06 accelerometer
+ *
+ *  Copyright (C) 2016 Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
+ *  Copyright (c) 2012 Domintech Technology Co., Ltd
+ *
+ * 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; version 2 of the License.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/irq.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+
+/* Device data registers */
+#define DMARD06_CHIP_ID_REG		0x0f
+#define DMARD06_TOUT_REG		0x40
+#define DMARD06_XOUT_REG		0x41
+#define DMARD06_YOUT_REG		0x42
+#define DMARD06_ZOUT_REG		0x43
+
+/* Device control registers */
+#define DMARD06_CTRL_1_REG		0x44
+#define DMARD06_CTRL_2_REG		0x45
+#define DMARD06_CTRL_3_REG		0x46
+#define DMARD06_CTRL_4_REG		0x47
+#define DMARD06_CTRL_5_REG		0x48
+#define DMARD06_STATUS_REG		0x49
+#define DMARD06_INT_CFG_REG		0x4a
+#define DMARD06_FILTER_RESET_REG	0x52
+#define DMARD06_CHIP_RESET_REG		0x53
+
+/* Device ID value */
+#define DMARD06_CHIP_ID			0x06
+
+/* Device max value */
+#define DMARD06_MAX_VALUE		0x3f
+
+/* Each axis is represented by a 1-byte data word */
+#define DMARD06_XYZ_DATA_SIZE		3
+
+/* Interrupt sources */
+#define DMARD06_INT_SRC1		0x04
+#define DMARD06_INT_SRC_BOTH		0x08
+#define DMARD06_INT_MOV_DETECT		0x40
+#define DMARD06_INT_POS_DETECT		0xc0
+
+/* Device modes */
+#define DMARD06_MODE_NORMAL		0x27
+#define DMARD06_MODE_POWERDOWN		0x00
+
+/* Input poll interval in milliseconds */
+#define DMARD06_POLL_INTERVAL		10
+#define DMARD06_POLL_MAX		100
+#define DMARD06_POLL_MIN		0
+
+struct dmard06_data {
+	struct i2c_client *client;
+	struct input_dev *input;
+	struct input_polled_dev *input_poll;
+	unsigned long irq_flags;
+	int range;
+	u8 chip_id;
+	u8 mode;
+};
+
+static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
+{
+	int error;
+
+	error = i2c_master_send(client, &reg, 1);
+	if (unlikely(error < 0))
+		return error;
+
+	error = i2c_master_recv(client, buf, 1);
+	if (unlikely(error < 0))
+		return error;
+
+	return 0;
+}
+
+static int dmard06_i2c_write(struct i2c_client *client, u8 reg, u8 val)
+{
+	int error;
+	u8 buf[2] = {reg, val};
+
+	error = i2c_master_send(client, buf, 2);
+	if (unlikely(error < 0))
+		return error;
+
+	return 0;
+}
+
+static int dmard06_reset(struct i2c_client *client)
+{
+	u8 buf;
+	int error;
+
+	error = dmard06_i2c_read(client, DMARD06_CHIP_RESET_REG, &buf);
+	if (error) {
+		dev_err(&client->dev, "Failed to reset controller: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
+static int dmard06_set_mode(struct dmard06_data *dmard06, u8 mode)
+{
+	int error;
+
+	error = dmard06_i2c_write(dmard06->client, DMARD06_CTRL_1_REG, mode);
+	if (error) {
+		dev_err(&dmard06->client->dev, "Failed to set mode: %d",
+			error);
+		return error;
+	}
+
+	dmard06->mode = mode;
+
+	return 0;
+}
+
+static int dmard06_init_irq(struct i2c_client *client)
+{
+	int error;
+
+	error = dmard06_i2c_write(client, DMARD06_CTRL_4_REG,
+				  DMARD06_INT_SRC1);
+	if (error) {
+		dev_err(&client->dev, "Failed to set interrupt source: %d",
+			error);
+		return error;
+	}
+
+	error = dmard06_i2c_write(client, DMARD06_INT_CFG_REG,
+				  DMARD06_INT_POS_DETECT);
+	if (error) {
+		dev_err(&client->dev, "Failed to set interrupt event: %d",
+			error);
+		return error;
+	}
+
+	return 0;
+}
+
+static int dmard06_open(struct dmard06_data *dmard06)
+{
+	int error;
+
+	if (dmard06->mode != DMARD06_MODE_NORMAL) {
+		error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static void dmard06_close(struct dmard06_data *dmard06)
+{
+	if (dmard06->mode != DMARD06_MODE_POWERDOWN)
+		dmard06_set_mode(dmard06, DMARD06_MODE_POWERDOWN);
+}
+
+static int dmard06_irq_open(struct input_dev *input)
+{
+	struct dmard06_data *dmard06 = input_get_drvdata(input);
+
+	return dmard06_open(dmard06);
+}
+
+static void dmard06_irq_close(struct input_dev *input)
+{
+	struct dmard06_data *dmard06 = input_get_drvdata(input);
+
+	dmard06_close(dmard06);
+}
+
+static void dmard06_poll_open(struct input_polled_dev *input_poll)
+{
+	struct dmard06_data *dmard06 = input_poll->private;
+
+	dmard06_open(dmard06);
+}
+
+static void dmard06_poll_close(struct input_polled_dev *input_poll)
+{
+	struct dmard06_data *dmard06 = input_poll->private;
+
+	dmard06_close(dmard06);
+}
+
+static int dmard06_read_chip_id(struct dmard06_data *dmard06)
+{
+	u8 buf;
+	u8 chip_id;
+	int error;
+
+	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
+	if (error) {
+		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
+			error);
+		return error;
+	}
+
+	chip_id = (buf & 0x00ff);
+	if (chip_id != DMARD06_CHIP_ID) {
+		dev_err(&dmard06->client->dev, "Unknown chip id: %02x\n",
+			chip_id);
+		return -EINVAL;
+	}
+
+	dmard06->chip_id = chip_id;
+	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
+
+	return 0;
+}
+
+static void dmard06_report_xyz(struct dmard06_data *dmard06)
+{
+	u8 axis[DMARD06_XYZ_DATA_SIZE];
+	bool reversed;
+	int xyz[3];
+	int error;
+	int i;
+
+	error = i2c_smbus_read_i2c_block_data(dmard06->client,
+					      DMARD06_XOUT_REG,
+					      DMARD06_XYZ_DATA_SIZE, axis);
+	if (error != DMARD06_XYZ_DATA_SIZE)
+		return;
+
+	for (i = 0; i < DMARD06_XYZ_DATA_SIZE; i++) {
+		reversed = (axis[i] >> 7) & 1;
+		xyz[i] = (int)((axis[i] & 0x7f) >> 1);
+		if (reversed)
+			xyz[i] = xyz[i] - dmard06->range;
+	}
+
+	input_report_abs(dmard06->input, ABS_X, xyz[0]);
+	input_report_abs(dmard06->input, ABS_Y, xyz[1]);
+	input_report_abs(dmard06->input, ABS_Z, xyz[2]);
+	input_sync(dmard06->input);
+}
+
+static irqreturn_t dmard06_irq_handler(int irq, void *dev_id)
+{
+	struct dmard06_data *dmard06 = dev_id;
+
+	dmard06_report_xyz(dmard06);
+
+	return IRQ_HANDLED;
+}
+
+static void dmard06_poll(struct input_polled_dev *input_poll)
+{
+	dmard06_report_xyz(input_poll->private);
+}
+
+static void dmard06_init_input_device(struct dmard06_data *dmard06,
+				      struct input_dev *input)
+{
+	int range;
+
+	input->name = "dmard06";
+	input->id.bustype = BUS_I2C;
+	input->id.version = dmard06->chip_id;
+	input->dev.parent = &dmard06->client->dev;
+
+	set_bit(EV_ABS, input->evbit);
+
+	range = DMARD06_MAX_VALUE;
+	input_set_abs_params(input, ABS_X, -range, range, 0, 0);
+	input_set_abs_params(input, ABS_Y, -range, range, 0, 0);
+	input_set_abs_params(input, ABS_Z, -range, range, 0, 0);
+
+	dmard06->range = range;
+}
+
+static int dmard06_request_input_device(struct dmard06_data *dmard06)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&dmard06->client->dev);
+	if (!input) {
+		dev_err(&dmard06->client->dev,
+			"Failed to allocate input device\n");
+		return -ENOMEM;
+	}
+
+	dmard06_init_input_device(dmard06, input);
+
+	input->open = dmard06_irq_open;
+	input->close = dmard06_irq_close;
+	input_set_drvdata(input, dmard06);
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&dmard06->client->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	dmard06->input = input;
+
+	return 0;
+}
+
+static int dmard06_request_polled_device(struct dmard06_data *dmard06)
+{
+	struct input_polled_dev *input_poll;
+	int error;
+
+	input_poll = devm_input_allocate_polled_device(&dmard06->client->dev);
+	if (!input_poll) {
+		dev_err(&dmard06->client->dev,
+			"Failed to allocate polled input device\n");
+		return -ENOMEM;
+	}
+
+	input_poll->private = dmard06;
+	input_poll->open = dmard06_poll_open;
+	input_poll->close = dmard06_poll_close;
+	input_poll->poll = dmard06_poll;
+	input_poll->poll_interval = DMARD06_POLL_INTERVAL;
+	input_poll->poll_interval_min = DMARD06_POLL_MIN;
+	input_poll->poll_interval_max = DMARD06_POLL_MAX;
+
+	dmard06_init_input_device(dmard06, input_poll->input);
+
+	error = input_register_polled_device(input_poll);
+	if (error) {
+		dev_err(&dmard06->client->dev,
+			"Failed to register polled device: %d", error);
+		return error;
+	}
+
+	dmard06->input_poll = input_poll;
+	dmard06->input = input_poll->input;
+
+	return 0;
+}
+
+static int dmard06_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct dmard06_data *dmard06;
+	int error;
+
+	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		dev_err(&client->dev, "I2C check functionality failed\n");
+		return -ENXIO;
+	}
+
+	dmard06 = devm_kzalloc(&client->dev, sizeof(*dmard06), GFP_KERNEL);
+	if (!dmard06)
+		return -ENOMEM;
+
+	dmard06->client = client;
+	i2c_set_clientdata(client, dmard06);
+
+	error = dmard06_reset(dmard06->client);
+	if (error)
+		return error;
+
+	error = dmard06_read_chip_id(dmard06);
+	if (error)
+		return error;
+
+	error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
+	if (error)
+		return error;
+
+	if (dmard06->client->irq) {
+		error = dmard06_init_irq(dmard06->client);
+		if (error)
+			return error;
+
+		error = dmard06_request_input_device(dmard06);
+		if (error)
+			return error;
+
+		dmard06->irq_flags = IRQ_TYPE_LEVEL_LOW | IRQF_ONESHOT;
+		error = devm_request_threaded_irq(&dmard06->client->dev,
+						  dmard06->client->irq, NULL,
+						  dmard06_irq_handler,
+						  dmard06->irq_flags,
+						  dmard06->client->name,
+						  dmard06);
+		if (error) {
+			dev_err(&dmard06->client->dev,
+				"Request IRQ failed: %d\n", error);
+			return error;
+		}
+
+	} else {
+		error = dmard06_request_polled_device(dmard06);
+		if (error)
+			return error;
+	}
+
+	return 0;
+}
+
+static const struct i2c_device_id dmard06_id[] = {
+	{ "dmard06", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, dmard06_id);
+
+static const struct of_device_id dmard06_of_match[] = {
+	{ .compatible = "domintech,dmard06" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, dmard06_of_match);
+
+static struct i2c_driver dmard06_driver = {
+	.probe = dmard06_probe,
+	.class = I2C_CLASS_HWMON,
+	.id_table = dmard06_id,
+	.driver = {
+		.name = "dmard06",
+		.of_match_table = of_match_ptr(dmard06_of_match),
+	},
+};
+module_i2c_driver(dmard06_driver);
+
+MODULE_AUTHOR("Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>");
+MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
+MODULE_LICENSE("GPL v2");
-- 
2.7.3

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer
  2016-07-18 12:48   ` Aleksei Mamlin
  (?)
@ 2016-07-20  0:02   ` Dmitry Torokhov
  2016-07-21  6:13     ` Jonathan Cameron
  -1 siblings, 1 reply; 23+ messages in thread
From: Dmitry Torokhov @ 2016-07-20  0:02 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: linux-input, devicetree, linux-kernel, Jonathan Cameron, Guenter Roeck

On Mon, Jul 18, 2016 at 03:48:12PM +0300, Aleksei Mamlin wrote:
> This patch add support for Domintech DMARD06 accelerometer.
> 
> Domintech DMARD06 is a low-g tri-axial digital accelerometer with
> special power saving modes suitable for consumer mobile application.

I'd say this belongs to IIO. The input/misc accelerometers are mostly
legacy.

> 
> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> ---
>  .../devicetree/bindings/input/dmard06.txt          |  24 ++
>  drivers/input/misc/Kconfig                         |  12 +
>  drivers/input/misc/Makefile                        |   1 +
>  drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
>  4 files changed, 479 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
>  create mode 100644 drivers/input/misc/dmard06.c
> 
> diff --git a/Documentation/devicetree/bindings/input/dmard06.txt b/Documentation/devicetree/bindings/input/dmard06.txt
> new file mode 100644
> index 0000000..528c318
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/input/dmard06.txt
> @@ -0,0 +1,24 @@
> +Device tree bindings for Domintech DMARD06 acceletometer
> +
> +Required properties:
> + - compatible		: Should be "domintech,dmard06"
> + - reg			: I2C address of the chip. Should be 0x1c
> +
> +Optional properties:
> + - interrupt-parent	: Interrupt controller to which the chip is connected
> + - interrupts		: Interrupt to which the chip is connected
> +
> +
> +Example:
> +	&i2c1 {
> +		/* ... */
> +
> +		accelerometer@1c {
> +			compatible = "domintech,dmard06";
> +			reg = <0x1c>;
> +			interrupt-parent = <&gpio>;
> +			interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
> +		};
> +
> +		/* ... */
> +	};
> diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
> index efb0ca8..b95e0e7 100644
> --- a/drivers/input/misc/Kconfig
> +++ b/drivers/input/misc/Kconfig
> @@ -107,6 +107,18 @@ config INPUT_BMA150
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called bma150.
>  
> +config INPUT_DMARD06
> +	tristate "Domintech DMARD06 Digital Accelerometer support"
> +	depends on OF || COMPILE_TEST
> +	depends on I2C
> +	select INPUT_POLLDEV
> +	help
> +	  Say Y to enable support for the Domintech DMARD06 Low-G Tri-Axial
> +	  digital accelerometer connected via an I2C bus.
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called dmard06.
> +
>  config INPUT_E3X0_BUTTON
>  	tristate "NI Ettus Research USRP E3xx Button support."
>  	default n
> diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
> index 6a1e5e2..d83866f 100644
> --- a/drivers/input/misc/Makefile
> +++ b/drivers/input/misc/Makefile
> @@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_DA9052_ONKEY)	+= da9052_onkey.o
>  obj-$(CONFIG_INPUT_DA9055_ONKEY)	+= da9055_onkey.o
>  obj-$(CONFIG_INPUT_DA9063_ONKEY)	+= da9063_onkey.o
>  obj-$(CONFIG_INPUT_DM355EVM)		+= dm355evm_keys.o
> +obj-$(CONFIG_INPUT_DMARD06)		+= dmard06.o
>  obj-$(CONFIG_INPUT_E3X0_BUTTON)		+= e3x0-button.o
>  obj-$(CONFIG_INPUT_DRV260X_HAPTICS)	+= drv260x.o
>  obj-$(CONFIG_INPUT_DRV2665_HAPTICS)	+= drv2665.o
> diff --git a/drivers/input/misc/dmard06.c b/drivers/input/misc/dmard06.c
> new file mode 100644
> index 0000000..cc1e5a2
> --- /dev/null
> +++ b/drivers/input/misc/dmard06.c
> @@ -0,0 +1,442 @@
> +/*
> + *  Driver for Domintech DMARD06 accelerometer
> + *
> + *  Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> + *  Copyright (c) 2012 Domintech Technology Co., Ltd
> + *
> + * 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; version 2 of the License.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/input.h>
> +#include <linux/input-polldev.h>
> +#include <linux/irq.h>
> +#include <linux/interrupt.h>
> +#include <linux/delay.h>
> +#include <linux/slab.h>
> +#include <linux/of.h>
> +
> +/* Device data registers */
> +#define DMARD06_CHIP_ID_REG		0x0f
> +#define DMARD06_TOUT_REG		0x40
> +#define DMARD06_XOUT_REG		0x41
> +#define DMARD06_YOUT_REG		0x42
> +#define DMARD06_ZOUT_REG		0x43
> +
> +/* Device control registers */
> +#define DMARD06_CTRL_1_REG		0x44
> +#define DMARD06_CTRL_2_REG		0x45
> +#define DMARD06_CTRL_3_REG		0x46
> +#define DMARD06_CTRL_4_REG		0x47
> +#define DMARD06_CTRL_5_REG		0x48
> +#define DMARD06_STATUS_REG		0x49
> +#define DMARD06_INT_CFG_REG		0x4a
> +#define DMARD06_FILTER_RESET_REG	0x52
> +#define DMARD06_CHIP_RESET_REG		0x53
> +
> +/* Device ID value */
> +#define DMARD06_CHIP_ID			0x06
> +
> +/* Device max value */
> +#define DMARD06_MAX_VALUE		0x3f
> +
> +/* Each axis is represented by a 1-byte data word */
> +#define DMARD06_XYZ_DATA_SIZE		3
> +
> +/* Interrupt sources */
> +#define DMARD06_INT_SRC1		0x04
> +#define DMARD06_INT_SRC_BOTH		0x08
> +#define DMARD06_INT_MOV_DETECT		0x40
> +#define DMARD06_INT_POS_DETECT		0xc0
> +
> +/* Device modes */
> +#define DMARD06_MODE_NORMAL		0x27
> +#define DMARD06_MODE_POWERDOWN		0x00
> +
> +/* Input poll interval in milliseconds */
> +#define DMARD06_POLL_INTERVAL		10
> +#define DMARD06_POLL_MAX		100
> +#define DMARD06_POLL_MIN		0
> +
> +struct dmard06_data {
> +	struct i2c_client *client;
> +	struct input_dev *input;
> +	struct input_polled_dev *input_poll;
> +	unsigned long irq_flags;
> +	int range;
> +	u8 chip_id;
> +	u8 mode;
> +};
> +
> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> +{
> +	int error;
> +
> +	error = i2c_master_send(client, &reg, 1);
> +	if (unlikely(error < 0))
> +		return error;
> +
> +	error = i2c_master_recv(client, buf, 1);
> +	if (unlikely(error < 0))
> +		return error;
> +
> +	return 0;
> +}
> +
> +static int dmard06_i2c_write(struct i2c_client *client, u8 reg, u8 val)
> +{
> +	int error;
> +	u8 buf[2] = {reg, val};
> +
> +	error = i2c_master_send(client, buf, 2);
> +	if (unlikely(error < 0))
> +		return error;
> +
> +	return 0;
> +}
> +
> +static int dmard06_reset(struct i2c_client *client)
> +{
> +	u8 buf;
> +	int error;
> +
> +	error = dmard06_i2c_read(client, DMARD06_CHIP_RESET_REG, &buf);
> +	if (error) {
> +		dev_err(&client->dev, "Failed to reset controller: %d", error);
> +		return error;
> +	}
> +
> +	return 0;
> +}
> +
> +static int dmard06_set_mode(struct dmard06_data *dmard06, u8 mode)
> +{
> +	int error;
> +
> +	error = dmard06_i2c_write(dmard06->client, DMARD06_CTRL_1_REG, mode);
> +	if (error) {
> +		dev_err(&dmard06->client->dev, "Failed to set mode: %d",
> +			error);
> +		return error;
> +	}
> +
> +	dmard06->mode = mode;
> +
> +	return 0;
> +}
> +
> +static int dmard06_init_irq(struct i2c_client *client)
> +{
> +	int error;
> +
> +	error = dmard06_i2c_write(client, DMARD06_CTRL_4_REG,
> +				  DMARD06_INT_SRC1);
> +	if (error) {
> +		dev_err(&client->dev, "Failed to set interrupt source: %d",
> +			error);
> +		return error;
> +	}
> +
> +	error = dmard06_i2c_write(client, DMARD06_INT_CFG_REG,
> +				  DMARD06_INT_POS_DETECT);
> +	if (error) {
> +		dev_err(&client->dev, "Failed to set interrupt event: %d",
> +			error);
> +		return error;
> +	}
> +
> +	return 0;
> +}
> +
> +static int dmard06_open(struct dmard06_data *dmard06)
> +{
> +	int error;
> +
> +	if (dmard06->mode != DMARD06_MODE_NORMAL) {
> +		error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
> +		if (error)
> +			return error;
> +	}
> +
> +	return 0;
> +}
> +
> +static void dmard06_close(struct dmard06_data *dmard06)
> +{
> +	if (dmard06->mode != DMARD06_MODE_POWERDOWN)
> +		dmard06_set_mode(dmard06, DMARD06_MODE_POWERDOWN);
> +}
> +
> +static int dmard06_irq_open(struct input_dev *input)
> +{
> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
> +
> +	return dmard06_open(dmard06);
> +}
> +
> +static void dmard06_irq_close(struct input_dev *input)
> +{
> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
> +
> +	dmard06_close(dmard06);
> +}
> +
> +static void dmard06_poll_open(struct input_polled_dev *input_poll)
> +{
> +	struct dmard06_data *dmard06 = input_poll->private;
> +
> +	dmard06_open(dmard06);
> +}
> +
> +static void dmard06_poll_close(struct input_polled_dev *input_poll)
> +{
> +	struct dmard06_data *dmard06 = input_poll->private;
> +
> +	dmard06_close(dmard06);
> +}
> +
> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> +{
> +	u8 buf;
> +	u8 chip_id;
> +	int error;
> +
> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> +	if (error) {
> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> +			error);
> +		return error;
> +	}
> +
> +	chip_id = (buf & 0x00ff);
> +	if (chip_id != DMARD06_CHIP_ID) {
> +		dev_err(&dmard06->client->dev, "Unknown chip id: %02x\n",
> +			chip_id);
> +		return -EINVAL;
> +	}
> +
> +	dmard06->chip_id = chip_id;
> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> +
> +	return 0;
> +}
> +
> +static void dmard06_report_xyz(struct dmard06_data *dmard06)
> +{
> +	u8 axis[DMARD06_XYZ_DATA_SIZE];
> +	bool reversed;
> +	int xyz[3];
> +	int error;
> +	int i;
> +
> +	error = i2c_smbus_read_i2c_block_data(dmard06->client,
> +					      DMARD06_XOUT_REG,
> +					      DMARD06_XYZ_DATA_SIZE, axis);
> +	if (error != DMARD06_XYZ_DATA_SIZE)
> +		return;
> +
> +	for (i = 0; i < DMARD06_XYZ_DATA_SIZE; i++) {
> +		reversed = (axis[i] >> 7) & 1;
> +		xyz[i] = (int)((axis[i] & 0x7f) >> 1);
> +		if (reversed)
> +			xyz[i] = xyz[i] - dmard06->range;
> +	}
> +
> +	input_report_abs(dmard06->input, ABS_X, xyz[0]);
> +	input_report_abs(dmard06->input, ABS_Y, xyz[1]);
> +	input_report_abs(dmard06->input, ABS_Z, xyz[2]);
> +	input_sync(dmard06->input);
> +}
> +
> +static irqreturn_t dmard06_irq_handler(int irq, void *dev_id)
> +{
> +	struct dmard06_data *dmard06 = dev_id;
> +
> +	dmard06_report_xyz(dmard06);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static void dmard06_poll(struct input_polled_dev *input_poll)
> +{
> +	dmard06_report_xyz(input_poll->private);
> +}
> +
> +static void dmard06_init_input_device(struct dmard06_data *dmard06,
> +				      struct input_dev *input)
> +{
> +	int range;
> +
> +	input->name = "dmard06";
> +	input->id.bustype = BUS_I2C;
> +	input->id.version = dmard06->chip_id;
> +	input->dev.parent = &dmard06->client->dev;
> +
> +	set_bit(EV_ABS, input->evbit);
> +
> +	range = DMARD06_MAX_VALUE;
> +	input_set_abs_params(input, ABS_X, -range, range, 0, 0);
> +	input_set_abs_params(input, ABS_Y, -range, range, 0, 0);
> +	input_set_abs_params(input, ABS_Z, -range, range, 0, 0);
> +
> +	dmard06->range = range;
> +}
> +
> +static int dmard06_request_input_device(struct dmard06_data *dmard06)
> +{
> +	struct input_dev *input;
> +	int error;
> +
> +	input = devm_input_allocate_device(&dmard06->client->dev);
> +	if (!input) {
> +		dev_err(&dmard06->client->dev,
> +			"Failed to allocate input device\n");
> +		return -ENOMEM;
> +	}
> +
> +	dmard06_init_input_device(dmard06, input);
> +
> +	input->open = dmard06_irq_open;
> +	input->close = dmard06_irq_close;
> +	input_set_drvdata(input, dmard06);
> +
> +	error = input_register_device(input);
> +	if (error) {
> +		dev_err(&dmard06->client->dev,
> +			"Failed to register input device: %d", error);
> +		return error;
> +	}
> +
> +	dmard06->input = input;
> +
> +	return 0;
> +}
> +
> +static int dmard06_request_polled_device(struct dmard06_data *dmard06)
> +{
> +	struct input_polled_dev *input_poll;
> +	int error;
> +
> +	input_poll = devm_input_allocate_polled_device(&dmard06->client->dev);
> +	if (!input_poll) {
> +		dev_err(&dmard06->client->dev,
> +			"Failed to allocate polled input device\n");
> +		return -ENOMEM;
> +	}
> +
> +	input_poll->private = dmard06;
> +	input_poll->open = dmard06_poll_open;
> +	input_poll->close = dmard06_poll_close;
> +	input_poll->poll = dmard06_poll;
> +	input_poll->poll_interval = DMARD06_POLL_INTERVAL;
> +	input_poll->poll_interval_min = DMARD06_POLL_MIN;
> +	input_poll->poll_interval_max = DMARD06_POLL_MAX;
> +
> +	dmard06_init_input_device(dmard06, input_poll->input);
> +
> +	error = input_register_polled_device(input_poll);
> +	if (error) {
> +		dev_err(&dmard06->client->dev,
> +			"Failed to register polled device: %d", error);
> +		return error;
> +	}
> +
> +	dmard06->input_poll = input_poll;
> +	dmard06->input = input_poll->input;
> +
> +	return 0;
> +}
> +
> +static int dmard06_probe(struct i2c_client *client,
> +			 const struct i2c_device_id *id)
> +{
> +	struct dmard06_data *dmard06;
> +	int error;
> +
> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> +
> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> +		dev_err(&client->dev, "I2C check functionality failed\n");
> +		return -ENXIO;
> +	}
> +
> +	dmard06 = devm_kzalloc(&client->dev, sizeof(*dmard06), GFP_KERNEL);
> +	if (!dmard06)
> +		return -ENOMEM;
> +
> +	dmard06->client = client;
> +	i2c_set_clientdata(client, dmard06);
> +
> +	error = dmard06_reset(dmard06->client);
> +	if (error)
> +		return error;
> +
> +	error = dmard06_read_chip_id(dmard06);
> +	if (error)
> +		return error;
> +
> +	error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
> +	if (error)
> +		return error;
> +
> +	if (dmard06->client->irq) {
> +		error = dmard06_init_irq(dmard06->client);
> +		if (error)
> +			return error;
> +
> +		error = dmard06_request_input_device(dmard06);
> +		if (error)
> +			return error;
> +
> +		dmard06->irq_flags = IRQ_TYPE_LEVEL_LOW | IRQF_ONESHOT;
> +		error = devm_request_threaded_irq(&dmard06->client->dev,
> +						  dmard06->client->irq, NULL,
> +						  dmard06_irq_handler,
> +						  dmard06->irq_flags,
> +						  dmard06->client->name,
> +						  dmard06);
> +		if (error) {
> +			dev_err(&dmard06->client->dev,
> +				"Request IRQ failed: %d\n", error);
> +			return error;
> +		}
> +
> +	} else {
> +		error = dmard06_request_polled_device(dmard06);
> +		if (error)
> +			return error;
> +	}
> +
> +	return 0;
> +}
> +
> +static const struct i2c_device_id dmard06_id[] = {
> +	{ "dmard06", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> +
> +static const struct of_device_id dmard06_of_match[] = {
> +	{ .compatible = "domintech,dmard06" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> +
> +static struct i2c_driver dmard06_driver = {
> +	.probe = dmard06_probe,
> +	.class = I2C_CLASS_HWMON,
> +	.id_table = dmard06_id,
> +	.driver = {
> +		.name = "dmard06",
> +		.of_match_table = of_match_ptr(dmard06_of_match),
> +	},
> +};
> +module_i2c_driver(dmard06_driver);
> +
> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> +MODULE_LICENSE("GPL v2");
> -- 
> 2.7.3
> 

-- 
Dmitry

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
  2016-07-18 12:48 ` [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd Aleksei Mamlin
@ 2016-07-20  1:09   ` Rob Herring
  0 siblings, 0 replies; 23+ messages in thread
From: Rob Herring @ 2016-07-20  1:09 UTC (permalink / raw)
  To: Aleksei Mamlin; +Cc: Dmitry Torokhov, linux-input, devicetree, linux-kernel

On Mon, Jul 18, 2016 at 03:48:11PM +0300, Aleksei Mamlin wrote:
> Add vendor-prefix for Domintech Co., Ltd.
> 
> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> ---
>  Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
>  1 file changed, 1 insertion(+)

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer
  2016-07-20  0:02   ` Dmitry Torokhov
@ 2016-07-21  6:13     ` Jonathan Cameron
  2016-07-21  6:35       ` Aleksei Mamlin
  0 siblings, 1 reply; 23+ messages in thread
From: Jonathan Cameron @ 2016-07-21  6:13 UTC (permalink / raw)
  To: Dmitry Torokhov, Aleksei Mamlin
  Cc: linux-input, devicetree, linux-kernel, Jonathan Cameron, Guenter Roeck

On 20/07/16 01:02, Dmitry Torokhov wrote:
> On Mon, Jul 18, 2016 at 03:48:12PM +0300, Aleksei Mamlin wrote:
>> This patch add support for Domintech DMARD06 accelerometer.
>>
>> Domintech DMARD06 is a low-g tri-axial digital accelerometer with
>> special power saving modes suitable for consumer mobile application.
> 
> I'd say this belongs to IIO. The input/misc accelerometers are mostly
> legacy.
Also note we have a dmard09 driver under review on the IIO list
at the moment. At quick glance, this looks to have a totally different
register set though so probably doesn't make sense to combine the
drivers.

Do you have docs for this part? The dmard09 driver was written
based on a vendor driver so little is known about what the other registers
are for!

Jonathan
> 
>>
>> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
>> ---
>>  .../devicetree/bindings/input/dmard06.txt          |  24 ++
>>  drivers/input/misc/Kconfig                         |  12 +
>>  drivers/input/misc/Makefile                        |   1 +
>>  drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
>>  4 files changed, 479 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
>>  create mode 100644 drivers/input/misc/dmard06.c
>>
>> diff --git a/Documentation/devicetree/bindings/input/dmard06.txt b/Documentation/devicetree/bindings/input/dmard06.txt
>> new file mode 100644
>> index 0000000..528c318
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/input/dmard06.txt
>> @@ -0,0 +1,24 @@
>> +Device tree bindings for Domintech DMARD06 acceletometer
>> +
>> +Required properties:
>> + - compatible		: Should be "domintech,dmard06"
>> + - reg			: I2C address of the chip. Should be 0x1c
>> +
>> +Optional properties:
>> + - interrupt-parent	: Interrupt controller to which the chip is connected
>> + - interrupts		: Interrupt to which the chip is connected
>> +
>> +
>> +Example:
>> +	&i2c1 {
>> +		/* ... */
>> +
>> +		accelerometer@1c {
>> +			compatible = "domintech,dmard06";
>> +			reg = <0x1c>;
>> +			interrupt-parent = <&gpio>;
>> +			interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
>> +		};
>> +
>> +		/* ... */
>> +	};
>> diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
>> index efb0ca8..b95e0e7 100644
>> --- a/drivers/input/misc/Kconfig
>> +++ b/drivers/input/misc/Kconfig
>> @@ -107,6 +107,18 @@ config INPUT_BMA150
>>  	  To compile this driver as a module, choose M here: the
>>  	  module will be called bma150.
>>  
>> +config INPUT_DMARD06
>> +	tristate "Domintech DMARD06 Digital Accelerometer support"
>> +	depends on OF || COMPILE_TEST
>> +	depends on I2C
>> +	select INPUT_POLLDEV
>> +	help
>> +	  Say Y to enable support for the Domintech DMARD06 Low-G Tri-Axial
>> +	  digital accelerometer connected via an I2C bus.
>> +
>> +	  To compile this driver as a module, choose M here: the
>> +	  module will be called dmard06.
>> +
>>  config INPUT_E3X0_BUTTON
>>  	tristate "NI Ettus Research USRP E3xx Button support."
>>  	default n
>> diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
>> index 6a1e5e2..d83866f 100644
>> --- a/drivers/input/misc/Makefile
>> +++ b/drivers/input/misc/Makefile
>> @@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_DA9052_ONKEY)	+= da9052_onkey.o
>>  obj-$(CONFIG_INPUT_DA9055_ONKEY)	+= da9055_onkey.o
>>  obj-$(CONFIG_INPUT_DA9063_ONKEY)	+= da9063_onkey.o
>>  obj-$(CONFIG_INPUT_DM355EVM)		+= dm355evm_keys.o
>> +obj-$(CONFIG_INPUT_DMARD06)		+= dmard06.o
>>  obj-$(CONFIG_INPUT_E3X0_BUTTON)		+= e3x0-button.o
>>  obj-$(CONFIG_INPUT_DRV260X_HAPTICS)	+= drv260x.o
>>  obj-$(CONFIG_INPUT_DRV2665_HAPTICS)	+= drv2665.o
>> diff --git a/drivers/input/misc/dmard06.c b/drivers/input/misc/dmard06.c
>> new file mode 100644
>> index 0000000..cc1e5a2
>> --- /dev/null
>> +++ b/drivers/input/misc/dmard06.c
>> @@ -0,0 +1,442 @@
>> +/*
>> + *  Driver for Domintech DMARD06 accelerometer
>> + *
>> + *  Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
>> + *  Copyright (c) 2012 Domintech Technology Co., Ltd
>> + *
>> + * 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; version 2 of the License.
>> + */
>> +
>> +#include <linux/kernel.h>
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/input.h>
>> +#include <linux/input-polldev.h>
>> +#include <linux/irq.h>
>> +#include <linux/interrupt.h>
>> +#include <linux/delay.h>
>> +#include <linux/slab.h>
>> +#include <linux/of.h>
>> +
>> +/* Device data registers */
>> +#define DMARD06_CHIP_ID_REG		0x0f
>> +#define DMARD06_TOUT_REG		0x40
>> +#define DMARD06_XOUT_REG		0x41
>> +#define DMARD06_YOUT_REG		0x42
>> +#define DMARD06_ZOUT_REG		0x43
>> +
>> +/* Device control registers */
>> +#define DMARD06_CTRL_1_REG		0x44
>> +#define DMARD06_CTRL_2_REG		0x45
>> +#define DMARD06_CTRL_3_REG		0x46
>> +#define DMARD06_CTRL_4_REG		0x47
>> +#define DMARD06_CTRL_5_REG		0x48
>> +#define DMARD06_STATUS_REG		0x49
>> +#define DMARD06_INT_CFG_REG		0x4a
>> +#define DMARD06_FILTER_RESET_REG	0x52
>> +#define DMARD06_CHIP_RESET_REG		0x53
>> +
>> +/* Device ID value */
>> +#define DMARD06_CHIP_ID			0x06
>> +
>> +/* Device max value */
>> +#define DMARD06_MAX_VALUE		0x3f
>> +
>> +/* Each axis is represented by a 1-byte data word */
>> +#define DMARD06_XYZ_DATA_SIZE		3
>> +
>> +/* Interrupt sources */
>> +#define DMARD06_INT_SRC1		0x04
>> +#define DMARD06_INT_SRC_BOTH		0x08
>> +#define DMARD06_INT_MOV_DETECT		0x40
>> +#define DMARD06_INT_POS_DETECT		0xc0
>> +
>> +/* Device modes */
>> +#define DMARD06_MODE_NORMAL		0x27
>> +#define DMARD06_MODE_POWERDOWN		0x00
>> +
>> +/* Input poll interval in milliseconds */
>> +#define DMARD06_POLL_INTERVAL		10
>> +#define DMARD06_POLL_MAX		100
>> +#define DMARD06_POLL_MIN		0
>> +
>> +struct dmard06_data {
>> +	struct i2c_client *client;
>> +	struct input_dev *input;
>> +	struct input_polled_dev *input_poll;
>> +	unsigned long irq_flags;
>> +	int range;
>> +	u8 chip_id;
>> +	u8 mode;
>> +};
>> +
>> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
>> +{
>> +	int error;
>> +
>> +	error = i2c_master_send(client, &reg, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
>> +
>> +	error = i2c_master_recv(client, buf, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_i2c_write(struct i2c_client *client, u8 reg, u8 val)
>> +{
>> +	int error;
>> +	u8 buf[2] = {reg, val};
>> +
>> +	error = i2c_master_send(client, buf, 2);
>> +	if (unlikely(error < 0))
>> +		return error;
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_reset(struct i2c_client *client)
>> +{
>> +	u8 buf;
>> +	int error;
>> +
>> +	error = dmard06_i2c_read(client, DMARD06_CHIP_RESET_REG, &buf);
>> +	if (error) {
>> +		dev_err(&client->dev, "Failed to reset controller: %d", error);
>> +		return error;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_set_mode(struct dmard06_data *dmard06, u8 mode)
>> +{
>> +	int error;
>> +
>> +	error = dmard06_i2c_write(dmard06->client, DMARD06_CTRL_1_REG, mode);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev, "Failed to set mode: %d",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	dmard06->mode = mode;
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_init_irq(struct i2c_client *client)
>> +{
>> +	int error;
>> +
>> +	error = dmard06_i2c_write(client, DMARD06_CTRL_4_REG,
>> +				  DMARD06_INT_SRC1);
>> +	if (error) {
>> +		dev_err(&client->dev, "Failed to set interrupt source: %d",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	error = dmard06_i2c_write(client, DMARD06_INT_CFG_REG,
>> +				  DMARD06_INT_POS_DETECT);
>> +	if (error) {
>> +		dev_err(&client->dev, "Failed to set interrupt event: %d",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_open(struct dmard06_data *dmard06)
>> +{
>> +	int error;
>> +
>> +	if (dmard06->mode != DMARD06_MODE_NORMAL) {
>> +		error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
>> +		if (error)
>> +			return error;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static void dmard06_close(struct dmard06_data *dmard06)
>> +{
>> +	if (dmard06->mode != DMARD06_MODE_POWERDOWN)
>> +		dmard06_set_mode(dmard06, DMARD06_MODE_POWERDOWN);
>> +}
>> +
>> +static int dmard06_irq_open(struct input_dev *input)
>> +{
>> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
>> +
>> +	return dmard06_open(dmard06);
>> +}
>> +
>> +static void dmard06_irq_close(struct input_dev *input)
>> +{
>> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
>> +
>> +	dmard06_close(dmard06);
>> +}
>> +
>> +static void dmard06_poll_open(struct input_polled_dev *input_poll)
>> +{
>> +	struct dmard06_data *dmard06 = input_poll->private;
>> +
>> +	dmard06_open(dmard06);
>> +}
>> +
>> +static void dmard06_poll_close(struct input_polled_dev *input_poll)
>> +{
>> +	struct dmard06_data *dmard06 = input_poll->private;
>> +
>> +	dmard06_close(dmard06);
>> +}
>> +
>> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
>> +{
>> +	u8 buf;
>> +	u8 chip_id;
>> +	int error;
>> +
>> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	chip_id = (buf & 0x00ff);
>> +	if (chip_id != DMARD06_CHIP_ID) {
>> +		dev_err(&dmard06->client->dev, "Unknown chip id: %02x\n",
>> +			chip_id);
>> +		return -EINVAL;
>> +	}
>> +
>> +	dmard06->chip_id = chip_id;
>> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
>> +
>> +	return 0;
>> +}
>> +
>> +static void dmard06_report_xyz(struct dmard06_data *dmard06)
>> +{
>> +	u8 axis[DMARD06_XYZ_DATA_SIZE];
>> +	bool reversed;
>> +	int xyz[3];
>> +	int error;
>> +	int i;
>> +
>> +	error = i2c_smbus_read_i2c_block_data(dmard06->client,
>> +					      DMARD06_XOUT_REG,
>> +					      DMARD06_XYZ_DATA_SIZE, axis);
>> +	if (error != DMARD06_XYZ_DATA_SIZE)
>> +		return;
>> +
>> +	for (i = 0; i < DMARD06_XYZ_DATA_SIZE; i++) {
>> +		reversed = (axis[i] >> 7) & 1;
>> +		xyz[i] = (int)((axis[i] & 0x7f) >> 1);
>> +		if (reversed)
>> +			xyz[i] = xyz[i] - dmard06->range;
>> +	}
>> +
>> +	input_report_abs(dmard06->input, ABS_X, xyz[0]);
>> +	input_report_abs(dmard06->input, ABS_Y, xyz[1]);
>> +	input_report_abs(dmard06->input, ABS_Z, xyz[2]);
>> +	input_sync(dmard06->input);
>> +}
>> +
>> +static irqreturn_t dmard06_irq_handler(int irq, void *dev_id)
>> +{
>> +	struct dmard06_data *dmard06 = dev_id;
>> +
>> +	dmard06_report_xyz(dmard06);
>> +
>> +	return IRQ_HANDLED;
>> +}
>> +
>> +static void dmard06_poll(struct input_polled_dev *input_poll)
>> +{
>> +	dmard06_report_xyz(input_poll->private);
>> +}
>> +
>> +static void dmard06_init_input_device(struct dmard06_data *dmard06,
>> +				      struct input_dev *input)
>> +{
>> +	int range;
>> +
>> +	input->name = "dmard06";
>> +	input->id.bustype = BUS_I2C;
>> +	input->id.version = dmard06->chip_id;
>> +	input->dev.parent = &dmard06->client->dev;
>> +
>> +	set_bit(EV_ABS, input->evbit);
>> +
>> +	range = DMARD06_MAX_VALUE;
>> +	input_set_abs_params(input, ABS_X, -range, range, 0, 0);
>> +	input_set_abs_params(input, ABS_Y, -range, range, 0, 0);
>> +	input_set_abs_params(input, ABS_Z, -range, range, 0, 0);
>> +
>> +	dmard06->range = range;
>> +}
>> +
>> +static int dmard06_request_input_device(struct dmard06_data *dmard06)
>> +{
>> +	struct input_dev *input;
>> +	int error;
>> +
>> +	input = devm_input_allocate_device(&dmard06->client->dev);
>> +	if (!input) {
>> +		dev_err(&dmard06->client->dev,
>> +			"Failed to allocate input device\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	dmard06_init_input_device(dmard06, input);
>> +
>> +	input->open = dmard06_irq_open;
>> +	input->close = dmard06_irq_close;
>> +	input_set_drvdata(input, dmard06);
>> +
>> +	error = input_register_device(input);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev,
>> +			"Failed to register input device: %d", error);
>> +		return error;
>> +	}
>> +
>> +	dmard06->input = input;
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_request_polled_device(struct dmard06_data *dmard06)
>> +{
>> +	struct input_polled_dev *input_poll;
>> +	int error;
>> +
>> +	input_poll = devm_input_allocate_polled_device(&dmard06->client->dev);
>> +	if (!input_poll) {
>> +		dev_err(&dmard06->client->dev,
>> +			"Failed to allocate polled input device\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	input_poll->private = dmard06;
>> +	input_poll->open = dmard06_poll_open;
>> +	input_poll->close = dmard06_poll_close;
>> +	input_poll->poll = dmard06_poll;
>> +	input_poll->poll_interval = DMARD06_POLL_INTERVAL;
>> +	input_poll->poll_interval_min = DMARD06_POLL_MIN;
>> +	input_poll->poll_interval_max = DMARD06_POLL_MAX;
>> +
>> +	dmard06_init_input_device(dmard06, input_poll->input);
>> +
>> +	error = input_register_polled_device(input_poll);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev,
>> +			"Failed to register polled device: %d", error);
>> +		return error;
>> +	}
>> +
>> +	dmard06->input_poll = input_poll;
>> +	dmard06->input = input_poll->input;
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_probe(struct i2c_client *client,
>> +			 const struct i2c_device_id *id)
>> +{
>> +	struct dmard06_data *dmard06;
>> +	int error;
>> +
>> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
>> +
>> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
>> +		dev_err(&client->dev, "I2C check functionality failed\n");
>> +		return -ENXIO;
>> +	}
>> +
>> +	dmard06 = devm_kzalloc(&client->dev, sizeof(*dmard06), GFP_KERNEL);
>> +	if (!dmard06)
>> +		return -ENOMEM;
>> +
>> +	dmard06->client = client;
>> +	i2c_set_clientdata(client, dmard06);
>> +
>> +	error = dmard06_reset(dmard06->client);
>> +	if (error)
>> +		return error;
>> +
>> +	error = dmard06_read_chip_id(dmard06);
>> +	if (error)
>> +		return error;
>> +
>> +	error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
>> +	if (error)
>> +		return error;
>> +
>> +	if (dmard06->client->irq) {
>> +		error = dmard06_init_irq(dmard06->client);
>> +		if (error)
>> +			return error;
>> +
>> +		error = dmard06_request_input_device(dmard06);
>> +		if (error)
>> +			return error;
>> +
>> +		dmard06->irq_flags = IRQ_TYPE_LEVEL_LOW | IRQF_ONESHOT;
>> +		error = devm_request_threaded_irq(&dmard06->client->dev,
>> +						  dmard06->client->irq, NULL,
>> +						  dmard06_irq_handler,
>> +						  dmard06->irq_flags,
>> +						  dmard06->client->name,
>> +						  dmard06);
>> +		if (error) {
>> +			dev_err(&dmard06->client->dev,
>> +				"Request IRQ failed: %d\n", error);
>> +			return error;
>> +		}
>> +
>> +	} else {
>> +		error = dmard06_request_polled_device(dmard06);
>> +		if (error)
>> +			return error;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static const struct i2c_device_id dmard06_id[] = {
>> +	{ "dmard06", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
>> +
>> +static const struct of_device_id dmard06_of_match[] = {
>> +	{ .compatible = "domintech,dmard06" },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
>> +
>> +static struct i2c_driver dmard06_driver = {
>> +	.probe = dmard06_probe,
>> +	.class = I2C_CLASS_HWMON,
>> +	.id_table = dmard06_id,
>> +	.driver = {
>> +		.name = "dmard06",
>> +		.of_match_table = of_match_ptr(dmard06_of_match),
>> +	},
>> +};
>> +module_i2c_driver(dmard06_driver);
>> +
>> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
>> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
>> +MODULE_LICENSE("GPL v2");
>> -- 
>> 2.7.3
>>
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer
  2016-07-21  6:13     ` Jonathan Cameron
@ 2016-07-21  6:35       ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21  6:35 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Dmitry Torokhov, linux-input, devicetree, linux-kernel, Guenter Roeck

On Thu, 21 Jul 2016 07:13:05 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On 20/07/16 01:02, Dmitry Torokhov wrote:
> > On Mon, Jul 18, 2016 at 03:48:12PM +0300, Aleksei Mamlin wrote:
> >> This patch add support for Domintech DMARD06 accelerometer.
> >>
> >> Domintech DMARD06 is a low-g tri-axial digital accelerometer with
> >> special power saving modes suitable for consumer mobile application.
> > 
> > I'd say this belongs to IIO. The input/misc accelerometers are mostly
> > legacy.
> Also note we have a dmard09 driver under review on the IIO list
> at the moment. At quick glance, this looks to have a totally different
> register set though so probably doesn't make sense to combine the
> drivers.
> 

I see dmard09 driver in linux-iio list and already prepare basic iio driver for
dmard06. I'll send it to linux-iio list today.
As you said, dmard06 and dmard09 use completely different registers.

>
> Do you have docs for this part? The dmard09 driver was written
> based on a vendor driver so little is known about what the other registers
> are for!

I have dmard05, dmard06 and dmard07 datasheets, but never seen dmard09.
Most of dmard06 registers used for interrupt source and events configuration,
power control and filtering.

> 
> Jonathan
> > 
> >>
> >> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> >> ---
> >>  .../devicetree/bindings/input/dmard06.txt          |  24 ++
> >>  drivers/input/misc/Kconfig                         |  12 +
> >>  drivers/input/misc/Makefile                        |   1 +
> >>  drivers/input/misc/dmard06.c                       | 442 +++++++++++++++++++++
> >>  4 files changed, 479 insertions(+)
> >>  create mode 100644 Documentation/devicetree/bindings/input/dmard06.txt
> >>  create mode 100644 drivers/input/misc/dmard06.c
> >>
> >> diff --git a/Documentation/devicetree/bindings/input/dmard06.txt b/Documentation/devicetree/bindings/input/dmard06.txt
> >> new file mode 100644
> >> index 0000000..528c318
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/input/dmard06.txt
> >> @@ -0,0 +1,24 @@
> >> +Device tree bindings for Domintech DMARD06 acceletometer
> >> +
> >> +Required properties:
> >> + - compatible		: Should be "domintech,dmard06"
> >> + - reg			: I2C address of the chip. Should be 0x1c
> >> +
> >> +Optional properties:
> >> + - interrupt-parent	: Interrupt controller to which the chip is connected
> >> + - interrupts		: Interrupt to which the chip is connected
> >> +
> >> +
> >> +Example:
> >> +	&i2c1 {
> >> +		/* ... */
> >> +
> >> +		accelerometer@1c {
> >> +			compatible = "domintech,dmard06";
> >> +			reg = <0x1c>;
> >> +			interrupt-parent = <&gpio>;
> >> +			interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
> >> +		};
> >> +
> >> +		/* ... */
> >> +	};
> >> diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
> >> index efb0ca8..b95e0e7 100644
> >> --- a/drivers/input/misc/Kconfig
> >> +++ b/drivers/input/misc/Kconfig
> >> @@ -107,6 +107,18 @@ config INPUT_BMA150
> >>  	  To compile this driver as a module, choose M here: the
> >>  	  module will be called bma150.
> >>  
> >> +config INPUT_DMARD06
> >> +	tristate "Domintech DMARD06 Digital Accelerometer support"
> >> +	depends on OF || COMPILE_TEST
> >> +	depends on I2C
> >> +	select INPUT_POLLDEV
> >> +	help
> >> +	  Say Y to enable support for the Domintech DMARD06 Low-G Tri-Axial
> >> +	  digital accelerometer connected via an I2C bus.
> >> +
> >> +	  To compile this driver as a module, choose M here: the
> >> +	  module will be called dmard06.
> >> +
> >>  config INPUT_E3X0_BUTTON
> >>  	tristate "NI Ettus Research USRP E3xx Button support."
> >>  	default n
> >> diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
> >> index 6a1e5e2..d83866f 100644
> >> --- a/drivers/input/misc/Makefile
> >> +++ b/drivers/input/misc/Makefile
> >> @@ -28,6 +28,7 @@ obj-$(CONFIG_INPUT_DA9052_ONKEY)	+= da9052_onkey.o
> >>  obj-$(CONFIG_INPUT_DA9055_ONKEY)	+= da9055_onkey.o
> >>  obj-$(CONFIG_INPUT_DA9063_ONKEY)	+= da9063_onkey.o
> >>  obj-$(CONFIG_INPUT_DM355EVM)		+= dm355evm_keys.o
> >> +obj-$(CONFIG_INPUT_DMARD06)		+= dmard06.o
> >>  obj-$(CONFIG_INPUT_E3X0_BUTTON)		+= e3x0-button.o
> >>  obj-$(CONFIG_INPUT_DRV260X_HAPTICS)	+= drv260x.o
> >>  obj-$(CONFIG_INPUT_DRV2665_HAPTICS)	+= drv2665.o
> >> diff --git a/drivers/input/misc/dmard06.c b/drivers/input/misc/dmard06.c
> >> new file mode 100644
> >> index 0000000..cc1e5a2
> >> --- /dev/null
> >> +++ b/drivers/input/misc/dmard06.c
> >> @@ -0,0 +1,442 @@
> >> +/*
> >> + *  Driver for Domintech DMARD06 accelerometer
> >> + *
> >> + *  Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> >> + *  Copyright (c) 2012 Domintech Technology Co., Ltd
> >> + *
> >> + * 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; version 2 of the License.
> >> + */
> >> +
> >> +#include <linux/kernel.h>
> >> +#include <linux/module.h>
> >> +#include <linux/i2c.h>
> >> +#include <linux/input.h>
> >> +#include <linux/input-polldev.h>
> >> +#include <linux/irq.h>
> >> +#include <linux/interrupt.h>
> >> +#include <linux/delay.h>
> >> +#include <linux/slab.h>
> >> +#include <linux/of.h>
> >> +
> >> +/* Device data registers */
> >> +#define DMARD06_CHIP_ID_REG		0x0f
> >> +#define DMARD06_TOUT_REG		0x40
> >> +#define DMARD06_XOUT_REG		0x41
> >> +#define DMARD06_YOUT_REG		0x42
> >> +#define DMARD06_ZOUT_REG		0x43
> >> +
> >> +/* Device control registers */
> >> +#define DMARD06_CTRL_1_REG		0x44
> >> +#define DMARD06_CTRL_2_REG		0x45
> >> +#define DMARD06_CTRL_3_REG		0x46
> >> +#define DMARD06_CTRL_4_REG		0x47
> >> +#define DMARD06_CTRL_5_REG		0x48
> >> +#define DMARD06_STATUS_REG		0x49
> >> +#define DMARD06_INT_CFG_REG		0x4a
> >> +#define DMARD06_FILTER_RESET_REG	0x52
> >> +#define DMARD06_CHIP_RESET_REG		0x53
> >> +
> >> +/* Device ID value */
> >> +#define DMARD06_CHIP_ID			0x06
> >> +
> >> +/* Device max value */
> >> +#define DMARD06_MAX_VALUE		0x3f
> >> +
> >> +/* Each axis is represented by a 1-byte data word */
> >> +#define DMARD06_XYZ_DATA_SIZE		3
> >> +
> >> +/* Interrupt sources */
> >> +#define DMARD06_INT_SRC1		0x04
> >> +#define DMARD06_INT_SRC_BOTH		0x08
> >> +#define DMARD06_INT_MOV_DETECT		0x40
> >> +#define DMARD06_INT_POS_DETECT		0xc0
> >> +
> >> +/* Device modes */
> >> +#define DMARD06_MODE_NORMAL		0x27
> >> +#define DMARD06_MODE_POWERDOWN		0x00
> >> +
> >> +/* Input poll interval in milliseconds */
> >> +#define DMARD06_POLL_INTERVAL		10
> >> +#define DMARD06_POLL_MAX		100
> >> +#define DMARD06_POLL_MIN		0
> >> +
> >> +struct dmard06_data {
> >> +	struct i2c_client *client;
> >> +	struct input_dev *input;
> >> +	struct input_polled_dev *input_poll;
> >> +	unsigned long irq_flags;
> >> +	int range;
> >> +	u8 chip_id;
> >> +	u8 mode;
> >> +};
> >> +
> >> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> >> +{
> >> +	int error;
> >> +
> >> +	error = i2c_master_send(client, &reg, 1);
> >> +	if (unlikely(error < 0))
> >> +		return error;
> >> +
> >> +	error = i2c_master_recv(client, buf, 1);
> >> +	if (unlikely(error < 0))
> >> +		return error;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_i2c_write(struct i2c_client *client, u8 reg, u8 val)
> >> +{
> >> +	int error;
> >> +	u8 buf[2] = {reg, val};
> >> +
> >> +	error = i2c_master_send(client, buf, 2);
> >> +	if (unlikely(error < 0))
> >> +		return error;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_reset(struct i2c_client *client)
> >> +{
> >> +	u8 buf;
> >> +	int error;
> >> +
> >> +	error = dmard06_i2c_read(client, DMARD06_CHIP_RESET_REG, &buf);
> >> +	if (error) {
> >> +		dev_err(&client->dev, "Failed to reset controller: %d", error);
> >> +		return error;
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_set_mode(struct dmard06_data *dmard06, u8 mode)
> >> +{
> >> +	int error;
> >> +
> >> +	error = dmard06_i2c_write(dmard06->client, DMARD06_CTRL_1_REG, mode);
> >> +	if (error) {
> >> +		dev_err(&dmard06->client->dev, "Failed to set mode: %d",
> >> +			error);
> >> +		return error;
> >> +	}
> >> +
> >> +	dmard06->mode = mode;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_init_irq(struct i2c_client *client)
> >> +{
> >> +	int error;
> >> +
> >> +	error = dmard06_i2c_write(client, DMARD06_CTRL_4_REG,
> >> +				  DMARD06_INT_SRC1);
> >> +	if (error) {
> >> +		dev_err(&client->dev, "Failed to set interrupt source: %d",
> >> +			error);
> >> +		return error;
> >> +	}
> >> +
> >> +	error = dmard06_i2c_write(client, DMARD06_INT_CFG_REG,
> >> +				  DMARD06_INT_POS_DETECT);
> >> +	if (error) {
> >> +		dev_err(&client->dev, "Failed to set interrupt event: %d",
> >> +			error);
> >> +		return error;
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_open(struct dmard06_data *dmard06)
> >> +{
> >> +	int error;
> >> +
> >> +	if (dmard06->mode != DMARD06_MODE_NORMAL) {
> >> +		error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
> >> +		if (error)
> >> +			return error;
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static void dmard06_close(struct dmard06_data *dmard06)
> >> +{
> >> +	if (dmard06->mode != DMARD06_MODE_POWERDOWN)
> >> +		dmard06_set_mode(dmard06, DMARD06_MODE_POWERDOWN);
> >> +}
> >> +
> >> +static int dmard06_irq_open(struct input_dev *input)
> >> +{
> >> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
> >> +
> >> +	return dmard06_open(dmard06);
> >> +}
> >> +
> >> +static void dmard06_irq_close(struct input_dev *input)
> >> +{
> >> +	struct dmard06_data *dmard06 = input_get_drvdata(input);
> >> +
> >> +	dmard06_close(dmard06);
> >> +}
> >> +
> >> +static void dmard06_poll_open(struct input_polled_dev *input_poll)
> >> +{
> >> +	struct dmard06_data *dmard06 = input_poll->private;
> >> +
> >> +	dmard06_open(dmard06);
> >> +}
> >> +
> >> +static void dmard06_poll_close(struct input_polled_dev *input_poll)
> >> +{
> >> +	struct dmard06_data *dmard06 = input_poll->private;
> >> +
> >> +	dmard06_close(dmard06);
> >> +}
> >> +
> >> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> >> +{
> >> +	u8 buf;
> >> +	u8 chip_id;
> >> +	int error;
> >> +
> >> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> >> +	if (error) {
> >> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> >> +			error);
> >> +		return error;
> >> +	}
> >> +
> >> +	chip_id = (buf & 0x00ff);
> >> +	if (chip_id != DMARD06_CHIP_ID) {
> >> +		dev_err(&dmard06->client->dev, "Unknown chip id: %02x\n",
> >> +			chip_id);
> >> +		return -EINVAL;
> >> +	}
> >> +
> >> +	dmard06->chip_id = chip_id;
> >> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static void dmard06_report_xyz(struct dmard06_data *dmard06)
> >> +{
> >> +	u8 axis[DMARD06_XYZ_DATA_SIZE];
> >> +	bool reversed;
> >> +	int xyz[3];
> >> +	int error;
> >> +	int i;
> >> +
> >> +	error = i2c_smbus_read_i2c_block_data(dmard06->client,
> >> +					      DMARD06_XOUT_REG,
> >> +					      DMARD06_XYZ_DATA_SIZE, axis);
> >> +	if (error != DMARD06_XYZ_DATA_SIZE)
> >> +		return;
> >> +
> >> +	for (i = 0; i < DMARD06_XYZ_DATA_SIZE; i++) {
> >> +		reversed = (axis[i] >> 7) & 1;
> >> +		xyz[i] = (int)((axis[i] & 0x7f) >> 1);
> >> +		if (reversed)
> >> +			xyz[i] = xyz[i] - dmard06->range;
> >> +	}
> >> +
> >> +	input_report_abs(dmard06->input, ABS_X, xyz[0]);
> >> +	input_report_abs(dmard06->input, ABS_Y, xyz[1]);
> >> +	input_report_abs(dmard06->input, ABS_Z, xyz[2]);
> >> +	input_sync(dmard06->input);
> >> +}
> >> +
> >> +static irqreturn_t dmard06_irq_handler(int irq, void *dev_id)
> >> +{
> >> +	struct dmard06_data *dmard06 = dev_id;
> >> +
> >> +	dmard06_report_xyz(dmard06);
> >> +
> >> +	return IRQ_HANDLED;
> >> +}
> >> +
> >> +static void dmard06_poll(struct input_polled_dev *input_poll)
> >> +{
> >> +	dmard06_report_xyz(input_poll->private);
> >> +}
> >> +
> >> +static void dmard06_init_input_device(struct dmard06_data *dmard06,
> >> +				      struct input_dev *input)
> >> +{
> >> +	int range;
> >> +
> >> +	input->name = "dmard06";
> >> +	input->id.bustype = BUS_I2C;
> >> +	input->id.version = dmard06->chip_id;
> >> +	input->dev.parent = &dmard06->client->dev;
> >> +
> >> +	set_bit(EV_ABS, input->evbit);
> >> +
> >> +	range = DMARD06_MAX_VALUE;
> >> +	input_set_abs_params(input, ABS_X, -range, range, 0, 0);
> >> +	input_set_abs_params(input, ABS_Y, -range, range, 0, 0);
> >> +	input_set_abs_params(input, ABS_Z, -range, range, 0, 0);
> >> +
> >> +	dmard06->range = range;
> >> +}
> >> +
> >> +static int dmard06_request_input_device(struct dmard06_data *dmard06)
> >> +{
> >> +	struct input_dev *input;
> >> +	int error;
> >> +
> >> +	input = devm_input_allocate_device(&dmard06->client->dev);
> >> +	if (!input) {
> >> +		dev_err(&dmard06->client->dev,
> >> +			"Failed to allocate input device\n");
> >> +		return -ENOMEM;
> >> +	}
> >> +
> >> +	dmard06_init_input_device(dmard06, input);
> >> +
> >> +	input->open = dmard06_irq_open;
> >> +	input->close = dmard06_irq_close;
> >> +	input_set_drvdata(input, dmard06);
> >> +
> >> +	error = input_register_device(input);
> >> +	if (error) {
> >> +		dev_err(&dmard06->client->dev,
> >> +			"Failed to register input device: %d", error);
> >> +		return error;
> >> +	}
> >> +
> >> +	dmard06->input = input;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_request_polled_device(struct dmard06_data *dmard06)
> >> +{
> >> +	struct input_polled_dev *input_poll;
> >> +	int error;
> >> +
> >> +	input_poll = devm_input_allocate_polled_device(&dmard06->client->dev);
> >> +	if (!input_poll) {
> >> +		dev_err(&dmard06->client->dev,
> >> +			"Failed to allocate polled input device\n");
> >> +		return -ENOMEM;
> >> +	}
> >> +
> >> +	input_poll->private = dmard06;
> >> +	input_poll->open = dmard06_poll_open;
> >> +	input_poll->close = dmard06_poll_close;
> >> +	input_poll->poll = dmard06_poll;
> >> +	input_poll->poll_interval = DMARD06_POLL_INTERVAL;
> >> +	input_poll->poll_interval_min = DMARD06_POLL_MIN;
> >> +	input_poll->poll_interval_max = DMARD06_POLL_MAX;
> >> +
> >> +	dmard06_init_input_device(dmard06, input_poll->input);
> >> +
> >> +	error = input_register_polled_device(input_poll);
> >> +	if (error) {
> >> +		dev_err(&dmard06->client->dev,
> >> +			"Failed to register polled device: %d", error);
> >> +		return error;
> >> +	}
> >> +
> >> +	dmard06->input_poll = input_poll;
> >> +	dmard06->input = input_poll->input;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_probe(struct i2c_client *client,
> >> +			 const struct i2c_device_id *id)
> >> +{
> >> +	struct dmard06_data *dmard06;
> >> +	int error;
> >> +
> >> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> >> +
> >> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> >> +		dev_err(&client->dev, "I2C check functionality failed\n");
> >> +		return -ENXIO;
> >> +	}
> >> +
> >> +	dmard06 = devm_kzalloc(&client->dev, sizeof(*dmard06), GFP_KERNEL);
> >> +	if (!dmard06)
> >> +		return -ENOMEM;
> >> +
> >> +	dmard06->client = client;
> >> +	i2c_set_clientdata(client, dmard06);
> >> +
> >> +	error = dmard06_reset(dmard06->client);
> >> +	if (error)
> >> +		return error;
> >> +
> >> +	error = dmard06_read_chip_id(dmard06);
> >> +	if (error)
> >> +		return error;
> >> +
> >> +	error = dmard06_set_mode(dmard06, DMARD06_MODE_NORMAL);
> >> +	if (error)
> >> +		return error;
> >> +
> >> +	if (dmard06->client->irq) {
> >> +		error = dmard06_init_irq(dmard06->client);
> >> +		if (error)
> >> +			return error;
> >> +
> >> +		error = dmard06_request_input_device(dmard06);
> >> +		if (error)
> >> +			return error;
> >> +
> >> +		dmard06->irq_flags = IRQ_TYPE_LEVEL_LOW | IRQF_ONESHOT;
> >> +		error = devm_request_threaded_irq(&dmard06->client->dev,
> >> +						  dmard06->client->irq, NULL,
> >> +						  dmard06_irq_handler,
> >> +						  dmard06->irq_flags,
> >> +						  dmard06->client->name,
> >> +						  dmard06);
> >> +		if (error) {
> >> +			dev_err(&dmard06->client->dev,
> >> +				"Request IRQ failed: %d\n", error);
> >> +			return error;
> >> +		}
> >> +
> >> +	} else {
> >> +		error = dmard06_request_polled_device(dmard06);
> >> +		if (error)
> >> +			return error;
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static const struct i2c_device_id dmard06_id[] = {
> >> +	{ "dmard06", 0 },
> >> +	{ }
> >> +};
> >> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> >> +
> >> +static const struct of_device_id dmard06_of_match[] = {
> >> +	{ .compatible = "domintech,dmard06" },
> >> +	{ }
> >> +};
> >> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> >> +
> >> +static struct i2c_driver dmard06_driver = {
> >> +	.probe = dmard06_probe,
> >> +	.class = I2C_CLASS_HWMON,
> >> +	.id_table = dmard06_id,
> >> +	.driver = {
> >> +		.name = "dmard06",
> >> +		.of_match_table = of_match_ptr(dmard06_of_match),
> >> +	},
> >> +};
> >> +module_i2c_driver(dmard06_driver);
> >> +
> >> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> >> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> >> +MODULE_LICENSE("GPL v2");
> >> -- 
> >> 2.7.3
> >>
> > 
> 


-- 
Thanks and regards,
Aleksei Mamlin

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 0/2] Add driver for Domintech DMARD06 accelerometer
@ 2016-07-21 20:32   ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21 20:32 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, devicetree, linux-kernel, linux-iio, Aleksei Mamlin

This patch series adds driver for Domintech DMARD06 low-g tri-axial
digital accelerometer and vendor prefix for Domintech Co., Ltd.

Changes since v1:
 - Rewrite driver to iio subsystem

Aleksei Mamlin (2):
  devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
  iio: accel: Add support for Domintech DMARD06 accelerometer

 .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/iio/accel/Kconfig                          |  11 ++
 drivers/iio/accel/Makefile                         |   1 +
 drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
 5 files changed, 249 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
 create mode 100644 drivers/iio/accel/dmard06.c

-- 
2.7.3

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 0/2] Add driver for Domintech DMARD06 accelerometer
@ 2016-07-21 20:32   ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21 20:32 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA, Aleksei Mamlin

This patch series adds driver for Domintech DMARD06 low-g tri-axial
digital accelerometer and vendor prefix for Domintech Co., Ltd.

Changes since v1:
 - Rewrite driver to iio subsystem

Aleksei Mamlin (2):
  devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
  iio: accel: Add support for Domintech DMARD06 accelerometer

 .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 drivers/iio/accel/Kconfig                          |  11 ++
 drivers/iio/accel/Makefile                         |   1 +
 drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
 5 files changed, 249 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
 create mode 100644 drivers/iio/accel/dmard06.c

-- 
2.7.3

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
@ 2016-07-21 20:32     ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21 20:32 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, devicetree, linux-kernel, linux-iio, Aleksei Mamlin

Add vendor-prefix for Domintech Co., Ltd.

Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index d2bce22..0c12f2d 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -75,6 +75,7 @@ digilent	Diglent, Inc.
 dlg	Dialog Semiconductor
 dlink	D-Link Corporation
 dmo	Data Modul AG
+domintech	Domintech Co., Ltd.
 dptechnics	DPTechnics
 dragino	Dragino Technology Co., Limited
 ea	Embedded Artists AB
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
@ 2016-07-21 20:32     ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21 20:32 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA, Aleksei Mamlin

Add vendor-prefix for Domintech Co., Ltd.

Signed-off-by: Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt
index d2bce22..0c12f2d 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -75,6 +75,7 @@ digilent	Diglent, Inc.
 dlg	Dialog Semiconductor
 dlink	D-Link Corporation
 dmo	Data Modul AG
+domintech	Domintech Co., Ltd.
 dptechnics	DPTechnics
 dragino	Dragino Technology Co., Limited
 ea	Embedded Artists AB
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
  2016-07-21 20:32   ` Aleksei Mamlin
  (?)
  (?)
@ 2016-07-21 20:33   ` Aleksei Mamlin
  2016-07-21 22:19     ` Rob Herring
  2016-07-21 22:41     ` Peter Meerwald-Stadler
  -1 siblings, 2 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-21 20:33 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, devicetree, linux-kernel, linux-iio, Aleksei Mamlin

This patch add support for Domintech DMARD06 accelerometer.

Domintech DMARD06 is a low-g tri-axial digital accelerometer for
cost-sensitive consumer application.

Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
---
 .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
 drivers/iio/accel/Kconfig                          |  11 ++
 drivers/iio/accel/Makefile                         |   1 +
 drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
 4 files changed, 248 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
 create mode 100644 drivers/iio/accel/dmard06.c

diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
new file mode 100644
index 0000000..cc2231a
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
@@ -0,0 +1,17 @@
+Device tree bindings for Domintech DMARD06 acceletometer
+
+Required properties:
+ - compatible		: Should be "domintech,dmard06"
+ - reg			: I2C address of the chip. Should be 0x1c
+
+Example:
+	&i2c1 {
+		/* ... */
+
+		accelerometer@1c {
+			compatible = "domintech,dmard06";
+			reg = <0x1c>;
+		};
+
+		/* ... */
+	};
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index e4a758c..abc8536 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
 	tristate
 	select REGMAP_SPI
 
+config DMARD06
+	tristate "Domintech DMARD06 Digital Accelerometer Driver"
+	depends on OF || COMPILE_TEST
+	depends on I2C
+	help
+	  Say yes here to build support for the Domintech DMARD06 low-g
+	  tri-axial digital accelerometer connected via an I2C bus.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called dmard06.
+
 config HID_SENSOR_ACCEL_3D
 	depends on HID_SENSOR_HUB
 	select IIO_BUFFER
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index 71b6794..a96b269 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
 obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
 obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
 obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
+obj-$(CONFIG_DMARD06)	+= dmard06.o
 obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
 obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
 obj-$(CONFIG_KXSD9)	+= kxsd9.o
diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
new file mode 100644
index 0000000..433e430
--- /dev/null
+++ b/drivers/iio/accel/dmard06.c
@@ -0,0 +1,219 @@
+/*
+ * IIO driver for Domintech DMARD06 accelerometer
+ *
+ * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+
+struct dmard06_data {
+	struct i2c_client *client;
+};
+
+#define DMARD06_DRV_NAME		"dmard06"
+
+/* Device data registers */
+#define DMARD06_CHIP_ID_REG		0x0f
+#define DMARD06_TOUT_REG		0x40
+#define DMARD06_XOUT_REG		0x41
+#define DMARD06_YOUT_REG		0x42
+#define DMARD06_ZOUT_REG		0x43
+
+/* Device ID value */
+#define DMARD06_CHIP_ID			0x06
+
+/* Device values */
+#define DMARD06_AXIS_MAX_VAL		64
+#define DMARD06_AXIS_SCALE_VAL		31250
+#define DMARD06_TEMP_CENTER_VAL		25
+
+static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
+{
+	int error;
+
+	error = i2c_master_send(client, &reg, 1);
+	if (unlikely(error < 0))
+		return error;
+
+	error = i2c_master_recv(client, buf, 1);
+	if (unlikely(error < 0))
+		return error;
+	return 0;
+}
+
+static int dmard06_read_chip_id(struct dmard06_data *dmard06)
+{
+	u8 buf;
+	u8 chip_id;
+	int error;
+
+	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
+	if (error) {
+		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
+			error);
+		return error;
+	}
+
+	chip_id = (buf & 0x00ff);
+	if (chip_id != DMARD06_CHIP_ID) {
+		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
+			chip_id);
+		return -EINVAL;
+	}
+
+	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
+
+	return 0;
+}
+
+static int dmard06_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct dmard06_data *dmard06 = iio_priv(indio_dev);
+	u8 buf;
+	bool reversed;
+	int error;
+	int tmp;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
+		if (error) {
+			dev_err(&dmard06->client->dev,
+				"Failed to read data %d\n", error);
+			return error;
+		}
+
+		reversed = (buf >> 7) & 1;
+		tmp = (int)((buf & 0x7f) >> 1);
+
+		switch (chan->type) {
+		case IIO_ACCEL:
+			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
+			return IIO_VAL_INT;
+		case IIO_TEMP:
+			*val = reversed ? -tmp : tmp;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_OFFSET:
+		switch (chan->type) {
+		case IIO_TEMP:
+			*val = DMARD06_TEMP_CENTER_VAL;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			*val = 0;
+			*val2 = DMARD06_AXIS_SCALE_VAL;
+			return IIO_VAL_INT_PLUS_MICRO;
+		default:
+			return -EINVAL;
+		}
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info dmard06_info = {
+	.driver_module	= THIS_MODULE,
+	.read_raw	= dmard06_read_raw,
+};
+
+#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
+	.type = IIO_ACCEL,					\
+	.address = reg,						\
+	.channel2 = IIO_MOD_##_axis,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
+	.modified = 1,						\
+}
+
+#define DMARD06_TEMP_CHANNEL(reg) {				\
+	.type = IIO_TEMP,					\
+	.address = reg,						\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
+			      BIT(IIO_CHAN_INFO_OFFSET),	\
+}
+
+static const struct iio_chan_spec dmard06_channels[] = {
+	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
+	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
+	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
+	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
+};
+
+static int dmard06_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	int error;
+	struct iio_dev *indio_dev;
+	struct dmard06_data *dmard06;
+
+	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
+		dev_err(&client->dev, "I2C check functionality failed\n");
+		return -ENXIO;
+	}
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
+	if (!indio_dev) {
+		dev_err(&client->dev, "Failed to allocate iio device\n");
+		return -ENOMEM;
+	}
+
+	dmard06 = iio_priv(indio_dev);
+	dmard06->client = client;
+
+	error = dmard06_read_chip_id(dmard06);
+	if (error)
+		return error;
+
+	i2c_set_clientdata(client, indio_dev);
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->name = DMARD06_DRV_NAME;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = dmard06_channels;
+	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
+	indio_dev->info = &dmard06_info;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static const struct i2c_device_id dmard06_id[] = {
+	{ "dmard06", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, dmard06_id);
+
+static const struct of_device_id dmard06_of_match[] = {
+	{ .compatible = "domintech,dmard06" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, dmard06_of_match);
+
+static struct i2c_driver dmard06_driver = {
+	.probe = dmard06_probe,
+	.id_table = dmard06_id,
+	.driver = {
+		.name = DMARD06_DRV_NAME,
+		.of_match_table = of_match_ptr(dmard06_of_match),
+	},
+};
+module_i2c_driver(dmard06_driver);
+
+MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
+MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
+MODULE_LICENSE("GPL v2");
-- 
2.7.3

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
@ 2016-07-21 22:16       ` Rob Herring
  0 siblings, 0 replies; 23+ messages in thread
From: Rob Herring @ 2016-07-21 22:16 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, devicetree, linux-kernel, linux-iio

On Thu, Jul 21, 2016 at 11:32:58PM +0300, Aleksei Mamlin wrote:
> Add vendor-prefix for Domintech Co., Ltd.
> 
> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> ---
>  Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
>  1 file changed, 1 insertion(+)

Please add acks when posting new versions.

Rob

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd.
@ 2016-07-21 22:16       ` Rob Herring
  0 siblings, 0 replies; 23+ messages in thread
From: Rob Herring @ 2016-07-21 22:16 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA

On Thu, Jul 21, 2016 at 11:32:58PM +0300, Aleksei Mamlin wrote:
> Add vendor-prefix for Domintech Co., Ltd.
> 
> Signed-off-by: Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> ---
>  Documentation/devicetree/bindings/vendor-prefixes.txt | 1 +
>  1 file changed, 1 insertion(+)

Please add acks when posting new versions.

Rob

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
  2016-07-21 20:33   ` [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer Aleksei Mamlin
@ 2016-07-21 22:19     ` Rob Herring
  2016-07-21 22:41     ` Peter Meerwald-Stadler
  1 sibling, 0 replies; 23+ messages in thread
From: Rob Herring @ 2016-07-21 22:19 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, devicetree, linux-kernel, linux-iio

On Thu, Jul 21, 2016 at 11:33:11PM +0300, Aleksei Mamlin wrote:
> This patch add support for Domintech DMARD06 accelerometer.
> 
> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> cost-sensitive consumer application.
> 
> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> ---
>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
>  drivers/iio/accel/Kconfig                          |  11 ++
>  drivers/iio/accel/Makefile                         |   1 +
>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
>  4 files changed, 248 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
>  create mode 100644 drivers/iio/accel/dmard06.c
> 
> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> new file mode 100644
> index 0000000..cc2231a
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> @@ -0,0 +1,17 @@
> +Device tree bindings for Domintech DMARD06 acceletometer
> +
> +Required properties:
> + - compatible		: Should be "domintech,dmard06"
> + - reg			: I2C address of the chip. Should be 0x1c

No interrupt? If not, for the binding:

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
  2016-07-21 20:33   ` [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer Aleksei Mamlin
  2016-07-21 22:19     ` Rob Herring
@ 2016-07-21 22:41     ` Peter Meerwald-Stadler
  2016-07-23  7:26         ` Jonathan Cameron
  2016-07-23 21:49         ` Aleksei Mamlin
  1 sibling, 2 replies; 23+ messages in thread
From: Peter Meerwald-Stadler @ 2016-07-21 22:41 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Rob Herring, devicetree, linux-kernel, linux-iio


> This patch add support for Domintech DMARD06 accelerometer.

comments below
 
> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> cost-sensitive consumer application.
> 
> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> ---
>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
>  drivers/iio/accel/Kconfig                          |  11 ++
>  drivers/iio/accel/Makefile                         |   1 +
>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
>  4 files changed, 248 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
>  create mode 100644 drivers/iio/accel/dmard06.c
> 
> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> new file mode 100644
> index 0000000..cc2231a
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> @@ -0,0 +1,17 @@
> +Device tree bindings for Domintech DMARD06 acceletometer

typo: accelerometer

> +
> +Required properties:
> + - compatible		: Should be "domintech,dmard06"
> + - reg			: I2C address of the chip. Should be 0x1c
> +
> +Example:
> +	&i2c1 {
> +		/* ... */
> +
> +		accelerometer@1c {
> +			compatible = "domintech,dmard06";
> +			reg = <0x1c>;
> +		};
> +
> +		/* ... */
> +	};
> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> index e4a758c..abc8536 100644
> --- a/drivers/iio/accel/Kconfig
> +++ b/drivers/iio/accel/Kconfig
> @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
>  	tristate
>  	select REGMAP_SPI
>  
> +config DMARD06
> +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
> +	depends on OF || COMPILE_TEST
> +	depends on I2C
> +	help
> +	  Say yes here to build support for the Domintech DMARD06 low-g
> +	  tri-axial digital accelerometer connected via an I2C bus.
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called dmard06.
> +
>  config HID_SENSOR_ACCEL_3D
>  	depends on HID_SENSOR_HUB
>  	select IIO_BUFFER
> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> index 71b6794..a96b269 100644
> --- a/drivers/iio/accel/Makefile
> +++ b/drivers/iio/accel/Makefile
> @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
>  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
>  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
>  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
> +obj-$(CONFIG_DMARD06)	+= dmard06.o
>  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
>  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
>  obj-$(CONFIG_KXSD9)	+= kxsd9.o
> diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
> new file mode 100644
> index 0000000..433e430
> --- /dev/null
> +++ b/drivers/iio/accel/dmard06.c
> @@ -0,0 +1,219 @@
> +/*
> + * IIO driver for Domintech DMARD06 accelerometer
> + *
> + * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + */
> +
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/iio.h>
> +
> +struct dmard06_data {
> +	struct i2c_client *client;
> +};
> +
> +#define DMARD06_DRV_NAME		"dmard06"
> +
> +/* Device data registers */
> +#define DMARD06_CHIP_ID_REG		0x0f
> +#define DMARD06_TOUT_REG		0x40
> +#define DMARD06_XOUT_REG		0x41
> +#define DMARD06_YOUT_REG		0x42
> +#define DMARD06_ZOUT_REG		0x43
> +
> +/* Device ID value */
> +#define DMARD06_CHIP_ID			0x06
> +
> +/* Device values */
> +#define DMARD06_AXIS_MAX_VAL		64
> +#define DMARD06_AXIS_SCALE_VAL		31250
> +#define DMARD06_TEMP_CENTER_VAL		25
> +
> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> +{
> +	int error;

'error' is a bit pessimistic, how about 'ret'? :-)

> +

can't use i2c_smbus_read_byte_data()?

> +	error = i2c_master_send(client, &reg, 1);
> +	if (unlikely(error < 0))
> +		return error;
> +
> +	error = i2c_master_recv(client, buf, 1);
> +	if (unlikely(error < 0))
> +		return error;

not sure if it is worth to annotate unlikely() here -- i2c is slooow
locking is needed if you really need two i2c_master calls

> +	return 0;
> +}
> +
> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> +{
> +	u8 buf;
> +	u8 chip_id;
> +	int error;
> +
> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> +	if (error) {
> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> +			error);
> +		return error;
> +	}
> +
> +	chip_id = (buf & 0x00ff);

why? buf is u8 anyways

> +	if (chip_id != DMARD06_CHIP_ID) {
> +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
> +			chip_id);
> +		return -EINVAL;

often ENODEV is used for this case

> +	}
> +
> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);

we prefer silent probing of the driver, please don't clutter the log

> +
> +	return 0;
> +}
> +
> +static int dmard06_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
> +	u8 buf;
> +	bool reversed;
> +	int error;
> +	int tmp;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
> +		if (error) {
> +			dev_err(&dmard06->client->dev,
> +				"Failed to read data %d\n", error);
> +			return error;
> +		}
> +
> +		reversed = (buf >> 7) & 1;
> +		tmp = (int)((buf & 0x7f) >> 1);


> +
> +		switch (chan->type) {
> +		case IIO_ACCEL:

wouldn't sign_extend32(buf >> 1, 6) do the job?

> +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
> +			return IIO_VAL_INT;
> +		case IIO_TEMP:
> +			*val = reversed ? -tmp : tmp;
> +			return IIO_VAL_INT;
> +		default:
> +			return -EINVAL;
> +		}
> +	case IIO_CHAN_INFO_OFFSET:
> +		switch (chan->type) {
> +		case IIO_TEMP:
> +			*val = DMARD06_TEMP_CENTER_VAL;
> +			return IIO_VAL_INT;
> +		default:
> +			return -EINVAL;
> +		}
> +	case IIO_CHAN_INFO_SCALE:
> +		switch (chan->type) {
> +		case IIO_ACCEL:
> +			*val = 0;
> +			*val2 = DMARD06_AXIS_SCALE_VAL;
> +			return IIO_VAL_INT_PLUS_MICRO;
> +		default:
> +			return -EINVAL;
> +		}
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static const struct iio_info dmard06_info = {
> +	.driver_module	= THIS_MODULE,
> +	.read_raw	= dmard06_read_raw,
> +};
> +
> +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\

maybe rename reg to _reg

> +	.type = IIO_ACCEL,					\
> +	.address = reg,						\
> +	.channel2 = IIO_MOD_##_axis,				\
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> +	.modified = 1,						\
> +}
> +
> +#define DMARD06_TEMP_CHANNEL(reg) {				\
> +	.type = IIO_TEMP,					\
> +	.address = reg,						\
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> +			      BIT(IIO_CHAN_INFO_OFFSET),	\
> +}
> +
> +static const struct iio_chan_spec dmard06_channels[] = {
> +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
> +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
> +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
> +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
> +};
> +
> +static int dmard06_probe(struct i2c_client *client,
> +			const struct i2c_device_id *id)
> +{
> +	int error;
> +	struct iio_dev *indio_dev;
> +	struct dmard06_data *dmard06;
> +
> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);

not needed

> +
> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> +		dev_err(&client->dev, "I2C check functionality failed\n");
> +		return -ENXIO;
> +	}
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
> +	if (!indio_dev) {
> +		dev_err(&client->dev, "Failed to allocate iio device\n");
> +		return -ENOMEM;
> +	}
> +
> +	dmard06 = iio_priv(indio_dev);
> +	dmard06->client = client;
> +
> +	error = dmard06_read_chip_id(dmard06);
> +	if (error)
> +		return error;
> +
> +	i2c_set_clientdata(client, indio_dev);
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->name = DMARD06_DRV_NAME;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = dmard06_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
> +	indio_dev->info = &dmard06_info;
> +
> +	return devm_iio_device_register(&client->dev, indio_dev);
> +}
> +
> +static const struct i2c_device_id dmard06_id[] = {
> +	{ "dmard06", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> +
> +static const struct of_device_id dmard06_of_match[] = {
> +	{ .compatible = "domintech,dmard06" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> +
> +static struct i2c_driver dmard06_driver = {
> +	.probe = dmard06_probe,
> +	.id_table = dmard06_id,
> +	.driver = {
> +		.name = DMARD06_DRV_NAME,
> +		.of_match_table = of_match_ptr(dmard06_of_match),
> +	},
> +};
> +module_i2c_driver(dmard06_driver);
> +
> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> +MODULE_LICENSE("GPL v2");
> 

-- 

Peter Meerwald-Stadler
+43-664-2444418 (mobile)

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
@ 2016-07-23  7:26         ` Jonathan Cameron
  0 siblings, 0 replies; 23+ messages in thread
From: Jonathan Cameron @ 2016-07-23  7:26 UTC (permalink / raw)
  To: Peter Meerwald-Stadler, Aleksei Mamlin
  Cc: Hartmut Knaack, Lars-Peter Clausen, Rob Herring, devicetree,
	linux-kernel, linux-iio

On 22/07/16 00:41, Peter Meerwald-Stadler wrote:
> 
>> This patch add support for Domintech DMARD06 accelerometer.
> 
> comments below
>  
>> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
>> cost-sensitive consumer application.
>>
>> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
I don't have anything to add here. Nice fairly clean first version.
>> ---
>>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
>>  drivers/iio/accel/Kconfig                          |  11 ++
>>  drivers/iio/accel/Makefile                         |   1 +
>>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
>>  4 files changed, 248 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
>>  create mode 100644 drivers/iio/accel/dmard06.c
>>
>> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
>> new file mode 100644
>> index 0000000..cc2231a
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
>> @@ -0,0 +1,17 @@
>> +Device tree bindings for Domintech DMARD06 acceletometer
> 
> typo: accelerometer
> 
>> +
>> +Required properties:
>> + - compatible		: Should be "domintech,dmard06"
>> + - reg			: I2C address of the chip. Should be 0x1c
>> +
>> +Example:
>> +	&i2c1 {
>> +		/* ... */
>> +
>> +		accelerometer@1c {
>> +			compatible = "domintech,dmard06";
>> +			reg = <0x1c>;
>> +		};
>> +
>> +		/* ... */
>> +	};
>> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
>> index e4a758c..abc8536 100644
>> --- a/drivers/iio/accel/Kconfig
>> +++ b/drivers/iio/accel/Kconfig
>> @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
>>  	tristate
>>  	select REGMAP_SPI
>>  
>> +config DMARD06
>> +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
>> +	depends on OF || COMPILE_TEST
>> +	depends on I2C
>> +	help
>> +	  Say yes here to build support for the Domintech DMARD06 low-g
>> +	  tri-axial digital accelerometer connected via an I2C bus.
>> +
>> +	  To compile this driver as a module, choose M here: the
>> +	  module will be called dmard06.
>> +
>>  config HID_SENSOR_ACCEL_3D
>>  	depends on HID_SENSOR_HUB
>>  	select IIO_BUFFER
>> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
>> index 71b6794..a96b269 100644
>> --- a/drivers/iio/accel/Makefile
>> +++ b/drivers/iio/accel/Makefile
>> @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
>>  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
>>  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
>>  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
>> +obj-$(CONFIG_DMARD06)	+= dmard06.o
>>  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
>>  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
>>  obj-$(CONFIG_KXSD9)	+= kxsd9.o
>> diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
>> new file mode 100644
>> index 0000000..433e430
>> --- /dev/null
>> +++ b/drivers/iio/accel/dmard06.c
>> @@ -0,0 +1,219 @@
>> +/*
>> + * IIO driver for Domintech DMARD06 accelerometer
>> + *
>> + * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify it
>> + * under the terms and conditions of the GNU General Public License,
>> + * version 2, as published by the Free Software Foundation.
>> + */
>> +
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/iio/iio.h>
>> +
>> +struct dmard06_data {
>> +	struct i2c_client *client;
>> +};
>> +
>> +#define DMARD06_DRV_NAME		"dmard06"
>> +
>> +/* Device data registers */
>> +#define DMARD06_CHIP_ID_REG		0x0f
>> +#define DMARD06_TOUT_REG		0x40
>> +#define DMARD06_XOUT_REG		0x41
>> +#define DMARD06_YOUT_REG		0x42
>> +#define DMARD06_ZOUT_REG		0x43
>> +
>> +/* Device ID value */
>> +#define DMARD06_CHIP_ID			0x06
>> +
>> +/* Device values */
>> +#define DMARD06_AXIS_MAX_VAL		64
>> +#define DMARD06_AXIS_SCALE_VAL		31250
>> +#define DMARD06_TEMP_CENTER_VAL		25
>> +
>> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
>> +{
>> +	int error;
> 
> 'error' is a bit pessimistic, how about 'ret'? :-)
> 
>> +
> 
> can't use i2c_smbus_read_byte_data()?
> 
>> +	error = i2c_master_send(client, &reg, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
>> +
>> +	error = i2c_master_recv(client, buf, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
> 
> not sure if it is worth to annotate unlikely() here -- i2c is slooow
> locking is needed if you really need two i2c_master calls
> 
>> +	return 0;
>> +}
>> +
>> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
>> +{
>> +	u8 buf;
>> +	u8 chip_id;
>> +	int error;
>> +
>> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	chip_id = (buf & 0x00ff);
> 
> why? buf is u8 anyways
> 
>> +	if (chip_id != DMARD06_CHIP_ID) {
>> +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
>> +			chip_id);
>> +		return -EINVAL;
> 
> often ENODEV is used for this case
> 
>> +	}
>> +
>> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> 
> we prefer silent probing of the driver, please don't clutter the log
> 
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_read_raw(struct iio_dev *indio_dev,
>> +			    struct iio_chan_spec const *chan,
>> +			    int *val, int *val2, long mask)
>> +{
>> +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
>> +	u8 buf;
>> +	bool reversed;
>> +	int error;
>> +	int tmp;
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_RAW:
>> +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
>> +		if (error) {
>> +			dev_err(&dmard06->client->dev,
>> +				"Failed to read data %d\n", error);
>> +			return error;
>> +		}
>> +
>> +		reversed = (buf >> 7) & 1;
>> +		tmp = (int)((buf & 0x7f) >> 1);
> 
> 
>> +
>> +		switch (chan->type) {
>> +		case IIO_ACCEL:
> 
> wouldn't sign_extend32(buf >> 1, 6) do the job?
> 
>> +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
>> +			return IIO_VAL_INT;
>> +		case IIO_TEMP:
>> +			*val = reversed ? -tmp : tmp;
>> +			return IIO_VAL_INT;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	case IIO_CHAN_INFO_OFFSET:
>> +		switch (chan->type) {
>> +		case IIO_TEMP:
>> +			*val = DMARD06_TEMP_CENTER_VAL;
>> +			return IIO_VAL_INT;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	case IIO_CHAN_INFO_SCALE:
>> +		switch (chan->type) {
>> +		case IIO_ACCEL:
>> +			*val = 0;
>> +			*val2 = DMARD06_AXIS_SCALE_VAL;
>> +			return IIO_VAL_INT_PLUS_MICRO;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static const struct iio_info dmard06_info = {
>> +	.driver_module	= THIS_MODULE,
>> +	.read_raw	= dmard06_read_raw,
>> +};
>> +
>> +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> 
> maybe rename reg to _reg
> 
>> +	.type = IIO_ACCEL,					\
>> +	.address = reg,						\
>> +	.channel2 = IIO_MOD_##_axis,				\
>> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
>> +	.modified = 1,						\
>> +}
>> +
>> +#define DMARD06_TEMP_CHANNEL(reg) {				\
>> +	.type = IIO_TEMP,					\
>> +	.address = reg,						\
>> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
>> +			      BIT(IIO_CHAN_INFO_OFFSET),	\
>> +}
>> +
>> +static const struct iio_chan_spec dmard06_channels[] = {
>> +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
>> +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
>> +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
>> +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
>> +};
>> +
>> +static int dmard06_probe(struct i2c_client *client,
>> +			const struct i2c_device_id *id)
>> +{
>> +	int error;
>> +	struct iio_dev *indio_dev;
>> +	struct dmard06_data *dmard06;
>> +
>> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> 
> not needed
> 
>> +
>> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
>> +		dev_err(&client->dev, "I2C check functionality failed\n");
>> +		return -ENXIO;
>> +	}
>> +
>> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
>> +	if (!indio_dev) {
>> +		dev_err(&client->dev, "Failed to allocate iio device\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	dmard06 = iio_priv(indio_dev);
>> +	dmard06->client = client;
>> +
>> +	error = dmard06_read_chip_id(dmard06);
>> +	if (error)
>> +		return error;
>> +
>> +	i2c_set_clientdata(client, indio_dev);
>> +	indio_dev->dev.parent = &client->dev;
>> +	indio_dev->name = DMARD06_DRV_NAME;
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +	indio_dev->channels = dmard06_channels;
>> +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
>> +	indio_dev->info = &dmard06_info;
>> +
>> +	return devm_iio_device_register(&client->dev, indio_dev);
>> +}
>> +
>> +static const struct i2c_device_id dmard06_id[] = {
>> +	{ "dmard06", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
>> +
>> +static const struct of_device_id dmard06_of_match[] = {
>> +	{ .compatible = "domintech,dmard06" },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
>> +
>> +static struct i2c_driver dmard06_driver = {
>> +	.probe = dmard06_probe,
>> +	.id_table = dmard06_id,
>> +	.driver = {
>> +		.name = DMARD06_DRV_NAME,
>> +		.of_match_table = of_match_ptr(dmard06_of_match),
>> +	},
>> +};
>> +module_i2c_driver(dmard06_driver);
>> +
>> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
>> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
>> +MODULE_LICENSE("GPL v2");
>>
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
@ 2016-07-23  7:26         ` Jonathan Cameron
  0 siblings, 0 replies; 23+ messages in thread
From: Jonathan Cameron @ 2016-07-23  7:26 UTC (permalink / raw)
  To: Peter Meerwald-Stadler, Aleksei Mamlin
  Cc: Hartmut Knaack, Lars-Peter Clausen, Rob Herring,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA

On 22/07/16 00:41, Peter Meerwald-Stadler wrote:
> 
>> This patch add support for Domintech DMARD06 accelerometer.
> 
> comments below
>  
>> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
>> cost-sensitive consumer application.
>>
>> Signed-off-by: Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
I don't have anything to add here. Nice fairly clean first version.
>> ---
>>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
>>  drivers/iio/accel/Kconfig                          |  11 ++
>>  drivers/iio/accel/Makefile                         |   1 +
>>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
>>  4 files changed, 248 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
>>  create mode 100644 drivers/iio/accel/dmard06.c
>>
>> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
>> new file mode 100644
>> index 0000000..cc2231a
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
>> @@ -0,0 +1,17 @@
>> +Device tree bindings for Domintech DMARD06 acceletometer
> 
> typo: accelerometer
> 
>> +
>> +Required properties:
>> + - compatible		: Should be "domintech,dmard06"
>> + - reg			: I2C address of the chip. Should be 0x1c
>> +
>> +Example:
>> +	&i2c1 {
>> +		/* ... */
>> +
>> +		accelerometer@1c {
>> +			compatible = "domintech,dmard06";
>> +			reg = <0x1c>;
>> +		};
>> +
>> +		/* ... */
>> +	};
>> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
>> index e4a758c..abc8536 100644
>> --- a/drivers/iio/accel/Kconfig
>> +++ b/drivers/iio/accel/Kconfig
>> @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
>>  	tristate
>>  	select REGMAP_SPI
>>  
>> +config DMARD06
>> +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
>> +	depends on OF || COMPILE_TEST
>> +	depends on I2C
>> +	help
>> +	  Say yes here to build support for the Domintech DMARD06 low-g
>> +	  tri-axial digital accelerometer connected via an I2C bus.
>> +
>> +	  To compile this driver as a module, choose M here: the
>> +	  module will be called dmard06.
>> +
>>  config HID_SENSOR_ACCEL_3D
>>  	depends on HID_SENSOR_HUB
>>  	select IIO_BUFFER
>> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
>> index 71b6794..a96b269 100644
>> --- a/drivers/iio/accel/Makefile
>> +++ b/drivers/iio/accel/Makefile
>> @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
>>  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
>>  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
>>  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
>> +obj-$(CONFIG_DMARD06)	+= dmard06.o
>>  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
>>  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
>>  obj-$(CONFIG_KXSD9)	+= kxsd9.o
>> diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
>> new file mode 100644
>> index 0000000..433e430
>> --- /dev/null
>> +++ b/drivers/iio/accel/dmard06.c
>> @@ -0,0 +1,219 @@
>> +/*
>> + * IIO driver for Domintech DMARD06 accelerometer
>> + *
>> + * Copyright (C) 2016 Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>> + *
>> + * This program is free software; you can redistribute it and/or modify it
>> + * under the terms and conditions of the GNU General Public License,
>> + * version 2, as published by the Free Software Foundation.
>> + */
>> +
>> +#include <linux/module.h>
>> +#include <linux/i2c.h>
>> +#include <linux/iio/iio.h>
>> +
>> +struct dmard06_data {
>> +	struct i2c_client *client;
>> +};
>> +
>> +#define DMARD06_DRV_NAME		"dmard06"
>> +
>> +/* Device data registers */
>> +#define DMARD06_CHIP_ID_REG		0x0f
>> +#define DMARD06_TOUT_REG		0x40
>> +#define DMARD06_XOUT_REG		0x41
>> +#define DMARD06_YOUT_REG		0x42
>> +#define DMARD06_ZOUT_REG		0x43
>> +
>> +/* Device ID value */
>> +#define DMARD06_CHIP_ID			0x06
>> +
>> +/* Device values */
>> +#define DMARD06_AXIS_MAX_VAL		64
>> +#define DMARD06_AXIS_SCALE_VAL		31250
>> +#define DMARD06_TEMP_CENTER_VAL		25
>> +
>> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
>> +{
>> +	int error;
> 
> 'error' is a bit pessimistic, how about 'ret'? :-)
> 
>> +
> 
> can't use i2c_smbus_read_byte_data()?
> 
>> +	error = i2c_master_send(client, &reg, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
>> +
>> +	error = i2c_master_recv(client, buf, 1);
>> +	if (unlikely(error < 0))
>> +		return error;
> 
> not sure if it is worth to annotate unlikely() here -- i2c is slooow
> locking is needed if you really need two i2c_master calls
> 
>> +	return 0;
>> +}
>> +
>> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
>> +{
>> +	u8 buf;
>> +	u8 chip_id;
>> +	int error;
>> +
>> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
>> +	if (error) {
>> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
>> +			error);
>> +		return error;
>> +	}
>> +
>> +	chip_id = (buf & 0x00ff);
> 
> why? buf is u8 anyways
> 
>> +	if (chip_id != DMARD06_CHIP_ID) {
>> +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
>> +			chip_id);
>> +		return -EINVAL;
> 
> often ENODEV is used for this case
> 
>> +	}
>> +
>> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> 
> we prefer silent probing of the driver, please don't clutter the log
> 
>> +
>> +	return 0;
>> +}
>> +
>> +static int dmard06_read_raw(struct iio_dev *indio_dev,
>> +			    struct iio_chan_spec const *chan,
>> +			    int *val, int *val2, long mask)
>> +{
>> +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
>> +	u8 buf;
>> +	bool reversed;
>> +	int error;
>> +	int tmp;
>> +
>> +	switch (mask) {
>> +	case IIO_CHAN_INFO_RAW:
>> +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
>> +		if (error) {
>> +			dev_err(&dmard06->client->dev,
>> +				"Failed to read data %d\n", error);
>> +			return error;
>> +		}
>> +
>> +		reversed = (buf >> 7) & 1;
>> +		tmp = (int)((buf & 0x7f) >> 1);
> 
> 
>> +
>> +		switch (chan->type) {
>> +		case IIO_ACCEL:
> 
> wouldn't sign_extend32(buf >> 1, 6) do the job?
> 
>> +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
>> +			return IIO_VAL_INT;
>> +		case IIO_TEMP:
>> +			*val = reversed ? -tmp : tmp;
>> +			return IIO_VAL_INT;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	case IIO_CHAN_INFO_OFFSET:
>> +		switch (chan->type) {
>> +		case IIO_TEMP:
>> +			*val = DMARD06_TEMP_CENTER_VAL;
>> +			return IIO_VAL_INT;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	case IIO_CHAN_INFO_SCALE:
>> +		switch (chan->type) {
>> +		case IIO_ACCEL:
>> +			*val = 0;
>> +			*val2 = DMARD06_AXIS_SCALE_VAL;
>> +			return IIO_VAL_INT_PLUS_MICRO;
>> +		default:
>> +			return -EINVAL;
>> +		}
>> +	default:
>> +		return -EINVAL;
>> +	}
>> +}
>> +
>> +static const struct iio_info dmard06_info = {
>> +	.driver_module	= THIS_MODULE,
>> +	.read_raw	= dmard06_read_raw,
>> +};
>> +
>> +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> 
> maybe rename reg to _reg
> 
>> +	.type = IIO_ACCEL,					\
>> +	.address = reg,						\
>> +	.channel2 = IIO_MOD_##_axis,				\
>> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
>> +	.modified = 1,						\
>> +}
>> +
>> +#define DMARD06_TEMP_CHANNEL(reg) {				\
>> +	.type = IIO_TEMP,					\
>> +	.address = reg,						\
>> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
>> +			      BIT(IIO_CHAN_INFO_OFFSET),	\
>> +}
>> +
>> +static const struct iio_chan_spec dmard06_channels[] = {
>> +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
>> +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
>> +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
>> +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
>> +};
>> +
>> +static int dmard06_probe(struct i2c_client *client,
>> +			const struct i2c_device_id *id)
>> +{
>> +	int error;
>> +	struct iio_dev *indio_dev;
>> +	struct dmard06_data *dmard06;
>> +
>> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> 
> not needed
> 
>> +
>> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
>> +		dev_err(&client->dev, "I2C check functionality failed\n");
>> +		return -ENXIO;
>> +	}
>> +
>> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
>> +	if (!indio_dev) {
>> +		dev_err(&client->dev, "Failed to allocate iio device\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	dmard06 = iio_priv(indio_dev);
>> +	dmard06->client = client;
>> +
>> +	error = dmard06_read_chip_id(dmard06);
>> +	if (error)
>> +		return error;
>> +
>> +	i2c_set_clientdata(client, indio_dev);
>> +	indio_dev->dev.parent = &client->dev;
>> +	indio_dev->name = DMARD06_DRV_NAME;
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +	indio_dev->channels = dmard06_channels;
>> +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
>> +	indio_dev->info = &dmard06_info;
>> +
>> +	return devm_iio_device_register(&client->dev, indio_dev);
>> +}
>> +
>> +static const struct i2c_device_id dmard06_id[] = {
>> +	{ "dmard06", 0 },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
>> +
>> +static const struct of_device_id dmard06_of_match[] = {
>> +	{ .compatible = "domintech,dmard06" },
>> +	{ }
>> +};
>> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
>> +
>> +static struct i2c_driver dmard06_driver = {
>> +	.probe = dmard06_probe,
>> +	.id_table = dmard06_id,
>> +	.driver = {
>> +		.name = DMARD06_DRV_NAME,
>> +		.of_match_table = of_match_ptr(dmard06_of_match),
>> +	},
>> +};
>> +module_i2c_driver(dmard06_driver);
>> +
>> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>");
>> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
>> +MODULE_LICENSE("GPL v2");
>>
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
@ 2016-07-23 21:49         ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-23 21:49 UTC (permalink / raw)
  To: Peter Meerwald-Stadler
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Rob Herring, devicetree, linux-kernel, linux-iio

On Fri, 22 Jul 2016 00:41:39 +0200 (CEST)
Peter Meerwald-Stadler <pmeerw@pmeerw.net> wrote:

> 
> > This patch add support for Domintech DMARD06 accelerometer.
> 
> comments below
>  
> > Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> > cost-sensitive consumer application.
> > 
> > Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> > ---
> >  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
> >  drivers/iio/accel/Kconfig                          |  11 ++
> >  drivers/iio/accel/Makefile                         |   1 +
> >  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
> >  4 files changed, 248 insertions(+)
> >  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
> >  create mode 100644 drivers/iio/accel/dmard06.c
> > 
> > diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > new file mode 100644
> > index 0000000..cc2231a
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > @@ -0,0 +1,17 @@
> > +Device tree bindings for Domintech DMARD06 acceletometer
> 
> typo: accelerometer
> 
> > +
> > +Required properties:
> > + - compatible		: Should be "domintech,dmard06"
> > + - reg			: I2C address of the chip. Should be 0x1c
> > +
> > +Example:
> > +	&i2c1 {
> > +		/* ... */
> > +
> > +		accelerometer@1c {
> > +			compatible = "domintech,dmard06";
> > +			reg = <0x1c>;
> > +		};
> > +
> > +		/* ... */
> > +	};
> > diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> > index e4a758c..abc8536 100644
> > --- a/drivers/iio/accel/Kconfig
> > +++ b/drivers/iio/accel/Kconfig
> > @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
> >  	tristate
> >  	select REGMAP_SPI
> >  
> > +config DMARD06
> > +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
> > +	depends on OF || COMPILE_TEST
> > +	depends on I2C
> > +	help
> > +	  Say yes here to build support for the Domintech DMARD06 low-g
> > +	  tri-axial digital accelerometer connected via an I2C bus.
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called dmard06.
> > +
> >  config HID_SENSOR_ACCEL_3D
> >  	depends on HID_SENSOR_HUB
> >  	select IIO_BUFFER
> > diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> > index 71b6794..a96b269 100644
> > --- a/drivers/iio/accel/Makefile
> > +++ b/drivers/iio/accel/Makefile
> > @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
> >  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
> >  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
> >  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
> > +obj-$(CONFIG_DMARD06)	+= dmard06.o
> >  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
> >  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
> >  obj-$(CONFIG_KXSD9)	+= kxsd9.o
> > diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
> > new file mode 100644
> > index 0000000..433e430
> > --- /dev/null
> > +++ b/drivers/iio/accel/dmard06.c
> > @@ -0,0 +1,219 @@
> > +/*
> > + * IIO driver for Domintech DMARD06 accelerometer
> > + *
> > + * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> > + *
> > + * This program is free software; you can redistribute it and/or modify it
> > + * under the terms and conditions of the GNU General Public License,
> > + * version 2, as published by the Free Software Foundation.
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/i2c.h>
> > +#include <linux/iio/iio.h>
> > +
> > +struct dmard06_data {
> > +	struct i2c_client *client;
> > +};
> > +
> > +#define DMARD06_DRV_NAME		"dmard06"
> > +
> > +/* Device data registers */
> > +#define DMARD06_CHIP_ID_REG		0x0f
> > +#define DMARD06_TOUT_REG		0x40
> > +#define DMARD06_XOUT_REG		0x41
> > +#define DMARD06_YOUT_REG		0x42
> > +#define DMARD06_ZOUT_REG		0x43
> > +
> > +/* Device ID value */
> > +#define DMARD06_CHIP_ID			0x06
> > +
> > +/* Device values */
> > +#define DMARD06_AXIS_MAX_VAL		64
> > +#define DMARD06_AXIS_SCALE_VAL		31250
> > +#define DMARD06_TEMP_CENTER_VAL		25
> > +
> > +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> > +{
> > +	int error;
> 
> 'error' is a bit pessimistic, how about 'ret'? :-)
> 
> > +
> 
> can't use i2c_smbus_read_byte_data()?
> 

I'll use it in next patch version

> 
> > +	error = i2c_master_send(client, &reg, 1);
> > +	if (unlikely(error < 0))
> > +		return error;
> > +
> > +	error = i2c_master_recv(client, buf, 1);
> > +	if (unlikely(error < 0))
> > +		return error;
> 
> not sure if it is worth to annotate unlikely() here -- i2c is slooow
> locking is needed if you really need two i2c_master calls
> 
> > +	return 0;
> > +}
> > +
> > +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> > +{
> > +	u8 buf;
> > +	u8 chip_id;
> > +	int error;
> > +
> > +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> > +	if (error) {
> > +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> > +			error);
> > +		return error;
> > +	}
> > +
> > +	chip_id = (buf & 0x00ff);
> 
> why? buf is u8 anyways
> 
> > +	if (chip_id != DMARD06_CHIP_ID) {
> > +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
> > +			chip_id);
> > +		return -EINVAL;
> 
> often ENODEV is used for this case
> 
> > +	}
> > +
> > +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> 
> we prefer silent probing of the driver, please don't clutter the log
> 
> > +
> > +	return 0;
> > +}
> > +
> > +static int dmard06_read_raw(struct iio_dev *indio_dev,
> > +			    struct iio_chan_spec const *chan,
> > +			    int *val, int *val2, long mask)
> > +{
> > +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
> > +	u8 buf;
> > +	bool reversed;
> > +	int error;
> > +	int tmp;
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW:
> > +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
> > +		if (error) {
> > +			dev_err(&dmard06->client->dev,
> > +				"Failed to read data %d\n", error);
> > +			return error;
> > +		}
> > +
> > +		reversed = (buf >> 7) & 1;
> > +		tmp = (int)((buf & 0x7f) >> 1);
> 
> 
> > +
> > +		switch (chan->type) {
> > +		case IIO_ACCEL:
> 
> wouldn't sign_extend32(buf >> 1, 6) do the job?
> 

You are right, this do the job for axis and temperature. Thanks

> 
> > +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
> > +			return IIO_VAL_INT;
> > +		case IIO_TEMP:
> > +			*val = reversed ? -tmp : tmp;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_OFFSET:
> > +		switch (chan->type) {
> > +		case IIO_TEMP:
> > +			*val = DMARD06_TEMP_CENTER_VAL;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_SCALE:
> > +		switch (chan->type) {
> > +		case IIO_ACCEL:
> > +			*val = 0;
> > +			*val2 = DMARD06_AXIS_SCALE_VAL;
> > +			return IIO_VAL_INT_PLUS_MICRO;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> > +static const struct iio_info dmard06_info = {
> > +	.driver_module	= THIS_MODULE,
> > +	.read_raw	= dmard06_read_raw,
> > +};
> > +
> > +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> 
> maybe rename reg to _reg
> 
> > +	.type = IIO_ACCEL,					\
> > +	.address = reg,						\
> > +	.channel2 = IIO_MOD_##_axis,				\
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> > +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> > +	.modified = 1,						\
> > +}
> > +
> > +#define DMARD06_TEMP_CHANNEL(reg) {				\
> > +	.type = IIO_TEMP,					\
> > +	.address = reg,						\
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> > +			      BIT(IIO_CHAN_INFO_OFFSET),	\
> > +}
> > +
> > +static const struct iio_chan_spec dmard06_channels[] = {
> > +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
> > +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
> > +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
> > +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
> > +};
> > +
> > +static int dmard06_probe(struct i2c_client *client,
> > +			const struct i2c_device_id *id)
> > +{
> > +	int error;
> > +	struct iio_dev *indio_dev;
> > +	struct dmard06_data *dmard06;
> > +
> > +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> 
> not needed
> 
> > +
> > +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> > +		dev_err(&client->dev, "I2C check functionality failed\n");
> > +		return -ENXIO;
> > +	}
> > +
> > +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
> > +	if (!indio_dev) {
> > +		dev_err(&client->dev, "Failed to allocate iio device\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	dmard06 = iio_priv(indio_dev);
> > +	dmard06->client = client;
> > +
> > +	error = dmard06_read_chip_id(dmard06);
> > +	if (error)
> > +		return error;
> > +
> > +	i2c_set_clientdata(client, indio_dev);
> > +	indio_dev->dev.parent = &client->dev;
> > +	indio_dev->name = DMARD06_DRV_NAME;
> > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > +	indio_dev->channels = dmard06_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
> > +	indio_dev->info = &dmard06_info;
> > +
> > +	return devm_iio_device_register(&client->dev, indio_dev);
> > +}
> > +
> > +static const struct i2c_device_id dmard06_id[] = {
> > +	{ "dmard06", 0 },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> > +
> > +static const struct of_device_id dmard06_of_match[] = {
> > +	{ .compatible = "domintech,dmard06" },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> > +
> > +static struct i2c_driver dmard06_driver = {
> > +	.probe = dmard06_probe,
> > +	.id_table = dmard06_id,
> > +	.driver = {
> > +		.name = DMARD06_DRV_NAME,
> > +		.of_match_table = of_match_ptr(dmard06_of_match),
> > +	},
> > +};
> > +module_i2c_driver(dmard06_driver);
> > +
> > +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> > +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> > +MODULE_LICENSE("GPL v2");
> > 
> 
> -- 
> 
> Peter Meerwald-Stadler
> +43-664-2444418 (mobile)


-- 
Thanks and regards,
Aleksei Mamlin

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
@ 2016-07-23 21:49         ` Aleksei Mamlin
  0 siblings, 0 replies; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-23 21:49 UTC (permalink / raw)
  To: Peter Meerwald-Stadler
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Rob Herring, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-iio-u79uwXL29TY76Z2rM5mHXA

On Fri, 22 Jul 2016 00:41:39 +0200 (CEST)
Peter Meerwald-Stadler <pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org> wrote:

> 
> > This patch add support for Domintech DMARD06 accelerometer.
> 
> comments below
>  
> > Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> > cost-sensitive consumer application.
> > 
> > Signed-off-by: Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > ---
> >  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
> >  drivers/iio/accel/Kconfig                          |  11 ++
> >  drivers/iio/accel/Makefile                         |   1 +
> >  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
> >  4 files changed, 248 insertions(+)
> >  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
> >  create mode 100644 drivers/iio/accel/dmard06.c
> > 
> > diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > new file mode 100644
> > index 0000000..cc2231a
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > @@ -0,0 +1,17 @@
> > +Device tree bindings for Domintech DMARD06 acceletometer
> 
> typo: accelerometer
> 
> > +
> > +Required properties:
> > + - compatible		: Should be "domintech,dmard06"
> > + - reg			: I2C address of the chip. Should be 0x1c
> > +
> > +Example:
> > +	&i2c1 {
> > +		/* ... */
> > +
> > +		accelerometer@1c {
> > +			compatible = "domintech,dmard06";
> > +			reg = <0x1c>;
> > +		};
> > +
> > +		/* ... */
> > +	};
> > diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> > index e4a758c..abc8536 100644
> > --- a/drivers/iio/accel/Kconfig
> > +++ b/drivers/iio/accel/Kconfig
> > @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
> >  	tristate
> >  	select REGMAP_SPI
> >  
> > +config DMARD06
> > +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
> > +	depends on OF || COMPILE_TEST
> > +	depends on I2C
> > +	help
> > +	  Say yes here to build support for the Domintech DMARD06 low-g
> > +	  tri-axial digital accelerometer connected via an I2C bus.
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called dmard06.
> > +
> >  config HID_SENSOR_ACCEL_3D
> >  	depends on HID_SENSOR_HUB
> >  	select IIO_BUFFER
> > diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> > index 71b6794..a96b269 100644
> > --- a/drivers/iio/accel/Makefile
> > +++ b/drivers/iio/accel/Makefile
> > @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
> >  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
> >  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
> >  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
> > +obj-$(CONFIG_DMARD06)	+= dmard06.o
> >  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
> >  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
> >  obj-$(CONFIG_KXSD9)	+= kxsd9.o
> > diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
> > new file mode 100644
> > index 0000000..433e430
> > --- /dev/null
> > +++ b/drivers/iio/accel/dmard06.c
> > @@ -0,0 +1,219 @@
> > +/*
> > + * IIO driver for Domintech DMARD06 accelerometer
> > + *
> > + * Copyright (C) 2016 Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > + *
> > + * This program is free software; you can redistribute it and/or modify it
> > + * under the terms and conditions of the GNU General Public License,
> > + * version 2, as published by the Free Software Foundation.
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/i2c.h>
> > +#include <linux/iio/iio.h>
> > +
> > +struct dmard06_data {
> > +	struct i2c_client *client;
> > +};
> > +
> > +#define DMARD06_DRV_NAME		"dmard06"
> > +
> > +/* Device data registers */
> > +#define DMARD06_CHIP_ID_REG		0x0f
> > +#define DMARD06_TOUT_REG		0x40
> > +#define DMARD06_XOUT_REG		0x41
> > +#define DMARD06_YOUT_REG		0x42
> > +#define DMARD06_ZOUT_REG		0x43
> > +
> > +/* Device ID value */
> > +#define DMARD06_CHIP_ID			0x06
> > +
> > +/* Device values */
> > +#define DMARD06_AXIS_MAX_VAL		64
> > +#define DMARD06_AXIS_SCALE_VAL		31250
> > +#define DMARD06_TEMP_CENTER_VAL		25
> > +
> > +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> > +{
> > +	int error;
> 
> 'error' is a bit pessimistic, how about 'ret'? :-)
> 
> > +
> 
> can't use i2c_smbus_read_byte_data()?
> 

I'll use it in next patch version

> 
> > +	error = i2c_master_send(client, &reg, 1);
> > +	if (unlikely(error < 0))
> > +		return error;
> > +
> > +	error = i2c_master_recv(client, buf, 1);
> > +	if (unlikely(error < 0))
> > +		return error;
> 
> not sure if it is worth to annotate unlikely() here -- i2c is slooow
> locking is needed if you really need two i2c_master calls
> 
> > +	return 0;
> > +}
> > +
> > +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> > +{
> > +	u8 buf;
> > +	u8 chip_id;
> > +	int error;
> > +
> > +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> > +	if (error) {
> > +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> > +			error);
> > +		return error;
> > +	}
> > +
> > +	chip_id = (buf & 0x00ff);
> 
> why? buf is u8 anyways
> 
> > +	if (chip_id != DMARD06_CHIP_ID) {
> > +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
> > +			chip_id);
> > +		return -EINVAL;
> 
> often ENODEV is used for this case
> 
> > +	}
> > +
> > +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> 
> we prefer silent probing of the driver, please don't clutter the log
> 
> > +
> > +	return 0;
> > +}
> > +
> > +static int dmard06_read_raw(struct iio_dev *indio_dev,
> > +			    struct iio_chan_spec const *chan,
> > +			    int *val, int *val2, long mask)
> > +{
> > +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
> > +	u8 buf;
> > +	bool reversed;
> > +	int error;
> > +	int tmp;
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW:
> > +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
> > +		if (error) {
> > +			dev_err(&dmard06->client->dev,
> > +				"Failed to read data %d\n", error);
> > +			return error;
> > +		}
> > +
> > +		reversed = (buf >> 7) & 1;
> > +		tmp = (int)((buf & 0x7f) >> 1);
> 
> 
> > +
> > +		switch (chan->type) {
> > +		case IIO_ACCEL:
> 
> wouldn't sign_extend32(buf >> 1, 6) do the job?
> 

You are right, this do the job for axis and temperature. Thanks

> 
> > +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
> > +			return IIO_VAL_INT;
> > +		case IIO_TEMP:
> > +			*val = reversed ? -tmp : tmp;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_OFFSET:
> > +		switch (chan->type) {
> > +		case IIO_TEMP:
> > +			*val = DMARD06_TEMP_CENTER_VAL;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_SCALE:
> > +		switch (chan->type) {
> > +		case IIO_ACCEL:
> > +			*val = 0;
> > +			*val2 = DMARD06_AXIS_SCALE_VAL;
> > +			return IIO_VAL_INT_PLUS_MICRO;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> > +static const struct iio_info dmard06_info = {
> > +	.driver_module	= THIS_MODULE,
> > +	.read_raw	= dmard06_read_raw,
> > +};
> > +
> > +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> 
> maybe rename reg to _reg
> 
> > +	.type = IIO_ACCEL,					\
> > +	.address = reg,						\
> > +	.channel2 = IIO_MOD_##_axis,				\
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> > +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> > +	.modified = 1,						\
> > +}
> > +
> > +#define DMARD06_TEMP_CHANNEL(reg) {				\
> > +	.type = IIO_TEMP,					\
> > +	.address = reg,						\
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> > +			      BIT(IIO_CHAN_INFO_OFFSET),	\
> > +}
> > +
> > +static const struct iio_chan_spec dmard06_channels[] = {
> > +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
> > +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
> > +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
> > +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
> > +};
> > +
> > +static int dmard06_probe(struct i2c_client *client,
> > +			const struct i2c_device_id *id)
> > +{
> > +	int error;
> > +	struct iio_dev *indio_dev;
> > +	struct dmard06_data *dmard06;
> > +
> > +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> 
> not needed
> 
> > +
> > +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> > +		dev_err(&client->dev, "I2C check functionality failed\n");
> > +		return -ENXIO;
> > +	}
> > +
> > +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
> > +	if (!indio_dev) {
> > +		dev_err(&client->dev, "Failed to allocate iio device\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	dmard06 = iio_priv(indio_dev);
> > +	dmard06->client = client;
> > +
> > +	error = dmard06_read_chip_id(dmard06);
> > +	if (error)
> > +		return error;
> > +
> > +	i2c_set_clientdata(client, indio_dev);
> > +	indio_dev->dev.parent = &client->dev;
> > +	indio_dev->name = DMARD06_DRV_NAME;
> > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > +	indio_dev->channels = dmard06_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
> > +	indio_dev->info = &dmard06_info;
> > +
> > +	return devm_iio_device_register(&client->dev, indio_dev);
> > +}
> > +
> > +static const struct i2c_device_id dmard06_id[] = {
> > +	{ "dmard06", 0 },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> > +
> > +static const struct of_device_id dmard06_of_match[] = {
> > +	{ .compatible = "domintech,dmard06" },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> > +
> > +static struct i2c_driver dmard06_driver = {
> > +	.probe = dmard06_probe,
> > +	.id_table = dmard06_id,
> > +	.driver = {
> > +		.name = DMARD06_DRV_NAME,
> > +		.of_match_table = of_match_ptr(dmard06_of_match),
> > +	},
> > +};
> > +module_i2c_driver(dmard06_driver);
> > +
> > +MODULE_AUTHOR("Aleksei Mamlin <mamlinav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>");
> > +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> > +MODULE_LICENSE("GPL v2");
> > 
> 
> -- 
> 
> Peter Meerwald-Stadler
> +43-664-2444418 (mobile)


-- 
Thanks and regards,
Aleksei Mamlin

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
  2016-07-23  7:26         ` Jonathan Cameron
  (?)
@ 2016-07-25  7:50         ` Aleksei Mamlin
  2016-07-25  8:36           ` Peter Meerwald-Stadler
  -1 siblings, 1 reply; 23+ messages in thread
From: Aleksei Mamlin @ 2016-07-25  7:50 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Peter Meerwald-Stadler, Hartmut Knaack, Lars-Peter Clausen,
	Rob Herring, devicetree, linux-kernel, linux-iio

On Sat, 23 Jul 2016 09:26:39 +0200
Jonathan Cameron <jic23@kernel.org> wrote:

> On 22/07/16 00:41, Peter Meerwald-Stadler wrote:
> > 
> >> This patch add support for Domintech DMARD06 accelerometer.
> > 
> > comments below
> >  
> >> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> >> cost-sensitive consumer application.
> >>
> >> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> I don't have anything to add here. Nice fairly clean first version.

Jonathan, I found that dmard05, dmard06 and dmard07 chips are very similar
and have the same registers. There is not so many difference between chips,
dmard05 and dmard07 have 8 bit adc and dmard06 only 7 bit adc.

I want to add support of those chips in driver, but there is the question:
should driver be renamed to something like dmard050607 or just dmard05
as first chip in this series?

> >> ---
> >>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
> >>  drivers/iio/accel/Kconfig                          |  11 ++
> >>  drivers/iio/accel/Makefile                         |   1 +
> >>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
> >>  4 files changed, 248 insertions(+)
> >>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
> >>  create mode 100644 drivers/iio/accel/dmard06.c
> >>
> >> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> >> new file mode 100644
> >> index 0000000..cc2231a
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> >> @@ -0,0 +1,17 @@
> >> +Device tree bindings for Domintech DMARD06 acceletometer
> > 
> > typo: accelerometer
> > 
> >> +
> >> +Required properties:
> >> + - compatible		: Should be "domintech,dmard06"
> >> + - reg			: I2C address of the chip. Should be 0x1c
> >> +
> >> +Example:
> >> +	&i2c1 {
> >> +		/* ... */
> >> +
> >> +		accelerometer@1c {
> >> +			compatible = "domintech,dmard06";
> >> +			reg = <0x1c>;
> >> +		};
> >> +
> >> +		/* ... */
> >> +	};
> >> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> >> index e4a758c..abc8536 100644
> >> --- a/drivers/iio/accel/Kconfig
> >> +++ b/drivers/iio/accel/Kconfig
> >> @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
> >>  	tristate
> >>  	select REGMAP_SPI
> >>  
> >> +config DMARD06
> >> +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
> >> +	depends on OF || COMPILE_TEST
> >> +	depends on I2C
> >> +	help
> >> +	  Say yes here to build support for the Domintech DMARD06 low-g
> >> +	  tri-axial digital accelerometer connected via an I2C bus.
> >> +
> >> +	  To compile this driver as a module, choose M here: the
> >> +	  module will be called dmard06.
> >> +
> >>  config HID_SENSOR_ACCEL_3D
> >>  	depends on HID_SENSOR_HUB
> >>  	select IIO_BUFFER
> >> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> >> index 71b6794..a96b269 100644
> >> --- a/drivers/iio/accel/Makefile
> >> +++ b/drivers/iio/accel/Makefile
> >> @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
> >>  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
> >>  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
> >>  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
> >> +obj-$(CONFIG_DMARD06)	+= dmard06.o
> >>  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
> >>  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
> >>  obj-$(CONFIG_KXSD9)	+= kxsd9.o
> >> diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
> >> new file mode 100644
> >> index 0000000..433e430
> >> --- /dev/null
> >> +++ b/drivers/iio/accel/dmard06.c
> >> @@ -0,0 +1,219 @@
> >> +/*
> >> + * IIO driver for Domintech DMARD06 accelerometer
> >> + *
> >> + * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> >> + *
> >> + * This program is free software; you can redistribute it and/or modify it
> >> + * under the terms and conditions of the GNU General Public License,
> >> + * version 2, as published by the Free Software Foundation.
> >> + */
> >> +
> >> +#include <linux/module.h>
> >> +#include <linux/i2c.h>
> >> +#include <linux/iio/iio.h>
> >> +
> >> +struct dmard06_data {
> >> +	struct i2c_client *client;
> >> +};
> >> +
> >> +#define DMARD06_DRV_NAME		"dmard06"
> >> +
> >> +/* Device data registers */
> >> +#define DMARD06_CHIP_ID_REG		0x0f
> >> +#define DMARD06_TOUT_REG		0x40
> >> +#define DMARD06_XOUT_REG		0x41
> >> +#define DMARD06_YOUT_REG		0x42
> >> +#define DMARD06_ZOUT_REG		0x43
> >> +
> >> +/* Device ID value */
> >> +#define DMARD06_CHIP_ID			0x06
> >> +
> >> +/* Device values */
> >> +#define DMARD06_AXIS_MAX_VAL		64
> >> +#define DMARD06_AXIS_SCALE_VAL		31250
> >> +#define DMARD06_TEMP_CENTER_VAL		25
> >> +
> >> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> >> +{
> >> +	int error;
> > 
> > 'error' is a bit pessimistic, how about 'ret'? :-)
> > 
> >> +
> > 
> > can't use i2c_smbus_read_byte_data()?
> > 
> >> +	error = i2c_master_send(client, &reg, 1);
> >> +	if (unlikely(error < 0))
> >> +		return error;
> >> +
> >> +	error = i2c_master_recv(client, buf, 1);
> >> +	if (unlikely(error < 0))
> >> +		return error;
> > 
> > not sure if it is worth to annotate unlikely() here -- i2c is slooow
> > locking is needed if you really need two i2c_master calls
> > 
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> >> +{
> >> +	u8 buf;
> >> +	u8 chip_id;
> >> +	int error;
> >> +
> >> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> >> +	if (error) {
> >> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> >> +			error);
> >> +		return error;
> >> +	}
> >> +
> >> +	chip_id = (buf & 0x00ff);
> > 
> > why? buf is u8 anyways
> > 
> >> +	if (chip_id != DMARD06_CHIP_ID) {
> >> +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
> >> +			chip_id);
> >> +		return -EINVAL;
> > 
> > often ENODEV is used for this case
> > 
> >> +	}
> >> +
> >> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> > 
> > we prefer silent probing of the driver, please don't clutter the log
> > 
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static int dmard06_read_raw(struct iio_dev *indio_dev,
> >> +			    struct iio_chan_spec const *chan,
> >> +			    int *val, int *val2, long mask)
> >> +{
> >> +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
> >> +	u8 buf;
> >> +	bool reversed;
> >> +	int error;
> >> +	int tmp;
> >> +
> >> +	switch (mask) {
> >> +	case IIO_CHAN_INFO_RAW:
> >> +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
> >> +		if (error) {
> >> +			dev_err(&dmard06->client->dev,
> >> +				"Failed to read data %d\n", error);
> >> +			return error;
> >> +		}
> >> +
> >> +		reversed = (buf >> 7) & 1;
> >> +		tmp = (int)((buf & 0x7f) >> 1);
> > 
> > 
> >> +
> >> +		switch (chan->type) {
> >> +		case IIO_ACCEL:
> > 
> > wouldn't sign_extend32(buf >> 1, 6) do the job?
> > 
> >> +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
> >> +			return IIO_VAL_INT;
> >> +		case IIO_TEMP:
> >> +			*val = reversed ? -tmp : tmp;
> >> +			return IIO_VAL_INT;
> >> +		default:
> >> +			return -EINVAL;
> >> +		}
> >> +	case IIO_CHAN_INFO_OFFSET:
> >> +		switch (chan->type) {
> >> +		case IIO_TEMP:
> >> +			*val = DMARD06_TEMP_CENTER_VAL;
> >> +			return IIO_VAL_INT;
> >> +		default:
> >> +			return -EINVAL;
> >> +		}
> >> +	case IIO_CHAN_INFO_SCALE:
> >> +		switch (chan->type) {
> >> +		case IIO_ACCEL:
> >> +			*val = 0;
> >> +			*val2 = DMARD06_AXIS_SCALE_VAL;
> >> +			return IIO_VAL_INT_PLUS_MICRO;
> >> +		default:
> >> +			return -EINVAL;
> >> +		}
> >> +	default:
> >> +		return -EINVAL;
> >> +	}
> >> +}
> >> +
> >> +static const struct iio_info dmard06_info = {
> >> +	.driver_module	= THIS_MODULE,
> >> +	.read_raw	= dmard06_read_raw,
> >> +};
> >> +
> >> +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> > 
> > maybe rename reg to _reg
> > 
> >> +	.type = IIO_ACCEL,					\
> >> +	.address = reg,						\
> >> +	.channel2 = IIO_MOD_##_axis,				\
> >> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> >> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> >> +	.modified = 1,						\
> >> +}
> >> +
> >> +#define DMARD06_TEMP_CHANNEL(reg) {				\
> >> +	.type = IIO_TEMP,					\
> >> +	.address = reg,						\
> >> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> >> +			      BIT(IIO_CHAN_INFO_OFFSET),	\
> >> +}
> >> +
> >> +static const struct iio_chan_spec dmard06_channels[] = {
> >> +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
> >> +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
> >> +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
> >> +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
> >> +};
> >> +
> >> +static int dmard06_probe(struct i2c_client *client,
> >> +			const struct i2c_device_id *id)
> >> +{
> >> +	int error;
> >> +	struct iio_dev *indio_dev;
> >> +	struct dmard06_data *dmard06;
> >> +
> >> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> > 
> > not needed
> > 
> >> +
> >> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> >> +		dev_err(&client->dev, "I2C check functionality failed\n");
> >> +		return -ENXIO;
> >> +	}
> >> +
> >> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
> >> +	if (!indio_dev) {
> >> +		dev_err(&client->dev, "Failed to allocate iio device\n");
> >> +		return -ENOMEM;
> >> +	}
> >> +
> >> +	dmard06 = iio_priv(indio_dev);
> >> +	dmard06->client = client;
> >> +
> >> +	error = dmard06_read_chip_id(dmard06);
> >> +	if (error)
> >> +		return error;
> >> +
> >> +	i2c_set_clientdata(client, indio_dev);
> >> +	indio_dev->dev.parent = &client->dev;
> >> +	indio_dev->name = DMARD06_DRV_NAME;
> >> +	indio_dev->modes = INDIO_DIRECT_MODE;
> >> +	indio_dev->channels = dmard06_channels;
> >> +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
> >> +	indio_dev->info = &dmard06_info;
> >> +
> >> +	return devm_iio_device_register(&client->dev, indio_dev);
> >> +}
> >> +
> >> +static const struct i2c_device_id dmard06_id[] = {
> >> +	{ "dmard06", 0 },
> >> +	{ }
> >> +};
> >> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> >> +
> >> +static const struct of_device_id dmard06_of_match[] = {
> >> +	{ .compatible = "domintech,dmard06" },
> >> +	{ }
> >> +};
> >> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> >> +
> >> +static struct i2c_driver dmard06_driver = {
> >> +	.probe = dmard06_probe,
> >> +	.id_table = dmard06_id,
> >> +	.driver = {
> >> +		.name = DMARD06_DRV_NAME,
> >> +		.of_match_table = of_match_ptr(dmard06_of_match),
> >> +	},
> >> +};
> >> +module_i2c_driver(dmard06_driver);
> >> +
> >> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> >> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> >> +MODULE_LICENSE("GPL v2");
> >>
> > 
> 


-- 
Thanks and regards,
Aleksei Mamlin

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer
  2016-07-25  7:50         ` Aleksei Mamlin
@ 2016-07-25  8:36           ` Peter Meerwald-Stadler
  0 siblings, 0 replies; 23+ messages in thread
From: Peter Meerwald-Stadler @ 2016-07-25  8:36 UTC (permalink / raw)
  To: Aleksei Mamlin
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Rob Herring, devicetree, linux-kernel, linux-iio


> > >> This patch add support for Domintech DMARD06 accelerometer.
> > > 
> > > comments below
> > >  
> > >> Domintech DMARD06 is a low-g tri-axial digital accelerometer for
> > >> cost-sensitive consumer application.
> > >>
> > >> Signed-off-by: Aleksei Mamlin <mamlinav@gmail.com>
> > I don't have anything to add here. Nice fairly clean first version.
> 
> Jonathan, I found that dmard05, dmard06 and dmard07 chips are very similar
> and have the same registers. There is not so many difference between chips,
> dmard05 and dmard07 have 8 bit adc and dmard06 only 7 bit adc.
> 
> I want to add support of those chips in driver, but there is the question:
> should driver be renamed to something like dmard050607 or just dmard05
> as first chip in this series?

just pick one name, e.g. dmard06; the file name should NOT contain things 
such as dmard0x or 050607 -- marketing is just too creative :) and 
remember: 
"There are two hard things in computer science: cache invalidation, naming 
things, and off-by-one errors."

p.

> > >> ---
> > >>  .../devicetree/bindings/iio/accel/dmard06.txt      |  17 ++
> > >>  drivers/iio/accel/Kconfig                          |  11 ++
> > >>  drivers/iio/accel/Makefile                         |   1 +
> > >>  drivers/iio/accel/dmard06.c                        | 219 +++++++++++++++++++++
> > >>  4 files changed, 248 insertions(+)
> > >>  create mode 100644 Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > >>  create mode 100644 drivers/iio/accel/dmard06.c
> > >>
> > >> diff --git a/Documentation/devicetree/bindings/iio/accel/dmard06.txt b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > >> new file mode 100644
> > >> index 0000000..cc2231a
> > >> --- /dev/null
> > >> +++ b/Documentation/devicetree/bindings/iio/accel/dmard06.txt
> > >> @@ -0,0 +1,17 @@
> > >> +Device tree bindings for Domintech DMARD06 acceletometer
> > > 
> > > typo: accelerometer
> > > 
> > >> +
> > >> +Required properties:
> > >> + - compatible		: Should be "domintech,dmard06"
> > >> + - reg			: I2C address of the chip. Should be 0x1c
> > >> +
> > >> +Example:
> > >> +	&i2c1 {
> > >> +		/* ... */
> > >> +
> > >> +		accelerometer@1c {
> > >> +			compatible = "domintech,dmard06";
> > >> +			reg = <0x1c>;
> > >> +		};
> > >> +
> > >> +		/* ... */
> > >> +	};
> > >> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> > >> index e4a758c..abc8536 100644
> > >> --- a/drivers/iio/accel/Kconfig
> > >> +++ b/drivers/iio/accel/Kconfig
> > >> @@ -40,6 +40,17 @@ config BMC150_ACCEL_SPI
> > >>  	tristate
> > >>  	select REGMAP_SPI
> > >>  
> > >> +config DMARD06
> > >> +	tristate "Domintech DMARD06 Digital Accelerometer Driver"
> > >> +	depends on OF || COMPILE_TEST
> > >> +	depends on I2C
> > >> +	help
> > >> +	  Say yes here to build support for the Domintech DMARD06 low-g
> > >> +	  tri-axial digital accelerometer connected via an I2C bus.
> > >> +
> > >> +	  To compile this driver as a module, choose M here: the
> > >> +	  module will be called dmard06.
> > >> +
> > >>  config HID_SENSOR_ACCEL_3D
> > >>  	depends on HID_SENSOR_HUB
> > >>  	select IIO_BUFFER
> > >> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> > >> index 71b6794..a96b269 100644
> > >> --- a/drivers/iio/accel/Makefile
> > >> +++ b/drivers/iio/accel/Makefile
> > >> @@ -7,6 +7,7 @@ obj-$(CONFIG_BMA180) += bma180.o
> > >>  obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
> > >>  obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
> > >>  obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
> > >> +obj-$(CONFIG_DMARD06)	+= dmard06.o
> > >>  obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
> > >>  obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
> > >>  obj-$(CONFIG_KXSD9)	+= kxsd9.o
> > >> diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
> > >> new file mode 100644
> > >> index 0000000..433e430
> > >> --- /dev/null
> > >> +++ b/drivers/iio/accel/dmard06.c
> > >> @@ -0,0 +1,219 @@
> > >> +/*
> > >> + * IIO driver for Domintech DMARD06 accelerometer
> > >> + *
> > >> + * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
> > >> + *
> > >> + * This program is free software; you can redistribute it and/or modify it
> > >> + * under the terms and conditions of the GNU General Public License,
> > >> + * version 2, as published by the Free Software Foundation.
> > >> + */
> > >> +
> > >> +#include <linux/module.h>
> > >> +#include <linux/i2c.h>
> > >> +#include <linux/iio/iio.h>
> > >> +
> > >> +struct dmard06_data {
> > >> +	struct i2c_client *client;
> > >> +};
> > >> +
> > >> +#define DMARD06_DRV_NAME		"dmard06"
> > >> +
> > >> +/* Device data registers */
> > >> +#define DMARD06_CHIP_ID_REG		0x0f
> > >> +#define DMARD06_TOUT_REG		0x40
> > >> +#define DMARD06_XOUT_REG		0x41
> > >> +#define DMARD06_YOUT_REG		0x42
> > >> +#define DMARD06_ZOUT_REG		0x43
> > >> +
> > >> +/* Device ID value */
> > >> +#define DMARD06_CHIP_ID			0x06
> > >> +
> > >> +/* Device values */
> > >> +#define DMARD06_AXIS_MAX_VAL		64
> > >> +#define DMARD06_AXIS_SCALE_VAL		31250
> > >> +#define DMARD06_TEMP_CENTER_VAL		25
> > >> +
> > >> +static int dmard06_i2c_read(struct i2c_client *client, u8 reg, u8 *buf)
> > >> +{
> > >> +	int error;
> > > 
> > > 'error' is a bit pessimistic, how about 'ret'? :-)
> > > 
> > >> +
> > > 
> > > can't use i2c_smbus_read_byte_data()?
> > > 
> > >> +	error = i2c_master_send(client, &reg, 1);
> > >> +	if (unlikely(error < 0))
> > >> +		return error;
> > >> +
> > >> +	error = i2c_master_recv(client, buf, 1);
> > >> +	if (unlikely(error < 0))
> > >> +		return error;
> > > 
> > > not sure if it is worth to annotate unlikely() here -- i2c is slooow
> > > locking is needed if you really need two i2c_master calls
> > > 
> > >> +	return 0;
> > >> +}
> > >> +
> > >> +static int dmard06_read_chip_id(struct dmard06_data *dmard06)
> > >> +{
> > >> +	u8 buf;
> > >> +	u8 chip_id;
> > >> +	int error;
> > >> +
> > >> +	error = dmard06_i2c_read(dmard06->client, DMARD06_CHIP_ID_REG, &buf);
> > >> +	if (error) {
> > >> +		dev_err(&dmard06->client->dev, "Failed to read chip id: %d\n",
> > >> +			error);
> > >> +		return error;
> > >> +	}
> > >> +
> > >> +	chip_id = (buf & 0x00ff);
> > > 
> > > why? buf is u8 anyways
> > > 
> > >> +	if (chip_id != DMARD06_CHIP_ID) {
> > >> +		dev_err(&dmard06->client->dev, "Invalid chip id: %02x\n",
> > >> +			chip_id);
> > >> +		return -EINVAL;
> > > 
> > > often ENODEV is used for this case
> > > 
> > >> +	}
> > >> +
> > >> +	dev_info(&dmard06->client->dev, "Found DMARD%02x chip\n", chip_id);
> > > 
> > > we prefer silent probing of the driver, please don't clutter the log
> > > 
> > >> +
> > >> +	return 0;
> > >> +}
> > >> +
> > >> +static int dmard06_read_raw(struct iio_dev *indio_dev,
> > >> +			    struct iio_chan_spec const *chan,
> > >> +			    int *val, int *val2, long mask)
> > >> +{
> > >> +	struct dmard06_data *dmard06 = iio_priv(indio_dev);
> > >> +	u8 buf;
> > >> +	bool reversed;
> > >> +	int error;
> > >> +	int tmp;
> > >> +
> > >> +	switch (mask) {
> > >> +	case IIO_CHAN_INFO_RAW:
> > >> +		error = dmard06_i2c_read(dmard06->client, chan->address, &buf);
> > >> +		if (error) {
> > >> +			dev_err(&dmard06->client->dev,
> > >> +				"Failed to read data %d\n", error);
> > >> +			return error;
> > >> +		}
> > >> +
> > >> +		reversed = (buf >> 7) & 1;
> > >> +		tmp = (int)((buf & 0x7f) >> 1);
> > > 
> > > 
> > >> +
> > >> +		switch (chan->type) {
> > >> +		case IIO_ACCEL:
> > > 
> > > wouldn't sign_extend32(buf >> 1, 6) do the job?
> > > 
> > >> +			*val = reversed ? (tmp - DMARD06_AXIS_MAX_VAL) : tmp;
> > >> +			return IIO_VAL_INT;
> > >> +		case IIO_TEMP:
> > >> +			*val = reversed ? -tmp : tmp;
> > >> +			return IIO_VAL_INT;
> > >> +		default:
> > >> +			return -EINVAL;
> > >> +		}
> > >> +	case IIO_CHAN_INFO_OFFSET:
> > >> +		switch (chan->type) {
> > >> +		case IIO_TEMP:
> > >> +			*val = DMARD06_TEMP_CENTER_VAL;
> > >> +			return IIO_VAL_INT;
> > >> +		default:
> > >> +			return -EINVAL;
> > >> +		}
> > >> +	case IIO_CHAN_INFO_SCALE:
> > >> +		switch (chan->type) {
> > >> +		case IIO_ACCEL:
> > >> +			*val = 0;
> > >> +			*val2 = DMARD06_AXIS_SCALE_VAL;
> > >> +			return IIO_VAL_INT_PLUS_MICRO;
> > >> +		default:
> > >> +			return -EINVAL;
> > >> +		}
> > >> +	default:
> > >> +		return -EINVAL;
> > >> +	}
> > >> +}
> > >> +
> > >> +static const struct iio_info dmard06_info = {
> > >> +	.driver_module	= THIS_MODULE,
> > >> +	.read_raw	= dmard06_read_raw,
> > >> +};
> > >> +
> > >> +#define DMARD06_ACCEL_CHANNEL(_axis, reg) {			\
> > > 
> > > maybe rename reg to _reg
> > > 
> > >> +	.type = IIO_ACCEL,					\
> > >> +	.address = reg,						\
> > >> +	.channel2 = IIO_MOD_##_axis,				\
> > >> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> > >> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> > >> +	.modified = 1,						\
> > >> +}
> > >> +
> > >> +#define DMARD06_TEMP_CHANNEL(reg) {				\
> > >> +	.type = IIO_TEMP,					\
> > >> +	.address = reg,						\
> > >> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> > >> +			      BIT(IIO_CHAN_INFO_OFFSET),	\
> > >> +}
> > >> +
> > >> +static const struct iio_chan_spec dmard06_channels[] = {
> > >> +	DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
> > >> +	DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
> > >> +	DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
> > >> +	DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
> > >> +};
> > >> +
> > >> +static int dmard06_probe(struct i2c_client *client,
> > >> +			const struct i2c_device_id *id)
> > >> +{
> > >> +	int error;
> > >> +	struct iio_dev *indio_dev;
> > >> +	struct dmard06_data *dmard06;
> > >> +
> > >> +	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
> > > 
> > > not needed
> > > 
> > >> +
> > >> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
> > >> +		dev_err(&client->dev, "I2C check functionality failed\n");
> > >> +		return -ENXIO;
> > >> +	}
> > >> +
> > >> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
> > >> +	if (!indio_dev) {
> > >> +		dev_err(&client->dev, "Failed to allocate iio device\n");
> > >> +		return -ENOMEM;
> > >> +	}
> > >> +
> > >> +	dmard06 = iio_priv(indio_dev);
> > >> +	dmard06->client = client;
> > >> +
> > >> +	error = dmard06_read_chip_id(dmard06);
> > >> +	if (error)
> > >> +		return error;
> > >> +
> > >> +	i2c_set_clientdata(client, indio_dev);
> > >> +	indio_dev->dev.parent = &client->dev;
> > >> +	indio_dev->name = DMARD06_DRV_NAME;
> > >> +	indio_dev->modes = INDIO_DIRECT_MODE;
> > >> +	indio_dev->channels = dmard06_channels;
> > >> +	indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
> > >> +	indio_dev->info = &dmard06_info;
> > >> +
> > >> +	return devm_iio_device_register(&client->dev, indio_dev);
> > >> +}
> > >> +
> > >> +static const struct i2c_device_id dmard06_id[] = {
> > >> +	{ "dmard06", 0 },
> > >> +	{ }
> > >> +};
> > >> +MODULE_DEVICE_TABLE(i2c, dmard06_id);
> > >> +
> > >> +static const struct of_device_id dmard06_of_match[] = {
> > >> +	{ .compatible = "domintech,dmard06" },
> > >> +	{ }
> > >> +};
> > >> +MODULE_DEVICE_TABLE(of, dmard06_of_match);
> > >> +
> > >> +static struct i2c_driver dmard06_driver = {
> > >> +	.probe = dmard06_probe,
> > >> +	.id_table = dmard06_id,
> > >> +	.driver = {
> > >> +		.name = DMARD06_DRV_NAME,
> > >> +		.of_match_table = of_match_ptr(dmard06_of_match),
> > >> +	},
> > >> +};
> > >> +module_i2c_driver(dmard06_driver);
> > >> +
> > >> +MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
> > >> +MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
> > >> +MODULE_LICENSE("GPL v2");
> > >>
> > > 
> > 
> 
> 
> 

-- 

Peter Meerwald-Stadler
+43-664-2444418 (mobile)

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2016-07-25  8:37 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-18 12:48 [PATCH 0/2] Add driver for Domintech DMARD06 accelerometer Aleksei Mamlin
2016-07-18 12:48 ` [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd Aleksei Mamlin
2016-07-20  1:09   ` Rob Herring
2016-07-18 12:48 ` [PATCH 2/2] input: misc: Add support for Domintech DMARD06 accelerometer Aleksei Mamlin
2016-07-18 12:48   ` Aleksei Mamlin
2016-07-20  0:02   ` Dmitry Torokhov
2016-07-21  6:13     ` Jonathan Cameron
2016-07-21  6:35       ` Aleksei Mamlin
2016-07-21 20:32 ` [PATCH 0/2] Add driver " Aleksei Mamlin
2016-07-21 20:32   ` Aleksei Mamlin
2016-07-21 20:32   ` [PATCH 1/2] devicetree: bindings: Add vendor prefix for Domintech Co., Ltd Aleksei Mamlin
2016-07-21 20:32     ` Aleksei Mamlin
2016-07-21 22:16     ` Rob Herring
2016-07-21 22:16       ` Rob Herring
2016-07-21 20:33   ` [PATCH 2/2] iio: accel: Add support for Domintech DMARD06 accelerometer Aleksei Mamlin
2016-07-21 22:19     ` Rob Herring
2016-07-21 22:41     ` Peter Meerwald-Stadler
2016-07-23  7:26       ` Jonathan Cameron
2016-07-23  7:26         ` Jonathan Cameron
2016-07-25  7:50         ` Aleksei Mamlin
2016-07-25  8:36           ` Peter Meerwald-Stadler
2016-07-23 21:49       ` Aleksei Mamlin
2016-07-23 21:49         ` Aleksei Mamlin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.