Linux 4.13.16
[linux/fpc-iii.git] / include / media / soc_camera.h
blob4d8cb0796bc60d637cd69a7381cb93e1318a723d
1 /*
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.
12 #ifndef SOC_CAMERA_H
13 #define SOC_CAMERA_H
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/videobuf2-v4l2.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
25 struct file;
26 struct soc_camera_desc;
27 struct soc_camera_async_client;
29 struct soc_camera_device {
30 struct list_head list; /* list of all registered devices */
31 struct soc_camera_desc *sdesc;
32 struct device *pdev; /* Platform device */
33 struct device *parent; /* Camera host device */
34 struct device *control; /* E.g., the i2c client */
35 s32 user_width;
36 s32 user_height;
37 u32 bytesperline; /* for padding, zero if unused */
38 u32 sizeimage;
39 enum v4l2_colorspace colorspace;
40 unsigned char iface; /* Host number */
41 unsigned char devnum; /* Device number per host */
42 struct soc_camera_sense *sense; /* See comment in struct definition */
43 struct video_device *vdev;
44 struct v4l2_ctrl_handler ctrl_handler;
45 const struct soc_camera_format_xlate *current_fmt;
46 struct soc_camera_format_xlate *user_formats;
47 int num_user_formats;
48 enum v4l2_field field; /* Preserve field over close() */
49 void *host_priv; /* Per-device host private data */
50 /* soc_camera.c private count. Only accessed with .host_lock held */
51 int use_count;
52 struct file *streamer; /* stream owner */
53 struct v4l2_clk *clk;
54 /* Asynchronous subdevice management */
55 struct soc_camera_async_client *sasc;
56 /* video buffer queue */
57 struct vb2_queue vb2_vidq;
60 /* Host supports programmable stride */
61 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
63 enum soc_camera_subdev_role {
64 SOCAM_SUBDEV_DATA_SOURCE = 1,
65 SOCAM_SUBDEV_DATA_SINK,
66 SOCAM_SUBDEV_DATA_PROCESSOR,
69 struct soc_camera_async_subdev {
70 struct v4l2_async_subdev asd;
71 enum soc_camera_subdev_role role;
74 struct soc_camera_host {
75 struct v4l2_device v4l2_dev;
76 struct list_head list;
77 struct mutex host_lock; /* Main synchronisation lock */
78 struct mutex clk_lock; /* Protect pipeline modifications */
79 unsigned char nr; /* Host number */
80 u32 capabilities;
81 struct soc_camera_device *icd; /* Currently attached client */
82 void *priv;
83 const char *drv_name;
84 struct soc_camera_host_ops *ops;
85 struct v4l2_async_subdev **asd; /* Flat array, arranged in groups */
86 unsigned int *asd_sizes; /* 0-terminated array of asd group sizes */
89 struct soc_camera_host_ops {
90 struct module *owner;
91 int (*add)(struct soc_camera_device *);
92 void (*remove)(struct soc_camera_device *);
93 int (*clock_start)(struct soc_camera_host *);
94 void (*clock_stop)(struct soc_camera_host *);
96 * .get_formats() is called for each client device format, but
97 * .put_formats() is only called once. Further, if any of the calls to
98 * .get_formats() fail, .put_formats() will not be called at all, the
99 * failing .get_formats() must then clean up internally.
101 int (*get_formats)(struct soc_camera_device *, unsigned int,
102 struct soc_camera_format_xlate *);
103 void (*put_formats)(struct soc_camera_device *);
104 int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
105 int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
107 * The difference to .set_selection() is, that .set_liveselection is not allowed
108 * to change the output sizes
110 int (*set_liveselection)(struct soc_camera_device *, struct v4l2_selection *);
111 int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
112 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
113 int (*init_videobuf2)(struct vb2_queue *,
114 struct soc_camera_device *);
115 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
116 int (*set_bus_param)(struct soc_camera_device *);
117 int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
118 int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
119 int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
120 unsigned int (*poll)(struct file *, poll_table *);
123 #define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
124 #define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
125 #define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
126 #define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
127 #define SOCAM_SENSOR_INVERT_DATA (1 << 4)
129 struct i2c_board_info;
130 struct regulator_bulk_data;
132 struct soc_camera_subdev_desc {
133 /* Per camera SOCAM_SENSOR_* bus flags */
134 unsigned long flags;
136 /* sensor driver private platform data */
137 void *drv_priv;
140 * Set unbalanced_power to true to deal with legacy drivers, failing to
141 * balance their calls to subdevice's .s_power() method. clock_state is
142 * then used internally by helper functions, it shouldn't be touched by
143 * drivers or the platform code.
145 bool unbalanced_power;
146 unsigned long clock_state;
148 /* Optional callbacks to power on or off and reset the sensor */
149 int (*power)(struct device *, int);
150 int (*reset)(struct device *);
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 *);
161 /* Optional regulators that have to be managed on power on/off events */
162 struct v4l2_subdev_platform_data sd_pdata;
165 struct soc_camera_host_desc {
166 /* Camera bus id, used to match a camera and a bus */
167 int bus_id;
168 int i2c_adapter_id;
169 struct i2c_board_info *board_info;
170 const char *module_name;
173 * For non-I2C devices platform has to provide methods to add a device
174 * to the system and to remove it
176 int (*add_device)(struct soc_camera_device *);
177 void (*del_device)(struct soc_camera_device *);
181 * Platform data for "soc-camera-pdrv"
182 * This MUST be kept binary-identical to struct soc_camera_link below, until
183 * it is completely replaced by this one, after which we can split it into its
184 * two components.
186 struct soc_camera_desc {
187 struct soc_camera_subdev_desc subdev_desc;
188 struct soc_camera_host_desc host_desc;
191 /* Prepare to replace this struct: don't change its layout any more! */
192 struct soc_camera_link {
194 * Subdevice part - keep at top and compatible to
195 * struct soc_camera_subdev_desc
198 /* Per camera SOCAM_SENSOR_* bus flags */
199 unsigned long flags;
201 void *priv;
203 /* Set by platforms to handle misbehaving drivers */
204 bool unbalanced_power;
205 /* Used by soc-camera helper functions */
206 unsigned long clock_state;
208 /* Optional callbacks to power on or off and reset the sensor */
209 int (*power)(struct device *, int);
210 int (*reset)(struct device *);
212 * some platforms may support different data widths than the sensors
213 * native ones due to different data line routing. Let the board code
214 * overwrite the width flags.
216 int (*set_bus_param)(struct soc_camera_link *, unsigned long flags);
217 unsigned long (*query_bus_param)(struct soc_camera_link *);
218 void (*free_bus)(struct soc_camera_link *);
220 /* Optional regulators that have to be managed on power on/off events */
221 struct regulator_bulk_data *regulators;
222 int num_regulators;
224 void *host_priv;
227 * Host part - keep at bottom and compatible to
228 * struct soc_camera_host_desc
231 /* Camera bus id, used to match a camera and a bus */
232 int bus_id;
233 int i2c_adapter_id;
234 struct i2c_board_info *board_info;
235 const char *module_name;
238 * For non-I2C devices platform has to provide methods to add a device
239 * to the system and to remove it
241 int (*add_device)(struct soc_camera_device *);
242 void (*del_device)(struct soc_camera_device *);
245 static inline struct soc_camera_host *to_soc_camera_host(
246 const struct device *dev)
248 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
250 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
253 static inline struct soc_camera_desc *to_soc_camera_desc(
254 const struct soc_camera_device *icd)
256 return icd->sdesc;
259 static inline struct device *to_soc_camera_control(
260 const struct soc_camera_device *icd)
262 return icd->control;
265 static inline struct v4l2_subdev *soc_camera_to_subdev(
266 const struct soc_camera_device *icd)
268 struct device *control = to_soc_camera_control(icd);
269 return dev_get_drvdata(control);
272 int soc_camera_host_register(struct soc_camera_host *ici);
273 void soc_camera_host_unregister(struct soc_camera_host *ici);
275 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
276 struct soc_camera_device *icd, unsigned int fourcc);
279 * struct soc_camera_format_xlate - match between host and sensor formats
280 * @code: code of a sensor provided format
281 * @host_fmt: host format after host translation from code
283 * Host and sensor translation structure. Used in table of host and sensor
284 * formats matchings in soc_camera_device. A host can override the generic list
285 * generation by implementing get_formats(), and use it for format checks and
286 * format setup.
288 struct soc_camera_format_xlate {
289 u32 code;
290 const struct soc_mbus_pixelfmt *host_fmt;
293 #define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
296 * This struct can be attached to struct soc_camera_device by the host driver
297 * to request sense from the camera, for example, when calling .set_fmt(). The
298 * host then can check which flags are set and verify respective values if any.
299 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
300 * changed during this operation. After completion the host should detach sense.
302 * @flags ored SOCAM_SENSE_* flags
303 * @master_clock if the host wants to be informed about pixel-clock
304 * change, it better set master_clock.
305 * @pixel_clock_max maximum pixel clock frequency supported by the host,
306 * camera is not allowed to exceed this.
307 * @pixel_clock if the camera driver changed pixel clock during this
308 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
309 * master_clock to calculate the new pixel-clock and
310 * sets this field.
312 struct soc_camera_sense {
313 unsigned long flags;
314 unsigned long master_clock;
315 unsigned long pixel_clock_max;
316 unsigned long pixel_clock;
319 #define SOCAM_DATAWIDTH(x) BIT((x) - 1)
320 #define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
321 #define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
322 #define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
323 #define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
324 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
325 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
326 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
327 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
328 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
330 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
331 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
332 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
333 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
334 SOCAM_DATAWIDTH_24)
336 static inline void soc_camera_limit_side(int *start, int *length,
337 unsigned int start_min,
338 unsigned int length_min, unsigned int length_max)
340 if (*length < length_min)
341 *length = length_min;
342 else if (*length > length_max)
343 *length = length_max;
345 if (*start < start_min)
346 *start = start_min;
347 else if (*start > start_min + length_max - *length)
348 *start = start_min + length_max - *length;
351 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
352 const struct v4l2_mbus_config *cfg);
354 int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd);
355 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
356 struct v4l2_clk *clk);
357 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
358 struct v4l2_clk *clk);
360 static inline int soc_camera_set_power(struct device *dev,
361 struct soc_camera_subdev_desc *ssdd, struct v4l2_clk *clk, bool on)
363 return on ? soc_camera_power_on(dev, ssdd, clk)
364 : soc_camera_power_off(dev, ssdd, clk);
367 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
368 #include <linux/i2c.h>
369 static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
371 struct v4l2_subdev *sd = i2c_get_clientdata(client);
372 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
373 return icd ? icd->vdev : NULL;
376 static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
378 return client->dev.platform_data;
381 static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
383 struct soc_camera_device *icd = video_get_drvdata(vdev);
384 return soc_camera_to_subdev(icd);
387 static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
389 return container_of(vq, struct soc_camera_device, vb2_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);
400 #endif