]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/media/soc_camera.h
[media] V4L2: support asynchronous subdevice registration
[mirror_ubuntu-zesty-kernel.git] / include / media / soc_camera.h
CommitLineData
e55222ef
GL
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
d839fe17 15#include <linux/bitops.h>
52d268a3 16#include <linux/device.h>
1c3bb743
GL
17#include <linux/mutex.h>
18#include <linux/pm.h>
e55222ef 19#include <linux/videodev2.h>
092d3921 20#include <media/videobuf-core.h>
592c2aba 21#include <media/videobuf2-core.h>
ee02da64 22#include <media/v4l2-ctrls.h>
979ea1dd 23#include <media/v4l2-device.h>
e55222ef 24
57bee29d 25struct file;
25a34811 26struct soc_camera_desc;
57bee29d 27
e55222ef 28struct soc_camera_device {
d33b290a 29 struct list_head list; /* list of all registered devices */
25a34811 30 struct soc_camera_desc *sdesc;
979ea1dd 31 struct device *pdev; /* Platform device */
7dfff953
GL
32 struct device *parent; /* Camera host device */
33 struct device *control; /* E.g., the i2c client */
6a6c8786
GL
34 s32 user_width;
35 s32 user_height;
0e4c180d
SA
36 u32 bytesperline; /* for padding, zero if unused */
37 u32 sizeimage;
760697be 38 enum v4l2_colorspace colorspace;
e55222ef
GL
39 unsigned char iface; /* Host number */
40 unsigned char devnum; /* Device number per host */
a9bef518 41 struct soc_camera_sense *sense; /* See comment in struct definition */
e55222ef 42 struct video_device *vdev;
ee02da64 43 struct v4l2_ctrl_handler ctrl_handler;
760697be 44 const struct soc_camera_format_xlate *current_fmt;
c2786ad2
GL
45 struct soc_camera_format_xlate *user_formats;
46 int num_user_formats;
025c18a1 47 enum v4l2_field field; /* Preserve field over close() */
1c3bb743 48 void *host_priv; /* Per-device host private data */
dd669e90 49 /* soc_camera.c private count. Only accessed with .host_lock held */
9dc4e48f 50 int use_count;
57bee29d 51 struct file *streamer; /* stream owner */
592c2aba
GL
52 union {
53 struct videobuf_queue vb_vidq;
54 struct vb2_queue vb2_vidq;
55 };
e55222ef
GL
56};
57
914f05c8
LP
58/* Host supports programmable stride */
59#define SOCAM_HOST_CAP_STRIDE (1 << 0)
60
e55222ef 61struct soc_camera_host {
979ea1dd 62 struct v4l2_device v4l2_dev;
e55222ef 63 struct list_head list;
7d051b35 64 struct mutex host_lock; /* Protect pipeline modifications */
2f9a0c88 65 unsigned char nr; /* Host number */
914f05c8 66 u32 capabilities;
f7f6ce2d 67 struct soc_camera_device *icd; /* Currently attached client */
e55222ef 68 void *priv;
af128a10 69 const char *drv_name;
b8d9904c
GL
70 struct soc_camera_host_ops *ops;
71};
72
73struct soc_camera_host_ops {
74 struct module *owner;
e55222ef
GL
75 int (*add)(struct soc_camera_device *);
76 void (*remove)(struct soc_camera_device *);
eb569cf9
GL
77 int (*clock_start)(struct soc_camera_host *);
78 void (*clock_stop)(struct soc_camera_host *);
fa48984e
GL
79 /*
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.
84 */
3805f201 85 int (*get_formats)(struct soc_camera_device *, unsigned int,
c2786ad2 86 struct soc_camera_format_xlate *);
fa48984e 87 void (*put_formats)(struct soc_camera_device *);
6a6c8786
GL
88 int (*cropcap)(struct soc_camera_device *, struct v4l2_cropcap *);
89 int (*get_crop)(struct soc_camera_device *, struct v4l2_crop *);
4f996594 90 int (*set_crop)(struct soc_camera_device *, const struct v4l2_crop *);
3bfb4100
GL
91 int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
92 int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
aee5c2f1
GL
93 /*
94 * The difference to .set_crop() is, that .set_livecrop is not allowed
95 * to change the output sizes
96 */
4f996594 97 int (*set_livecrop)(struct soc_camera_device *, const struct v4l2_crop *);
09e231b3 98 int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
d8fac217 99 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
a034d1b7 100 void (*init_videobuf)(struct videobuf_queue *,
092d3921 101 struct soc_camera_device *);
592c2aba
GL
102 int (*init_videobuf2)(struct vb2_queue *,
103 struct soc_camera_device *);
57bee29d 104 int (*reqbufs)(struct soc_camera_device *, struct v4l2_requestbuffers *);
e55222ef 105 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
8843d119 106 int (*set_bus_param)(struct soc_camera_device *);
c9f6ef69
GL
107 int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
108 int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
ad3537b5 109 int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
e55222ef
GL
110 unsigned int (*poll)(struct file *, poll_table *);
111};
112
bd73b36f
GL
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)
118
0fd327bd 119struct i2c_board_info;
96e442c1 120struct regulator_bulk_data;
0fd327bd 121
25a34811 122struct soc_camera_subdev_desc {
bd73b36f
GL
123 /* Per camera SOCAM_SENSOR_* bus flags */
124 unsigned long flags;
25a34811
GL
125
126 /* sensor driver private platform data */
127 void *drv_priv;
3fd7ceff 128
96e442c1
AP
129 /* Optional regulators that have to be managed on power on/off events */
130 struct regulator_bulk_data *regulators;
131 int num_regulators;
132
25a34811
GL
133 /* Optional callbacks to power on or off and reset the sensor */
134 int (*power)(struct device *, int);
135 int (*reset)(struct device *);
136
137 /*
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.
141 */
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 *);
145};
146
147struct soc_camera_host_desc {
148 /* Camera bus id, used to match a camera and a bus */
149 int bus_id;
150 int i2c_adapter_id;
151 struct i2c_board_info *board_info;
152 const char *module_name;
153
c41debaf 154 /*
d33b290a
GL
155 * For non-I2C devices platform has to provide methods to add a device
156 * to the system and to remove it
c41debaf 157 */
7dfff953
GL
158 int (*add_device)(struct soc_camera_device *);
159 void (*del_device)(struct soc_camera_device *);
25a34811
GL
160};
161
162/*
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
165 * two components.
166 */
167struct soc_camera_desc {
168 struct soc_camera_subdev_desc subdev_desc;
169 struct soc_camera_host_desc host_desc;
170};
171
172/* Prepare to replace this struct: don't change its layout any more! */
173struct soc_camera_link {
174 /*
175 * Subdevice part - keep at top and compatible to
176 * struct soc_camera_subdev_desc
177 */
178
179 /* Per camera SOCAM_SENSOR_* bus flags */
180 unsigned long flags;
181
182 void *priv;
183
184 /* Optional regulators that have to be managed on power on/off events */
185 struct regulator_bulk_data *regulators;
186 int num_regulators;
187
81034663
SH
188 /* Optional callbacks to power on or off and reset the sensor */
189 int (*power)(struct device *, int);
190 int (*reset)(struct device *);
28f59339
SH
191 /*
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.
195 */
196 int (*set_bus_param)(struct soc_camera_link *, unsigned long flags);
197 unsigned long (*query_bus_param)(struct soc_camera_link *);
594bb46d 198 void (*free_bus)(struct soc_camera_link *);
25a34811
GL
199
200 /*
201 * Host part - keep at bottom and compatible to
202 * struct soc_camera_host_desc
203 */
204
205 /* Camera bus id, used to match a camera and a bus */
206 int bus_id;
207 int i2c_adapter_id;
208 struct i2c_board_info *board_info;
209 const char *module_name;
210
211 /*
212 * For non-I2C devices platform has to provide methods to add a device
213 * to the system and to remove it
214 */
215 int (*add_device)(struct soc_camera_device *);
216 void (*del_device)(struct soc_camera_device *);
e55222ef
GL
217};
218
96c75399
GL
219static inline struct soc_camera_host *to_soc_camera_host(
220 const struct device *dev)
e55222ef 221{
979ea1dd
GL
222 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
223
224 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
e55222ef
GL
225}
226
25a34811 227static inline struct soc_camera_desc *to_soc_camera_desc(
96c75399 228 const struct soc_camera_device *icd)
40e2e092 229{
25a34811 230 return icd->sdesc;
40e2e092 231}
e55222ef 232
96c75399
GL
233static inline struct device *to_soc_camera_control(
234 const struct soc_camera_device *icd)
40e2e092 235{
7dfff953 236 return icd->control;
40e2e092 237}
e55222ef 238
96c75399
GL
239static inline struct v4l2_subdev *soc_camera_to_subdev(
240 const struct soc_camera_device *icd)
c9c1f1c0
GL
241{
242 struct device *control = to_soc_camera_control(icd);
243 return dev_get_drvdata(control);
244}
245
40e2e092
GL
246int soc_camera_host_register(struct soc_camera_host *ici);
247void soc_camera_host_unregister(struct soc_camera_host *ici);
248
40e2e092 249const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
c2786ad2 250 struct soc_camera_device *icd, unsigned int fourcc);
25c4d74e 251
c2786ad2
GL
252/**
253 * struct soc_camera_format_xlate - match between host and sensor formats
760697be
GL
254 * @code: code of a sensor provided format
255 * @host_fmt: host format after host translation from code
c2786ad2
GL
256 *
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
260 * format setup.
261 */
262struct soc_camera_format_xlate {
760697be
GL
263 enum v4l2_mbus_pixelcode code;
264 const struct soc_mbus_pixelfmt *host_fmt;
c2786ad2
GL
265};
266
a9bef518
GL
267#define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
268
269/**
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.
275 *
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
284 * sets this field.
285 */
286struct soc_camera_sense {
287 unsigned long flags;
288 unsigned long master_clock;
289 unsigned long pixel_clock_max;
290 unsigned long pixel_clock;
291};
292
d839fe17
GL
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)
7b88fc08 298#define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
d839fe17
GL
299#define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
300#define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
7b88fc08
PE
301#define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
302#define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
042d8790
GL
303
304#define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
305 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
7b88fc08
PE
306 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
307 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
308 SOCAM_DATAWIDTH_24)
ad5f2e85 309
e26b3144
MN
310static inline void soc_camera_limit_side(int *start, int *length,
311 unsigned int start_min,
6a6c8786
GL
312 unsigned int length_min, unsigned int length_max)
313{
314 if (*length < length_min)
315 *length = length_min;
316 else if (*length > length_max)
317 *length = length_max;
318
319 if (*start < start_min)
320 *start = start_min;
321 else if (*start > start_min + length_max - *length)
322 *start = start_min + length_max - *length;
323}
324
25a34811 325unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
32c69fcc 326 const struct v4l2_mbus_config *cfg);
bd73b36f 327
25a34811
GL
328int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd);
329int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd);
4ec10bac
LP
330
331static inline int soc_camera_set_power(struct device *dev,
25a34811 332 struct soc_camera_subdev_desc *ssdd, bool on)
4ec10bac 333{
25a34811
GL
334 return on ? soc_camera_power_on(dev, ssdd)
335 : soc_camera_power_off(dev, ssdd);
4ec10bac
LP
336}
337
4f9fb5ed
MCC
338/* This is only temporary here - until v4l2-subdev begins to link to video_device */
339#include <linux/i2c.h>
14178aa5 340static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
4f9fb5ed 341{
2f0babb7 342 struct v4l2_subdev *sd = i2c_get_clientdata(client);
4c0b036d 343 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
14178aa5 344 return icd ? icd->vdev : NULL;
4f9fb5ed
MCC
345}
346
25a34811 347static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
14178aa5 348{
2f0babb7 349 return client->dev.platform_data;
14178aa5
GL
350}
351
14381c26 352static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
14178aa5 353{
14381c26 354 struct soc_camera_device *icd = video_get_drvdata(vdev);
14178aa5
GL
355 return soc_camera_to_subdev(icd);
356}
357
14178aa5 358static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
8eb4476d
GL
359{
360 return container_of(vq, struct soc_camera_device, vb2_vidq);
361}
362
14178aa5 363static inline struct soc_camera_device *soc_camera_from_vbq(const struct videobuf_queue *vq)
8eb4476d
GL
364{
365 return container_of(vq, struct soc_camera_device, vb_vidq);
366}
367
4c0b036d
GL
368static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
369{
370 return (icd->iface << 8) | (icd->devnum + 1);
371}
372
592c2aba
GL
373void soc_camera_lock(struct vb2_queue *vq);
374void soc_camera_unlock(struct vb2_queue *vq);
375
e55222ef 376#endif