blob: ba0c15dd092cc7c758dddb7f2f6da808df48a2df [file] [log] [blame]
Mauro Carvalho Chehabc0d01382016-07-17 20:44:13 -03001The Soc-Camera Drivers
2======================
3
4Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Guennadi Liakhovetski28531552008-08-29 15:16:31 -03005
6Terminology
7-----------
8
9The following terms are used in this document:
10 - camera / camera device / camera sensor - a video-camera sensor chip, capable
11 of connecting to a variety of systems and interfaces, typically uses i2c for
12 control and configuration, and a parallel or a serial bus for data.
13 - camera host - an interface, to which a camera is connected. Typically a
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030014 specialised interface, present on many SoCs, e.g. PXA27x and PXA3xx, SuperH,
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030015 AVR32, i.MX27, i.MX31.
16 - camera host bus - a connection between a camera host and a camera. Can be
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030017 parallel or serial, consists of data and control lines, e.g. clock, vertical
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030018 and horizontal synchronization signals.
19
20Purpose of the soc-camera subsystem
21-----------------------------------
22
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030023The soc-camera subsystem initially provided a unified API between camera host
24drivers and camera sensor drivers. Later the soc-camera sensor API has been
25replaced with the V4L2 standard subdev API. This also made camera driver re-use
26with non-soc-camera hosts possible. The camera host API to the soc-camera core
27has been preserved.
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030028
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030029Soc-camera implements a V4L2 interface to the user, currently only the "mmap"
30method is supported by host drivers. However, the soc-camera core also provides
31support for the "read" method.
32
33The subsystem has been designed to support multiple camera host interfaces and
34multiple cameras per interface, although most applications have only one camera
35sensor.
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030036
37Existing drivers
38----------------
39
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030040As of 3.7 there are seven host drivers in the mainline: atmel-isi.c,
41mx1_camera.c (broken, scheduled for removal), mx2_camera.c, mx3_camera.c,
42omap1_camera.c, pxa_camera.c, sh_mobile_ceu_camera.c, and multiple sensor
43drivers under drivers/media/i2c/soc_camera/.
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030044
45Camera host API
46---------------
47
48A host camera driver is registered using the
49
Mauro Carvalho Chehabc0d01382016-07-17 20:44:13 -030050.. code-block:: none
51
52 soc_camera_host_register(struct soc_camera_host *);
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030053
54function. The host object can be initialized as follows:
55
Mauro Carvalho Chehabc0d01382016-07-17 20:44:13 -030056.. code-block:: none
57
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030058 struct soc_camera_host *ici;
59 ici->drv_name = DRV_NAME;
60 ici->ops = &camera_host_ops;
61 ici->priv = pcdev;
62 ici->v4l2_dev.dev = &pdev->dev;
63 ici->nr = pdev->id;
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030064
65All camera host methods are passed in a struct soc_camera_host_ops:
66
Mauro Carvalho Chehabc0d01382016-07-17 20:44:13 -030067.. code-block:: none
68
69 static struct soc_camera_host_ops camera_host_ops = {
70 .owner = THIS_MODULE,
71 .add = camera_add_device,
72 .remove = camera_remove_device,
73 .set_fmt = camera_set_fmt_cap,
74 .try_fmt = camera_try_fmt_cap,
75 .init_videobuf2 = camera_init_videobuf2,
76 .poll = camera_poll,
77 .querycap = camera_querycap,
78 .set_bus_param = camera_set_bus_param,
79 /* The rest of host operations are optional */
80 };
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030081
82.add and .remove methods are called when a sensor is attached to or detached
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030083from the host. .set_bus_param is used to configure physical connection
84parameters between the host and the sensor. .init_videobuf2 is called by
85soc-camera core when a video-device is opened, the host driver would typically
86call vb2_queue_init() in this method. Further video-buffer management is
87implemented completely by the specific camera host driver. If the host driver
88supports non-standard pixel format conversion, it should implement a
89.get_formats and, possibly, a .put_formats operations. See below for more
90details about format conversion. The rest of the methods are called from
Guennadi Liakhovetski28531552008-08-29 15:16:31 -030091respective V4L2 operations.
92
93Camera API
94----------
95
96Sensor drivers can use struct soc_camera_link, typically provided by the
97platform, and used to specify to which camera host bus the sensor is connected,
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -030098and optionally provide platform .power and .reset methods for the camera. This
99struct is provided to the camera driver via the I2C client device platform data
100and can be obtained, using the soc_camera_i2c_to_link() macro. Care should be
101taken, when using soc_camera_vdev_to_subdev() and when accessing struct
102soc_camera_device, using v4l2_get_subdev_hostdata(): both only work, when
103running on an soc-camera host. The actual camera driver operation is implemented
104using the V4L2 subdev API. Additionally soc-camera camera drivers can use
105auxiliary soc-camera helper functions like soc_camera_power_on() and
106soc_camera_power_off(), which switch regulators, provided by the platform and call
107board-specific power switching methods. soc_camera_apply_board_flags() takes
108camera bus configuration capability flags and applies any board transformations,
109e.g. signal polarity inversion. soc_mbus_get_fmtdesc() can be used to obtain a
110pixel format descriptor, corresponding to a certain media-bus pixel format code.
111soc_camera_limit_side() can be used to restrict beginning and length of a frame
112side, based on camera capabilities.
Guennadi Liakhovetski28531552008-08-29 15:16:31 -0300113
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300114VIDIOC_S_CROP and VIDIOC_S_FMT behaviour
115----------------------------------------
116
117Above user ioctls modify image geometry as follows:
118
119VIDIOC_S_CROP: sets location and sizes of the sensor window. Unit is one sensor
120pixel. Changing sensor window sizes preserves any scaling factors, therefore
121user window sizes change as well.
122
123VIDIOC_S_FMT: sets user window. Should preserve previously set sensor window as
124much as possible by modifying scaling factors. If the sensor window cannot be
125preserved precisely, it may be changed too.
126
Stefan Huber8bfcb932013-06-05 12:24:33 +0200127In soc-camera there are two locations, where scaling and cropping can take
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300128place: in the camera driver and in the host driver. User ioctls are first passed
129to the host driver, which then generally passes them down to the camera driver.
130It is more efficient to perform scaling and cropping in the camera driver to
131save camera bus bandwidth and maximise the framerate. However, if the camera
132driver failed to set the required parameters with sufficient precision, the host
133driver may decide to also use its own scaling and cropping to fulfill the user's
134request.
135
136Camera drivers are interfaced to the soc-camera core and to host drivers over
137the v4l2-subdev API, which is completely functional, it doesn't pass any data.
138Therefore all camera drivers shall reply to .g_fmt() requests with their current
139output geometry. This is necessary to correctly configure the camera bus.
140.s_fmt() and .try_fmt() have to be implemented too. Sensor window and scaling
141factors have to be maintained by camera drivers internally. According to the
142V4L2 API all capture drivers must support the VIDIOC_CROPCAP ioctl, hence we
143rely on camera drivers implementing .cropcap(). If the camera driver does not
144support cropping, it may choose to not implement .s_crop(), but to enable
145cropping support by the camera host driver at least the .g_crop method must be
146implemented.
147
148User window geometry is kept in .user_width and .user_height fields in struct
149soc_camera_device and used by the soc-camera core and host drivers. The core
150updates these fields upon successful completion of a .s_fmt() call, but if these
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -0300151fields change elsewhere, e.g. during .s_crop() processing, the host driver is
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300152responsible for updating them.
153
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -0300154Format conversion
155-----------------
156
157V4L2 distinguishes between pixel formats, as they are stored in memory, and as
158they are transferred over a media bus. Soc-camera provides support to
159conveniently manage these formats. A table of standard transformations is
160maintained by soc-camera core, which describes, what FOURCC pixel format will
161be obtained, if a media-bus pixel format is stored in memory according to
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300162certain rules. E.g. if MEDIA_BUS_FMT_YUYV8_2X8 data is sampled with 8 bits per
Guennadi Liakhovetski454547f2012-10-05 12:33:45 -0300163sample and stored in memory in the little-endian order with no gaps between
164bytes, data in memory will represent the V4L2_PIX_FMT_YUYV FOURCC format. These
165standard transformations will be used by soc-camera or by camera host drivers to
166configure camera drivers to produce the FOURCC format, requested by the user,
167using the VIDIOC_S_FMT ioctl(). Apart from those standard format conversions,
168host drivers can also provide their own conversion rules by implementing a
169.get_formats and, if required, a .put_formats methods.