]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/media/soc_camera.h
Merge tag 'linux-watchdog-5.4-rc1' of git://www.linux-watchdog.org/linux-watchdog
[mirror_ubuntu-focal-kernel.git] / include / media / soc_camera.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
e55222ef
GL
2/*
3 * camera image capture (abstract) bus driver header
4 *
5 * Copyright (C) 2006, Sascha Hauer, Pengutronix
6 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
e55222ef
GL
7 */
8
9#ifndef SOC_CAMERA_H
10#define SOC_CAMERA_H
11
d839fe17 12#include <linux/bitops.h>
52d268a3 13#include <linux/device.h>
1c3bb743
GL
14#include <linux/mutex.h>
15#include <linux/pm.h>
e55222ef 16#include <linux/videodev2.h>
c139990e 17#include <media/videobuf2-v4l2.h>
e09da11d 18#include <media/v4l2-async.h>
ee02da64 19#include <media/v4l2-ctrls.h>
979ea1dd 20#include <media/v4l2-device.h>
e55222ef 21
57bee29d 22struct file;
25a34811 23struct soc_camera_desc;
e09da11d 24struct soc_camera_async_client;
57bee29d 25
e55222ef 26struct soc_camera_device {
d33b290a 27 struct list_head list; /* list of all registered devices */
25a34811 28 struct soc_camera_desc *sdesc;
979ea1dd 29 struct device *pdev; /* Platform device */
7dfff953
GL
30 struct device *parent; /* Camera host device */
31 struct device *control; /* E.g., the i2c client */
6a6c8786
GL
32 s32 user_width;
33 s32 user_height;
0e4c180d
SA
34 u32 bytesperline; /* for padding, zero if unused */
35 u32 sizeimage;
760697be 36 enum v4l2_colorspace colorspace;
e55222ef
GL
37 unsigned char iface; /* Host number */
38 unsigned char devnum; /* Device number per host */
a9bef518 39 struct soc_camera_sense *sense; /* See comment in struct definition */
e55222ef 40 struct video_device *vdev;
ee02da64 41 struct v4l2_ctrl_handler ctrl_handler;
760697be 42 const struct soc_camera_format_xlate *current_fmt;
c2786ad2
GL
43 struct soc_camera_format_xlate *user_formats;
44 int num_user_formats;
025c18a1 45 enum v4l2_field field; /* Preserve field over close() */
1c3bb743 46 void *host_priv; /* Per-device host private data */
dd669e90 47 /* soc_camera.c private count. Only accessed with .host_lock held */
9dc4e48f 48 int use_count;
57bee29d 49 struct file *streamer; /* stream owner */
9aea470b 50 struct v4l2_clk *clk;
e09da11d
GL
51 /* Asynchronous subdevice management */
52 struct soc_camera_async_client *sasc;
53 /* video buffer queue */
73a01599 54 struct vb2_queue vb2_vidq;
e55222ef
GL
55};
56
914f05c8
LP
57/* Host supports programmable stride */
58#define SOCAM_HOST_CAP_STRIDE (1 << 0)
59
e09da11d
GL
60enum soc_camera_subdev_role {
61 SOCAM_SUBDEV_DATA_SOURCE = 1,
62 SOCAM_SUBDEV_DATA_SINK,
63 SOCAM_SUBDEV_DATA_PROCESSOR,
64};
65
66struct soc_camera_async_subdev {
67 struct v4l2_async_subdev asd;
68 enum soc_camera_subdev_role role;
69};
70
e55222ef 71struct soc_camera_host {
979ea1dd 72 struct v4l2_device v4l2_dev;
e55222ef 73 struct list_head list;
e09da11d
GL
74 struct mutex host_lock; /* Main synchronisation lock */
75 struct mutex clk_lock; /* Protect pipeline modifications */
2f9a0c88 76 unsigned char nr; /* Host number */
914f05c8 77 u32 capabilities;
f7f6ce2d 78 struct soc_camera_device *icd; /* Currently attached client */
e55222ef 79 void *priv;
af128a10 80 const char *drv_name;
b8d9904c 81 struct soc_camera_host_ops *ops;
e09da11d 82 struct v4l2_async_subdev **asd; /* Flat array, arranged in groups */
f687f326 83 unsigned int *asd_sizes; /* 0-terminated array of asd group sizes */
b8d9904c
GL
84};
85
86struct soc_camera_host_ops {
87 struct module *owner;
e55222ef
GL
88 int (*add)(struct soc_camera_device *);
89 void (*remove)(struct soc_camera_device *);
eb569cf9
GL
90 int (*clock_start)(struct soc_camera_host *);
91 void (*clock_stop)(struct soc_camera_host *);
fa48984e
GL
92 /*
93 * .get_formats() is called for each client device format, but
94 * .put_formats() is only called once. Further, if any of the calls to
95 * .get_formats() fail, .put_formats() will not be called at all, the
96 * failing .get_formats() must then clean up internally.
97 */
3805f201 98 int (*get_formats)(struct soc_camera_device *, unsigned int,
c2786ad2 99 struct soc_camera_format_xlate *);
fa48984e 100 void (*put_formats)(struct soc_camera_device *);
3bfb4100
GL
101 int (*get_selection)(struct soc_camera_device *, struct v4l2_selection *);
102 int (*set_selection)(struct soc_camera_device *, struct v4l2_selection *);
aee5c2f1 103 /*
10d5509c 104 * The difference to .set_selection() is, that .set_liveselection is not allowed
aee5c2f1
GL
105 * to change the output sizes
106 */
10d5509c 107 int (*set_liveselection)(struct soc_camera_device *, struct v4l2_selection *);
09e231b3 108 int (*set_fmt)(struct soc_camera_device *, struct v4l2_format *);
d8fac217 109 int (*try_fmt)(struct soc_camera_device *, struct v4l2_format *);
592c2aba
GL
110 int (*init_videobuf2)(struct vb2_queue *,
111 struct soc_camera_device *);
e55222ef 112 int (*querycap)(struct soc_camera_host *, struct v4l2_capability *);
8843d119 113 int (*set_bus_param)(struct soc_camera_device *);
c9f6ef69
GL
114 int (*get_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
115 int (*set_parm)(struct soc_camera_device *, struct v4l2_streamparm *);
ad3537b5 116 int (*enum_framesizes)(struct soc_camera_device *, struct v4l2_frmsizeenum *);
a3f8683b 117 __poll_t (*poll)(struct file *, poll_table *);
e55222ef
GL
118};
119
bd73b36f
GL
120#define SOCAM_SENSOR_INVERT_PCLK (1 << 0)
121#define SOCAM_SENSOR_INVERT_MCLK (1 << 1)
122#define SOCAM_SENSOR_INVERT_HSYNC (1 << 2)
123#define SOCAM_SENSOR_INVERT_VSYNC (1 << 3)
124#define SOCAM_SENSOR_INVERT_DATA (1 << 4)
125
0fd327bd 126struct i2c_board_info;
96e442c1 127struct regulator_bulk_data;
0fd327bd 128
25a34811 129struct soc_camera_subdev_desc {
bd73b36f
GL
130 /* Per camera SOCAM_SENSOR_* bus flags */
131 unsigned long flags;
25a34811
GL
132
133 /* sensor driver private platform data */
134 void *drv_priv;
3fd7ceff 135
40f07533
GL
136 /*
137 * Set unbalanced_power to true to deal with legacy drivers, failing to
138 * balance their calls to subdevice's .s_power() method. clock_state is
139 * then used internally by helper functions, it shouldn't be touched by
140 * drivers or the platform code.
141 */
142 bool unbalanced_power;
143 unsigned long clock_state;
144
25a34811
GL
145 /* Optional callbacks to power on or off and reset the sensor */
146 int (*power)(struct device *, int);
147 int (*reset)(struct device *);
148
149 /*
150 * some platforms may support different data widths than the sensors
151 * native ones due to different data line routing. Let the board code
152 * overwrite the width flags.
153 */
154 int (*set_bus_param)(struct soc_camera_subdev_desc *, unsigned long flags);
155 unsigned long (*query_bus_param)(struct soc_camera_subdev_desc *);
156 void (*free_bus)(struct soc_camera_subdev_desc *);
d3f884a7
GL
157
158 /* Optional regulators that have to be managed on power on/off events */
159 struct v4l2_subdev_platform_data sd_pdata;
25a34811
GL
160};
161
162struct soc_camera_host_desc {
163 /* Camera bus id, used to match a camera and a bus */
164 int bus_id;
165 int i2c_adapter_id;
166 struct i2c_board_info *board_info;
167 const char *module_name;
168
c41debaf 169 /*
d33b290a
GL
170 * For non-I2C devices platform has to provide methods to add a device
171 * to the system and to remove it
c41debaf 172 */
7dfff953
GL
173 int (*add_device)(struct soc_camera_device *);
174 void (*del_device)(struct soc_camera_device *);
25a34811
GL
175};
176
177/*
e09da11d 178 * Platform data for "soc-camera-pdrv"
25a34811
GL
179 * This MUST be kept binary-identical to struct soc_camera_link below, until
180 * it is completely replaced by this one, after which we can split it into its
181 * two components.
182 */
183struct soc_camera_desc {
184 struct soc_camera_subdev_desc subdev_desc;
185 struct soc_camera_host_desc host_desc;
186};
187
188/* Prepare to replace this struct: don't change its layout any more! */
189struct soc_camera_link {
190 /*
191 * Subdevice part - keep at top and compatible to
192 * struct soc_camera_subdev_desc
193 */
194
195 /* Per camera SOCAM_SENSOR_* bus flags */
196 unsigned long flags;
197
198 void *priv;
199
40f07533
GL
200 /* Set by platforms to handle misbehaving drivers */
201 bool unbalanced_power;
202 /* Used by soc-camera helper functions */
203 unsigned long clock_state;
204
81034663
SH
205 /* Optional callbacks to power on or off and reset the sensor */
206 int (*power)(struct device *, int);
207 int (*reset)(struct device *);
28f59339
SH
208 /*
209 * some platforms may support different data widths than the sensors
210 * native ones due to different data line routing. Let the board code
211 * overwrite the width flags.
212 */
213 int (*set_bus_param)(struct soc_camera_link *, unsigned long flags);
214 unsigned long (*query_bus_param)(struct soc_camera_link *);
594bb46d 215 void (*free_bus)(struct soc_camera_link *);
25a34811 216
d3f884a7
GL
217 /* Optional regulators that have to be managed on power on/off events */
218 struct regulator_bulk_data *regulators;
219 int num_regulators;
220
221 void *host_priv;
222
25a34811
GL
223 /*
224 * Host part - keep at bottom and compatible to
225 * struct soc_camera_host_desc
226 */
227
228 /* Camera bus id, used to match a camera and a bus */
229 int bus_id;
230 int i2c_adapter_id;
231 struct i2c_board_info *board_info;
232 const char *module_name;
233
234 /*
235 * For non-I2C devices platform has to provide methods to add a device
236 * to the system and to remove it
237 */
238 int (*add_device)(struct soc_camera_device *);
239 void (*del_device)(struct soc_camera_device *);
e55222ef
GL
240};
241
96c75399
GL
242static inline struct soc_camera_host *to_soc_camera_host(
243 const struct device *dev)
e55222ef 244{
979ea1dd
GL
245 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
246
247 return container_of(v4l2_dev, struct soc_camera_host, v4l2_dev);
e55222ef
GL
248}
249
25a34811 250static inline struct soc_camera_desc *to_soc_camera_desc(
96c75399 251 const struct soc_camera_device *icd)
40e2e092 252{
25a34811 253 return icd->sdesc;
40e2e092 254}
e55222ef 255
96c75399
GL
256static inline struct device *to_soc_camera_control(
257 const struct soc_camera_device *icd)
40e2e092 258{
7dfff953 259 return icd->control;
40e2e092 260}
e55222ef 261
96c75399
GL
262static inline struct v4l2_subdev *soc_camera_to_subdev(
263 const struct soc_camera_device *icd)
c9c1f1c0
GL
264{
265 struct device *control = to_soc_camera_control(icd);
266 return dev_get_drvdata(control);
267}
268
40e2e092
GL
269int soc_camera_host_register(struct soc_camera_host *ici);
270void soc_camera_host_unregister(struct soc_camera_host *ici);
271
40e2e092 272const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
c2786ad2 273 struct soc_camera_device *icd, unsigned int fourcc);
25c4d74e 274
c2786ad2
GL
275/**
276 * struct soc_camera_format_xlate - match between host and sensor formats
760697be
GL
277 * @code: code of a sensor provided format
278 * @host_fmt: host format after host translation from code
c2786ad2
GL
279 *
280 * Host and sensor translation structure. Used in table of host and sensor
281 * formats matchings in soc_camera_device. A host can override the generic list
282 * generation by implementing get_formats(), and use it for format checks and
283 * format setup.
284 */
285struct soc_camera_format_xlate {
27ffaeb0 286 u32 code;
760697be 287 const struct soc_mbus_pixelfmt *host_fmt;
c2786ad2
GL
288};
289
a9bef518
GL
290#define SOCAM_SENSE_PCLK_CHANGED (1 << 0)
291
292/**
293 * This struct can be attached to struct soc_camera_device by the host driver
294 * to request sense from the camera, for example, when calling .set_fmt(). The
295 * host then can check which flags are set and verify respective values if any.
296 * For example, if SOCAM_SENSE_PCLK_CHANGED is set, it means, pixclock has
297 * changed during this operation. After completion the host should detach sense.
298 *
299 * @flags ored SOCAM_SENSE_* flags
300 * @master_clock if the host wants to be informed about pixel-clock
301 * change, it better set master_clock.
302 * @pixel_clock_max maximum pixel clock frequency supported by the host,
303 * camera is not allowed to exceed this.
304 * @pixel_clock if the camera driver changed pixel clock during this
305 * operation, it sets SOCAM_SENSE_PCLK_CHANGED, uses
306 * master_clock to calculate the new pixel-clock and
307 * sets this field.
308 */
309struct soc_camera_sense {
310 unsigned long flags;
311 unsigned long master_clock;
312 unsigned long pixel_clock_max;
313 unsigned long pixel_clock;
314};
315
d839fe17
GL
316#define SOCAM_DATAWIDTH(x) BIT((x) - 1)
317#define SOCAM_DATAWIDTH_4 SOCAM_DATAWIDTH(4)
318#define SOCAM_DATAWIDTH_8 SOCAM_DATAWIDTH(8)
319#define SOCAM_DATAWIDTH_9 SOCAM_DATAWIDTH(9)
320#define SOCAM_DATAWIDTH_10 SOCAM_DATAWIDTH(10)
7b88fc08 321#define SOCAM_DATAWIDTH_12 SOCAM_DATAWIDTH(12)
d839fe17
GL
322#define SOCAM_DATAWIDTH_15 SOCAM_DATAWIDTH(15)
323#define SOCAM_DATAWIDTH_16 SOCAM_DATAWIDTH(16)
7b88fc08
PE
324#define SOCAM_DATAWIDTH_18 SOCAM_DATAWIDTH(18)
325#define SOCAM_DATAWIDTH_24 SOCAM_DATAWIDTH(24)
042d8790
GL
326
327#define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_4 | SOCAM_DATAWIDTH_8 | \
328 SOCAM_DATAWIDTH_9 | SOCAM_DATAWIDTH_10 | \
7b88fc08
PE
329 SOCAM_DATAWIDTH_12 | SOCAM_DATAWIDTH_15 | \
330 SOCAM_DATAWIDTH_16 | SOCAM_DATAWIDTH_18 | \
331 SOCAM_DATAWIDTH_24)
ad5f2e85 332
e26b3144
MN
333static inline void soc_camera_limit_side(int *start, int *length,
334 unsigned int start_min,
6a6c8786
GL
335 unsigned int length_min, unsigned int length_max)
336{
337 if (*length < length_min)
338 *length = length_min;
339 else if (*length > length_max)
340 *length = length_max;
341
342 if (*start < start_min)
343 *start = start_min;
344 else if (*start > start_min + length_max - *length)
345 *start = start_min + length_max - *length;
346}
347
25a34811 348unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
32c69fcc 349 const struct v4l2_mbus_config *cfg);
bd73b36f 350
e09da11d 351int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd);
9aea470b
GL
352int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
353 struct v4l2_clk *clk);
354int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
355 struct v4l2_clk *clk);
4ec10bac
LP
356
357static inline int soc_camera_set_power(struct device *dev,
9aea470b 358 struct soc_camera_subdev_desc *ssdd, struct v4l2_clk *clk, bool on)
4ec10bac 359{
9aea470b
GL
360 return on ? soc_camera_power_on(dev, ssdd, clk)
361 : soc_camera_power_off(dev, ssdd, clk);
4ec10bac
LP
362}
363
4f9fb5ed
MCC
364/* This is only temporary here - until v4l2-subdev begins to link to video_device */
365#include <linux/i2c.h>
14178aa5 366static inline struct video_device *soc_camera_i2c_to_vdev(const struct i2c_client *client)
4f9fb5ed 367{
2f0babb7 368 struct v4l2_subdev *sd = i2c_get_clientdata(client);
4c0b036d 369 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
14178aa5 370 return icd ? icd->vdev : NULL;
4f9fb5ed
MCC
371}
372
25a34811 373static inline struct soc_camera_subdev_desc *soc_camera_i2c_to_desc(const struct i2c_client *client)
14178aa5 374{
2f0babb7 375 return client->dev.platform_data;
14178aa5
GL
376}
377
14381c26 378static inline struct v4l2_subdev *soc_camera_vdev_to_subdev(struct video_device *vdev)
14178aa5 379{
14381c26 380 struct soc_camera_device *icd = video_get_drvdata(vdev);
14178aa5
GL
381 return soc_camera_to_subdev(icd);
382}
383
14178aa5 384static inline struct soc_camera_device *soc_camera_from_vb2q(const struct vb2_queue *vq)
8eb4476d
GL
385{
386 return container_of(vq, struct soc_camera_device, vb2_vidq);
387}
388
4c0b036d
GL
389static inline u32 soc_camera_grp_id(const struct soc_camera_device *icd)
390{
391 return (icd->iface << 8) | (icd->devnum + 1);
392}
393
592c2aba
GL
394void soc_camera_lock(struct vb2_queue *vq);
395void soc_camera_unlock(struct vb2_queue *vq);
396
e55222ef 397#endif