| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC |
| 4 |
* |
| 5 |
* Copyright (c) 2014-2017 Mentor Graphics Inc. |
| 6 |
* Copyright (C) 2017 Pengutronix, Philipp Zabel <[email protected]> |
| 7 |
*/ |
| 8 |
#include <linux/delay.h> |
| 9 |
#include <linux/gcd.h> |
| 10 |
#include <linux/interrupt.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/of_graph.h> |
| 13 |
#include <linux/pinctrl/consumer.h> |
| 14 |
#include <linux/platform_device.h> |
| 15 |
#include <media/v4l2-ctrls.h> |
| 16 |
#include <media/v4l2-device.h> |
| 17 |
#include <media/v4l2-event.h> |
| 18 |
#include <media/v4l2-fwnode.h> |
| 19 |
#include <media/v4l2-mc.h> |
| 20 |
#include <media/v4l2-subdev.h> |
| 21 |
#include <media/videobuf2-dma-contig.h> |
| 22 |
#include <video/imx-ipu-v3.h> |
| 23 |
#include <media/imx.h> |
| 24 |
#include "imx-media.h" |
| 25 |
|
| 26 |
/* |
| 27 |
* Min/Max supported width and heights. |
| 28 |
* |
| 29 |
* We allow planar output, so we have to align width by 16 pixels |
| 30 |
* to meet IDMAC alignment requirements. |
| 31 |
* |
| 32 |
* TODO: move this into pad format negotiation, if capture device |
| 33 |
* has not requested planar formats, we should allow 8 pixel |
| 34 |
* alignment. |
| 35 |
*/ |
| 36 |
#define MIN_W 32 |
| 37 |
#define MIN_H 32 |
| 38 |
#define MAX_W 4096 |
| 39 |
#define MAX_H 4096 |
| 40 |
#define W_ALIGN 1 /* multiple of 2 pixels */ |
| 41 |
#define H_ALIGN 1 /* multiple of 2 lines */ |
| 42 |
#define S_ALIGN 1 /* multiple of 2 */ |
| 43 |
|
| 44 |
/* |
| 45 |
* struct csi_skip_desc - CSI frame skipping descriptor |
| 46 |
* @keep - number of frames kept per max_ratio frames |
| 47 |
* @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield |
| 48 |
* @skip_smfc - skip pattern written to the SKIP_SMFC bitfield |
| 49 |
*/ |
| 50 |
struct csi_skip_desc { |
| 51 |
u8 keep; |
| 52 |
u8 max_ratio; |
| 53 |
u8 skip_smfc; |
| 54 |
}; |
| 55 |
|
| 56 |
struct csi_priv { |
| 57 |
struct device *dev; |
| 58 |
struct ipu_soc *ipu; |
| 59 |
struct v4l2_subdev sd; |
| 60 |
struct media_pad pad[CSI_NUM_PADS]; |
| 61 |
struct v4l2_async_notifier notifier; |
| 62 |
|
| 63 |
/* the video device at IDMAC output pad */ |
| 64 |
struct imx_media_video_dev *vdev; |
| 65 |
struct imx_media_fim *fim; |
| 66 |
int csi_id; |
| 67 |
int smfc_id; |
| 68 |
|
| 69 |
/* lock to protect all members below */ |
| 70 |
struct mutex lock; |
| 71 |
|
| 72 |
int active_output_pad; |
| 73 |
|
| 74 |
struct ipuv3_channel *idmac_ch; |
| 75 |
struct ipu_smfc *smfc; |
| 76 |
struct ipu_csi *csi; |
| 77 |
|
| 78 |
struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS]; |
| 79 |
const struct imx_media_pixfmt *cc[CSI_NUM_PADS]; |
| 80 |
struct v4l2_fract frame_interval[CSI_NUM_PADS]; |
| 81 |
struct v4l2_rect crop; |
| 82 |
struct v4l2_rect compose; |
| 83 |
const struct csi_skip_desc *skip; |
| 84 |
|
| 85 |
/* active vb2 buffers to send to video dev sink */ |
| 86 |
struct imx_media_buffer *active_vb2_buf[2]; |
| 87 |
struct imx_media_dma_buf underrun_buf; |
| 88 |
|
| 89 |
int ipu_buf_num; /* ipu double buffer index: 0-1 */ |
| 90 |
|
| 91 |
/* the sink for the captured frames */ |
| 92 |
struct media_entity *sink; |
| 93 |
enum ipu_csi_dest dest; |
| 94 |
/* the source subdev */ |
| 95 |
struct v4l2_subdev *src_sd; |
| 96 |
|
| 97 |
/* the mipi virtual channel number at link validate */ |
| 98 |
int vc_num; |
| 99 |
|
| 100 |
spinlock_t irqlock; /* protect eof_irq handler */ |
| 101 |
struct timer_list eof_timeout_timer; |
| 102 |
int eof_irq; |
| 103 |
int nfb4eof_irq; |
| 104 |
|
| 105 |
struct v4l2_ctrl_handler ctrl_hdlr; |
| 106 |
|
| 107 |
int stream_count; /* streaming counter */ |
| 108 |
u32 frame_sequence; /* frame sequence counter */ |
| 109 |
bool last_eof; /* waiting for last EOF at stream off */ |
| 110 |
bool nfb4eof; /* NFB4EOF encountered during streaming */ |
| 111 |
bool interweave_swap; /* swap top/bottom lines when interweaving */ |
| 112 |
struct completion last_eof_comp; |
| 113 |
}; |
| 114 |
|
| 115 |
static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev) |
| 116 |
{ |
| 117 |
return container_of(sdev, struct csi_priv, sd); |
| 118 |
} |
| 119 |
|
| 120 |
static inline struct csi_priv *notifier_to_dev(struct v4l2_async_notifier *n) |
| 121 |
{ |
| 122 |
return container_of(n, struct csi_priv, notifier); |
| 123 |
} |
| 124 |
|
| 125 |
static inline bool is_parallel_bus(struct v4l2_mbus_config *mbus_cfg) |
| 126 |
{ |
| 127 |
return mbus_cfg->type != V4L2_MBUS_CSI2_DPHY; |
| 128 |
} |
| 129 |
|
| 130 |
static inline bool is_parallel_16bit_bus(struct v4l2_mbus_config *mbus_cfg) |
| 131 |
{ |
| 132 |
return is_parallel_bus(mbus_cfg) && mbus_cfg->bus.parallel.bus_width >= 16; |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* Check for conditions that require the IPU to handle the |
| 137 |
* data internally as generic data, aka passthrough mode: |
| 138 |
* - raw bayer media bus formats, or |
| 139 |
* - BT.656 and BT.1120 (8/10-bit YUV422) data can always be processed |
| 140 |
* on-the-fly |
| 141 |
* - the CSI is receiving from a 16-bit parallel bus, or |
| 142 |
* - the CSI is receiving from an 8-bit parallel bus and the incoming |
| 143 |
* media bus format is other than UYVY8_2X8/YUYV8_2X8. |
| 144 |
*/ |
| 145 |
static inline bool requires_passthrough(struct v4l2_mbus_config *mbus_cfg, |
| 146 |
struct v4l2_mbus_framefmt *infmt, |
| 147 |
const struct imx_media_pixfmt *incc) |
| 148 |
{ |
| 149 |
if (mbus_cfg->type == V4L2_MBUS_BT656) // including BT.1120 |
| 150 |
return false; |
| 151 |
|
| 152 |
return incc->bayer || is_parallel_16bit_bus(mbus_cfg) || |
| 153 |
(is_parallel_bus(mbus_cfg) && |
| 154 |
infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 && |
| 155 |
infmt->code != MEDIA_BUS_FMT_YUYV8_2X8); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Queries the media bus config of the upstream entity that provides data to |
| 160 |
* the CSI. This will either be the entity directly upstream from the CSI-2 |
| 161 |
* receiver, directly upstream from a video mux, or directly upstream from |
| 162 |
* the CSI itself. |
| 163 |
*/ |
| 164 |
static int csi_get_upstream_mbus_config(struct csi_priv *priv, |
| 165 |
struct v4l2_mbus_config *mbus_cfg) |
| 166 |
{ |
| 167 |
struct v4l2_subdev *sd, *remote_sd; |
| 168 |
struct media_pad *remote_pad; |
| 169 |
int ret; |
| 170 |
|
| 171 |
if (!priv->src_sd) |
| 172 |
return -EPIPE; |
| 173 |
|
| 174 |
sd = priv->src_sd; |
| 175 |
|
| 176 |
switch (sd->grp_id) { |
| 177 |
case IMX_MEDIA_GRP_ID_CSI_MUX: |
| 178 |
/* |
| 179 |
* CSI is connected directly to CSI mux, skip up to |
| 180 |
* CSI-2 receiver if it is in the path, otherwise stay |
| 181 |
* with the CSI mux. |
| 182 |
*/ |
| 183 |
sd = imx_media_pipeline_subdev(&sd->entity, |
| 184 |
IMX_MEDIA_GRP_ID_CSI2, |
| 185 |
true); |
| 186 |
if (IS_ERR(sd)) |
| 187 |
sd = priv->src_sd; |
| 188 |
break; |
| 189 |
case IMX_MEDIA_GRP_ID_CSI2: |
| 190 |
break; |
| 191 |
default: |
| 192 |
/* |
| 193 |
* the source is neither the CSI mux nor the CSI-2 receiver, |
| 194 |
* get the source pad directly upstream from CSI itself. |
| 195 |
*/ |
| 196 |
sd = &priv->sd; |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
/* get source pad of entity directly upstream from sd */ |
| 201 |
remote_pad = media_entity_remote_pad_unique(&sd->entity, |
| 202 |
MEDIA_PAD_FL_SOURCE); |
| 203 |
if (IS_ERR(remote_pad)) |
| 204 |
return PTR_ERR(remote_pad); |
| 205 |
|
| 206 |
remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity); |
| 207 |
|
| 208 |
ret = v4l2_subdev_call(remote_sd, pad, get_mbus_config, |
| 209 |
remote_pad->index, mbus_cfg); |
| 210 |
if (ret == -ENOIOCTLCMD) |
| 211 |
v4l2_err(&priv->sd, |
| 212 |
"entity %s does not implement get_mbus_config()\n", |
| 213 |
remote_pad->entity->name); |
| 214 |
|
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
static void csi_idmac_put_ipu_resources(struct csi_priv *priv) |
| 219 |
{ |
| 220 |
if (priv->idmac_ch) |
| 221 |
ipu_idmac_put(priv->idmac_ch); |
| 222 |
priv->idmac_ch = NULL; |
| 223 |
|
| 224 |
if (priv->smfc) |
| 225 |
ipu_smfc_put(priv->smfc); |
| 226 |
priv->smfc = NULL; |
| 227 |
} |
| 228 |
|
| 229 |
static int csi_idmac_get_ipu_resources(struct csi_priv *priv) |
| 230 |
{ |
| 231 |
int ch_num, ret; |
| 232 |
struct ipu_smfc *smfc; |
| 233 |
struct ipuv3_channel *idmac_ch; |
| 234 |
|
| 235 |
ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id; |
| 236 |
|
| 237 |
smfc = ipu_smfc_get(priv->ipu, ch_num); |
| 238 |
if (IS_ERR(smfc)) { |
| 239 |
v4l2_err(&priv->sd, "failed to get SMFC\n"); |
| 240 |
ret = PTR_ERR(smfc); |
| 241 |
goto out; |
| 242 |
} |
| 243 |
priv->smfc = smfc; |
| 244 |
|
| 245 |
idmac_ch = ipu_idmac_get(priv->ipu, ch_num); |
| 246 |
if (IS_ERR(idmac_ch)) { |
| 247 |
v4l2_err(&priv->sd, "could not get IDMAC channel %u\n", |
| 248 |
ch_num); |
| 249 |
ret = PTR_ERR(idmac_ch); |
| 250 |
goto out; |
| 251 |
} |
| 252 |
priv->idmac_ch = idmac_ch; |
| 253 |
|
| 254 |
return 0; |
| 255 |
out: |
| 256 |
csi_idmac_put_ipu_resources(priv); |
| 257 |
return ret; |
| 258 |
} |
| 259 |
|
| 260 |
static void csi_vb2_buf_done(struct csi_priv *priv) |
| 261 |
{ |
| 262 |
struct imx_media_video_dev *vdev = priv->vdev; |
| 263 |
struct imx_media_buffer *done, *next; |
| 264 |
struct vb2_buffer *vb; |
| 265 |
dma_addr_t phys; |
| 266 |
|
| 267 |
done = priv->active_vb2_buf[priv->ipu_buf_num]; |
| 268 |
if (done) { |
| 269 |
done->vbuf.field = vdev->fmt.field; |
| 270 |
done->vbuf.sequence = priv->frame_sequence; |
| 271 |
vb = &done->vbuf.vb2_buf; |
| 272 |
vb->timestamp = ktime_get_ns(); |
| 273 |
vb2_buffer_done(vb, priv->nfb4eof ? |
| 274 |
VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); |
| 275 |
} |
| 276 |
|
| 277 |
priv->frame_sequence++; |
| 278 |
priv->nfb4eof = false; |
| 279 |
|
| 280 |
/* get next queued buffer */ |
| 281 |
next = imx_media_capture_device_next_buf(vdev); |
| 282 |
if (next) { |
| 283 |
phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0); |
| 284 |
priv->active_vb2_buf[priv->ipu_buf_num] = next; |
| 285 |
} else { |
| 286 |
phys = priv->underrun_buf.phys; |
| 287 |
priv->active_vb2_buf[priv->ipu_buf_num] = NULL; |
| 288 |
} |
| 289 |
|
| 290 |
if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num)) |
| 291 |
ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num); |
| 292 |
|
| 293 |
if (priv->interweave_swap) |
| 294 |
phys += vdev->fmt.bytesperline; |
| 295 |
|
| 296 |
ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys); |
| 297 |
} |
| 298 |
|
| 299 |
static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id) |
| 300 |
{ |
| 301 |
struct csi_priv *priv = dev_id; |
| 302 |
|
| 303 |
spin_lock(&priv->irqlock); |
| 304 |
|
| 305 |
if (priv->last_eof) { |
| 306 |
complete(&priv->last_eof_comp); |
| 307 |
priv->last_eof = false; |
| 308 |
goto unlock; |
| 309 |
} |
| 310 |
|
| 311 |
if (priv->fim) |
| 312 |
/* call frame interval monitor */ |
| 313 |
imx_media_fim_eof_monitor(priv->fim, ktime_get()); |
| 314 |
|
| 315 |
csi_vb2_buf_done(priv); |
| 316 |
|
| 317 |
/* select new IPU buf */ |
| 318 |
ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num); |
| 319 |
/* toggle IPU double-buffer index */ |
| 320 |
priv->ipu_buf_num ^= 1; |
| 321 |
|
| 322 |
/* bump the EOF timeout timer */ |
| 323 |
mod_timer(&priv->eof_timeout_timer, |
| 324 |
jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); |
| 325 |
|
| 326 |
unlock: |
| 327 |
spin_unlock(&priv->irqlock); |
| 328 |
return IRQ_HANDLED; |
| 329 |
} |
| 330 |
|
| 331 |
static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id) |
| 332 |
{ |
| 333 |
struct csi_priv *priv = dev_id; |
| 334 |
|
| 335 |
spin_lock(&priv->irqlock); |
| 336 |
|
| 337 |
/* |
| 338 |
* this is not an unrecoverable error, just mark |
| 339 |
* the next captured frame with vb2 error flag. |
| 340 |
*/ |
| 341 |
priv->nfb4eof = true; |
| 342 |
|
| 343 |
v4l2_err(&priv->sd, "NFB4EOF\n"); |
| 344 |
|
| 345 |
spin_unlock(&priv->irqlock); |
| 346 |
|
| 347 |
return IRQ_HANDLED; |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* EOF timeout timer function. This is an unrecoverable condition |
| 352 |
* without a stream restart. |
| 353 |
*/ |
| 354 |
static void csi_idmac_eof_timeout(struct timer_list *t) |
| 355 |
{ |
| 356 |
struct csi_priv *priv = timer_container_of(priv, t, eof_timeout_timer); |
| 357 |
struct imx_media_video_dev *vdev = priv->vdev; |
| 358 |
|
| 359 |
v4l2_err(&priv->sd, "EOF timeout\n"); |
| 360 |
|
| 361 |
/* signal a fatal error to capture device */ |
| 362 |
imx_media_capture_device_error(vdev); |
| 363 |
} |
| 364 |
|
| 365 |
static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys) |
| 366 |
{ |
| 367 |
struct imx_media_video_dev *vdev = priv->vdev; |
| 368 |
struct imx_media_buffer *buf; |
| 369 |
int i; |
| 370 |
|
| 371 |
for (i = 0; i < 2; i++) { |
| 372 |
buf = imx_media_capture_device_next_buf(vdev); |
| 373 |
if (buf) { |
| 374 |
priv->active_vb2_buf[i] = buf; |
| 375 |
phys[i] = vb2_dma_contig_plane_dma_addr( |
| 376 |
&buf->vbuf.vb2_buf, 0); |
| 377 |
} else { |
| 378 |
priv->active_vb2_buf[i] = NULL; |
| 379 |
phys[i] = priv->underrun_buf.phys; |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv, |
| 385 |
enum vb2_buffer_state return_status) |
| 386 |
{ |
| 387 |
struct imx_media_buffer *buf; |
| 388 |
int i; |
| 389 |
|
| 390 |
/* return any remaining active frames with return_status */ |
| 391 |
for (i = 0; i < 2; i++) { |
| 392 |
buf = priv->active_vb2_buf[i]; |
| 393 |
if (buf) { |
| 394 |
struct vb2_buffer *vb = &buf->vbuf.vb2_buf; |
| 395 |
|
| 396 |
vb->timestamp = ktime_get_ns(); |
| 397 |
vb2_buffer_done(vb, return_status); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/* init the SMFC IDMAC channel */ |
| 403 |
static int csi_idmac_setup_channel(struct csi_priv *priv, |
| 404 |
struct v4l2_mbus_config *mbus_cfg) |
| 405 |
{ |
| 406 |
struct imx_media_video_dev *vdev = priv->vdev; |
| 407 |
const struct imx_media_pixfmt *incc; |
| 408 |
struct v4l2_mbus_framefmt *infmt; |
| 409 |
struct v4l2_mbus_framefmt *outfmt; |
| 410 |
bool passthrough, interweave; |
| 411 |
struct ipu_image image; |
| 412 |
u32 passthrough_bits; |
| 413 |
u32 passthrough_cycles; |
| 414 |
dma_addr_t phys[2]; |
| 415 |
u32 burst_size; |
| 416 |
int ret; |
| 417 |
|
| 418 |
infmt = &priv->format_mbus[CSI_SINK_PAD]; |
| 419 |
incc = priv->cc[CSI_SINK_PAD]; |
| 420 |
outfmt = &priv->format_mbus[CSI_SRC_PAD_IDMAC]; |
| 421 |
|
| 422 |
ipu_cpmem_zero(priv->idmac_ch); |
| 423 |
|
| 424 |
memset(&image, 0, sizeof(image)); |
| 425 |
image.pix = vdev->fmt; |
| 426 |
image.rect = vdev->compose; |
| 427 |
|
| 428 |
csi_idmac_setup_vb2_buf(priv, phys); |
| 429 |
|
| 430 |
image.phys0 = phys[0]; |
| 431 |
image.phys1 = phys[1]; |
| 432 |
|
| 433 |
passthrough = requires_passthrough(mbus_cfg, infmt, incc); |
| 434 |
passthrough_cycles = 1; |
| 435 |
|
| 436 |
/* |
| 437 |
* If the field type at capture interface is interlaced, and |
| 438 |
* the output IDMAC pad is sequential, enable interweave at |
| 439 |
* the IDMAC output channel. |
| 440 |
*/ |
| 441 |
interweave = V4L2_FIELD_IS_INTERLACED(image.pix.field) && |
| 442 |
V4L2_FIELD_IS_SEQUENTIAL(outfmt->field); |
| 443 |
priv->interweave_swap = interweave && |
| 444 |
image.pix.field == V4L2_FIELD_INTERLACED_BT; |
| 445 |
|
| 446 |
switch (image.pix.pixelformat) { |
| 447 |
case V4L2_PIX_FMT_SBGGR8: |
| 448 |
case V4L2_PIX_FMT_SGBRG8: |
| 449 |
case V4L2_PIX_FMT_SGRBG8: |
| 450 |
case V4L2_PIX_FMT_SRGGB8: |
| 451 |
case V4L2_PIX_FMT_GREY: |
| 452 |
burst_size = 16; |
| 453 |
passthrough_bits = 8; |
| 454 |
break; |
| 455 |
case V4L2_PIX_FMT_SBGGR16: |
| 456 |
case V4L2_PIX_FMT_SGBRG16: |
| 457 |
case V4L2_PIX_FMT_SGRBG16: |
| 458 |
case V4L2_PIX_FMT_SRGGB16: |
| 459 |
case V4L2_PIX_FMT_Y10: |
| 460 |
case V4L2_PIX_FMT_Y12: |
| 461 |
burst_size = 8; |
| 462 |
passthrough_bits = 16; |
| 463 |
break; |
| 464 |
case V4L2_PIX_FMT_YUV420: |
| 465 |
case V4L2_PIX_FMT_YVU420: |
| 466 |
case V4L2_PIX_FMT_NV12: |
| 467 |
burst_size = (image.pix.width & 0x3f) ? |
| 468 |
((image.pix.width & 0x1f) ? |
| 469 |
((image.pix.width & 0xf) ? 8 : 16) : 32) : 64; |
| 470 |
passthrough_bits = 16; |
| 471 |
/* |
| 472 |
* Skip writing U and V components to odd rows (but not |
| 473 |
* when enabling IDMAC interweaving, they are incompatible). |
| 474 |
*/ |
| 475 |
if (!interweave) |
| 476 |
ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch); |
| 477 |
break; |
| 478 |
case V4L2_PIX_FMT_YUYV: |
| 479 |
case V4L2_PIX_FMT_UYVY: |
| 480 |
burst_size = (image.pix.width & 0x1f) ? |
| 481 |
((image.pix.width & 0xf) ? 8 : 16) : 32; |
| 482 |
passthrough_bits = 16; |
| 483 |
break; |
| 484 |
case V4L2_PIX_FMT_RGB565: |
| 485 |
if (passthrough) { |
| 486 |
burst_size = 16; |
| 487 |
passthrough_bits = 8; |
| 488 |
passthrough_cycles = incc->cycles; |
| 489 |
break; |
| 490 |
} |
| 491 |
fallthrough; /* non-passthrough RGB565 (CSI-2 bus) */ |
| 492 |
default: |
| 493 |
burst_size = (image.pix.width & 0xf) ? 8 : 16; |
| 494 |
passthrough_bits = 16; |
| 495 |
break; |
| 496 |
} |
| 497 |
|
| 498 |
if (passthrough) { |
| 499 |
if (priv->interweave_swap) { |
| 500 |
/* start interweave scan at 1st top line (2nd line) */ |
| 501 |
image.phys0 += image.pix.bytesperline; |
| 502 |
image.phys1 += image.pix.bytesperline; |
| 503 |
} |
| 504 |
|
| 505 |
ipu_cpmem_set_resolution(priv->idmac_ch, |
| 506 |
image.rect.width * passthrough_cycles, |
| 507 |
image.rect.height); |
| 508 |
ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline); |
| 509 |
ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0); |
| 510 |
ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1); |
| 511 |
ipu_cpmem_set_format_passthrough(priv->idmac_ch, |
| 512 |
passthrough_bits); |
| 513 |
} else { |
| 514 |
if (priv->interweave_swap) { |
| 515 |
/* start interweave scan at 1st top line (2nd line) */ |
| 516 |
image.rect.top = 1; |
| 517 |
} |
| 518 |
|
| 519 |
ret = ipu_cpmem_set_image(priv->idmac_ch, &image); |
| 520 |
if (ret) |
| 521 |
goto unsetup_vb2; |
| 522 |
} |
| 523 |
|
| 524 |
ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size); |
| 525 |
|
| 526 |
/* |
| 527 |
* Set the channel for the direct CSI-->memory via SMFC |
| 528 |
* use-case to very high priority, by enabling the watermark |
| 529 |
* signal in the SMFC, enabling WM in the channel, and setting |
| 530 |
* the channel priority to high. |
| 531 |
* |
| 532 |
* Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority |
| 533 |
* value. |
| 534 |
* |
| 535 |
* The WM's are set very low by intention here to ensure that |
| 536 |
* the SMFC FIFOs do not overflow. |
| 537 |
*/ |
| 538 |
ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01); |
| 539 |
ipu_cpmem_set_high_priority(priv->idmac_ch); |
| 540 |
ipu_idmac_enable_watermark(priv->idmac_ch, true); |
| 541 |
ipu_cpmem_set_axi_id(priv->idmac_ch, 0); |
| 542 |
|
| 543 |
burst_size = passthrough ? |
| 544 |
(burst_size >> 3) - 1 : (burst_size >> 2) - 1; |
| 545 |
|
| 546 |
ipu_smfc_set_burstsize(priv->smfc, burst_size); |
| 547 |
|
| 548 |
if (interweave) |
| 549 |
ipu_cpmem_interlaced_scan(priv->idmac_ch, |
| 550 |
priv->interweave_swap ? |
| 551 |
-image.pix.bytesperline : |
| 552 |
image.pix.bytesperline, |
| 553 |
image.pix.pixelformat); |
| 554 |
|
| 555 |
ipu_idmac_set_double_buffer(priv->idmac_ch, true); |
| 556 |
|
| 557 |
return 0; |
| 558 |
|
| 559 |
unsetup_vb2: |
| 560 |
csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED); |
| 561 |
return ret; |
| 562 |
} |
| 563 |
|
| 564 |
static void csi_idmac_unsetup(struct csi_priv *priv, |
| 565 |
enum vb2_buffer_state state) |
| 566 |
{ |
| 567 |
ipu_idmac_disable_channel(priv->idmac_ch); |
| 568 |
ipu_smfc_disable(priv->smfc); |
| 569 |
|
| 570 |
csi_idmac_unsetup_vb2_buf(priv, state); |
| 571 |
} |
| 572 |
|
| 573 |
static int csi_idmac_setup(struct csi_priv *priv, |
| 574 |
struct v4l2_mbus_config *mbus_cfg) |
| 575 |
{ |
| 576 |
int ret; |
| 577 |
|
| 578 |
ret = csi_idmac_setup_channel(priv, mbus_cfg); |
| 579 |
if (ret) |
| 580 |
return ret; |
| 581 |
|
| 582 |
ipu_cpmem_dump(priv->idmac_ch); |
| 583 |
ipu_dump(priv->ipu); |
| 584 |
|
| 585 |
ipu_smfc_enable(priv->smfc); |
| 586 |
|
| 587 |
/* set buffers ready */ |
| 588 |
ipu_idmac_select_buffer(priv->idmac_ch, 0); |
| 589 |
ipu_idmac_select_buffer(priv->idmac_ch, 1); |
| 590 |
|
| 591 |
/* enable the channels */ |
| 592 |
ipu_idmac_enable_channel(priv->idmac_ch); |
| 593 |
|
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
static int csi_idmac_start(struct csi_priv *priv, |
| 598 |
struct v4l2_mbus_config *mbus_cfg) |
| 599 |
{ |
| 600 |
struct imx_media_video_dev *vdev = priv->vdev; |
| 601 |
int ret; |
| 602 |
|
| 603 |
ret = csi_idmac_get_ipu_resources(priv); |
| 604 |
if (ret) |
| 605 |
return ret; |
| 606 |
|
| 607 |
ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num); |
| 608 |
|
| 609 |
ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf, |
| 610 |
vdev->fmt.sizeimage); |
| 611 |
if (ret) |
| 612 |
goto out_put_ipu; |
| 613 |
|
| 614 |
priv->ipu_buf_num = 0; |
| 615 |
|
| 616 |
/* init EOF completion waitq */ |
| 617 |
init_completion(&priv->last_eof_comp); |
| 618 |
priv->frame_sequence = 0; |
| 619 |
priv->last_eof = false; |
| 620 |
priv->nfb4eof = false; |
| 621 |
|
| 622 |
ret = csi_idmac_setup(priv, mbus_cfg); |
| 623 |
if (ret) { |
| 624 |
v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret); |
| 625 |
goto out_free_dma_buf; |
| 626 |
} |
| 627 |
|
| 628 |
priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu, |
| 629 |
priv->idmac_ch, |
| 630 |
IPU_IRQ_NFB4EOF); |
| 631 |
ret = devm_request_irq(priv->dev, priv->nfb4eof_irq, |
| 632 |
csi_idmac_nfb4eof_interrupt, 0, |
| 633 |
"imx-smfc-nfb4eof", priv); |
| 634 |
if (ret) { |
| 635 |
v4l2_err(&priv->sd, |
| 636 |
"Error registering NFB4EOF irq: %d\n", ret); |
| 637 |
goto out_unsetup; |
| 638 |
} |
| 639 |
|
| 640 |
priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch, |
| 641 |
IPU_IRQ_EOF); |
| 642 |
|
| 643 |
ret = devm_request_irq(priv->dev, priv->eof_irq, |
| 644 |
csi_idmac_eof_interrupt, 0, |
| 645 |
"imx-smfc-eof", priv); |
| 646 |
if (ret) { |
| 647 |
v4l2_err(&priv->sd, |
| 648 |
"Error registering eof irq: %d\n", ret); |
| 649 |
goto out_free_nfb4eof_irq; |
| 650 |
} |
| 651 |
|
| 652 |
/* start the EOF timeout timer */ |
| 653 |
mod_timer(&priv->eof_timeout_timer, |
| 654 |
jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); |
| 655 |
|
| 656 |
return 0; |
| 657 |
|
| 658 |
out_free_nfb4eof_irq: |
| 659 |
devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); |
| 660 |
out_unsetup: |
| 661 |
csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED); |
| 662 |
out_free_dma_buf: |
| 663 |
imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); |
| 664 |
out_put_ipu: |
| 665 |
csi_idmac_put_ipu_resources(priv); |
| 666 |
return ret; |
| 667 |
} |
| 668 |
|
| 669 |
static void csi_idmac_wait_last_eof(struct csi_priv *priv) |
| 670 |
{ |
| 671 |
unsigned long flags; |
| 672 |
int ret; |
| 673 |
|
| 674 |
/* mark next EOF interrupt as the last before stream off */ |
| 675 |
spin_lock_irqsave(&priv->irqlock, flags); |
| 676 |
priv->last_eof = true; |
| 677 |
spin_unlock_irqrestore(&priv->irqlock, flags); |
| 678 |
|
| 679 |
/* |
| 680 |
* and then wait for interrupt handler to mark completion. |
| 681 |
*/ |
| 682 |
ret = wait_for_completion_timeout( |
| 683 |
&priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); |
| 684 |
if (ret == 0) |
| 685 |
v4l2_warn(&priv->sd, "wait last EOF timeout\n"); |
| 686 |
} |
| 687 |
|
| 688 |
static void csi_idmac_stop(struct csi_priv *priv) |
| 689 |
{ |
| 690 |
devm_free_irq(priv->dev, priv->eof_irq, priv); |
| 691 |
devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); |
| 692 |
|
| 693 |
csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR); |
| 694 |
|
| 695 |
imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); |
| 696 |
|
| 697 |
/* cancel the EOF timeout timer */ |
| 698 |
timer_delete_sync(&priv->eof_timeout_timer); |
| 699 |
|
| 700 |
csi_idmac_put_ipu_resources(priv); |
| 701 |
} |
| 702 |
|
| 703 |
/* Update the CSI whole sensor and active windows */ |
| 704 |
static int csi_setup(struct csi_priv *priv, |
| 705 |
struct v4l2_mbus_config *mbus_cfg) |
| 706 |
{ |
| 707 |
struct v4l2_mbus_framefmt *infmt, *outfmt; |
| 708 |
const struct imx_media_pixfmt *incc; |
| 709 |
struct v4l2_mbus_framefmt if_fmt; |
| 710 |
struct v4l2_rect crop; |
| 711 |
|
| 712 |
infmt = &priv->format_mbus[CSI_SINK_PAD]; |
| 713 |
incc = priv->cc[CSI_SINK_PAD]; |
| 714 |
outfmt = &priv->format_mbus[priv->active_output_pad]; |
| 715 |
|
| 716 |
if_fmt = *infmt; |
| 717 |
crop = priv->crop; |
| 718 |
|
| 719 |
/* |
| 720 |
* if cycles is set, we need to handle this over multiple cycles as |
| 721 |
* generic/bayer data |
| 722 |
*/ |
| 723 |
if (is_parallel_bus(mbus_cfg) && incc->cycles) { |
| 724 |
if_fmt.width *= incc->cycles; |
| 725 |
crop.width *= incc->cycles; |
| 726 |
} |
| 727 |
|
| 728 |
ipu_csi_set_window(priv->csi, &crop); |
| 729 |
|
| 730 |
ipu_csi_set_downsize(priv->csi, |
| 731 |
priv->crop.width == 2 * priv->compose.width, |
| 732 |
priv->crop.height == 2 * priv->compose.height); |
| 733 |
|
| 734 |
ipu_csi_init_interface(priv->csi, mbus_cfg, &if_fmt, outfmt); |
| 735 |
|
| 736 |
ipu_csi_set_dest(priv->csi, priv->dest); |
| 737 |
|
| 738 |
if (priv->dest == IPU_CSI_DEST_IDMAC) |
| 739 |
ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc, |
| 740 |
priv->skip->max_ratio - 1, 0); |
| 741 |
|
| 742 |
ipu_csi_dump(priv->csi); |
| 743 |
|
| 744 |
return 0; |
| 745 |
} |
| 746 |
|
| 747 |
static void csi_set_src(struct csi_priv *priv, |
| 748 |
struct v4l2_mbus_config *mbus_cfg) |
| 749 |
{ |
| 750 |
bool is_csi2; |
| 751 |
|
| 752 |
is_csi2 = !is_parallel_bus(mbus_cfg); |
| 753 |
if (is_csi2) { |
| 754 |
/* |
| 755 |
* NOTE! It seems the virtual channels from the mipi csi-2 |
| 756 |
* receiver are used only for routing by the video mux's, |
| 757 |
* or for hard-wired routing to the CSI's. Once the stream |
| 758 |
* enters the CSI's however, they are treated internally |
| 759 |
* in the IPU as virtual channel 0. |
| 760 |
*/ |
| 761 |
ipu_csi_set_mipi_datatype(priv->csi, 0, |
| 762 |
&priv->format_mbus[CSI_SINK_PAD]); |
| 763 |
} |
| 764 |
|
| 765 |
/* select either parallel or MIPI-CSI2 as input to CSI */ |
| 766 |
ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2); |
| 767 |
} |
| 768 |
|
| 769 |
static int csi_start(struct csi_priv *priv) |
| 770 |
{ |
| 771 |
struct v4l2_mbus_config mbus_cfg = { .type = 0 }; |
| 772 |
struct v4l2_fract *input_fi, *output_fi; |
| 773 |
int ret; |
| 774 |
|
| 775 |
ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); |
| 776 |
if (ret) { |
| 777 |
v4l2_err(&priv->sd, |
| 778 |
"failed to get upstream media bus configuration\n"); |
| 779 |
return ret; |
| 780 |
} |
| 781 |
|
| 782 |
input_fi = &priv->frame_interval[CSI_SINK_PAD]; |
| 783 |
output_fi = &priv->frame_interval[priv->active_output_pad]; |
| 784 |
|
| 785 |
csi_set_src(priv, &mbus_cfg); |
| 786 |
|
| 787 |
/* start upstream */ |
| 788 |
ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1); |
| 789 |
ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0; |
| 790 |
if (ret) |
| 791 |
return ret; |
| 792 |
|
| 793 |
/* Skip first few frames from a BT.656 source */ |
| 794 |
if (mbus_cfg.type == V4L2_MBUS_BT656) { |
| 795 |
u32 delay_usec, bad_frames = 20; |
| 796 |
|
| 797 |
delay_usec = DIV_ROUND_UP_ULL((u64)USEC_PER_SEC * |
| 798 |
input_fi->numerator * bad_frames, |
| 799 |
input_fi->denominator); |
| 800 |
|
| 801 |
usleep_range(delay_usec, delay_usec + 1000); |
| 802 |
} |
| 803 |
|
| 804 |
if (priv->dest == IPU_CSI_DEST_IDMAC) { |
| 805 |
ret = csi_idmac_start(priv, &mbus_cfg); |
| 806 |
if (ret) |
| 807 |
goto stop_upstream; |
| 808 |
} |
| 809 |
|
| 810 |
ret = csi_setup(priv, &mbus_cfg); |
| 811 |
if (ret) |
| 812 |
goto idmac_stop; |
| 813 |
|
| 814 |
/* start the frame interval monitor */ |
| 815 |
if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) |
| 816 |
imx_media_fim_set_stream(priv->fim, output_fi, true); |
| 817 |
|
| 818 |
ret = ipu_csi_enable(priv->csi); |
| 819 |
if (ret) { |
| 820 |
v4l2_err(&priv->sd, "CSI enable error: %d\n", ret); |
| 821 |
goto fim_off; |
| 822 |
} |
| 823 |
|
| 824 |
return 0; |
| 825 |
|
| 826 |
fim_off: |
| 827 |
if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) |
| 828 |
imx_media_fim_set_stream(priv->fim, NULL, false); |
| 829 |
idmac_stop: |
| 830 |
if (priv->dest == IPU_CSI_DEST_IDMAC) |
| 831 |
csi_idmac_stop(priv); |
| 832 |
stop_upstream: |
| 833 |
v4l2_subdev_call(priv->src_sd, video, s_stream, 0); |
| 834 |
return ret; |
| 835 |
} |
| 836 |
|
| 837 |
static void csi_stop(struct csi_priv *priv) |
| 838 |
{ |
| 839 |
if (priv->dest == IPU_CSI_DEST_IDMAC) |
| 840 |
csi_idmac_wait_last_eof(priv); |
| 841 |
|
| 842 |
/* |
| 843 |
* Disable the CSI asap, after syncing with the last EOF. |
| 844 |
* Doing so after the IDMA channel is disabled has shown to |
| 845 |
* create hard system-wide hangs. |
| 846 |
*/ |
| 847 |
ipu_csi_disable(priv->csi); |
| 848 |
|
| 849 |
/* stop upstream */ |
| 850 |
v4l2_subdev_call(priv->src_sd, video, s_stream, 0); |
| 851 |
|
| 852 |
if (priv->dest == IPU_CSI_DEST_IDMAC) { |
| 853 |
csi_idmac_stop(priv); |
| 854 |
|
| 855 |
/* stop the frame interval monitor */ |
| 856 |
if (priv->fim) |
| 857 |
imx_media_fim_set_stream(priv->fim, NULL, false); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
static const struct csi_skip_desc csi_skip[12] = { |
| 862 |
{ 1, 1, 0x00 }, /* Keep all frames */ |
| 863 |
{ 5, 6, 0x10 }, /* Skip every sixth frame */ |
| 864 |
{ 4, 5, 0x08 }, /* Skip every fifth frame */ |
| 865 |
{ 3, 4, 0x04 }, /* Skip every fourth frame */ |
| 866 |
{ 2, 3, 0x02 }, /* Skip every third frame */ |
| 867 |
{ 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */ |
| 868 |
{ 1, 2, 0x01 }, /* Skip every second frame */ |
| 869 |
{ 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */ |
| 870 |
{ 1, 3, 0x03 }, /* Keep one in three frames */ |
| 871 |
{ 1, 4, 0x07 }, /* Keep one in four frames */ |
| 872 |
{ 1, 5, 0x0f }, /* Keep one in five frames */ |
| 873 |
{ 1, 6, 0x1f }, /* Keep one in six frames */ |
| 874 |
}; |
| 875 |
|
| 876 |
static void csi_apply_skip_interval(const struct csi_skip_desc *skip, |
| 877 |
struct v4l2_fract *interval) |
| 878 |
{ |
| 879 |
unsigned int div; |
| 880 |
|
| 881 |
interval->numerator *= skip->max_ratio; |
| 882 |
interval->denominator *= skip->keep; |
| 883 |
|
| 884 |
/* Reduce fraction to lowest terms */ |
| 885 |
div = gcd(interval->numerator, interval->denominator); |
| 886 |
if (div > 1) { |
| 887 |
interval->numerator /= div; |
| 888 |
interval->denominator /= div; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
* Find the skip pattern to produce the output frame interval closest to the |
| 894 |
* requested one, for the given input frame interval. Updates the output frame |
| 895 |
* interval to the exact value. |
| 896 |
*/ |
| 897 |
static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in, |
| 898 |
struct v4l2_fract *out) |
| 899 |
{ |
| 900 |
const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip; |
| 901 |
u32 min_err = UINT_MAX; |
| 902 |
u64 want_us; |
| 903 |
int i; |
| 904 |
|
| 905 |
/* Default to 1:1 ratio */ |
| 906 |
if (out->numerator == 0 || out->denominator == 0 || |
| 907 |
in->numerator == 0 || in->denominator == 0) { |
| 908 |
*out = *in; |
| 909 |
return best_skip; |
| 910 |
} |
| 911 |
|
| 912 |
want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator); |
| 913 |
|
| 914 |
/* Find the reduction closest to the requested time per frame */ |
| 915 |
for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) { |
| 916 |
u64 tmp, err; |
| 917 |
|
| 918 |
tmp = div_u64((u64)USEC_PER_SEC * in->numerator * |
| 919 |
skip->max_ratio, in->denominator * skip->keep); |
| 920 |
|
| 921 |
err = abs((s64)tmp - want_us); |
| 922 |
if (err < min_err) { |
| 923 |
min_err = err; |
| 924 |
best_skip = skip; |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
*out = *in; |
| 929 |
csi_apply_skip_interval(best_skip, out); |
| 930 |
|
| 931 |
return best_skip; |
| 932 |
} |
| 933 |
|
| 934 |
/* |
| 935 |
* V4L2 subdev operations. |
| 936 |
*/ |
| 937 |
|
| 938 |
static int csi_get_frame_interval(struct v4l2_subdev *sd, |
| 939 |
struct v4l2_subdev_state *sd_state, |
| 940 |
struct v4l2_subdev_frame_interval *fi) |
| 941 |
{ |
| 942 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 943 |
|
| 944 |
/* |
| 945 |
* FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2 |
| 946 |
* subdev active state API. |
| 947 |
*/ |
| 948 |
if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 949 |
return -EINVAL; |
| 950 |
|
| 951 |
if (fi->pad >= CSI_NUM_PADS) |
| 952 |
return -EINVAL; |
| 953 |
|
| 954 |
mutex_lock(&priv->lock); |
| 955 |
|
| 956 |
fi->interval = priv->frame_interval[fi->pad]; |
| 957 |
|
| 958 |
mutex_unlock(&priv->lock); |
| 959 |
|
| 960 |
return 0; |
| 961 |
} |
| 962 |
|
| 963 |
static int csi_set_frame_interval(struct v4l2_subdev *sd, |
| 964 |
struct v4l2_subdev_state *sd_state, |
| 965 |
struct v4l2_subdev_frame_interval *fi) |
| 966 |
{ |
| 967 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 968 |
struct v4l2_fract *input_fi; |
| 969 |
int ret = 0; |
| 970 |
|
| 971 |
/* |
| 972 |
* FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2 |
| 973 |
* subdev active state API. |
| 974 |
*/ |
| 975 |
if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 976 |
return -EINVAL; |
| 977 |
|
| 978 |
mutex_lock(&priv->lock); |
| 979 |
|
| 980 |
input_fi = &priv->frame_interval[CSI_SINK_PAD]; |
| 981 |
|
| 982 |
switch (fi->pad) { |
| 983 |
case CSI_SINK_PAD: |
| 984 |
/* No limits on valid input frame intervals */ |
| 985 |
if (fi->interval.numerator == 0 || |
| 986 |
fi->interval.denominator == 0) |
| 987 |
fi->interval = *input_fi; |
| 988 |
/* Reset output intervals and frame skipping ratio to 1:1 */ |
| 989 |
priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval; |
| 990 |
priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval; |
| 991 |
priv->skip = &csi_skip[0]; |
| 992 |
break; |
| 993 |
case CSI_SRC_PAD_IDMAC: |
| 994 |
/* |
| 995 |
* frame interval at IDMAC output pad depends on input |
| 996 |
* interval, modified by frame skipping. |
| 997 |
*/ |
| 998 |
priv->skip = csi_find_best_skip(input_fi, &fi->interval); |
| 999 |
break; |
| 1000 |
case CSI_SRC_PAD_DIRECT: |
| 1001 |
/* |
| 1002 |
* frame interval at DIRECT output pad is same as input |
| 1003 |
* interval. |
| 1004 |
*/ |
| 1005 |
fi->interval = *input_fi; |
| 1006 |
break; |
| 1007 |
default: |
| 1008 |
ret = -EINVAL; |
| 1009 |
goto out; |
| 1010 |
} |
| 1011 |
|
| 1012 |
priv->frame_interval[fi->pad] = fi->interval; |
| 1013 |
out: |
| 1014 |
mutex_unlock(&priv->lock); |
| 1015 |
return ret; |
| 1016 |
} |
| 1017 |
|
| 1018 |
static int csi_s_stream(struct v4l2_subdev *sd, int enable) |
| 1019 |
{ |
| 1020 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1021 |
int ret = 0; |
| 1022 |
|
| 1023 |
mutex_lock(&priv->lock); |
| 1024 |
|
| 1025 |
if (!priv->src_sd || !priv->sink) { |
| 1026 |
ret = -EPIPE; |
| 1027 |
goto out; |
| 1028 |
} |
| 1029 |
|
| 1030 |
/* |
| 1031 |
* enable/disable streaming only if stream_count is |
| 1032 |
* going from 0 to 1 / 1 to 0. |
| 1033 |
*/ |
| 1034 |
if (priv->stream_count != !enable) |
| 1035 |
goto update_count; |
| 1036 |
|
| 1037 |
if (enable) { |
| 1038 |
dev_dbg(priv->dev, "stream ON\n"); |
| 1039 |
ret = csi_start(priv); |
| 1040 |
if (ret) |
| 1041 |
goto out; |
| 1042 |
} else { |
| 1043 |
dev_dbg(priv->dev, "stream OFF\n"); |
| 1044 |
csi_stop(priv); |
| 1045 |
} |
| 1046 |
|
| 1047 |
update_count: |
| 1048 |
priv->stream_count += enable ? 1 : -1; |
| 1049 |
if (priv->stream_count < 0) |
| 1050 |
priv->stream_count = 0; |
| 1051 |
out: |
| 1052 |
mutex_unlock(&priv->lock); |
| 1053 |
return ret; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static int csi_link_setup(struct media_entity *entity, |
| 1057 |
const struct media_pad *local, |
| 1058 |
const struct media_pad *remote, u32 flags) |
| 1059 |
{ |
| 1060 |
struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
| 1061 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1062 |
struct v4l2_subdev *remote_sd; |
| 1063 |
int ret = 0; |
| 1064 |
|
| 1065 |
dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name, |
| 1066 |
local->entity->name); |
| 1067 |
|
| 1068 |
mutex_lock(&priv->lock); |
| 1069 |
|
| 1070 |
if (local->flags & MEDIA_PAD_FL_SINK) { |
| 1071 |
if (!is_media_entity_v4l2_subdev(remote->entity)) { |
| 1072 |
ret = -EINVAL; |
| 1073 |
goto out; |
| 1074 |
} |
| 1075 |
|
| 1076 |
remote_sd = media_entity_to_v4l2_subdev(remote->entity); |
| 1077 |
|
| 1078 |
if (flags & MEDIA_LNK_FL_ENABLED) { |
| 1079 |
if (priv->src_sd) { |
| 1080 |
ret = -EBUSY; |
| 1081 |
goto out; |
| 1082 |
} |
| 1083 |
priv->src_sd = remote_sd; |
| 1084 |
} else { |
| 1085 |
priv->src_sd = NULL; |
| 1086 |
} |
| 1087 |
|
| 1088 |
goto out; |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* this is a source pad */ |
| 1092 |
|
| 1093 |
if (flags & MEDIA_LNK_FL_ENABLED) { |
| 1094 |
if (priv->sink) { |
| 1095 |
ret = -EBUSY; |
| 1096 |
goto out; |
| 1097 |
} |
| 1098 |
} else { |
| 1099 |
v4l2_ctrl_handler_free(&priv->ctrl_hdlr); |
| 1100 |
v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); |
| 1101 |
priv->sink = NULL; |
| 1102 |
/* do not apply IC burst alignment in csi_try_crop */ |
| 1103 |
priv->active_output_pad = CSI_SRC_PAD_IDMAC; |
| 1104 |
goto out; |
| 1105 |
} |
| 1106 |
|
| 1107 |
/* record which output pad is now active */ |
| 1108 |
priv->active_output_pad = local->index; |
| 1109 |
|
| 1110 |
/* set CSI destination */ |
| 1111 |
if (local->index == CSI_SRC_PAD_IDMAC) { |
| 1112 |
if (!is_media_entity_v4l2_video_device(remote->entity)) { |
| 1113 |
ret = -EINVAL; |
| 1114 |
goto out; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (priv->fim) { |
| 1118 |
ret = imx_media_fim_add_controls(priv->fim); |
| 1119 |
if (ret) |
| 1120 |
goto out; |
| 1121 |
} |
| 1122 |
|
| 1123 |
priv->dest = IPU_CSI_DEST_IDMAC; |
| 1124 |
} else { |
| 1125 |
if (!is_media_entity_v4l2_subdev(remote->entity)) { |
| 1126 |
ret = -EINVAL; |
| 1127 |
goto out; |
| 1128 |
} |
| 1129 |
|
| 1130 |
remote_sd = media_entity_to_v4l2_subdev(remote->entity); |
| 1131 |
switch (remote_sd->grp_id) { |
| 1132 |
case IMX_MEDIA_GRP_ID_IPU_VDIC: |
| 1133 |
priv->dest = IPU_CSI_DEST_VDIC; |
| 1134 |
break; |
| 1135 |
case IMX_MEDIA_GRP_ID_IPU_IC_PRP: |
| 1136 |
priv->dest = IPU_CSI_DEST_IC; |
| 1137 |
break; |
| 1138 |
default: |
| 1139 |
ret = -EINVAL; |
| 1140 |
goto out; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
priv->sink = remote->entity; |
| 1145 |
out: |
| 1146 |
mutex_unlock(&priv->lock); |
| 1147 |
return ret; |
| 1148 |
} |
| 1149 |
|
| 1150 |
static int csi_link_validate(struct v4l2_subdev *sd, |
| 1151 |
struct media_link *link, |
| 1152 |
struct v4l2_subdev_format *source_fmt, |
| 1153 |
struct v4l2_subdev_format *sink_fmt) |
| 1154 |
{ |
| 1155 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1156 |
struct v4l2_mbus_config mbus_cfg = { .type = 0 }; |
| 1157 |
int ret; |
| 1158 |
|
| 1159 |
ret = v4l2_subdev_link_validate_default(sd, link, |
| 1160 |
source_fmt, sink_fmt); |
| 1161 |
if (ret) |
| 1162 |
return ret; |
| 1163 |
|
| 1164 |
ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); |
| 1165 |
if (ret) { |
| 1166 |
v4l2_err(&priv->sd, |
| 1167 |
"failed to get upstream media bus configuration\n"); |
| 1168 |
return ret; |
| 1169 |
} |
| 1170 |
|
| 1171 |
return ret; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static struct v4l2_mbus_framefmt * |
| 1175 |
__csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, |
| 1176 |
unsigned int pad, enum v4l2_subdev_format_whence which) |
| 1177 |
{ |
| 1178 |
if (which == V4L2_SUBDEV_FORMAT_TRY) |
| 1179 |
return v4l2_subdev_state_get_format(sd_state, pad); |
| 1180 |
else |
| 1181 |
return &priv->format_mbus[pad]; |
| 1182 |
} |
| 1183 |
|
| 1184 |
static struct v4l2_rect * |
| 1185 |
__csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, |
| 1186 |
enum v4l2_subdev_format_whence which) |
| 1187 |
{ |
| 1188 |
if (which == V4L2_SUBDEV_FORMAT_TRY) |
| 1189 |
return v4l2_subdev_state_get_crop(sd_state, CSI_SINK_PAD); |
| 1190 |
else |
| 1191 |
return &priv->crop; |
| 1192 |
} |
| 1193 |
|
| 1194 |
static struct v4l2_rect * |
| 1195 |
__csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, |
| 1196 |
enum v4l2_subdev_format_whence which) |
| 1197 |
{ |
| 1198 |
if (which == V4L2_SUBDEV_FORMAT_TRY) |
| 1199 |
return v4l2_subdev_state_get_compose(sd_state, CSI_SINK_PAD); |
| 1200 |
else |
| 1201 |
return &priv->compose; |
| 1202 |
} |
| 1203 |
|
| 1204 |
static void csi_try_crop(struct csi_priv *priv, |
| 1205 |
struct v4l2_rect *crop, |
| 1206 |
struct v4l2_subdev_state *sd_state, |
| 1207 |
struct v4l2_mbus_framefmt *infmt, |
| 1208 |
struct v4l2_mbus_config *mbus_cfg) |
| 1209 |
{ |
| 1210 |
u32 in_height; |
| 1211 |
|
| 1212 |
crop->width = min_t(__u32, infmt->width, crop->width); |
| 1213 |
if (crop->left + crop->width > infmt->width) |
| 1214 |
crop->left = infmt->width - crop->width; |
| 1215 |
/* adjust crop left/width to h/w alignment restrictions */ |
| 1216 |
crop->left &= ~0x3; |
| 1217 |
if (priv->active_output_pad == CSI_SRC_PAD_DIRECT) |
| 1218 |
crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */ |
| 1219 |
else |
| 1220 |
crop->width &= ~0x1; /* multiple of 2 pixels */ |
| 1221 |
|
| 1222 |
in_height = infmt->height; |
| 1223 |
if (infmt->field == V4L2_FIELD_ALTERNATE) |
| 1224 |
in_height *= 2; |
| 1225 |
|
| 1226 |
/* |
| 1227 |
* FIXME: not sure why yet, but on interlaced bt.656, |
| 1228 |
* changing the vertical cropping causes loss of vertical |
| 1229 |
* sync, so fix it to NTSC/PAL active lines. NTSC contains |
| 1230 |
* 2 extra lines of active video that need to be cropped. |
| 1231 |
*/ |
| 1232 |
if (mbus_cfg->type == V4L2_MBUS_BT656 && |
| 1233 |
(V4L2_FIELD_HAS_BOTH(infmt->field) || |
| 1234 |
infmt->field == V4L2_FIELD_ALTERNATE)) { |
| 1235 |
crop->height = in_height; |
| 1236 |
crop->top = (in_height == 480) ? 2 : 0; |
| 1237 |
} else { |
| 1238 |
crop->height = min_t(__u32, in_height, crop->height); |
| 1239 |
if (crop->top + crop->height > in_height) |
| 1240 |
crop->top = in_height - crop->height; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
static int csi_enum_mbus_code(struct v4l2_subdev *sd, |
| 1245 |
struct v4l2_subdev_state *sd_state, |
| 1246 |
struct v4l2_subdev_mbus_code_enum *code) |
| 1247 |
{ |
| 1248 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1249 |
struct v4l2_mbus_config mbus_cfg = { .type = 0 }; |
| 1250 |
const struct imx_media_pixfmt *incc; |
| 1251 |
struct v4l2_mbus_framefmt *infmt; |
| 1252 |
int ret = 0; |
| 1253 |
|
| 1254 |
mutex_lock(&priv->lock); |
| 1255 |
|
| 1256 |
infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, code->which); |
| 1257 |
incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); |
| 1258 |
|
| 1259 |
switch (code->pad) { |
| 1260 |
case CSI_SINK_PAD: |
| 1261 |
ret = imx_media_enum_mbus_formats(&code->code, code->index, |
| 1262 |
PIXFMT_SEL_ANY); |
| 1263 |
break; |
| 1264 |
case CSI_SRC_PAD_DIRECT: |
| 1265 |
case CSI_SRC_PAD_IDMAC: |
| 1266 |
ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); |
| 1267 |
if (ret) { |
| 1268 |
v4l2_err(&priv->sd, |
| 1269 |
"failed to get upstream media bus configuration\n"); |
| 1270 |
goto out; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (requires_passthrough(&mbus_cfg, infmt, incc)) { |
| 1274 |
if (code->index != 0) { |
| 1275 |
ret = -EINVAL; |
| 1276 |
goto out; |
| 1277 |
} |
| 1278 |
code->code = infmt->code; |
| 1279 |
} else { |
| 1280 |
enum imx_pixfmt_sel fmt_sel = |
| 1281 |
(incc->cs == IPUV3_COLORSPACE_YUV) ? |
| 1282 |
PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; |
| 1283 |
|
| 1284 |
ret = imx_media_enum_ipu_formats(&code->code, |
| 1285 |
code->index, |
| 1286 |
fmt_sel); |
| 1287 |
} |
| 1288 |
break; |
| 1289 |
default: |
| 1290 |
ret = -EINVAL; |
| 1291 |
} |
| 1292 |
|
| 1293 |
out: |
| 1294 |
mutex_unlock(&priv->lock); |
| 1295 |
return ret; |
| 1296 |
} |
| 1297 |
|
| 1298 |
static int csi_enum_frame_size(struct v4l2_subdev *sd, |
| 1299 |
struct v4l2_subdev_state *sd_state, |
| 1300 |
struct v4l2_subdev_frame_size_enum *fse) |
| 1301 |
{ |
| 1302 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1303 |
struct v4l2_rect *crop; |
| 1304 |
int ret = 0; |
| 1305 |
|
| 1306 |
if (fse->pad >= CSI_NUM_PADS || |
| 1307 |
fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3)) |
| 1308 |
return -EINVAL; |
| 1309 |
|
| 1310 |
mutex_lock(&priv->lock); |
| 1311 |
|
| 1312 |
if (fse->pad == CSI_SINK_PAD) { |
| 1313 |
fse->min_width = MIN_W; |
| 1314 |
fse->max_width = MAX_W; |
| 1315 |
fse->min_height = MIN_H; |
| 1316 |
fse->max_height = MAX_H; |
| 1317 |
} else { |
| 1318 |
crop = __csi_get_crop(priv, sd_state, fse->which); |
| 1319 |
|
| 1320 |
fse->min_width = fse->index & 1 ? |
| 1321 |
crop->width / 2 : crop->width; |
| 1322 |
fse->max_width = fse->min_width; |
| 1323 |
fse->min_height = fse->index & 2 ? |
| 1324 |
crop->height / 2 : crop->height; |
| 1325 |
fse->max_height = fse->min_height; |
| 1326 |
} |
| 1327 |
|
| 1328 |
mutex_unlock(&priv->lock); |
| 1329 |
return ret; |
| 1330 |
} |
| 1331 |
|
| 1332 |
static int csi_enum_frame_interval(struct v4l2_subdev *sd, |
| 1333 |
struct v4l2_subdev_state *sd_state, |
| 1334 |
struct v4l2_subdev_frame_interval_enum *fie) |
| 1335 |
{ |
| 1336 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1337 |
struct v4l2_fract *input_fi; |
| 1338 |
struct v4l2_rect *crop; |
| 1339 |
int ret = 0; |
| 1340 |
|
| 1341 |
if (fie->pad >= CSI_NUM_PADS || |
| 1342 |
fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ? |
| 1343 |
1 : ARRAY_SIZE(csi_skip))) |
| 1344 |
return -EINVAL; |
| 1345 |
|
| 1346 |
mutex_lock(&priv->lock); |
| 1347 |
|
| 1348 |
input_fi = &priv->frame_interval[CSI_SINK_PAD]; |
| 1349 |
crop = __csi_get_crop(priv, sd_state, fie->which); |
| 1350 |
|
| 1351 |
if ((fie->width != crop->width && fie->width != crop->width / 2) || |
| 1352 |
(fie->height != crop->height && fie->height != crop->height / 2)) { |
| 1353 |
ret = -EINVAL; |
| 1354 |
goto out; |
| 1355 |
} |
| 1356 |
|
| 1357 |
fie->interval = *input_fi; |
| 1358 |
|
| 1359 |
if (fie->pad == CSI_SRC_PAD_IDMAC) |
| 1360 |
csi_apply_skip_interval(&csi_skip[fie->index], |
| 1361 |
&fie->interval); |
| 1362 |
|
| 1363 |
out: |
| 1364 |
mutex_unlock(&priv->lock); |
| 1365 |
return ret; |
| 1366 |
} |
| 1367 |
|
| 1368 |
static int csi_get_fmt(struct v4l2_subdev *sd, |
| 1369 |
struct v4l2_subdev_state *sd_state, |
| 1370 |
struct v4l2_subdev_format *sdformat) |
| 1371 |
{ |
| 1372 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1373 |
struct v4l2_mbus_framefmt *fmt; |
| 1374 |
int ret = 0; |
| 1375 |
|
| 1376 |
if (sdformat->pad >= CSI_NUM_PADS) |
| 1377 |
return -EINVAL; |
| 1378 |
|
| 1379 |
mutex_lock(&priv->lock); |
| 1380 |
|
| 1381 |
fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which); |
| 1382 |
if (!fmt) { |
| 1383 |
ret = -EINVAL; |
| 1384 |
goto out; |
| 1385 |
} |
| 1386 |
|
| 1387 |
sdformat->format = *fmt; |
| 1388 |
out: |
| 1389 |
mutex_unlock(&priv->lock); |
| 1390 |
return ret; |
| 1391 |
} |
| 1392 |
|
| 1393 |
static void csi_try_field(struct csi_priv *priv, |
| 1394 |
struct v4l2_subdev_state *sd_state, |
| 1395 |
struct v4l2_subdev_format *sdformat) |
| 1396 |
{ |
| 1397 |
struct v4l2_mbus_framefmt *infmt = |
| 1398 |
__csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which); |
| 1399 |
|
| 1400 |
/* |
| 1401 |
* no restrictions on sink pad field type except must |
| 1402 |
* be initialized. |
| 1403 |
*/ |
| 1404 |
if (sdformat->pad == CSI_SINK_PAD) { |
| 1405 |
if (sdformat->format.field == V4L2_FIELD_ANY) |
| 1406 |
sdformat->format.field = V4L2_FIELD_NONE; |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
|
| 1410 |
switch (infmt->field) { |
| 1411 |
case V4L2_FIELD_SEQ_TB: |
| 1412 |
case V4L2_FIELD_SEQ_BT: |
| 1413 |
/* |
| 1414 |
* If the user requests sequential at the source pad, |
| 1415 |
* allow it (along with possibly inverting field order). |
| 1416 |
* Otherwise passthrough the field type. |
| 1417 |
*/ |
| 1418 |
if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) |
| 1419 |
sdformat->format.field = infmt->field; |
| 1420 |
break; |
| 1421 |
case V4L2_FIELD_ALTERNATE: |
| 1422 |
/* |
| 1423 |
* This driver does not support alternate field mode, and |
| 1424 |
* the CSI captures a whole frame, so the CSI never presents |
| 1425 |
* alternate mode at its source pads. If user has not |
| 1426 |
* already requested sequential, translate ALTERNATE at |
| 1427 |
* sink pad to SEQ_TB or SEQ_BT at the source pad depending |
| 1428 |
* on input height (assume NTSC BT order if 480 total active |
| 1429 |
* frame lines, otherwise PAL TB order). |
| 1430 |
*/ |
| 1431 |
if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) |
| 1432 |
sdformat->format.field = (infmt->height == 480 / 2) ? |
| 1433 |
V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB; |
| 1434 |
break; |
| 1435 |
default: |
| 1436 |
/* Passthrough for all other input field types */ |
| 1437 |
sdformat->format.field = infmt->field; |
| 1438 |
break; |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
static void csi_try_fmt(struct csi_priv *priv, |
| 1443 |
struct v4l2_mbus_config *mbus_cfg, |
| 1444 |
struct v4l2_subdev_state *sd_state, |
| 1445 |
struct v4l2_subdev_format *sdformat, |
| 1446 |
struct v4l2_rect *crop, |
| 1447 |
struct v4l2_rect *compose, |
| 1448 |
const struct imx_media_pixfmt **cc) |
| 1449 |
{ |
| 1450 |
const struct imx_media_pixfmt *incc; |
| 1451 |
struct v4l2_mbus_framefmt *infmt; |
| 1452 |
u32 code; |
| 1453 |
|
| 1454 |
infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which); |
| 1455 |
|
| 1456 |
switch (sdformat->pad) { |
| 1457 |
case CSI_SRC_PAD_DIRECT: |
| 1458 |
case CSI_SRC_PAD_IDMAC: |
| 1459 |
incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); |
| 1460 |
|
| 1461 |
sdformat->format.width = compose->width; |
| 1462 |
sdformat->format.height = compose->height; |
| 1463 |
|
| 1464 |
if (requires_passthrough(mbus_cfg, infmt, incc)) { |
| 1465 |
sdformat->format.code = infmt->code; |
| 1466 |
*cc = incc; |
| 1467 |
} else { |
| 1468 |
enum imx_pixfmt_sel fmt_sel = |
| 1469 |
(incc->cs == IPUV3_COLORSPACE_YUV) ? |
| 1470 |
PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; |
| 1471 |
|
| 1472 |
*cc = imx_media_find_ipu_format(sdformat->format.code, |
| 1473 |
fmt_sel); |
| 1474 |
if (!*cc) { |
| 1475 |
imx_media_enum_ipu_formats(&code, 0, fmt_sel); |
| 1476 |
*cc = imx_media_find_ipu_format(code, fmt_sel); |
| 1477 |
sdformat->format.code = (*cc)->codes[0]; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
csi_try_field(priv, sd_state, sdformat); |
| 1482 |
|
| 1483 |
/* propagate colorimetry from sink */ |
| 1484 |
sdformat->format.colorspace = infmt->colorspace; |
| 1485 |
sdformat->format.xfer_func = infmt->xfer_func; |
| 1486 |
sdformat->format.quantization = infmt->quantization; |
| 1487 |
sdformat->format.ycbcr_enc = infmt->ycbcr_enc; |
| 1488 |
|
| 1489 |
break; |
| 1490 |
case CSI_SINK_PAD: |
| 1491 |
v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W, |
| 1492 |
W_ALIGN, &sdformat->format.height, |
| 1493 |
MIN_H, MAX_H, H_ALIGN, S_ALIGN); |
| 1494 |
|
| 1495 |
*cc = imx_media_find_mbus_format(sdformat->format.code, |
| 1496 |
PIXFMT_SEL_ANY); |
| 1497 |
if (!*cc) { |
| 1498 |
imx_media_enum_mbus_formats(&code, 0, |
| 1499 |
PIXFMT_SEL_YUV_RGB); |
| 1500 |
*cc = imx_media_find_mbus_format(code, |
| 1501 |
PIXFMT_SEL_YUV_RGB); |
| 1502 |
sdformat->format.code = (*cc)->codes[0]; |
| 1503 |
} |
| 1504 |
|
| 1505 |
csi_try_field(priv, sd_state, sdformat); |
| 1506 |
|
| 1507 |
/* Reset crop and compose rectangles */ |
| 1508 |
crop->left = 0; |
| 1509 |
crop->top = 0; |
| 1510 |
crop->width = sdformat->format.width; |
| 1511 |
crop->height = sdformat->format.height; |
| 1512 |
if (sdformat->format.field == V4L2_FIELD_ALTERNATE) |
| 1513 |
crop->height *= 2; |
| 1514 |
csi_try_crop(priv, crop, sd_state, &sdformat->format, mbus_cfg); |
| 1515 |
compose->left = 0; |
| 1516 |
compose->top = 0; |
| 1517 |
compose->width = crop->width; |
| 1518 |
compose->height = crop->height; |
| 1519 |
|
| 1520 |
break; |
| 1521 |
} |
| 1522 |
|
| 1523 |
imx_media_try_colorimetry(&sdformat->format, |
| 1524 |
priv->active_output_pad == CSI_SRC_PAD_DIRECT); |
| 1525 |
} |
| 1526 |
|
| 1527 |
static int csi_set_fmt(struct v4l2_subdev *sd, |
| 1528 |
struct v4l2_subdev_state *sd_state, |
| 1529 |
struct v4l2_subdev_format *sdformat) |
| 1530 |
{ |
| 1531 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1532 |
struct v4l2_mbus_config mbus_cfg = { .type = 0 }; |
| 1533 |
const struct imx_media_pixfmt *cc; |
| 1534 |
struct v4l2_mbus_framefmt *fmt; |
| 1535 |
struct v4l2_rect *crop, *compose; |
| 1536 |
int ret; |
| 1537 |
|
| 1538 |
if (sdformat->pad >= CSI_NUM_PADS) |
| 1539 |
return -EINVAL; |
| 1540 |
|
| 1541 |
ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); |
| 1542 |
if (ret) { |
| 1543 |
v4l2_err(&priv->sd, |
| 1544 |
"failed to get upstream media bus configuration\n"); |
| 1545 |
return ret; |
| 1546 |
} |
| 1547 |
|
| 1548 |
mutex_lock(&priv->lock); |
| 1549 |
|
| 1550 |
if (priv->stream_count > 0) { |
| 1551 |
ret = -EBUSY; |
| 1552 |
goto out; |
| 1553 |
} |
| 1554 |
|
| 1555 |
crop = __csi_get_crop(priv, sd_state, sdformat->which); |
| 1556 |
compose = __csi_get_compose(priv, sd_state, sdformat->which); |
| 1557 |
|
| 1558 |
csi_try_fmt(priv, &mbus_cfg, sd_state, sdformat, crop, compose, &cc); |
| 1559 |
|
| 1560 |
fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which); |
| 1561 |
*fmt = sdformat->format; |
| 1562 |
|
| 1563 |
if (sdformat->pad == CSI_SINK_PAD) { |
| 1564 |
int pad; |
| 1565 |
|
| 1566 |
/* propagate format to source pads */ |
| 1567 |
for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { |
| 1568 |
const struct imx_media_pixfmt *outcc; |
| 1569 |
struct v4l2_mbus_framefmt *outfmt; |
| 1570 |
struct v4l2_subdev_format format; |
| 1571 |
|
| 1572 |
format.pad = pad; |
| 1573 |
format.which = sdformat->which; |
| 1574 |
format.format = sdformat->format; |
| 1575 |
csi_try_fmt(priv, &mbus_cfg, sd_state, &format, NULL, |
| 1576 |
compose, &outcc); |
| 1577 |
|
| 1578 |
outfmt = __csi_get_fmt(priv, sd_state, pad, |
| 1579 |
sdformat->which); |
| 1580 |
*outfmt = format.format; |
| 1581 |
|
| 1582 |
if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 1583 |
priv->cc[pad] = outcc; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 1588 |
priv->cc[sdformat->pad] = cc; |
| 1589 |
|
| 1590 |
out: |
| 1591 |
mutex_unlock(&priv->lock); |
| 1592 |
return ret; |
| 1593 |
} |
| 1594 |
|
| 1595 |
static int csi_get_selection(struct v4l2_subdev *sd, |
| 1596 |
struct v4l2_subdev_state *sd_state, |
| 1597 |
struct v4l2_subdev_selection *sel) |
| 1598 |
{ |
| 1599 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1600 |
struct v4l2_mbus_framefmt *infmt; |
| 1601 |
struct v4l2_rect *crop, *compose; |
| 1602 |
int ret = 0; |
| 1603 |
|
| 1604 |
if (sel->pad != CSI_SINK_PAD) |
| 1605 |
return -EINVAL; |
| 1606 |
|
| 1607 |
mutex_lock(&priv->lock); |
| 1608 |
|
| 1609 |
infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which); |
| 1610 |
crop = __csi_get_crop(priv, sd_state, sel->which); |
| 1611 |
compose = __csi_get_compose(priv, sd_state, sel->which); |
| 1612 |
|
| 1613 |
switch (sel->target) { |
| 1614 |
case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1615 |
sel->r.left = 0; |
| 1616 |
sel->r.top = 0; |
| 1617 |
sel->r.width = infmt->width; |
| 1618 |
sel->r.height = infmt->height; |
| 1619 |
if (infmt->field == V4L2_FIELD_ALTERNATE) |
| 1620 |
sel->r.height *= 2; |
| 1621 |
break; |
| 1622 |
case V4L2_SEL_TGT_CROP: |
| 1623 |
sel->r = *crop; |
| 1624 |
break; |
| 1625 |
case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1626 |
sel->r.left = 0; |
| 1627 |
sel->r.top = 0; |
| 1628 |
sel->r.width = crop->width; |
| 1629 |
sel->r.height = crop->height; |
| 1630 |
break; |
| 1631 |
case V4L2_SEL_TGT_COMPOSE: |
| 1632 |
sel->r = *compose; |
| 1633 |
break; |
| 1634 |
default: |
| 1635 |
ret = -EINVAL; |
| 1636 |
} |
| 1637 |
|
| 1638 |
mutex_unlock(&priv->lock); |
| 1639 |
return ret; |
| 1640 |
} |
| 1641 |
|
| 1642 |
static int csi_set_scale(u32 *compose, u32 crop, u32 flags) |
| 1643 |
{ |
| 1644 |
if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) == |
| 1645 |
(V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) && |
| 1646 |
*compose != crop && *compose != crop / 2) |
| 1647 |
return -ERANGE; |
| 1648 |
|
| 1649 |
if (*compose <= crop / 2 || |
| 1650 |
(*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) || |
| 1651 |
(*compose < crop && (flags & V4L2_SEL_FLAG_LE))) |
| 1652 |
*compose = crop / 2; |
| 1653 |
else |
| 1654 |
*compose = crop; |
| 1655 |
|
| 1656 |
return 0; |
| 1657 |
} |
| 1658 |
|
| 1659 |
static int csi_set_selection(struct v4l2_subdev *sd, |
| 1660 |
struct v4l2_subdev_state *sd_state, |
| 1661 |
struct v4l2_subdev_selection *sel) |
| 1662 |
{ |
| 1663 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1664 |
struct v4l2_mbus_config mbus_cfg = { .type = 0 }; |
| 1665 |
struct v4l2_mbus_framefmt *infmt; |
| 1666 |
struct v4l2_rect *crop, *compose; |
| 1667 |
int pad, ret; |
| 1668 |
|
| 1669 |
if (sel->pad != CSI_SINK_PAD) |
| 1670 |
return -EINVAL; |
| 1671 |
|
| 1672 |
ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); |
| 1673 |
if (ret) { |
| 1674 |
v4l2_err(&priv->sd, |
| 1675 |
"failed to get upstream media bus configuration\n"); |
| 1676 |
return ret; |
| 1677 |
} |
| 1678 |
|
| 1679 |
mutex_lock(&priv->lock); |
| 1680 |
|
| 1681 |
if (priv->stream_count > 0) { |
| 1682 |
ret = -EBUSY; |
| 1683 |
goto out; |
| 1684 |
} |
| 1685 |
|
| 1686 |
infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which); |
| 1687 |
crop = __csi_get_crop(priv, sd_state, sel->which); |
| 1688 |
compose = __csi_get_compose(priv, sd_state, sel->which); |
| 1689 |
|
| 1690 |
switch (sel->target) { |
| 1691 |
case V4L2_SEL_TGT_CROP: |
| 1692 |
/* |
| 1693 |
* Modifying the crop rectangle always changes the format on |
| 1694 |
* the source pads. If the KEEP_CONFIG flag is set, just return |
| 1695 |
* the current crop rectangle. |
| 1696 |
*/ |
| 1697 |
if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { |
| 1698 |
sel->r = priv->crop; |
| 1699 |
if (sel->which == V4L2_SUBDEV_FORMAT_TRY) |
| 1700 |
*crop = sel->r; |
| 1701 |
goto out; |
| 1702 |
} |
| 1703 |
|
| 1704 |
csi_try_crop(priv, &sel->r, sd_state, infmt, &mbus_cfg); |
| 1705 |
|
| 1706 |
*crop = sel->r; |
| 1707 |
|
| 1708 |
/* Reset scaling to 1:1 */ |
| 1709 |
compose->width = crop->width; |
| 1710 |
compose->height = crop->height; |
| 1711 |
break; |
| 1712 |
case V4L2_SEL_TGT_COMPOSE: |
| 1713 |
/* |
| 1714 |
* Modifying the compose rectangle always changes the format on |
| 1715 |
* the source pads. If the KEEP_CONFIG flag is set, just return |
| 1716 |
* the current compose rectangle. |
| 1717 |
*/ |
| 1718 |
if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { |
| 1719 |
sel->r = priv->compose; |
| 1720 |
if (sel->which == V4L2_SUBDEV_FORMAT_TRY) |
| 1721 |
*compose = sel->r; |
| 1722 |
goto out; |
| 1723 |
} |
| 1724 |
|
| 1725 |
sel->r.left = 0; |
| 1726 |
sel->r.top = 0; |
| 1727 |
ret = csi_set_scale(&sel->r.width, crop->width, sel->flags); |
| 1728 |
if (ret) |
| 1729 |
goto out; |
| 1730 |
ret = csi_set_scale(&sel->r.height, crop->height, sel->flags); |
| 1731 |
if (ret) |
| 1732 |
goto out; |
| 1733 |
|
| 1734 |
*compose = sel->r; |
| 1735 |
break; |
| 1736 |
default: |
| 1737 |
ret = -EINVAL; |
| 1738 |
goto out; |
| 1739 |
} |
| 1740 |
|
| 1741 |
/* Reset source pads to sink compose rectangle */ |
| 1742 |
for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { |
| 1743 |
struct v4l2_mbus_framefmt *outfmt; |
| 1744 |
|
| 1745 |
outfmt = __csi_get_fmt(priv, sd_state, pad, sel->which); |
| 1746 |
outfmt->width = compose->width; |
| 1747 |
outfmt->height = compose->height; |
| 1748 |
} |
| 1749 |
|
| 1750 |
out: |
| 1751 |
mutex_unlock(&priv->lock); |
| 1752 |
return ret; |
| 1753 |
} |
| 1754 |
|
| 1755 |
static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, |
| 1756 |
struct v4l2_event_subscription *sub) |
| 1757 |
{ |
| 1758 |
if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR) |
| 1759 |
return -EINVAL; |
| 1760 |
if (sub->id != 0) |
| 1761 |
return -EINVAL; |
| 1762 |
|
| 1763 |
return v4l2_event_subscribe(fh, sub, 0, NULL); |
| 1764 |
} |
| 1765 |
|
| 1766 |
static int csi_registered(struct v4l2_subdev *sd) |
| 1767 |
{ |
| 1768 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1769 |
struct ipu_csi *csi; |
| 1770 |
int i, ret; |
| 1771 |
u32 code; |
| 1772 |
|
| 1773 |
/* get handle to IPU CSI */ |
| 1774 |
csi = ipu_csi_get(priv->ipu, priv->csi_id); |
| 1775 |
if (IS_ERR(csi)) { |
| 1776 |
v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); |
| 1777 |
return PTR_ERR(csi); |
| 1778 |
} |
| 1779 |
priv->csi = csi; |
| 1780 |
|
| 1781 |
for (i = 0; i < CSI_NUM_PADS; i++) { |
| 1782 |
code = 0; |
| 1783 |
if (i != CSI_SINK_PAD) |
| 1784 |
imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV); |
| 1785 |
|
| 1786 |
/* set a default mbus format */ |
| 1787 |
ret = imx_media_init_mbus_fmt(&priv->format_mbus[i], |
| 1788 |
IMX_MEDIA_DEF_PIX_WIDTH, |
| 1789 |
IMX_MEDIA_DEF_PIX_HEIGHT, code, |
| 1790 |
V4L2_FIELD_NONE, &priv->cc[i]); |
| 1791 |
if (ret) |
| 1792 |
goto put_csi; |
| 1793 |
|
| 1794 |
/* init default frame interval */ |
| 1795 |
priv->frame_interval[i].numerator = 1; |
| 1796 |
priv->frame_interval[i].denominator = 30; |
| 1797 |
} |
| 1798 |
|
| 1799 |
/* disable frame skipping */ |
| 1800 |
priv->skip = &csi_skip[0]; |
| 1801 |
|
| 1802 |
/* init default crop and compose rectangle sizes */ |
| 1803 |
priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH; |
| 1804 |
priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT; |
| 1805 |
priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH; |
| 1806 |
priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT; |
| 1807 |
|
| 1808 |
priv->fim = imx_media_fim_init(&priv->sd); |
| 1809 |
if (IS_ERR(priv->fim)) { |
| 1810 |
ret = PTR_ERR(priv->fim); |
| 1811 |
goto put_csi; |
| 1812 |
} |
| 1813 |
|
| 1814 |
priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd, |
| 1815 |
CSI_SRC_PAD_IDMAC, true); |
| 1816 |
if (IS_ERR(priv->vdev)) { |
| 1817 |
ret = PTR_ERR(priv->vdev); |
| 1818 |
goto free_fim; |
| 1819 |
} |
| 1820 |
|
| 1821 |
ret = imx_media_capture_device_register(priv->vdev, 0); |
| 1822 |
if (ret) |
| 1823 |
goto remove_vdev; |
| 1824 |
|
| 1825 |
return 0; |
| 1826 |
|
| 1827 |
remove_vdev: |
| 1828 |
imx_media_capture_device_remove(priv->vdev); |
| 1829 |
free_fim: |
| 1830 |
if (priv->fim) |
| 1831 |
imx_media_fim_free(priv->fim); |
| 1832 |
put_csi: |
| 1833 |
ipu_csi_put(priv->csi); |
| 1834 |
return ret; |
| 1835 |
} |
| 1836 |
|
| 1837 |
static void csi_unregistered(struct v4l2_subdev *sd) |
| 1838 |
{ |
| 1839 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1840 |
|
| 1841 |
imx_media_capture_device_unregister(priv->vdev); |
| 1842 |
imx_media_capture_device_remove(priv->vdev); |
| 1843 |
|
| 1844 |
if (priv->fim) |
| 1845 |
imx_media_fim_free(priv->fim); |
| 1846 |
|
| 1847 |
if (priv->csi) |
| 1848 |
ipu_csi_put(priv->csi); |
| 1849 |
} |
| 1850 |
|
| 1851 |
/* |
| 1852 |
* The CSI has only one fwnode endpoint, at the sink pad. Verify the |
| 1853 |
* endpoint belongs to us, and return CSI_SINK_PAD. |
| 1854 |
*/ |
| 1855 |
static int csi_get_fwnode_pad(struct media_entity *entity, |
| 1856 |
struct fwnode_endpoint *endpoint) |
| 1857 |
{ |
| 1858 |
struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
| 1859 |
struct csi_priv *priv = v4l2_get_subdevdata(sd); |
| 1860 |
struct fwnode_handle *csi_port = dev_fwnode(priv->dev); |
| 1861 |
struct fwnode_handle *csi_ep; |
| 1862 |
int ret; |
| 1863 |
|
| 1864 |
csi_ep = fwnode_get_next_child_node(csi_port, NULL); |
| 1865 |
|
| 1866 |
ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO; |
| 1867 |
|
| 1868 |
fwnode_handle_put(csi_ep); |
| 1869 |
|
| 1870 |
return ret; |
| 1871 |
} |
| 1872 |
|
| 1873 |
static const struct media_entity_operations csi_entity_ops = { |
| 1874 |
.link_setup = csi_link_setup, |
| 1875 |
.link_validate = v4l2_subdev_link_validate, |
| 1876 |
.get_fwnode_pad = csi_get_fwnode_pad, |
| 1877 |
}; |
| 1878 |
|
| 1879 |
static const struct v4l2_subdev_core_ops csi_core_ops = { |
| 1880 |
.subscribe_event = csi_subscribe_event, |
| 1881 |
.unsubscribe_event = v4l2_event_subdev_unsubscribe, |
| 1882 |
}; |
| 1883 |
|
| 1884 |
static const struct v4l2_subdev_video_ops csi_video_ops = { |
| 1885 |
.s_stream = csi_s_stream, |
| 1886 |
}; |
| 1887 |
|
| 1888 |
static const struct v4l2_subdev_pad_ops csi_pad_ops = { |
| 1889 |
.enum_mbus_code = csi_enum_mbus_code, |
| 1890 |
.enum_frame_size = csi_enum_frame_size, |
| 1891 |
.enum_frame_interval = csi_enum_frame_interval, |
| 1892 |
.get_fmt = csi_get_fmt, |
| 1893 |
.set_fmt = csi_set_fmt, |
| 1894 |
.get_selection = csi_get_selection, |
| 1895 |
.set_selection = csi_set_selection, |
| 1896 |
.get_frame_interval = csi_get_frame_interval, |
| 1897 |
.set_frame_interval = csi_set_frame_interval, |
| 1898 |
.link_validate = csi_link_validate, |
| 1899 |
}; |
| 1900 |
|
| 1901 |
static const struct v4l2_subdev_ops csi_subdev_ops = { |
| 1902 |
.core = &csi_core_ops, |
| 1903 |
.video = &csi_video_ops, |
| 1904 |
.pad = &csi_pad_ops, |
| 1905 |
}; |
| 1906 |
|
| 1907 |
static const struct v4l2_subdev_internal_ops csi_internal_ops = { |
| 1908 |
.init_state = imx_media_init_state, |
| 1909 |
.registered = csi_registered, |
| 1910 |
.unregistered = csi_unregistered, |
| 1911 |
}; |
| 1912 |
|
| 1913 |
static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier, |
| 1914 |
struct v4l2_subdev *sd, |
| 1915 |
struct v4l2_async_connection *asd) |
| 1916 |
{ |
| 1917 |
struct csi_priv *priv = notifier_to_dev(notifier); |
| 1918 |
struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD]; |
| 1919 |
|
| 1920 |
/* |
| 1921 |
* If the subdev is a video mux, it must be one of the CSI |
| 1922 |
* muxes. Mark it as such via its group id. |
| 1923 |
*/ |
| 1924 |
if (sd->entity.function == MEDIA_ENT_F_VID_MUX) |
| 1925 |
sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX; |
| 1926 |
|
| 1927 |
return v4l2_create_fwnode_links_to_pad(sd, sink, 0); |
| 1928 |
} |
| 1929 |
|
| 1930 |
static const struct v4l2_async_notifier_operations csi_notify_ops = { |
| 1931 |
.bound = imx_csi_notify_bound, |
| 1932 |
}; |
| 1933 |
|
| 1934 |
static int imx_csi_async_register(struct csi_priv *priv) |
| 1935 |
{ |
| 1936 |
struct v4l2_async_connection *asd = NULL; |
| 1937 |
struct fwnode_handle *ep; |
| 1938 |
unsigned int port; |
| 1939 |
int ret; |
| 1940 |
|
| 1941 |
v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd); |
| 1942 |
|
| 1943 |
/* get this CSI's port id */ |
| 1944 |
ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port); |
| 1945 |
if (ret < 0) |
| 1946 |
return ret; |
| 1947 |
|
| 1948 |
ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent), |
| 1949 |
port, 0, |
| 1950 |
FWNODE_GRAPH_ENDPOINT_NEXT); |
| 1951 |
if (ep) { |
| 1952 |
asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep, |
| 1953 |
struct v4l2_async_connection); |
| 1954 |
|
| 1955 |
fwnode_handle_put(ep); |
| 1956 |
|
| 1957 |
if (IS_ERR(asd)) { |
| 1958 |
ret = PTR_ERR(asd); |
| 1959 |
/* OK if asd already exists */ |
| 1960 |
if (ret != -EEXIST) |
| 1961 |
return ret; |
| 1962 |
} |
| 1963 |
} |
| 1964 |
|
| 1965 |
priv->notifier.ops = &csi_notify_ops; |
| 1966 |
|
| 1967 |
ret = v4l2_async_nf_register(&priv->notifier); |
| 1968 |
if (ret) |
| 1969 |
return ret; |
| 1970 |
|
| 1971 |
return v4l2_async_register_subdev(&priv->sd); |
| 1972 |
} |
| 1973 |
|
| 1974 |
static int imx_csi_probe(struct platform_device *pdev) |
| 1975 |
{ |
| 1976 |
struct ipu_client_platformdata *pdata; |
| 1977 |
struct pinctrl *pinctrl; |
| 1978 |
struct csi_priv *priv; |
| 1979 |
int i, ret; |
| 1980 |
|
| 1981 |
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 1982 |
if (!priv) |
| 1983 |
return -ENOMEM; |
| 1984 |
|
| 1985 |
platform_set_drvdata(pdev, &priv->sd); |
| 1986 |
priv->dev = &pdev->dev; |
| 1987 |
|
| 1988 |
ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32)); |
| 1989 |
if (ret) |
| 1990 |
return ret; |
| 1991 |
|
| 1992 |
/* get parent IPU */ |
| 1993 |
priv->ipu = dev_get_drvdata(priv->dev->parent); |
| 1994 |
|
| 1995 |
/* get our CSI id */ |
| 1996 |
pdata = priv->dev->platform_data; |
| 1997 |
priv->csi_id = pdata->csi; |
| 1998 |
priv->smfc_id = (priv->csi_id == 0) ? 0 : 2; |
| 1999 |
|
| 2000 |
priv->active_output_pad = CSI_SRC_PAD_IDMAC; |
| 2001 |
|
| 2002 |
timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0); |
| 2003 |
spin_lock_init(&priv->irqlock); |
| 2004 |
|
| 2005 |
v4l2_subdev_init(&priv->sd, &csi_subdev_ops); |
| 2006 |
v4l2_set_subdevdata(&priv->sd, priv); |
| 2007 |
priv->sd.internal_ops = &csi_internal_ops; |
| 2008 |
priv->sd.entity.ops = &csi_entity_ops; |
| 2009 |
priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; |
| 2010 |
priv->sd.dev = &pdev->dev; |
| 2011 |
priv->sd.fwnode = of_fwnode_handle(pdata->of_node); |
| 2012 |
priv->sd.owner = THIS_MODULE; |
| 2013 |
priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; |
| 2014 |
priv->sd.grp_id = priv->csi_id ? |
| 2015 |
IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0; |
| 2016 |
imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name), |
| 2017 |
priv->sd.grp_id, ipu_get_num(priv->ipu)); |
| 2018 |
|
| 2019 |
for (i = 0; i < CSI_NUM_PADS; i++) |
| 2020 |
priv->pad[i].flags = (i == CSI_SINK_PAD) ? |
| 2021 |
MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; |
| 2022 |
|
| 2023 |
ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS, |
| 2024 |
priv->pad); |
| 2025 |
if (ret) |
| 2026 |
return ret; |
| 2027 |
|
| 2028 |
mutex_init(&priv->lock); |
| 2029 |
|
| 2030 |
v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); |
| 2031 |
priv->sd.ctrl_handler = &priv->ctrl_hdlr; |
| 2032 |
|
| 2033 |
/* |
| 2034 |
* The IPUv3 driver did not assign an of_node to this |
| 2035 |
* device. As a result, pinctrl does not automatically |
| 2036 |
* configure our pin groups, so we need to do that manually |
| 2037 |
* here, after setting this device's of_node. |
| 2038 |
*/ |
| 2039 |
priv->dev->of_node = pdata->of_node; |
| 2040 |
pinctrl = devm_pinctrl_get_select_default(priv->dev); |
| 2041 |
if (IS_ERR(pinctrl)) { |
| 2042 |
ret = PTR_ERR(pinctrl); |
| 2043 |
dev_dbg(priv->dev, |
| 2044 |
"devm_pinctrl_get_select_default() failed: %d\n", ret); |
| 2045 |
if (ret != -ENODEV) |
| 2046 |
goto free; |
| 2047 |
} |
| 2048 |
|
| 2049 |
ret = imx_csi_async_register(priv); |
| 2050 |
if (ret) |
| 2051 |
goto cleanup; |
| 2052 |
|
| 2053 |
return 0; |
| 2054 |
|
| 2055 |
cleanup: |
| 2056 |
v4l2_async_nf_unregister(&priv->notifier); |
| 2057 |
v4l2_async_nf_cleanup(&priv->notifier); |
| 2058 |
free: |
| 2059 |
v4l2_ctrl_handler_free(&priv->ctrl_hdlr); |
| 2060 |
mutex_destroy(&priv->lock); |
| 2061 |
return ret; |
| 2062 |
} |
| 2063 |
|
| 2064 |
static void imx_csi_remove(struct platform_device *pdev) |
| 2065 |
{ |
| 2066 |
struct v4l2_subdev *sd = platform_get_drvdata(pdev); |
| 2067 |
struct csi_priv *priv = sd_to_dev(sd); |
| 2068 |
|
| 2069 |
v4l2_ctrl_handler_free(&priv->ctrl_hdlr); |
| 2070 |
mutex_destroy(&priv->lock); |
| 2071 |
v4l2_async_nf_unregister(&priv->notifier); |
| 2072 |
v4l2_async_nf_cleanup(&priv->notifier); |
| 2073 |
v4l2_async_unregister_subdev(sd); |
| 2074 |
media_entity_cleanup(&sd->entity); |
| 2075 |
} |
| 2076 |
|
| 2077 |
static const struct platform_device_id imx_csi_ids[] = { |
| 2078 |
{ .name = "imx-ipuv3-csi" }, |
| 2079 |
{ }, |
| 2080 |
}; |
| 2081 |
MODULE_DEVICE_TABLE(platform, imx_csi_ids); |
| 2082 |
|
| 2083 |
static struct platform_driver imx_csi_driver = { |
| 2084 |
.probe = imx_csi_probe, |
| 2085 |
.remove = imx_csi_remove, |
| 2086 |
.id_table = imx_csi_ids, |
| 2087 |
.driver = { |
| 2088 |
.name = "imx-ipuv3-csi", |
| 2089 |
}, |
| 2090 |
}; |
| 2091 |
module_platform_driver(imx_csi_driver); |
| 2092 |
|
| 2093 |
MODULE_DESCRIPTION("i.MX CSI subdev driver"); |
| 2094 |
MODULE_AUTHOR("Steve Longerbeam <[email protected]>"); |
| 2095 |
MODULE_LICENSE("GPL"); |