1
2
3
4
5
6
7
8
9
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/videobuf2-v4l2.h>
21#include <media/v4l2-async.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-device.h>
24
25struct file;
26struct soc_camera_desc;
27struct soc_camera_async_client;
28
29struct soc_camera_device {
30 struct list_head list;
31 struct soc_camera_desc *sdesc;
32 struct device *pdev;
33 struct device *parent;
34 struct device *control;
35 s32 user_width;
36 s32 user_height;
37 u32 bytesperline;
38 u32 sizeimage;
39 enum v4l2_colorspace colorspace;
40 unsigned char iface;
41 unsigned char devnum;
42 struct soc_camera_sense *sense;
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;
49 void *host_priv;
50
51 int use_count;
52 struct file *streamer;
53 struct v4l2_clk *clk;
54
55 struct soc_camera_async_client *sasc;
56
57 struct vb2_queue vb2_vidq;
58};
59
60
61#define SOCAM_HOST_CAP_STRIDE (1 << 0)
62
63enum soc_camera_subdev_role {
64 SOCAM_SUBDEV_DATA_SOURCE = 1,
65 SOCAM_SUBDEV_DATA_SINK,
66 SOCAM_SUBDEV_DATA_PROCESSOR,
67};
68
69struct soc_camera_async_subdev {
70 struct v4l2_async_subdev asd;
71 enum soc_camera_subdev_role role;
72};
73
74struct soc_camera_host {
75 struct v4l2_device v4l2_dev;
76 struct list_head list;
77 struct mutex host_lock;
78 struct mutex clk_lock;
79 unsigned char nr;
80 u32 capabilities;
81 struct soc_camera_device *icd;
82 void *priv;
83 const char *drv_name;
84 struct soc_camera_host_ops *ops;
85 struct v4l2_async_subdev **asd;
86 unsigned int *asd_sizes;
87};
88
89struct 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 *);
95
96
97
98
99
100
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 *);
106
107
108
109
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 __poll_t (*poll)(struct file *, poll_table *);
121};
122
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
129struct i2c_board_info;
130struct regulator_bulk_data;
131
132struct soc_camera_subdev_desc {
133
134 unsigned long flags;
135
136
137 void *drv_priv;
138
139
140
141
142
143
144
145 bool unbalanced_power;
146 unsigned long clock_state;
147
148
149 int (*power)(struct device *, int);
150 int (*reset)(struct device *);
151
152
153
154
155
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 v4l2_subdev_platform_data sd_pdata;
163};
164
165struct soc_camera_host_desc {
166
167 int bus_id;
168 int i2c_adapter_id;
169 struct i2c_board_info *board_info;
170 const char *module_name;
171
172
173
174
175
176 int (*add_device)(struct soc_camera_device *);
177 void (*del_device)(struct soc_camera_device *);
178};
179
180
181
182
183
184
185
186struct soc_camera_desc {
187 struct soc_camera_subdev_desc subdev_desc;
188 struct soc_camera_host_desc host_desc;
189};
190
191
192struct soc_camera_link {
193
194
195
196
197
198
199 unsigned long flags;
200
201 void *priv;
202
203
204 bool unbalanced_power;
205
206 unsigned long clock_state;
207
208
209 int (*power)(struct device *, int);
210 int (*reset)(struct device *);
211
212
213
214
215
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 *);
219
220
221 struct regulator_bulk_data *regulators;
222 int num_regulators;
223
224 void *host_priv;
225
226
227
228
229
230
231
232 int bus_id;
233 int i2c_adapter_id;
234 struct i2c_board_info *board_info;
235 const char *module_name;
236
237
238
239
240
241 int (*add_device)(struct soc_camera_device *);
242 void (*del_device)(struct soc_camera_device *);
243};
244
245static inline struct soc_camera_host *to_soc_camera_host(
246 const struct device *dev)
247{
248 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
249
250 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
251}
252
253static inline struct soc_camera_desc *to_soc_camera_desc(
254 const struct soc_camera_device *icd)
255{
256 return icd->sdesc;
257}
258
259static inline struct device *to_soc_camera_control(
260 const struct soc_camera_device *icd)
261{
262 return icd->control;
263}
264
265static inline struct v4l2_subdev *soc_camera_to_subdev(
266 const struct soc_camera_device *icd)
267{
268 struct device *control = to_soc_camera_control(icd);
269 return dev_get_drvdata(control);
270}
271
272int soc_camera_host_register(struct soc_camera_host *ici);
273void soc_camera_host_unregister(struct soc_camera_host *ici);
274
275const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
276 struct soc_camera_device *icd, unsigned int fourcc);
277
278
279
280
281
282
283
284
285
286
287
288struct soc_camera_format_xlate {
289 u32 code;
290 const struct soc_mbus_pixelfmt *host_fmt;
291};
292
293#define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312struct 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
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)
329
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)
335
336static inline void soc_camera_limit_side(int *start, int *length,
337 unsigned int start_min,
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
351unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
352 const struct v4l2_mbus_config *cfg);
353
354int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd);
355int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
356 struct v4l2_clk *clk);
357int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
358 struct v4l2_clk *clk);
359
360static inline int soc_camera_set_power(struct device *dev,
361 struct soc_camera_subdev_desc *ssdd, struct v4l2_clk *clk, bool on)
362{
363 return on ? soc_camera_power_on(dev, ssdd, clk)
364 : soc_camera_power_off(dev, ssdd, clk);
365}
366
367
368#include <linux/i2c.h>
369static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
370{
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;
374}
375
376static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
377{
378 return client->dev.platform_data;
379}
380
381static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
382{
383 struct soc_camera_device *icd = video_get_drvdata(vdev);
384 return soc_camera_to_subdev(icd);
385}
386
387static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
388{
389 return container_of(vq, struct soc_camera_device, vb2_vidq);
390}
391
392static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
393{
394 return (icd->iface << 8) | (icd->devnum + 1);
395}
396
397void soc_camera_lock(struct vb2_queue *vq);
398void soc_camera_unlock(struct vb2_queue *vq);
399
400#endif
401