ARM: rockchip: rk3228: add grf definition
[firefly-linux-kernel-4.4.55.git] / include / media / soc_camera.h
1 /*
2  * camera image capture (abstract) bus driver header
3  *
4  * Copyright (C) 2006, Sascha Hauer, Pengutronix
5  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6  *
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.
10  */
11
12 #ifndef SOC_CAMERA_H
13 #define SOC_CAMERA_H
14
15 #include <linux/bitops.h>
16 #include <linux/device.h>
17 #include <linux/mutex.h>
18 #include <linux/pm.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>
24
25 struct file;
26 struct soc_camera_desc;
27
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 */
34         s32 user_width;
35         s32 user_height;
36         u32 bytesperline;               /* for padding, zero if unused */
37         u32 sizeimage;
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
43         struct soc_camera_ops *ops;/*yzm*/
44         struct mutex video_lock;/*yzm*/
45
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;
50         int num_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 */
54         int use_count;
55         struct file *streamer;          /* stream owner */
56         union {
57                 struct videobuf_queue vb_vidq;
58                 struct vb2_queue vb2_vidq;
59         };
60 };
61
62 /* Host supports programmable stride */
63 #define SOCAM_HOST_CAP_STRIDE           (1 << 0)
64
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 */
70         u32 capabilities;
71         void *priv;
72         const char *drv_name;
73         struct soc_camera_host_ops *ops;
74 };
75
76 struct soc_camera_host_ops {
77         struct module *owner;
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;
88         int num_controls;
89         /***************yzm*****************/
90         
91         /*
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.
96          */
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 *);
105         /*
106          * The difference to .set_crop() is, that .set_livecrop is not allowed
107          * to change the output sizes
108          */
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 *);
123 };
124
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)
130
131 struct i2c_board_info;
132 struct regulator_bulk_data;
133
134 struct soc_camera_subdev_desc {
135         /* Per camera SOCAM_SENSOR_* bus flags */
136         unsigned long flags;
137
138         /* sensor driver private platform data */
139         void *drv_priv;
140
141         struct soc_camera_device *socdev;/*yzm*/
142         
143         /* Optional regulators that have to be managed on power on/off events */
144         struct regulator_bulk_data *regulators;
145         int num_regulators;
146
147         /* Optional callbacks to power on or off and reset the sensor */
148         int (*power)(struct device *, int);
149         int (*reset)(struct device *);
150
151         int (*powerdown)(struct device *, int);/*yzm*/
152         /*
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.
156          */
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 *);
160 };
161
162 struct soc_camera_host_desc {
163         /* Camera bus id, used to match a camera and a bus */
164         int bus_id;
165         int i2c_adapter_id;
166         struct i2c_board_info *board_info;
167         const char *module_name;
168
169         /*
170          * For non-I2C devices platform has to provide methods to add a device
171          * to the system and to remove it
172          */
173         int (*add_device)(struct soc_camera_device *);
174         void (*del_device)(struct soc_camera_device *);
175 };
176
177 /*
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
180  * two components.
181  */
182 struct soc_camera_desc {
183         struct soc_camera_subdev_desc subdev_desc;
184         struct soc_camera_host_desc host_desc;
185 };
186
187 /* Prepare to replace this struct: don't change its layout any more! */
188 struct soc_camera_link {
189         /*
190          * Subdevice part - keep at top and compatible to
191          * struct soc_camera_subdev_desc
192          */
193
194         /* Per camera SOCAM_SENSOR_* bus flags */
195         unsigned long flags;
196
197         void *priv;
198         void *priv_usr;         /*yzm*/
199         /* Optional regulators that have to be managed on power on/off events */
200         struct regulator_bulk_data *regulators;
201         int num_regulators;
202
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*/
207         /*
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.
211          */
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 *);
215
216         /*
217          * Host part - keep at bottom and compatible to
218          * struct soc_camera_host_desc
219          */
220
221         /* Camera bus id, used to match a camera and a bus */
222         int bus_id;
223         int i2c_adapter_id;
224         struct i2c_board_info *board_info;
225         const char *module_name;
226
227         /*
228          * For non-I2C devices platform has to provide methods to add a device
229          * to the system and to remove it
230          */
231         int (*add_device)(struct soc_camera_device *);
232         void (*del_device)(struct soc_camera_device *);
233 };
234
235 static inline struct soc_camera_host *to_soc_camera_host(
236         const struct device *dev)
237 {
238         struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
239
240         return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
241 }
242
243 static inline struct soc_camera_desc *to_soc_camera_desc(
244         const struct soc_camera_device *icd)
245 {
246         return icd->sdesc;
247 }
248
249 static inline struct device *to_soc_camera_control(
250         const struct soc_camera_device *icd)
251 {
252         return icd->control;
253 }
254
255 static inline struct v4l2_subdev *soc_camera_to_subdev(
256         const struct soc_camera_device *icd)
257 {
258         struct device *control = to_soc_camera_control(icd);
259         return dev_get_drvdata(control);
260 }
261
262 int soc_camera_host_register(struct soc_camera_host *ici);
263 void soc_camera_host_unregister(struct soc_camera_host *ici);
264
265 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
266         struct soc_camera_device *icd, unsigned int fourcc);
267
268 /**
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
272  *
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
276  * format setup.
277  */
278 struct soc_camera_format_xlate {
279         enum v4l2_mbus_pixelcode code;
280         const struct soc_mbus_pixelfmt *host_fmt;
281 };
282
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;
292         int num_controls;
293         int num_menus;
294 };      
295 /****************yzm***************/
296
297 #define SOCAM_SENSE_PCLK_CHANGED        (1 << 0)
298
299 /**
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.
305  *
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
314  *                      sets this field.
315  */
316 struct soc_camera_sense {
317         unsigned long flags;
318         unsigned long master_clock;
319         unsigned long pixel_clock_max;
320         unsigned long pixel_clock;
321 };
322 /***************yzm****************/
323 static inline struct v4l2_queryctrl const *soc_camera_find_qctrl(
324         struct soc_camera_ops *ops, int id)
325 {
326         int i;
327
328         for (i = 0; i < ops->num_controls; i++)
329                 if (ops->controls[i].id == id)
330                         return &ops->controls[i];
331
332         return NULL;
333 }
334 /***************yzm****************rnd*/
335
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 | \
354                               SOCAM_DATAWIDTH_24)
355
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)
359 {
360         if (*length < length_min)
361                 *length = length_min;
362         else if (*length > length_max)
363                 *length = length_max;
364
365         if (*start < start_min)
366                 *start = start_min;
367         else if (*start > start_min + length_max - *length)
368                 *start = start_min + length_max - *length;
369 }
370
371 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
372                                            const struct v4l2_mbus_config *cfg);
373
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);
376
377 static inline int soc_camera_set_power(struct device *dev,
378                                 struct soc_camera_subdev_desc *ssdd, bool on)
379 {
380         return on ? soc_camera_power_on(dev, ssdd)
381                   : soc_camera_power_off(dev, ssdd);
382 }
383
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)
387 {
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;
391 }
392
393 static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
394 {
395         return client->dev.platform_data;
396 }
397
398 static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(const struct video_device *vdev)
399 {
400         struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
401         return soc_camera_to_subdev(icd);
402 }
403
404 static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
405 {
406         return container_of(vq, struct soc_camera_device, vb2_vidq);
407 }
408
409 static inline struct soc_camera_device *soc_camera_from_vbq(const struct videobuf_queue *vq)
410 {
411         return container_of(vq, struct soc_camera_device, vb_vidq);
412 }
413
414 static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
415 {
416         return (icd->iface << 8) | (icd->devnum + 1);
417 }
418
419 void soc_camera_lock(struct vb2_queue *vq);
420 void soc_camera_unlock(struct vb2_queue *vq);
421
422 #endif