Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-10 Thread Tomasz Figa
Hi Raj,

On Thu, May 11, 2017 at 12:12 PM, Mani, Rajmohan
 wrote:
> Hi Tomasz,
> Thanks for the reviews. Please see comments inline.
>
>> -Original Message-
>> From: Tomasz Figa [mailto:tf...@chromium.org]
>> Sent: Tuesday, May 09, 2017 4:44 PM
>> To: Mani, Rajmohan 
>> Cc: linux-media@vger.kernel.org; mche...@kernel.org; Hans Verkuil
>> 
>> Subject: Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM
>>
>> Hi Rajmohan,
>>
>> Some comments below.
>>
>> On Mon, May 8, 2017 at 10:36 PM, Rajmohan Mani
>>  wrote:
>> > 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.
>> >
>> > Signed-off-by: Rajmohan Mani 
>> > ---
>> > Changes in v2:
>> > - Addressed review comments from Hans Verkuil
>> > - Fixed a debug message typo
>> > - Got rid of a return variable
>> > ---
>> >  drivers/media/i2c/Kconfig  |   9 ++
>> >  drivers/media/i2c/Makefile |   1 +
>> >  drivers/media/i2c/dw9714.c | 320
>> > +
>> >  3 files changed, 330 insertions(+)
>> >  create mode 100644 drivers/media/i2c/dw9714.c
>> [snip]
>> > diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c
>> > new file mode 100644 index 000..cd6cde7
>> > --- /dev/null
>> > +++ b/drivers/media/i2c/dw9714.c
>> > @@ -0,0 +1,320 @@
>> > +/*
>> > + * 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 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +
>> > +#define DW9714_NAME"dw9714"
>> > +#define DW9714_MAX_FOCUS_POS   1023
>> > +#define DW9714_CTRL_STEPS  16  /* Keep this value power of 2 */
>>
>> Because?
>>
>
> This acts as the minimum granularity of lens movement.
> Keep this value power of 2, so the control steps can be uniformly adjusted 
> for gradual lens movement, with desired number of control steps.

I mean, the comment should explain the reason.

>
>> > +#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))
>>
>> Do we need this cast?
>>
> Yes. This is a write to a 2 byte register

Still, I'm not sure what this cast really gives us. If we want strict
type checking we should make this a static inline that has all types
specified, but that's probably not necessary either.

Best regards,
Tomasz


RE: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-10 Thread Mani, Rajmohan
Hi Tomasz,
Thanks for the reviews. Please see comments inline.

> -Original Message-
> From: Tomasz Figa [mailto:tf...@chromium.org]
> Sent: Tuesday, May 09, 2017 4:44 PM
> To: Mani, Rajmohan 
> Cc: linux-media@vger.kernel.org; mche...@kernel.org; Hans Verkuil
> 
> Subject: Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM
> 
> Hi Rajmohan,
> 
> Some comments below.
> 
> On Mon, May 8, 2017 at 10:36 PM, Rajmohan Mani
>  wrote:
> > 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.
> >
> > Signed-off-by: Rajmohan Mani 
> > ---
> > Changes in v2:
> > - Addressed review comments from Hans Verkuil
> > - Fixed a debug message typo
> > - Got rid of a return variable
> > ---
> >  drivers/media/i2c/Kconfig  |   9 ++
> >  drivers/media/i2c/Makefile |   1 +
> >  drivers/media/i2c/dw9714.c | 320
> > +
> >  3 files changed, 330 insertions(+)
> >  create mode 100644 drivers/media/i2c/dw9714.c
> [snip]
> > diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c
> > new file mode 100644 index 000..cd6cde7
> > --- /dev/null
> > +++ b/drivers/media/i2c/dw9714.c
> > @@ -0,0 +1,320 @@
> > +/*
> > + * 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 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#define DW9714_NAME"dw9714"
> > +#define DW9714_MAX_FOCUS_POS   1023
> > +#define DW9714_CTRL_STEPS  16  /* Keep this value power of 2 */
> 
> Because?
> 

This acts as the minimum granularity of lens movement.
Keep this value power of 2, so the control steps can be uniformly adjusted for 
gradual lens movement, with desired number of control steps.

> > +#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))
> 
> Do we need this cast?
> 
Yes. This is a write to a 2 byte register

> > +
> > +/* 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)
> 
> Please use a static inline function for type safety.
> 

Ack

> > +
> > +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\n");
> > +   return -EIO;
> > +   }
> 
> I believe i2c_master_send() would simplify this function significantly.
> 

Ack

> > +   return 0;
> > +}
> > +
> > +static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16
> > +val) {
> > +   struct i2c_client *client = dw9714_dev->client;
> > +
> > +   dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
> > +   dw9714_dev->current_val = val;
> > 

Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-09 Thread Sakari Ailus
Hi Tomasz,

On Tue, May 09, 2017 at 04:44:13PM +0800, Tomasz Figa wrote:
...
> > +/* 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);
> 
> Is it necessary to change the value in such steps? If so, shouldn't
> the control handler behave in the same way, making sure that userspace
> does not request changes in bigger steps?

I believe the reason for this is to gradually move the lens to the resting
position in order to avoid an audible click that results from the lens
hitting the mechanical stopper.

That said, this chip should have ringing compensation so I wonder if
gradually setting the desired value necessary. I let Rajmohan to comment on
that.

What still might be needed is to delay powering the device off by a small
amount of time.

-- 
Regards,

Sakari Ailus
e-mail: sakari.ai...@iki.fi XMPP: sai...@retiisi.org.uk


Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-09 Thread Tomasz Figa
Hi Rajmohan,

Some comments below.

On Mon, May 8, 2017 at 10:36 PM, Rajmohan Mani  wrote:
> 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.
>
> Signed-off-by: Rajmohan Mani 
> ---
> Changes in v2:
> - Addressed review comments from Hans Verkuil
> - Fixed a debug message typo
> - Got rid of a return variable
> ---
>  drivers/media/i2c/Kconfig  |   9 ++
>  drivers/media/i2c/Makefile |   1 +
>  drivers/media/i2c/dw9714.c | 320 
> +
>  3 files changed, 330 insertions(+)
>  create mode 100644 drivers/media/i2c/dw9714.c
[snip]
> diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c
> new file mode 100644
> index 000..cd6cde7
> --- /dev/null
> +++ b/drivers/media/i2c/dw9714.c
> @@ -0,0 +1,320 @@
> +/*
> + * 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 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define DW9714_NAME"dw9714"
> +#define DW9714_MAX_FOCUS_POS   1023
> +#define DW9714_CTRL_STEPS  16  /* Keep this value power of 2 */

Because?

> +#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))

Do we need this cast?

> +
> +/* 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)

Please use a static inline function for type safety.

> +
> +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\n");
> +   return -EIO;
> +   }

I believe i2c_master_send() would simplify this function significantly.

> +   return 0;
> +}
> +
> +static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
> +{
> +   struct i2c_client *client = dw9714_dev->client;
> +
> +   dev_dbg(&client->dev, "Setting new value VCM: %d\n", val);
> +   dw9714_dev->current_val = val;
> +
> +   return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
> +}
> +
> +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);
> +
> +   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);
> +}
> +
> +static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
> +{
> +   struct dw9714_device *dw9714_dev = container_of(sd,
> +   stru

RE: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-09 Thread Mani, Rajmohan
Hi Sakari,

> -Original Message-
> From: Sakari Ailus [mailto:sakari.ai...@iki.fi]
> Sent: Tuesday, May 09, 2017 4:56 AM
> To: Mani, Rajmohan 
> Cc: linux-media@vger.kernel.org; mche...@kernel.org; hverk...@xs4all.nl
> Subject: Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM
> 
> On Mon, May 08, 2017 at 07:36:48AM -0700, Rajmohan Mani wrote:
> > +   dev_dbg(dev, "%s ret = %d\n", __func__, ret);
> 
> Please remove such debug prints.

I have removed all dev_dbg prints and this will be addressed in v3 of this 
patch.
> 
> --
> Sakari Ailus
> e-mail: sakari.ai...@iki.fi   XMPP: sai...@retiisi.org.uk


Re: [PATCH v2] dw9714: Initial driver for dw9714 VCM

2017-05-08 Thread Sakari Ailus
On Mon, May 08, 2017 at 07:36:48AM -0700, Rajmohan Mani wrote:
> + dev_dbg(dev, "%s ret = %d\n", __func__, ret);

Please remove such debug prints.

-- 
Sakari Ailus
e-mail: sakari.ai...@iki.fi XMPP: sai...@retiisi.org.uk