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 */
43 struct soc_camera_ops *ops;/*yzm*/
44 struct mutex video_lock;/*yzm*/
46 struct video_device *vdev;
47 struct v4l2_ctrl_handler ctrl_handler;
48 const struct soc_camera_format_xlate *current_fmt;
49 struct soc_camera_format_xlate *user_formats;
51 enum v4l2_field field; /* Preserve field over close() */
52 void *host_priv; /* Per-device host private data */
53 /* soc_camera.c private count. Only accessed with .host_lock held */
55 struct file *streamer; /* stream owner */
57 struct videobuf_queue vb_vidq;
58 struct vb2_queue vb2_vidq;
62 /* Host supports programmable stride */
63 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
65 struct soc_camera_host {
66 struct v4l2_device v4l2_dev;
67 struct list_head list;
68 struct mutex host_lock; /* Protect pipeline modifications */
69 unsigned char nr; /* Host number */
73 struct soc_camera_host_ops *ops;
76 struct soc_camera_host_ops {
78 int (*add)(struct soc_camera_device *);
79 void (*remove)(struct soc_camera_device *);
80 /****************yzm**************/
81 int (*suspend)(struct soc_camera_device *, pm_message_t);
82 int (*resume)(struct soc_camera_device *);
83 int (*enum_frameinervals)(struct soc_camera_device *, struct v4l2_frmivalenum *);/* ddl@rock-chips.com :Add ioctrl - VIDIOC_ENUM_FRAMEINTERVALS for soc-camera */
84 int (*get_ctrl)(struct soc_camera_device *, struct v4l2_control *);
85 int (*set_ctrl)(struct soc_camera_device *, struct v4l2_control *);
86 int (*s_stream)(struct soc_camera_device *, int enable);
87 const struct v4l2_queryctrl *controls;
89 /***************yzm*****************/
92 * .get_formats() is called for each client device format, but
93 * .put_formats() is only called once. Further, if any of the calls to
94 * .get_formats() fail, .put_formats() will not be called at all, the
95 * failing .get_formats() must then clean up internally.
97 int (*get_formats)(struct soc_camera_device *, unsigned int,
98 struct soc_camera_format_xlate *);
99 void (*put_formats)(struct soc_camera_device *);
100 int (*cropcap)(struct soc_camera_device *, struct v4l2_cropcap *);
101 int (*get_crop)(struct soc_camera_device *, struct v4l2_crop *);
102 int (*set_crop)(struct soc_camera_device *, const struct v4l2_crop *);
103 int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
104 int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
106 * The difference to .set_crop() is, that .set_livecrop is not allowed
107 * to change the output sizes
109 int (*set_livecrop)(struct soc_camera_device *, const struct v4l2_crop *);
110 int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
111 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
112 void (*init_videobuf)(struct videobuf_queue *,
113 struct soc_camera_device *);
114 int (*init_videobuf2)(struct vb2_queue *,
115 struct soc_camera_device *);
116 int (*reqbufs)(struct soc_camera_device *, struct v4l2_requestbuffers *);
117 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
118 int (*set_bus_param)(struct soc_camera_device *, __u32);/*yzm*/
119 int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
120 int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
121 int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
122 unsigned int (*poll)(struct file *, poll_table *);
125 #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
126 #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
127 #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
128 #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
129 #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
131 struct i2c_board_info;
132 struct regulator_bulk_data;
134 struct soc_camera_subdev_desc {
135 /* Per camera SOCAM_SENSOR_* bus flags */
138 /* sensor driver private platform data */
141 struct soc_camera_device *socdev;/*yzm*/
143 /* Optional regulators that have to be managed on power on/off events */
144 struct regulator_bulk_data *regulators;
147 /* Optional callbacks to power on or off and reset the sensor */
148 int (*power)(struct device *, int);
149 int (*reset)(struct device *);
151 int (*powerdown)(struct device *, int);/*yzm*/
153 * some platforms may support different data widths than the sensors
154 * native ones due to different data line routing. Let the board code
155 * overwrite the width flags.
157 int (*set_bus_param)(struct soc_camera_subdev_desc *, unsigned long flags);
158 unsigned long (*query_bus_param)(struct soc_camera_subdev_desc *);
159 void (*free_bus)(struct soc_camera_subdev_desc *);
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 * This MUST be kept binary-identical to struct soc_camera_link below, until
179 * it is completely replaced by this one, after which we can split it into its
182 struct soc_camera_desc {
183 struct soc_camera_subdev_desc subdev_desc;
184 struct soc_camera_host_desc host_desc;
187 /* Prepare to replace this struct: don't change its layout any more! */
188 struct soc_camera_link {
190 * Subdevice part - keep at top and compatible to
191 * struct soc_camera_subdev_desc
194 /* Per camera SOCAM_SENSOR_* bus flags */
198 void *priv_usr; /*yzm*/
199 /* Optional regulators that have to be managed on power on/off events */
200 struct regulator_bulk_data *regulators;
203 /* Optional callbacks to power on or off and reset the sensor */
204 int (*power)(struct device *, int);
205 int (*reset)(struct device *);
206 int (*powerdown)(struct device *,int); /*yzm*/
208 * some platforms may support different data widths than the sensors
209 * native ones due to different data line routing. Let the board code
210 * overwrite the width flags.
212 int (*set_bus_param)(struct soc_camera_link *, unsigned long flags);
213 unsigned long (*query_bus_param)(struct soc_camera_link *);
214 void (*free_bus)(struct soc_camera_link *);
217 * Host part - keep at bottom and compatible to
218 * struct soc_camera_host_desc
221 /* Camera bus id, used to match a camera and a bus */
224 struct i2c_board_info *board_info;
225 const char *module_name;
228 * For non-I2C devices platform has to provide methods to add a device
229 * to the system and to remove it
231 int (*add_device)(struct soc_camera_device *);
232 void (*del_device)(struct soc_camera_device *);
235 static inline struct soc_camera_host *to_soc_camera_host(
236 const struct device *dev)
238 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
240 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
243 static inline struct soc_camera_desc *to_soc_camera_desc(
244 const struct soc_camera_device *icd)
249 static inline struct device *to_soc_camera_control(
250 const struct soc_camera_device *icd)
255 static inline struct v4l2_subdev *soc_camera_to_subdev(
256 const struct soc_camera_device *icd)
258 struct device *control = to_soc_camera_control(icd);
259 return dev_get_drvdata(control);
262 int soc_camera_host_register(struct soc_camera_host *ici);
263 void soc_camera_host_unregister(struct soc_camera_host *ici);
265 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
266 struct soc_camera_device *icd, unsigned int fourcc);
269 * struct soc_camera_format_xlate - match between host and sensor formats
270 * @code: code of a sensor provided format
271 * @host_fmt: host format after host translation from code
273 * Host and sensor translation structure. Used in table of host and sensor
274 * formats matchings in soc_camera_device. A host can override the generic list
275 * generation by implementing get_formats(), and use it for format checks and
278 struct soc_camera_format_xlate {
279 enum v4l2_mbus_pixelcode code;
280 const struct soc_mbus_pixelfmt *host_fmt;
283 /*****************yzm***************/
284 struct soc_camera_ops {
285 int (*suspend)(struct soc_camera_device *, pm_message_t state);
286 int (*resume)(struct soc_camera_device *);
287 unsigned long (*query_bus_param)(struct soc_camera_device *);
288 int (*set_bus_param)(struct soc_camera_device *, unsigned long);
289 int (*enum_input)(struct soc_camera_device *, struct v4l2_input *);
290 const struct v4l2_queryctrl *controls;
291 struct v4l2_querymenu *menus;
295 /****************yzm***************/
297 #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
300 * This struct can be attached to struct soc_camera_device by the host driver
301 * to request sense from the camera, for example, when calling .set_fmt(). The
302 * host then can check which flags are set and verify respective values if any.
303 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
304 * changed during this operation. After completion the host should detach sense.
306 * @flags ored SOCAM_SENSE_* flags
307 * @master_clock if the host wants to be informed about pixel-clock
308 * change, it better set master_clock.
309 * @pixel_clock_max maximum pixel clock frequency supported by the host,
310 * camera is not allowed to exceed this.
311 * @pixel_clock if the camera driver changed pixel clock during this
312 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
313 * master_clock to calculate the new pixel-clock and
316 struct soc_camera_sense {
318 unsigned long master_clock;
319 unsigned long pixel_clock_max;
320 unsigned long pixel_clock;
322 /***************yzm****************/
323 static inline struct v4l2_queryctrl const *soc_camera_find_qctrl(
324 struct soc_camera_ops *ops, int id)
328 for (i = 0; i < ops->num_controls; i++)
329 if (ops->controls[i].id == id)
330 return &ops->controls[i];
334 /***************yzm****************rnd*/
336 #define SOCAM_DATAWIDTH(x) BIT((x) - 1)
337 #define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
338 #define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
339 #define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
340 #define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
341 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
342 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
343 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
344 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
345 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
346 /**************yzm***********/
347 #define SOCAM_MCLK_24MHZ (1<<29)
348 #define SOCAM_MCLK_48MHZ (1<<31)
349 //*************yzm***********end
350 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
351 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
352 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
353 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
356 static inline void soc_camera_limit_side(int *start, int *length,
357 unsigned int start_min,
358 unsigned int length_min, unsigned int length_max)
360 if (*length < length_min)
361 *length = length_min;
362 else if (*length > length_max)
363 *length = length_max;
365 if (*start < start_min)
367 else if (*start > start_min + length_max - *length)
368 *start = start_min + length_max - *length;
371 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
372 const struct v4l2_mbus_config *cfg);
374 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd);
375 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd);
377 static inline int soc_camera_set_power(struct device *dev,
378 struct soc_camera_subdev_desc *ssdd, bool on)
380 return on ? soc_camera_power_on(dev, ssdd)
381 : soc_camera_power_off(dev, ssdd);
384 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
385 #include <linux/i2c.h>
386 static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
388 struct v4l2_subdev *sd = i2c_get_clientdata(client);
389 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
390 return icd ? icd->vdev : NULL;
393 static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
395 return client->dev.platform_data;
398 static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(const struct video_device *vdev)
400 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
401 return soc_camera_to_subdev(icd);
404 static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
406 return container_of(vq, struct soc_camera_device, vb2_vidq);
409 static inline struct soc_camera_device *soc_camera_from_vbq(const struct videobuf_queue *vq)
411 return container_of(vq, struct soc_camera_device, vb_vidq);
414 static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
416 return (icd->iface << 8) | (icd->devnum + 1);
419 void soc_camera_lock(struct vb2_queue *vq);
420 void soc_camera_unlock(struct vb2_queue *vq);