Skip to contentSkip to conversation

iio: imu: Add support for Kionix KMX61 sensor

PATCH Superseded Daniel Baluta 3 participants 7 messages Nov 18, 2014 – Nov 25, 2014 IIO

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: imu: Add support for Kionix KMX61 sensor

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It exports raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and Peter
* http://marc.info/?l=linux-iio&m=141564019602400&w=2
* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +
 drivers/iio/imu/kmx61.c  | 766 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +9 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
 	  Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
 	  ADIS16485, ADIS16488 inertial sensors.
 
+config KMX61
+	tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for Kionix KMX61 6-axis accelerometer
+	  and magnetometer.
+	  To compile this driver as module, choose M here: the module will be called
+	  kmx61.
+
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +2 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += inv_mpu6050/
+
+obj-$(CONFIG_KMX61) += kmx61.o
drivers/iio/imu/kmx61.c new file +766 -0

766 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 7 lines · Daniel Baluta

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It exports raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

I would slightly have prefered the runtime suspend stuff as a follow up patch for ease fo reviewing, but it seems straight forward and correct so fair enough.,

Applied to the togreg branch of iio.git. Initially pushed out as testing for the autobuilders to play with it.

Jonathan

Quoted 818 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and Peter
* http://marc.info/?l=linux-iio&m=141564019602400&w=2
* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +
 drivers/iio/imu/kmx61.c  | 766 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +9 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
 	  Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
 	  ADIS16485, ADIS16488 inertial sensors.
 
+config KMX61
+	tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for Kionix KMX61 6-axis accelerometer
+	  and magnetometer.
+	  To compile this driver as module, choose M here: the module will be called
+	  kmx61.
+
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +2 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += inv_mpu6050/
+
+obj-$(CONFIG_KMX61) += kmx61.o
drivers/iio/imu/kmx61.c new file +766 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I	0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L	0x0A
+#define KMX61_ACC_XOUT_H	0x0B
+#define KMX61_ACC_YOUT_L	0x0C
+#define KMX61_ACC_YOUT_H	0x0D
+#define KMX61_ACC_ZOUT_L	0x0E
+#define KMX61_ACC_ZOUT_H	0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L		0x10
+#define KMX61_TEMP_H		0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L	0x12
+#define KMX61_MAG_XOUT_H	0x13
+#define KMX61_MAG_YOUT_L	0x14
+#define KMX61_MAG_YOUT_H	0x15
+#define KMX61_MAG_ZOUT_L	0x16
+#define KMX61_MAG_ZOUT_H	0x17
+
+#define KMX61_REG_ODCNTL	0x2C
+#define KMX61_REG_STBY		0x29
+#define KMX61_REG_CTRL1		0x2A
+
+#define KMX61_ACC_STBY_BIT	BIT(0)
+#define KMX61_MAG_STBY_BIT	BIT(1)
+#define KMX61_ACT_STBY_BIT	BIT(7)
+
+#define KMX61_ALL_STBY		(KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT	0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT	1
+#define KMX61_REG_CTRL1_GSEL0_MASK	0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK	0x02
+
+#define KMX61_REG_CTRL1_BIT_RES		BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT	0
+#define KMX61_MAG_ODR_SHIFT	4
+#define KMX61_ACC_ODR_MASK	0x0F
+#define KMX61_MAG_ODR_MASK	0xF0
+
+#define KMX61_SLEEP_DELAY_MS	2000
+
+#define KMX61_CHIP_ID		0x12
+
+struct kmx61_data {
+	struct i2c_client *client;
+
+	/* serialize access to non-atomic ops, e.g set_mode */
+	struct mutex lock;
+	u8 range;
+	u8 odr_bits;
+
+	/* standby state */
+	u8 acc_stby;
+	u8 mag_stby;
+
+	/* power state */
+	bool acc_ps;
+	bool mag_ps;
+};
+
+enum kmx61_range {
+	KMX61_RANGE_2G,
+	KMX61_RANGE_4G,
+	KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+	KMX61_SCAN_ACC_X,
+	KMX61_SCAN_ACC_Y,
+	KMX61_SCAN_ACC_Z,
+	KMX61_SCAN_TEMP,
+	KMX61_SCAN_MAG_X,
+	KMX61_SCAN_MAG_Y,
+	KMX61_SCAN_MAG_Z,
+};
+
+static const struct {
+	u16 uscale;
+	u8 gsel0;
+	u8 gsel1;
+} kmx61_scale_table[] = {
+	{9582, 0, 0},
+	{19163, 1, 0},
+	{38326, 0, 1},
+};
+
+/* KMX61 devices */
+#define KMX61_ACC	0x01
+#define KMX61_MAG	0x02
+
+static const struct {
+	int val;
+	int val2;
+	u8 odr_bits;
+} kmx61_samp_freq_table[] = { {12, 500000, 0x00},
+			{25, 0, 0x01},
+			{50, 0, 0x02},
+			{100, 0, 0x03},
+			{200, 0, 0x04},
+			{400, 0, 0x05},
+			{800, 0, 0x06},
+			{1600, 0, 0x07},
+			{0, 781000, 0x08},
+			{1, 563000, 0x09},
+			{3, 125000, 0x0A},
+			{6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+	"0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
+
+static struct attribute *kmx61_attributes[] = {
+	&iio_const_attr_accel_scale_available.dev_attr.attr,
+	&iio_const_attr_magn_scale_available.dev_attr.attr,
+	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group = {
+	.attrs = kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _axis, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+	.address = KMX61_ACC, \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 12, \
+		.storagebits = 16, \
+		.shift = 4, \
+		.endianness = IIO_LE, \
+	}, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+	.type = IIO_MAGN, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _axis, \
+	.address = KMX61_MAG, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 14, \
+		.storagebits = 16, \
+		.shift = 2, \
+		.endianness = IIO_LE, \
+	}, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] = {
+	KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+	KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+	KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+	KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+	KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+	KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+		if (val == kmx61_samp_freq_table[i].val &&
+		    val2 == kmx61_samp_freq_table[i].val2)
+			return kmx61_samp_freq_table[i].odr_bits;
+	return -EINVAL;
+}
+/**
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be set
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operating modes
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
+ * if they are both enabled. Internal sensors state is saved in acc_stby and
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
+			  bool update)
+{
+	int ret;
+	int acc_stby = -1, mag_stby = -1;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading reg_stby\n");
+		return ret;
+	}
+	if (device & KMX61_ACC) {
+		if (mode & KMX61_ACC_STBY_BIT) {
+			ret |= KMX61_ACC_STBY_BIT;
+			acc_stby = 1;
+		} else {
+			ret &= ~KMX61_ACC_STBY_BIT;
+			acc_stby = 0;
+		}
+	}
+
+	if (device & KMX61_MAG) {
+		if (mode & KMX61_MAG_STBY_BIT) {
+			ret |= KMX61_MAG_STBY_BIT;
+			mag_stby = 1;
+		} else {
+			ret &= ~KMX61_MAG_STBY_BIT;
+			mag_stby = 0;
+		}
+	}
+
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error writing reg_stby\n");
+		return ret;
+	}
+
+	if (acc_stby != -1 && update)
+		data->acc_stby = !!acc_stby;
+	if (mag_stby != -1 && update)
+		data->mag_stby = !!mag_stby;
+
+	return ret;
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading reg_stby\n");
+		return ret;
+	}
+	*mode = 0;
+
+	if (device & KMX61_ACC) {
+		if (ret & KMX61_ACC_STBY_BIT)
+			*mode |= KMX61_ACC_STBY_BIT;
+		else
+			*mode &= ~KMX61_ACC_STBY_BIT;
+	}
+
+	if (device & KMX61_MAG) {
+		if (ret & KMX61_MAG_STBY_BIT)
+			*mode |= KMX61_MAG_STBY_BIT;
+		else
+			*mode &= ~KMX61_MAG_STBY_BIT;
+	}
+
+	return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
+{
+	int ret;
+	u8 mode;
+	int lodr_bits, odr_bits;
+
+	ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+	if (ret < 0)
+		return ret;
+
+	lodr_bits = kmx61_convert_freq_to_bit(val, val2);
+	if (lodr_bits < 0)
+		return lodr_bits;
+
+	/* To change ODR, accel and magn must be in STDBY */
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+			     true);
+	if (ret < 0)
+		return ret;
+
+	odr_bits = 0;
+	if (device & KMX61_ACC)
+		odr_bits |= lodr_bits;
+	if (device & KMX61_MAG)
+		odr_bits |= (lodr_bits << KMX61_MAG_ODR_SHIFT);
+
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
+					odr_bits);
+	if (ret < 0)
+		return ret;
+
+	ret = kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
+	if (ret < 0)
+		return ret;
+
+	data->odr_bits = lodr_bits;
+
+	return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 device)
+{	int i;
+	u8 lodr_bits;
+
+	if (device & KMX61_ACC)
+		lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+			     KMX61_ACC_ODR_MASK;
+	else if (device & KMX61_MAG)
+		lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+			     KMX61_MAG_ODR_MASK;
+	else
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+		if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) {
+			*val = kmx61_samp_freq_table[i].val;
+			*val2 = kmx61_samp_freq_table[i].val2;
+			return 0;
+		}
+	return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
+		return ret;
+	}
+
+	ret &= ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_MASK);
+	ret |= kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL0_SHIFT;
+	ret |= kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL1_SHIFT;
+
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
+		return ret;
+	}
+
+	data->range = range;
+
+	return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+	int ret, i;
+	u8  mode;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
+		if (kmx61_scale_table[i].uscale == uscale) {
+			ret = kmx61_get_mode(data, &mode,
+					     KMX61_ACC | KMX61_MAG);
+			if (ret < 0)
+				return ret;
+
+			ret = kmx61_set_mode(data, KMX61_ALL_STBY,
+					     KMX61_ACC | KMX61_MAG, true);
+			if (ret < 0)
+				return ret;
+
+			ret = kmx61_set_range(data, i);
+			if (ret < 0)
+				return ret;
+
+			return  kmx61_set_mode(data, mode,
+					       KMX61_ACC | KMX61_MAG, true);
+		}
+	}
+	return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading who_am_i\n");
+		return ret;
+	}
+
+	if (ret != KMX61_CHIP_ID) {
+		dev_err(&data->client->dev,
+			"Wrong chip id, got %x expected %x\n",
+			 ret, KMX61_CHIP_ID);
+		return -EINVAL;
+	}
+
+	/* set accel 12bit, 4g range */
+	ret = kmx61_set_range(data, KMX61_RANGE_4G);
+	if (ret < 0)
+		return ret;
+
+	/* set acc/magn to OPERATION mode */
+	ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to be set
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is in
+ * OPERATION then we know that kmx61_runtime_resume was already called
+ * so we must set ACC OPERATION mode here. The same happens when MAG power
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
+{
+#ifdef CONFIG_PM_RUNTIME
+	int ret;
+
+	if (device & KMX61_ACC) {
+		if (on && !data->acc_ps && !data->mag_stby)
+			kmx61_set_mode(data, 0, KMX61_ACC, true);
+		data->acc_ps = on;
+	}
+	if (device & KMX61_MAG) {
+		if (on && !data->mag_ps && !data->acc_stby)
+			kmx61_set_mode(data, 0, KMX61_MAG, true);
+		data->mag_ps = on;
+	}
+
+	if (on) {
+		ret = pm_runtime_get_sync(&data->client->dev);
+	} else {
+		pm_runtime_mark_last_busy(&data->client->dev);
+		ret = pm_runtime_put_autosuspend(&data->client->dev);
+	}
+	if (ret < 0) {
+		dev_err(&data->client->dev,
+			"Failed: kmx61_set_power_state for %d, ret %d\n",
+			on, ret);
+		return ret;
+	}
+#endif
+	return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base, int offset)
+{
+	int ret;
+	u8 reg = base + offset * 2;
+
+	ret = i2c_smbus_read_word_data(data->client, reg);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
+		return ret;
+	}
+
+	return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan, int *val,
+			      int *val2, long mask)
+{
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+	u8 base_reg;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		switch (chan->type) {
+		case IIO_ACCEL:
+		case IIO_MAGN:
+			base_reg = KMX61_ACC_XOUT_L;
+			break;
+		default:
+			return -EINVAL;
+		}
+		mutex_lock(&data->lock);
+
+		kmx61_set_power_state(data, true, chan->address);
+		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
+		if (ret < 0) {
+			kmx61_set_power_state(data, false, chan->address);
+			mutex_unlock(&data->lock);
+			return ret;
+		}
+		*val = sign_extend32(ret >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
+		kmx61_set_power_state(data, false, chan->address);
+
+		mutex_unlock(&data->lock);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			*val = 0;
+			*val2 = kmx61_scale_table[data->range].uscale;
+			return IIO_VAL_INT_PLUS_MICRO;
+		case IIO_MAGN:
+			/* 14 bits res, 1465 microGauss per magn count */
+			*val = 0;
+			*val2 = 1465;
+			return IIO_VAL_INT_PLUS_MICRO;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		ret = kmx61_get_odr(data, val, val2, chan->address);
+		mutex_unlock(&data->lock);
+		if (ret)
+			return -EINVAL;
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+	return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan, int val,
+			       int val2, long mask)
+{
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		ret = kmx61_set_odr(data, val, val2, chan->address);
+		mutex_unlock(&data->lock);
+		return ret;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			if (val != 0)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = kmx61_set_scale(data, val2);
+			mutex_unlock(&data->lock);
+			return ret;
+		default:
+			return -EINVAL;
+		}
+		return ret;
+	default:
+		return -EINVAL;
+	}
+	return ret;
+}
+
+static const struct iio_info kmx61_info = {
+	.driver_module		= THIS_MODULE,
+	.read_raw		= kmx61_read_raw,
+	.write_raw		= kmx61_write_raw,
+	.attrs			= &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct kmx61_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+	const char *name = NULL;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	if (id)
+		name = id->name;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->channels = kmx61_channels;
+	indio_dev->num_channels = ARRAY_SIZE(kmx61_channels);
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &kmx61_info;
+
+	mutex_init(&data->lock);
+
+	ret = kmx61_chip_init(data);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(&client->dev, "Failed to register iio device\n");
+		goto err_iio_device_register;
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret < 0)
+		goto err_pm_runtime_set_active;
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(&client->dev);
+
+	return 0;
+
+err_pm_runtime_set_active:
+	iio_device_unregister(indio_dev);
+err_iio_device_register:
+	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	iio_device_unregister(indio_dev);
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+			     false);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	u8 stby = 0;
+
+	if (data->acc_stby)
+		stby |= KMX61_ACC_STBY_BIT;
+	if (data->mag_stby)
+		stby |= KMX61_MAG_STBY_BIT;
+
+	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	u8 stby = 0;
+
+	if (!data->acc_ps)
+		stby |= KMX61_ACC_STBY_BIT;
+	if (!data->mag_ps)
+		stby |= KMX61_MAG_STBY_BIT;
+
+	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+	SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
+};
+
+static const struct i2c_device_id kmx61_id[] = {
+	{"kmx611021", 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver = {
+	.driver = {
+		.name = KMX61_DRV_NAME,
+		.pm = &kmx61_pm_ops,
+	},
+	.probe		= kmx61_probe,
+	.remove		= kmx61_remove,
+	.id_table	= kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");
#2 lore raw
In reply to Daniel Baluta

Daniel Baluta schrieb am 18.11.2014 17:47:

Quoted 6 lines

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It exports raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

Finally managed to have a close look, and found a few issues.

Quoted 23 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and Peter
* http://marc.info/?l=linux-iio&m=141564019602400&w=2
* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +
 drivers/iio/imu/kmx61.c  | 766 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +0 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
 	  Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
 	  ADIS16485, ADIS16488 inertial sensors.

drivers/iio/imu/Kconfig config ADIS16480 · line 36

Quoted 10 lines
drivers/iio/imu/Kconfig +9 -0
 
+config KMX61
+	tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for Kionix KMX61 6-axis accelerometer
+	  and magnetometer.
+	  To compile this driver as module, choose M here: the module will be called
+	  kmx61.
+

This help text is more than 80 characters wide and may better be wrapped a bit differently.

Quoted 78 lines
drivers/iio/imu/Kconfig +0 -0
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +2 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += inv_mpu6050/
+
+obj-$(CONFIG_KMX61) += kmx61.o
drivers/iio/imu/kmx61.c new file +59 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I	0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L	0x0A
+#define KMX61_ACC_XOUT_H	0x0B
+#define KMX61_ACC_YOUT_L	0x0C
+#define KMX61_ACC_YOUT_H	0x0D
+#define KMX61_ACC_ZOUT_L	0x0E
+#define KMX61_ACC_ZOUT_H	0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L		0x10
+#define KMX61_TEMP_H		0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L	0x12
+#define KMX61_MAG_XOUT_H	0x13
+#define KMX61_MAG_YOUT_L	0x14
+#define KMX61_MAG_YOUT_H	0x15
+#define KMX61_MAG_ZOUT_L	0x16
+#define KMX61_MAG_ZOUT_H	0x17
+
+#define KMX61_REG_ODCNTL	0x2C
+#define KMX61_REG_STBY		0x29
+#define KMX61_REG_CTRL1		0x2A
+

drivers/iio/imu/kmx61.c line 69

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+#define KMX61_ACC_STBY_BIT	BIT(0)
+#define KMX61_MAG_STBY_BIT	BIT(1)
+#define KMX61_ACT_STBY_BIT	BIT(7)
+
+#define KMX61_ALL_STBY		(KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT	0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT	1
+#define KMX61_REG_CTRL1_GSEL0_MASK	0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK	0x02

I don't see the benefit of separating GSEL0 and GSEL1.

Quoted 13 lines
drivers/iio/imu/kmx61.c new file +13 -0
+
+#define KMX61_REG_CTRL1_BIT_RES		BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT	0
+#define KMX61_MAG_ODR_SHIFT	4
+#define KMX61_ACC_ODR_MASK	0x0F
+#define KMX61_MAG_ODR_MASK	0xF0
+
+#define KMX61_SLEEP_DELAY_MS	2000
+
+#define KMX61_CHIP_ID		0x12
+
+struct kmx61_data {

drivers/iio/imu/kmx61.c line 92

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+	struct i2c_client *client;
+
+	/* serialize access to non-atomic ops, e.g set_mode */
+	struct mutex lock;
+	u8 range;
+	u8 odr_bits;
+
+	/* standby state */
+	u8 acc_stby;
+	u8 mag_stby;

Why u8 instead of bool?

Quoted 20 lines
drivers/iio/imu/kmx61.c new file +20 -0
+
+	/* power state */
+	bool acc_ps;
+	bool mag_ps;
+};
+
+enum kmx61_range {
+	KMX61_RANGE_2G,
+	KMX61_RANGE_4G,
+	KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+	KMX61_SCAN_ACC_X,
+	KMX61_SCAN_ACC_Y,
+	KMX61_SCAN_ACC_Z,
+	KMX61_SCAN_TEMP,
+	KMX61_SCAN_MAG_X,
+	KMX61_SCAN_MAG_Y,
+	KMX61_SCAN_MAG_Z,

drivers/iio/imu/kmx61.c line 122

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+};
+
+static const struct {
+	u16 uscale;
+	u8 gsel0;
+	u8 gsel1;
+} kmx61_scale_table[] = {
+	{9582, 0, 0},
+	{19163, 1, 0},
+	{38326, 0, 1},

Why not consolidate gsel0 and gsel1 to gsel? Then this could become u16 kmx61_scale[] = {9582, 19163, 38326} with gsel as index.

Quoted 85 lines
drivers/iio/imu/kmx61.c new file +85 -0
+};
+
+/* KMX61 devices */
+#define KMX61_ACC	0x01
+#define KMX61_MAG	0x02
+
+static const struct {
+	int val;
+	int val2;
+	u8 odr_bits;
+} kmx61_samp_freq_table[] = { {12, 500000, 0x00},
+			{25, 0, 0x01},
+			{50, 0, 0x02},
+			{100, 0, 0x03},
+			{200, 0, 0x04},
+			{400, 0, 0x05},
+			{800, 0, 0x06},
+			{1600, 0, 0x07},
+			{0, 781000, 0x08},
+			{1, 563000, 0x09},
+			{3, 125000, 0x0A},
+			{6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+	"0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
+
+static struct attribute *kmx61_attributes[] = {
+	&iio_const_attr_accel_scale_available.dev_attr.attr,
+	&iio_const_attr_magn_scale_available.dev_attr.attr,
+	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group = {
+	.attrs = kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _axis, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+	.address = KMX61_ACC, \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 12, \
+		.storagebits = 16, \
+		.shift = 4, \
+		.endianness = IIO_LE, \
+	}, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+	.type = IIO_MAGN, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_ ## _axis, \
+	.address = KMX61_MAG, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+	.scan_index = _index, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 14, \
+		.storagebits = 16, \
+		.shift = 2, \
+		.endianness = IIO_LE, \
+	}, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] = {
+	KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+	KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+	KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+	KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+	KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+	KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)

drivers/iio/imu/kmx61.c line 217

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+		if (val == kmx61_samp_freq_table[i].val &&
+		    val2 == kmx61_samp_freq_table[i].val2)
+			return kmx61_samp_freq_table[i].odr_bits;
+	return -EINVAL;
+}
+/**

Like here, the first line of the following multiline comments have one asterisk too much.

Quoted 42 lines
drivers/iio/imu/kmx61.c new file +42 -0
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be set
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operating modes
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
+ * if they are both enabled. Internal sensors state is saved in acc_stby and
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
+			  bool update)
+{
+	int ret;
+	int acc_stby = -1, mag_stby = -1;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading reg_stby\n");
+		return ret;
+	}
+	if (device & KMX61_ACC) {
+		if (mode & KMX61_ACC_STBY_BIT) {
+			ret |= KMX61_ACC_STBY_BIT;
+			acc_stby = 1;
+		} else {
+			ret &= ~KMX61_ACC_STBY_BIT;
+			acc_stby = 0;
+		}
+	}
+
+	if (device & KMX61_MAG) {
+		if (mode & KMX61_MAG_STBY_BIT) {
+			ret |= KMX61_MAG_STBY_BIT;
+			mag_stby = 1;
+		} else {
+			ret &= ~KMX61_MAG_STBY_BIT;
+			mag_stby = 0;
+		}
+	}
+

drivers/iio/imu/kmx61.c line 269

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error writing reg_stby\n");
+		return ret;
+	}
+
+	if (acc_stby != -1 && update)
+		data->acc_stby = !!acc_stby;
+	if (mag_stby != -1 && update)
+		data->mag_stby = !!mag_stby;

Why is there a need for double negation?

drivers/iio/imu/kmx61.c line 271

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+	return ret;

Since no error occured until this point, just return 0.

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+	if (ret < 0) {

drivers/iio/imu/kmx61.c line 289

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+		dev_err(&data->client->dev, "Error reading reg_stby\n");
+		return ret;
+	}
+	*mode = 0;
+
+	if (device & KMX61_ACC) {
+		if (ret & KMX61_ACC_STBY_BIT)
+			*mode |= KMX61_ACC_STBY_BIT;
+		else
+			*mode &= ~KMX61_ACC_STBY_BIT;

Simply:

		*mode |= ret & KMX61_ACC_STBY_BIT;

drivers/iio/imu/kmx61.c line 296

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+	}
+
+	if (device & KMX61_MAG) {
+		if (ret & KMX61_MAG_STBY_BIT)
+			*mode |= KMX61_MAG_STBY_BIT;
+		else
+			*mode &= ~KMX61_MAG_STBY_BIT;

Same here:

		*mode |= ret & KMX61_MAG_STBY_BIT;
Quoted 20 lines
drivers/iio/imu/kmx61.c new file +20 -0
+	}
+
+	return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
+{
+	int ret;
+	u8 mode;
+	int lodr_bits, odr_bits;
+
+	ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+	if (ret < 0)
+		return ret;
+
+	lodr_bits = kmx61_convert_freq_to_bit(val, val2);
+	if (lodr_bits < 0)
+		return lodr_bits;
+
+	/* To change ODR, accel and magn must be in STDBY */

drivers/iio/imu/kmx61.c line 326

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+			     true);
+	if (ret < 0)
+		return ret;
+
+	odr_bits = 0;
+	if (device & KMX61_ACC)
+		odr_bits |= lodr_bits;
+	if (device & KMX61_MAG)
+		odr_bits |= (lodr_bits << KMX61_MAG_ODR_SHIFT);

No need for parenthesis.

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
+					odr_bits);
+	if (ret < 0)
+		return ret;
+
+	ret = kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);

drivers/iio/imu/kmx61.c line 343

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+	if (ret < 0)
+		return ret;
+
+	data->odr_bits = lodr_bits;
+
+	return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 device)

Usually wrap parameters? And don't put code on line with {.

drivers/iio/imu/kmx61.c line 349

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{	int i;
+	u8 lodr_bits;
+
+	if (device & KMX61_ACC)
+		lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+			     KMX61_ACC_ODR_MASK;

KMX61_ACC_ODR_SHIFT is just a placebo and not used in kmx61_set_odr. Choose one way and stay consistent.

drivers/iio/imu/kmx61.c line 360

Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+	else if (device & KMX61_MAG)
+		lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+			     KMX61_MAG_ODR_MASK;
+	else
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+		if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) {
+			*val = kmx61_samp_freq_table[i].val;
+			*val2 = kmx61_samp_freq_table[i].val2;
+			return 0;

return IIO_VAL_INT_PLUS_MICRO allows you to faciliate _read_raw a bit to just return ret there.

Quoted 71 lines
drivers/iio/imu/kmx61.c new file +71 -0
+		}
+	return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
+		return ret;
+	}
+
+	ret &= ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_MASK);
+	ret |= kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL0_SHIFT;
+	ret |= kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL1_SHIFT;
+
+	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
+		return ret;
+	}
+
+	data->range = range;
+
+	return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+	int ret, i;
+	u8  mode;
+
+	for (i = 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
+		if (kmx61_scale_table[i].uscale == uscale) {
+			ret = kmx61_get_mode(data, &mode,
+					     KMX61_ACC | KMX61_MAG);
+			if (ret < 0)
+				return ret;
+
+			ret = kmx61_set_mode(data, KMX61_ALL_STBY,
+					     KMX61_ACC | KMX61_MAG, true);
+			if (ret < 0)
+				return ret;
+
+			ret = kmx61_set_range(data, i);
+			if (ret < 0)
+				return ret;
+
+			return  kmx61_set_mode(data, mode,
+					       KMX61_ACC | KMX61_MAG, true);
+		}
+	}
+	return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "Error reading who_am_i\n");
+		return ret;
+	}
+
+	if (ret != KMX61_CHIP_ID) {
+		dev_err(&data->client->dev,
+			"Wrong chip id, got %x expected %x\n",
+			 ret, KMX61_CHIP_ID);

drivers/iio/imu/kmx61.c line 441

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+		return -EINVAL;
+	}
+
+	/* set accel 12bit, 4g range */
+	ret = kmx61_set_range(data, KMX61_RANGE_4G);
+	if (ret < 0)
+		return ret;
+
+	/* set acc/magn to OPERATION mode */
+	ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Just return kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Quoted 39 lines
drivers/iio/imu/kmx61.c new file +39 -0
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to be set
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is in
+ * OPERATION then we know that kmx61_runtime_resume was already called
+ * so we must set ACC OPERATION mode here. The same happens when MAG power
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
+{
+#ifdef CONFIG_PM_RUNTIME
+	int ret;
+
+	if (device & KMX61_ACC) {
+		if (on && !data->acc_ps && !data->mag_stby)
+			kmx61_set_mode(data, 0, KMX61_ACC, true);
+		data->acc_ps = on;
+	}
+	if (device & KMX61_MAG) {
+		if (on && !data->mag_ps && !data->acc_stby)
+			kmx61_set_mode(data, 0, KMX61_MAG, true);
+		data->mag_ps = on;
+	}
+
+	if (on) {
+		ret = pm_runtime_get_sync(&data->client->dev);
+	} else {
+		pm_runtime_mark_last_busy(&data->client->dev);
+		ret = pm_runtime_put_autosuspend(&data->client->dev);
+	}
+	if (ret < 0) {

drivers/iio/imu/kmx61.c line 490

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+		dev_err(&data->client->dev,
+			"Failed: kmx61_set_power_state for %d, ret %d\n",
+			on, ret);
+		return ret;
+	}
+#endif
+	return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base, int offset)

u8 base, u8 offset?

drivers/iio/imu/kmx61.c line 498

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+{
+	int ret;
+	u8 reg = base + offset * 2;
+
+	ret = i2c_smbus_read_word_data(data->client, reg);
+	if (ret < 0) {
+		dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
+		return ret;

This return is unnecessary.

drivers/iio/imu/kmx61.c line 506

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+	}
+
+	return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan, int *val,
+			      int *val2, long mask)

Indentation of the parameters is a bit too high.

Quoted 41 lines
drivers/iio/imu/kmx61.c new file +41 -0
+{
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+	u8 base_reg;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		switch (chan->type) {
+		case IIO_ACCEL:
+		case IIO_MAGN:
+			base_reg = KMX61_ACC_XOUT_L;
+			break;
+		default:
+			return -EINVAL;
+		}
+		mutex_lock(&data->lock);
+
+		kmx61_set_power_state(data, true, chan->address);
+		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
+		if (ret < 0) {
+			kmx61_set_power_state(data, false, chan->address);
+			mutex_unlock(&data->lock);
+			return ret;
+		}
+		*val = sign_extend32(ret >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
+		kmx61_set_power_state(data, false, chan->address);
+
+		mutex_unlock(&data->lock);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			*val = 0;
+			*val2 = kmx61_scale_table[data->range].uscale;
+			return IIO_VAL_INT_PLUS_MICRO;
+		case IIO_MAGN:
+			/* 14 bits res, 1465 microGauss per magn count */
+			*val = 0;
+			*val2 = 1465;
+			return IIO_VAL_INT_PLUS_MICRO;

drivers/iio/imu/kmx61.c line 557

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		ret = kmx61_get_odr(data, val, val2, chan->address);
+		mutex_unlock(&data->lock);

Could be just return ret here, if kmx61_get_odr() returns IIO_VAL_INT_PLUS_MICRO on success and -EINVAL on error.

drivers/iio/imu/kmx61.c line 567

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+		if (ret)
+			return -EINVAL;
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+	return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan, int val,
+			       int val2, long mask)

Indentation of parameters is too high.

Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+{
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+			return -EINVAL;
+
+		mutex_lock(&data->lock);
+		ret = kmx61_set_odr(data, val, val2, chan->address);
+		mutex_unlock(&data->lock);
+		return ret;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:

drivers/iio/imu/kmx61.c line 593

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+			if (val != 0)
+				return -EINVAL;
+			mutex_lock(&data->lock);
+			ret = kmx61_set_scale(data, val2);
+			mutex_unlock(&data->lock);
+			return ret;
+		default:
+			return -EINVAL;
+		}
+		return ret;

This won't be reached (if so, ret would not be initialized).

drivers/iio/imu/kmx61.c line 529

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+	default:
+		return -EINVAL;
+	}
+	return ret;

Same here.

Quoted 170 lines
drivers/iio/imu/kmx61.c new file +169 -0
+}
+
+static const struct iio_info kmx61_info = {
+	.driver_module		= THIS_MODULE,
+	.read_raw		= kmx61_read_raw,
+	.write_raw		= kmx61_write_raw,
+	.attrs			= &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
+		       const struct i2c_device_id *id)
+{
+	struct kmx61_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+	const char *name = NULL;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	if (id)
+		name = id->name;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->channels = kmx61_channels;
+	indio_dev->num_channels = ARRAY_SIZE(kmx61_channels);
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &kmx61_info;
+
+	mutex_init(&data->lock);
+
+	ret = kmx61_chip_init(data);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(&client->dev, "Failed to register iio device\n");
+		goto err_iio_device_register;
+	}
+
+	ret = pm_runtime_set_active(&client->dev);
+	if (ret < 0)
+		goto err_pm_runtime_set_active;
+
+	pm_runtime_enable(&client->dev);
+	pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(&client->dev);
+
+	return 0;
+
+err_pm_runtime_set_active:
+	iio_device_unregister(indio_dev);
+err_iio_device_register:
+	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_set_suspended(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+
+	iio_device_unregister(indio_dev);
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+			     false);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	u8 stby = 0;
+
+	if (data->acc_stby)
+		stby |= KMX61_ACC_STBY_BIT;
+	if (data->mag_stby)
+		stby |= KMX61_MAG_STBY_BIT;
+
+	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->lock);
+	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+	struct kmx61_data *data = iio_priv(indio_dev);
+	u8 stby = 0;
+
+	if (!data->acc_ps)
+		stby |= KMX61_ACC_STBY_BIT;
+	if (!data->mag_ps)
+		stby |= KMX61_MAG_STBY_BIT;
+
+	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+	SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
+};
+
+static const struct i2c_device_id kmx61_id[] = {
+	{"kmx611021", 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver = {
+	.driver = {
+		.name = KMX61_DRV_NAME,
+		.pm = &kmx61_pm_ops,
+	},
+	.probe		= kmx61_probe,
+	.remove		= kmx61_remove,
+	.id_table	= kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");
#3 lore raw
Quoted 32 lines · Hartmut Knaack

Daniel Baluta schrieb am 18.11.2014 17:47:

Quoted 6 lines

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It exports raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

Finally managed to have a close look, and found a few issues.

Quoted 24 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and Peter
* http://marc.info/?l=linux-iio&m=141564019602400&w=2
* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +
 drivers/iio/imu/kmx61.c  | 766 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +0 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
        Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
        ADIS16485, ADIS16488 inertial sensors.
 
Quoted 10 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 36

Quoted 9 lines
drivers/iio/imu/Kconfig +9 -0
+config KMX61
+     tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+     depends on I2C
+     help
+       Say Y here if you want to build a driver for Kionix KMX61 6-axis accelerometer
+       and magnetometer.
+       To compile this driver as module, choose M here: the module will be called
+       kmx61.
+

This help text is more than 80 characters wide and may better be wrapped a bit differently.

Good catch. checkpatch.pl didn't complain.

Quoted 105 lines
Quoted 78 lines
drivers/iio/imu/Kconfig +0 -0
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +2 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += inv_mpu6050/
+
+obj-$(CONFIG_KMX61) += kmx61.o
drivers/iio/imu/kmx61.c new file +59 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I   0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L     0x0A
+#define KMX61_ACC_XOUT_H     0x0B
+#define KMX61_ACC_YOUT_L     0x0C
+#define KMX61_ACC_YOUT_H     0x0D
+#define KMX61_ACC_ZOUT_L     0x0E
+#define KMX61_ACC_ZOUT_H     0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L         0x10
+#define KMX61_TEMP_H         0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L     0x12
+#define KMX61_MAG_XOUT_H     0x13
+#define KMX61_MAG_YOUT_L     0x14
+#define KMX61_MAG_YOUT_H     0x15
+#define KMX61_MAG_ZOUT_L     0x16
+#define KMX61_MAG_ZOUT_H     0x17
+
+#define KMX61_REG_ODCNTL     0x2C
+#define KMX61_REG_STBY               0x29
+#define KMX61_REG_CTRL1              0x2A
+

drivers/iio/imu/kmx61.c line 69

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+#define KMX61_ACC_STBY_BIT   BIT(0)
+#define KMX61_MAG_STBY_BIT   BIT(1)
+#define KMX61_ACT_STBY_BIT   BIT(7)
+
+#define KMX61_ALL_STBY               (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT  0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT  1
+#define KMX61_REG_CTRL1_GSEL0_MASK   0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK   0x02

I don't see the benefit of separating GSEL0 and GSEL1.

Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+
+#define KMX61_REG_CTRL1_BIT_RES              BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT  0
+#define KMX61_MAG_ODR_SHIFT  4
+#define KMX61_ACC_ODR_MASK   0x0F
+#define KMX61_MAG_ODR_MASK   0xF0
+
+#define KMX61_SLEEP_DELAY_MS 2000
+
+#define KMX61_CHIP_ID                0x12
+
+struct kmx61_data {
+     struct i2c_client *client;
+
+     /* serialize access to non-atomic ops, e.g set_mode */
Quoted 8 lines

drivers/iio/imu/kmx61.c line 92

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct mutex lock;
+     u8 range;
+     u8 odr_bits;
+
+     /* standby state */
+     u8 acc_stby;
+     u8 mag_stby;

Why u8 instead of bool?

Hmm, correct. I was thinking bitwise, but you are right. Will fix.

Quoted 23 lines
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+
+     /* power state */
+     bool acc_ps;
+     bool mag_ps;
+};
+
+enum kmx61_range {
+     KMX61_RANGE_2G,
+     KMX61_RANGE_4G,
+     KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+     KMX61_SCAN_ACC_X,
+     KMX61_SCAN_ACC_Y,
+     KMX61_SCAN_ACC_Z,
+     KMX61_SCAN_TEMP,
+     KMX61_SCAN_MAG_X,
+     KMX61_SCAN_MAG_Y,
+     KMX61_SCAN_MAG_Z,
+};
+
+static const struct {
Quoted 8 lines

drivers/iio/imu/kmx61.c line 122

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     u16 uscale;
+     u8 gsel0;
+     u8 gsel1;
+} kmx61_scale_table[] = {
+     {9582, 0, 0},
+     {19163, 1, 0},
+     {38326, 0, 1},

Why not consolidate gsel0 and gsel1 to gsel? Then this could become u16 kmx61_scale[] = {9582, 19163, 38326} with gsel as index.

True. Will fix.

Quoted 88 lines
Quoted 88 lines
drivers/iio/imu/kmx61.c new file +88 -0
+};
+
+/* KMX61 devices */
+#define KMX61_ACC    0x01
+#define KMX61_MAG    0x02
+
+static const struct {
+     int val;
+     int val2;
+     u8 odr_bits;
+} kmx61_samp_freq_table[] = { {12, 500000, 0x00},
+                     {25, 0, 0x01},
+                     {50, 0, 0x02},
+                     {100, 0, 0x03},
+                     {200, 0, 0x04},
+                     {400, 0, 0x05},
+                     {800, 0, 0x06},
+                     {1600, 0, 0x07},
+                     {0, 781000, 0x08},
+                     {1, 563000, 0x09},
+                     {3, 125000, 0x0A},
+                     {6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+     "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
+
+static struct attribute *kmx61_attributes[] = {
+     &iio_const_attr_accel_scale_available.dev_attr.attr,
+     &iio_const_attr_magn_scale_available.dev_attr.attr,
+     &iio_const_attr_sampling_frequency_available.dev_attr.attr,
+     NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group = {
+     .attrs = kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+     .type = IIO_ACCEL, \
+     .modified = 1, \
+     .channel2 = IIO_MOD_ ## _axis, \
+     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .address = KMX61_ACC, \
+     .scan_index = _index, \
+     .scan_type = { \
+             .sign = 's', \
+             .realbits = 12, \
+             .storagebits = 16, \
+             .shift = 4, \
+             .endianness = IIO_LE, \
+     }, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+     .type = IIO_MAGN, \
+     .modified = 1, \
+     .channel2 = IIO_MOD_ ## _axis, \
+     .address = KMX61_MAG, \
+     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .scan_index = _index, \
+     .scan_type = { \
+             .sign = 's', \
+             .realbits = 14, \
+             .storagebits = 16, \
+             .shift = 2, \
+             .endianness = IIO_LE, \
+     }, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] = {
+     KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+     KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+     KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+     KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+     KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+     KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)
+{
+     int i;
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 217

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (val == kmx61_samp_freq_table[i].val &&
+                 val2 == kmx61_samp_freq_table[i].val2)
+                     return kmx61_samp_freq_table[i].odr_bits;
+     return -EINVAL;
+}
+/**

Like here, the first line of the following multiline comments have one asterisk too much.

I think this is the convention, when adding doxygen style documentation:
http://lxr.free-electrons.com/source/fs/eventpoll.c#L382

Quoted 45 lines
Quoted 45 lines
drivers/iio/imu/kmx61.c new file +45 -0
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be set
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operating modes
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
+ * if they are both enabled. Internal sensors state is saved in acc_stby and
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
+                       bool update)
+{
+     int ret;
+     int acc_stby = -1, mag_stby = -1;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     if (device & KMX61_ACC) {
+             if (mode & KMX61_ACC_STBY_BIT) {
+                     ret |= KMX61_ACC_STBY_BIT;
+                     acc_stby = 1;
+             } else {
+                     ret &= ~KMX61_ACC_STBY_BIT;
+                     acc_stby = 0;
+             }
+     }
+
+     if (device & KMX61_MAG) {
+             if (mode & KMX61_MAG_STBY_BIT) {
+                     ret |= KMX61_MAG_STBY_BIT;
+                     mag_stby = 1;
+             } else {
+                     ret &= ~KMX61_MAG_STBY_BIT;
+                     mag_stby = 0;
+             }
+     }
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_stby\n");
Quoted 8 lines

drivers/iio/imu/kmx61.c line 269

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+     }
+
+     if (acc_stby != -1 && update)
+             data->acc_stby = !!acc_stby;
+     if (mag_stby != -1 && update)
+             data->mag_stby = !!mag_stby;

Why is there a need for double negation?

Correct. Now it doesn't make sense since data->acc_stby is u8.

But, with your suggestion if I will use bool to represent data->acc_stby then I think the code should use double negation to make it clear that data->acc_stby is bool and acc_stby is int.

Quoted 3 lines

drivers/iio/imu/kmx61.c line 271

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     return ret;

Since no error occured until this point, just return 0.

Ok. Will fix.

Quoted 19 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {

drivers/iio/imu/kmx61.c line 289

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     *mode = 0;
+
+     if (device & KMX61_ACC) {
+             if (ret & KMX61_ACC_STBY_BIT)
+                     *mode |= KMX61_ACC_STBY_BIT;
+             else
+                     *mode &= ~KMX61_ACC_STBY_BIT;

Simply:

Quoted 1 line
                *mode |= ret & KMX61_ACC_STBY_BIT;

Ok.

Quoted 8 lines

drivers/iio/imu/kmx61.c line 296

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     if (device & KMX61_MAG) {
+             if (ret & KMX61_MAG_STBY_BIT)
+                     *mode |= KMX61_MAG_STBY_BIT;
+             else
+                     *mode &= ~KMX61_MAG_STBY_BIT;

Same here:

Ok.

Quoted 24 lines
                *mode |= ret & KMX61_MAG_STBY_BIT;
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+     }
+
+     return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
+{
+     int ret;
+     u8 mode;
+     int lodr_bits, odr_bits;
+
+     ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+     if (ret < 0)
+             return ret;
+
+     lodr_bits = kmx61_convert_freq_to_bit(val, val2);
+     if (lodr_bits < 0)
+             return lodr_bits;
+
+     /* To change ODR, accel and magn must be in STDBY */
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+                          true);
+     if (ret < 0)
Quoted 8 lines

drivers/iio/imu/kmx61.c line 326

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+
+     odr_bits = 0;
+     if (device & KMX61_ACC)
+             odr_bits |= lodr_bits;
+     if (device & KMX61_MAG)
+             odr_bits |= (lodr_bits << KMX61_MAG_ODR_SHIFT);

No need for parenthesis.

ok.

Quoted 10 lines
Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
+                                     odr_bits);
+     if (ret < 0)
+             return ret;
+
+     ret = kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
+     if (ret < 0)
+             return ret;
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 343

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     data->odr_bits = lodr_bits;
+
+     return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 device)

Usually wrap parameters? And don't put code on line with {.

Will fix.

Quoted 7 lines

drivers/iio/imu/kmx61.c line 349

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{    int i;
+     u8 lodr_bits;
+
+     if (device & KMX61_ACC)
+             lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+                          KMX61_ACC_ODR_MASK;

KMX61_ACC_ODR_SHIFT is just a placebo and not used in kmx61_set_odr. Choose one way and stay consistent.

placebo is good. will fix kmx61_set_odr :)

Quoted 11 lines

drivers/iio/imu/kmx61.c line 360

Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+     else if (device & KMX61_MAG)
+             lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+                          KMX61_MAG_ODR_MASK;
+     else
+             return -EINVAL;
+
+     for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) {
+                     *val = kmx61_samp_freq_table[i].val;
+                     *val2 = kmx61_samp_freq_table[i].val2;
+                     return 0;
Quoted 1 line

return IIO_VAL_INT_PLUS_MICRO allows you to faciliate _read_raw a bit to just return ret there.

Hmm, If you don't mind I want to keep this as it is, just to be consistent with the rest of the functions. The convention used is to return 0 in case of success.

Quoted 74 lines
Quoted 74 lines
drivers/iio/imu/kmx61.c new file +74 -0
+             }
+     return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
+             return ret;
+     }
+
+     ret &= ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_MASK);
+     ret |= kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL0_SHIFT;
+     ret |= kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL1_SHIFT;
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
+             return ret;
+     }
+
+     data->range = range;
+
+     return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+     int ret, i;
+     u8  mode;
+
+     for (i = 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
+             if (kmx61_scale_table[i].uscale == uscale) {
+                     ret = kmx61_get_mode(data, &mode,
+                                          KMX61_ACC | KMX61_MAG);
+                     if (ret < 0)
+                             return ret;
+
+                     ret = kmx61_set_mode(data, KMX61_ALL_STBY,
+                                          KMX61_ACC | KMX61_MAG, true);
+                     if (ret < 0)
+                             return ret;
+
+                     ret = kmx61_set_range(data, i);
+                     if (ret < 0)
+                             return ret;
+
+                     return  kmx61_set_mode(data, mode,
+                                            KMX61_ACC | KMX61_MAG, true);
+             }
+     }
+     return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading who_am_i\n");
+             return ret;
+     }
+
+     if (ret != KMX61_CHIP_ID) {
+             dev_err(&data->client->dev,
+                     "Wrong chip id, got %x expected %x\n",
+                      ret, KMX61_CHIP_ID);
+             return -EINVAL;
+     }
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 441

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     /* set accel 12bit, 4g range */
+     ret = kmx61_set_range(data, KMX61_RANGE_4G);
+     if (ret < 0)
+             return ret;
+
+     /* set acc/magn to OPERATION mode */
+     ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Just return kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Ok.

Quoted 42 lines
Quoted 42 lines
drivers/iio/imu/kmx61.c new file +42 -0
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to be set
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is in
+ * OPERATION then we know that kmx61_runtime_resume was already called
+ * so we must set ACC OPERATION mode here. The same happens when MAG power
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
+{
+#ifdef CONFIG_PM_RUNTIME
+     int ret;
+
+     if (device & KMX61_ACC) {
+             if (on && !data->acc_ps && !data->mag_stby)
+                     kmx61_set_mode(data, 0, KMX61_ACC, true);
+             data->acc_ps = on;
+     }
+     if (device & KMX61_MAG) {
+             if (on && !data->mag_ps && !data->acc_stby)
+                     kmx61_set_mode(data, 0, KMX61_MAG, true);
+             data->mag_ps = on;
+     }
+
+     if (on) {
+             ret = pm_runtime_get_sync(&data->client->dev);
+     } else {
+             pm_runtime_mark_last_busy(&data->client->dev);
+             ret = pm_runtime_put_autosuspend(&data->client->dev);
+     }
+     if (ret < 0) {
+             dev_err(&data->client->dev,
+                     "Failed: kmx61_set_power_state for %d, ret %d\n",
+                     on, ret);
Quoted 8 lines

drivers/iio/imu/kmx61.c line 490

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+     }
+#endif
+     return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base, int offset)

u8 base, u8 offset?

Correct.

Quoted 9 lines

drivers/iio/imu/kmx61.c line 498

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+{
+     int ret;
+     u8 reg = base + offset * 2;
+
+     ret = i2c_smbus_read_word_data(data->client, reg);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
+             return ret;

This return is unnecessary.

Ok.

Quoted 9 lines

drivers/iio/imu/kmx61.c line 506

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     }
+
+     return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+                           struct iio_chan_spec const *chan, int *val,
+                           int *val2, long mask)

Indentation of the parameters is a bit too high.

Ok, will fix.

Quoted 44 lines
Quoted 44 lines
drivers/iio/imu/kmx61.c new file +44 -0
+{
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+     u8 base_reg;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW:
+             switch (chan->type) {
+             case IIO_ACCEL:
+             case IIO_MAGN:
+                     base_reg = KMX61_ACC_XOUT_L;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+             mutex_lock(&data->lock);
+
+             kmx61_set_power_state(data, true, chan->address);
+             ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
+             if (ret < 0) {
+                     kmx61_set_power_state(data, false, chan->address);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             }
+             *val = sign_extend32(ret >> chan->scan_type.shift,
+                                  chan->scan_type.realbits - 1);
+             kmx61_set_power_state(data, false, chan->address);
+
+             mutex_unlock(&data->lock);
+             return IIO_VAL_INT;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:
+                     *val = 0;
+                     *val2 = kmx61_scale_table[data->range].uscale;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             case IIO_MAGN:
+                     /* 14 bits res, 1465 microGauss per magn count */
+                     *val = 0;
+                     *val2 = 1465;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             default:
+                     return -EINVAL;
+             }
Quoted 8 lines

drivers/iio/imu/kmx61.c line 557

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret = kmx61_get_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);

Could be just return ret here, if kmx61_get_odr() returns IIO_VAL_INT_PLUS_MICRO on success and -EINVAL on error.

As explained above, I prefer not to do this. More than that, I can't do ret here because the data->lock is held.

Quoted 11 lines

drivers/iio/imu/kmx61.c line 567

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             if (ret)
+                     return -EINVAL;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+     return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan, int val,
+                            int val2, long mask)

Indentation of parameters is too high.

Ok.

Quoted 27 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+{
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret = kmx61_set_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);
+             return ret;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:

drivers/iio/imu/kmx61.c line 593

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+                     if (val != 0)
+                             return -EINVAL;
+                     mutex_lock(&data->lock);
+                     ret = kmx61_set_scale(data, val2);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             default:
+                     return -EINVAL;
+             }
+             return ret;

This won't be reached (if so, ret would not be initialized).

Quoted 5 lines

drivers/iio/imu/kmx61.c line 529

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+     default:
+             return -EINVAL;
+     }
+     return ret;

Same here.

I know. Would initializing ret with 0 will be enough?

Quoted 161 lines
Quoted 161 lines
drivers/iio/imu/kmx61.c new file +161 -0
+}
+
+static const struct iio_info kmx61_info = {
+     .driver_module          = THIS_MODULE,
+     .read_raw               = kmx61_read_raw,
+     .write_raw              = kmx61_write_raw,
+     .attrs                  = &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
+                    const struct i2c_device_id *id)
+{
+     struct kmx61_data *data;
+     struct iio_dev *indio_dev;
+     int ret;
+     const char *name = NULL;
+
+     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+     if (!indio_dev)
+             return -ENOMEM;
+
+     data = iio_priv(indio_dev);
+     i2c_set_clientdata(client, indio_dev);
+     data->client = client;
+
+     if (id)
+             name = id->name;
+
+     indio_dev->dev.parent = &client->dev;
+     indio_dev->channels = kmx61_channels;
+     indio_dev->num_channels = ARRAY_SIZE(kmx61_channels);
+     indio_dev->name = name;
+     indio_dev->modes = INDIO_DIRECT_MODE;
+     indio_dev->info = &kmx61_info;
+
+     mutex_init(&data->lock);
+
+     ret = kmx61_chip_init(data);
+     if (ret < 0)
+             return ret;
+
+     ret = iio_device_register(indio_dev);
+     if (ret < 0) {
+             dev_err(&client->dev, "Failed to register iio device\n");
+             goto err_iio_device_register;
+     }
+
+     ret = pm_runtime_set_active(&client->dev);
+     if (ret < 0)
+             goto err_pm_runtime_set_active;
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
+     pm_runtime_use_autosuspend(&client->dev);
+
+     return 0;
+
+err_pm_runtime_set_active:
+     iio_device_unregister(indio_dev);
+err_iio_device_register:
+     kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(client);
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     iio_device_unregister(indio_dev);
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+                          false);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     u8 stby = 0;
+
+     if (data->acc_stby)
+             stby |= KMX61_ACC_STBY_BIT;
+     if (data->mag_stby)
+             stby |= KMX61_MAG_STBY_BIT;
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     u8 stby = 0;
+
+     if (!data->acc_ps)
+             stby |= KMX61_ACC_STBY_BIT;
+     if (!data->mag_ps)
+             stby |= KMX61_MAG_STBY_BIT;
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops = {
+     SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+     SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
+};
+
+static const struct i2c_device_id kmx61_id[] = {
+     {"kmx611021", 0},
+     {}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver = {
+     .driver = {
+             .name = KMX61_DRV_NAME,
+             .pm = &kmx61_pm_ops,
+     },
+     .probe          = kmx61_probe,
+     .remove         = kmx61_remove,
Quoted 8 lines

drivers/iio/imu/kmx61.c line 766

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     .id_table       = kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");

Hartmut, thanks a lot for your reviews. I will send a patch series with these cleanups together with changes for ACPI, buffer and trigger support.

Daniel.

#4 lore raw

Daniel Baluta schrieb am 25.11.2014 10:54:

Quoted 350 lines · Hartmut Knaack
Quoted 32 lines

Daniel Baluta schrieb am 18.11.2014 17:47:

Quoted 6 lines

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It exports raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

Finally managed to have a close look, and found a few issues.

Quoted 24 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and Peter
* http://marc.info/?l=linux-iio&m=141564019602400&w=2
* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +
 drivers/iio/imu/kmx61.c  | 766 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +0 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
        Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
        ADIS16485, ADIS16488 inertial sensors.
 
Quoted 10 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 36

Quoted 9 lines
drivers/iio/imu/Kconfig +9 -0
+config KMX61
+     tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+     depends on I2C
+     help
+       Say Y here if you want to build a driver for Kionix KMX61 6-axis accelerometer
+       and magnetometer.
+       To compile this driver as module, choose M here: the module will be called
+       kmx61.
+

This help text is more than 80 characters wide and may better be wrapped a bit differently.

Good catch. checkpatch.pl didn't complain.

Quoted 105 lines
Quoted 78 lines
drivers/iio/imu/Kconfig +0 -0
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +2 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += inv_mpu6050/
+
+obj-$(CONFIG_KMX61) += kmx61.o
drivers/iio/imu/kmx61.c new file +59 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I   0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L     0x0A
+#define KMX61_ACC_XOUT_H     0x0B
+#define KMX61_ACC_YOUT_L     0x0C
+#define KMX61_ACC_YOUT_H     0x0D
+#define KMX61_ACC_ZOUT_L     0x0E
+#define KMX61_ACC_ZOUT_H     0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L         0x10
+#define KMX61_TEMP_H         0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L     0x12
+#define KMX61_MAG_XOUT_H     0x13
+#define KMX61_MAG_YOUT_L     0x14
+#define KMX61_MAG_YOUT_H     0x15
+#define KMX61_MAG_ZOUT_L     0x16
+#define KMX61_MAG_ZOUT_H     0x17
+
+#define KMX61_REG_ODCNTL     0x2C
+#define KMX61_REG_STBY               0x29
+#define KMX61_REG_CTRL1              0x2A
+

drivers/iio/imu/kmx61.c line 69

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+#define KMX61_ACC_STBY_BIT   BIT(0)
+#define KMX61_MAG_STBY_BIT   BIT(1)
+#define KMX61_ACT_STBY_BIT   BIT(7)
+
+#define KMX61_ALL_STBY               (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT  0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT  1
+#define KMX61_REG_CTRL1_GSEL0_MASK   0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK   0x02

I don't see the benefit of separating GSEL0 and GSEL1.

Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+
+#define KMX61_REG_CTRL1_BIT_RES              BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT  0
+#define KMX61_MAG_ODR_SHIFT  4
+#define KMX61_ACC_ODR_MASK   0x0F
+#define KMX61_MAG_ODR_MASK   0xF0
+
+#define KMX61_SLEEP_DELAY_MS 2000
+
+#define KMX61_CHIP_ID                0x12
+
+struct kmx61_data {
+     struct i2c_client *client;
+
+     /* serialize access to non-atomic ops, e.g set_mode */
Quoted 8 lines

drivers/iio/imu/kmx61.c line 92

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct mutex lock;
+     u8 range;
+     u8 odr_bits;
+
+     /* standby state */
+     u8 acc_stby;
+     u8 mag_stby;

Why u8 instead of bool?

Hmm, correct. I was thinking bitwise, but you are right. Will fix.

Quoted 23 lines
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+
+     /* power state */
+     bool acc_ps;
+     bool mag_ps;
+};
+
+enum kmx61_range {
+     KMX61_RANGE_2G,
+     KMX61_RANGE_4G,
+     KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+     KMX61_SCAN_ACC_X,
+     KMX61_SCAN_ACC_Y,
+     KMX61_SCAN_ACC_Z,
+     KMX61_SCAN_TEMP,
+     KMX61_SCAN_MAG_X,
+     KMX61_SCAN_MAG_Y,
+     KMX61_SCAN_MAG_Z,
+};
+
+static const struct {
Quoted 8 lines

drivers/iio/imu/kmx61.c line 122

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     u16 uscale;
+     u8 gsel0;
+     u8 gsel1;
+} kmx61_scale_table[] = {
+     {9582, 0, 0},
+     {19163, 1, 0},
+     {38326, 0, 1},

Why not consolidate gsel0 and gsel1 to gsel? Then this could become u16 kmx61_scale[] = {9582, 19163, 38326} with gsel as index.

True. Will fix.

Quoted 88 lines
Quoted 88 lines
drivers/iio/imu/kmx61.c new file +88 -0
+};
+
+/* KMX61 devices */
+#define KMX61_ACC    0x01
+#define KMX61_MAG    0x02
+
+static const struct {
+     int val;
+     int val2;
+     u8 odr_bits;
+} kmx61_samp_freq_table[] = { {12, 500000, 0x00},
+                     {25, 0, 0x01},
+                     {50, 0, 0x02},
+                     {100, 0, 0x03},
+                     {200, 0, 0x04},
+                     {400, 0, 0x05},
+                     {800, 0, 0x06},
+                     {1600, 0, 0x07},
+                     {0, 781000, 0x08},
+                     {1, 563000, 0x09},
+                     {3, 125000, 0x0A},
+                     {6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+     "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
+
+static struct attribute *kmx61_attributes[] = {
+     &iio_const_attr_accel_scale_available.dev_attr.attr,
+     &iio_const_attr_magn_scale_available.dev_attr.attr,
+     &iio_const_attr_sampling_frequency_available.dev_attr.attr,
+     NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group = {
+     .attrs = kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+     .type = IIO_ACCEL, \
+     .modified = 1, \
+     .channel2 = IIO_MOD_ ## _axis, \
+     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .address = KMX61_ACC, \
+     .scan_index = _index, \
+     .scan_type = { \
+             .sign = 's', \
+             .realbits = 12, \
+             .storagebits = 16, \
+             .shift = 4, \
+             .endianness = IIO_LE, \
+     }, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+     .type = IIO_MAGN, \
+     .modified = 1, \
+     .channel2 = IIO_MOD_ ## _axis, \
+     .address = KMX61_MAG, \
+     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .scan_index = _index, \
+     .scan_type = { \
+             .sign = 's', \
+             .realbits = 14, \
+             .storagebits = 16, \
+             .shift = 2, \
+             .endianness = IIO_LE, \
+     }, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] = {
+     KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+     KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+     KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+     KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+     KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+     KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)
+{
+     int i;
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 217

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (val == kmx61_samp_freq_table[i].val &&
+                 val2 == kmx61_samp_freq_table[i].val2)
+                     return kmx61_samp_freq_table[i].odr_bits;
+     return -EINVAL;
+}
+/**

Like here, the first line of the following multiline comments have one asterisk too much.

I think this is the convention, when adding doxygen style documentation:
http://lxr.free-electrons.com/source/fs/eventpoll.c#L382

Quoted 45 lines
Quoted 45 lines
drivers/iio/imu/kmx61.c new file +45 -0
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be set
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operating modes
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
+ * if they are both enabled. Internal sensors state is saved in acc_stby and
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
+                       bool update)
+{
+     int ret;
+     int acc_stby = -1, mag_stby = -1;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     if (device & KMX61_ACC) {
+             if (mode & KMX61_ACC_STBY_BIT) {
+                     ret |= KMX61_ACC_STBY_BIT;
+                     acc_stby = 1;
+             } else {
+                     ret &= ~KMX61_ACC_STBY_BIT;
+                     acc_stby = 0;
+             }
+     }
+
+     if (device & KMX61_MAG) {
+             if (mode & KMX61_MAG_STBY_BIT) {
+                     ret |= KMX61_MAG_STBY_BIT;
+                     mag_stby = 1;
+             } else {
+                     ret &= ~KMX61_MAG_STBY_BIT;
+                     mag_stby = 0;
+             }
+     }
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_stby\n");
Quoted 8 lines

drivers/iio/imu/kmx61.c line 269

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+     }
+
+     if (acc_stby != -1 && update)
+             data->acc_stby = !!acc_stby;
+     if (mag_stby != -1 && update)
+             data->mag_stby = !!mag_stby;

Why is there a need for double negation?

Quoted 6 lines

Correct. Now it doesn't make sense since data->acc_stby is u8.

But, with your suggestion if I will use bool to represent data->acc_stby then I think the code should use double negation to make it clear that data->acc_stby is bool and acc_stby is int.

Well, if it makes you happy, then go ahead. My interpretation of a bool is, that an assignment of 0 becomes 0/false, and an assignment of anything else becomes 1/true. So, I wouldn't see a need here (especially since acc_stby/mag_stby were distinctly assigned either 0 or 1).

Quoted 114 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c line 271

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     return ret;

Since no error occured until this point, just return 0.

Ok. Will fix.

Quoted 19 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {

drivers/iio/imu/kmx61.c line 289

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     *mode = 0;
+
+     if (device & KMX61_ACC) {
+             if (ret & KMX61_ACC_STBY_BIT)
+                     *mode |= KMX61_ACC_STBY_BIT;
+             else
+                     *mode &= ~KMX61_ACC_STBY_BIT;

Simply:

Quoted 1 line
                *mode |= ret & KMX61_ACC_STBY_BIT;

Ok.

Quoted 8 lines

drivers/iio/imu/kmx61.c line 296

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     if (device & KMX61_MAG) {
+             if (ret & KMX61_MAG_STBY_BIT)
+                     *mode |= KMX61_MAG_STBY_BIT;
+             else
+                     *mode &= ~KMX61_MAG_STBY_BIT;

Same here:

Ok.

Quoted 24 lines
                *mode |= ret & KMX61_MAG_STBY_BIT;
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+     }
+
+     return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
+{
+     int ret;
+     u8 mode;
+     int lodr_bits, odr_bits;
+
+     ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+     if (ret < 0)
+             return ret;
+
+     lodr_bits = kmx61_convert_freq_to_bit(val, val2);
+     if (lodr_bits < 0)
+             return lodr_bits;
+
+     /* To change ODR, accel and magn must be in STDBY */
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+                          true);
+     if (ret < 0)
Quoted 8 lines

drivers/iio/imu/kmx61.c line 326

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+
+     odr_bits = 0;
+     if (device & KMX61_ACC)
+             odr_bits |= lodr_bits;
+     if (device & KMX61_MAG)
+             odr_bits |= (lodr_bits << KMX61_MAG_ODR_SHIFT);

No need for parenthesis.

ok.

Quoted 10 lines
Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
+                                     odr_bits);
+     if (ret < 0)
+             return ret;
+
+     ret = kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
+     if (ret < 0)
+             return ret;
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 343

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     data->odr_bits = lodr_bits;
+
+     return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 device)

Usually wrap parameters? And don't put code on line with {.

Will fix.

Quoted 7 lines

drivers/iio/imu/kmx61.c line 349

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{    int i;
+     u8 lodr_bits;
+
+     if (device & KMX61_ACC)
+             lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+                          KMX61_ACC_ODR_MASK;

KMX61_ACC_ODR_SHIFT is just a placebo and not used in kmx61_set_odr. Choose one way and stay consistent.

placebo is good. will fix kmx61_set_odr :)

Quoted 11 lines
Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+     else if (device & KMX61_MAG)
+             lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+                          KMX61_MAG_ODR_MASK;
+     else
+             return -EINVAL;
+
+     for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) {
+                     *val = kmx61_samp_freq_table[i].val;
+                     *val2 = kmx61_samp_freq_table[i].val2;
+                     return 0;
Quoted 6 lines
Quoted 1 line

return IIO_VAL_INT_PLUS_MICRO allows you to faciliate _read_raw a bit to just return ret there.

Hmm, If you don't mind I want to keep this as it is, just to be consistent with the rest of the functions. The convention used is to return 0 in case of success.

Up to you. Though kmx61_read_raw() would be an example of a function that doesn't follow this convention ;-) And there is no real benefit of checking an error code there, which is passed up anyway.

Quoted 215 lines
Quoted 74 lines
Quoted 74 lines
drivers/iio/imu/kmx61.c new file +74 -0
+             }
+     return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
+             return ret;
+     }
+
+     ret &= ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_MASK);
+     ret |= kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL0_SHIFT;
+     ret |= kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL1_SHIFT;
+
+     ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
+             return ret;
+     }
+
+     data->range = range;
+
+     return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+     int ret, i;
+     u8  mode;
+
+     for (i = 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
+             if (kmx61_scale_table[i].uscale == uscale) {
+                     ret = kmx61_get_mode(data, &mode,
+                                          KMX61_ACC | KMX61_MAG);
+                     if (ret < 0)
+                             return ret;
+
+                     ret = kmx61_set_mode(data, KMX61_ALL_STBY,
+                                          KMX61_ACC | KMX61_MAG, true);
+                     if (ret < 0)
+                             return ret;
+
+                     ret = kmx61_set_range(data, i);
+                     if (ret < 0)
+                             return ret;
+
+                     return  kmx61_set_mode(data, mode,
+                                            KMX61_ACC | KMX61_MAG, true);
+             }
+     }
+     return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+     int ret;
+
+     ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading who_am_i\n");
+             return ret;
+     }
+
+     if (ret != KMX61_CHIP_ID) {
+             dev_err(&data->client->dev,
+                     "Wrong chip id, got %x expected %x\n",
+                      ret, KMX61_CHIP_ID);
+             return -EINVAL;
+     }
+
Quoted 8 lines

drivers/iio/imu/kmx61.c line 441

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     /* set accel 12bit, 4g range */
+     ret = kmx61_set_range(data, KMX61_RANGE_4G);
+     if (ret < 0)
+             return ret;
+
+     /* set acc/magn to OPERATION mode */
+     ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Just return kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Ok.

Quoted 42 lines
Quoted 42 lines
drivers/iio/imu/kmx61.c new file +42 -0
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to be set
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is in
+ * OPERATION then we know that kmx61_runtime_resume was already called
+ * so we must set ACC OPERATION mode here. The same happens when MAG power
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
+{
+#ifdef CONFIG_PM_RUNTIME
+     int ret;
+
+     if (device & KMX61_ACC) {
+             if (on && !data->acc_ps && !data->mag_stby)
+                     kmx61_set_mode(data, 0, KMX61_ACC, true);
+             data->acc_ps = on;
+     }
+     if (device & KMX61_MAG) {
+             if (on && !data->mag_ps && !data->acc_stby)
+                     kmx61_set_mode(data, 0, KMX61_MAG, true);
+             data->mag_ps = on;
+     }
+
+     if (on) {
+             ret = pm_runtime_get_sync(&data->client->dev);
+     } else {
+             pm_runtime_mark_last_busy(&data->client->dev);
+             ret = pm_runtime_put_autosuspend(&data->client->dev);
+     }
+     if (ret < 0) {
+             dev_err(&data->client->dev,
+                     "Failed: kmx61_set_power_state for %d, ret %d\n",
+                     on, ret);
Quoted 8 lines

drivers/iio/imu/kmx61.c line 490

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+     }
+#endif
+     return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base, int offset)

u8 base, u8 offset?

Correct.

Quoted 9 lines

drivers/iio/imu/kmx61.c line 498

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+{
+     int ret;
+     u8 reg = base + offset * 2;
+
+     ret = i2c_smbus_read_word_data(data->client, reg);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
+             return ret;

This return is unnecessary.

Ok.

Quoted 9 lines

drivers/iio/imu/kmx61.c line 506

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     }
+
+     return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+                           struct iio_chan_spec const *chan, int *val,
+                           int *val2, long mask)

Indentation of the parameters is a bit too high.

Ok, will fix.

Quoted 44 lines
Quoted 44 lines
drivers/iio/imu/kmx61.c new file +44 -0
+{
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+     u8 base_reg;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW:
+             switch (chan->type) {
+             case IIO_ACCEL:
+             case IIO_MAGN:
+                     base_reg = KMX61_ACC_XOUT_L;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+             mutex_lock(&data->lock);
+
+             kmx61_set_power_state(data, true, chan->address);
+             ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
+             if (ret < 0) {
+                     kmx61_set_power_state(data, false, chan->address);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             }
+             *val = sign_extend32(ret >> chan->scan_type.shift,
+                                  chan->scan_type.realbits - 1);
+             kmx61_set_power_state(data, false, chan->address);
+
+             mutex_unlock(&data->lock);
+             return IIO_VAL_INT;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:
+                     *val = 0;
+                     *val2 = kmx61_scale_table[data->range].uscale;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             case IIO_MAGN:
+                     /* 14 bits res, 1465 microGauss per magn count */
+                     *val = 0;
+                     *val2 = 1465;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             default:
+                     return -EINVAL;
+             }
Quoted 8 lines

drivers/iio/imu/kmx61.c line 557

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret = kmx61_get_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);

Could be just return ret here, if kmx61_get_odr() returns IIO_VAL_INT_PLUS_MICRO on success and -EINVAL on error.

Quoted 3 lines

As explained above, I prefer not to do this. More than that, I can't do ret here because the data->lock is held.

Well, I actually intended to have an unconditional "return ret;" right after the mutex_unlock. But up to you. I also see a good point to make it obvious here, that the return value is IIO_VAL_INT_PLUS_MICRO. So, up to you.

Quoted 39 lines
Quoted 11 lines

drivers/iio/imu/kmx61.c line 567

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             if (ret)
+                     return -EINVAL;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+     return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan, int val,
+                            int val2, long mask)

Indentation of parameters is too high.

Ok.

Quoted 27 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+{
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret = kmx61_set_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);
+             return ret;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:

drivers/iio/imu/kmx61.c line 593

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+                     if (val != 0)
+                             return -EINVAL;
+                     mutex_lock(&data->lock);
+                     ret = kmx61_set_scale(data, val2);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             default:
+                     return -EINVAL;
+             }
+             return ret;

This won't be reached (if so, ret would not be initialized).

Quoted 7 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c line 529

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+     default:
+             return -EINVAL;
+     }
+     return ret;

Same here.

I know. Would initializing ret with 0 will be enough?

Since the switch already catches all cases and returns, there is no need for return ret, here. But if you prefer to have a return at the end of the function, you could drop the default case, or leave it empty, or just put a break in there - and return -EINVAL at the end.

Quoted 166 lines
Quoted 166 lines
Quoted 166 lines
drivers/iio/imu/kmx61.c new file +165 -0
+}
+
+static const struct iio_info kmx61_info = {
+     .driver_module          = THIS_MODULE,
+     .read_raw               = kmx61_read_raw,
+     .write_raw              = kmx61_write_raw,
+     .attrs                  = &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
+                    const struct i2c_device_id *id)
+{
+     struct kmx61_data *data;
+     struct iio_dev *indio_dev;
+     int ret;
+     const char *name = NULL;
+
+     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+     if (!indio_dev)
+             return -ENOMEM;
+
+     data = iio_priv(indio_dev);
+     i2c_set_clientdata(client, indio_dev);
+     data->client = client;
+
+     if (id)
+             name = id->name;
+
+     indio_dev->dev.parent = &client->dev;
+     indio_dev->channels = kmx61_channels;
+     indio_dev->num_channels = ARRAY_SIZE(kmx61_channels);
+     indio_dev->name = name;
+     indio_dev->modes = INDIO_DIRECT_MODE;
+     indio_dev->info = &kmx61_info;
+
+     mutex_init(&data->lock);
+
+     ret = kmx61_chip_init(data);
+     if (ret < 0)
+             return ret;
+
+     ret = iio_device_register(indio_dev);
+     if (ret < 0) {
+             dev_err(&client->dev, "Failed to register iio device\n");
+             goto err_iio_device_register;
+     }
+
+     ret = pm_runtime_set_active(&client->dev);
+     if (ret < 0)
+             goto err_pm_runtime_set_active;
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
+     pm_runtime_use_autosuspend(&client->dev);
+
+     return 0;
+
+err_pm_runtime_set_active:
+     iio_device_unregister(indio_dev);
+err_iio_device_register:
+     kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(client);
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     iio_device_unregister(indio_dev);
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+                          false);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     u8 stby = 0;
+
+     if (data->acc_stby)
+             stby |= KMX61_ACC_STBY_BIT;
+     if (data->mag_stby)
+             stby |= KMX61_MAG_STBY_BIT;
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+     struct kmx61_data *data = iio_priv(indio_dev);
+     u8 stby = 0;
+
+     if (!data->acc_ps)
+             stby |= KMX61_ACC_STBY_BIT;
+     if (!data->mag_ps)
+             stby |= KMX61_MAG_STBY_BIT;
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops = {
+     SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+     SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
+};
+
+static const struct i2c_device_id kmx61_id[] = {
+     {"kmx611021", 0},
+     {}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver = {
+     .driver = {
+             .name = KMX61_DRV_NAME,
+             .pm = &kmx61_pm_ops,
+     },
+     .probe          = kmx61_probe,
+     .remove         = kmx61_remove,
+     .id_table       = kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
Quoted 8 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c line 766

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");

Hartmut, thanks a lot for your reviews. I will send a patch series with these cleanups together with changes for ACPI, buffer and trigger support.

Don't know if Jonathan would like to see it smashed together, already. Take care

Hartmut

Quoted 7 lines

Daniel.
--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 4 lines · Hartmut Knaack

Daniel Baluta schrieb am 25.11.2014 10:54:

Quoted 3 lines · Hartmut Knaack
Quoted 2 lines

Daniel Baluta schrieb am 18.11.2014 17:47:

Quoted 1 line

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. It=

exports

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

Finally managed to have a close look, and found a few issues.

Quoted 4 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and P=

eter

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

* http://marc.info/?l=3Dlinux-iio&m=3D141564019602400&w=

=3D2

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +

drivers/iio/imu/kmx61.c | 766 ++++++++++++++++++++++++++++++++++++++=

+++++++++

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 25

Quoted 9 lines
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +0 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
        Say yes here to build support for Analog Devices ADIS16375, AD=

IS16480,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/imu/Kconfig config ADIS16480

Quoted 7 lines
drivers/iio/imu/Kconfig +5 -0
        ADIS16485, ADIS16488 inertial sensors.
 
+config KMX61
+     tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+     depends on I2C
+     help
+       Say Y here if you want to build a driver for Kionix KMX61 6-ax=

is accelerometer

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 34

Quoted 2 lines
drivers/iio/imu/Kconfig +2 -0
+       and magnetometer.
+       To compile this driver as module, choose M here: the module wi=

ll be called

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 36

Quoted 2 lines
drivers/iio/imu/Kconfig +2 -0
+       kmx61.
+

This help text is more than 80 characters wide and may better be wrappe=

d a bit differently.

Quoted 11 lines
Quoted 11 lines

Good catch. checkpatch.pl didn't complain.

Quoted 8 lines

drivers/iio/imu/Makefile adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) +=3D adis_buf=

Quoted 8 lines
drivers/iio/imu/Kconfig +0 -0
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +0 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) +=3D adis_buf=

fer.o

Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
drivers/iio/imu/Makefile +2 -0
 obj-$(CONFIG_IIO_ADIS_LIB) +=3D adis_lib.o
 
 obj-y +=3D inv_mpu6050/
+
+obj-$(CONFIG_KMX61) +=3D kmx61.o
drivers/iio/imu/kmx61.c new file +56 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I   0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L     0x0A
+#define KMX61_ACC_XOUT_H     0x0B
+#define KMX61_ACC_YOUT_L     0x0C
+#define KMX61_ACC_YOUT_H     0x0D
+#define KMX61_ACC_ZOUT_L     0x0E
+#define KMX61_ACC_ZOUT_H     0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L         0x10
+#define KMX61_TEMP_H         0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L     0x12
+#define KMX61_MAG_XOUT_H     0x13
+#define KMX61_MAG_YOUT_L     0x14
+#define KMX61_MAG_YOUT_H     0x15
+#define KMX61_MAG_ZOUT_L     0x16
+#define KMX61_MAG_ZOUT_H     0x17
+
+#define KMX61_REG_ODCNTL     0x2C
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+#define KMX61_REG_STBY               0x29
+#define KMX61_REG_CTRL1              0x2A
+
+#define KMX61_ACC_STBY_BIT   BIT(0)
+#define KMX61_MAG_STBY_BIT   BIT(1)
+#define KMX61_ACT_STBY_BIT   BIT(7)
+
+#define KMX61_ALL_STBY               (KMX61_ACC_STBY_BIT | KMX61_MAG_=

STBY_BIT)

Quoted 56 lines
Quoted 56 lines
Quoted 22 lines

drivers/iio/imu/kmx61.c line 69

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT  0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT  1
+#define KMX61_REG_CTRL1_GSEL0_MASK   0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK   0x02

I don't see the benefit of separating GSEL0 and GSEL1.

Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+
+#define KMX61_REG_CTRL1_BIT_RES              BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT  0
+#define KMX61_MAG_ODR_SHIFT  4
+#define KMX61_ACC_ODR_MASK   0x0F
+#define KMX61_MAG_ODR_MASK   0xF0
+
+#define KMX61_SLEEP_DELAY_MS 2000
+
+#define KMX61_CHIP_ID                0x12
+
+struct kmx61_data {
+     struct i2c_client *client;
+
+     /* serialize access to non-atomic ops, e.g set_mode */
Quoted 8 lines

drivers/iio/imu/kmx61.c line 92

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct mutex lock;
+     u8 range;
+     u8 odr_bits;
+
+     /* standby state */
+     u8 acc_stby;
+     u8 mag_stby;

Why u8 instead of bool?

Hmm, correct. I was thinking bitwise, but you are right. Will fix.

Quoted 23 lines
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+
+     /* power state */
+     bool acc_ps;
+     bool mag_ps;
+};
+
+enum kmx61_range {
+     KMX61_RANGE_2G,
+     KMX61_RANGE_4G,
+     KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+     KMX61_SCAN_ACC_X,
+     KMX61_SCAN_ACC_Y,
+     KMX61_SCAN_ACC_Z,
+     KMX61_SCAN_TEMP,
+     KMX61_SCAN_MAG_X,
+     KMX61_SCAN_MAG_Y,
+     KMX61_SCAN_MAG_Z,
+};
+
+static const struct {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c line 122

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     u16 uscale;
+     u8 gsel0;
+     u8 gsel1;
+} kmx61_scale_table[] =3D {
+     {9582, 0, 0},
+     {19163, 1, 0},
+     {38326, 0, 1},

Why not consolidate gsel0 and gsel1 to gsel? Then this could become u16=

kmx61_scale[] =3D {9582, 19163, 38326} with gsel as index.

Quoted 19 lines
Quoted 19 lines

True. Will fix.

Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+};
+
+/* KMX61 devices */
+#define KMX61_ACC    0x01
+#define KMX61_MAG    0x02
+
+static const struct {
+     int val;
+     int val2;
+     u8 odr_bits;
+} kmx61_samp_freq_table[] =3D { {12, 500000, 0x00},
+                     {25, 0, 0x01},
+                     {50, 0, 0x02},
+                     {100, 0, 0x03},
+                     {200, 0, 0x04},
+                     {400, 0, 0x05},
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     {800, 0, 0x06},
+                     {1600, 0, 0x07},
+                     {0, 781000, 0x08},
+                     {1, 563000, 0x09},
+                     {3, 125000, 0x0A},
+                     {6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038=

326");

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+     "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400=

800");

Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
drivers/iio/imu/kmx61.c new file +61 -0
+
+static struct attribute *kmx61_attributes[] =3D {
+     &iio_const_attr_accel_scale_available.dev_attr.attr,
+     &iio_const_attr_magn_scale_available.dev_attr.attr,
+     &iio_const_attr_sampling_frequency_available.dev_attr.attr,
+     NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group =3D {
+     .attrs =3D kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+     .type =3D IIO_ACCEL, \
+     .modified =3D 1, \
+     .channel2 =3D IIO_MOD_ ## _axis, \
+     .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .address =3D KMX61_ACC, \
+     .scan_index =3D _index, \
+     .scan_type =3D { \
+             .sign =3D 's', \
+             .realbits =3D 12, \
+             .storagebits =3D 16, \
+             .shift =3D 4, \
+             .endianness =3D IIO_LE, \
+     }, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+     .type =3D IIO_MAGN, \
+     .modified =3D 1, \
+     .channel2 =3D IIO_MOD_ ## _axis, \
+     .address =3D KMX61_MAG, \
+     .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .scan_index =3D _index, \
+     .scan_type =3D { \
+             .sign =3D 's', \
+             .realbits =3D 14, \
+             .storagebits =3D 16, \
+             .shift =3D 2, \
+             .endianness =3D IIO_LE, \
+     }, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] =3D {
+     KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+     KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+     KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+     KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+     KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+     KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)
+{
+     int i;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c line 217

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (val =3D=3D kmx61_samp_freq_table[i].val &&
+                 val2 =3D=3D kmx61_samp_freq_table[i].val2)
+                     return kmx61_samp_freq_table[i].odr_bits;
+     return -EINVAL;
+}
+/**

Like here, the first line of the following multiline comments have one =

asterisk too much.

Quoted 8 lines
Quoted 8 lines

I think this is the convention, when adding doxygen style documentation:
http://lxr.free-electrons.com/source/fs/eventpoll.c#L382

Quoted 4 lines
Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be s=

et

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operati=

ng modes

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 225

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled inde=

pendently

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 226

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * if they are both enabled. Internal sensors state is saved in acc_s=

tby and

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device=

,

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+                       bool update)
+{
+     int ret;
+     int acc_stby =3D -1, mag_stby =3D -1;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     if (device & KMX61_ACC) {
+             if (mode & KMX61_ACC_STBY_BIT) {
+                     ret |=3D KMX61_ACC_STBY_BIT;
+                     acc_stby =3D 1;
+             } else {
+                     ret &=3D ~KMX61_ACC_STBY_BIT;
+                     acc_stby =3D 0;
+             }
+     }
+
+     if (device & KMX61_MAG) {
+             if (mode & KMX61_MAG_STBY_BIT) {
+                     ret |=3D KMX61_MAG_STBY_BIT;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     mag_stby =3D 1;
+             } else {
+                     ret &=3D ~KMX61_MAG_STBY_BIT;
+                     mag_stby =3D 0;
+             }
+     }
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, =

ret);

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_stby\n");
+             return ret;
+     }
+
+     if (acc_stby !=3D -1 && update)
+             data->acc_stby =3D !!acc_stby;
+     if (mag_stby !=3D -1 && update)
+             data->mag_stby =3D !!mag_stby;

Why is there a need for double negation?

Quoted 7 lines
Quoted 6 lines

Correct. Now it doesn't make sense since data->acc_stby is u8.

But, with your suggestion if I will use bool to represent data->acc_stby then I think the code should use double negation to make it clear that data->acc_stby is bool and acc_stby is int.

Well, if it makes you happy, then go ahead. My interpretation of a bool i=

s, that an assignment of 0 becomes 0/false, and an assignment of anything e= lse becomes 1/true. So, I wouldn't see a need here (especially since acc_st= by/mag_stby were distinctly assigned either 0 or 1).

All right. It makes sense.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c line 271

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     return ret;

Since no error occured until this point, just return 0.

Ok. Will fix.

Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 devic=

e)

Quoted 29 lines
Quoted 29 lines
Quoted 16 lines
Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             dev_err(&data->client->dev, "Error reading reg_stby\n");
+             return ret;
+     }
+     *mode =3D 0;
+
+     if (device & KMX61_ACC) {
+             if (ret & KMX61_ACC_STBY_BIT)
+                     *mode |=3D KMX61_ACC_STBY_BIT;
+             else
+                     *mode &=3D ~KMX61_ACC_STBY_BIT;

Simply:

Quoted 1 line
                *mode |=3D ret & KMX61_ACC_STBY_BIT;

Ok.

Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     if (device & KMX61_MAG) {
+             if (ret & KMX61_MAG_STBY_BIT)
+                     *mode |=3D KMX61_MAG_STBY_BIT;
+             else
+                     *mode &=3D ~KMX61_MAG_STBY_BIT;

Same here:

Ok.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
                *mode |=3D ret & KMX61_MAG_STBY_BIT;

drivers/iio/imu/kmx61.c

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+     }
+
+     return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, =

u8 device)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     int ret;
+     u8 mode;
+     int lodr_bits, odr_bits;
+
+     ret =3D kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+     if (ret < 0)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             return ret;
+
+     lodr_bits =3D kmx61_convert_freq_to_bit(val, val2);
+     if (lodr_bits < 0)
+             return lodr_bits;
+
+     /* To change ODR, accel and magn must be in STDBY */
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_M=

AG,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                          true);
+     if (ret < 0)
+             return ret;
+
+     odr_bits =3D 0;
+     if (device & KMX61_ACC)
+             odr_bits |=3D lodr_bits;
+     if (device & KMX61_MAG)
+             odr_bits |=3D (lodr_bits << KMX61_MAG_ODR_SHIFT);
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

No need for parenthesis.

ok.

Quoted 2 lines
Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL=

,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+                                     odr_bits);
+     if (ret < 0)
+             return ret;
+
+     ret =3D kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
+     if (ret < 0)
+             return ret;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+     data->odr_bits =3D lodr_bits;
+
+     return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 de=

vice)

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

Usually wrap parameters? And don't put code on line with {.

Will fix.

Quoted 7 lines

drivers/iio/imu/kmx61.c line 349

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{    int i;
+     u8 lodr_bits;
+
+     if (device & KMX61_ACC)
+             lodr_bits =3D (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+                          KMX61_ACC_ODR_MASK;

KMX61_ACC_ODR_SHIFT is just a placebo and not used in kmx61_set_odr. Ch=

oose one way and stay consistent.

Quoted 11 lines
Quoted 11 lines

placebo is good. will fix kmx61_set_odr :)

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     else if (device & KMX61_MAG)
+             lodr_bits =3D (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+                          KMX61_MAG_ODR_MASK;
+     else
+             return -EINVAL;
+
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (lodr_bits =3D=3D kmx61_samp_freq_table[i].odr_bits) =

{

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c line 360

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+                     *val =3D kmx61_samp_freq_table[i].val;
+                     *val2 =3D kmx61_samp_freq_table[i].val2;
+                     return 0;

return IIO_VAL_INT_PLUS_MICRO allows you to faciliate _read_raw a bit t=

o just return ret there.

Quoted 6 lines
Quoted 5 lines

Hmm, If you don't mind I want to keep this as it is, just to be consistent with the rest of the functions. The convention used is to return 0 in case of success.

Up to you. Though kmx61_read_raw() would be an example of a function that=

doesn't follow this convention ;-) And there is no real benefit of checkin=
g an error code there, which is passed up anyway.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/iio/imu/kmx61.c

Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+             }
+     return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_ctrl1\n")=

;

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+             return ret;
+     }
+
+     ret &=3D ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_MA=

SK);

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 376

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+     ret |=3D kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL0=

_SHIFT;

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 377

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+     ret |=3D kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL1=

_SHIFT;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 379

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1,=

ret);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 381

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_ctrl1\n")=

;

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/imu/kmx61.c new file +14 -0
+             return ret;
+     }
+
+     data->range =3D range;
+
+     return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+     int ret, i;
+     u8  mode;
+
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             if (kmx61_scale_table[i].uscale =3D=3D uscale) {
+                     ret =3D kmx61_get_mode(data, &mode,
+                                          KMX61_ACC | KMX61_MAG);
+                     if (ret < 0)
+                             return ret;
+
+                     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY,
+                                          KMX61_ACC | KMX61_MAG, true=

);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                     if (ret < 0)
+                             return ret;
+
+                     ret =3D kmx61_set_range(data, i);
+                     if (ret < 0)
+                             return ret;
+
+                     return  kmx61_set_mode(data, mode,
+                                            KMX61_ACC | KMX61_MAG, tr=

ue);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             }
+     }
+     return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_=

I);

Quoted 23 lines
Quoted 23 lines
Quoted 12 lines
Quoted 12 lines
drivers/iio/imu/kmx61.c new file +12 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading who_am_i\n");
+             return ret;
+     }
+
+     if (ret !=3D KMX61_CHIP_ID) {
+             dev_err(&data->client->dev,
+                     "Wrong chip id, got %x expected %x\n",
+                      ret, KMX61_CHIP_ID);
+             return -EINVAL;
+     }
+
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     /* set accel 12bit, 4g range */
+     ret =3D kmx61_set_range(data, KMX61_RANGE_4G);
+     if (ret < 0)
+             return ret;
+
+     /* set acc/magn to OPERATION mode */
+     ret =3D kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Just return kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Ok.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to b=

e set

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 453

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is =

in

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 454

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * OPERATION then we know that kmx61_runtime_resume was already calle=

d

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 455

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * so we must set ACC OPERATION mode here. The same happens when MAG =

power

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8=

device)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+{
+#ifdef CONFIG_PM_RUNTIME
+     int ret;
+
+     if (device & KMX61_ACC) {
+             if (on && !data->acc_ps && !data->mag_stby)
+                     kmx61_set_mode(data, 0, KMX61_ACC, true);
+             data->acc_ps =3D on;
+     }
+     if (device & KMX61_MAG) {
+             if (on && !data->mag_ps && !data->acc_stby)
+                     kmx61_set_mode(data, 0, KMX61_MAG, true);
+             data->mag_ps =3D on;
+     }
+
+     if (on) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             ret =3D pm_runtime_get_sync(&data->client->dev);
+     } else {
+             pm_runtime_mark_last_busy(&data->client->dev);
+             ret =3D pm_runtime_put_autosuspend(&data->client->dev);
+     }
+     if (ret < 0) {
+             dev_err(&data->client->dev,
+                     "Failed: kmx61_set_power_state for %d, ret %d\n"=

,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     on, ret);
+             return ret;
+     }
+#endif
+     return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base, =

int offset)

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

u8 base, u8 offset?

Correct.

Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     int ret;
+     u8 reg =3D base + offset * 2;
+
+     ret =3D i2c_smbus_read_word_data(data->client, reg);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "failed to read reg at %x\n"=

, reg);

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 498

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+             return ret;

This return is unnecessary.

Ok.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+                           struct iio_chan_spec const *chan, int *val=

,

Quoted 16 lines
Quoted 16 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 506

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+                           int *val2, long mask)

Indentation of the parameters is a bit too high.

Ok, will fix.

Quoted 11 lines
Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+{
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+     u8 base_reg;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW:
+             switch (chan->type) {
+             case IIO_ACCEL:
+             case IIO_MAGN:
+                     base_reg =3D KMX61_ACC_XOUT_L;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     break;
+             default:
+                     return -EINVAL;
+             }
+             mutex_lock(&data->lock);
+
+             kmx61_set_power_state(data, true, chan->address);
+             ret =3D kmx61_read_measurement(data, base_reg, chan->sca=

n_index);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 527

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+             if (ret < 0) {
+                     kmx61_set_power_state(data, false, chan->address=

);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                     mutex_unlock(&data->lock);
+                     return ret;
+             }
+             *val =3D sign_extend32(ret >> chan->scan_type.shift,
+                                  chan->scan_type.realbits - 1);
+             kmx61_set_power_state(data, false, chan->address);
+
+             mutex_unlock(&data->lock);
+             return IIO_VAL_INT;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:
+                     *val =3D 0;
+                     *val2 =3D kmx61_scale_table[data->range].uscale;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             case IIO_MAGN:
+                     /* 14 bits res, 1465 microGauss per magn count *=

/

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     *val =3D 0;
+                     *val2 =3D 1465;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             default:
+                     return -EINVAL;
+             }
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type !=3D IIO_ACCEL && chan->type !=3D IIO_MAG=

N)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/imu/kmx61.c line 557

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret =3D kmx61_get_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);

Could be just return ret here, if kmx61_get_odr() returns IIO_VAL_INT_P=

LUS_MICRO on success and -EINVAL on error.

Quoted 2 lines
Quoted 2 lines

As explained above, I prefer not to do this. More than that, I can't do =

ret here

Quoted 3 lines
Quoted 2 lines

because the data->lock is held.

Well, I actually intended to have an unconditional "return ret;" right af=

ter the mutex_unlock. But up to you. I also see a good point to make it obv= ious here, that the return value is IIO_VAL_INT_PLUS_MICRO. So, up to you.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             if (ret)
+                     return -EINVAL;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+     return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan, int val=

,

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 567

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+                            int val2, long mask)

Indentation of parameters is too high.

Ok.

Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type !=3D IIO_ACCEL && chan->type !=3D IIO_MAG=

N)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret =3D kmx61_set_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);
+             return ret;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:

drivers/iio/imu/kmx61.c line 593

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+                     if (val !=3D 0)
+                             return -EINVAL;
+                     mutex_lock(&data->lock);
+                     ret =3D kmx61_set_scale(data, val2);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             default:
+                     return -EINVAL;
+             }
+             return ret;

This won't be reached (if so, ret would not be initialized).

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c line 529

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+     default:
+             return -EINVAL;
+     }
+     return ret;

Same here.

I know. Would initializing ret with 0 will be enough?

Since the switch already catches all cases and returns, there is no need =

for return ret, here. But if you prefer to have a return at the end of the = function, you could drop the default case, or leave it empty, or just put a=
break in there - and return -EINVAL at the end.

Ok. I will remove the the unnecessary return.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/imu/kmx61.c new file +10 -0
+}
+
+static const struct iio_info kmx61_info =3D {
+     .driver_module          =3D THIS_MODULE,
+     .read_raw               =3D kmx61_read_raw,
+     .write_raw              =3D kmx61_write_raw,
+     .attrs                  =3D &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                    const struct i2c_device_id *id)
+{
+     struct kmx61_data *data;
+     struct iio_dev *indio_dev;
+     int ret;
+     const char *name =3D NULL;
+
+     indio_dev =3D devm_iio_device_alloc(&client->dev, sizeof(*data))=

;

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/imu/kmx61.c new file +18 -0
+     if (!indio_dev)
+             return -ENOMEM;
+
+     data =3D iio_priv(indio_dev);
+     i2c_set_clientdata(client, indio_dev);
+     data->client =3D client;
+
+     if (id)
+             name =3D id->name;
+
+     indio_dev->dev.parent =3D &client->dev;
+     indio_dev->channels =3D kmx61_channels;
+     indio_dev->num_channels =3D ARRAY_SIZE(kmx61_channels);
+     indio_dev->name =3D name;
+     indio_dev->modes =3D INDIO_DIRECT_MODE;
+     indio_dev->info =3D &kmx61_info;
+
+     mutex_init(&data->lock);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+     ret =3D kmx61_chip_init(data);
+     if (ret < 0)
+             return ret;
+
+     ret =3D iio_device_register(indio_dev);
+     if (ret < 0) {
+             dev_err(&client->dev, "Failed to register iio device\n")=

;

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             goto err_iio_device_register;
+     }
+
+     ret =3D pm_runtime_set_active(&client->dev);
+     if (ret < 0)
+             goto err_pm_runtime_set_active;
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY=

_MS);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     pm_runtime_use_autosuspend(&client->dev);
+
+     return 0;
+
+err_pm_runtime_set_active:
+     iio_device_unregister(indio_dev);
+err_iio_device_register:
+     kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true=

);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+     return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(client);
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     iio_device_unregister(indio_dev);
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_M=

AG, true);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(d=

ev));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_M=

AG,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                          false);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(d=

ev));

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     u8 stby =3D 0;
+
+     if (data->acc_stby)
+             stby |=3D KMX61_ACC_STBY_BIT;
+     if (data->mag_stby)
+             stby |=3D KMX61_MAG_STBY_BIT;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(d=

ev));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_M=

AG, true);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(d=

ev));

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     u8 stby =3D 0;
+
+     if (!data->acc_ps)
+             stby |=3D KMX61_ACC_STBY_BIT;
+     if (!data->mag_ps)
+             stby |=3D KMX61_MAG_STBY_BIT;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops =3D {
+     SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+     SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, =

NULL)

Quoted 28 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/iio/imu/kmx61.c new file +20 -0
+};
+
+static const struct i2c_device_id kmx61_id[] =3D {
+     {"kmx611021", 0},
+     {}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver =3D {
+     .driver =3D {
+             .name =3D KMX61_DRV_NAME,
+             .pm =3D &kmx61_pm_ops,
+     },
+     .probe          =3D kmx61_probe,
+     .remove         =3D kmx61_remove,
+     .id_table       =3D kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
Quoted 8 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c line 766

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");

Hartmut, thanks a lot for your reviews. I will send a patch series with these cleanups together with changes for ACPI, buffer and trigger support.

Quoted 2 lines

Don't know if Jonathan would like to see it smashed together, already. Take care

By together I mean separate patches in the same patch set.

I will take
Daniel.

#6 lore raw

On Wed, Nov 26, 2014 at 12:44 AM, Daniel Baluta <[email protected]> w= rote:

Quoted 3 lines · Hartmut Knaack
Quoted 2 lines

Daniel Baluta schrieb am 25.11.2014 10:54:

Quoted 1 line

On Tue, Nov 25, 2014 at 2:45 AM, Hartmut Knaack <[email protected]> wrote=

:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Daniel Baluta schrieb am 18.11.2014 17:47:

Quoted 1 line

Minimal implementation for KMX61 6-axis accelerometer/magnetometer. I=

t exports

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

raw accel/magn readings together with scale and sampling frequency.

Datasheet will be available at:
http://www.kionix.com/6-axis-accelerometer-magnetometer/kmx61

Finally managed to have a close look, and found a few issues.

Quoted 4 lines

---
Changes since v2:
* mostly style fixes after comments received from Jonathan and =

Peter

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

* http://marc.info/?l=3Dlinux-iio&m=3D141564019602400&w=

=3D2

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

* implemented PM runtime and PM sleep resume/suspend hooks
* introduced new parameter for kmx61_set_mode to help updating stby bits in kmx61_data.
* use kmx6111021 instead of kmx61 for i2c_device_id.

 drivers/iio/imu/Kconfig  |   9 +
 drivers/iio/imu/Makefile |   2 +

drivers/iio/imu/kmx61.c | 766 +++++++++++++++++++++++++++++++++++++=

++++++++++

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 25

Quoted 9 lines
 3 files changed, 777 insertions(+)
 create mode 100644 drivers/iio/imu/kmx61.c
drivers/iio/imu/Kconfig +0 -0
index 2b0e451..d675f43 100644
@@ -25,6 +25,15 @@ config ADIS16480
        Say yes here to build support for Analog Devices ADIS16375, A=

DIS16480,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/imu/Kconfig config ADIS16480

Quoted 7 lines
drivers/iio/imu/Kconfig +5 -0
        ADIS16485, ADIS16488 inertial sensors.
 
+config KMX61
+     tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
+     depends on I2C
+     help
+       Say Y here if you want to build a driver for Kionix KMX61 6-a=

xis accelerometer

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 34

Quoted 2 lines
drivers/iio/imu/Kconfig +2 -0
+       and magnetometer.
+       To compile this driver as module, choose M here: the module w=

ill be called

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/Kconfig config ADIS16480 · line 36

Quoted 2 lines
drivers/iio/imu/Kconfig +2 -0
+       kmx61.
+

This help text is more than 80 characters wide and may better be wrapp=

ed a bit differently.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Good catch. checkpatch.pl didn't complain.

Quoted 8 lines

drivers/iio/imu/Makefile adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) +=3D adis_bu=

Quoted 8 lines
drivers/iio/imu/Kconfig +0 -0
 source "drivers/iio/imu/inv_mpu6050/Kconfig"
 
 endmenu
drivers/iio/imu/Makefile +0 -0
index 114d2c1..e1e6e3d 100644
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) +=3D adis_bu=

ffer.o

Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
Quoted 67 lines
drivers/iio/imu/Makefile +2 -0
 obj-$(CONFIG_IIO_ADIS_LIB) +=3D adis_lib.o
 
 obj-y +=3D inv_mpu6050/
+
+obj-$(CONFIG_KMX61) +=3D kmx61.o
drivers/iio/imu/kmx61.c new file +56 -0
new file mode 100644
index 0000000..f68b3ef
@@ -0,0 +1,766 @@
+/*
+ * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
+ *
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
+ *
+ * TODO: buffer, interrupt, thresholds, acpi, temperature sensor
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define KMX61_DRV_NAME "kmx61"
+
+#define KMX61_REG_WHO_AM_I   0x00
+
+/*
+ * three 16-bit accelerometer output registers for X/Y/Z axis
+ * we use only XOUT_L as a base register, all other addresses
+ * can be obtained by applying an offset and are provided here
+ * only for clarity.
+ */
+#define KMX61_ACC_XOUT_L     0x0A
+#define KMX61_ACC_XOUT_H     0x0B
+#define KMX61_ACC_YOUT_L     0x0C
+#define KMX61_ACC_YOUT_H     0x0D
+#define KMX61_ACC_ZOUT_L     0x0E
+#define KMX61_ACC_ZOUT_H     0x0F
+
+/*
+ * one 16-bit temperature output register
+ */
+#define KMX61_TEMP_L         0x10
+#define KMX61_TEMP_H         0x11
+
+/*
+ * three 16-bit magnetometer output registers for X/Y/Z axis
+ */
+#define KMX61_MAG_XOUT_L     0x12
+#define KMX61_MAG_XOUT_H     0x13
+#define KMX61_MAG_YOUT_L     0x14
+#define KMX61_MAG_YOUT_H     0x15
+#define KMX61_MAG_ZOUT_L     0x16
+#define KMX61_MAG_ZOUT_H     0x17
+
+#define KMX61_REG_ODCNTL     0x2C
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+#define KMX61_REG_STBY               0x29
+#define KMX61_REG_CTRL1              0x2A
+
+#define KMX61_ACC_STBY_BIT   BIT(0)
+#define KMX61_MAG_STBY_BIT   BIT(1)
+#define KMX61_ACT_STBY_BIT   BIT(7)
+
+#define KMX61_ALL_STBY               (KMX61_ACC_STBY_BIT | KMX61_MAG=

_STBY_BIT)

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 22 lines

drivers/iio/imu/kmx61.c line 69

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+
+#define KMX61_REG_CTRL1_GSEL0_SHIFT  0
+#define KMX61_REG_CTRL1_GSEL1_SHIFT  1
+#define KMX61_REG_CTRL1_GSEL0_MASK   0x01
+#define KMX61_REG_CTRL1_GSEL1_MASK   0x02

I don't see the benefit of separating GSEL0 and GSEL1.

Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+
+#define KMX61_REG_CTRL1_BIT_RES              BIT(4)
+
+#define KMX61_ACC_ODR_SHIFT  0
+#define KMX61_MAG_ODR_SHIFT  4
+#define KMX61_ACC_ODR_MASK   0x0F
+#define KMX61_MAG_ODR_MASK   0xF0
+
+#define KMX61_SLEEP_DELAY_MS 2000
+
+#define KMX61_CHIP_ID                0x12
+
+struct kmx61_data {
+     struct i2c_client *client;
+
+     /* serialize access to non-atomic ops, e.g set_mode */
Quoted 8 lines

drivers/iio/imu/kmx61.c line 92

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct mutex lock;
+     u8 range;
+     u8 odr_bits;
+
+     /* standby state */
+     u8 acc_stby;
+     u8 mag_stby;

Why u8 instead of bool?

Hmm, correct. I was thinking bitwise, but you are right. Will fix.

Quoted 23 lines
Quoted 23 lines
drivers/iio/imu/kmx61.c new file +23 -0
+
+     /* power state */
+     bool acc_ps;
+     bool mag_ps;
+};
+
+enum kmx61_range {
+     KMX61_RANGE_2G,
+     KMX61_RANGE_4G,
+     KMX61_RANGE_8G,
+};
+
+enum kmx61_scan {
+     KMX61_SCAN_ACC_X,
+     KMX61_SCAN_ACC_Y,
+     KMX61_SCAN_ACC_Z,
+     KMX61_SCAN_TEMP,
+     KMX61_SCAN_MAG_X,
+     KMX61_SCAN_MAG_Y,
+     KMX61_SCAN_MAG_Z,
+};
+
+static const struct {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c line 122

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     u16 uscale;
+     u8 gsel0;
+     u8 gsel1;
+} kmx61_scale_table[] =3D {
+     {9582, 0, 0},
+     {19163, 1, 0},
+     {38326, 0, 1},

Why not consolidate gsel0 and gsel1 to gsel? Then this could become u1=

6 kmx61_scale[] =3D {9582, 19163, 38326} with gsel as index.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

True. Will fix.

Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+};
+
+/* KMX61 devices */
+#define KMX61_ACC    0x01
+#define KMX61_MAG    0x02
+
+static const struct {
+     int val;
+     int val2;
+     u8 odr_bits;
+} kmx61_samp_freq_table[] =3D { {12, 500000, 0x00},
+                     {25, 0, 0x01},
+                     {50, 0, 0x02},
+                     {100, 0, 0x03},
+                     {200, 0, 0x04},
+                     {400, 0, 0x05},
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     {800, 0, 0x06},
+                     {1600, 0, 0x07},
+                     {0, 781000, 0x08},
+                     {1, 563000, 0x09},
+                     {3, 125000, 0x0A},
+                     {6, 250000, 0x0B} };
+
+static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.03=

8326");

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+static IIO_CONST_ATTR(magn_scale_available, "0.001465");
+static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
+     "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 40=

0 800");

Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
drivers/iio/imu/kmx61.c new file +61 -0
+
+static struct attribute *kmx61_attributes[] =3D {
+     &iio_const_attr_accel_scale_available.dev_attr.attr,
+     &iio_const_attr_magn_scale_available.dev_attr.attr,
+     &iio_const_attr_sampling_frequency_available.dev_attr.attr,
+     NULL,
+};
+
+static const struct attribute_group kmx61_attribute_group =3D {
+     .attrs =3D kmx61_attributes,
+};
+
+#define KMX61_ACC_CHAN(_axis, _index) { \
+     .type =3D IIO_ACCEL, \
+     .modified =3D 1, \
+     .channel2 =3D IIO_MOD_ ## _axis, \
+     .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .address =3D KMX61_ACC, \
+     .scan_index =3D _index, \
+     .scan_type =3D { \
+             .sign =3D 's', \
+             .realbits =3D 12, \
+             .storagebits =3D 16, \
+             .shift =3D 4, \
+             .endianness =3D IIO_LE, \
+     }, \
+}
+
+#define KMX61_MAG_CHAN(_axis, _index) { \
+     .type =3D IIO_MAGN, \
+     .modified =3D 1, \
+     .channel2 =3D IIO_MOD_ ## _axis, \
+     .address =3D KMX61_MAG, \
+     .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \
+     .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \
+                             BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+     .scan_index =3D _index, \
+     .scan_type =3D { \
+             .sign =3D 's', \
+             .realbits =3D 14, \
+             .storagebits =3D 16, \
+             .shift =3D 2, \
+             .endianness =3D IIO_LE, \
+     }, \
+}
+
+static const struct iio_chan_spec kmx61_channels[] =3D {
+     KMX61_ACC_CHAN(X, KMX61_SCAN_ACC_X),
+     KMX61_ACC_CHAN(Y, KMX61_SCAN_ACC_Y),
+     KMX61_ACC_CHAN(Z, KMX61_SCAN_ACC_Z),
+     KMX61_MAG_CHAN(X, KMX61_SCAN_MAG_X),
+     KMX61_MAG_CHAN(Y, KMX61_SCAN_MAG_Y),
+     KMX61_MAG_CHAN(Z, KMX61_SCAN_MAG_Z),
+};
+
+static int kmx61_convert_freq_to_bit(int val, int val2)
+{
+     int i;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c line 217

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (val =3D=3D kmx61_samp_freq_table[i].val &&
+                 val2 =3D=3D kmx61_samp_freq_table[i].val2)
+                     return kmx61_samp_freq_table[i].odr_bits;
+     return -EINVAL;
+}
+/**

Like here, the first line of the following multiline comments have one=

asterisk too much.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I think this is the convention, when adding doxygen style documentation=

:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

http://lxr.free-electrons.com/source/fs/eventpoll.c#L382

Quoted 4 lines
Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+ * kmx61_set_mode() - set KMX61 device operating mode
+ * @data - kmx61 device private data pointer
+ * @mode - bitmask, indicating operating mode for @device
+ * @device - bitmask, indicating device for which @mode needs to be =

set

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * @update - update stby bits stored in device's private  @data
+ *
+ * For each sensor (accelerometer/magnetometer) there are two operat=

ing modes

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 225

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * STANDBY and OPERATION. Neither accel nor magn can be disabled ind=

ependently

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 226

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * if they are both enabled. Internal sensors state is saved in acc_=

stby and

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * mag_stby members of driver's private @data.
+ */
+static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 devic=

e,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                       bool update)
+{
+     int ret;
+     int acc_stby =3D -1, mag_stby =3D -1;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_stby\n")=

;

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/iio/imu/kmx61.c new file +15 -0
+             return ret;
+     }
+     if (device & KMX61_ACC) {
+             if (mode & KMX61_ACC_STBY_BIT) {
+                     ret |=3D KMX61_ACC_STBY_BIT;
+                     acc_stby =3D 1;
+             } else {
+                     ret &=3D ~KMX61_ACC_STBY_BIT;
+                     acc_stby =3D 0;
+             }
+     }
+
+     if (device & KMX61_MAG) {
+             if (mode & KMX61_MAG_STBY_BIT) {
+                     ret |=3D KMX61_MAG_STBY_BIT;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     mag_stby =3D 1;
+             } else {
+                     ret &=3D ~KMX61_MAG_STBY_BIT;
+                     mag_stby =3D 0;
+             }
+     }
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY,=

ret);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 262

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_stby\n")=

;

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+             return ret;
+     }
+
+     if (acc_stby !=3D -1 && update)
+             data->acc_stby =3D !!acc_stby;
+     if (mag_stby !=3D -1 && update)
+             data->mag_stby =3D !!mag_stby;
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Why is there a need for double negation?

Correct. Now it doesn't make sense since data->acc_stby is u8.

But, with your suggestion if I will use bool to represent data->acc_stb=

y

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

then I think the code should use double negation to make it clear that data->acc_stby is bool and acc_stby is int.

Well, if it makes you happy, then go ahead. My interpretation of a bool =

is, that an assignment of 0 becomes 0/false, and an assignment of anything = else becomes 1/true. So, I wouldn't see a need here (especially since acc_s= tby/mag_stby were distinctly assigned either 0 or 1).

Quoted 5 lines

All right. It makes sense.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     return ret;
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Since no error occured until this point, just return 0.

Ok. Will fix.

Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+}
+
+static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 devi=

ce)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/imu/kmx61.c

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_stby\n")=

;

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             return ret;
+     }
+     *mode =3D 0;
+
+     if (device & KMX61_ACC) {
+             if (ret & KMX61_ACC_STBY_BIT)
+                     *mode |=3D KMX61_ACC_STBY_BIT;
+             else
+                     *mode &=3D ~KMX61_ACC_STBY_BIT;

Simply:

                *mode |=3D ret & KMX61_ACC_STBY_BIT;

Ok.

Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     if (device & KMX61_MAG) {
+             if (ret & KMX61_MAG_STBY_BIT)
+                     *mode |=3D KMX61_MAG_STBY_BIT;
+             else
+                     *mode &=3D ~KMX61_MAG_STBY_BIT;

Same here:

Ok.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
                *mode |=3D ret & KMX61_MAG_STBY_BIT;

drivers/iio/imu/kmx61.c

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+     }
+
+     return 0;
+}
+
+static int kmx61_set_odr(struct kmx61_data *data, int val, int val2,=

u8 device)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     int ret;
+     u8 mode;
+     int lodr_bits, odr_bits;
+
+     ret =3D kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
+     if (ret < 0)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             return ret;
+
+     lodr_bits =3D kmx61_convert_freq_to_bit(val, val2);
+     if (lodr_bits < 0)
+             return lodr_bits;
+
+     /* To change ODR, accel and magn must be in STDBY */
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_=

MAG,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                          true);
+     if (ret < 0)
+             return ret;
+
+     odr_bits =3D 0;
+     if (device & KMX61_ACC)
+             odr_bits |=3D lodr_bits;
+     if (device & KMX61_MAG)
+             odr_bits |=3D (lodr_bits << KMX61_MAG_ODR_SHIFT);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

No need for parenthesis.

ok.

Quoted 2 lines
Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNT=

L,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+                                     odr_bits);
+     if (ret < 0)
+             return ret;
+
+     ret =3D kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true)=

;

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+     if (ret < 0)
+             return ret;
+
+     data->odr_bits =3D lodr_bits;
+
+     return 0;
+}
+
+static
+int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, u8 d=

evice)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

Usually wrap parameters? And don't put code on line with {.

Will fix.

Quoted 7 lines

drivers/iio/imu/kmx61.c line 349

Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+{    int i;
+     u8 lodr_bits;
+
+     if (device & KMX61_ACC)
+             lodr_bits =3D (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
+                          KMX61_ACC_ODR_MASK;

KMX61_ACC_ODR_SHIFT is just a placebo and not used in kmx61_set_odr. C=

hoose one way and stay consistent.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

placebo is good. will fix kmx61_set_odr :)

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     else if (device & KMX61_MAG)
+             lodr_bits =3D (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
+                          KMX61_MAG_ODR_MASK;
+     else
+             return -EINVAL;
+
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
+             if (lodr_bits =3D=3D kmx61_samp_freq_table[i].odr_bits)=

{

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c line 360

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+                     *val =3D kmx61_samp_freq_table[i].val;
+                     *val2 =3D kmx61_samp_freq_table[i].val2;
+                     return 0;

return IIO_VAL_INT_PLUS_MICRO allows you to faciliate _read_raw a bit =

to just return ret there.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Hmm, If you don't mind I want to keep this as it is, just to be consistent with the rest of the functions. The convention used is to return 0 in case of success.

Up to you. Though kmx61_read_raw() would be an example of a function tha=

t doesn't follow this convention ;-) And there is no real benefit of checki= ng an error code there, which is passed up anyway.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             }
+     return -EINVAL;
+}
+
+static int kmx61_set_range(struct kmx61_data *data, int range)
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1)=

;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 371

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading reg_ctrl1\n"=

);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/imu/kmx61.c

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+             return ret;
+     }
+
+     ret &=3D ~(KMX61_REG_CTRL1_GSEL0_MASK | KMX61_REG_CTRL1_GSEL1_M=

ASK);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 376

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+     ret |=3D kmx61_scale_table[range].gsel0 << KMX61_REG_CTRL1_GSEL=

0_SHIFT;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 377

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+     ret |=3D kmx61_scale_table[range].gsel1 << KMX61_REG_CTRL1_GSEL=

1_SHIFT;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 379

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+
+     ret =3D i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1=

, ret);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 381

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error writing reg_ctrl1\n"=

);

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/imu/kmx61.c new file +14 -0
+             return ret;
+     }
+
+     data->range =3D range;
+
+     return 0;
+}
+
+static int kmx61_set_scale(struct kmx61_data *data, int uscale)
+{
+     int ret, i;
+     u8  mode;
+
+     for (i =3D 0; i < ARRAY_SIZE(kmx61_scale_table); i++) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             if (kmx61_scale_table[i].uscale =3D=3D uscale) {
+                     ret =3D kmx61_get_mode(data, &mode,
+                                          KMX61_ACC | KMX61_MAG);
+                     if (ret < 0)
+                             return ret;
+
+                     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY,
+                                          KMX61_ACC | KMX61_MAG, tru=

e);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                     if (ret < 0)
+                             return ret;
+
+                     ret =3D kmx61_set_range(data, i);
+                     if (ret < 0)
+                             return ret;
+
+                     return  kmx61_set_mode(data, mode,
+                                            KMX61_ACC | KMX61_MAG, t=

rue);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             }
+     }
+     return -EINVAL;
+}
+
+static int kmx61_chip_init(struct kmx61_data *data)
+{
+     int ret;
+
+     ret =3D i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM=

_I);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 424

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+     if (ret < 0) {
+             dev_err(&data->client->dev, "Error reading who_am_i\n")=

;

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+             return ret;
+     }
+
+     if (ret !=3D KMX61_CHIP_ID) {
+             dev_err(&data->client->dev,
+                     "Wrong chip id, got %x expected %x\n",
+                      ret, KMX61_CHIP_ID);
+             return -EINVAL;
+     }
+
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     /* set accel 12bit, 4g range */
+     ret =3D kmx61_set_range(data, KMX61_RANGE_4G);
+     if (ret < 0)
+             return ret;
+
+     /* set acc/magn to OPERATION mode */
+     ret =3D kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Just return kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);

Ok.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/imu/kmx61.c

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+     if (ret < 0)
+             return ret;
+
+     return 0;
+}
+/**
+ * kmx61_set_power_state() - set power state for kmx61 @device
+ * @data - kmx61 device private pointer
+ * @on - power state to be set for @device
+ * @device - bitmask indicating device for which @on state needs to =

be set

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 453

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+ *
+ * Notice that when ACC power state needs to be set to ON and MAG is=

in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 454

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * OPERATION then we know that kmx61_runtime_resume was already call=

ed

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c line 455

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+ * so we must set ACC OPERATION mode here. The same happens when MAG=

power

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+ * state needs to be set to ON and ACC is in OPERATION.
+ */
+static int kmx61_set_power_state(struct kmx61_data *data, bool on, u=

8 device)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+{
+#ifdef CONFIG_PM_RUNTIME
+     int ret;
+
+     if (device & KMX61_ACC) {
+             if (on && !data->acc_ps && !data->mag_stby)
+                     kmx61_set_mode(data, 0, KMX61_ACC, true);
+             data->acc_ps =3D on;
+     }
+     if (device & KMX61_MAG) {
+             if (on && !data->mag_ps && !data->acc_stby)
+                     kmx61_set_mode(data, 0, KMX61_MAG, true);
+             data->mag_ps =3D on;
+     }
+
+     if (on) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+             ret =3D pm_runtime_get_sync(&data->client->dev);
+     } else {
+             pm_runtime_mark_last_busy(&data->client->dev);
+             ret =3D pm_runtime_put_autosuspend(&data->client->dev);
+     }
+     if (ret < 0) {
+             dev_err(&data->client->dev,
+                     "Failed: kmx61_set_power_state for %d, ret %d\n=

",

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     on, ret);
+             return ret;
+     }
+#endif
+     return 0;
+}
+
+static int kmx61_read_measurement(struct kmx61_data *data, int base,=

int offset)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

u8 base, u8 offset?

Correct.

Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     int ret;
+     u8 reg =3D base + offset * 2;
+
+     ret =3D i2c_smbus_read_word_data(data->client, reg);
+     if (ret < 0) {
+             dev_err(&data->client->dev, "failed to read reg at %x\n=

", reg);

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 498

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+             return ret;

This return is unnecessary.

Ok.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     }
+
+     return ret;
+}
+
+static int kmx61_read_raw(struct iio_dev *indio_dev,
+                           struct iio_chan_spec const *chan, int *va=

l,

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 506

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+                           int *val2, long mask)

Indentation of the parameters is a bit too high.

Ok, will fix.

Quoted 11 lines
Quoted 11 lines
drivers/iio/imu/kmx61.c new file +11 -0
+{
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+     u8 base_reg;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_RAW:
+             switch (chan->type) {
+             case IIO_ACCEL:
+             case IIO_MAGN:
+                     base_reg =3D KMX61_ACC_XOUT_L;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     break;
+             default:
+                     return -EINVAL;
+             }
+             mutex_lock(&data->lock);
+
+             kmx61_set_power_state(data, true, chan->address);
+             ret =3D kmx61_read_measurement(data, base_reg, chan->sc=

an_index);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 527

Quoted 2 lines
drivers/iio/imu/kmx61.c new file +2 -0
+             if (ret < 0) {
+                     kmx61_set_power_state(data, false, chan->addres=

s);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/iio/imu/kmx61.c new file +6 -0
+                     mutex_unlock(&data->lock);
+                     return ret;
+             }
+             *val =3D sign_extend32(ret >> chan->scan_type.shift,
+                                  chan->scan_type.realbits - 1);
+             kmx61_set_power_state(data, false, chan->address);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+             mutex_unlock(&data->lock);
+             return IIO_VAL_INT;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:
+                     *val =3D 0;
+                     *val2 =3D kmx61_scale_table[data->range].uscale=

;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/imu/kmx61.c

Quoted 3 lines
drivers/iio/imu/kmx61.c new file +3 -0
+                     return IIO_VAL_INT_PLUS_MICRO;
+             case IIO_MAGN:
+                     /* 14 bits res, 1465 microGauss per magn count =

*/

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                     *val =3D 0;
+                     *val2 =3D 1465;
+                     return IIO_VAL_INT_PLUS_MICRO;
+             default:
+                     return -EINVAL;
+             }
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type !=3D IIO_ACCEL && chan->type !=3D IIO_MA=

GN)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/imu/kmx61.c line 557

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret =3D kmx61_get_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);

Could be just return ret here, if kmx61_get_odr() returns IIO_VAL_INT_=

PLUS_MICRO on success and -EINVAL on error.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

As explained above, I prefer not to do this. More than that, I can't do=

ret here

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

because the data->lock is held.

Well, I actually intended to have an unconditional "return ret;" right a=

fter the mutex_unlock. But up to you. I also see a good point to make it ob= vious here, that the return value is IIO_VAL_INT_PLUS_MICRO. So, up to you.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             if (ret)
+                     return -EINVAL;
+             return IIO_VAL_INT_PLUS_MICRO;
+     }
+     return -EINVAL;
+}
+
+static int kmx61_write_raw(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan, int va=

l,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines

drivers/iio/imu/kmx61.c line 567

Quoted 1 line
drivers/iio/imu/kmx61.c new file +1 -0
+                            int val2, long mask)

Indentation of parameters is too high.

Ok.

Quoted 7 lines

drivers/iio/imu/kmx61.c

Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+{
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             if (chan->type !=3D IIO_ACCEL && chan->type !=3D IIO_MA=

GN)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                     return -EINVAL;
+
+             mutex_lock(&data->lock);
+             ret =3D kmx61_set_odr(data, val, val2, chan->address);
+             mutex_unlock(&data->lock);
+             return ret;
+     case IIO_CHAN_INFO_SCALE:
+             switch (chan->type) {
+             case IIO_ACCEL:

drivers/iio/imu/kmx61.c line 593

Quoted 10 lines
drivers/iio/imu/kmx61.c new file +10 -0
+                     if (val !=3D 0)
+                             return -EINVAL;
+                     mutex_lock(&data->lock);
+                     ret =3D kmx61_set_scale(data, val2);
+                     mutex_unlock(&data->lock);
+                     return ret;
+             default:
+                     return -EINVAL;
+             }
+             return ret;

This won't be reached (if so, ret would not be initialized).

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c line 529

Quoted 4 lines
drivers/iio/imu/kmx61.c new file +4 -0
+     default:
+             return -EINVAL;
+     }
+     return ret;

Same here.

I know. Would initializing ret with 0 will be enough?

Since the switch already catches all cases and returns, there is no need=

for return ret, here. But if you prefer to have a return at the end of the= function, you could drop the default case, or leave it empty, or just put =
a break in there - and return -EINVAL at the end.

Quoted 14 lines

Ok. I will remove the the unnecessary return.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/imu/kmx61.c new file +10 -0
+}
+
+static const struct iio_info kmx61_info =3D {
+     .driver_module          =3D THIS_MODULE,
+     .read_raw               =3D kmx61_read_raw,
+     .write_raw              =3D kmx61_write_raw,
+     .attrs                  =3D &kmx61_attribute_group,
+};
+
+static int kmx61_probe(struct i2c_client *client,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+                    const struct i2c_device_id *id)
+{
+     struct kmx61_data *data;
+     struct iio_dev *indio_dev;
+     int ret;
+     const char *name =3D NULL;
+
+     indio_dev =3D devm_iio_device_alloc(&client->dev, sizeof(*data)=

);

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/imu/kmx61.c new file +18 -0
+     if (!indio_dev)
+             return -ENOMEM;
+
+     data =3D iio_priv(indio_dev);
+     i2c_set_clientdata(client, indio_dev);
+     data->client =3D client;
+
+     if (id)
+             name =3D id->name;
+
+     indio_dev->dev.parent =3D &client->dev;
+     indio_dev->channels =3D kmx61_channels;
+     indio_dev->num_channels =3D ARRAY_SIZE(kmx61_channels);
+     indio_dev->name =3D name;
+     indio_dev->modes =3D INDIO_DIRECT_MODE;
+     indio_dev->info =3D &kmx61_info;
+
+     mutex_init(&data->lock);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+     ret =3D kmx61_chip_init(data);
+     if (ret < 0)
+             return ret;
+
+     ret =3D iio_device_register(indio_dev);
+     if (ret < 0) {
+             dev_err(&client->dev, "Failed to register iio device\n"=

);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+             goto err_iio_device_register;
+     }
+
+     ret =3D pm_runtime_set_active(&client->dev);
+     if (ret < 0)
+             goto err_pm_runtime_set_active;
+
+     pm_runtime_enable(&client->dev);
+     pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELA=

Y_MS);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     pm_runtime_use_autosuspend(&client->dev);
+
+     return 0;
+
+err_pm_runtime_set_active:
+     iio_device_unregister(indio_dev);
+err_iio_device_register:
+     kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, tru=

e);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+     return ret;
+}
+
+static int kmx61_remove(struct i2c_client *client)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(client);
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     pm_runtime_disable(&client->dev);
+     pm_runtime_set_suspended(&client->dev);
+     pm_runtime_put_noidle(&client->dev);
+
+     iio_device_unregister(indio_dev);
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_=

MAG, true);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int kmx61_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(=

dev));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_=

MAG,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/iio/imu/kmx61.c

Quoted 9 lines
drivers/iio/imu/kmx61.c new file +9 -0
+                          false);
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(=

dev));

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     u8 stby =3D 0;
+
+     if (data->acc_stby)
+             stby |=3D KMX61_ACC_STBY_BIT;
+     if (data->mag_stby)
+             stby |=3D KMX61_MAG_STBY_BIT;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int kmx61_runtime_suspend(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(=

dev));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/imu/kmx61.c

Quoted 5 lines
drivers/iio/imu/kmx61.c new file +5 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     int ret;
+
+     mutex_lock(&data->lock);
+     ret =3D kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_=

MAG, true);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     mutex_unlock(&data->lock);
+
+     return ret;
+}
+
+static int kmx61_runtime_resume(struct device *dev)
+{
+     struct iio_dev *indio_dev =3D i2c_get_clientdata(to_i2c_client(=

dev));

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/kmx61.c new file +7 -0
+     struct kmx61_data *data =3D iio_priv(indio_dev);
+     u8 stby =3D 0;
+
+     if (!data->acc_ps)
+             stby |=3D KMX61_ACC_STBY_BIT;
+     if (!data->mag_ps)
+             stby |=3D KMX61_MAG_STBY_BIT;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/imu/kmx61.c

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+
+     return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
+}
+#endif
+
+static const struct dev_pm_ops kmx61_pm_ops =3D {
+     SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
+     SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume,=

NULL)

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/kmx61.c new file +16 -0
+};
+
+static const struct i2c_device_id kmx61_id[] =3D {
+     {"kmx611021", 0},
+     {}
+};
+
+MODULE_DEVICE_TABLE(i2c, kmx61_id);
+
+static struct i2c_driver kmx61_driver =3D {
+     .driver =3D {
+             .name =3D KMX61_DRV_NAME,
+             .pm =3D &kmx61_pm_ops,
+     },
+     .probe          =3D kmx61_probe,
+     .remove         =3D kmx61_remove,
Quoted 8 lines

drivers/iio/imu/kmx61.c line 766

Quoted 8 lines
drivers/iio/imu/kmx61.c new file +8 -0
+     .id_table       =3D kmx61_id,
+};
+
+module_i2c_driver(kmx61_driver);
+
+MODULE_AUTHOR("Daniel Baluta <[email protected]>");
+MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
+MODULE_LICENSE("GPL v2");

Hartmut, thanks a lot for your reviews. I will send a patch series with these cleanups together with changes for ACPI, buffer and trigger support.

Quoted 6 lines
Quoted 2 lines

Don't know if Jonathan would like to see it smashed together, already. Take care

By together I mean separate patches in the same patch set.

I will take

Hmm, pressed send to early.

I will take any suggestion on how to send this. Jonathan what do you think?

Daniel.