2 * camera image capture (abstract) bus driver header
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
15 #include <linux/bitops.h>
16 #include <linux/device.h>
17 #include <linux/mutex.h>
19 #include <linux/videodev2.h>
20 #include <media/videobuf-core.h>
21 #include <media/videobuf2-core.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
27 struct soc_camera_desc
;
28 struct soc_camera_async_client
;
30 struct soc_camera_device
{
31 struct list_head list
; /* list of all registered devices */
32 struct soc_camera_desc
*sdesc
;
33 struct device
*pdev
; /* Platform device */
34 struct device
*parent
; /* Camera host device */
35 struct device
*control
; /* E.g., the i2c client */
38 u32 bytesperline
; /* for padding, zero if unused */
40 enum v4l2_colorspace colorspace
;
41 unsigned char iface
; /* Host number */
42 unsigned char devnum
; /* Device number per host */
43 struct soc_camera_sense
*sense
; /* See comment in struct definition */
44 struct video_device
*vdev
;
45 struct v4l2_ctrl_handler ctrl_handler
;
46 const struct soc_camera_format_xlate
*current_fmt
;
47 struct soc_camera_format_xlate
*user_formats
;
49 enum v4l2_field field
; /* Preserve field over close() */
50 void *host_priv
; /* Per-device host private data */
51 /* soc_camera.c private count. Only accessed with .host_lock held */
53 struct file
*streamer
; /* stream owner */
55 /* Asynchronous subdevice management */
56 struct soc_camera_async_client
*sasc
;
57 /* video buffer queue */
59 struct videobuf_queue vb_vidq
;
60 struct vb2_queue vb2_vidq
;
64 /* Host supports programmable stride */
65 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
67 enum soc_camera_subdev_role
{
68 SOCAM_SUBDEV_DATA_SOURCE
= 1,
69 SOCAM_SUBDEV_DATA_SINK
,
70 SOCAM_SUBDEV_DATA_PROCESSOR
,
73 struct soc_camera_async_subdev
{
74 struct v4l2_async_subdev asd
;
75 enum soc_camera_subdev_role role
;
78 struct soc_camera_host
{
79 struct v4l2_device v4l2_dev
;
80 struct list_head list
;
81 struct mutex host_lock
; /* Main synchronisation lock */
82 struct mutex clk_lock
; /* Protect pipeline modifications */
83 unsigned char nr
; /* Host number */
85 struct soc_camera_device
*icd
; /* Currently attached client */
88 struct soc_camera_host_ops
*ops
;
89 struct v4l2_async_subdev
**asd
; /* Flat array, arranged in groups */
90 unsigned int *asd_sizes
; /* 0-terminated array of asd group sizes */
93 struct soc_camera_host_ops
{
95 int (*add
)(struct soc_camera_device
*);
96 void (*remove
)(struct soc_camera_device
*);
97 int (*clock_start
)(struct soc_camera_host
*);
98 void (*clock_stop
)(struct soc_camera_host
*);
100 * .get_formats() is called for each client device format, but
101 * .put_formats() is only called once. Further, if any of the calls to
102 * .get_formats() fail, .put_formats() will not be called at all, the
103 * failing .get_formats() must then clean up internally.
105 int (*get_formats
)(struct soc_camera_device
*, unsigned int,
106 struct soc_camera_format_xlate
*);
107 void (*put_formats
)(struct soc_camera_device
*);
108 int (*cropcap
)(struct soc_camera_device
*, struct v4l2_cropcap
*);
109 int (*get_crop
)(struct soc_camera_device
*, struct v4l2_crop
*);
110 int (*set_crop
)(struct soc_camera_device
*, const struct v4l2_crop
*);
111 int (*get_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
112 int (*set_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
114 * The difference to .set_crop() is, that .set_livecrop is not allowed
115 * to change the output sizes
117 int (*set_livecrop
)(struct soc_camera_device
*, const struct v4l2_crop
*);
118 int (*set_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
119 int (*try_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
120 void (*init_videobuf
)(struct videobuf_queue
*,
121 struct soc_camera_device
*);
122 int (*init_videobuf2
)(struct vb2_queue
*,
123 struct soc_camera_device
*);
124 int (*reqbufs
)(struct soc_camera_device
*, struct v4l2_requestbuffers
*);
125 int (*querycap
)(struct soc_camera_host
*, struct v4l2_capability
*);
126 int (*set_bus_param
)(struct soc_camera_device
*);
127 int (*get_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
128 int (*set_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
129 int (*enum_framesizes
)(struct soc_camera_device
*, struct v4l2_frmsizeenum
*);
130 unsigned int (*poll
)(struct file
*, poll_table
*);
133 #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
134 #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
135 #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
136 #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
137 #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
139 struct i2c_board_info
;
140 struct regulator_bulk_data
;
142 struct soc_camera_subdev_desc
{
143 /* Per camera SOCAM_SENSOR_* bus flags */
146 /* sensor driver private platform data */
149 /* Optional regulators that have to be managed on power on/off events */
150 struct regulator_bulk_data
*regulators
;
153 /* Optional callbacks to power on or off and reset the sensor */
154 int (*power
)(struct device
*, int);
155 int (*reset
)(struct device
*);
158 * some platforms may support different data widths than the sensors
159 * native ones due to different data line routing. Let the board code
160 * overwrite the width flags.
162 int (*set_bus_param
)(struct soc_camera_subdev_desc
*, unsigned long flags
);
163 unsigned long (*query_bus_param
)(struct soc_camera_subdev_desc
*);
164 void (*free_bus
)(struct soc_camera_subdev_desc
*);
167 struct soc_camera_host_desc
{
168 /* Camera bus id, used to match a camera and a bus */
171 struct i2c_board_info
*board_info
;
172 const char *module_name
;
175 * For non-I2C devices platform has to provide methods to add a device
176 * to the system and to remove it
178 int (*add_device
)(struct soc_camera_device
*);
179 void (*del_device
)(struct soc_camera_device
*);
183 * Platform data for "soc-camera-pdrv"
184 * This MUST be kept binary-identical to struct soc_camera_link below, until
185 * it is completely replaced by this one, after which we can split it into its
188 struct soc_camera_desc
{
189 struct soc_camera_subdev_desc subdev_desc
;
190 struct soc_camera_host_desc host_desc
;
193 /* Prepare to replace this struct: don't change its layout any more! */
194 struct soc_camera_link
{
196 * Subdevice part - keep at top and compatible to
197 * struct soc_camera_subdev_desc
200 /* Per camera SOCAM_SENSOR_* bus flags */
205 /* Optional regulators that have to be managed on power on/off events */
206 struct regulator_bulk_data
*regulators
;
209 /* Optional callbacks to power on or off and reset the sensor */
210 int (*power
)(struct device
*, int);
211 int (*reset
)(struct device
*);
213 * some platforms may support different data widths than the sensors
214 * native ones due to different data line routing. Let the board code
215 * overwrite the width flags.
217 int (*set_bus_param
)(struct soc_camera_link
*, unsigned long flags
);
218 unsigned long (*query_bus_param
)(struct soc_camera_link
*);
219 void (*free_bus
)(struct soc_camera_link
*);
222 * Host part - keep at bottom and compatible to
223 * struct soc_camera_host_desc
226 /* Camera bus id, used to match a camera and a bus */
229 struct i2c_board_info
*board_info
;
230 const char *module_name
;
233 * For non-I2C devices platform has to provide methods to add a device
234 * to the system and to remove it
236 int (*add_device
)(struct soc_camera_device
*);
237 void (*del_device
)(struct soc_camera_device
*);
240 static inline struct soc_camera_host
*to_soc_camera_host(
241 const struct device
*dev
)
243 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
245 return container_of(v4l2_dev
, struct soc_camera_host
, v4l2_dev
);
248 static inline struct soc_camera_desc
*to_soc_camera_desc(
249 const struct soc_camera_device
*icd
)
254 static inline struct device
*to_soc_camera_control(
255 const struct soc_camera_device
*icd
)
260 static inline struct v4l2_subdev
*soc_camera_to_subdev(
261 const struct soc_camera_device
*icd
)
263 struct device
*control
= to_soc_camera_control(icd
);
264 return dev_get_drvdata(control
);
267 int soc_camera_host_register(struct soc_camera_host
*ici
);
268 void soc_camera_host_unregister(struct soc_camera_host
*ici
);
270 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
271 struct soc_camera_device
*icd
, unsigned int fourcc
);
274 * struct soc_camera_format_xlate - match between host and sensor formats
275 * @code: code of a sensor provided format
276 * @host_fmt: host format after host translation from code
278 * Host and sensor translation structure. Used in table of host and sensor
279 * formats matchings in soc_camera_device. A host can override the generic list
280 * generation by implementing get_formats(), and use it for format checks and
283 struct soc_camera_format_xlate
{
284 enum v4l2_mbus_pixelcode code
;
285 const struct soc_mbus_pixelfmt
*host_fmt
;
288 #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
291 * This struct can be attached to struct soc_camera_device by the host driver
292 * to request sense from the camera, for example, when calling .set_fmt(). The
293 * host then can check which flags are set and verify respective values if any.
294 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
295 * changed during this operation. After completion the host should detach sense.
297 * @flags ored SOCAM_SENSE_* flags
298 * @master_clock if the host wants to be informed about pixel-clock
299 * change, it better set master_clock.
300 * @pixel_clock_max maximum pixel clock frequency supported by the host,
301 * camera is not allowed to exceed this.
302 * @pixel_clock if the camera driver changed pixel clock during this
303 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
304 * master_clock to calculate the new pixel-clock and
307 struct soc_camera_sense
{
309 unsigned long master_clock
;
310 unsigned long pixel_clock_max
;
311 unsigned long pixel_clock
;
314 #define SOCAM_DATAWIDTH(x) BIT((x) - 1)
315 #define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
316 #define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
317 #define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
318 #define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
319 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
320 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
321 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
322 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
323 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
325 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
326 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
327 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
328 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
331 static inline void soc_camera_limit_side(int *start
, int *length
,
332 unsigned int start_min
,
333 unsigned int length_min
, unsigned int length_max
)
335 if (*length
< length_min
)
336 *length
= length_min
;
337 else if (*length
> length_max
)
338 *length
= length_max
;
340 if (*start
< start_min
)
342 else if (*start
> start_min
+ length_max
- *length
)
343 *start
= start_min
+ length_max
- *length
;
346 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc
*ssdd
,
347 const struct v4l2_mbus_config
*cfg
);
349 int soc_camera_power_init(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
);
350 int soc_camera_power_on(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
351 struct v4l2_clk
*clk
);
352 int soc_camera_power_off(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
353 struct v4l2_clk
*clk
);
355 static inline int soc_camera_set_power(struct device
*dev
,
356 struct soc_camera_subdev_desc
*ssdd
, struct v4l2_clk
*clk
, bool on
)
358 return on
? soc_camera_power_on(dev
, ssdd
, clk
)
359 : soc_camera_power_off(dev
, ssdd
, clk
);
362 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
363 #include <linux/i2c.h>
364 static inline struct video_device
*soc_camera_i2c_to_vdev(const struct i2c_client
*client
)
366 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
367 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
368 return icd
? icd
->vdev
: NULL
;
371 static inline struct soc_camera_subdev_desc
*soc_camera_i2c_to_desc(const struct i2c_client
*client
)
373 return client
->dev
.platform_data
;
376 static inline struct v4l2_subdev
*soc_camera_vdev_to_subdev(struct video_device
*vdev
)
378 struct soc_camera_device
*icd
= video_get_drvdata(vdev
);
379 return soc_camera_to_subdev(icd
);
382 static inline struct soc_camera_device
*soc_camera_from_vb2q(const struct vb2_queue
*vq
)
384 return container_of(vq
, struct soc_camera_device
, vb2_vidq
);
387 static inline struct soc_camera_device
*soc_camera_from_vbq(const struct videobuf_queue
*vq
)
389 return container_of(vq
, struct soc_camera_device
, vb_vidq
);
392 static inline u32
soc_camera_grp_id(const struct soc_camera_device
*icd
)
394 return (icd
->iface
<< 8) | (icd
->devnum
+ 1);
397 void soc_camera_lock(struct vb2_queue
*vq
);
398 void soc_camera_unlock(struct vb2_queue
*vq
);