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-ctrls.h>
23 #include <media/v4l2-device.h>
26 struct soc_camera_desc
;
28 struct soc_camera_device
{
29 struct list_head list
; /* list of all registered devices */
30 struct soc_camera_desc
*sdesc
;
31 struct device
*pdev
; /* Platform device */
32 struct device
*parent
; /* Camera host device */
33 struct device
*control
; /* E.g., the i2c client */
36 u32 bytesperline
; /* for padding, zero if unused */
38 enum v4l2_colorspace colorspace
;
39 unsigned char iface
; /* Host number */
40 unsigned char devnum
; /* Device number per host */
41 struct soc_camera_sense
*sense
; /* See comment in struct definition */
42 struct video_device
*vdev
;
43 struct v4l2_ctrl_handler ctrl_handler
;
44 const struct soc_camera_format_xlate
*current_fmt
;
45 struct soc_camera_format_xlate
*user_formats
;
47 enum v4l2_field field
; /* Preserve field over close() */
48 void *host_priv
; /* Per-device host private data */
49 /* soc_camera.c private count. Only accessed with .host_lock held */
51 struct file
*streamer
; /* stream owner */
53 struct videobuf_queue vb_vidq
;
54 struct vb2_queue vb2_vidq
;
58 /* Host supports programmable stride */
59 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
61 struct soc_camera_host
{
62 struct v4l2_device v4l2_dev
;
63 struct list_head list
;
64 struct mutex host_lock
; /* Protect pipeline modifications */
65 unsigned char nr
; /* Host number */
67 struct soc_camera_device
*icd
; /* Currently attached client */
70 struct soc_camera_host_ops
*ops
;
73 struct soc_camera_host_ops
{
75 int (*add
)(struct soc_camera_device
*);
76 void (*remove
)(struct soc_camera_device
*);
77 int (*clock_start
)(struct soc_camera_host
*);
78 void (*clock_stop
)(struct soc_camera_host
*);
80 * .get_formats() is called for each client device format, but
81 * .put_formats() is only called once. Further, if any of the calls to
82 * .get_formats() fail, .put_formats() will not be called at all, the
83 * failing .get_formats() must then clean up internally.
85 int (*get_formats
)(struct soc_camera_device
*, unsigned int,
86 struct soc_camera_format_xlate
*);
87 void (*put_formats
)(struct soc_camera_device
*);
88 int (*cropcap
)(struct soc_camera_device
*, struct v4l2_cropcap
*);
89 int (*get_crop
)(struct soc_camera_device
*, struct v4l2_crop
*);
90 int (*set_crop
)(struct soc_camera_device
*, const struct v4l2_crop
*);
91 int (*get_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
92 int (*set_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
94 * The difference to .set_crop() is, that .set_livecrop is not allowed
95 * to change the output sizes
97 int (*set_livecrop
)(struct soc_camera_device
*, const struct v4l2_crop
*);
98 int (*set_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
99 int (*try_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
100 void (*init_videobuf
)(struct videobuf_queue
*,
101 struct soc_camera_device
*);
102 int (*init_videobuf2
)(struct vb2_queue
*,
103 struct soc_camera_device
*);
104 int (*reqbufs
)(struct soc_camera_device
*, struct v4l2_requestbuffers
*);
105 int (*querycap
)(struct soc_camera_host
*, struct v4l2_capability
*);
106 int (*set_bus_param
)(struct soc_camera_device
*);
107 int (*get_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
108 int (*set_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
109 int (*enum_framesizes
)(struct soc_camera_device
*, struct v4l2_frmsizeenum
*);
110 unsigned int (*poll
)(struct file
*, poll_table
*);
113 #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
114 #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
115 #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
116 #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
117 #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
119 struct i2c_board_info
;
120 struct regulator_bulk_data
;
122 struct soc_camera_subdev_desc
{
123 /* Per camera SOCAM_SENSOR_* bus flags */
126 /* sensor driver private platform data */
129 /* Optional regulators that have to be managed on power on/off events */
130 struct regulator_bulk_data
*regulators
;
133 /* Optional callbacks to power on or off and reset the sensor */
134 int (*power
)(struct device
*, int);
135 int (*reset
)(struct device
*);
138 * some platforms may support different data widths than the sensors
139 * native ones due to different data line routing. Let the board code
140 * overwrite the width flags.
142 int (*set_bus_param
)(struct soc_camera_subdev_desc
*, unsigned long flags
);
143 unsigned long (*query_bus_param
)(struct soc_camera_subdev_desc
*);
144 void (*free_bus
)(struct soc_camera_subdev_desc
*);
147 struct soc_camera_host_desc
{
148 /* Camera bus id, used to match a camera and a bus */
151 struct i2c_board_info
*board_info
;
152 const char *module_name
;
155 * For non-I2C devices platform has to provide methods to add a device
156 * to the system and to remove it
158 int (*add_device
)(struct soc_camera_device
*);
159 void (*del_device
)(struct soc_camera_device
*);
163 * This MUST be kept binary-identical to struct soc_camera_link below, until
164 * it is completely replaced by this one, after which we can split it into its
167 struct soc_camera_desc
{
168 struct soc_camera_subdev_desc subdev_desc
;
169 struct soc_camera_host_desc host_desc
;
172 /* Prepare to replace this struct: don't change its layout any more! */
173 struct soc_camera_link
{
175 * Subdevice part - keep at top and compatible to
176 * struct soc_camera_subdev_desc
179 /* Per camera SOCAM_SENSOR_* bus flags */
184 /* Optional regulators that have to be managed on power on/off events */
185 struct regulator_bulk_data
*regulators
;
188 /* Optional callbacks to power on or off and reset the sensor */
189 int (*power
)(struct device
*, int);
190 int (*reset
)(struct device
*);
192 * some platforms may support different data widths than the sensors
193 * native ones due to different data line routing. Let the board code
194 * overwrite the width flags.
196 int (*set_bus_param
)(struct soc_camera_link
*, unsigned long flags
);
197 unsigned long (*query_bus_param
)(struct soc_camera_link
*);
198 void (*free_bus
)(struct soc_camera_link
*);
201 * Host part - keep at bottom and compatible to
202 * struct soc_camera_host_desc
205 /* Camera bus id, used to match a camera and a bus */
208 struct i2c_board_info
*board_info
;
209 const char *module_name
;
212 * For non-I2C devices platform has to provide methods to add a device
213 * to the system and to remove it
215 int (*add_device
)(struct soc_camera_device
*);
216 void (*del_device
)(struct soc_camera_device
*);
219 static inline struct soc_camera_host
*to_soc_camera_host(
220 const struct device
*dev
)
222 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
224 return container_of(v4l2_dev
, struct soc_camera_host
, v4l2_dev
);
227 static inline struct soc_camera_desc
*to_soc_camera_desc(
228 const struct soc_camera_device
*icd
)
233 static inline struct device
*to_soc_camera_control(
234 const struct soc_camera_device
*icd
)
239 static inline struct v4l2_subdev
*soc_camera_to_subdev(
240 const struct soc_camera_device
*icd
)
242 struct device
*control
= to_soc_camera_control(icd
);
243 return dev_get_drvdata(control
);
246 int soc_camera_host_register(struct soc_camera_host
*ici
);
247 void soc_camera_host_unregister(struct soc_camera_host
*ici
);
249 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
250 struct soc_camera_device
*icd
, unsigned int fourcc
);
253 * struct soc_camera_format_xlate - match between host and sensor formats
254 * @code: code of a sensor provided format
255 * @host_fmt: host format after host translation from code
257 * Host and sensor translation structure. Used in table of host and sensor
258 * formats matchings in soc_camera_device. A host can override the generic list
259 * generation by implementing get_formats(), and use it for format checks and
262 struct soc_camera_format_xlate
{
263 enum v4l2_mbus_pixelcode code
;
264 const struct soc_mbus_pixelfmt
*host_fmt
;
267 #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
270 * This struct can be attached to struct soc_camera_device by the host driver
271 * to request sense from the camera, for example, when calling .set_fmt(). The
272 * host then can check which flags are set and verify respective values if any.
273 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
274 * changed during this operation. After completion the host should detach sense.
276 * @flags ored SOCAM_SENSE_* flags
277 * @master_clock if the host wants to be informed about pixel-clock
278 * change, it better set master_clock.
279 * @pixel_clock_max maximum pixel clock frequency supported by the host,
280 * camera is not allowed to exceed this.
281 * @pixel_clock if the camera driver changed pixel clock during this
282 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
283 * master_clock to calculate the new pixel-clock and
286 struct soc_camera_sense
{
288 unsigned long master_clock
;
289 unsigned long pixel_clock_max
;
290 unsigned long pixel_clock
;
293 #define SOCAM_DATAWIDTH(x) BIT((x) - 1)
294 #define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
295 #define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
296 #define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
297 #define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
298 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
299 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
300 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
301 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
302 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
304 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
305 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
306 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
307 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
310 static inline void soc_camera_limit_side(int *start
, int *length
,
311 unsigned int start_min
,
312 unsigned int length_min
, unsigned int length_max
)
314 if (*length
< length_min
)
315 *length
= length_min
;
316 else if (*length
> length_max
)
317 *length
= length_max
;
319 if (*start
< start_min
)
321 else if (*start
> start_min
+ length_max
- *length
)
322 *start
= start_min
+ length_max
- *length
;
325 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc
*ssdd
,
326 const struct v4l2_mbus_config
*cfg
);
328 int soc_camera_power_on(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
);
329 int soc_camera_power_off(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
);
331 static inline int soc_camera_set_power(struct device
*dev
,
332 struct soc_camera_subdev_desc
*ssdd
, bool on
)
334 return on
? soc_camera_power_on(dev
, ssdd
)
335 : soc_camera_power_off(dev
, ssdd
);
338 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
339 #include <linux/i2c.h>
340 static inline struct video_device
*soc_camera_i2c_to_vdev(const struct i2c_client
*client
)
342 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
343 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
344 return icd
? icd
->vdev
: NULL
;
347 static inline struct soc_camera_subdev_desc
*soc_camera_i2c_to_desc(const struct i2c_client
*client
)
349 return client
->dev
.platform_data
;
352 static inline struct v4l2_subdev
*soc_camera_vdev_to_subdev(struct video_device
*vdev
)
354 struct soc_camera_device
*icd
= video_get_drvdata(vdev
);
355 return soc_camera_to_subdev(icd
);
358 static inline struct soc_camera_device
*soc_camera_from_vb2q(const struct vb2_queue
*vq
)
360 return container_of(vq
, struct soc_camera_device
, vb2_vidq
);
363 static inline struct soc_camera_device
*soc_camera_from_vbq(const struct videobuf_queue
*vq
)
365 return container_of(vq
, struct soc_camera_device
, vb_vidq
);
368 static inline u32
soc_camera_grp_id(const struct soc_camera_device
*icd
)
370 return (icd
->iface
<< 8) | (icd
->devnum
+ 1);
373 void soc_camera_lock(struct vb2_queue
*vq
);
374 void soc_camera_unlock(struct vb2_queue
*vq
);