Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1420069 > unrolled thread

Re: [PATCH] userspace API definitions for auto-focus coil

Started bySakari Ailus <sakari.ailus@iki.fi>
First post2016-06-12 00:10 +0200
Last post2016-06-18 17:40 +0200
Articles 9 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [PATCH] userspace API definitions for auto-focus coil Sakari Ailus <sakari.ailus@iki.fi> - 2016-06-12 00:10 +0200
    Re: [PATCH] userspace API definitions for auto-focus coil Pavel Machek <pavel@ucw.cz> - 2016-06-12 10:00 +0200
      Re: [PATCH] userspace API definitions for auto-focus coil Sakari Ailus <sakari.ailus@iki.fi> - 2016-06-17 23:30 +0200
    Re: [PATCH] userspace API definitions for auto-focus coil Pavel Machek <pavel@ucw.cz> - 2016-06-12 10:50 +0200
      Re: [PATCH] userspace API definitions for auto-focus coil Sakari Ailus <sakari.ailus@iki.fi> - 2016-06-12 13:30 +0200
        Re: [PATCH] userspace API definitions for auto-focus coil Pavel Machek <pavel@ucw.cz> - 2016-06-13 21:20 +0200
          Re: [PATCH] userspace API definitions for auto-focus coil Sakari Ailus <sakari.ailus@iki.fi> - 2016-06-17 23:40 +0200
            [PATCHv4] support for AD5820 camera auto-focus coil Pavel Machek <pavel@ucw.cz> - 2016-06-18 17:40 +0200
            Re: [PATCH] userspace API definitions for auto-focus coil Pavel Machek <pavel@ucw.cz> - 2016-06-18 17:40 +0200

#1420069 — Re: [PATCH] userspace API definitions for auto-focus coil

FromSakari Ailus <sakari.ailus@iki.fi>
Date2016-06-12 00:10 +0200
SubjectRe: [PATCH] userspace API definitions for auto-focus coil
Message-ID<rIZgl-hG-1@gated-at.bofh.it>
Hi Ivaylo,

On Mon, Jun 06, 2016 at 09:06:29AM +0300, Ivaylo Dimitrov wrote:
> Hi,
> 
> On  5.06.2016 22:07, Pavel Machek wrote:
> >Add userspace API definitions.
> >
> >Signed-off-by: Pavel Machek <pavel@ucw.cz>
> >
> >diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> >index b6a357a..23011cc 100644
> >--- a/include/uapi/linux/v4l2-controls.h
> >+++ b/include/uapi/linux/v4l2-controls.h
> >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> >
> >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)

Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
nowadays.

> >+#define V4L2_CID_FOCUS_AD5820_RAMP_TIME	(V4L2_CID_FOCUS_AD5820_BASE+0)
> >+#define V4L2_CID_FOCUS_AD5820_RAMP_MODE	(V4L2_CID_FOCUS_AD5820_BASE+1)
> >+
> >  #endif
> >
> 
> Sakari, what about adding those as standard camera controls? It seems ad5820
> is not the only VCM driver to implement "antiringing" controls, http://rohmfs.rohm.com/en/products/databook/datasheet/ic/motor/mobile_module/bu64241gwz-e.pdf
> is another example I found by quick search.

These controls however seem to be related to some thing else --- configuring
the driver to drive the lens little by little to the target using a
pre-defined step time and size. I presume it is intended for something else,
most likely for performing a full search for a regular AF algorithm. I also
wonder how much this functionality is used nowadays, most devices use
continuous AF algorithm that has little or no use for such. It also provides
no help in synchronising lens movement to exposure of the AF window.

Now that I think about this, the original implementation in N900 very likely
did not use either of the two controls; the device driver was still written
to provide access to full capabilities of the chip. And that one had no
continuous AF.

> What about:
> 
> #define V4L2_CID_FOCUS_STEP_MODE xxx
> enum v4l2_cid_focus_step_mode {
> V4L2_CID_FOCUS_STEP_MODE_DIRECT,
> V4L2_CID_FOCUS_STEP_MODE_LINEAR,
> V4L2_CID_FOCUS_STEP_MODE_AUTO
> };
> #define V4L2_CID_FOCUS_STEP_TIME xxx+1
> 
> Also, how the userspace(or the kernel) is notified by v4l that there is an
> event? The point is - I think it is a good idea to notify when VCM has
> completed its movement, we can start a timer based on the current position,
> mode, step time etc and notify after the pre-calculated movement time.

You'd have to start modelling the movement of the lens somehow. And for
that, we'd need to know about the lens and the spring in the kernel, too. I
presume that's already a lot of the algorithm to get the lens moving, and
supposing this is in the kernel, what else would go to the kernel?

These device provide very basic functionality for moving the lens; current
is applied on a coil and that has the effect of moving the lens, bringing
the focus distance closer as more current is applied but that's pretty much
it: there's no reliable way to focus at a particular distance for example.

I might as well drop the two controls, up to you. If someone ever needs them
they can always be reintroduced. I'd be happy to get a new patch, the
current driver patch does not compile (just tried) as the definitions of
these controls are missing.

Ringing compensation functionality present in the other chip should be far
more useful.

If there are AF experts reading this, feel free to challenge me. :-) I can't
claim to be one.

-- 
Kind regards,

Sakari Ailus
e-mail: sakari.ailus@iki.fi	XMPP: sailus@retiisi.org.uk

[toc] | [next] | [standalone]


#1420198

FromPavel Machek <pavel@ucw.cz>
Date2016-06-12 10:00 +0200
Message-ID<rJ8tk-5RI-11@gated-at.bofh.it>
In reply to#1420069
Hi!

> > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > >
> > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> 
> Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> nowadays.

Let me see...

> Now that I think about this, the original implementation in N900 very likely
> did not use either of the two controls; the device driver was still written
> to provide access to full capabilities of the chip. And that one had no
> continuous AF.

I'm not sure about the original implementation, but fcam-dev library
(which is our best chance for usable camera) does use both:

pavel@duo:~/g/fcam-dev$ grep -ri RAMP_TIME .
./.svn/pristine/05/0574680922f59e07bd49e16a951d69421690a323.svn-base:
int val = ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_TIME,
1000000.0f/diopterRateToTickRate(speed));
./src/N900/Lens.cpp:    int val =
ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_TIME,
1000000.0f/diopterRateToTickRate(speed));
pavel@duo:~/g/fcam-dev$ grep -ri RAMP_MODE .
./.svn/pristine/05/0574680922f59e07bd49e16a951d69421690a323.svn-base:
ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_MODE, 0);
./src/N900/Lens.cpp:    ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_MODE, 0);
pavel@duo:~/g/fcam-dev$

> I might as well drop the two controls, up to you. If someone ever needs them
> they can always be reintroduced. I'd be happy to get a new patch, the
> current driver patch does not compile (just tried) as the definitions of
> these controls are missing.

I'd prefer to keep the controls, as we have userspace using them. I
got it to compile but have yet to get it to work (subdevs split, so it
needs some modifications).

Best regards,
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[toc] | [prev] | [next] | [standalone]


#1425491

FromSakari Ailus <sakari.ailus@iki.fi>
Date2016-06-17 23:30 +0200
Message-ID<rL9uV-3qd-11@gated-at.bofh.it>
In reply to#1420198
Hi Pavel,

On Sun, Jun 12, 2016 at 09:54:17AM +0200, Pavel Machek wrote:
> Hi!
> 
> > > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > > >
> > > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> > 
> > Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> > nowadays.
> 
> Let me see...
> 
> > Now that I think about this, the original implementation in N900 very likely
> > did not use either of the two controls; the device driver was still written
> > to provide access to full capabilities of the chip. And that one had no
> > continuous AF.
> 
> I'm not sure about the original implementation, but fcam-dev library
> (which is our best chance for usable camera) does use both:
> 
> pavel@duo:~/g/fcam-dev$ grep -ri RAMP_TIME .
> ./.svn/pristine/05/0574680922f59e07bd49e16a951d69421690a323.svn-base:
> int val = ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_TIME,
> 1000000.0f/diopterRateToTickRate(speed));
> ./src/N900/Lens.cpp:    int val =
> ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_TIME,
> 1000000.0f/diopterRateToTickRate(speed));
> pavel@duo:~/g/fcam-dev$ grep -ri RAMP_MODE .
> ./.svn/pristine/05/0574680922f59e07bd49e16a951d69421690a323.svn-base:
> ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_MODE, 0);
> ./src/N900/Lens.cpp:    ioctlSet(V4L2_CID_FOCUS_AD5820_RAMP_MODE, 0);
> pavel@duo:~/g/fcam-dev$
> 
> > I might as well drop the two controls, up to you. If someone ever needs them
> > they can always be reintroduced. I'd be happy to get a new patch, the
> > current driver patch does not compile (just tried) as the definitions of
> > these controls are missing.
> 
> I'd prefer to keep the controls, as we have userspace using them. I
> got it to compile but have yet to get it to work (subdevs split, so it
> needs some modifications).

Right. I didn't know Fcam used them. Still, using them is hardly an optimal
way to control the lens (as far as camera functionality goes, using them
requires less CPU time consumption though).

The flash control patches should receive a proper RFC that discusses the
problem area and proposes a solution. I'll write one in the near future.

I think that for this particular controller it's relatively clear though: it
provides very basic functionality that maps well to controls that I don't
really see alternative options for that.

I don't object defining standard controls for ramp mode nor time either. But
I expect you to write a patch in that case. :-)

-- 
Regards,

Sakari Ailus
e-mail: sakari.ailus@iki.fi	XMPP: sailus@retiisi.org.uk

[toc] | [prev] | [next] | [standalone]


#1420201

FromPavel Machek <pavel@ucw.cz>
Date2016-06-12 10:50 +0200
Message-ID<rJ9fH-6oM-3@gated-at.bofh.it>
In reply to#1420069
Hi!

> > >Add userspace API definitions.
> > >
> > >Signed-off-by: Pavel Machek <pavel@ucw.cz>
> > >
> > >diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> > >index b6a357a..23011cc 100644
> > >--- a/include/uapi/linux/v4l2-controls.h
> > >+++ b/include/uapi/linux/v4l2-controls.h
> > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > >
> > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> 
> Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> nowadays.

So something like this?

Thanks,
									Pavel

diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
index 2efa5dc1..b04b471 100644
--- a/drivers/media/i2c/ad5820.c
+++ b/drivers/media/i2c/ad5820.c
@@ -40,6 +40,11 @@
 #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
 #define AD5820_RAMP_MODE_64_16		(1 << 3)
 
+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
+#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_USER_AD5820_BASE+0)
+#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
+
+
 #define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
 #define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
 
diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index 23011cc..4b24546 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -181,6 +181,10 @@ enum v4l2_colorfx {
  * We reserve 16 controls for this driver. */
 #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
 
+/* The base for the ad5820 driver controls.
+ * We reserve 16 controls for this driver. */
+#define V4L2_CID_USER_AD5820_BASE		(V4L2_CID_USER_BASE + 0x1090)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */
@@ -986,9 +990,4 @@ enum v4l2_detect_md_mode {
 #define V4L2_CID_MODE_SENSITIVITY		(V4L2_CID_MODE_CLASS_BASE+6)
 #define V4L2_CID_MODE_OPSYSCLOCK		(V4L2_CID_MODE_CLASS_BASE+7)
 
-/* Control IDs specific to the AD5820 driver as defined by V4L2 */
-#define V4L2_CID_FOCUS_AD5820_BASE 		(V4L2_CTRL_CLASS_CAMERA | 0x10af)
-#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_FOCUS_AD5820_BASE+0)
-#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
-
 #endif


-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[toc] | [prev] | [next] | [standalone]


#1420236

FromSakari Ailus <sakari.ailus@iki.fi>
Date2016-06-12 13:30 +0200
Message-ID<rJbKy-825-17@gated-at.bofh.it>
In reply to#1420201
Hi Pavel,

On Sun, Jun 12, 2016 at 10:48:11AM +0200, Pavel Machek wrote:
> Hi!
> 
> > > >Add userspace API definitions.
> > > >
> > > >Signed-off-by: Pavel Machek <pavel@ucw.cz>
> > > >
> > > >diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> > > >index b6a357a..23011cc 100644
> > > >--- a/include/uapi/linux/v4l2-controls.h
> > > >+++ b/include/uapi/linux/v4l2-controls.h
> > > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > > >
> > > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> > 
> > Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> > nowadays.
> 
> So something like this?
> 
> Thanks,
> 									Pavel
> 
> diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
> index 2efa5dc1..b04b471 100644
> --- a/drivers/media/i2c/ad5820.c
> +++ b/drivers/media/i2c/ad5820.c
> @@ -40,6 +40,11 @@
>  #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
>  #define AD5820_RAMP_MODE_64_16		(1 << 3)
>  
> +/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> +#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_USER_AD5820_BASE+0)
> +#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
> +
> +

We could still define these in a header file that can be included by the
user space. Please use V4L2_CID_AD5820 prefix.

A separate header file should be used, e.g. include/uapi/linux/ad5820.h.

>  #define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
>  #define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
>  
> diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> index 23011cc..4b24546 100644
> --- a/include/uapi/linux/v4l2-controls.h
> +++ b/include/uapi/linux/v4l2-controls.h
> @@ -181,6 +181,10 @@ enum v4l2_colorfx {
>   * We reserve 16 controls for this driver. */
>  #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
>  
> +/* The base for the ad5820 driver controls.
> + * We reserve 16 controls for this driver. */
> +#define V4L2_CID_USER_AD5820_BASE		(V4L2_CID_USER_BASE + 0x1090)
> +
>  /* MPEG-class control IDs */
>  /* The MPEG controls are applicable to all codec controls
>   * and the 'MPEG' part of the define is historical */
> @@ -986,9 +990,4 @@ enum v4l2_detect_md_mode {
>  #define V4L2_CID_MODE_SENSITIVITY		(V4L2_CID_MODE_CLASS_BASE+6)
>  #define V4L2_CID_MODE_OPSYSCLOCK		(V4L2_CID_MODE_CLASS_BASE+7)
>  
> -/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> -#define V4L2_CID_FOCUS_AD5820_BASE 		(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> -#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_FOCUS_AD5820_BASE+0)
> -#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
> -
>  #endif
> 
> 

-- 
Kind regards,

Sakari Ailus
e-mail: sakari.ailus@iki.fi	XMPP: sailus@retiisi.org.uk

[toc] | [prev] | [next] | [standalone]


#1421237

FromPavel Machek <pavel@ucw.cz>
Date2016-06-13 21:20 +0200
Message-ID<rJFyX-26n-57@gated-at.bofh.it>
In reply to#1420236
On Sun 2016-06-12 14:22:53, Sakari Ailus wrote:
> Hi Pavel,
> 
> On Sun, Jun 12, 2016 at 10:48:11AM +0200, Pavel Machek wrote:
> > Hi!
> > 
> > > > >Add userspace API definitions.
> > > > >
> > > > >Signed-off-by: Pavel Machek <pavel@ucw.cz>
> > > > >
> > > > >diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> > > > >index b6a357a..23011cc 100644
> > > > >--- a/include/uapi/linux/v4l2-controls.h
> > > > >+++ b/include/uapi/linux/v4l2-controls.h
> > > > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > > > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > > > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > > > >
> > > > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > > > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> > > 
> > > Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> > > nowadays.
> > 
> > So something like this?
> > 
> > Thanks,
> > 									Pavel
> > 
> > diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
> > index 2efa5dc1..b04b471 100644
> > --- a/drivers/media/i2c/ad5820.c
> > +++ b/drivers/media/i2c/ad5820.c
> > @@ -40,6 +40,11 @@
> >  #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
> >  #define AD5820_RAMP_MODE_64_16		(1 << 3)
> >  
> > +/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > +#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_USER_AD5820_BASE+0)
> > +#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
> > +
> > +
> 
> We could still define these in a header file that can be included by the
> user space. Please use V4L2_CID_AD5820 prefix.

Not V4L2_CID_USER_AD5820...?

> A separate header file should be used,
>  e.g. include/uapi/linux/ad5820.h.

Ok, separate header file for 2 lines seemed like a bit of overkill,
but why not.

Something like this?

commit 8dd701d2580e41b06bb2285e6bd59a4f1702b4d8
Author: Pavel <pavel@ucw.cz>
Date:   Mon Jun 13 21:17:15 2016 +0200

    Userspace api, as Sakari asked.

diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
index 2efa5dc1..ed3facc 100644
--- a/drivers/media/i2c/ad5820.c
+++ b/drivers/media/i2c/ad5820.c
@@ -32,6 +32,8 @@
 #include <media/v4l2-device.h>
 #include <media/v4l2-subdev.h>
 
+#include <uapi/linux/ad5820.h>
+
 #define AD5820_NAME		"ad5820"
 
 /* Register definitions */
@@ -40,6 +42,8 @@
 #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
 #define AD5820_RAMP_MODE_64_16		(1 << 3)
 
+
+
 #define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
 #define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
 
@@ -165,13 +169,13 @@ static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
 		coil->focus_absolute = ctrl->val;
 		return ad5820_update_hw(coil);
 
-	case V4L2_CID_FOCUS_AD5820_RAMP_TIME:
+	case V4L2_CID_AD5820_RAMP_TIME:
 		code = RAMP_US_TO_CODE(ctrl->val);
 		ctrl->val = CODE_TO_RAMP_US(code);
 		coil->focus_ramp_time = ctrl->val;
 		break;
 
-	case V4L2_CID_FOCUS_AD5820_RAMP_MODE:
+	case V4L2_CID_AD5820_RAMP_MODE:
 		coil->focus_ramp_mode = ctrl->val;
 		break;
 	}
@@ -191,7 +195,7 @@ static const char * const ad5820_focus_menu[] = {
 static const struct v4l2_ctrl_config ad5820_ctrls[] = {
 	{
 		.ops		= &ad5820_ctrl_ops,
-		.id		= V4L2_CID_FOCUS_AD5820_RAMP_TIME,
+		.id		= V4L2_CID_AD5820_RAMP_TIME,
 		.type		= V4L2_CTRL_TYPE_INTEGER,
 		.name		= "Focus ramping time [us]",
 		.min		= 0,
@@ -202,7 +206,7 @@ static const struct v4l2_ctrl_config ad5820_ctrls[] = {
 	},
 	{
 		.ops		= &ad5820_ctrl_ops,
-		.id		= V4L2_CID_FOCUS_AD5820_RAMP_MODE,
+		.id		= V4L2_CID_AD5820_RAMP_MODE,
 		.type		= V4L2_CTRL_TYPE_MENU,
 		.name		= "Focus ramping mode",
 		.min		= 0,
diff --git a/include/uapi/linux/ad5820.h b/include/uapi/linux/ad5820.h
new file mode 100644
index 0000000..d44ba9c
--- /dev/null
+++ b/include/uapi/linux/ad5820.h
@@ -0,0 +1,18 @@
+/*
+ * AD5820 DAC driver for camera voice coil focus.
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_AD5820_H
+#define __LINUX_AD5820_H
+
+#include <linux/v4l2-controls.h>
+
+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
+#define V4L2_CID_AD5820_RAMP_TIME	(V4L2_CID_AD5820_BASE+0)
+#define V4L2_CID_AD5820_RAMP_MODE	(V4L2_CID_AD5820_BASE+1)
+
+#endif
diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index 23011cc..015e90b 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -181,6 +181,10 @@ enum v4l2_colorfx {
  * We reserve 16 controls for this driver. */
 #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
 
+/* The base for the ad5820 driver controls.
+ * We reserve 16 controls for this driver. */
+#define V4L2_CID_AD5820_BASE			(V4L2_CID_USER_BASE + 0x1090)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */
@@ -986,9 +990,4 @@ enum v4l2_detect_md_mode {
 #define V4L2_CID_MODE_SENSITIVITY		(V4L2_CID_MODE_CLASS_BASE+6)
 #define V4L2_CID_MODE_OPSYSCLOCK		(V4L2_CID_MODE_CLASS_BASE+7)
 
-/* Control IDs specific to the AD5820 driver as defined by V4L2 */
-#define V4L2_CID_FOCUS_AD5820_BASE 		(V4L2_CTRL_CLASS_CAMERA | 0x10af)
-#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_FOCUS_AD5820_BASE+0)
-#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
-
 #endif


-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[toc] | [prev] | [next] | [standalone]


#1425501

FromSakari Ailus <sakari.ailus@iki.fi>
Date2016-06-17 23:40 +0200
Message-ID<rL9EC-3tj-15@gated-at.bofh.it>
In reply to#1421237
Hi Pavel,

On Mon, Jun 13, 2016 at 09:17:53PM +0200, Pavel Machek wrote:
> On Sun 2016-06-12 14:22:53, Sakari Ailus wrote:
> > Hi Pavel,
> > 
> > On Sun, Jun 12, 2016 at 10:48:11AM +0200, Pavel Machek wrote:
> > > Hi!
> > > 
> > > > > >Add userspace API definitions.
> > > > > >
> > > > > >Signed-off-by: Pavel Machek <pavel@ucw.cz>
> > > > > >
> > > > > >diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> > > > > >index b6a357a..23011cc 100644
> > > > > >--- a/include/uapi/linux/v4l2-controls.h
> > > > > >+++ b/include/uapi/linux/v4l2-controls.h
> > > > > >@@ -974,4 +975,9 @@ enum v4l2_detect_md_mode {
> > > > > >  #define V4L2_CID_DETECT_MD_THRESHOLD_GRID	(V4L2_CID_DETECT_CLASS_BASE + 3)
> > > > > >  #define V4L2_CID_DETECT_MD_REGION_GRID		(V4L2_CID_DETECT_CLASS_BASE + 4)
> > > > > >
> > > > > >+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > > > > >+#define V4L2_CID_FOCUS_AD5820_BASE 	(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> > > > 
> > > > Please check V4L2_CID_USER_*_BASE. That's how custom controls are handled
> > > > nowadays.
> > > 
> > > So something like this?
> > > 
> > > Thanks,
> > > 									Pavel
> > > 
> > > diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
> > > index 2efa5dc1..b04b471 100644
> > > --- a/drivers/media/i2c/ad5820.c
> > > +++ b/drivers/media/i2c/ad5820.c
> > > @@ -40,6 +40,11 @@
> > >  #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
> > >  #define AD5820_RAMP_MODE_64_16		(1 << 3)
> > >  
> > > +/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> > > +#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_USER_AD5820_BASE+0)
> > > +#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
> > > +
> > > +
> > 
> > We could still define these in a header file that can be included by the
> > user space. Please use V4L2_CID_AD5820 prefix.
> 
> Not V4L2_CID_USER_AD5820...?

The rest of the controls have no USER as part of the macro name, so I
wouldn't use it here either.

> 
> > A separate header file should be used,
> >  e.g. include/uapi/linux/ad5820.h.
> 
> Ok, separate header file for 2 lines seemed like a bit of overkill,
> but why not.

That follows an existing pattern of how controls have been implemented in
other drivers.

> 
> Something like this?

Yes. A few minor comments below.

Could you merge this with the driver patch? I've dropped that from my ad5820
branch as it does not compile.

> 
> commit 8dd701d2580e41b06bb2285e6bd59a4f1702b4d8
> Author: Pavel <pavel@ucw.cz>
> Date:   Mon Jun 13 21:17:15 2016 +0200
> 
>     Userspace api, as Sakari asked.
> 
> diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
> index 2efa5dc1..ed3facc 100644
> --- a/drivers/media/i2c/ad5820.c
> +++ b/drivers/media/i2c/ad5820.c
> @@ -32,6 +32,8 @@
>  #include <media/v4l2-device.h>
>  #include <media/v4l2-subdev.h>
>  
> +#include <uapi/linux/ad5820.h>
> +
>  #define AD5820_NAME		"ad5820"
>  
>  /* Register definitions */
> @@ -40,6 +42,8 @@
>  #define AD5820_RAMP_MODE_LINEAR		(0 << 3)
>  #define AD5820_RAMP_MODE_64_16		(1 << 3)
>  
> +
> +

Extra newlines.

>  #define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
>  #define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
>  
> @@ -165,13 +169,13 @@ static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
>  		coil->focus_absolute = ctrl->val;
>  		return ad5820_update_hw(coil);
>  
> -	case V4L2_CID_FOCUS_AD5820_RAMP_TIME:
> +	case V4L2_CID_AD5820_RAMP_TIME:
>  		code = RAMP_US_TO_CODE(ctrl->val);
>  		ctrl->val = CODE_TO_RAMP_US(code);
>  		coil->focus_ramp_time = ctrl->val;
>  		break;
>  
> -	case V4L2_CID_FOCUS_AD5820_RAMP_MODE:
> +	case V4L2_CID_AD5820_RAMP_MODE:
>  		coil->focus_ramp_mode = ctrl->val;
>  		break;
>  	}
> @@ -191,7 +195,7 @@ static const char * const ad5820_focus_menu[] = {
>  static const struct v4l2_ctrl_config ad5820_ctrls[] = {
>  	{
>  		.ops		= &ad5820_ctrl_ops,
> -		.id		= V4L2_CID_FOCUS_AD5820_RAMP_TIME,
> +		.id		= V4L2_CID_AD5820_RAMP_TIME,
>  		.type		= V4L2_CTRL_TYPE_INTEGER,
>  		.name		= "Focus ramping time [us]",
>  		.min		= 0,
> @@ -202,7 +206,7 @@ static const struct v4l2_ctrl_config ad5820_ctrls[] = {
>  	},
>  	{
>  		.ops		= &ad5820_ctrl_ops,
> -		.id		= V4L2_CID_FOCUS_AD5820_RAMP_MODE,
> +		.id		= V4L2_CID_AD5820_RAMP_MODE,
>  		.type		= V4L2_CTRL_TYPE_MENU,
>  		.name		= "Focus ramping mode",
>  		.min		= 0,
> diff --git a/include/uapi/linux/ad5820.h b/include/uapi/linux/ad5820.h
> new file mode 100644
> index 0000000..d44ba9c
> --- /dev/null
> +++ b/include/uapi/linux/ad5820.h
> @@ -0,0 +1,18 @@
> +/*
> + * AD5820 DAC driver for camera voice coil focus.
> + *
> + * 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.
> + */
> +
> +#ifndef __LINUX_AD5820_H
> +#define __LINUX_AD5820_H
> +
> +#include <linux/v4l2-controls.h>
> +
> +/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> +#define V4L2_CID_AD5820_RAMP_TIME	(V4L2_CID_AD5820_BASE+0)

(V4L2_CID_AD5820_BASE + 0)

Same below.

> +#define V4L2_CID_AD5820_RAMP_MODE	(V4L2_CID_AD5820_BASE+1)
> +
> +#endif
> diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
> index 23011cc..015e90b 100644
> --- a/include/uapi/linux/v4l2-controls.h
> +++ b/include/uapi/linux/v4l2-controls.h
> @@ -181,6 +181,10 @@ enum v4l2_colorfx {
>   * We reserve 16 controls for this driver. */
>  #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
>  
> +/* The base for the ad5820 driver controls.
> + * We reserve 16 controls for this driver. */
> +#define V4L2_CID_AD5820_BASE			(V4L2_CID_USER_BASE + 0x1090)
> +
>  /* MPEG-class control IDs */
>  /* The MPEG controls are applicable to all codec controls
>   * and the 'MPEG' part of the define is historical */
> @@ -986,9 +990,4 @@ enum v4l2_detect_md_mode {
>  #define V4L2_CID_MODE_SENSITIVITY		(V4L2_CID_MODE_CLASS_BASE+6)
>  #define V4L2_CID_MODE_OPSYSCLOCK		(V4L2_CID_MODE_CLASS_BASE+7)
>  
> -/* Control IDs specific to the AD5820 driver as defined by V4L2 */
> -#define V4L2_CID_FOCUS_AD5820_BASE 		(V4L2_CTRL_CLASS_CAMERA | 0x10af)
> -#define V4L2_CID_FOCUS_AD5820_RAMP_TIME		(V4L2_CID_FOCUS_AD5820_BASE+0)
> -#define V4L2_CID_FOCUS_AD5820_RAMP_MODE		(V4L2_CID_FOCUS_AD5820_BASE+1)
> -
>  #endif
> 
> 

-- 
Regards,

Sakari Ailus
e-mail: sakari.ailus@iki.fi	XMPP: sailus@retiisi.org.uk

[toc] | [prev] | [next] | [standalone]


#1425750 — [PATCHv4] support for AD5820 camera auto-focus coil

FromPavel Machek <pavel@ucw.cz>
Date2016-06-18 17:40 +0200
Subject[PATCHv4] support for AD5820 camera auto-focus coil
Message-ID<rLqvL-5Po-3@gated-at.bofh.it>
In reply to#1425501
This adds support for AD5820 autofocus coil, found for example in
Nokia N900 smartphone.

Signed-off-by: Pavel Machek <pavel@ucw.cz>

---
v2: simple cleanups, fix error paths, simplify probe

v3: more cleanups, remove printk, add include

v4: add uapi definitions

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 993dc50..77313a1 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -279,6 +279,13 @@ config VIDEO_ML86V7667
 	  To compile this driver as a module, choose M here: the
 	  module will be called ml86v7667.
 
+config VIDEO_AD5820
+	tristate "AD5820 lens voice coil support"
+	depends on I2C && VIDEO_V4L2 && MEDIA_CONTROLLER
+	---help---
+	  This is a driver for the AD5820 camera lens voice coil.
+	  It is used for example in Nokia N900 (RX-51).
+
 config VIDEO_SAA7110
 	tristate "Philips SAA7110 video decoder"
 	depends on VIDEO_V4L2 && I2C
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index 94f2c99..34434ae 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -19,6 +20,7 @@ obj-$(CONFIG_VIDEO_SAA717X) += saa717x.o
 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_ADV7170) += adv7170.o
 obj-$(CONFIG_VIDEO_ADV7175) += adv7175.o
 obj-$(CONFIG_VIDEO_ADV7180) += adv7180.o
diff --git a/drivers/media/i2c/ad5820.c b/drivers/media/i2c/ad5820.c
new file mode 100644
index 0000000..a1be956
--- /dev/null
+++ b/drivers/media/i2c/ad5820.c
@@ -0,0 +1,430 @@
+/*
+ * drivers/media/i2c/ad5820.c
+ *
+ * AD5820 DAC driver for camera voice coil focus.
+ *
+ * Copyright (C) 2008 Nokia Corporation
+ * Copyright (C) 2007 Texas Instruments
+ * Copyright (C) 2016 Pavel Machek <pavel@ucw.cz>
+ *
+ * Contact: Tuukka Toivonen <tuukkat76@gmail.com>
+ *	    Sakari Ailus <sakari.ailus@iki.fi>
+ *
+ * Based on af_d88.c by Texas Instruments.
+ *
+ * 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/errno.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/regulator/consumer.h>
+
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-subdev.h>
+
+#include <uapi/linux/ad5820.h>
+
+#define AD5820_NAME		"ad5820"
+
+/* Register definitions */
+#define AD5820_POWER_DOWN		(1 << 15)
+#define AD5820_DAC_SHIFT		4
+#define AD5820_RAMP_MODE_LINEAR		(0 << 3)
+#define AD5820_RAMP_MODE_64_16		(1 << 3)
+
+#define CODE_TO_RAMP_US(s)	((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
+#define RAMP_US_TO_CODE(c)	fls(((c) + ((c)>>1)) / 50)
+
+#define to_ad5820_device(sd)	container_of(sd, struct ad5820_device, subdev)
+
+struct ad5820_device {
+	struct v4l2_subdev subdev;
+	struct ad5820_platform_data *platform_data;
+	struct regulator *vana;
+
+	struct v4l2_ctrl_handler ctrls;
+	u32 focus_absolute;
+	u32 focus_ramp_time;
+	u32 focus_ramp_mode;
+
+	struct mutex power_lock;
+	int power_count;
+
+	int standby : 1;
+};
+
+static int ad5820_write(struct ad5820_device *coil, u16 data)
+{
+	struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
+	struct i2c_msg msg;
+	int r;
+
+	if (!client->adapter)
+		return -ENODEV;
+
+	data = cpu_to_be16(data);
+	msg.addr  = client->addr;
+	msg.flags = 0;
+	msg.len   = 2;
+	msg.buf   = (u8 *)&data;
+
+	r = i2c_transfer(client->adapter, &msg, 1);
+	if (r < 0) {
+		dev_err(&client->dev, "write failed, error %d\n", r);
+		return r;
+	}
+
+	return 0;
+}
+
+/*
+ * Calculate status word and write it to the device based on current
+ * values of V4L2 controls. It is assumed that the stored V4L2 control
+ * values are properly limited and rounded.
+ */
+static int ad5820_update_hw(struct ad5820_device *coil)
+{
+	u16 status;
+
+	status = RAMP_US_TO_CODE(coil->focus_ramp_time);
+	status |= coil->focus_ramp_mode
+		? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
+	status |= coil->focus_absolute << AD5820_DAC_SHIFT;
+
+	if (coil->standby)
+		status |= AD5820_POWER_DOWN;
+
+	return ad5820_write(coil, status);
+}
+
+/*
+ * Power handling
+ */
+static int ad5820_power_off(struct ad5820_device *coil, int standby)
+{
+	int ret = 0, ret2;
+
+	/*
+	 * Go to standby first as real power off my be denied by the hardware
+	 * (single power line control for both coil and sensor).
+	 */
+	if (standby) {
+		coil->standby = 1;
+		ret = ad5820_update_hw(coil);
+	}
+
+	ret2 = regulator_disable(coil->vana);
+	if (ret)
+		return ret;
+	return ret2;
+}
+
+static int ad5820_power_on(struct ad5820_device *coil, int restore)
+{
+	int ret;
+
+	ret = regulator_enable(coil->vana);
+	if (ret < 0)
+		return ret;
+
+	if (restore) {
+		/* Restore the hardware settings. */
+		coil->standby = 0;
+		ret = ad5820_update_hw(coil);
+		if (ret)
+			goto fail;
+	}
+	return 0;
+
+fail:
+	coil->standby = 1;
+	regulator_disable(coil->vana);
+
+	return ret;
+}
+
+/*
+ * V4L2 controls
+ */
+static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct ad5820_device *coil =
+		container_of(ctrl->handler, struct ad5820_device, ctrls);
+	u32 code;
+
+	switch (ctrl->id) {
+	case V4L2_CID_FOCUS_ABSOLUTE:
+		coil->focus_absolute = ctrl->val;
+		return ad5820_update_hw(coil);
+
+	case V4L2_CID_AD5820_RAMP_TIME:
+		code = RAMP_US_TO_CODE(ctrl->val);
+		ctrl->val = CODE_TO_RAMP_US(code);
+		coil->focus_ramp_time = ctrl->val;
+		break;
+
+	case V4L2_CID_AD5820_RAMP_MODE:
+		coil->focus_ramp_mode = ctrl->val;
+		break;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
+	.s_ctrl = ad5820_set_ctrl,
+};
+
+static const char * const ad5820_focus_menu[] = {
+	"Linear ramp",
+	"64/16 ramp",
+};
+
+static const struct v4l2_ctrl_config ad5820_ctrls[] = {
+	{
+		.ops		= &ad5820_ctrl_ops,
+		.id		= V4L2_CID_AD5820_RAMP_TIME,
+		.type		= V4L2_CTRL_TYPE_INTEGER,
+		.name		= "Focus ramping time [us]",
+		.min		= 0,
+		.max		= 3200,
+		.step		= 50,
+		.def		= 0,
+		.flags		= 0,
+	},
+	{
+		.ops		= &ad5820_ctrl_ops,
+		.id		= V4L2_CID_AD5820_RAMP_MODE,
+		.type		= V4L2_CTRL_TYPE_MENU,
+		.name		= "Focus ramping mode",
+		.min		= 0,
+		.max		= ARRAY_SIZE(ad5820_focus_menu),
+		.step		= 0,
+		.def		= 0,
+		.flags		= 0,
+		.qmenu		= ad5820_focus_menu,
+	},
+};
+
+
+static int ad5820_init_controls(struct ad5820_device *coil)
+{
+	unsigned int i;
+
+	v4l2_ctrl_handler_init(&coil->ctrls, ARRAY_SIZE(ad5820_ctrls) + 1);
+
+	/*
+	 * V4L2_CID_FOCUS_ABSOLUTE
+	 *
+	 * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
+	 * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
+	 * for focus position, because it is meaningless for user. Meaningful
+	 * would be to use focus distance or even its inverse, but since the
+	 * driver doesn't have sufficiently knowledge to do the conversion, we
+	 * will just use abstract codes here. In any case, smaller value = focus
+	 * position farther from camera. The default zero value means focus at
+	 * infinity, and also least current consumption.
+	 */
+	v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
+			  V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
+
+	/* V4L2_CID_TEST_PATTERN and V4L2_CID_MODE_* */
+	for (i = 0; i < ARRAY_SIZE(ad5820_ctrls); ++i)
+		v4l2_ctrl_new_custom(&coil->ctrls, &ad5820_ctrls[i], NULL);
+
+	if (coil->ctrls.error)
+		return coil->ctrls.error;
+
+	coil->focus_absolute = 0;
+	coil->focus_ramp_time = 0;
+	coil->focus_ramp_mode = 0;
+
+	coil->subdev.ctrl_handler = &coil->ctrls;
+
+	return 0;
+}
+
+/*
+ * V4L2 subdev operations
+ */
+static int ad5820_registered(struct v4l2_subdev *subdev)
+{
+	struct ad5820_device *coil = to_ad5820_device(subdev);
+
+	return ad5820_init_controls(coil);
+}
+
+static int
+ad5820_set_power(struct v4l2_subdev *subdev, int on)
+{
+	struct ad5820_device *coil = to_ad5820_device(subdev);
+	int ret = 0;
+
+	mutex_lock(&coil->power_lock);
+
+	/*
+	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
+	 * update the power state.
+	 */
+	if (coil->power_count == !on) {
+		ret = on ? ad5820_power_on(coil, 1) : ad5820_power_off(coil, 1);
+		if (ret < 0)
+			goto done;
+	}
+
+	/* Update the power count. */
+	coil->power_count += on ? 1 : -1;
+	WARN_ON(coil->power_count < 0);
+
+done:
+	mutex_unlock(&coil->power_lock);
+	return ret;
+}
+
+static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	return ad5820_set_power(sd, 1);
+}
+
+static int ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
+{
+	return ad5820_set_power(sd, 0);
+}
+
+static const struct v4l2_subdev_core_ops ad5820_core_ops = {
+	.s_power = ad5820_set_power,
+};
+
+static const struct v4l2_subdev_ops ad5820_ops = {
+	.core = &ad5820_core_ops,
+};
+
+static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
+	.registered = ad5820_registered,
+	.open = ad5820_open,
+	.close = ad5820_close,
+};
+
+/*
+ * I2C driver
+ */
+#ifdef CONFIG_PM
+
+static int ad5820_suspend(struct device *dev)
+{
+	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
+	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
+	struct ad5820_device *coil = to_ad5820_device(subdev);
+
+	if (!coil->power_count)
+		return 0;
+
+	return ad5820_power_off(coil, 0);
+}
+
+static int ad5820_resume(struct device *dev)
+{
+	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
+	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
+	struct ad5820_device *coil = to_ad5820_device(subdev);
+
+	if (!coil->power_count)
+		return 0;
+
+	return ad5820_power_on(coil, 1);
+}
+
+#else
+
+#define ad5820_suspend	NULL
+#define ad5820_resume	NULL
+
+#endif /* CONFIG_PM */
+
+static int ad5820_probe(struct i2c_client *client,
+			const struct i2c_device_id *devid)
+{
+	struct ad5820_device *coil;
+	int ret;
+
+	coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
+	if (!coil)
+		return -ENOMEM;
+
+	coil->vana = devm_regulator_get(&client->dev, "VANA");
+	if (IS_ERR(coil->vana)) {
+		ret = PTR_ERR(coil->vana);
+		if (ret != -EPROBE_DEFER)
+			dev_err(&client->dev, "could not get regulator for vana\n");
+		return ret;
+	}
+	
+	mutex_init(&coil->power_lock);
+
+	v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
+	coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	coil->subdev.internal_ops = &ad5820_internal_ops;
+	strcpy(coil->subdev.name, "ad5820 focus");
+
+	ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
+	if (ret < 0)
+		goto cleanup2;
+
+	ret = v4l2_async_register_subdev(&coil->subdev);
+	if (ret < 0)
+		goto cleanup;
+
+	return ret;
+
+cleanup2:
+	mutex_destroy(&coil->power_lock);
+cleanup:
+	media_entity_cleanup(&coil->subdev.entity);
+	return ret;
+}
+
+static int __exit ad5820_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
+	struct ad5820_device *coil = to_ad5820_device(subdev);
+
+	v4l2_device_unregister_subdev(&coil->subdev);
+	v4l2_ctrl_handler_free(&coil->ctrls);
+	media_entity_cleanup(&coil->subdev.entity);
+	mutex_destroy(&coil->power_lock);
+	return 0;
+}
+
+static const struct i2c_device_id ad5820_id_table[] = {
+	{ AD5820_NAME, 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
+
+static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
+
+static struct i2c_driver ad5820_i2c_driver = {
+	.driver		= {
+		.name	= AD5820_NAME,
+		.pm	= &ad5820_pm,
+	},
+	.probe		= ad5820_probe,
+	.remove		= __exit_p(ad5820_remove),
+	.id_table	= ad5820_id_table,
+};
+
+module_i2c_driver(ad5820_i2c_driver);
+
+MODULE_AUTHOR("Tuukka Toivonen");
+MODULE_DESCRIPTION("AD5820 camera lens driver");
+MODULE_LICENSE("GPL");
diff --git a/include/uapi/linux/ad5820.h b/include/uapi/linux/ad5820.h
new file mode 100644
index 0000000..4e541e8
--- /dev/null
+++ b/include/uapi/linux/ad5820.h
@@ -0,0 +1,18 @@
+/*
+ * AD5820 DAC driver for camera voice coil focus.
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_AD5820_H
+#define __LINUX_AD5820_H
+
+#include <linux/v4l2-controls.h>
+
+/* Control IDs specific to the AD5820 driver as defined by V4L2 */
+#define V4L2_CID_AD5820_RAMP_TIME	(V4L2_CID_AD5820_BASE + 0)
+#define V4L2_CID_AD5820_RAMP_MODE	(V4L2_CID_AD5820_BASE + 1)
+
+#endif
diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index b6a357a..015e90b 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -180,6 +181,10 @@ enum v4l2_colorfx {
  * We reserve 16 controls for this driver. */
 #define V4L2_CID_USER_TC358743_BASE		(V4L2_CID_USER_BASE + 0x1080)
 
+/* The base for the ad5820 driver controls.
+ * We reserve 16 controls for this driver. */
+#define V4L2_CID_AD5820_BASE			(V4L2_CID_USER_BASE + 0x1090)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */



-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[toc] | [prev] | [next] | [standalone]


#1425752

FromPavel Machek <pavel@ucw.cz>
Date2016-06-18 17:40 +0200
Message-ID<rLqvL-5Po-19@gated-at.bofh.it>
In reply to#1425501
Hi!

> > Not V4L2_CID_USER_AD5820...?
> 
> The rest of the controls have no USER as part of the macro name, so I
> wouldn't use it here either.

Ok.

> > Ok, separate header file for 2 lines seemed like a bit of overkill,
> > but why not.
> 
> That follows an existing pattern of how controls have been implemented in
> other drivers.

Ok.

> Could you merge this with the driver patch? I've dropped that from my ad5820
> branch as it does not compile.

Yes, merged patch should be in your inbox now.

Thanks,
									Pavel

-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web