All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: cros_ec: Add lid angle driver
@ 2019-02-28  1:50 Gwendal Grignou
  2019-02-28 11:18 ` Enric Balletbo i Serra
  2019-03-11  8:24 ` [PATCH] " Lee Jones
  0 siblings, 2 replies; 18+ messages in thread
From: Gwendal Grignou @ 2019-02-28  1:50 UTC (permalink / raw)
  To: lee.jones, groeck, enric.balletbo, bleung, lars, jic23, egranata
  Cc: linux-iio

Add a IIO driver that reports the angle between the lid and the base for
ChromeOS convertible device.

Tested on eve with ToT EC firmware.
Check driver is loaded and lid angle is correct.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../cros_ec_sensors/cros_ec_lid_angle.c       | 164 ++++++++++++++++++
 drivers/mfd/cros_ec_dev.c                     |  15 +-
 4 files changed, 186 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
index 135f6825903f..aacc2ab9c34f 100644
--- a/drivers/iio/common/cros_ec_sensors/Kconfig
+++ b/drivers/iio/common/cros_ec_sensors/Kconfig
@@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
 	  Accelerometers, Gyroscope and Magnetometer that are
 	  presented by the ChromeOS EC Sensor hub.
 	  Creates an IIO device for each functions.
+
+config IIO_CROS_EC_SENSORS_LID_ANGLE
+	tristate "ChromeOS EC Sensor for lid angle"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to report the angle between lid and base for some
+	  convertible devices.
+	  This module is loaded when the EC can calculate the angle between the base
+	  and the lid.
diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
index ec716ff2a775..a35ee232ac07 100644
--- a/drivers/iio/common/cros_ec_sensors/Makefile
+++ b/drivers/iio/common/cros_ec_sensors/Makefile
@@ -4,3 +4,4 @@
 
 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
new file mode 100644
index 000000000000..370b3e9a1405
--- /dev/null
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/*
+ * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
+ *
+ * Copyright 2018 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS
+ * EC about counter sensors. Counters are presented through
+ * iio sysfs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#define DRV_NAME "cros-ec-lid-angle"
+
+/*
+ * One channel for the lid angle, the other for timestamp.
+ */
+#define MAX_CHANNELS (1 + 1)
+
+/* State data for ec_sensors iio driver. */
+struct cros_ec_lid_angle_state {
+	/* Shared by all sensors */
+	struct cros_ec_sensors_core_state core;
+
+	struct iio_chan_spec channels[MAX_CHANNELS];
+};
+
+static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
+					  unsigned long scan_mask, s16 *data)
+{
+	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
+	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
+	if (ret) {
+		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
+		return ret;
+	}
+
+	if (scan_mask & 1)
+		*data = st->resp->lid_angle.value;
+	return 0;
+}
+
+static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan,
+				    int *val, int *val2, long mask)
+{
+	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
+	u16 data;
+	int ret;
+	int idx = chan->scan_index;
+
+	mutex_lock(&st->core.cmd_lock);
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = cros_ec_sensors_read_lid_angle(
+				indio_dev, 1 << idx, &data);
+		if (ret)
+			break;
+		ret = IIO_VAL_INT;
+		*val = data;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&st->core.cmd_lock);
+	return ret;
+}
+
+static const struct iio_info cros_ec_lid_angle_info = {
+	.read_raw = &cros_ec_lid_angle_read,
+	.driver_module = THIS_MODULE,
+};
+
+static int cros_ec_lid_angle_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct cros_ec_lid_angle_state *state;
+	struct iio_chan_spec *channel;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &cros_ec_lid_angle_info;
+	state = iio_priv(indio_dev);
+	channel = state->channels;
+
+	channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+	channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
+	channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
+	channel->scan_type.shift = 0;
+	channel->scan_index = 0;
+	channel->scan_type.sign = 'u';
+	channel->type = IIO_ANGL;
+
+	state->core.calib[0] = 0;
+
+	/* Timestamp */
+	channel++;
+	channel->type = IIO_TIMESTAMP;
+	channel->channel = -1;
+	channel->scan_index = 1;
+	channel->scan_type.sign = 's';
+	channel->scan_type.realbits = 64;
+	channel->scan_type.storagebits = 64;
+
+	indio_dev->channels = state->channels;
+	indio_dev->num_channels = MAX_CHANNELS;
+
+	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+			cros_ec_sensors_capture, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct platform_device_id cros_ec_lid_angle_ids[] = {
+	{
+		.name = DRV_NAME,
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
+
+static struct platform_driver cros_ec_lid_angle_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+		.pm	= &cros_ec_sensors_pm_ops,
+	},
+	.probe		= cros_ec_lid_angle_probe,
+	.id_table	= cros_ec_lid_angle_ids,
+};
+module_platform_driver(cros_ec_lid_angle_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index d275deaecb12..11b5b2fd1f33 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 
 	resp = (struct ec_response_motion_sense *)msg->data;
 	sensor_num = resp->dump.sensor_count;
-	/* Allocate 1 extra sensors in FIFO are needed */
-	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
+	/*
+	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
+	 */
+#define NUM_EXTRA_SENSORS 2
+	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
+			       sizeof(struct mfd_cell),
 			       GFP_KERNEL);
 	if (sensor_cells == NULL)
 		goto error;
 
-	sensor_platforms = kcalloc(sensor_num + 1,
+	sensor_platforms = kcalloc(sensor_num,
 				   sizeof(struct cros_ec_sensor_platform),
 				   GFP_KERNEL);
 	if (sensor_platforms == NULL)
@@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 		sensor_cells[id].name = "cros-ec-ring";
 		id++;
 	}
+	if (cros_ec_check_features(ec,
+				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
+		sensor_cells[id].name = "cros-ec-lid-angle";
+		id++;
+	}
 
 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
 			      NULL, 0, NULL);
-- 
2.21.0.rc2.261.ga7da99ff1b-goog


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

* Re: [PATCH] iio: cros_ec: Add lid angle driver
  2019-02-28  1:50 [PATCH] iio: cros_ec: Add lid angle driver Gwendal Grignou
@ 2019-02-28 11:18 ` Enric Balletbo i Serra
  2019-02-28 17:41   ` Gwendal Grignou
  2019-03-11  8:24 ` [PATCH] " Lee Jones
  1 sibling, 1 reply; 18+ messages in thread
From: Enric Balletbo i Serra @ 2019-02-28 11:18 UTC (permalink / raw)
  To: Gwendal Grignou, lee.jones, groeck, bleung, lars, jic23, egranata
  Cc: linux-iio

Hi Gwendal,

Thanks to send this upstream.

On 28/2/19 2:50, Gwendal Grignou wrote:
> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
> 
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 164 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  15 +-

The mfd/cros_ec_dev.c part looks good to me. I have some few comments in the IIO
part, see below.

>  4 files changed, 186 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> 
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>  	  Accelerometers, Gyroscope and Magnetometer that are
>  	  presented by the ChromeOS EC Sensor hub.
>  	  Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +	tristate "ChromeOS EC Sensor for lid angle"
> +	depends on IIO_CROS_EC_SENSORS_CORE
> +	help
> +	  Module to report the angle between lid and base for some
> +	  convertible devices.
> +	  This module is loaded when the EC can calculate the angle between the base
> +	  and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
>  
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..370b3e9a1405
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,164 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +

There is a license mismatch between the SPDX identifier (GPLv2 or later) and the
MODULE_LICENSE (GPLv2). I think that this should be only GPL-2.0

> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +#define MAX_CHANNELS (1 + 1)
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +	/* Shared by all sensors */
> +	struct cros_ec_sensors_core_state core;
> +
> +	struct iio_chan_spec channels[MAX_CHANNELS];
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +					  unsigned long scan_mask, s16 *data)
> +{
> +	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +	if (ret) {
> +		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +		return ret;
> +	}
> +
> +	if (scan_mask & 1)
> +		*data = st->resp->lid_angle.value;
> +	return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +				    struct iio_chan_spec const *chan,
> +				    int *val, int *val2, long mask)
> +{
> +	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +	u16 data;
> +	int ret;
> +	int idx = chan->scan_index;
> +
> +	mutex_lock(&st->core.cmd_lock);
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		ret = cros_ec_sensors_read_lid_angle(
> +				indio_dev, 1 << idx, &data);
> +		if (ret)
> +			break;
> +		ret = IIO_VAL_INT;
> +		*val = data;
> +		break;
> +	default:
> +		ret = -EINVAL;
> +	}
> +	mutex_unlock(&st->core.cmd_lock);
> +	return ret;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +	.read_raw = &cros_ec_lid_angle_read,
> +	.driver_module = THIS_MODULE,

This is not needed now, you can remove it.

> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct cros_ec_lid_angle_state *state;
> +	struct iio_chan_spec *channel;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +	if (ret)
> +		return ret;
> +
> +	indio_dev->info = &cros_ec_lid_angle_info;
> +	state = iio_priv(indio_dev);
> +	channel = state->channels;
> +
> +	channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
> +	channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
> +	channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
> +	channel->scan_type.shift = 0;
> +	channel->scan_index = 0;
> +	channel->scan_type.sign = 'u';
> +	channel->type = IIO_ANGL;
> +
> +	state->core.calib[0] = 0;
> +
> +	/* Timestamp */
> +	channel++;
> +	channel->type = IIO_TIMESTAMP;
> +	channel->channel = -1;
> +	channel->scan_index = 1;
> +	channel->scan_type.sign = 's';
> +	channel->scan_type.realbits = 64;
> +	channel->scan_type.storagebits = 64;
> +
> +	indio_dev->channels = state->channels;
> +	indio_dev->num_channels = MAX_CHANNELS;
> +
> +	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +			cros_ec_sensors_capture, NULL);
> +	if (ret)
> +		return ret;
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +	{
> +		.name = DRV_NAME,
> +	},
> +	{ /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +	.driver = {
> +		.name	= DRV_NAME,
> +		.pm	= &cros_ec_sensors_pm_ops,
> +	},
> +	.probe		= cros_ec_lid_angle_probe,
> +	.id_table	= cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..11b5b2fd1f33 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  
>  	resp = (struct ec_response_motion_sense *)msg->data;
>  	sensor_num = resp->dump.sensor_count;
> -	/* Allocate 1 extra sensors in FIFO are needed */
> -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +	/*
> +	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
> +	 */
> +#define NUM_EXTRA_SENSORS 2
> +	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> +			       sizeof(struct mfd_cell),
>  			       GFP_KERNEL);
>  	if (sensor_cells == NULL)
>  		goto error;
>  
> -	sensor_platforms = kcalloc(sensor_num + 1,
> +	sensor_platforms = kcalloc(sensor_num,
>  				   sizeof(struct cros_ec_sensor_platform),
>  				   GFP_KERNEL);
>  	if (sensor_platforms == NULL)
> @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  		sensor_cells[id].name = "cros-ec-ring";
>  		id++;
>  	}
> +	if (cros_ec_check_features(ec,
> +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +		sensor_cells[id].name = "cros-ec-lid-angle";
> +		id++;
> +	}
>  
>  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>  			      NULL, 0, NULL);
> 

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

* Re: [PATCH] iio: cros_ec: Add lid angle driver
  2019-02-28 11:18 ` Enric Balletbo i Serra
@ 2019-02-28 17:41   ` Gwendal Grignou
  2019-02-28 17:41     ` [PATCH v2] " Gwendal Grignou
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-02-28 17:41 UTC (permalink / raw)
  To: Enric Balletbo i Serra
  Cc: lee.jones, Guenter Roeck, bleung, lars, jic23, egranata, linux-iio

On Thu, Feb 28, 2019 at 3:18 AM Enric Balletbo i Serra
<enric.balletbo@collabora.com> wrote:
>
> Hi Gwendal,
>
> Thanks to send this upstream.
>
> On 28/2/19 2:50, Gwendal Grignou wrote:
> > Add a IIO driver that reports the angle between the lid and the base for
> > ChromeOS convertible device.
> >
> > Tested on eve with ToT EC firmware.
> > Check driver is loaded and lid angle is correct.
> >
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> > ---
> >  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
> >  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
> >  .../cros_ec_sensors/cros_ec_lid_angle.c       | 164 ++++++++++++++++++
> >  drivers/mfd/cros_ec_dev.c                     |  15 +-
>
> The mfd/cros_ec_dev.c part looks good to me. I have some few comments in the IIO
> part, see below.
>
> >  4 files changed, 186 insertions(+), 3 deletions(-)
> >  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> >
> > diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> > index 135f6825903f..aacc2ab9c34f 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> > +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> > @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
> >         Accelerometers, Gyroscope and Magnetometer that are
> >         presented by the ChromeOS EC Sensor hub.
> >         Creates an IIO device for each functions.
> > +
> > +config IIO_CROS_EC_SENSORS_LID_ANGLE
> > +     tristate "ChromeOS EC Sensor for lid angle"
> > +     depends on IIO_CROS_EC_SENSORS_CORE
> > +     help
> > +       Module to report the angle between lid and base for some
> > +       convertible devices.
> > +       This module is loaded when the EC can calculate the angle between the base
> > +       and the lid.
> > diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> > index ec716ff2a775..a35ee232ac07 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Makefile
> > +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> > @@ -4,3 +4,4 @@
> >
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> > +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> > diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > new file mode 100644
> > index 000000000000..370b3e9a1405
> > --- /dev/null
> > +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > @@ -0,0 +1,164 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +
>
> There is a license mismatch between the SPDX identifier (GPLv2 or later) and the
> MODULE_LICENSE (GPLv2). I think that this should be only GPL-2.0
Done.
>
> > +/*
> > + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> > + *
...
> > +static const struct iio_info cros_ec_lid_angle_info = {
> > +     .read_raw = &cros_ec_lid_angle_read,
> > +     .driver_module = THIS_MODULE,
>
> This is not needed now, you can remove it.
Done.
>
> > +};
> > +
> > +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
...

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

* [PATCH v2] iio: cros_ec: Add lid angle driver
  2019-02-28 17:41   ` Gwendal Grignou
@ 2019-02-28 17:41     ` Gwendal Grignou
  2019-03-03 12:43       ` Jonathan Cameron
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-02-28 17:41 UTC (permalink / raw)
  To: lee.jones, groeck, enric.balletbo, bleung, lars, jic23, egranata
  Cc: linux-iio

Add a IIO driver that reports the angle between the lid and the base for
ChromeOS convertible device.

Tested on eve with ToT EC firmware.
Check driver is loaded and lid angle is correct.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
Changes in v2:
- Fix license, remove driver_module field.

 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../cros_ec_sensors/cros_ec_lid_angle.c       | 163 ++++++++++++++++++
 drivers/mfd/cros_ec_dev.c                     |  15 +-
 4 files changed, 185 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
index 135f6825903f..aacc2ab9c34f 100644
--- a/drivers/iio/common/cros_ec_sensors/Kconfig
+++ b/drivers/iio/common/cros_ec_sensors/Kconfig
@@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
 	  Accelerometers, Gyroscope and Magnetometer that are
 	  presented by the ChromeOS EC Sensor hub.
 	  Creates an IIO device for each functions.
+
+config IIO_CROS_EC_SENSORS_LID_ANGLE
+	tristate "ChromeOS EC Sensor for lid angle"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to report the angle between lid and base for some
+	  convertible devices.
+	  This module is loaded when the EC can calculate the angle between the base
+	  and the lid.
diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
index ec716ff2a775..a35ee232ac07 100644
--- a/drivers/iio/common/cros_ec_sensors/Makefile
+++ b/drivers/iio/common/cros_ec_sensors/Makefile
@@ -4,3 +4,4 @@
 
 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
new file mode 100644
index 000000000000..09d30976a743
--- /dev/null
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
+ *
+ * Copyright 2018 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS
+ * EC about counter sensors. Counters are presented through
+ * iio sysfs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#define DRV_NAME "cros-ec-lid-angle"
+
+/*
+ * One channel for the lid angle, the other for timestamp.
+ */
+#define MAX_CHANNELS (1 + 1)
+
+/* State data for ec_sensors iio driver. */
+struct cros_ec_lid_angle_state {
+	/* Shared by all sensors */
+	struct cros_ec_sensors_core_state core;
+
+	struct iio_chan_spec channels[MAX_CHANNELS];
+};
+
+static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
+					  unsigned long scan_mask, s16 *data)
+{
+	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
+	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
+	if (ret) {
+		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
+		return ret;
+	}
+
+	if (scan_mask & 1)
+		*data = st->resp->lid_angle.value;
+	return 0;
+}
+
+static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan,
+				    int *val, int *val2, long mask)
+{
+	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
+	u16 data;
+	int ret;
+	int idx = chan->scan_index;
+
+	mutex_lock(&st->core.cmd_lock);
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = cros_ec_sensors_read_lid_angle(
+				indio_dev, 1 << idx, &data);
+		if (ret)
+			break;
+		ret = IIO_VAL_INT;
+		*val = data;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+	mutex_unlock(&st->core.cmd_lock);
+	return ret;
+}
+
+static const struct iio_info cros_ec_lid_angle_info = {
+	.read_raw = &cros_ec_lid_angle_read,
+};
+
+static int cros_ec_lid_angle_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct cros_ec_lid_angle_state *state;
+	struct iio_chan_spec *channel;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &cros_ec_lid_angle_info;
+	state = iio_priv(indio_dev);
+	channel = state->channels;
+
+	channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
+	channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
+	channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
+	channel->scan_type.shift = 0;
+	channel->scan_index = 0;
+	channel->scan_type.sign = 'u';
+	channel->type = IIO_ANGL;
+
+	state->core.calib[0] = 0;
+
+	/* Timestamp */
+	channel++;
+	channel->type = IIO_TIMESTAMP;
+	channel->channel = -1;
+	channel->scan_index = 1;
+	channel->scan_type.sign = 's';
+	channel->scan_type.realbits = 64;
+	channel->scan_type.storagebits = 64;
+
+	indio_dev->channels = state->channels;
+	indio_dev->num_channels = MAX_CHANNELS;
+
+	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+			cros_ec_sensors_capture, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct platform_device_id cros_ec_lid_angle_ids[] = {
+	{
+		.name = DRV_NAME,
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
+
+static struct platform_driver cros_ec_lid_angle_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+		.pm	= &cros_ec_sensors_pm_ops,
+	},
+	.probe		= cros_ec_lid_angle_probe,
+	.id_table	= cros_ec_lid_angle_ids,
+};
+module_platform_driver(cros_ec_lid_angle_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index d275deaecb12..11b5b2fd1f33 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 
 	resp = (struct ec_response_motion_sense *)msg->data;
 	sensor_num = resp->dump.sensor_count;
-	/* Allocate 1 extra sensors in FIFO are needed */
-	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
+	/*
+	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
+	 */
+#define NUM_EXTRA_SENSORS 2
+	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
+			       sizeof(struct mfd_cell),
 			       GFP_KERNEL);
 	if (sensor_cells == NULL)
 		goto error;
 
-	sensor_platforms = kcalloc(sensor_num + 1,
+	sensor_platforms = kcalloc(sensor_num,
 				   sizeof(struct cros_ec_sensor_platform),
 				   GFP_KERNEL);
 	if (sensor_platforms == NULL)
@@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 		sensor_cells[id].name = "cros-ec-ring";
 		id++;
 	}
+	if (cros_ec_check_features(ec,
+				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
+		sensor_cells[id].name = "cros-ec-lid-angle";
+		id++;
+	}
 
 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
 			      NULL, 0, NULL);
-- 
2.21.0.rc2.261.ga7da99ff1b-goog


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

* Re: [PATCH v2] iio: cros_ec: Add lid angle driver
  2019-02-28 17:41     ` [PATCH v2] " Gwendal Grignou
@ 2019-03-03 12:43       ` Jonathan Cameron
  2019-03-07  0:33         ` Gwendal Grignou
  0 siblings, 1 reply; 18+ messages in thread
From: Jonathan Cameron @ 2019-03-03 12:43 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: lee.jones, groeck, enric.balletbo, bleung, lars, egranata, linux-iio

On Thu, 28 Feb 2019 09:41:54 -0800
Gwendal Grignou <gwendal@chromium.org> wrote:

> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
> 
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>

Hi Gwendal,

Just a few minor comments inline.  It seems a bit like this has been
copied from a driver needing a lot more flexibility and as a result
various items that can be hard coded, simplifying this, are left
flexible.

Also one question about the cros_ec part.

Jonathan

> ---
> Changes in v2:
> - Fix license, remove driver_module field.
> 
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 163 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  15 +-
>  4 files changed, 185 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> 
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>  	  Accelerometers, Gyroscope and Magnetometer that are
>  	  presented by the ChromeOS EC Sensor hub.
>  	  Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +	tristate "ChromeOS EC Sensor for lid angle"
> +	depends on IIO_CROS_EC_SENSORS_CORE
> +	help
> +	  Module to report the angle between lid and base for some
> +	  convertible devices.
> +	  This module is loaded when the EC can calculate the angle between the base
> +	  and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
>  
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..09d30976a743
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,163 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +#define MAX_CHANNELS (1 + 1)
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +	/* Shared by all sensors */
> +	struct cros_ec_sensors_core_state core;
> +
> +	struct iio_chan_spec channels[MAX_CHANNELS];
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +					  unsigned long scan_mask, s16 *data)
> +{
> +	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +	if (ret) {
> +		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +		return ret;
> +	}
> +
> +	if (scan_mask & 1)

As below, this seems like flexibility with no benefit. I think it
is always true.

> +		*data = st->resp->lid_angle.value;
> +	return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +				    struct iio_chan_spec const *chan,
> +				    int *val, int *val2, long mask)
> +{
> +	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +	u16 data;
> +	int ret;
> +	int idx = chan->scan_index;
> +
> +	mutex_lock(&st->core.cmd_lock);
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		ret = cros_ec_sensors_read_lid_angle(
> +				indio_dev, 1 << idx, &data);

This seems to suffer a bit from unwarranted flexibility.
idx is always 0 in this driver - hence there is an obvious
simplification!

> +		if (ret)
> +			break;
> +		ret = IIO_VAL_INT;
> +		*val = data;
> +		break;
> +	default:
> +		ret = -EINVAL;
> +	}
> +	mutex_unlock(&st->core.cmd_lock);
> +	return ret;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +	.read_raw = &cros_ec_lid_angle_read,
> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct cros_ec_lid_angle_state *state;
> +	struct iio_chan_spec *channel;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +	if (ret)
> +		return ret;
> +
> +	indio_dev->info = &cros_ec_lid_angle_info;
> +	state = iio_priv(indio_dev);
> +	channel = state->channels;
> +
> +	channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
> +	channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
> +	channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
> +	channel->scan_type.shift = 0;

Shift has an 'obvious' default of 0 so I wouldn't bother
explicitly setting it.

> +	channel->scan_index = 0;
> +	channel->scan_type.sign = 'u';
> +	channel->type = IIO_ANGL;
> +
> +	state->core.calib[0] = 0;
> +
> +	/* Timestamp */
> +	channel++;
> +	channel->type = IIO_TIMESTAMP;
> +	channel->channel = -1;
> +	channel->scan_index = 1;
> +	channel->scan_type.sign = 's';
> +	channel->scan_type.realbits = 64;
> +	channel->scan_type.storagebits = 64;
> +

These appear to be constant.  Why not just use a static
const array for them rather than within the state structure?

> +	indio_dev->channels = state->channels;
> +	indio_dev->num_channels = MAX_CHANNELS;
> +
> +	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +			cros_ec_sensors_capture, NULL);
> +	if (ret)
> +		return ret;
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +	{
> +		.name = DRV_NAME,
> +	},
> +	{ /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +	.driver = {
> +		.name	= DRV_NAME,
> +		.pm	= &cros_ec_sensors_pm_ops,
> +	},
> +	.probe		= cros_ec_lid_angle_probe,
> +	.id_table	= cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..11b5b2fd1f33 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  
>  	resp = (struct ec_response_motion_sense *)msg->data;
>  	sensor_num = resp->dump.sensor_count;
> -	/* Allocate 1 extra sensors in FIFO are needed */
> -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +	/*
> +	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
> +	 */
> +#define NUM_EXTRA_SENSORS 2
> +	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> +			       sizeof(struct mfd_cell),
>  			       GFP_KERNEL);
>  	if (sensor_cells == NULL)
>  		goto error;
>  
> -	sensor_platforms = kcalloc(sensor_num + 1,
> +	sensor_platforms = kcalloc(sensor_num,

This change has me curious.  Why was the +1 ever needed?
As far as I can see we only ever use <= sensor_num entries in this array.

>  				   sizeof(struct cros_ec_sensor_platform),
>  				   GFP_KERNEL);
>  	if (sensor_platforms == NULL)
> @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  		sensor_cells[id].name = "cros-ec-ring";
>  		id++;
>  	}
> +	if (cros_ec_check_features(ec,
> +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +		sensor_cells[id].name = "cros-ec-lid-angle";
> +		id++;
> +	}
>  
>  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>  			      NULL, 0, NULL);


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

* Re: [PATCH v2] iio: cros_ec: Add lid angle driver
  2019-03-03 12:43       ` Jonathan Cameron
@ 2019-03-07  0:33         ` Gwendal Grignou
  2019-03-07  0:35           ` [PATCH v3] " Gwendal Grignou
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-07  0:33 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lee Jones, Guenter Roeck, Enric Balletbo i Serra, Benson Leung,
	lars, Enrico Granata, linux-iio

On Sun, Mar 3, 2019 at 4:43 AM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Thu, 28 Feb 2019 09:41:54 -0800
> Gwendal Grignou <gwendal@chromium.org> wrote:
>
> > Add a IIO driver that reports the angle between the lid and the base for
> > ChromeOS convertible device.
> >
> > Tested on eve with ToT EC firmware.
> > Check driver is loaded and lid angle is correct.
> >
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
>
> Hi Gwendal,
>
> Just a few minor comments inline.  It seems a bit like this has been
> copied from a driver needing a lot more flexibility and as a result
> various items that can be hard coded, simplifying this, are left
> flexible.
>
> Also one question about the cros_ec part.
>
> Jonathan
Thanks for the review, v3 coming soon.

Gwendal.
>
> > ---
> > Changes in v2:
> > - Fix license, remove driver_module field.
> >
> >  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
> >  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
> >  .../cros_ec_sensors/cros_ec_lid_angle.c       | 163 ++++++++++++++++++
> >  drivers/mfd/cros_ec_dev.c                     |  15 +-
> >  4 files changed, 185 insertions(+), 3 deletions(-)
> >  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> >
> > diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> > index 135f6825903f..aacc2ab9c34f 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> > +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> > @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
> >         Accelerometers, Gyroscope and Magnetometer that are
> >         presented by the ChromeOS EC Sensor hub.
> >         Creates an IIO device for each functions.
> > +
> > +config IIO_CROS_EC_SENSORS_LID_ANGLE
> > +     tristate "ChromeOS EC Sensor for lid angle"
> > +     depends on IIO_CROS_EC_SENSORS_CORE
> > +     help
> > +       Module to report the angle between lid and base for some
> > +       convertible devices.
> > +       This module is loaded when the EC can calculate the angle between the base
> > +       and the lid.
> > diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> > index ec716ff2a775..a35ee232ac07 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Makefile
> > +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> > @@ -4,3 +4,4 @@
> >
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> > +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> > diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > new file mode 100644
> > index 000000000000..09d30976a743
> > --- /dev/null
> > +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > @@ -0,0 +1,163 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +/*
> > + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> > + *
> > + * Copyright 2018 Google, Inc
> > + *
> > + * This driver uses the cros-ec interface to communicate with the Chrome OS
> > + * EC about counter sensors. Counters are presented through
> > + * iio sysfs.
> > + */
> > +
> > +#include <linux/delay.h>
> > +#include <linux/device.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/common/cros_ec_sensors_core.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/kfifo_buf.h>
> > +#include <linux/iio/trigger.h>
> > +#include <linux/iio/triggered_buffer.h>
> > +#include <linux/iio/trigger_consumer.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/cros_ec.h>
> > +#include <linux/mfd/cros_ec_commands.h>
> > +#include <linux/module.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +#define DRV_NAME "cros-ec-lid-angle"
> > +
> > +/*
> > + * One channel for the lid angle, the other for timestamp.
> > + */
> > +#define MAX_CHANNELS (1 + 1)
> > +
> > +/* State data for ec_sensors iio driver. */
> > +struct cros_ec_lid_angle_state {
> > +     /* Shared by all sensors */
> > +     struct cros_ec_sensors_core_state core;
> > +
> > +     struct iio_chan_spec channels[MAX_CHANNELS];
> > +};
> > +
> > +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> > +                                       unsigned long scan_mask, s16 *data)
> > +{
> > +     struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> > +     int ret;
> > +
> > +     st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> > +     ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> > +     if (ret) {
> > +             dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> > +             return ret;
> > +     }
> > +
> > +     if (scan_mask & 1)
>
> As below, this seems like flexibility with no benefit. I think it
> is always true.
Done.
>
> > +             *data = st->resp->lid_angle.value;
> > +     return 0;
> > +}
> > +
> > +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> > +                                 struct iio_chan_spec const *chan,
> > +                                 int *val, int *val2, long mask)
> > +{
> > +     struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> > +     u16 data;
> > +     int ret;
> > +     int idx = chan->scan_index;
> > +
> > +     mutex_lock(&st->core.cmd_lock);
> > +     switch (mask) {
> > +     case IIO_CHAN_INFO_RAW:
> > +             ret = cros_ec_sensors_read_lid_angle(
> > +                             indio_dev, 1 << idx, &data);
>
> This seems to suffer a bit from unwarranted flexibility.
> idx is always 0 in this driver - hence there is an obvious
> simplification!
Done: I kept cros_ec_sensors_read_lid_angle as it is use when
populating /dev/iio:deviceX.
>
> > +             if (ret)
> > +                     break;
> > +             ret = IIO_VAL_INT;
> > +             *val = data;
> > +             break;
> > +     default:
> > +             ret = -EINVAL;
> > +     }
> > +     mutex_unlock(&st->core.cmd_lock);
> > +     return ret;
> > +}
> > +
> > +static const struct iio_info cros_ec_lid_angle_info = {
> > +     .read_raw = &cros_ec_lid_angle_read,
> > +};
> > +
> > +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> > +{
> > +     struct device *dev = &pdev->dev;
> > +     struct iio_dev *indio_dev;
> > +     struct cros_ec_lid_angle_state *state;
> > +     struct iio_chan_spec *channel;
> > +     int ret;
> > +
> > +     indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> > +     if (!indio_dev)
> > +             return -ENOMEM;
> > +
> > +     ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> > +     if (ret)
> > +             return ret;
> > +
> > +     indio_dev->info = &cros_ec_lid_angle_info;
> > +     state = iio_priv(indio_dev);
> > +     channel = state->channels;
> > +
> > +     channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
> > +     channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
> > +     channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
> > +     channel->scan_type.shift = 0;
>
> Shift has an 'obvious' default of 0 so I wouldn't bother
> explicitly setting it.
Done.
>
> > +     channel->scan_index = 0;
> > +     channel->scan_type.sign = 'u';
> > +     channel->type = IIO_ANGL;
> > +
> > +     state->core.calib[0] = 0;
> > +
> > +     /* Timestamp */
> > +     channel++;
> > +     channel->type = IIO_TIMESTAMP;
> > +     channel->channel = -1;
> > +     channel->scan_index = 1;
> > +     channel->scan_type.sign = 's';
> > +     channel->scan_type.realbits = 64;
> > +     channel->scan_type.storagebits = 64;
> > +
>
> These appear to be constant.  Why not just use a static
> const array for them rather than within the state structure?
Done.
>
> > +     indio_dev->channels = state->channels;
> > +     indio_dev->num_channels = MAX_CHANNELS;
> > +
> > +     state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> > +
> > +     ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> > +                     cros_ec_sensors_capture, NULL);
> > +     if (ret)
> > +             return ret;
> > +
> > +     return devm_iio_device_register(dev, indio_dev);
> > +}
> > +
> > +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> > +     {
> > +             .name = DRV_NAME,
> > +     },
> > +     { /* sentinel */ }
> > +};
> > +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> > +
> > +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> > +     .driver = {
> > +             .name   = DRV_NAME,
> > +             .pm     = &cros_ec_sensors_pm_ops,
> > +     },
> > +     .probe          = cros_ec_lid_angle_probe,
> > +     .id_table       = cros_ec_lid_angle_ids,
> > +};
> > +module_platform_driver(cros_ec_lid_angle_platform_driver);
> > +
> > +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> > +MODULE_LICENSE("GPL v2");
> > diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> > index d275deaecb12..11b5b2fd1f33 100644
> > --- a/drivers/mfd/cros_ec_dev.c
> > +++ b/drivers/mfd/cros_ec_dev.c
> > @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> >
> >       resp = (struct ec_response_motion_sense *)msg->data;
> >       sensor_num = resp->dump.sensor_count;
> > -     /* Allocate 1 extra sensors in FIFO are needed */
> > -     sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> > +     /*
> > +      * Allocate extra sensors if lid angle sensor or FIFO are needed.
> > +      */
> > +#define NUM_EXTRA_SENSORS 2
> > +     sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> > +                            sizeof(struct mfd_cell),
> >                              GFP_KERNEL);
> >       if (sensor_cells == NULL)
> >               goto error;
> >
> > -     sensor_platforms = kcalloc(sensor_num + 1,
> > +     sensor_platforms = kcalloc(sensor_num,
>
> This change has me curious.  Why was the +1 ever needed?
No, it was a mistake.
> As far as I can see we only ever use <= sensor_num entries in this array.
>
> >                                  sizeof(struct cros_ec_sensor_platform),
> >                                  GFP_KERNEL);
> >       if (sensor_platforms == NULL)
> > @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> >               sensor_cells[id].name = "cros-ec-ring";
> >               id++;
> >       }
> > +     if (cros_ec_check_features(ec,
> > +                             EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> > +             sensor_cells[id].name = "cros-ec-lid-angle";
> > +             id++;
> > +     }
> >
> >       ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> >                             NULL, 0, NULL);
>

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

* [PATCH v3] iio: cros_ec: Add lid angle driver
  2019-03-07  0:33         ` Gwendal Grignou
@ 2019-03-07  0:35           ` Gwendal Grignou
  2019-03-07 12:32             ` Enric Balletbo i Serra
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-07  0:35 UTC (permalink / raw)
  To: lee.jones, groeck, enric.balletbo, bleung, lars, jic23, egranata
  Cc: linux-iio

Add a IIO driver that reports the angle between the lid and the base for
ChromeOS convertible device.

Tested on eve with ToT EC firmware.
Check driver is loaded and lid angle is correct.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
Changes in v3:
- Use static channel array, simplify code because index is always 0.

Changes in v2:
- Fix license, remove driver_module field.

 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
 drivers/mfd/cros_ec_dev.c                     |  15 +-
 4 files changed, 160 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
index 135f6825903f..aacc2ab9c34f 100644
--- a/drivers/iio/common/cros_ec_sensors/Kconfig
+++ b/drivers/iio/common/cros_ec_sensors/Kconfig
@@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
 	  Accelerometers, Gyroscope and Magnetometer that are
 	  presented by the ChromeOS EC Sensor hub.
 	  Creates an IIO device for each functions.
+
+config IIO_CROS_EC_SENSORS_LID_ANGLE
+	tristate "ChromeOS EC Sensor for lid angle"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to report the angle between lid and base for some
+	  convertible devices.
+	  This module is loaded when the EC can calculate the angle between the base
+	  and the lid.
diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
index ec716ff2a775..a35ee232ac07 100644
--- a/drivers/iio/common/cros_ec_sensors/Makefile
+++ b/drivers/iio/common/cros_ec_sensors/Makefile
@@ -4,3 +4,4 @@
 
 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
new file mode 100644
index 000000000000..92be07d7fa36
--- /dev/null
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
+ *
+ * Copyright 2018 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS
+ * EC about counter sensors. Counters are presented through
+ * iio sysfs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#define DRV_NAME "cros-ec-lid-angle"
+
+/*
+ * One channel for the lid angle, the other for timestamp.
+ */
+static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
+	{
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_type.realbits = CROS_EC_SENSOR_BITS,
+		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
+		.scan_type.sign = 'u',
+		.type = IIO_ANGL
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+/* State data for ec_sensors iio driver. */
+struct cros_ec_lid_angle_state {
+	/* Shared by all sensors */
+	struct cros_ec_sensors_core_state core;
+};
+
+static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
+					  unsigned long scan_mask, s16 *data)
+{
+	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
+	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
+	if (ret) {
+		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
+		return ret;
+	}
+
+	*data = st->resp->lid_angle.value;
+	return 0;
+}
+
+static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan,
+				    int *val, int *val2, long mask)
+{
+	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
+	s16 data;
+	int ret;
+
+	mutex_lock(&st->core.cmd_lock);
+	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
+	if (ret)
+		return ret;
+	*val = data;
+	mutex_unlock(&st->core.cmd_lock);
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info cros_ec_lid_angle_info = {
+	.read_raw = &cros_ec_lid_angle_read,
+};
+
+static int cros_ec_lid_angle_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct cros_ec_lid_angle_state *state;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &cros_ec_lid_angle_info;
+	state = iio_priv(indio_dev);
+	indio_dev->channels = cros_ec_lid_angle_channels;
+	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
+
+	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+			cros_ec_sensors_capture, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct platform_device_id cros_ec_lid_angle_ids[] = {
+	{
+		.name = DRV_NAME,
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
+
+static struct platform_driver cros_ec_lid_angle_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+		.pm	= &cros_ec_sensors_pm_ops,
+	},
+	.probe		= cros_ec_lid_angle_probe,
+	.id_table	= cros_ec_lid_angle_ids,
+};
+module_platform_driver(cros_ec_lid_angle_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index d275deaecb12..11b5b2fd1f33 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 
 	resp = (struct ec_response_motion_sense *)msg->data;
 	sensor_num = resp->dump.sensor_count;
-	/* Allocate 1 extra sensors in FIFO are needed */
-	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
+	/*
+	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
+	 */
+#define NUM_EXTRA_SENSORS 2
+	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
+			       sizeof(struct mfd_cell),
 			       GFP_KERNEL);
 	if (sensor_cells == NULL)
 		goto error;
 
-	sensor_platforms = kcalloc(sensor_num + 1,
+	sensor_platforms = kcalloc(sensor_num,
 				   sizeof(struct cros_ec_sensor_platform),
 				   GFP_KERNEL);
 	if (sensor_platforms == NULL)
@@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
 		sensor_cells[id].name = "cros-ec-ring";
 		id++;
 	}
+	if (cros_ec_check_features(ec,
+				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
+		sensor_cells[id].name = "cros-ec-lid-angle";
+		id++;
+	}
 
 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
 			      NULL, 0, NULL);
-- 
2.21.0.352.gf09ad66450-goog


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

* Re: [PATCH v3] iio: cros_ec: Add lid angle driver
  2019-03-07  0:35           ` [PATCH v3] " Gwendal Grignou
@ 2019-03-07 12:32             ` Enric Balletbo i Serra
  2019-03-09 17:43               ` Jonathan Cameron
  0 siblings, 1 reply; 18+ messages in thread
From: Enric Balletbo i Serra @ 2019-03-07 12:32 UTC (permalink / raw)
  To: Gwendal Grignou, lee.jones, groeck, bleung, lars, jic23, egranata
  Cc: linux-iio

Hi,

On 7/3/19 1:35, Gwendal Grignou wrote:
> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
> 
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
> Changes in v3:
> - Use static channel array, simplify code because index is always 0.
> 
> Changes in v2:
> - Fix license, remove driver_module field.
> 
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  15 +-

Note that this patch depends on https://lkml.org/lkml/2019/2/27/723 to build.

Acked-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>

Thanks,
 Enric

>  4 files changed, 160 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> 
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>  	  Accelerometers, Gyroscope and Magnetometer that are
>  	  presented by the ChromeOS EC Sensor hub.
>  	  Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +	tristate "ChromeOS EC Sensor for lid angle"
> +	depends on IIO_CROS_EC_SENSORS_CORE
> +	help
> +	  Module to report the angle between lid and base for some
> +	  convertible devices.
> +	  This module is loaded when the EC can calculate the angle between the base
> +	  and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
>  
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..92be07d7fa36
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,138 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> +	{
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> +		.scan_type.realbits = CROS_EC_SENSOR_BITS,
> +		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
> +		.scan_type.sign = 'u',
> +		.type = IIO_ANGL
> +	},
> +	IIO_CHAN_SOFT_TIMESTAMP(1)
> +};
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +	/* Shared by all sensors */
> +	struct cros_ec_sensors_core_state core;
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +					  unsigned long scan_mask, s16 *data)
> +{
> +	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +	if (ret) {
> +		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +		return ret;
> +	}
> +
> +	*data = st->resp->lid_angle.value;
> +	return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +				    struct iio_chan_spec const *chan,
> +				    int *val, int *val2, long mask)
> +{
> +	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +	s16 data;
> +	int ret;
> +
> +	mutex_lock(&st->core.cmd_lock);
> +	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> +	if (ret)
> +		return ret;
> +	*val = data;
> +	mutex_unlock(&st->core.cmd_lock);
> +	return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +	.read_raw = &cros_ec_lid_angle_read,
> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct cros_ec_lid_angle_state *state;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +	if (ret)
> +		return ret;
> +
> +	indio_dev->info = &cros_ec_lid_angle_info;
> +	state = iio_priv(indio_dev);
> +	indio_dev->channels = cros_ec_lid_angle_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> +
> +	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +			cros_ec_sensors_capture, NULL);
> +	if (ret)
> +		return ret;
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +	{
> +		.name = DRV_NAME,
> +	},
> +	{ /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +	.driver = {
> +		.name	= DRV_NAME,
> +		.pm	= &cros_ec_sensors_pm_ops,
> +	},
> +	.probe		= cros_ec_lid_angle_probe,
> +	.id_table	= cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..11b5b2fd1f33 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  
>  	resp = (struct ec_response_motion_sense *)msg->data;
>  	sensor_num = resp->dump.sensor_count;
> -	/* Allocate 1 extra sensors in FIFO are needed */
> -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +	/*
> +	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
> +	 */
> +#define NUM_EXTRA_SENSORS 2
> +	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> +			       sizeof(struct mfd_cell),
>  			       GFP_KERNEL);
>  	if (sensor_cells == NULL)
>  		goto error;
>  
> -	sensor_platforms = kcalloc(sensor_num + 1,
> +	sensor_platforms = kcalloc(sensor_num,
>  				   sizeof(struct cros_ec_sensor_platform),
>  				   GFP_KERNEL);
>  	if (sensor_platforms == NULL)
> @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  		sensor_cells[id].name = "cros-ec-ring";
>  		id++;
>  	}
> +	if (cros_ec_check_features(ec,
> +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +		sensor_cells[id].name = "cros-ec-lid-angle";
> +		id++;
> +	}
>  
>  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>  			      NULL, 0, NULL);
> 

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

* Re: [PATCH v3] iio: cros_ec: Add lid angle driver
  2019-03-07 12:32             ` Enric Balletbo i Serra
@ 2019-03-09 17:43               ` Jonathan Cameron
  0 siblings, 0 replies; 18+ messages in thread
From: Jonathan Cameron @ 2019-03-09 17:43 UTC (permalink / raw)
  To: Enric Balletbo i Serra
  Cc: Gwendal Grignou, lee.jones, groeck, bleung, lars, egranata, linux-iio

On Thu, 7 Mar 2019 13:32:44 +0100
Enric Balletbo i Serra <enric.balletbo@collabora.com> wrote:

> Hi,
> 
> On 7/3/19 1:35, Gwendal Grignou wrote:
> > Add a IIO driver that reports the angle between the lid and the base for
> > ChromeOS convertible device.
> > 
> > Tested on eve with ToT EC firmware.
> > Check driver is loaded and lid angle is correct.
> > 
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> > ---
> > Changes in v3:
> > - Use static channel array, simplify code because index is always 0.
> > 
> > Changes in v2:
> > - Fix license, remove driver_module field.
> > 
> >  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
> >  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
> >  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
> >  drivers/mfd/cros_ec_dev.c                     |  15 +-  
> 
> Note that this patch depends on https://lkml.org/lkml/2019/2/27/723 to build.
Thanks for the heads up!

Gwendal, chances are I'll forget about this patch, so I'd appreciate
a poke once that precursor is upstream or ideally when it turns up
in staging/staging-next so I can rebase to get it into the iio togreg
branch.

Otherwise, looks good to me.

Thanks,

Jonathan
> 
> Acked-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
> 
> Thanks,
>  Enric
> 
> >  4 files changed, 160 insertions(+), 3 deletions(-)
> >  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > 
> > diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig b/drivers/iio/common/cros_ec_sensors/Kconfig
> > index 135f6825903f..aacc2ab9c34f 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> > +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> > @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
> >  	  Accelerometers, Gyroscope and Magnetometer that are
> >  	  presented by the ChromeOS EC Sensor hub.
> >  	  Creates an IIO device for each functions.
> > +
> > +config IIO_CROS_EC_SENSORS_LID_ANGLE
> > +	tristate "ChromeOS EC Sensor for lid angle"
> > +	depends on IIO_CROS_EC_SENSORS_CORE
> > +	help
> > +	  Module to report the angle between lid and base for some
> > +	  convertible devices.
> > +	  This module is loaded when the EC can calculate the angle between the base
> > +	  and the lid.
> > diff --git a/drivers/iio/common/cros_ec_sensors/Makefile b/drivers/iio/common/cros_ec_sensors/Makefile
> > index ec716ff2a775..a35ee232ac07 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Makefile
> > +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> > @@ -4,3 +4,4 @@
> >  
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> > +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> > diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > new file mode 100644
> > index 000000000000..92be07d7fa36
> > --- /dev/null
> > +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > @@ -0,0 +1,138 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +/*
> > + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> > + *
> > + * Copyright 2018 Google, Inc
> > + *
> > + * This driver uses the cros-ec interface to communicate with the Chrome OS
> > + * EC about counter sensors. Counters are presented through
> > + * iio sysfs.
> > + */
> > +
> > +#include <linux/delay.h>
> > +#include <linux/device.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/common/cros_ec_sensors_core.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/kfifo_buf.h>
> > +#include <linux/iio/trigger.h>
> > +#include <linux/iio/triggered_buffer.h>
> > +#include <linux/iio/trigger_consumer.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/cros_ec.h>
> > +#include <linux/mfd/cros_ec_commands.h>
> > +#include <linux/module.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +#define DRV_NAME "cros-ec-lid-angle"
> > +
> > +/*
> > + * One channel for the lid angle, the other for timestamp.
> > + */
> > +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> > +	{
> > +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> > +		.scan_type.realbits = CROS_EC_SENSOR_BITS,
> > +		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
> > +		.scan_type.sign = 'u',
> > +		.type = IIO_ANGL
> > +	},
> > +	IIO_CHAN_SOFT_TIMESTAMP(1)
> > +};
> > +
> > +/* State data for ec_sensors iio driver. */
> > +struct cros_ec_lid_angle_state {
> > +	/* Shared by all sensors */
> > +	struct cros_ec_sensors_core_state core;
> > +};
> > +
> > +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> > +					  unsigned long scan_mask, s16 *data)
> > +{
> > +	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> > +	int ret;
> > +
> > +	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> > +	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> > +	if (ret) {
> > +		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> > +		return ret;
> > +	}
> > +
> > +	*data = st->resp->lid_angle.value;
> > +	return 0;
> > +}
> > +
> > +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> > +				    struct iio_chan_spec const *chan,
> > +				    int *val, int *val2, long mask)
> > +{
> > +	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> > +	s16 data;
> > +	int ret;
> > +
> > +	mutex_lock(&st->core.cmd_lock);
> > +	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> > +	if (ret)
> > +		return ret;
> > +	*val = data;
> > +	mutex_unlock(&st->core.cmd_lock);
> > +	return IIO_VAL_INT;
> > +}
> > +
> > +static const struct iio_info cros_ec_lid_angle_info = {
> > +	.read_raw = &cros_ec_lid_angle_read,
> > +};
> > +
> > +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> > +{
> > +	struct device *dev = &pdev->dev;
> > +	struct iio_dev *indio_dev;
> > +	struct cros_ec_lid_angle_state *state;
> > +	int ret;
> > +
> > +	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> > +	if (!indio_dev)
> > +		return -ENOMEM;
> > +
> > +	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> > +	if (ret)
> > +		return ret;
> > +
> > +	indio_dev->info = &cros_ec_lid_angle_info;
> > +	state = iio_priv(indio_dev);
> > +	indio_dev->channels = cros_ec_lid_angle_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> > +
> > +	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> > +
> > +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> > +			cros_ec_sensors_capture, NULL);
> > +	if (ret)
> > +		return ret;
> > +
> > +	return devm_iio_device_register(dev, indio_dev);
> > +}
> > +
> > +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> > +	{
> > +		.name = DRV_NAME,
> > +	},
> > +	{ /* sentinel */ }
> > +};
> > +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> > +
> > +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> > +	.driver = {
> > +		.name	= DRV_NAME,
> > +		.pm	= &cros_ec_sensors_pm_ops,
> > +	},
> > +	.probe		= cros_ec_lid_angle_probe,
> > +	.id_table	= cros_ec_lid_angle_ids,
> > +};
> > +module_platform_driver(cros_ec_lid_angle_platform_driver);
> > +
> > +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> > +MODULE_LICENSE("GPL v2");
> > diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> > index d275deaecb12..11b5b2fd1f33 100644
> > --- a/drivers/mfd/cros_ec_dev.c
> > +++ b/drivers/mfd/cros_ec_dev.c
> > @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> >  
> >  	resp = (struct ec_response_motion_sense *)msg->data;
> >  	sensor_num = resp->dump.sensor_count;
> > -	/* Allocate 1 extra sensors in FIFO are needed */
> > -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> > +	/*
> > +	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
> > +	 */
> > +#define NUM_EXTRA_SENSORS 2
> > +	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> > +			       sizeof(struct mfd_cell),
> >  			       GFP_KERNEL);
> >  	if (sensor_cells == NULL)
> >  		goto error;
> >  
> > -	sensor_platforms = kcalloc(sensor_num + 1,
> > +	sensor_platforms = kcalloc(sensor_num,
> >  				   sizeof(struct cros_ec_sensor_platform),
> >  				   GFP_KERNEL);
> >  	if (sensor_platforms == NULL)
> > @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
> >  		sensor_cells[id].name = "cros-ec-ring";
> >  		id++;
> >  	}
> > +	if (cros_ec_check_features(ec,
> > +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> > +		sensor_cells[id].name = "cros-ec-lid-angle";
> > +		id++;
> > +	}
> >  
> >  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> >  			      NULL, 0, NULL);
> >   


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

* Re: [PATCH] iio: cros_ec: Add lid angle driver
  2019-02-28  1:50 [PATCH] iio: cros_ec: Add lid angle driver Gwendal Grignou
  2019-02-28 11:18 ` Enric Balletbo i Serra
@ 2019-03-11  8:24 ` Lee Jones
  2019-03-29  8:41   ` [PATCH v4 2/2] " Gwendal Grignou
  1 sibling, 1 reply; 18+ messages in thread
From: Lee Jones @ 2019-03-11  8:24 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: groeck, enric.balletbo, bleung, lars, jic23, egranata, linux-iio

On Wed, 27 Feb 2019, Gwendal Grignou wrote:

> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
> 
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 +
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 164 ++++++++++++++++++

>  drivers/mfd/cros_ec_dev.c                     |  15 +-

It doesn't look like there are build dependencies between the changes
in these different subsystems.  If my assumption is correct, please
pull the MFD changes out into a separate patch.

>  4 files changed, 186 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

[...]

> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..11b5b2fd1f33 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  
>  	resp = (struct ec_response_motion_sense *)msg->data;
>  	sensor_num = resp->dump.sensor_count;
> -	/* Allocate 1 extra sensors in FIFO are needed */
> -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +	/*
> +	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
> +	 */
> +#define NUM_EXTRA_SENSORS 2

Please refrain from placing #defines in the middle of functions.

Move it to the top of the file.

> +	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,

Actually, this isn't something that needs defining.

Just change the comment to:

  "Allocate 2 extra sensors if lid angle sensor or FIFO are needed."

... and drop the define.

> +			       sizeof(struct mfd_cell),
>  			       GFP_KERNEL);
>  	if (sensor_cells == NULL)
>  		goto error;
>  
> -	sensor_platforms = kcalloc(sensor_num + 1,
> +	sensor_platforms = kcalloc(sensor_num,
>  				   sizeof(struct cros_ec_sensor_platform),
>  				   GFP_KERNEL);
>  	if (sensor_platforms == NULL)
> @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct cros_ec_dev *ec)
>  		sensor_cells[id].name = "cros-ec-ring";
>  		id++;
>  	}
> +	if (cros_ec_check_features(ec,
> +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +		sensor_cells[id].name = "cros-ec-lid-angle";
> +		id++;
> +	}
>  
>  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>  			      NULL, 0, NULL);

-- 
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

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

* [PATCH v4 2/2] iio: cros_ec: Add lid angle driver
  2019-03-11  8:24 ` [PATCH] " Lee Jones
@ 2019-03-29  8:41   ` Gwendal Grignou
  2019-03-29 13:44     ` Guenter Roeck
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-29  8:41 UTC (permalink / raw)
  To: enric.balletbo, bleung, groeck, lee.jones, jic23
  Cc: linux-iio, linux-kernel, Gwendal Grignou

Add a IIO driver that reports the angle between the lid and the base for
ChromeOS convertible device.

Tested on eve with ToT EC firmware.
Check driver is loaded and lid angle is correct.

Change-Id: I3506be4a1405f108eebe6c1d3a3dbe975ca00519
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
 drivers/mfd/cros_ec_dev.c                     |  15 +-
 4 files changed, 160 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
b/drivers/iio/common/cros_ec_sensors/Kconfig
index 135f6825903f..aacc2ab9c34f 100644
--- a/drivers/iio/common/cros_ec_sensors/Kconfig
+++ b/drivers/iio/common/cros_ec_sensors/Kconfig
@@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
 	  Accelerometers, Gyroscope and Magnetometer that are
 	  presented by the ChromeOS EC Sensor hub.
 	  Creates an IIO device for each functions.
+
+config IIO_CROS_EC_SENSORS_LID_ANGLE
+	tristate "ChromeOS EC Sensor for lid angle"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to report the angle between lid and base for some
+	  convertible devices.
+	  This module is loaded when the EC can calculate the angle between the base
+	  and the lid.
diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
b/drivers/iio/common/cros_ec_sensors/Makefile
index ec716ff2a775..a35ee232ac07 100644
--- a/drivers/iio/common/cros_ec_sensors/Makefile
+++ b/drivers/iio/common/cros_ec_sensors/Makefile
@@ -4,3 +4,4 @@

 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
new file mode 100644
index 000000000000..92be07d7fa36
--- /dev/null
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
+ *
+ * Copyright 2018 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS
+ * EC about counter sensors. Counters are presented through
+ * iio sysfs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#define DRV_NAME "cros-ec-lid-angle"
+
+/*
+ * One channel for the lid angle, the other for timestamp.
+ */
+static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
+	{
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_type.realbits = CROS_EC_SENSOR_BITS,
+		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
+		.scan_type.sign = 'u',
+		.type = IIO_ANGL
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+/* State data for ec_sensors iio driver. */
+struct cros_ec_lid_angle_state {
+	/* Shared by all sensors */
+	struct cros_ec_sensors_core_state core;
+};
+
+static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
+					  unsigned long scan_mask, s16 *data)
+{
+	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
+	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
+	if (ret) {
+		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
+		return ret;
+	}
+
+	*data = st->resp->lid_angle.value;
+	return 0;
+}
+
+static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan,
+				    int *val, int *val2, long mask)
+{
+	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
+	s16 data;
+	int ret;
+
+	mutex_lock(&st->core.cmd_lock);
+	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
+	if (ret)
+		return ret;
+	*val = data;
+	mutex_unlock(&st->core.cmd_lock);
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info cros_ec_lid_angle_info = {
+	.read_raw = &cros_ec_lid_angle_read,
+};
+
+static int cros_ec_lid_angle_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct cros_ec_lid_angle_state *state;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &cros_ec_lid_angle_info;
+	state = iio_priv(indio_dev);
+	indio_dev->channels = cros_ec_lid_angle_channels;
+	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
+
+	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+			cros_ec_sensors_capture, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct platform_device_id cros_ec_lid_angle_ids[] = {
+	{
+		.name = DRV_NAME,
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
+
+static struct platform_driver cros_ec_lid_angle_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+		.pm	= &cros_ec_sensors_pm_ops,
+	},
+	.probe		= cros_ec_lid_angle_probe,
+	.id_table	= cros_ec_lid_angle_ids,
+};
+module_platform_driver(cros_ec_lid_angle_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index d275deaecb12..11b5b2fd1f33 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct
cros_ec_dev *ec)

 	resp = (struct ec_response_motion_sense *)msg->data;
 	sensor_num = resp->dump.sensor_count;
-	/* Allocate 1 extra sensors in FIFO are needed */
-	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
+	/*
+	 * Allocate extra sensors if lid angle sensor or FIFO are needed.
+	 */
+#define NUM_EXTRA_SENSORS 2
+	sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
+			       sizeof(struct mfd_cell),
 			       GFP_KERNEL);
 	if (sensor_cells == NULL)
 		goto error;

-	sensor_platforms = kcalloc(sensor_num + 1,
+	sensor_platforms = kcalloc(sensor_num,
 				   sizeof(struct cros_ec_sensor_platform),
 				   GFP_KERNEL);
 	if (sensor_platforms == NULL)
@@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct
cros_ec_dev *ec)
 		sensor_cells[id].name = "cros-ec-ring";
 		id++;
 	}
+	if (cros_ec_check_features(ec,
+				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
+		sensor_cells[id].name = "cros-ec-lid-angle";
+		id++;
+	}

 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
 			      NULL, 0, NULL);
-- 
2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v4 2/2] iio: cros_ec: Add lid angle driver
  2019-03-29  8:41   ` [PATCH v4 2/2] " Gwendal Grignou
@ 2019-03-29 13:44     ` Guenter Roeck
  2019-03-29 17:40       ` Gwendal Grignou
  0 siblings, 1 reply; 18+ messages in thread
From: Guenter Roeck @ 2019-03-29 13:44 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: Enric Balletbo i Serra, Benson Leung, Guenter Roeck, Lee Jones,
	Jonathan Cameron, linux-iio, linux-kernel

On Fri, Mar 29, 2019 at 1:41 AM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
>
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
>
> Change-Id: I3506be4a1405f108eebe6c1d3a3dbe975ca00519

drop. checkpatch.pl should report that.

> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  15 +-
>  4 files changed, 160 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
>
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
> b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>           Accelerometers, Gyroscope and Magnetometer that are
>           presented by the ChromeOS EC Sensor hub.
>           Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +       tristate "ChromeOS EC Sensor for lid angle"
> +       depends on IIO_CROS_EC_SENSORS_CORE
> +       help
> +         Module to report the angle between lid and base for some
> +         convertible devices.
> +         This module is loaded when the EC can calculate the angle between the base
> +         and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
> b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
>
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..92be07d7fa36
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,138 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> +       {
> +               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> +               .scan_type.realbits = CROS_EC_SENSOR_BITS,
> +               .scan_type.storagebits = CROS_EC_SENSOR_BITS,
> +               .scan_type.sign = 'u',
> +               .type = IIO_ANGL
> +       },
> +       IIO_CHAN_SOFT_TIMESTAMP(1)
> +};
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +       /* Shared by all sensors */
> +       struct cros_ec_sensors_core_state core;
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +                                         unsigned long scan_mask, s16 *data)
> +{
> +       struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +       int ret;
> +
> +       st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +       ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +       if (ret) {
> +               dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +               return ret;
> +       }
> +
> +       *data = st->resp->lid_angle.value;
> +       return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +                                   struct iio_chan_spec const *chan,
> +                                   int *val, int *val2, long mask)
> +{
> +       struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +       s16 data;
> +       int ret;
> +
> +       mutex_lock(&st->core.cmd_lock);
> +       ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> +       if (ret)
> +               return ret;
> +       *val = data;
> +       mutex_unlock(&st->core.cmd_lock);
> +       return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +       .read_raw = &cros_ec_lid_angle_read,
> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +       struct device *dev = &pdev->dev;
> +       struct iio_dev *indio_dev;
> +       struct cros_ec_lid_angle_state *state;
> +       int ret;
> +
> +       indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +       if (ret)
> +               return ret;
> +
> +       indio_dev->info = &cros_ec_lid_angle_info;
> +       state = iio_priv(indio_dev);
> +       indio_dev->channels = cros_ec_lid_angle_channels;
> +       indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> +
> +       state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +                       cros_ec_sensors_capture, NULL);
> +       if (ret)
> +               return ret;
> +
> +       return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +       {
> +               .name = DRV_NAME,
> +       },
> +       { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +       .driver = {
> +               .name   = DRV_NAME,
> +               .pm     = &cros_ec_sensors_pm_ops,
> +       },
> +       .probe          = cros_ec_lid_angle_probe,
> +       .id_table       = cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..11b5b2fd1f33 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
>
>         resp = (struct ec_response_motion_sense *)msg->data;
>         sensor_num = resp->dump.sensor_count;
> -       /* Allocate 1 extra sensors in FIFO are needed */
> -       sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +       /*
> +        * Allocate extra sensors if lid angle sensor or FIFO are needed.
> +        */
> +#define NUM_EXTRA_SENSORS 2
> +       sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> +                              sizeof(struct mfd_cell),
>                                GFP_KERNEL);
>         if (sensor_cells == NULL)
>                 goto error;
>
> -       sensor_platforms = kcalloc(sensor_num + 1,
> +       sensor_platforms = kcalloc(sensor_num,

Are you sure about this, or did "+ NUM_EXTRA_SENSORS" get lost ?

>                                    sizeof(struct cros_ec_sensor_platform),
>                                    GFP_KERNEL);
>         if (sensor_platforms == NULL)
> @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
>                 sensor_cells[id].name = "cros-ec-ring";
>                 id++;
>         }
> +       if (cros_ec_check_features(ec,
> +                               EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +               sensor_cells[id].name = "cros-ec-lid-angle";
> +               id++;
> +       }
>
>         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>                               NULL, 0, NULL);
> --
> 2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v4 2/2] iio: cros_ec: Add lid angle driver
  2019-03-29 13:44     ` Guenter Roeck
@ 2019-03-29 17:40       ` Gwendal Grignou
  2019-03-29 20:08         ` [PATCH v5] " Gwendal Grignou
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-29 17:40 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Enric Balletbo i Serra, Benson Leung, Guenter Roeck, Lee Jones,
	Jonathan Cameron, linux-iio, linux-kernel

On Fri, Mar 29, 2019 at 6:44 AM Guenter Roeck <groeck@google.com> wrote:
>
> On Fri, Mar 29, 2019 at 1:41 AM Gwendal Grignou <gwendal@chromium.org> wrote:
> >
> > Add a IIO driver that reports the angle between the lid and the base for
> > ChromeOS convertible device.
> >
> > Tested on eve with ToT EC firmware.
> > Check driver is loaded and lid angle is correct.
> >
> > Change-Id: I3506be4a1405f108eebe6c1d3a3dbe975ca00519
>
> drop. checkpatch.pl should report that.
>
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> > ---
> >  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
> >  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
> >  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
> >  drivers/mfd/cros_ec_dev.c                     |  15 +-
> >  4 files changed, 160 insertions(+), 3 deletions(-)
> >  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> >
> > diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
> > b/drivers/iio/common/cros_ec_sensors/Kconfig
> > index 135f6825903f..aacc2ab9c34f 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> > +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> > @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
> >           Accelerometers, Gyroscope and Magnetometer that are
> >           presented by the ChromeOS EC Sensor hub.
> >           Creates an IIO device for each functions.
> > +
> > +config IIO_CROS_EC_SENSORS_LID_ANGLE
> > +       tristate "ChromeOS EC Sensor for lid angle"
> > +       depends on IIO_CROS_EC_SENSORS_CORE
> > +       help
> > +         Module to report the angle between lid and base for some
> > +         convertible devices.
> > +         This module is loaded when the EC can calculate the angle between the base
> > +         and the lid.
> > diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
> > b/drivers/iio/common/cros_ec_sensors/Makefile
> > index ec716ff2a775..a35ee232ac07 100644
> > --- a/drivers/iio/common/cros_ec_sensors/Makefile
> > +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> > @@ -4,3 +4,4 @@
> >
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
> >  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> > +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> > diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > new file mode 100644
> > index 000000000000..92be07d7fa36
> > --- /dev/null
> > +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> > @@ -0,0 +1,138 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +/*
> > + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> > + *
> > + * Copyright 2018 Google, Inc
> > + *
> > + * This driver uses the cros-ec interface to communicate with the Chrome OS
> > + * EC about counter sensors. Counters are presented through
> > + * iio sysfs.
> > + */
> > +
> > +#include <linux/delay.h>
> > +#include <linux/device.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/common/cros_ec_sensors_core.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/kfifo_buf.h>
> > +#include <linux/iio/trigger.h>
> > +#include <linux/iio/triggered_buffer.h>
> > +#include <linux/iio/trigger_consumer.h>
> > +#include <linux/kernel.h>
> > +#include <linux/mfd/cros_ec.h>
> > +#include <linux/mfd/cros_ec_commands.h>
> > +#include <linux/module.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/slab.h>
> > +
> > +#define DRV_NAME "cros-ec-lid-angle"
> > +
> > +/*
> > + * One channel for the lid angle, the other for timestamp.
> > + */
> > +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> > +       {
> > +               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> > +               .scan_type.realbits = CROS_EC_SENSOR_BITS,
> > +               .scan_type.storagebits = CROS_EC_SENSOR_BITS,
> > +               .scan_type.sign = 'u',
> > +               .type = IIO_ANGL
> > +       },
> > +       IIO_CHAN_SOFT_TIMESTAMP(1)
> > +};
> > +
> > +/* State data for ec_sensors iio driver. */
> > +struct cros_ec_lid_angle_state {
> > +       /* Shared by all sensors */
> > +       struct cros_ec_sensors_core_state core;
> > +};
> > +
> > +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> > +                                         unsigned long scan_mask, s16 *data)
> > +{
> > +       struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> > +       int ret;
> > +
> > +       st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> > +       ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> > +       if (ret) {
> > +               dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> > +               return ret;
> > +       }
> > +
> > +       *data = st->resp->lid_angle.value;
> > +       return 0;
> > +}
> > +
> > +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> > +                                   struct iio_chan_spec const *chan,
> > +                                   int *val, int *val2, long mask)
> > +{
> > +       struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> > +       s16 data;
> > +       int ret;
> > +
> > +       mutex_lock(&st->core.cmd_lock);
> > +       ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> > +       if (ret)
> > +               return ret;
> > +       *val = data;
> > +       mutex_unlock(&st->core.cmd_lock);
> > +       return IIO_VAL_INT;
> > +}
> > +
> > +static const struct iio_info cros_ec_lid_angle_info = {
> > +       .read_raw = &cros_ec_lid_angle_read,
> > +};
> > +
> > +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> > +{
> > +       struct device *dev = &pdev->dev;
> > +       struct iio_dev *indio_dev;
> > +       struct cros_ec_lid_angle_state *state;
> > +       int ret;
> > +
> > +       indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> > +       if (!indio_dev)
> > +               return -ENOMEM;
> > +
> > +       ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> > +       if (ret)
> > +               return ret;
> > +
> > +       indio_dev->info = &cros_ec_lid_angle_info;
> > +       state = iio_priv(indio_dev);
> > +       indio_dev->channels = cros_ec_lid_angle_channels;
> > +       indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> > +
> > +       state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> > +
> > +       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> > +                       cros_ec_sensors_capture, NULL);
> > +       if (ret)
> > +               return ret;
> > +
> > +       return devm_iio_device_register(dev, indio_dev);
> > +}
> > +
> > +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> > +       {
> > +               .name = DRV_NAME,
> > +       },
> > +       { /* sentinel */ }
> > +};
> > +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> > +
> > +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> > +       .driver = {
> > +               .name   = DRV_NAME,
> > +               .pm     = &cros_ec_sensors_pm_ops,
> > +       },
> > +       .probe          = cros_ec_lid_angle_probe,
> > +       .id_table       = cros_ec_lid_angle_ids,
> > +};
> > +module_platform_driver(cros_ec_lid_angle_platform_driver);
> > +
> > +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> > +MODULE_LICENSE("GPL v2");
> > diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> > index d275deaecb12..11b5b2fd1f33 100644
> > --- a/drivers/mfd/cros_ec_dev.c
> > +++ b/drivers/mfd/cros_ec_dev.c
> > @@ -297,13 +297,17 @@ static void cros_ec_sensors_register(struct
> > cros_ec_dev *ec)
> >
> >         resp = (struct ec_response_motion_sense *)msg->data;
> >         sensor_num = resp->dump.sensor_count;
> > -       /* Allocate 1 extra sensors in FIFO are needed */
> > -       sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> > +       /*
> > +        * Allocate extra sensors if lid angle sensor or FIFO are needed.
> > +        */
> > +#define NUM_EXTRA_SENSORS 2
> > +       sensor_cells = kcalloc(sensor_num + NUM_EXTRA_SENSORS,
> > +                              sizeof(struct mfd_cell),
> >                                GFP_KERNEL);
> >         if (sensor_cells == NULL)
> >                 goto error;
> >
> > -       sensor_platforms = kcalloc(sensor_num + 1,
> > +       sensor_platforms = kcalloc(sensor_num,
>
> Are you sure about this, or did "+ NUM_EXTRA_SENSORS" get lost ?
You're right, that's not the patch I intended to send. Resending.
>
> >                                    sizeof(struct cros_ec_sensor_platform),
> >                                    GFP_KERNEL);
> >         if (sensor_platforms == NULL)
> > @@ -363,6 +367,11 @@ static void cros_ec_sensors_register(struct
> > cros_ec_dev *ec)
> >                 sensor_cells[id].name = "cros-ec-ring";
> >                 id++;
> >         }
> > +       if (cros_ec_check_features(ec,
> > +                               EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> > +               sensor_cells[id].name = "cros-ec-lid-angle";
> > +               id++;
> > +       }
> >
> >         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> >                               NULL, 0, NULL);
> > --
> > 2.21.0.360.g471c308f928-goog

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

* [PATCH v5] iio: cros_ec: Add lid angle driver
  2019-03-29 17:40       ` Gwendal Grignou
@ 2019-03-29 20:08         ` Gwendal Grignou
  2019-03-29 20:17           ` Guenter Roeck
  2019-03-31 10:28           ` Jonathan Cameron
  0 siblings, 2 replies; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-29 20:08 UTC (permalink / raw)
  To: enric.balletbo, bleung, groeck, lee.jones, jic23
  Cc: linux-iio, linux-kernel, Gwendal Grignou

Add a IIO driver that reports the angle between the lid and the base for
ChromeOS convertible device.

Tested on eve with ToT EC firmware.
Check driver is loaded and lid angle is correct.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
Changes in v5:
- Remove unnecessary define.
- v4 was the wrong patch file

Changes in v3:
- Use static channel array, simplify code because index is always 0.

Changes in v2:
- Fix license, remove driver_module field.

 drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
 drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
 .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
 drivers/mfd/cros_ec_dev.c                     |  13 +-
 4 files changed, 158 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c

diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
b/drivers/iio/common/cros_ec_sensors/Kconfig
index 135f6825903f..aacc2ab9c34f 100644
--- a/drivers/iio/common/cros_ec_sensors/Kconfig
+++ b/drivers/iio/common/cros_ec_sensors/Kconfig
@@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
 	  Accelerometers, Gyroscope and Magnetometer that are
 	  presented by the ChromeOS EC Sensor hub.
 	  Creates an IIO device for each functions.
+
+config IIO_CROS_EC_SENSORS_LID_ANGLE
+	tristate "ChromeOS EC Sensor for lid angle"
+	depends on IIO_CROS_EC_SENSORS_CORE
+	help
+	  Module to report the angle between lid and base for some
+	  convertible devices.
+	  This module is loaded when the EC can calculate the angle between the base
+	  and the lid.
diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
b/drivers/iio/common/cros_ec_sensors/Makefile
index ec716ff2a775..a35ee232ac07 100644
--- a/drivers/iio/common/cros_ec_sensors/Makefile
+++ b/drivers/iio/common/cros_ec_sensors/Makefile
@@ -4,3 +4,4 @@

 obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
 obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
new file mode 100644
index 000000000000..92be07d7fa36
--- /dev/null
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
+ *
+ * Copyright 2018 Google, Inc
+ *
+ * This driver uses the cros-ec interface to communicate with the Chrome OS
+ * EC about counter sensors. Counters are presented through
+ * iio sysfs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/common/cros_ec_sensors_core.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/kernel.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#define DRV_NAME "cros-ec-lid-angle"
+
+/*
+ * One channel for the lid angle, the other for timestamp.
+ */
+static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
+	{
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_type.realbits = CROS_EC_SENSOR_BITS,
+		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
+		.scan_type.sign = 'u',
+		.type = IIO_ANGL
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+/* State data for ec_sensors iio driver. */
+struct cros_ec_lid_angle_state {
+	/* Shared by all sensors */
+	struct cros_ec_sensors_core_state core;
+};
+
+static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
+					  unsigned long scan_mask, s16 *data)
+{
+	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
+	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
+	if (ret) {
+		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
+		return ret;
+	}
+
+	*data = st->resp->lid_angle.value;
+	return 0;
+}
+
+static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan,
+				    int *val, int *val2, long mask)
+{
+	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
+	s16 data;
+	int ret;
+
+	mutex_lock(&st->core.cmd_lock);
+	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
+	if (ret)
+		return ret;
+	*val = data;
+	mutex_unlock(&st->core.cmd_lock);
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info cros_ec_lid_angle_info = {
+	.read_raw = &cros_ec_lid_angle_read,
+};
+
+static int cros_ec_lid_angle_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct cros_ec_lid_angle_state *state;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
+	if (ret)
+		return ret;
+
+	indio_dev->info = &cros_ec_lid_angle_info;
+	state = iio_priv(indio_dev);
+	indio_dev->channels = cros_ec_lid_angle_channels;
+	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
+
+	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+			cros_ec_sensors_capture, NULL);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct platform_device_id cros_ec_lid_angle_ids[] = {
+	{
+		.name = DRV_NAME,
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
+
+static struct platform_driver cros_ec_lid_angle_platform_driver = {
+	.driver = {
+		.name	= DRV_NAME,
+		.pm	= &cros_ec_sensors_pm_ops,
+	},
+	.probe		= cros_ec_lid_angle_probe,
+	.id_table	= cros_ec_lid_angle_ids,
+};
+module_platform_driver(cros_ec_lid_angle_platform_driver);
+
+MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index d275deaecb12..52ea2f1c87a1 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -297,13 +297,15 @@ static void cros_ec_sensors_register(struct
cros_ec_dev *ec)

 	resp = (struct ec_response_motion_sense *)msg->data;
 	sensor_num = resp->dump.sensor_count;
-	/* Allocate 1 extra sensors in FIFO are needed */
-	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
+	/*
+	 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
+	 */
+	sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
 			       GFP_KERNEL);
 	if (sensor_cells == NULL)
 		goto error;

-	sensor_platforms = kcalloc(sensor_num + 1,
+	sensor_platforms = kcalloc(sensor_num,
 				   sizeof(struct cros_ec_sensor_platform),
 				   GFP_KERNEL);
 	if (sensor_platforms == NULL)
@@ -363,6 +365,11 @@ static void cros_ec_sensors_register(struct
cros_ec_dev *ec)
 		sensor_cells[id].name = "cros-ec-ring";
 		id++;
 	}
+	if (cros_ec_check_features(ec,
+				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
+		sensor_cells[id].name = "cros-ec-lid-angle";
+		id++;
+	}

 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
 			      NULL, 0, NULL);
-- 
2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v5] iio: cros_ec: Add lid angle driver
  2019-03-29 20:08         ` [PATCH v5] " Gwendal Grignou
@ 2019-03-29 20:17           ` Guenter Roeck
  2019-03-29 22:37             ` Gwendal Grignou
  2019-03-31 10:28           ` Jonathan Cameron
  1 sibling, 1 reply; 18+ messages in thread
From: Guenter Roeck @ 2019-03-29 20:17 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: Enric Balletbo i Serra, Benson Leung, Guenter Roeck, Lee Jones,
	Jonathan Cameron, linux-iio, linux-kernel

On Fri, Mar 29, 2019 at 1:08 PM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
>
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
>
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> ---
> Changes in v5:
> - Remove unnecessary define.
> - v4 was the wrong patch file
>
> Changes in v3:
> - Use static channel array, simplify code because index is always 0.
>
> Changes in v2:
> - Fix license, remove driver_module field.
>
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  13 +-
>  4 files changed, 158 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
>
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
> b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>           Accelerometers, Gyroscope and Magnetometer that are
>           presented by the ChromeOS EC Sensor hub.
>           Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +       tristate "ChromeOS EC Sensor for lid angle"
> +       depends on IIO_CROS_EC_SENSORS_CORE
> +       help
> +         Module to report the angle between lid and base for some
> +         convertible devices.
> +         This module is loaded when the EC can calculate the angle between the base
> +         and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
> b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
>
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..92be07d7fa36
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,138 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> +       {
> +               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> +               .scan_type.realbits = CROS_EC_SENSOR_BITS,
> +               .scan_type.storagebits = CROS_EC_SENSOR_BITS,
> +               .scan_type.sign = 'u',
> +               .type = IIO_ANGL
> +       },
> +       IIO_CHAN_SOFT_TIMESTAMP(1)
> +};
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +       /* Shared by all sensors */
> +       struct cros_ec_sensors_core_state core;
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +                                         unsigned long scan_mask, s16 *data)
> +{
> +       struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +       int ret;
> +
> +       st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +       ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +       if (ret) {
> +               dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +               return ret;
> +       }
> +
> +       *data = st->resp->lid_angle.value;
> +       return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +                                   struct iio_chan_spec const *chan,
> +                                   int *val, int *val2, long mask)
> +{
> +       struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +       s16 data;
> +       int ret;
> +
> +       mutex_lock(&st->core.cmd_lock);
> +       ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> +       if (ret)
> +               return ret;
> +       *val = data;
> +       mutex_unlock(&st->core.cmd_lock);
> +       return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +       .read_raw = &cros_ec_lid_angle_read,
> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +       struct device *dev = &pdev->dev;
> +       struct iio_dev *indio_dev;
> +       struct cros_ec_lid_angle_state *state;
> +       int ret;
> +
> +       indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +       if (ret)
> +               return ret;
> +
> +       indio_dev->info = &cros_ec_lid_angle_info;
> +       state = iio_priv(indio_dev);
> +       indio_dev->channels = cros_ec_lid_angle_channels;
> +       indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> +
> +       state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +                       cros_ec_sensors_capture, NULL);
> +       if (ret)
> +               return ret;
> +
> +       return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +       {
> +               .name = DRV_NAME,
> +       },
> +       { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +       .driver = {
> +               .name   = DRV_NAME,
> +               .pm     = &cros_ec_sensors_pm_ops,
> +       },
> +       .probe          = cros_ec_lid_angle_probe,
> +       .id_table       = cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..52ea2f1c87a1 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,15 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
>
>         resp = (struct ec_response_motion_sense *)msg->data;
>         sensor_num = resp->dump.sensor_count;
> -       /* Allocate 1 extra sensors in FIFO are needed */
> -       sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +       /*
> +        * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
> +        */
> +       sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
>                                GFP_KERNEL);
>         if (sensor_cells == NULL)
>                 goto error;
>
> -       sensor_platforms = kcalloc(sensor_num + 1,
> +       sensor_platforms = kcalloc(sensor_num,

I still don't understand why the "+ 1" is no longer needed. It seems
to me that it would have to change to "+ 2".

Guenter

>                                    sizeof(struct cros_ec_sensor_platform),
>                                    GFP_KERNEL);
>         if (sensor_platforms == NULL)
> @@ -363,6 +365,11 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
>                 sensor_cells[id].name = "cros-ec-ring";
>                 id++;
>         }
> +       if (cros_ec_check_features(ec,
> +                               EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +               sensor_cells[id].name = "cros-ec-lid-angle";
> +               id++;
> +       }
>
>         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>                               NULL, 0, NULL);
> --
> 2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v5] iio: cros_ec: Add lid angle driver
  2019-03-29 20:17           ` Guenter Roeck
@ 2019-03-29 22:37             ` Gwendal Grignou
  2019-03-29 23:38               ` Guenter Roeck
  0 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2019-03-29 22:37 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Enric Balletbo i Serra, Benson Leung, Guenter Roeck, Lee Jones,
	Jonathan Cameron, linux-iio, linux-kernel

On Fri, Mar 29, 2019 at 1:17 PM Guenter Roeck <groeck@google.com> wrote:
>
> On Fri, Mar 29, 2019 at 1:08 PM Gwendal Grignou <gwendal@chromium.org> wrote:
> >
> > Add a IIO driver that reports the angle between the lid and the base for
> > ChromeOS convertible device.
...
> > --- a/drivers/mfd/cros_ec_dev.c
> > +++ b/drivers/mfd/cros_ec_dev.c
> > @@ -297,13 +297,15 @@ static void cros_ec_sensors_register(struct
> > cros_ec_dev *ec)
> >
> >         resp = (struct ec_response_motion_sense *)msg->data;
> >         sensor_num = resp->dump.sensor_count;
> > -       /* Allocate 1 extra sensors in FIFO are needed */
> > -       sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> > +       /*
> > +        * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
> > +        */
> > +       sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
> >                                GFP_KERNEL);
> >         if (sensor_cells == NULL)
> >                 goto error;
> >
> > -       sensor_platforms = kcalloc(sensor_num + 1,
> > +       sensor_platforms = kcalloc(sensor_num,
>
> I still don't understand why the "+ 1" is no longer needed. It seems
> to me that it would have to change to "+ 2".
"+1" was never needed to begin with FIFO device will never use a
struct cros_ec_sensor_platform. I mentioned it earlier:
https://www.spinics.net/lists/linux-iio/msg43157.html

Gwendal.
>
> Guenter
>
> >                                    sizeof(struct cros_ec_sensor_platform),
> >                                    GFP_KERNEL);
> >         if (sensor_platforms == NULL)
> > @@ -363,6 +365,11 @@ static void cros_ec_sensors_register(struct
> > cros_ec_dev *ec)
> >                 sensor_cells[id].name = "cros-ec-ring";
> >                 id++;
> >         }
> > +       if (cros_ec_check_features(ec,
> > +                               EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> > +               sensor_cells[id].name = "cros-ec-lid-angle";
> > +               id++;
> > +       }
> >
> >         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> >                               NULL, 0, NULL);
> > --
> > 2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v5] iio: cros_ec: Add lid angle driver
  2019-03-29 22:37             ` Gwendal Grignou
@ 2019-03-29 23:38               ` Guenter Roeck
  0 siblings, 0 replies; 18+ messages in thread
From: Guenter Roeck @ 2019-03-29 23:38 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: Enric Balletbo i Serra, Benson Leung, Guenter Roeck, Lee Jones,
	Jonathan Cameron, linux-iio, linux-kernel

On Fri, Mar 29, 2019 at 3:37 PM Gwendal Grignou <gwendal@chromium.org> wrote:
>
> On Fri, Mar 29, 2019 at 1:17 PM Guenter Roeck <groeck@google.com> wrote:
> >
> > On Fri, Mar 29, 2019 at 1:08 PM Gwendal Grignou <gwendal@chromium.org> wrote:
> > >
> > > Add a IIO driver that reports the angle between the lid and the base for
> > > ChromeOS convertible device.
> ...
> > > --- a/drivers/mfd/cros_ec_dev.c
> > > +++ b/drivers/mfd/cros_ec_dev.c
> > > @@ -297,13 +297,15 @@ static void cros_ec_sensors_register(struct
> > > cros_ec_dev *ec)
> > >
> > >         resp = (struct ec_response_motion_sense *)msg->data;
> > >         sensor_num = resp->dump.sensor_count;
> > > -       /* Allocate 1 extra sensors in FIFO are needed */
> > > -       sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> > > +       /*
> > > +        * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
> > > +        */
> > > +       sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
> > >                                GFP_KERNEL);
> > >         if (sensor_cells == NULL)
> > >                 goto error;
> > >
> > > -       sensor_platforms = kcalloc(sensor_num + 1,
> > > +       sensor_platforms = kcalloc(sensor_num,
> >
> > I still don't understand why the "+ 1" is no longer needed. It seems
> > to me that it would have to change to "+ 2".
> "+1" was never needed to begin with FIFO device will never use a
> struct cros_ec_sensor_platform. I mentioned it earlier:
> https://www.spinics.net/lists/linux-iio/msg43157.html
>
Ah yes, now I see. Thanks, and sorry for the noise.

Guenter

> Gwendal.
> >
> > Guenter
> >
> > >                                    sizeof(struct cros_ec_sensor_platform),
> > >                                    GFP_KERNEL);
> > >         if (sensor_platforms == NULL)
> > > @@ -363,6 +365,11 @@ static void cros_ec_sensors_register(struct
> > > cros_ec_dev *ec)
> > >                 sensor_cells[id].name = "cros-ec-ring";
> > >                 id++;
> > >         }
> > > +       if (cros_ec_check_features(ec,
> > > +                               EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> > > +               sensor_cells[id].name = "cros-ec-lid-angle";
> > > +               id++;
> > > +       }
> > >
> > >         ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
> > >                               NULL, 0, NULL);
> > > --
> > > 2.21.0.360.g471c308f928-goog

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

* Re: [PATCH v5] iio: cros_ec: Add lid angle driver
  2019-03-29 20:08         ` [PATCH v5] " Gwendal Grignou
  2019-03-29 20:17           ` Guenter Roeck
@ 2019-03-31 10:28           ` Jonathan Cameron
  1 sibling, 0 replies; 18+ messages in thread
From: Jonathan Cameron @ 2019-03-31 10:28 UTC (permalink / raw)
  To: Gwendal Grignou
  Cc: enric.balletbo, bleung, groeck, lee.jones, linux-iio, linux-kernel

On Fri, 29 Mar 2019 13:08:52 -0700
Gwendal Grignou <gwendal@chromium.org> wrote:

> Add a IIO driver that reports the angle between the lid and the base for
> ChromeOS convertible device.
> 
> Tested on eve with ToT EC firmware.
> Check driver is loaded and lid angle is correct.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Hi Gwendal.

I'd really prefer if you didn't send new versions as replies to the older thread.
It just leads to very deep threads.  It's easy to associate a v5 with v4 thread
if anyone wants to, but it's hard to figure out that an email deep in a v4 thread
is infact about v5.

Looks like there is a lock held in an error path.  See inline.

Also, somewhere along the way your patch got corrupted by a few unwanted
line breaks.

Given this touches code under mfd, I'll be wanting an ack from Lee before
applying + whether he wants an immutable branch for this change.

Thanks,


Jonathan


> ---
> Changes in v5:
> - Remove unnecessary define.
> - v4 was the wrong patch file
> 
> Changes in v3:
> - Use static channel array, simplify code because index is always 0.
> 
> Changes in v2:
> - Fix license, remove driver_module field.
> 
>  drivers/iio/common/cros_ec_sensors/Kconfig    |   9 ++
>  drivers/iio/common/cros_ec_sensors/Makefile   |   1 +
>  .../cros_ec_sensors/cros_ec_lid_angle.c       | 138 ++++++++++++++++++
>  drivers/mfd/cros_ec_dev.c                     |  13 +-
>  4 files changed, 158 insertions(+), 3 deletions(-)
>  create mode 100644 drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> 
> diff --git a/drivers/iio/common/cros_ec_sensors/Kconfig
> b/drivers/iio/common/cros_ec_sensors/Kconfig
> index 135f6825903f..aacc2ab9c34f 100644
> --- a/drivers/iio/common/cros_ec_sensors/Kconfig
> +++ b/drivers/iio/common/cros_ec_sensors/Kconfig
> @@ -20,3 +20,12 @@ config IIO_CROS_EC_SENSORS
>  	  Accelerometers, Gyroscope and Magnetometer that are
>  	  presented by the ChromeOS EC Sensor hub.
>  	  Creates an IIO device for each functions.
> +
> +config IIO_CROS_EC_SENSORS_LID_ANGLE
> +	tristate "ChromeOS EC Sensor for lid angle"
> +	depends on IIO_CROS_EC_SENSORS_CORE
> +	help
> +	  Module to report the angle between lid and base for some
> +	  convertible devices.
> +	  This module is loaded when the EC can calculate the angle between the base
> +	  and the lid.
> diff --git a/drivers/iio/common/cros_ec_sensors/Makefile
> b/drivers/iio/common/cros_ec_sensors/Makefile
> index ec716ff2a775..a35ee232ac07 100644
> --- a/drivers/iio/common/cros_ec_sensors/Makefile
> +++ b/drivers/iio/common/cros_ec_sensors/Makefile
> @@ -4,3 +4,4 @@
> 
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o
>  obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
> +obj-$(CONFIG_IIO_CROS_EC_SENSORS_LID_ANGLE) += cros_ec_lid_angle.o
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> new file mode 100644
> index 000000000000..92be07d7fa36
> --- /dev/null
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_lid_angle.c
> @@ -0,0 +1,138 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
> + *
> + * Copyright 2018 Google, Inc
> + *
> + * This driver uses the cros-ec interface to communicate with the Chrome OS
> + * EC about counter sensors. Counters are presented through
> + * iio sysfs.
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/common/cros_ec_sensors_core.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/kfifo_buf.h>
> +#include <linux/iio/trigger.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/kernel.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define DRV_NAME "cros-ec-lid-angle"
> +
> +/*
> + * One channel for the lid angle, the other for timestamp.
> + */
> +static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
> +	{
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> +		.scan_type.realbits = CROS_EC_SENSOR_BITS,
> +		.scan_type.storagebits = CROS_EC_SENSOR_BITS,
> +		.scan_type.sign = 'u',
> +		.type = IIO_ANGL
> +	},
> +	IIO_CHAN_SOFT_TIMESTAMP(1)
> +};
> +
> +/* State data for ec_sensors iio driver. */
> +struct cros_ec_lid_angle_state {
> +	/* Shared by all sensors */
> +	struct cros_ec_sensors_core_state core;
> +};
> +
> +static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
> +					  unsigned long scan_mask, s16 *data)
> +{
> +	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
> +	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
> +	if (ret) {
> +		dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
> +		return ret;
> +	}
> +
> +	*data = st->resp->lid_angle.value;
> +	return 0;
> +}
> +
> +static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
> +				    struct iio_chan_spec const *chan,
> +				    int *val, int *val2, long mask)
> +{
> +	struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
> +	s16 data;
> +	int ret;
> +
> +	mutex_lock(&st->core.cmd_lock);
> +	ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
> +	if (ret)

Lock is still held.

> +		return ret;
> +	*val = data;
> +	mutex_unlock(&st->core.cmd_lock);
> +	return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info cros_ec_lid_angle_info = {
> +	.read_raw = &cros_ec_lid_angle_read,
> +};
> +
> +static int cros_ec_lid_angle_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct cros_ec_lid_angle_state *state;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	ret = cros_ec_sensors_core_init(pdev, indio_dev, false);
> +	if (ret)
> +		return ret;
> +
> +	indio_dev->info = &cros_ec_lid_angle_info;
> +	state = iio_priv(indio_dev);
> +	indio_dev->channels = cros_ec_lid_angle_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
> +
> +	state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
> +
> +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
> +			cros_ec_sensors_capture, NULL);
> +	if (ret)
> +		return ret;
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct platform_device_id cros_ec_lid_angle_ids[] = {
> +	{
> +		.name = DRV_NAME,
> +	},
> +	{ /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
> +
> +static struct platform_driver cros_ec_lid_angle_platform_driver = {
> +	.driver = {
> +		.name	= DRV_NAME,
> +		.pm	= &cros_ec_sensors_pm_ops,
> +	},
> +	.probe		= cros_ec_lid_angle_probe,
> +	.id_table	= cros_ec_lid_angle_ids,
> +};
> +module_platform_driver(cros_ec_lid_angle_platform_driver);
> +
> +MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
> index d275deaecb12..52ea2f1c87a1 100644
> --- a/drivers/mfd/cros_ec_dev.c
> +++ b/drivers/mfd/cros_ec_dev.c
> @@ -297,13 +297,15 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
Line break in the above means the patch doesn't apply.

> 
>  	resp = (struct ec_response_motion_sense *)msg->data;
>  	sensor_num = resp->dump.sensor_count;
> -	/* Allocate 1 extra sensors in FIFO are needed */
> -	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
> +	/*
> +	 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
> +	 */
> +	sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
>  			       GFP_KERNEL);
>  	if (sensor_cells == NULL)
>  		goto error;
> 
> -	sensor_platforms = kcalloc(sensor_num + 1,
> +	sensor_platforms = kcalloc(sensor_num,
>  				   sizeof(struct cros_ec_sensor_platform),
>  				   GFP_KERNEL);
>  	if (sensor_platforms == NULL)
> @@ -363,6 +365,11 @@ static void cros_ec_sensors_register(struct
> cros_ec_dev *ec)
Line break here means patch doesn't apply.
>  		sensor_cells[id].name = "cros-ec-ring";
>  		id++;
>  	}
> +	if (cros_ec_check_features(ec,
> +				EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
> +		sensor_cells[id].name = "cros-ec-lid-angle";
> +		id++;
> +	}
> 
>  	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
>  			      NULL, 0, NULL);


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

end of thread, other threads:[~2019-03-31 10:28 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-28  1:50 [PATCH] iio: cros_ec: Add lid angle driver Gwendal Grignou
2019-02-28 11:18 ` Enric Balletbo i Serra
2019-02-28 17:41   ` Gwendal Grignou
2019-02-28 17:41     ` [PATCH v2] " Gwendal Grignou
2019-03-03 12:43       ` Jonathan Cameron
2019-03-07  0:33         ` Gwendal Grignou
2019-03-07  0:35           ` [PATCH v3] " Gwendal Grignou
2019-03-07 12:32             ` Enric Balletbo i Serra
2019-03-09 17:43               ` Jonathan Cameron
2019-03-11  8:24 ` [PATCH] " Lee Jones
2019-03-29  8:41   ` [PATCH v4 2/2] " Gwendal Grignou
2019-03-29 13:44     ` Guenter Roeck
2019-03-29 17:40       ` Gwendal Grignou
2019-03-29 20:08         ` [PATCH v5] " Gwendal Grignou
2019-03-29 20:17           ` Guenter Roeck
2019-03-29 22:37             ` Gwendal Grignou
2019-03-29 23:38               ` Guenter Roeck
2019-03-31 10:28           ` Jonathan Cameron

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