Skip to contentSkip to conversation

dw9714: Initial driver for dw9714 VCM

PATCH Superseded [email protected] 4 participants 11 messages May 7, 2017 – May 9, 2017

Subsystems (3)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dw9714: Initial driver for dw9714 VCM

From: Rajmohan Mani <[email protected]>

DW9714 is a 10 bit DAC, designed for linear
control of voice coil motor.

This driver creates a V4L2 subdevice and
provides control to set the desired focus.

---
 drivers/media/i2c/Kconfig  |   9 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/dw9714.c | 333 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/media/i2c/dw9714.c
drivers/media/i2c/Kconfig +9 -0
index fd181c9..4f0a7ad 100644
@@ -300,6 +300,15 @@ config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
+config VIDEO_DW9714
+	tristate "DW9714 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER
+	---help---
+	  This is a driver for the DW9714 camera lens voice coil.
+	  DW9714 is a 10 bit DAC with 120mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
drivers/media/i2c/Makefile +1 -0
index 62323ec..987bd1f 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o
 obj-$(CONFIG_VIDEO_AD5820)  += ad5820.o
+obj-$(CONFIG_VIDEO_DW9714)  += dw9714.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o
drivers/media/i2c/dw9714.c new file +333 -0

333 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw

Hi Rajmohan,

Thanks for the patch!

A quick code review:

Quoted 109 lines · On 05/07/2017 01:33 PM, [email protected] wrote:

From: Rajmohan Mani <[email protected]>

DW9714 is a 10 bit DAC, designed for linear
control of voice coil motor.

This driver creates a V4L2 subdevice and
provides control to set the desired focus.

---
 drivers/media/i2c/Kconfig  |   9 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/dw9714.c | 333 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/media/i2c/dw9714.c
drivers/media/i2c/Kconfig +9 -0
index fd181c9..4f0a7ad 100644
@@ -300,6 +300,15 @@ config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
+config VIDEO_DW9714
+	tristate "DW9714 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER
+	---help---
+	  This is a driver for the DW9714 camera lens voice coil.
+	  DW9714 is a 10 bit DAC with 120mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
drivers/media/i2c/Makefile +1 -0
index 62323ec..987bd1f 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o
 obj-$(CONFIG_VIDEO_AD5820)  += ad5820.o
+obj-$(CONFIG_VIDEO_DW9714)  += dw9714.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o
drivers/media/i2c/dw9714.c new file +55 -0
new file mode 100644
index 0000000..276d3f2
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2015--2017 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+
+#define DW9714_NAME		"dw9714"
+#define DW9714_MAX_FOCUS_POS	1023
+#define DW9714_CTRL_STEPS	16	/* Keep this value power of 2 */
+#define DW9714_CTRL_DELAY_US	1000
+/*
+ * S[3:2] = 0x00, codes per step for "Linear Slope Control"
+ * S[1:0] = 0x00, step period
+ */
+#define DW9714_DEFAULT_S 0x0
+#define DW9714_VAL(data, s) (u16)((data) << 4 | (s))
+
+/* dw9714 device structure */
+struct dw9714_device {
+	struct i2c_client *client;
+	struct v4l2_ctrl_handler ctrls_vcm;
+	struct v4l2_subdev sd;
+	u16 current_val;
+};
+
+#define to_dw9714_vcm(_ctrl)	\
+	container_of(_ctrl->handler, struct dw9714_device, ctrls_vcm)
+
+static int dw9714_i2c_write(struct i2c_client *client, u16 data)
+{
+	const int num_msg = 1;
+	int ret;
+	u16 val = cpu_to_be16(data);
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = 0,
+		.len = sizeof(val),
+		.buf = (u8 *) &val,
+	};
+

drivers/media/i2c/dw9714.c line 65

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	/*One retry */
+	if (ret != num_msg)
+		ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	if (ret != num_msg) {
+		dev_err(&client->dev, "I2C write fail fail\n");
+		return -EIO;
+	} else {

No need for the 'else' here, since the 'if' already returns.

Quoted 4 lines
drivers/media/i2c/dw9714.c new file +4 -0
+		return 0;
+	}
+}
+

drivers/media/i2c/dw9714.c line 79

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
+{
+	struct i2c_client *client = dw9714_dev->client;
+	int ret = -EINVAL;
+
+	dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
+	dw9714_dev->current_val = val;
+
+	ret = dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
+	return ret;

Just do return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));

Thus dropping the 'ret' variable.

drivers/media/i2c/dw9714.c line 87

Quoted 8 lines
drivers/media/i2c/dw9714.c new file +8 -0
+}
+
+static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
+
+	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
+		return dw9714_t_focus_vcm(dev_vcm, ctrl->val);

Personally I would merge the dw9714_t_focus_vcm in here. And probably use a switch instead of an if. Up to you, though.

drivers/media/i2c/dw9714.c line 88

Quoted 1 line
drivers/media/i2c/dw9714.c new file +1 -0
+	else

No need for 'else'

Quoted 32 lines
drivers/media/i2c/dw9714.c new file +32 -0
+		return -EINVAL;
+}
+
+static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
+	.s_ctrl = dw9714_set_ctrl,
+};
+
+static int dw9714_init_controls(struct dw9714_device *dev_vcm)
+{
+	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
+	const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
+	struct i2c_client *client = dev_vcm->client;
+
+	v4l2_ctrl_handler_init(hdl, 1);
+
+	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
+			  0, DW9714_MAX_FOCUS_POS, 1, 0);
+
+	if (hdl->error)
+		dev_err(&client->dev, "dw9714_init_controls fail\n");
+	dev_vcm->sd.ctrl_handler = hdl;
+	return hdl->error;
+}
+
+static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
+{
+	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
+	v4l2_async_unregister_subdev(&dw9714_dev->sd);
+	v4l2_device_unregister_subdev(&dw9714_dev->sd);
+	media_entity_cleanup(&dw9714_dev->sd.entity);
+}
+

drivers/media/i2c/dw9714.c line 130

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+	int rval;
+
+	rval = pm_runtime_get_sync(dev);
+	dev_dbg(dev, "%s rval = %d\n", __func__, rval);

The error is propagated to userspace anyway as errno, so do you really need a dev_dbg here?

drivers/media/i2c/dw9714.c line 142

Quoted 12 lines
drivers/media/i2c/dw9714.c new file +12 -0
+
+	return rval;
+}
+
+static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+
+	dev_dbg(dev, "%s\n", __func__);

Seems useless here as well.

Quoted 24 lines
drivers/media/i2c/dw9714.c new file +24 -0
+	pm_runtime_put(dev);
+
+	return 0;
+}
+
+static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
+	.open = dw9714_open,
+	.close = dw9714_close,
+};
+
+static const struct v4l2_subdev_ops dw9714_ops = { };
+
+static int dw9714_probe(struct i2c_client *client,
+			const struct i2c_device_id *devid)
+{
+	struct dw9714_device *dw9714_dev;
+	int rval;
+
+	dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
+				  GFP_KERNEL);
+
+	if (dw9714_dev == NULL)
+		return -ENOMEM;
+

drivers/media/i2c/dw9714.c line 176

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	dw9714_dev->client = client;
+
+	v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
+	dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	dw9714_dev->sd.internal_ops = &dw9714_int_ops;
+
+	snprintf(dw9714_dev->sd.name,
+		 sizeof(dw9714_dev->sd.name),
+		 DW9714_NAME " %d-%4.4x", i2c_adapter_id(client->adapter),
+		 client->addr);

v4l2_i2c_subdev_init() already fills in the sd.name, so this can be dropped.

Quoted 148 lines
drivers/media/i2c/dw9714.c new file +147 -0
+
+	rval = dw9714_init_controls(dw9714_dev);
+	if (rval)
+		goto err_cleanup;
+
+	rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
+	if (rval < 0)
+		goto err_cleanup;
+
+	dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+	if (rval < 0)
+		goto err_cleanup;
+
+	pm_runtime_enable(&client->dev);
+
+	return 0;
+
+err_cleanup:
+	dw9714_subdev_cleanup(dw9714_dev);
+	dev_err(&client->dev, "Probe failed: %d\n", rval);
+	return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+
+	pm_runtime_disable(&client->dev);
+	dw9714_subdev_cleanup(dw9714_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+	return 0;
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+	     val >= 0; val -= DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+	}
+	return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+	     val += DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+	}
+
+	/* restore v4l2 control values */
+	ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);
+	dev_dbg(dev, "%s ret = %d\n", __func__, ret);
+	return ret;
+}
+
+#else
+
+#define dw9714_suspend	NULL
+#define dw9714_resume	NULL
+#define dw9714_runtime_suspend	NULL
+#define dw9714_runtime_resume	NULL
+
+#endif /* CONFIG_PM */
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id dw9714_acpi_match[] = {
+	{"DW9714", 0},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, dw9714_acpi_match);
+#endif
+
+static const struct i2c_device_id dw9714_id_table[] = {
+	{DW9714_NAME, 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
+
+static const struct dev_pm_ops dw9714_pm_ops = {
+	.suspend = dw9714_suspend,
+	.resume = dw9714_resume,
+	.runtime_suspend = dw9714_runtime_suspend,
+	.runtime_resume = dw9714_runtime_resume,
+};
+
+static struct i2c_driver dw9714_i2c_driver = {
+	.driver = {
+		.name = DW9714_NAME,
+		.pm = &dw9714_pm_ops,
+#ifdef CONFIG_ACPI
+		.acpi_match_table = ACPI_PTR(dw9714_acpi_match),
+#endif
+	},
+	.probe = dw9714_probe,
+	.remove = dw9714_remove,
+	.id_table = dw9714_id_table,
+};

drivers/media/i2c/dw9714.c line 333

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+module_i2c_driver(dw9714_i2c_driver);
+
+MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
+MODULE_AUTHOR("Jian Xu Zheng <[email protected]>");
+MODULE_AUTHOR("Yuning Pu <[email protected]>");
+MODULE_AUTHOR("Jouni Ukkonen <[email protected]>");
+MODULE_AUTHOR("Tommi Franttila <[email protected]>");
+MODULE_DESCRIPTION("DW9714 VCM driver");
+MODULE_LICENSE("GPL");

Regards,

Hans

#2 lore raw

Hi Hans,

Thanks for the quick review. Please see my comments inline... I will post the new v2 patch soon.

Quoted 128 lines

-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Monday, May 08, 2017 6:59 PM
To: Mani, Rajmohan <[email protected]>; linux- [email protected]; [email protected]
Subject: Re: [PATCH] dw9714: Initial driver for dw9714 VCM

Hi Rajmohan,

Thanks for the patch!

A quick code review:

Quoted 69 lines · On 05/07/2017 01:33 PM, [email protected] wrote:

From: Rajmohan Mani <[email protected]>

DW9714 is a 10 bit DAC, designed for linear control of voice coil motor.

This driver creates a V4L2 subdevice and provides control to set the desired focus.

---
 drivers/media/i2c/Kconfig  |   9 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/dw9714.c | 333
+++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/media/i2c/dw9714.c
drivers/media/i2c/Kconfig +9 -0
index fd181c9..4f0a7ad 100644
@@ -300,6 +300,15 @@ config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
+config VIDEO_DW9714
+	tristate "DW9714 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER
+	---help---
+	  This is a driver for the DW9714 camera lens voice coil.
+	  DW9714 is a 10 bit DAC with 120mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
drivers/media/i2c/Makefile +1 -0
index 62323ec..987bd1f 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o
 obj-$(CONFIG_VIDEO_AD5820)  += ad5820.o
+obj-$(CONFIG_VIDEO_DW9714)  += dw9714.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o diff --git

a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c new file mode 100644 index 0000000..276d3f2

drivers/media/i2c/dw9714.c new file +15 -0
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2015--2017 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>

drivers/media/i2c/dw9714.c line 25

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+
+#define DW9714_NAME		"dw9714"
+#define DW9714_MAX_FOCUS_POS	1023
+#define DW9714_CTRL_STEPS	16	/* Keep this value power of 2

*/

Quoted 34 lines
drivers/media/i2c/dw9714.c new file +34 -0
+#define DW9714_CTRL_DELAY_US	1000
+/*
+ * S[3:2] = 0x00, codes per step for "Linear Slope Control"
+ * S[1:0] = 0x00, step period
+ */
+#define DW9714_DEFAULT_S 0x0
+#define DW9714_VAL(data, s) (u16)((data) << 4 | (s))
+
+/* dw9714 device structure */
+struct dw9714_device {
+	struct i2c_client *client;
+	struct v4l2_ctrl_handler ctrls_vcm;
+	struct v4l2_subdev sd;
+	u16 current_val;
+};
+
+#define to_dw9714_vcm(_ctrl)	\
+	container_of(_ctrl->handler, struct dw9714_device, ctrls_vcm)
+
+static int dw9714_i2c_write(struct i2c_client *client, u16 data) {
+	const int num_msg = 1;
+	int ret;
+	u16 val = cpu_to_be16(data);
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = 0,
+		.len = sizeof(val),
+		.buf = (u8 *) &val,
+	};
+
+	ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	/*One retry */
+	if (ret != num_msg)
Quoted 8 lines

drivers/media/i2c/dw9714.c line 65

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+		ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	if (ret != num_msg) {
+		dev_err(&client->dev, "I2C write fail fail\n");
+		return -EIO;
+	} else {

No need for the 'else' here, since the 'if' already returns.

Ack
I also removed the redundant " fail" in the dev_err() message.

Quoted 15 lines
Quoted 4 lines
drivers/media/i2c/dw9714.c new file +3 -0
+		return 0;
+	}
+}

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16
+val) {
+	struct i2c_client *client = dw9714_dev->client;
+	int ret = -EINVAL;
+
+	dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
+	dw9714_dev->current_val = val;
+
+	ret = dw9714_i2c_write(client, DW9714_VAL(val,

DW9714_DEFAULT_S));

Quoted 6 lines

drivers/media/i2c/dw9714.c line 79

Quoted 1 line
drivers/media/i2c/dw9714.c new file +1 -0
+	return ret;

Just do return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));

Thus dropping the 'ret' variable.

Ack

Quoted 11 lines

drivers/media/i2c/dw9714.c line 87

Quoted 8 lines
drivers/media/i2c/dw9714.c new file +7 -0
+}
+
+static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl) {
+	struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
+
+	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
+		return dw9714_t_focus_vcm(dev_vcm, ctrl->val);

Personally I would merge the dw9714_t_focus_vcm in here. And probably use a switch instead of an if. Up to you, though.

With just one ctrl->id, dw9714_set_ctrl() looks to be more readable with the if and dw9714_t_focus_vcm() call. When more ids are added, merging of dw9714_t_focus_vcm() and switch statement becomes more apt. So, for now, I am leaving it as such.

Quoted 4 lines

drivers/media/i2c/dw9714.c line 88

Quoted 2 lines
drivers/media/i2c/dw9714.c new file +1 -0
+	else

No need for 'else'

Ack

Quoted 36 lines
Quoted 25 lines
drivers/media/i2c/dw9714.c new file +24 -0
+		return -EINVAL;
+}
+
+static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
+	.s_ctrl = dw9714_set_ctrl,
+};
+
+static int dw9714_init_controls(struct dw9714_device *dev_vcm) {
+	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
+	const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
+	struct i2c_client *client = dev_vcm->client;
+
+	v4l2_ctrl_handler_init(hdl, 1);
+
+	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
+			  0, DW9714_MAX_FOCUS_POS, 1, 0);
+
+	if (hdl->error)
+		dev_err(&client->dev, "dw9714_init_controls fail\n");
+	dev_vcm->sd.ctrl_handler = hdl;
+	return hdl->error;
+}
+
+static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) {

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
+	v4l2_async_unregister_subdev(&dw9714_dev->sd);
+	v4l2_device_unregister_subdev(&dw9714_dev->sd);
+	media_entity_cleanup(&dw9714_dev->sd.entity);
+}
+
+static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh
+*fh) {
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 9 lines

drivers/media/i2c/dw9714.c line 130

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+	int rval;
+
+	rval = pm_runtime_get_sync(dev);
+	dev_dbg(dev, "%s rval = %d\n", __func__, rval);

The error is propagated to userspace anyway as errno, so do you really need a dev_dbg here?

Ack
I removed the dev_dbg message. I also got rid of rval, by just doing ret pm_runtime_get_sync(dev)

Quoted 10 lines

drivers/media/i2c/dw9714.c

Quoted 9 lines
drivers/media/i2c/dw9714.c new file +8 -0
+
+	return rval;
+}
+
+static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh
+*fh) {
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 6 lines

drivers/media/i2c/dw9714.c line 142

Quoted 4 lines
drivers/media/i2c/dw9714.c new file +4 -0
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+
+	dev_dbg(dev, "%s\n", __func__);

Seems useless here as well.

Ack

Quoted 29 lines
Quoted 29 lines
drivers/media/i2c/dw9714.c new file +28 -0
+	pm_runtime_put(dev);
+
+	return 0;
+}
+
+static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
+	.open = dw9714_open,
+	.close = dw9714_close,
+};
+
+static const struct v4l2_subdev_ops dw9714_ops = { };
+
+static int dw9714_probe(struct i2c_client *client,
+			const struct i2c_device_id *devid) {
+	struct dw9714_device *dw9714_dev;
+	int rval;
+
+	dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
+				  GFP_KERNEL);
+
+	if (dw9714_dev == NULL)
+		return -ENOMEM;
+
+	dw9714_dev->client = client;
+
+	v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
+	dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	dw9714_dev->sd.internal_ops = &dw9714_int_ops;
Quoted 8 lines

drivers/media/i2c/dw9714.c line 176

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +4 -0
+
+	snprintf(dw9714_dev->sd.name,
+		 sizeof(dw9714_dev->sd.name),
+		 DW9714_NAME " %d-%4.4x", i2c_adapter_id(client-

adapter),

drivers/media/i2c/dw9714.c new file +1 -0
+		 client->addr);

v4l2_i2c_subdev_init() already fills in the sd.name, so this can be dropped.

Ack

Quoted 161 lines
Quoted 20 lines
drivers/media/i2c/dw9714.c new file +19 -0
+
+	rval = dw9714_init_controls(dw9714_dev);
+	if (rval)
+		goto err_cleanup;
+
+	rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
+	if (rval < 0)
+		goto err_cleanup;
+
+	dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+	if (rval < 0)
+		goto err_cleanup;
+
+	pm_runtime_enable(&client->dev);
+
+	return 0;
+

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+err_cleanup:
+	dw9714_subdev_cleanup(dw9714_dev);
+	dev_err(&client->dev, "Probe failed: %d\n", rval);
+	return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client) {
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 14 lines
drivers/media/i2c/dw9714.c new file +14 -0
+							sd);
+
+	pm_runtime_disable(&client->dev);
+	dw9714_subdev_cleanup(dw9714_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev) {
+	return 0;
+}
+

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+static int dw9714_runtime_resume(struct device *dev) {
+	return 0;
+}
+
+/* This function sets the vcm position, so it consumes least current
+*/ static int dw9714_suspend(struct device *dev) {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

drivers/media/i2c/dw9714.c

Quoted 9 lines
drivers/media/i2c/dw9714.c new file +9 -0
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+	     val >= 0; val -= DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val,

DW9714_DEFAULT_S));

drivers/media/i2c/dw9714.c

Quoted 3 lines
drivers/media/i2c/dw9714.c new file +3 -0
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US,

DW9714_CTRL_DELAY_US + 10);

drivers/media/i2c/dw9714.c

Quoted 13 lines
drivers/media/i2c/dw9714.c new file +13 -0
+	}
+	return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev) {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+	     val += DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val,

DW9714_DEFAULT_S));

drivers/media/i2c/dw9714.c

Quoted 3 lines
drivers/media/i2c/dw9714.c new file +3 -0
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US,

DW9714_CTRL_DELAY_US + 10);

Quoted 48 lines
drivers/media/i2c/dw9714.c new file +47 -0
+	}
+
+	/* restore v4l2 control values */
+	ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);
+	dev_dbg(dev, "%s ret = %d\n", __func__, ret);
+	return ret;
+}
+
+#else
+
+#define dw9714_suspend	NULL
+#define dw9714_resume	NULL
+#define dw9714_runtime_suspend	NULL
+#define dw9714_runtime_resume	NULL
+
+#endif /* CONFIG_PM */
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id dw9714_acpi_match[] = {
+	{"DW9714", 0},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, dw9714_acpi_match); #endif
+
+static const struct i2c_device_id dw9714_id_table[] = {
+	{DW9714_NAME, 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
+
+static const struct dev_pm_ops dw9714_pm_ops = {
+	.suspend = dw9714_suspend,
+	.resume = dw9714_resume,
+	.runtime_suspend = dw9714_runtime_suspend,
+	.runtime_resume = dw9714_runtime_resume, };
+
+static struct i2c_driver dw9714_i2c_driver = {
+	.driver = {
+		.name = DW9714_NAME,
+		.pm = &dw9714_pm_ops,
+#ifdef CONFIG_ACPI
+		.acpi_match_table = ACPI_PTR(dw9714_acpi_match), #endif
+	},
+	.probe = dw9714_probe,
+	.remove = dw9714_remove,
+	.id_table = dw9714_id_table,

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+};
+
+module_i2c_driver(dw9714_i2c_driver);
+
+MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
+MODULE_AUTHOR("Jian Xu Zheng <[email protected]>");
+MODULE_AUTHOR("Yuning Pu <[email protected]>");
+MODULE_AUTHOR("Jouni Ukkonen <[email protected]>");
+MODULE_AUTHOR("Tommi Franttila <[email protected]>");
+MODULE_DESCRIPTION("DW9714 VCM driver"); MODULE_LICENSE("GPL");

Regards,

Hans

#3 lore raw
In reply to [email protected]

Hi Rajmohan,

A few comments below...

Quoted 21 lines · On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

From: Rajmohan Mani <[email protected]>

DW9714 is a 10 bit DAC, designed for linear
control of voice coil motor.

This driver creates a V4L2 subdevice and
provides control to set the desired focus.

---
 drivers/media/i2c/Kconfig  |   9 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/dw9714.c | 333 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/media/i2c/dw9714.c
drivers/media/i2c/Kconfig +0 -0
index fd181c9..4f0a7ad 100644

drivers/media/i2c/Kconfig config VIDEO_AD5820 · line 305

Quoted 6 lines
drivers/media/i2c/Kconfig +3 -0
@@ -300,6 +300,15 @@ config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
+config VIDEO_DW9714
+	tristate "DW9714 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER

You should also depend on VIDEO_V4L2_SUBDEV_API .

Quoted 132 lines
drivers/media/i2c/Kconfig +6 -0
+	---help---
+	  This is a driver for the DW9714 camera lens voice coil.
+	  DW9714 is a 10 bit DAC with 120mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
drivers/media/i2c/Makefile +1 -0
index 62323ec..987bd1f 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o
 obj-$(CONFIG_VIDEO_AD5820)  += ad5820.o
+obj-$(CONFIG_VIDEO_DW9714)  += dw9714.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o
drivers/media/i2c/dw9714.c new file +105 -0
new file mode 100644
index 0000000..276d3f2
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2015--2017 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+
+#define DW9714_NAME		"dw9714"
+#define DW9714_MAX_FOCUS_POS	1023
+#define DW9714_CTRL_STEPS	16	/* Keep this value power of 2 */
+#define DW9714_CTRL_DELAY_US	1000
+/*
+ * S[3:2] = 0x00, codes per step for "Linear Slope Control"
+ * S[1:0] = 0x00, step period
+ */
+#define DW9714_DEFAULT_S 0x0
+#define DW9714_VAL(data, s) (u16)((data) << 4 | (s))
+
+/* dw9714 device structure */
+struct dw9714_device {
+	struct i2c_client *client;
+	struct v4l2_ctrl_handler ctrls_vcm;
+	struct v4l2_subdev sd;
+	u16 current_val;
+};
+
+#define to_dw9714_vcm(_ctrl)	\
+	container_of(_ctrl->handler, struct dw9714_device, ctrls_vcm)
+
+static int dw9714_i2c_write(struct i2c_client *client, u16 data)
+{
+	const int num_msg = 1;
+	int ret;
+	u16 val = cpu_to_be16(data);
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = 0,
+		.len = sizeof(val),
+		.buf = (u8 *) &val,
+	};
+
+	ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	/*One retry */
+	if (ret != num_msg)
+		ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	if (ret != num_msg) {
+		dev_err(&client->dev, "I2C write fail fail\n");
+		return -EIO;
+	} else {
+		return 0;
+	}
+}
+
+static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
+{
+	struct i2c_client *client = dw9714_dev->client;
+	int ret = -EINVAL;
+
+	dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
+	dw9714_dev->current_val = val;
+
+	ret = dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
+	return ret;
+}
+
+static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
+
+	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
+		return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
+	else
+		return -EINVAL;
+}
+
+static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
+	.s_ctrl = dw9714_set_ctrl,
+};
+
+static int dw9714_init_controls(struct dw9714_device *dev_vcm)
+{
+	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
+	const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
+	struct i2c_client *client = dev_vcm->client;
+
+	v4l2_ctrl_handler_init(hdl, 1);
+
+	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
+			  0, DW9714_MAX_FOCUS_POS, 1, 0);

drivers/media/i2c/dw9714.c line 115

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+	if (hdl->error)
+		dev_err(&client->dev, "dw9714_init_controls fail\n");
+	dev_vcm->sd.ctrl_handler = hdl;
+	return hdl->error;
+}
+
+static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
+{
+	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);

Please release the control handler after unregistering the sub-device.

Quoted 4 lines
drivers/media/i2c/dw9714.c new file +4 -0
+	v4l2_async_unregister_subdev(&dw9714_dev->sd);
+	v4l2_device_unregister_subdev(&dw9714_dev->sd);
+	media_entity_cleanup(&dw9714_dev->sd.entity);
+}

drivers/media/i2c/dw9714.c line 129

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+	int rval;
+
+	rval = pm_runtime_get_sync(dev);

If this fails you'll need to do pm_runtime_put(dev);

Quoted 14 lines
drivers/media/i2c/dw9714.c new file +14 -0
+	dev_dbg(dev, "%s rval = %d\n", __func__, rval);
+
+	return rval;
+}
+
+static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+
+	dev_dbg(dev, "%s\n", __func__);
+	pm_runtime_put(dev);

drivers/media/i2c/dw9714.c line 153

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+	return 0;
+}
+
+static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
+	.open = dw9714_open,
+	.close = dw9714_close,
+};
+
+static const struct v4l2_subdev_ops dw9714_ops = { };

You should use v4l2_subdev_internal_ops here.

Quoted 29 lines
drivers/media/i2c/dw9714.c new file +29 -0
+
+static int dw9714_probe(struct i2c_client *client,
+			const struct i2c_device_id *devid)
+{
+	struct dw9714_device *dw9714_dev;
+	int rval;
+
+	dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
+				  GFP_KERNEL);
+
+	if (dw9714_dev == NULL)
+		return -ENOMEM;
+
+	dw9714_dev->client = client;
+
+	v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
+	dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	dw9714_dev->sd.internal_ops = &dw9714_int_ops;
+
+	snprintf(dw9714_dev->sd.name,
+		 sizeof(dw9714_dev->sd.name),
+		 DW9714_NAME " %d-%4.4x", i2c_adapter_id(client->adapter),
+		 client->addr);
+
+	rval = dw9714_init_controls(dw9714_dev);
+	if (rval)
+		goto err_cleanup;
+
+	rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);

drivers/media/i2c/dw9714.c line 192

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	if (rval < 0)
+		goto err_cleanup;
+
+	dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+	if (rval < 0)
+		goto err_cleanup;
+
+	pm_runtime_enable(&client->dev);

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Quoted 22 lines
drivers/media/i2c/dw9714.c new file +22 -0
+
+	return 0;
+
+err_cleanup:
+	dw9714_subdev_cleanup(dw9714_dev);
+	dev_err(&client->dev, "Probe failed: %d\n", rval);
+	return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+
+	pm_runtime_disable(&client->dev);
+	dw9714_subdev_cleanup(dw9714_dev);
+
+	return 0;
+}
+

drivers/media/i2c/dw9714.c line 224

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
+	return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+	return 0;

I think it'd be fine to remove empty callbacks.

Quoted 42 lines
drivers/media/i2c/dw9714.c new file +42 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+	     val >= 0; val -= DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+	}
+	return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct dw9714_device,
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;

drivers/media/i2c/dw9714.c line 276

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	     val += DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+	}
+
+	/* restore v4l2 control values */
+	ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

Quoted 57 lines
drivers/media/i2c/dw9714.c new file +57 -0
+	dev_dbg(dev, "%s ret = %d\n", __func__, ret);
+	return ret;
+}
+
+#else
+
+#define dw9714_suspend	NULL
+#define dw9714_resume	NULL
+#define dw9714_runtime_suspend	NULL
+#define dw9714_runtime_resume	NULL
+
+#endif /* CONFIG_PM */
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id dw9714_acpi_match[] = {
+	{"DW9714", 0},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, dw9714_acpi_match);
+#endif
+
+static const struct i2c_device_id dw9714_id_table[] = {
+	{DW9714_NAME, 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
+
+static const struct dev_pm_ops dw9714_pm_ops = {
+	.suspend = dw9714_suspend,
+	.resume = dw9714_resume,
+	.runtime_suspend = dw9714_runtime_suspend,
+	.runtime_resume = dw9714_runtime_resume,
+};
+
+static struct i2c_driver dw9714_i2c_driver = {
+	.driver = {
+		.name = DW9714_NAME,
+		.pm = &dw9714_pm_ops,
+#ifdef CONFIG_ACPI
+		.acpi_match_table = ACPI_PTR(dw9714_acpi_match),
+#endif
+	},
+	.probe = dw9714_probe,
+	.remove = dw9714_remove,
+	.id_table = dw9714_id_table,
+};
+
+module_i2c_driver(dw9714_i2c_driver);
+
+MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
+MODULE_AUTHOR("Jian Xu Zheng <[email protected]>");
+MODULE_AUTHOR("Yuning Pu <[email protected]>");
+MODULE_AUTHOR("Jouni Ukkonen <[email protected]>");
+MODULE_AUTHOR("Tommi Franttila <[email protected]>");
+MODULE_DESCRIPTION("DW9714 VCM driver");
+MODULE_LICENSE("GPL");
Signature · 4 lines
Regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#4 lore raw

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

[snip]

Quoted 10 lines

drivers/media/i2c/dw9714.c line 192

Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+     rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+     if (rval < 0)
+             goto err_cleanup;
+
+     pm_runtime_enable(&client->dev);

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Is this something specific for I2C devices? For platform devices, typically pm_runtime_enable() is the only thing you would need to do.

Quoted 27 lines
Quoted 27 lines
drivers/media/i2c/dw9714.c new file +26 -0
+
+     return 0;
+
+err_cleanup:
+     dw9714_subdev_cleanup(dw9714_dev);
+     dev_err(&client->dev, "Probe failed: %d\n", rval);
+     return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+
+     pm_runtime_disable(&client->dev);
+     dw9714_subdev_cleanup(dw9714_dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
Quoted 8 lines

drivers/media/i2c/dw9714.c line 224

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

Quoted 47 lines
Quoted 47 lines
drivers/media/i2c/dw9714.c new file +46 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+          val >= 0; val -= DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+     return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+          val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+          val += DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
Quoted 8 lines

drivers/media/i2c/dw9714.c line 276

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+
+     /* restore v4l2 control values */
+     ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

This driver doesn't seem to be doing any physical power off in its runtime_suspend and I don't expect an I2C device to be put in a PM domain, so possibly no need for it.

Best regards,
Tomasz

#5 lore raw

Hi Tomasz,

Quoted 15 lines · Tomasz Figa

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

[snip]

Quoted 6 lines
Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+     rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+     if (rval < 0)
+             goto err_cleanup;
+
+     pm_runtime_enable(&client->dev);
Quoted 7 lines
Quoted 4 lines

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Is this something specific for I2C devices? For platform devices, typically pm_runtime_enable() is the only thing you would need to do.

I think you're right --- driver_probe_device() will call pm_request_idle() to the device right after probe. So indeed calling pm_runtime_enable() is enough.

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
drivers/media/i2c/dw9714.c new file +26 -0
+
+     return 0;
+
+err_cleanup:
+     dw9714_subdev_cleanup(dw9714_dev);
+     dev_err(&client->dev, "Probe failed: %d\n", rval);
+     return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+
+     pm_runtime_disable(&client->dev);
+     dw9714_subdev_cleanup(dw9714_dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
Quoted 8 lines

drivers/media/i2c/dw9714.c line 224

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

Quoted 4 lines

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

I wonder if I'm missing something --- acpi_subsys_runtime_resume() first calls acpi_dev_runtime_resume() and if all goes well, the proceeds to call pm_generic_runtime_resume() which calls device's runtime_resume() if it's non-NULL.

In other words, having a runtime_resume() and runtime_suspend() callbacks that return zero is equivalent of having neither of the callbacks.

Quoted 57 lines
Quoted 48 lines
Quoted 48 lines
drivers/media/i2c/dw9714.c new file +46 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+          val >= 0; val -= DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+     return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+          val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+          val += DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
Quoted 8 lines

drivers/media/i2c/dw9714.c line 276

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+
+     /* restore v4l2 control values */
+     ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

Quoted 3 lines

This driver doesn't seem to be doing any physical power off in its runtime_suspend and I don't expect an I2C device to be put in a PM domain, so possibly no need for it.

I'd expect runtime PM suspend callback to power the device off through ACPI PM. For this reason the device state must be restored when it's powered on, i.e. its runtime_resume callback.

Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#6 lore raw
Quoted 70 lines · Sakari Ailus

Hi Tomasz,

Quoted 15 lines · Tomasz Figa

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

[snip]

Quoted 6 lines
Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+     rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+     if (rval < 0)
+             goto err_cleanup;
+
+     pm_runtime_enable(&client->dev);
Quoted 7 lines
Quoted 4 lines

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Is this something specific for I2C devices? For platform devices, typically pm_runtime_enable() is the only thing you would need to do.

I think you're right --- driver_probe_device() will call pm_request_idle() to the device right after probe. So indeed calling pm_runtime_enable() is enough.

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
drivers/media/i2c/dw9714.c new file +26 -0
+
+     return 0;
+
+err_cleanup:
+     dw9714_subdev_cleanup(dw9714_dev);
+     dev_err(&client->dev, "Probe failed: %d\n", rval);
+     return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+
+     pm_runtime_disable(&client->dev);
+     dw9714_subdev_cleanup(dw9714_dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
Quoted 8 lines

drivers/media/i2c/dw9714.c line 224

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

Quoted 4 lines

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

Quoted 7 lines

I wonder if I'm missing something --- acpi_subsys_runtime_resume() first calls acpi_dev_runtime_resume() and if all goes well, the proceeds to call pm_generic_runtime_resume() which calls device's runtime_resume() if it's non-NULL.

In other words, having a runtime_resume() and runtime_suspend() callbacks that return zero is equivalent of having neither of the callbacks.

Ah, I missed the fact this device is instantiated by ACPI and it has different handling of runtime PM, which apparently means it doesn't use the code paths affected by the PM domain thing I mentioned.

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 45 lines
drivers/media/i2c/dw9714.c new file +42 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+          val >= 0; val -= DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+     return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+          val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;

drivers/media/i2c/dw9714.c line 276

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+          val += DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+
+     /* restore v4l2 control values */
+     ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

Quoted 7 lines
Quoted 3 lines

This driver doesn't seem to be doing any physical power off in its runtime_suspend and I don't expect an I2C device to be put in a PM domain, so possibly no need for it.

I'd expect runtime PM suspend callback to power the device off through ACPI PM. For this reason the device state must be restored when it's powered on, i.e. its runtime_resume callback.

Ah, again ACPI here, that's something not usual for me. Sorry for the noise.

Best regards,
Tomasz

#7 lore raw

Hi Tomasz,

Quoted 76 lines · Tomasz Figa
Quoted 75 lines · Sakari Ailus

Hi Tomasz,

Quoted 15 lines · Tomasz Figa

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

[snip]

Quoted 6 lines
Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+     rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+     if (rval < 0)
+             goto err_cleanup;
+
+     pm_runtime_enable(&client->dev);
Quoted 7 lines
Quoted 4 lines

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Is this something specific for I2C devices? For platform devices, typically pm_runtime_enable() is the only thing you would need to do.

I think you're right --- driver_probe_device() will call pm_request_idle() to the device right after probe. So indeed calling pm_runtime_enable() is enough.

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
drivers/media/i2c/dw9714.c new file +26 -0
+
+     return 0;
+
+err_cleanup:
+     dw9714_subdev_cleanup(dw9714_dev);
+     dev_err(&client->dev, "Probe failed: %d\n", rval);
+     return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+
+     pm_runtime_disable(&client->dev);
+     dw9714_subdev_cleanup(dw9714_dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
Quoted 8 lines

drivers/media/i2c/dw9714.c line 224

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

Quoted 4 lines

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

I wonder if I'm missing something --- acpi_subsys_runtime_resume() first calls acpi_dev_runtime_resume() and if all goes well, the proceeds to call pm_generic_runtime_resume() which calls device's runtime_resume() if it's non-NULL.

Quoted 6 lines
Quoted 2 lines

In other words, having a runtime_resume() and runtime_suspend() callbacks that return zero is equivalent of having neither of the callbacks.

Ah, I missed the fact this device is instantiated by ACPI and it has different handling of runtime PM, which apparently means it doesn't use the code paths affected by the PM domain thing I mentioned.

I have to admit I'm no expert in the topic but I'd presume that other implementations should still maintain consistent behaviour towards drivers. acpi_subsys_runtime_resume() is the PM domain runtime_resume() callback in acpi_general_pm_domain.

Quoted 63 lines
Quoted 63 lines
Quoted 62 lines
Quoted 50 lines
Quoted 50 lines
drivers/media/i2c/dw9714.c new file +46 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+          val >= 0; val -= DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+     return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+          val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+          val += DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
Quoted 8 lines

drivers/media/i2c/dw9714.c line 276

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+
+     /* restore v4l2 control values */
+     ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

This driver doesn't seem to be doing any physical power off in its runtime_suspend and I don't expect an I2C device to be put in a PM domain, so possibly no need for it.

Quoted 5 lines
Quoted 3 lines

I'd expect runtime PM suspend callback to power the device off through ACPI PM. For this reason the device state must be restored when it's powered on, i.e. its runtime_resume callback.

Ah, again ACPI here, that's something not usual for me. Sorry for the noise.

No harm done. Very good comments in general IMO, thanks for those!

Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#8 lore raw

+Rafael, Kevin and Ulf,

Quoted 82 lines · Sakari Ailus

Hi Tomasz,

Quoted 79 lines · Tomasz Figa
Quoted 77 lines · Sakari Ailus

Hi Tomasz,

Quoted 15 lines · Tomasz Figa

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

[snip]

Quoted 6 lines
Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+     rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+     if (rval < 0)
+             goto err_cleanup;
+
+     pm_runtime_enable(&client->dev);
Quoted 7 lines
Quoted 4 lines

Getting PM runtime right doesn't seem to be easy. :-I

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Is this something specific for I2C devices? For platform devices, typically pm_runtime_enable() is the only thing you would need to do.

I think you're right --- driver_probe_device() will call pm_request_idle() to the device right after probe. So indeed calling pm_runtime_enable() is enough.

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
drivers/media/i2c/dw9714.c new file +26 -0
+
+     return 0;
+
+err_cleanup:
+     dw9714_subdev_cleanup(dw9714_dev);
+     dev_err(&client->dev, "Probe failed: %d\n", rval);
+     return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client)
+{
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+
+     pm_runtime_disable(&client->dev);
+     dw9714_subdev_cleanup(dw9714_dev);
+
+     return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
Quoted 8 lines

drivers/media/i2c/dw9714.c line 224

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

Quoted 4 lines

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

I wonder if I'm missing something --- acpi_subsys_runtime_resume() first calls acpi_dev_runtime_resume() and if all goes well, the proceeds to call pm_generic_runtime_resume() which calls device's runtime_resume() if it's non-NULL.

In other words, having a runtime_resume() and runtime_suspend() callbacks that return zero is equivalent of having neither of the callbacks.

Quoted 8 lines
Quoted 3 lines

Ah, I missed the fact this device is instantiated by ACPI and it has different handling of runtime PM, which apparently means it doesn't use the code paths affected by the PM domain thing I mentioned.

I have to admit I'm no expert in the topic but I'd presume that other implementations should still maintain consistent behaviour towards drivers. acpi_subsys_runtime_resume() is the PM domain runtime_resume() callback in acpi_general_pm_domain.

Let's see. For platform bus this seems to be reasonable indeed - __rpm_get_callback() will use the callbacks of PM domain, device type, device class or device bus, whichever is available first, in this order. Looking at platform_bus_type, the equivalent resume callback is pm_generic_runtime_resume() and it will indeed silently return 0 if there is no dev->pm->runtime_resume.

However for I2C bus, i2c_bus_type doesn't seem to have .pm defined. Type and class are device/driver-specific things, so let's assume they don't have .pm set. If the driver doesn't have .pm, then the only way __rpm_get_callback() can return a non-NULL value is when dev->pm_domain is non NULL (which seems to be the ACPI case actually). Otherwise, if __rpm_get_callback() returns NULL, pm_runtime_get*() will fail with -ENOSYS. This is because rpm_resume() calls rpm_callback(callback, dev), where callback is the value returned by __rpm_get_callback() and rpm_callback() returns -ENOSYS if callback is NULL. This is where pm_runtime_no_callbacks() comes handy, as it bypasses the code getting and calling the callback, so it doesn't return the error anymore.

There is however the other side of the coin. If pm_runtime_no_callbacks() is called, all kind of callbacks are bypassed, so even the PM domain code is not invoked. This is kind of tricky, because the driver must be now aware of whether it's running under a PM domain or not and call this pm_runtime_no_callbacks() depending on the outcome, to guarantee correct operation (PM domain callbacks must be called, even if device driver doesn't have its own)... Which IMHO doesn't make sense.

I guess the correct way to proceed here would be adding .pm ops to i2c_bus_type, just as it's done with platform_bus_type.

Quoted 77 lines
Quoted 64 lines
Quoted 64 lines
Quoted 63 lines
Quoted 51 lines
Quoted 51 lines
drivers/media/i2c/dw9714.c new file +46 -0
+}
+
+/* This function sets the vcm position, so it consumes least current */
+static int dw9714_suspend(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+          val >= 0; val -= DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+     return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev)
+{
+     struct i2c_client *client = to_i2c_client(dev);
+     struct v4l2_subdev *sd = i2c_get_clientdata(client);
+     struct dw9714_device *dw9714_dev = container_of(sd,
+                                                     struct dw9714_device,
+                                                     sd);
+     int ret, val;
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+          val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+          val += DW9714_CTRL_STEPS) {
+             ret = dw9714_i2c_write(client,
+                                    DW9714_VAL((u16) val, DW9714_DEFAULT_S));
+             if (ret)
Quoted 8 lines

drivers/media/i2c/dw9714.c line 276

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+                     dev_err(dev, "%s I2C failure: %d", __func__, ret);
+             usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
+     }
+
+     /* restore v4l2 control values */
+     ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

This driver doesn't seem to be doing any physical power off in its runtime_suspend and I don't expect an I2C device to be put in a PM domain, so possibly no need for it.

Quoted 5 lines
Quoted 3 lines

I'd expect runtime PM suspend callback to power the device off through ACPI PM. For this reason the device state must be restored when it's powered on, i.e. its runtime_resume callback.

Ah, again ACPI here, that's something not usual for me. Sorry for the noise.

No harm done. Very good comments in general IMO, thanks for those!

--
Kind regards,

Sakari Ailus
e-mail: [email protected] XMPP: [email protected]

#9 lore raw
Quoted 82 lines · Tomasz Figa

+Rafael, Kevin and Ulf,

Quoted 41 lines · Sakari Ailus

Hi Tomasz,

Quoted 38 lines · Tomasz Figa
Quoted 36 lines · Sakari Ailus

Hi Tomasz,

Quoted 21 lines · Tomasz Figa

Hi Sakari,

Quoted 5 lines · Sakari Ailus

Hi Rajmohan,

A few comments below...

On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+#ifdef CONFIG_PM
+
+static int dw9714_runtime_suspend(struct device *dev)
+{
+     return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev)
+{
+     return 0;

I think it'd be fine to remove empty callbacks.

Quoted 4 lines

It's actually a bit more complicated (if a PM domain is attached, the callbacks must be present), however in case of external I2C devices it should be fine indeed. However, AFAIK, pm_runtime_no_callbacks() should be called.

I wonder if I'm missing something --- acpi_subsys_runtime_resume() first calls acpi_dev_runtime_resume() and if all goes well, the proceeds to call pm_generic_runtime_resume() which calls device's runtime_resume() if it's non-NULL.

In other words, having a runtime_resume() and runtime_suspend() callbacks that return zero is equivalent of having neither of the callbacks.

Quoted 8 lines
Quoted 3 lines

Ah, I missed the fact this device is instantiated by ACPI and it has different handling of runtime PM, which apparently means it doesn't use the code paths affected by the PM domain thing I mentioned.

I have to admit I'm no expert in the topic but I'd presume that other implementations should still maintain consistent behaviour towards drivers. acpi_subsys_runtime_resume() is the PM domain runtime_resume() callback in acpi_general_pm_domain.

Let's see. For platform bus this seems to be reasonable indeed - __rpm_get_callback() will use the callbacks of PM domain, device type, device class or device bus, whichever is available first, in this order. Looking at platform_bus_type, the equivalent resume callback is pm_generic_runtime_resume() and it will indeed silently return 0 if there is no dev->pm->runtime_resume.

However for I2C bus, i2c_bus_type doesn't seem to have .pm defined. Type and class are device/driver-specific things, so let's assume they don't have .pm set. If the driver doesn't have .pm, then the only way __rpm_get_callback() can return a non-NULL value is when dev->pm_domain is non NULL (which seems to be the ACPI case actually). Otherwise, if __rpm_get_callback() returns NULL, pm_runtime_get*() will fail with -ENOSYS. This is because rpm_resume() calls rpm_callback(callback, dev), where callback is the value returned by __rpm_get_callback() and rpm_callback() returns -ENOSYS if callback is NULL. This is where pm_runtime_no_callbacks() comes handy, as it bypasses the code getting and calling the callback, so it doesn't return the error anymore.

There is however the other side of the coin. If pm_runtime_no_callbacks() is called, all kind of callbacks are bypassed, so even the PM domain code is not invoked. This is kind of tricky, because the driver must be now aware of whether it's running under a PM domain or not and call this pm_runtime_no_callbacks() depending on the outcome, to guarantee correct operation (PM domain callbacks must be called, even if device driver doesn't have its own)... Which IMHO doesn't make sense.

Quoted 2 lines

I guess the correct way to proceed here would be adding .pm ops to i2c_bus_type, just as it's done with platform_bus_type.

Ah, sorry, I mean, the correct way to proceed here to make the behavior consistent. For Raj's patch, since a) the driver needs to restore the control value and b) it's supposed to be primarily instantiated by ACPI, we shouldn't hit the above problem for now.

Best regards,
Tomasz

#10 lore raw
In reply to Sakari Ailus

Hi Sakari,
Please see comments inline below.

Quoted 38 lines

-----Original Message-----
From: Sakari Ailus [mailto:[email protected]]
Sent: Tuesday, May 09, 2017 4:55 AM
To: Mani, Rajmohan <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] dw9714: Initial driver for dw9714 VCM

Hi Rajmohan,

A few comments below...

Quoted 25 lines · On Sun, May 07, 2017 at 04:33:24AM -0700, [email protected] wrote:

From: Rajmohan Mani <[email protected]>

DW9714 is a 10 bit DAC, designed for linear control of voice coil motor.

This driver creates a V4L2 subdevice and provides control to set the desired focus.

---
 drivers/media/i2c/Kconfig  |   9 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/dw9714.c | 333
+++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/media/i2c/dw9714.c
drivers/media/i2c/Kconfig +0 -0
index fd181c9..4f0a7ad 100644
@@ -300,6 +300,15 @@ config VIDEO_AD5820
 	  This is a driver for the AD5820 camera lens voice coil.
 	  It is used for example in Nokia N900 (RX-51).
 
Quoted 5 lines

drivers/media/i2c/Kconfig config VIDEO_AD5820 · line 305

Quoted 3 lines
drivers/media/i2c/Kconfig +3 -0
+config VIDEO_DW9714
+	tristate "DW9714 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER

You should also depend on VIDEO_V4L2_SUBDEV_API .

Ack

Quoted 135 lines
Quoted 42 lines
drivers/media/i2c/Kconfig +6 -0
+	---help---
+	  This is a driver for the DW9714 camera lens voice coil.
+	  DW9714 is a 10 bit DAC with 120mA output current sink
+	  capability. This is designed for linear control of
+	  voice coil motors, controlled via I2C serial interface.
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
drivers/media/i2c/Makefile +1 -0
index 62323ec..987bd1f 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o
 obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o
 obj-$(CONFIG_VIDEO_SAA6752HS) += saa6752hs.o
 obj-$(CONFIG_VIDEO_AD5820)  += ad5820.o
+obj-$(CONFIG_VIDEO_DW9714)  += dw9714.o
 obj-$(CONFIG_VIDEO_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o diff --git

a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c new file mode 100644 index 0000000..276d3f2

drivers/media/i2c/dw9714.c new file +15 -0
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2015--2017 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/acpi.h>

drivers/media/i2c/dw9714.c line 25

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+
+#define DW9714_NAME		"dw9714"
+#define DW9714_MAX_FOCUS_POS	1023
+#define DW9714_CTRL_STEPS	16	/* Keep this value power of 2

*/

Quoted 43 lines
drivers/media/i2c/dw9714.c new file +43 -0
+#define DW9714_CTRL_DELAY_US	1000
+/*
+ * S[3:2] = 0x00, codes per step for "Linear Slope Control"
+ * S[1:0] = 0x00, step period
+ */
+#define DW9714_DEFAULT_S 0x0
+#define DW9714_VAL(data, s) (u16)((data) << 4 | (s))
+
+/* dw9714 device structure */
+struct dw9714_device {
+	struct i2c_client *client;
+	struct v4l2_ctrl_handler ctrls_vcm;
+	struct v4l2_subdev sd;
+	u16 current_val;
+};
+
+#define to_dw9714_vcm(_ctrl)	\
+	container_of(_ctrl->handler, struct dw9714_device, ctrls_vcm)
+
+static int dw9714_i2c_write(struct i2c_client *client, u16 data) {
+	const int num_msg = 1;
+	int ret;
+	u16 val = cpu_to_be16(data);
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = 0,
+		.len = sizeof(val),
+		.buf = (u8 *) &val,
+	};
+
+	ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	/*One retry */
+	if (ret != num_msg)
+		ret = i2c_transfer(client->adapter, &msg, num_msg);
+
+	if (ret != num_msg) {
+		dev_err(&client->dev, "I2C write fail fail\n");
+		return -EIO;
+	} else {
+		return 0;
+	}
+}

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+
+static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16
+val) {
+	struct i2c_client *client = dw9714_dev->client;
+	int ret = -EINVAL;
+
+	dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
+	dw9714_dev->current_val = val;
+
+	ret = dw9714_i2c_write(client, DW9714_VAL(val,

DW9714_DEFAULT_S));

Quoted 28 lines
drivers/media/i2c/dw9714.c new file +28 -0
+	return ret;
+}
+
+static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl) {
+	struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
+
+	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
+		return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
+	else
+		return -EINVAL;
+}
+
+static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
+	.s_ctrl = dw9714_set_ctrl,
+};
+
+static int dw9714_init_controls(struct dw9714_device *dev_vcm) {
+	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
+	const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
+	struct i2c_client *client = dev_vcm->client;
+
+	v4l2_ctrl_handler_init(hdl, 1);
+
+	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
+			  0, DW9714_MAX_FOCUS_POS, 1, 0);
+
+	if (hdl->error)
+		dev_err(&client->dev, "dw9714_init_controls fail\n");
Quoted 8 lines

drivers/media/i2c/dw9714.c line 115

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+	dev_vcm->sd.ctrl_handler = hdl;
+	return hdl->error;
+}
+
+static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) {
+	v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);

Please release the control handler after unregistering the sub-device.

Ack

Quoted 11 lines

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +9 -0
+	v4l2_async_unregister_subdev(&dw9714_dev->sd);
+	v4l2_device_unregister_subdev(&dw9714_dev->sd);
+	media_entity_cleanup(&dw9714_dev->sd.entity);
+}
+
+static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh
+*fh) {
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 7 lines

drivers/media/i2c/dw9714.c line 129

Quoted 5 lines
drivers/media/i2c/dw9714.c new file +5 -0
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+	int rval;
+
+	rval = pm_runtime_get_sync(dev);

If this fails you'll need to do pm_runtime_put(dev);

Ack

Quoted 20 lines

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +9 -0
+	dev_dbg(dev, "%s rval = %d\n", __func__, rval);
+
+	return rval;
+}
+
+static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh
+*fh) {
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 9 lines
drivers/media/i2c/dw9714.c new file +9 -0
+							sd);
+	struct device *dev = &dw9714_dev->client->dev;
+
+	dev_dbg(dev, "%s\n", __func__);
+	pm_runtime_put(dev);
+
+	return 0;
+}
+
Quoted 9 lines

drivers/media/i2c/dw9714.c line 153

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
+	.open = dw9714_open,
+	.close = dw9714_close,
+};
+
+static const struct v4l2_subdev_ops dw9714_ops = { };

You should use v4l2_subdev_internal_ops here.

I am not sure I understand what you mean here. Can you please elaborate?

Quoted 42 lines
Quoted 29 lines
drivers/media/i2c/dw9714.c new file +21 -0
+
+static int dw9714_probe(struct i2c_client *client,
+			const struct i2c_device_id *devid) {
+	struct dw9714_device *dw9714_dev;
+	int rval;
+
+	dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
+				  GFP_KERNEL);
+
+	if (dw9714_dev == NULL)
+		return -ENOMEM;
+
+	dw9714_dev->client = client;
+
+	v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
+	dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	dw9714_dev->sd.internal_ops = &dw9714_int_ops;
+
+	snprintf(dw9714_dev->sd.name,
+		 sizeof(dw9714_dev->sd.name),
+		 DW9714_NAME " %d-%4.4x", i2c_adapter_id(client-

adapter),

drivers/media/i2c/dw9714.c new file +7 -0
+		 client->addr);
+
+	rval = dw9714_init_controls(dw9714_dev);
+	if (rval)
+		goto err_cleanup;
+
+	rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);

drivers/media/i2c/dw9714.c line 192

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+	if (rval < 0)
+		goto err_cleanup;
+
+	dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
+
+	rval = v4l2_async_register_subdev(&dw9714_dev->sd);
+	if (rval < 0)
+		goto err_cleanup;
+
+	pm_runtime_enable(&client->dev);

Getting PM runtime right doesn't seem to be easy. :-I

Quoted 3 lines

pm_runtime_enable() alone doesn't do the trick. I wonder if adding pm_runtime_suspend() would do the trick.

Based on the comments exchanged between you and Tomasz, I believe it is ok if we stop with just pm_runtime_enable(). P.S: Adding pm_runtime_suspend() did not result in dw9714_runtime_suspend() being invoked.

Quoted 24 lines

drivers/media/i2c/dw9714.c

Quoted 13 lines
drivers/media/i2c/dw9714.c new file +13 -0
+
+	return 0;
+
+err_cleanup:
+	dw9714_subdev_cleanup(dw9714_dev);
+	dev_err(&client->dev, "Probe failed: %d\n", rval);
+	return rval;
+}
+
+static int dw9714_remove(struct i2c_client *client) {
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+							sd);
+
+	pm_runtime_disable(&client->dev);
+	dw9714_subdev_cleanup(dw9714_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+
Quoted 8 lines

drivers/media/i2c/dw9714.c line 194

Quoted 6 lines
drivers/media/i2c/dw9714.c new file +6 -0
+static int dw9714_runtime_suspend(struct device *dev) {
+	return 0;
+}
+
+static int dw9714_runtime_resume(struct device *dev) {
+	return 0;

I think it'd be fine to remove empty callbacks.

Given that we need to do whatever that's done in dw9714_suspend/resume functions in dw9714_runtime_suspend/resume, these functions are not going to be empty callbacks.

Quoted 53 lines

drivers/media/i2c/dw9714.c

Quoted 9 lines
drivers/media/i2c/dw9714.c new file +8 -0
+}
+
+/* This function sets the vcm position, so it consumes least current
+*/ static int dw9714_suspend(struct device *dev) {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

drivers/media/i2c/dw9714.c

Quoted 9 lines
drivers/media/i2c/dw9714.c new file +9 -0
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
+	     val >= 0; val -= DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val,

DW9714_DEFAULT_S));

drivers/media/i2c/dw9714.c

Quoted 3 lines
drivers/media/i2c/dw9714.c new file +3 -0
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US,

DW9714_CTRL_DELAY_US + 10);

drivers/media/i2c/dw9714.c

Quoted 13 lines
drivers/media/i2c/dw9714.c new file +13 -0
+	}
+	return 0;
+}
+
+/*
+ * This function sets the vcm position, so the focus position is set
+ * closer to the camera
+ */
+static int dw9714_resume(struct device *dev) {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct dw9714_device *dw9714_dev = container_of(sd,
+							struct

dw9714_device,

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+							sd);
+	int ret, val;
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
+	     val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
+	     val += DW9714_CTRL_STEPS) {
+		ret = dw9714_i2c_write(client,
+				       DW9714_VAL((u16) val,

DW9714_DEFAULT_S));

drivers/media/i2c/dw9714.c

Quoted 3 lines
drivers/media/i2c/dw9714.c new file +3 -0
+		if (ret)
+			dev_err(dev, "%s I2C failure: %d", __func__, ret);
+		usleep_range(DW9714_CTRL_DELAY_US,

DW9714_CTRL_DELAY_US + 10);

Quoted 6 lines

drivers/media/i2c/dw9714.c line 276

Quoted 4 lines
drivers/media/i2c/dw9714.c new file +4 -0
+	}
+
+	/* restore v4l2 control values */
+	ret = v4l2_ctrl_handler_setup(&dw9714_dev->ctrls_vcm);

Doesn't this need to be done for runtime_resume as well?

Ack.
I will add dw9714_vcm_suspend/resume functions and invoke them from dw9714_suspend/resume and dw9714_runtime_suspend/resume functions

Quoted 60 lines
Quoted 44 lines
drivers/media/i2c/dw9714.c new file +43 -0
+	dev_dbg(dev, "%s ret = %d\n", __func__, ret);
+	return ret;
+}
+
+#else
+
+#define dw9714_suspend	NULL
+#define dw9714_resume	NULL
+#define dw9714_runtime_suspend	NULL
+#define dw9714_runtime_resume	NULL
+
+#endif /* CONFIG_PM */
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id dw9714_acpi_match[] = {
+	{"DW9714", 0},
+	{},
+};
+MODULE_DEVICE_TABLE(acpi, dw9714_acpi_match); #endif
+
+static const struct i2c_device_id dw9714_id_table[] = {
+	{DW9714_NAME, 0},
+	{}
+};
+
+MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
+
+static const struct dev_pm_ops dw9714_pm_ops = {
+	.suspend = dw9714_suspend,
+	.resume = dw9714_resume,
+	.runtime_suspend = dw9714_runtime_suspend,
+	.runtime_resume = dw9714_runtime_resume, };
+
+static struct i2c_driver dw9714_i2c_driver = {
+	.driver = {
+		.name = DW9714_NAME,
+		.pm = &dw9714_pm_ops,
+#ifdef CONFIG_ACPI
+		.acpi_match_table = ACPI_PTR(dw9714_acpi_match), #endif
+	},
+	.probe = dw9714_probe,
+	.remove = dw9714_remove,
+	.id_table = dw9714_id_table,

drivers/media/i2c/dw9714.c

Quoted 10 lines
drivers/media/i2c/dw9714.c new file +10 -0
+};
+
+module_i2c_driver(dw9714_i2c_driver);
+
+MODULE_AUTHOR("Tianshu Qiu <[email protected]>");
+MODULE_AUTHOR("Jian Xu Zheng <[email protected]>");
+MODULE_AUTHOR("Yuning Pu <[email protected]>");
+MODULE_AUTHOR("Jouni Ukkonen <[email protected]>");
+MODULE_AUTHOR("Tommi Franttila <[email protected]>");
+MODULE_DESCRIPTION("DW9714 VCM driver"); MODULE_LICENSE("GPL");

--
Regards,

Sakari Ailus
e-mail: [email protected] XMPP: [email protected]