[media] soc-camera: add host clock callbacks to start and stop the master clock
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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 struct video_device *vdev;
43 struct v4l2_ctrl_handler ctrl_handler;
44 const struct soc_camera_format_xlate *current_fmt;
45 struct soc_camera_format_xlate *user_formats;
46 int num_user_formats;
47 enum v4l2_field field; /* Preserve field over close() */
48 void *host_priv; /* Per-device host private data */
49 /* soc_camera.c private count. Only accessed with .host_lock held */
50 int use_count;
51 struct file *streamer; /* stream owner */
52 union {
53 struct videobuf_queue vb_vidq;
54 struct vb2_queue vb2_vidq;
55 };
56 };
57
58 /* Host supports programmable stride */
59 #define SOCAM_HOST_CAP_STRIDE (1 << 0)
60
61 struct soc_camera_host {
62 struct v4l2_device v4l2_dev;
63 struct list_head list;
64 struct mutex host_lock; /* Protect pipeline modifications */
65 unsigned char nr; /* Host number */
66 u32 capabilities;
67 struct soc_camera_device *icd; /* Currently attached client */
68 void *priv;
69 const char *drv_name;
70 struct soc_camera_host_ops *ops;
71 };
72
73 struct soc_camera_host_ops {
74 struct module *owner;
75 int (*add)(struct soc_camera_device *);
76 void (*remove)(struct soc_camera_device *);
77 int (*clock_start)(struct soc_camera_host *);
78 void (*clock_stop)(struct soc_camera_host *);
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 */
85 int (*get_formats)(struct soc_camera_device *, unsigned int,
86 struct soc_camera_format_xlate *);
87 void (*put_formats)(struct soc_camera_device *);
88 int (*cropcap)(struct soc_camera_device *, struct v4l2_cropcap *);
89 int (*get_crop)(struct soc_camera_device *, struct v4l2_crop *);
90 int (*set_crop)(struct soc_camera_device *, const struct v4l2_crop *);
91 int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
92 int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
93 /*
94 * The difference to .set_crop() is, that .set_livecrop is not allowed
95 * to change the output sizes
96 */
97 int (*set_livecrop)(struct soc_camera_device *, const struct v4l2_crop *);
98 int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
99 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
100 void (*init_videobuf)(struct videobuf_queue *,
101 struct soc_camera_device *);
102 int (*init_videobuf2)(struct vb2_queue *,
103 struct soc_camera_device *);
104 int (*reqbufs)(struct soc_camera_device *, struct v4l2_requestbuffers *);
105 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
106 int (*set_bus_param)(struct soc_camera_device *);
107 int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
108 int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
109 int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
110 unsigned int (*poll)(struct file *, poll_table *);
111 };
112
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
119 struct i2c_board_info;
120 struct regulator_bulk_data;
121
122 struct soc_camera_subdev_desc {
123 /* Per camera SOCAM_SENSOR_* bus flags */
124 unsigned long flags;
125
126 /* sensor driver private platform data */
127 void *drv_priv;
128
129 /* Optional regulators that have to be managed on power on/off events */
130 struct regulator_bulk_data *regulators;
131 int num_regulators;
132
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
147 struct 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
154 /*
155 * For non-I2C devices platform has to provide methods to add a device
156 * to the system and to remove it
157 */
158 int (*add_device)(struct soc_camera_device *);
159 void (*del_device)(struct soc_camera_device *);
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 */
167 struct 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! */
173 struct 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
188 /* Optional callbacks to power on or off and reset the sensor */
189 int (*power)(struct device *, int);
190 int (*reset)(struct device *);
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 *);
198 void (*free_bus)(struct soc_camera_link *);
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 *);
217 };
218
219 static inline struct soc_camera_host *to_soc_camera_host(
220 const struct device *dev)
221 {
222 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
223
224 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
225 }
226
227 static inline struct soc_camera_desc *to_soc_camera_desc(
228 const struct soc_camera_device *icd)
229 {
230 return icd->sdesc;
231 }
232
233 static inline struct device *to_soc_camera_control(
234 const struct soc_camera_device *icd)
235 {
236 return icd->control;
237 }
238
239 static inline struct v4l2_subdev *soc_camera_to_subdev(
240 const struct soc_camera_device *icd)
241 {
242 struct device *control = to_soc_camera_control(icd);
243 return dev_get_drvdata(control);
244 }
245
246 int soc_camera_host_register(struct soc_camera_host *ici);
247 void soc_camera_host_unregister(struct soc_camera_host *ici);
248
249 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
250 struct soc_camera_device *icd, unsigned int fourcc);
251
252 /**
253 * struct soc_camera_format_xlate - match between host and sensor formats
254 * @code: code of a sensor provided format
255 * @host_fmt: host format after host translation from code
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 */
262 struct soc_camera_format_xlate {
263 enum v4l2_mbus_pixelcode code;
264 const struct soc_mbus_pixelfmt *host_fmt;
265 };
266
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 */
286 struct 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
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)
298 #define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
299 #define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
300 #define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
301 #define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
302 #define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
303
304 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
305 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
306 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
307 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
308 SOCAM_DATAWIDTH_24)
309
310 static inline void soc_camera_limit_side(int *start, int *length,
311 unsigned int start_min,
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
325 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
326 const struct v4l2_mbus_config *cfg);
327
328 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd);
329 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd);
330
331 static inline int soc_camera_set_power(struct device *dev,
332 struct soc_camera_subdev_desc *ssdd, bool on)
333 {
334 return on ? soc_camera_power_on(dev, ssdd)
335 : soc_camera_power_off(dev, ssdd);
336 }
337
338 /* This is only temporary here - until v4l2-subdev begins to link to video_device */
339 #include <linux/i2c.h>
340 static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
341 {
342 struct v4l2_subdev *sd = i2c_get_clientdata(client);
343 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
344 return icd ? icd->vdev : NULL;
345 }
346
347 static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
348 {
349 return client->dev.platform_data;
350 }
351
352 static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
353 {
354 struct soc_camera_device *icd = video_get_drvdata(vdev);
355 return soc_camera_to_subdev(icd);
356 }
357
358 static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
359 {
360 return container_of(vq, struct soc_camera_device, vb2_vidq);
361 }
362
363 static inline struct soc_camera_device *soc_camera_from_vbq(const struct videobuf_queue *vq)
364 {
365 return container_of(vq, struct soc_camera_device, vb_vidq);
366 }
367
368 static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
369 {
370 return (icd->iface << 8) | (icd->devnum + 1);
371 }
372
373 void soc_camera_lock(struct vb2_queue *vq);
374 void soc_camera_unlock(struct vb2_queue *vq);
375
376 #endif