]>
Commit | Line | Data |
---|---|---|
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> |
c139990e | 20 | #include <media/videobuf2-v4l2.h> |
e09da11d | 21 | #include <media/v4l2-async.h> |
ee02da64 | 22 | #include <media/v4l2-ctrls.h> |
979ea1dd | 23 | #include <media/v4l2-device.h> |
e55222ef | 24 | |
57bee29d | 25 | struct file; |
25a34811 | 26 | struct soc_camera_desc; |
e09da11d | 27 | struct soc_camera_async_client; |
57bee29d | 28 | |
e55222ef | 29 | struct soc_camera_device { |
d33b290a | 30 | struct list_head list; /* list of all registered devices */ |
25a34811 | 31 | struct soc_camera_desc *sdesc; |
979ea1dd | 32 | struct device *pdev; /* Platform device */ |
7dfff953 GL |
33 | struct device *parent; /* Camera host device */ |
34 | struct device *control; /* E.g., the i2c client */ | |
6a6c8786 GL |
35 | s32 user_width; |
36 | s32 user_height; | |
0e4c180d SA |
37 | u32 bytesperline; /* for padding, zero if unused */ |
38 | u32 sizeimage; | |
760697be | 39 | enum v4l2_colorspace colorspace; |
e55222ef GL |
40 | unsigned char iface; /* Host number */ |
41 | unsigned char devnum; /* Device number per host */ | |
a9bef518 | 42 | struct soc_camera_sense *sense; /* See comment in struct definition */ |
e55222ef | 43 | struct video_device *vdev; |
ee02da64 | 44 | struct v4l2_ctrl_handler ctrl_handler; |
760697be | 45 | const struct soc_camera_format_xlate *current_fmt; |
c2786ad2 GL |
46 | struct soc_camera_format_xlate *user_formats; |
47 | int num_user_formats; | |
025c18a1 | 48 | enum v4l2_field field; /* Preserve field over close() */ |
1c3bb743 | 49 | void *host_priv; /* Per-device host private data */ |
dd669e90 | 50 | /* soc_camera.c private count. Only accessed with .host_lock held */ |
9dc4e48f | 51 | int use_count; |
57bee29d | 52 | struct file *streamer; /* stream owner */ |
9aea470b | 53 | struct v4l2_clk *clk; |
e09da11d GL |
54 | /* Asynchronous subdevice management */ |
55 | struct soc_camera_async_client *sasc; | |
56 | /* video buffer queue */ | |
73a01599 | 57 | struct vb2_queue vb2_vidq; |
e55222ef GL |
58 | }; |
59 | ||
914f05c8 LP |
60 | /* Host supports programmable stride */ |
61 | #define SOCAM_HOST_CAP_STRIDE (1 << 0) | |
62 | ||
e09da11d GL |
63 | enum soc_camera_subdev_role { |
64 | SOCAM_SUBDEV_DATA_SOURCE = 1, | |
65 | SOCAM_SUBDEV_DATA_SINK, | |
66 | SOCAM_SUBDEV_DATA_PROCESSOR, | |
67 | }; | |
68 | ||
69 | struct soc_camera_async_subdev { | |
70 | struct v4l2_async_subdev asd; | |
71 | enum soc_camera_subdev_role role; | |
72 | }; | |
73 | ||
e55222ef | 74 | struct soc_camera_host { |
979ea1dd | 75 | struct v4l2_device v4l2_dev; |
e55222ef | 76 | struct list_head list; |
e09da11d GL |
77 | struct mutex host_lock; /* Main synchronisation lock */ |
78 | struct mutex clk_lock; /* Protect pipeline modifications */ | |
2f9a0c88 | 79 | unsigned char nr; /* Host number */ |
914f05c8 | 80 | u32 capabilities; |
f7f6ce2d | 81 | struct soc_camera_device *icd; /* Currently attached client */ |
e55222ef | 82 | void *priv; |
af128a10 | 83 | const char *drv_name; |
b8d9904c | 84 | struct soc_camera_host_ops *ops; |
e09da11d | 85 | struct v4l2_async_subdev **asd; /* Flat array, arranged in groups */ |
f687f326 | 86 | unsigned int *asd_sizes; /* 0-terminated array of asd group sizes */ |
b8d9904c GL |
87 | }; |
88 | ||
89 | struct soc_camera_host_ops { | |
90 | struct module *owner; | |
e55222ef GL |
91 | int (*add)(struct soc_camera_device *); |
92 | void (*remove)(struct soc_camera_device *); | |
eb569cf9 GL |
93 | int (*clock_start)(struct soc_camera_host *); |
94 | void (*clock_stop)(struct soc_camera_host *); | |
fa48984e GL |
95 | /* |
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. | |
100 | */ | |
3805f201 | 101 | int (*get_formats)(struct soc_camera_device *, unsigned int, |
c2786ad2 | 102 | struct soc_camera_format_xlate *); |
fa48984e | 103 | void (*put_formats)(struct soc_camera_device *); |
3bfb4100 GL |
104 | int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *); |
105 | int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *); | |
aee5c2f1 | 106 | /* |
10d5509c | 107 | * The difference to .set_selection() is, that .set_liveselection is not allowed |
aee5c2f1 GL |
108 | * to change the output sizes |
109 | */ | |
10d5509c | 110 | int (*set_liveselection)(struct soc_camera_device *, struct v4l2_selection *); |
09e231b3 | 111 | int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *); |
d8fac217 | 112 | int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *); |
592c2aba GL |
113 | int (*init_videobuf2)(struct vb2_queue *, |
114 | struct soc_camera_device *); | |
e55222ef | 115 | int (*querycap)(struct soc_camera_host *, struct v4l2_capability *); |
8843d119 | 116 | int (*set_bus_param)(struct soc_camera_device *); |
c9f6ef69 GL |
117 | int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *); |
118 | int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *); | |
ad3537b5 | 119 | int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *); |
a3f8683b | 120 | __poll_t (*poll)(struct file *, poll_table *); |
e55222ef GL |
121 | }; |
122 | ||
bd73b36f GL |
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) | |
128 | ||
0fd327bd | 129 | struct i2c_board_info; |
96e442c1 | 130 | struct regulator_bulk_data; |
0fd327bd | 131 | |
25a34811 | 132 | struct soc_camera_subdev_desc { |
bd73b36f GL |
133 | /* Per camera SOCAM_SENSOR_* bus flags */ |
134 | unsigned long flags; | |
25a34811 GL |
135 | |
136 | /* sensor driver private platform data */ | |
137 | void *drv_priv; | |
3fd7ceff | 138 | |
40f07533 GL |
139 | /* |
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. | |
144 | */ | |
145 | bool unbalanced_power; | |
146 | unsigned long clock_state; | |
147 | ||
25a34811 GL |
148 | /* Optional callbacks to power on or off and reset the sensor */ |
149 | int (*power)(struct device *, int); | |
150 | int (*reset)(struct device *); | |
151 | ||
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 *); | |
d3f884a7 GL |
160 | |
161 | /* Optional regulators that have to be managed on power on/off events */ | |
162 | struct v4l2_subdev_platform_data sd_pdata; | |
25a34811 GL |
163 | }; |
164 | ||
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; | |
171 | ||
c41debaf | 172 | /* |
d33b290a GL |
173 | * For non-I2C devices platform has to provide methods to add a device |
174 | * to the system and to remove it | |
c41debaf | 175 | */ |
7dfff953 GL |
176 | int (*add_device)(struct soc_camera_device *); |
177 | void (*del_device)(struct soc_camera_device *); | |
25a34811 GL |
178 | }; |
179 | ||
180 | /* | |
e09da11d | 181 | * Platform data for "soc-camera-pdrv" |
25a34811 GL |
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. | |
185 | */ | |
186 | struct soc_camera_desc { | |
187 | struct soc_camera_subdev_desc subdev_desc; | |
188 | struct soc_camera_host_desc host_desc; | |
189 | }; | |
190 | ||
191 | /* Prepare to replace this struct: don't change its layout any more! */ | |
192 | struct soc_camera_link { | |
193 | /* | |
194 | * Subdevice part - keep at top and compatible to | |
195 | * struct soc_camera_subdev_desc | |
196 | */ | |
197 | ||
198 | /* Per camera SOCAM_SENSOR_* bus flags */ | |
199 | unsigned long flags; | |
200 | ||
201 | void *priv; | |
202 | ||
40f07533 GL |
203 | /* Set by platforms to handle misbehaving drivers */ |
204 | bool unbalanced_power; | |
205 | /* Used by soc-camera helper functions */ | |
206 | unsigned long clock_state; | |
207 | ||
81034663 SH |
208 | /* Optional callbacks to power on or off and reset the sensor */ |
209 | int (*power)(struct device *, int); | |
210 | int (*reset)(struct device *); | |
28f59339 SH |
211 | /* |
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. | |
215 | */ | |
216 | int (*set_bus_param)(struct soc_camera_link *, unsigned long flags); | |
217 | unsigned long (*query_bus_param)(struct soc_camera_link *); | |
594bb46d | 218 | void (*free_bus)(struct soc_camera_link *); |
25a34811 | 219 | |
d3f884a7 GL |
220 | /* Optional regulators that have to be managed on power on/off events */ |
221 | struct regulator_bulk_data *regulators; | |
222 | int num_regulators; | |
223 | ||
224 | void *host_priv; | |
225 | ||
25a34811 GL |
226 | /* |
227 | * Host part - keep at bottom and compatible to | |
228 | * struct soc_camera_host_desc | |
229 | */ | |
230 | ||
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; | |
236 | ||
237 | /* | |
238 | * For non-I2C devices platform has to provide methods to add a device | |
239 | * to the system and to remove it | |
240 | */ | |
241 | int (*add_device)(struct soc_camera_device *); | |
242 | void (*del_device)(struct soc_camera_device *); | |
e55222ef GL |
243 | }; |
244 | ||
96c75399 GL |
245 | static inline struct soc_camera_host *to_soc_camera_host( |
246 | const struct device *dev) | |
e55222ef | 247 | { |
979ea1dd GL |
248 | struct v4l2_device *v4l2_dev = dev_get_drvdata(dev); |
249 | ||
250 | return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev); | |
e55222ef GL |
251 | } |
252 | ||
25a34811 | 253 | static inline struct soc_camera_desc *to_soc_camera_desc( |
96c75399 | 254 | const struct soc_camera_device *icd) |
40e2e092 | 255 | { |
25a34811 | 256 | return icd->sdesc; |
40e2e092 | 257 | } |
e55222ef | 258 | |
96c75399 GL |
259 | static inline struct device *to_soc_camera_control( |
260 | const struct soc_camera_device *icd) | |
40e2e092 | 261 | { |
7dfff953 | 262 | return icd->control; |
40e2e092 | 263 | } |
e55222ef | 264 | |
96c75399 GL |
265 | static inline struct v4l2_subdev *soc_camera_to_subdev( |
266 | const struct soc_camera_device *icd) | |
c9c1f1c0 GL |
267 | { |
268 | struct device *control = to_soc_camera_control(icd); | |
269 | return dev_get_drvdata(control); | |
270 | } | |
271 | ||
40e2e092 GL |
272 | int soc_camera_host_register(struct soc_camera_host *ici); |
273 | void soc_camera_host_unregister(struct soc_camera_host *ici); | |
274 | ||
40e2e092 | 275 | const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc( |
c2786ad2 | 276 | struct soc_camera_device *icd, unsigned int fourcc); |
25c4d74e | 277 | |
c2786ad2 GL |
278 | /** |
279 | * struct soc_camera_format_xlate - match between host and sensor formats | |
760697be GL |
280 | * @code: code of a sensor provided format |
281 | * @host_fmt: host format after host translation from code | |
c2786ad2 GL |
282 | * |
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. | |
287 | */ | |
288 | struct soc_camera_format_xlate { | |
27ffaeb0 | 289 | u32 code; |
760697be | 290 | const struct soc_mbus_pixelfmt *host_fmt; |
c2786ad2 GL |
291 | }; |
292 | ||
a9bef518 GL |
293 | #define SOCAM_SENSE_PCLK_CHANGED (1 << 0) |
294 | ||
295 | /** | |
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. | |
301 | * | |
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. | |
311 | */ | |
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; | |
317 | }; | |
318 | ||
d839fe17 GL |
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) | |
7b88fc08 | 324 | #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12) |
d839fe17 GL |
325 | #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15) |
326 | #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16) | |
7b88fc08 PE |
327 | #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18) |
328 | #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24) | |
042d8790 GL |
329 | |
330 | #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \ | |
331 | SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \ | |
7b88fc08 PE |
332 | SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \ |
333 | SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \ | |
334 | SOCAM_DATAWIDTH_24) | |
ad5f2e85 | 335 | |
e26b3144 MN |
336 | static inline void soc_camera_limit_side(int *start, int *length, |
337 | unsigned int start_min, | |
6a6c8786 GL |
338 | unsigned int length_min, unsigned int length_max) |
339 | { | |
340 | if (*length < length_min) | |
341 | *length = length_min; | |
342 | else if (*length > length_max) | |
343 | *length = length_max; | |
344 | ||
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; | |
349 | } | |
350 | ||
25a34811 | 351 | unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd, |
32c69fcc | 352 | const struct v4l2_mbus_config *cfg); |
bd73b36f | 353 | |
e09da11d | 354 | int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd); |
9aea470b GL |
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); | |
4ec10bac LP |
359 | |
360 | static inline int soc_camera_set_power(struct device *dev, | |
9aea470b | 361 | struct soc_camera_subdev_desc *ssdd, struct v4l2_clk *clk, bool on) |
4ec10bac | 362 | { |
9aea470b GL |
363 | return on ? soc_camera_power_on(dev, ssdd, clk) |
364 | : soc_camera_power_off(dev, ssdd, clk); | |
4ec10bac LP |
365 | } |
366 | ||
4f9fb5ed MCC |
367 | /* This is only temporary here - until v4l2-subdev begins to link to video_device */ |
368 | #include <linux/i2c.h> | |
14178aa5 | 369 | static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client) |
4f9fb5ed | 370 | { |
2f0babb7 | 371 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
4c0b036d | 372 | struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd); |
14178aa5 | 373 | return icd ? icd->vdev : NULL; |
4f9fb5ed MCC |
374 | } |
375 | ||
25a34811 | 376 | static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client) |
14178aa5 | 377 | { |
2f0babb7 | 378 | return client->dev.platform_data; |
14178aa5 GL |
379 | } |
380 | ||
14381c26 | 381 | static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev) |
14178aa5 | 382 | { |
14381c26 | 383 | struct soc_camera_device *icd = video_get_drvdata(vdev); |
14178aa5 GL |
384 | return soc_camera_to_subdev(icd); |
385 | } | |
386 | ||
14178aa5 | 387 | static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq) |
8eb4476d GL |
388 | { |
389 | return container_of(vq, struct soc_camera_device, vb2_vidq); | |
390 | } | |
391 | ||
4c0b036d GL |
392 | static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd) |
393 | { | |
394 | return (icd->iface << 8) | (icd->devnum + 1); | |
395 | } | |
396 | ||
592c2aba GL |
397 | void soc_camera_lock(struct vb2_queue *vq); |
398 | void soc_camera_unlock(struct vb2_queue *vq); | |
399 | ||
e55222ef | 400 | #endif |