1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * camera image capture (abstract) bus driver header
5 * Copyright (C) 2006, Sascha Hauer, Pengutronix
6 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/mutex.h>
16 #include <linux/videodev2.h>
17 #include <media/videobuf2-v4l2.h>
18 #include <media/v4l2-async.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
23 struct soc_camera_desc
;
24 struct soc_camera_async_client
;
26 struct soc_camera_device
{
27 struct list_head list
; /* list of all registered devices */
28 struct soc_camera_desc
*sdesc
;
29 struct device
*pdev
; /* Platform device */
30 struct device
*parent
; /* Camera host device */
31 struct device
*control
; /* E.g., the i2c client */
34 u32 bytesperline
; /* for padding, zero if unused */
36 enum v4l2_colorspace colorspace
;
37 unsigned char iface
; /* Host number */
38 unsigned char devnum
; /* Device number per host */
39 struct soc_camera_sense
*sense
; /* See comment in struct definition */
40 struct video_device
*vdev
;
41 struct v4l2_ctrl_handler ctrl_handler
;
42 const struct soc_camera_format_xlate
*current_fmt
;
43 struct soc_camera_format_xlate
*user_formats
;
45 enum v4l2_field field
; /* Preserve field over close() */
46 void *host_priv
; /* Per-device host private data */
47 /* soc_camera.c private count. Only accessed with .host_lock held */
49 struct file
*streamer
; /* stream owner */
51 /* Asynchronous subdevice management */
52 struct soc_camera_async_client
*sasc
;
53 /* video buffer queue */
54 struct vb2_queue vb2_vidq
;
57 /* Host supports programmable stride */
58 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
60 enum soc_camera_subdev_role
{
61 SOCAM_SUBDEV_DATA_SOURCE
= 1,
62 SOCAM_SUBDEV_DATA_SINK
,
63 SOCAM_SUBDEV_DATA_PROCESSOR
,
66 struct soc_camera_async_subdev
{
67 struct v4l2_async_subdev asd
;
68 enum soc_camera_subdev_role role
;
71 struct soc_camera_host
{
72 struct v4l2_device v4l2_dev
;
73 struct list_head list
;
74 struct mutex host_lock
; /* Main synchronisation lock */
75 struct mutex clk_lock
; /* Protect pipeline modifications */
76 unsigned char nr
; /* Host number */
78 struct soc_camera_device
*icd
; /* Currently attached client */
81 struct soc_camera_host_ops
*ops
;
82 struct v4l2_async_subdev
**asd
; /* Flat array, arranged in groups */
83 unsigned int *asd_sizes
; /* 0-terminated array of asd group sizes */
86 struct soc_camera_host_ops
{
88 int (*add
)(struct soc_camera_device
*);
89 void (*remove
)(struct soc_camera_device
*);
90 int (*clock_start
)(struct soc_camera_host
*);
91 void (*clock_stop
)(struct soc_camera_host
*);
93 * .get_formats() is called for each client device format, but
94 * .put_formats() is only called once. Further, if any of the calls to
95 * .get_formats() fail, .put_formats() will not be called at all, the
96 * failing .get_formats() must then clean up internally.
98 int (*get_formats
)(struct soc_camera_device
*, unsigned int,
99 struct soc_camera_format_xlate
*);
100 void (*put_formats
)(struct soc_camera_device
*);
101 int (*get_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
102 int (*set_selection
)(struct soc_camera_device
*, struct v4l2_selection
*);
104 * The difference to .set_selection() is, that .set_liveselection is not allowed
105 * to change the output sizes
107 int (*set_liveselection
)(struct soc_camera_device
*, struct v4l2_selection
*);
108 int (*set_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
109 int (*try_fmt
)(struct soc_camera_device
*, struct v4l2_format
*);
110 int (*init_videobuf2
)(struct vb2_queue
*,
111 struct soc_camera_device
*);
112 int (*querycap
)(struct soc_camera_host
*, struct v4l2_capability
*);
113 int (*set_bus_param
)(struct soc_camera_device
*);
114 int (*get_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
115 int (*set_parm
)(struct soc_camera_device
*, struct v4l2_streamparm
*);
116 int (*enum_framesizes
)(struct soc_camera_device
*, struct v4l2_frmsizeenum
*);
117 __poll_t (*poll
)(struct file
*, poll_table
*);
120 #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
121 #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
122 #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
123 #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
124 #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
126 struct i2c_board_info
;
127 struct regulator_bulk_data
;
129 struct soc_camera_subdev_desc
{
130 /* Per camera SOCAM_SENSOR_* bus flags */
133 /* sensor driver private platform data */
137 * Set unbalanced_power to true to deal with legacy drivers, failing to
138 * balance their calls to subdevice's .s_power() method. clock_state is
139 * then used internally by helper functions, it shouldn't be touched by
140 * drivers or the platform code.
142 bool unbalanced_power
;
143 unsigned long clock_state
;
145 /* Optional callbacks to power on or off and reset the sensor */
146 int (*power
)(struct device
*, int);
147 int (*reset
)(struct device
*);
150 * some platforms may support different data widths than the sensors
151 * native ones due to different data line routing. Let the board code
152 * overwrite the width flags.
154 int (*set_bus_param
)(struct soc_camera_subdev_desc
*, unsigned long flags
);
155 unsigned long (*query_bus_param
)(struct soc_camera_subdev_desc
*);
156 void (*free_bus
)(struct soc_camera_subdev_desc
*);
158 /* Optional regulators that have to be managed on power on/off events */
159 struct v4l2_subdev_platform_data sd_pdata
;
162 struct soc_camera_host_desc
{
163 /* Camera bus id, used to match a camera and a bus */
166 struct i2c_board_info
*board_info
;
167 const char *module_name
;
170 * For non-I2C devices platform has to provide methods to add a device
171 * to the system and to remove it
173 int (*add_device
)(struct soc_camera_device
*);
174 void (*del_device
)(struct soc_camera_device
*);
178 * Platform data for "soc-camera-pdrv"
179 * This MUST be kept binary-identical to struct soc_camera_link below, until
180 * it is completely replaced by this one, after which we can split it into its
183 struct soc_camera_desc
{
184 struct soc_camera_subdev_desc subdev_desc
;
185 struct soc_camera_host_desc host_desc
;
188 /* Prepare to replace this struct: don't change its layout any more! */
189 struct soc_camera_link
{
191 * Subdevice part - keep at top and compatible to
192 * struct soc_camera_subdev_desc
195 /* Per camera SOCAM_SENSOR_* bus flags */
200 /* Set by platforms to handle misbehaving drivers */
201 bool unbalanced_power
;
202 /* Used by soc-camera helper functions */
203 unsigned long clock_state
;
205 /* Optional callbacks to power on or off and reset the sensor */
206 int (*power
)(struct device
*, int);
207 int (*reset
)(struct device
*);
209 * some platforms may support different data widths than the sensors
210 * native ones due to different data line routing. Let the board code
211 * overwrite the width flags.
213 int (*set_bus_param
)(struct soc_camera_link
*, unsigned long flags
);
214 unsigned long (*query_bus_param
)(struct soc_camera_link
*);
215 void (*free_bus
)(struct soc_camera_link
*);
217 /* Optional regulators that have to be managed on power on/off events */
218 struct regulator_bulk_data
*regulators
;
224 * Host part - keep at bottom and compatible to
225 * struct soc_camera_host_desc
228 /* Camera bus id, used to match a camera and a bus */
231 struct i2c_board_info
*board_info
;
232 const char *module_name
;
235 * For non-I2C devices platform has to provide methods to add a device
236 * to the system and to remove it
238 int (*add_device
)(struct soc_camera_device
*);
239 void (*del_device
)(struct soc_camera_device
*);
242 static inline struct soc_camera_host
*to_soc_camera_host(
243 const struct device
*dev
)
245 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
247 return container_of(v4l2_dev
, struct soc_camera_host
, v4l2_dev
);
250 static inline struct soc_camera_desc
*to_soc_camera_desc(
251 const struct soc_camera_device
*icd
)
256 static inline struct device
*to_soc_camera_control(
257 const struct soc_camera_device
*icd
)
262 static inline struct v4l2_subdev
*soc_camera_to_subdev(
263 const struct soc_camera_device
*icd
)
265 struct device
*control
= to_soc_camera_control(icd
);
266 return dev_get_drvdata(control
);
269 int soc_camera_host_register(struct soc_camera_host
*ici
);
270 void soc_camera_host_unregister(struct soc_camera_host
*ici
);
272 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
273 struct soc_camera_device
*icd
, unsigned int fourcc
);
276 * struct soc_camera_format_xlate - match between host and sensor formats
277 * @code: code of a sensor provided format
278 * @host_fmt: host format after host translation from code
280 * Host and sensor translation structure. Used in table of host and sensor
281 * formats matchings in soc_camera_device. A host can override the generic list
282 * generation by implementing get_formats(), and use it for format checks and
285 struct soc_camera_format_xlate
{
287 const struct soc_mbus_pixelfmt
*host_fmt
;
290 #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
293 * This struct can be attached to struct soc_camera_device by the host driver
294 * to request sense from the camera, for example, when calling .set_fmt(). The
295 * host then can check which flags are set and verify respective values if any.
296 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
297 * changed during this operation. After completion the host should detach sense.
299 * @flags ored SOCAM_SENSE_* flags
300 * @master_clock if the host wants to be informed about pixel-clock
301 * change, it better set master_clock.
302 * @pixel_clock_max maximum pixel clock frequency supported by the host,
303 * camera is not allowed to exceed this.
304 * @pixel_clock if the camera driver changed pixel clock during this
305 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
306 * master_clock to calculate the new pixel-clock and
309 struct soc_camera_sense
{
311 unsigned long master_clock
;
312 unsigned long pixel_clock_max
;
313 unsigned long pixel_clock
;
316 #define SOCAM_DATAWIDTH(x) BIT((x) - 1)
317 #define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
318 #define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
319 #define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
320 #define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
321 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
322 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
323 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
324 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
325 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
327 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
328 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
329 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
330 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
333 static inline void soc_camera_limit_side(int *start
, int *length
,
334 unsigned int start_min
,
335 unsigned int length_min
, unsigned int length_max
)
337 if (*length
< length_min
)
338 *length
= length_min
;
339 else if (*length
> length_max
)
340 *length
= length_max
;
342 if (*start
< start_min
)
344 else if (*start
> start_min
+ length_max
- *length
)
345 *start
= start_min
+ length_max
- *length
;
348 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc
*ssdd
,
349 const struct v4l2_mbus_config
*cfg
);
351 int soc_camera_power_init(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
);
352 int soc_camera_power_on(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
353 struct v4l2_clk
*clk
);
354 int soc_camera_power_off(struct device
*dev
, struct soc_camera_subdev_desc
*ssdd
,
355 struct v4l2_clk
*clk
);
357 static inline int soc_camera_set_power(struct device
*dev
,
358 struct soc_camera_subdev_desc
*ssdd
, struct v4l2_clk
*clk
, bool on
)
360 return on
? soc_camera_power_on(dev
, ssdd
, clk
)
361 : soc_camera_power_off(dev
, ssdd
, clk
);
364 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
365 #include <linux/i2c.h>
366 static inline struct video_device
*soc_camera_i2c_to_vdev(const struct i2c_client
*client
)
368 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
369 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
370 return icd
? icd
->vdev
: NULL
;
373 static inline struct soc_camera_subdev_desc
*soc_camera_i2c_to_desc(const struct i2c_client
*client
)
375 return client
->dev
.platform_data
;
378 static inline struct v4l2_subdev
*soc_camera_vdev_to_subdev(struct video_device
*vdev
)
380 struct soc_camera_device
*icd
= video_get_drvdata(vdev
);
381 return soc_camera_to_subdev(icd
);
384 static inline struct soc_camera_device
*soc_camera_from_vb2q(const struct vb2_queue
*vq
)
386 return container_of(vq
, struct soc_camera_device
, vb2_vidq
);
389 static inline u32
soc_camera_grp_id(const struct soc_camera_device
*icd
)
391 return (icd
->iface
<< 8) | (icd
->devnum
+ 1);
394 void soc_camera_lock(struct vb2_queue
*vq
);
395 void soc_camera_unlock(struct vb2_queue
*vq
);