2 * TI OMAP4 ISS V4L2 Driver - ISP IPIPE module
4 * Copyright (C) 2012 Texas Instruments, Inc.
6 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
20 #include <linux/sched.h>
24 #include "iss_ipipe.h"
26 static struct v4l2_mbus_framefmt *
27 __ipipe_get_format(struct iss_ipipe_device *ipipe,
28 struct v4l2_subdev_pad_config *cfg,
30 enum v4l2_subdev_format_whence which);
32 static const unsigned int ipipe_fmts[] = {
33 MEDIA_BUS_FMT_SGRBG10_1X10,
34 MEDIA_BUS_FMT_SRGGB10_1X10,
35 MEDIA_BUS_FMT_SBGGR10_1X10,
36 MEDIA_BUS_FMT_SGBRG10_1X10,
40 * ipipe_print_status - Print current IPIPE Module register values.
41 * @ipipe: Pointer to ISS ISP IPIPE device.
43 * Also prints other debug information stored in the IPIPE module.
45 #define IPIPE_PRINT_REGISTER(iss, name)\
46 dev_dbg(iss->dev, "###IPIPE " #name "=0x%08x\n", \
47 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_##name))
49 static void ipipe_print_status(struct iss_ipipe_device *ipipe)
51 struct iss_device *iss = to_iss_device(ipipe);
53 dev_dbg(iss->dev, "-------------IPIPE Register dump-------------\n");
55 IPIPE_PRINT_REGISTER(iss, SRC_EN);
56 IPIPE_PRINT_REGISTER(iss, SRC_MODE);
57 IPIPE_PRINT_REGISTER(iss, SRC_FMT);
58 IPIPE_PRINT_REGISTER(iss, SRC_COL);
59 IPIPE_PRINT_REGISTER(iss, SRC_VPS);
60 IPIPE_PRINT_REGISTER(iss, SRC_VSZ);
61 IPIPE_PRINT_REGISTER(iss, SRC_HPS);
62 IPIPE_PRINT_REGISTER(iss, SRC_HSZ);
63 IPIPE_PRINT_REGISTER(iss, GCK_MMR);
64 IPIPE_PRINT_REGISTER(iss, YUV_PHS);
66 dev_dbg(iss->dev, "-----------------------------------------------\n");
70 * ipipe_enable - Enable/Disable IPIPE.
71 * @enable: enable flag
74 static void ipipe_enable(struct iss_ipipe_device *ipipe, u8 enable)
76 struct iss_device *iss = to_iss_device(ipipe);
78 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_EN,
79 IPIPE_SRC_EN_EN, enable ? IPIPE_SRC_EN_EN : 0);
82 /* -----------------------------------------------------------------------------
83 * Format- and pipeline-related configuration helpers
86 static void ipipe_configure(struct iss_ipipe_device *ipipe)
88 struct iss_device *iss = to_iss_device(ipipe);
89 struct v4l2_mbus_framefmt *format;
92 format = &ipipe->formats[IPIPE_PAD_SINK];
94 /* NOTE: Currently just supporting pipeline IN: RGB, OUT: YUV422 */
95 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_FMT,
96 IPIPE_SRC_FMT_RAW2YUV);
98 /* Enable YUV444 -> YUV422 conversion */
99 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_YUV_PHS,
102 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VPS, 0);
103 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HPS, 0);
104 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VSZ,
105 (format->height - 2) & IPIPE_SRC_VSZ_MASK);
106 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HSZ,
107 (format->width - 1) & IPIPE_SRC_HSZ_MASK);
109 /* Ignore ipipeif_wrt signal, and operate on-the-fly. */
110 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_MODE,
111 IPIPE_SRC_MODE_WRT | IPIPE_SRC_MODE_OST);
113 /* HACK: Values tuned for Ducati SW (OV) */
114 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_COL,
115 IPIPE_SRC_COL_EE_B | IPIPE_SRC_COL_EO_GB |
116 IPIPE_SRC_COL_OE_GR | IPIPE_SRC_COL_OO_R);
118 /* IPIPE_PAD_SOURCE_VP */
119 format = &ipipe->formats[IPIPE_PAD_SOURCE_VP];
123 /* -----------------------------------------------------------------------------
124 * V4L2 subdev operations
128 * ipipe_set_stream - Enable/Disable streaming on the IPIPE module
129 * @sd: ISP IPIPE V4L2 subdevice
130 * @enable: Enable/disable stream
132 static int ipipe_set_stream(struct v4l2_subdev *sd, int enable)
134 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
135 struct iss_device *iss = to_iss_device(ipipe);
138 if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED) {
139 if (enable == ISS_PIPELINE_STREAM_STOPPED)
142 omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
144 /* Enable clk_arm_g0 */
145 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_MMR,
148 /* Enable clk_pix_g[3:0] */
149 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_PIX,
150 IPIPE_GCK_PIX_G3 | IPIPE_GCK_PIX_G2 |
151 IPIPE_GCK_PIX_G1 | IPIPE_GCK_PIX_G0);
155 case ISS_PIPELINE_STREAM_CONTINUOUS:
157 ipipe_configure(ipipe);
158 ipipe_print_status(ipipe);
160 atomic_set(&ipipe->stopping, 0);
161 ipipe_enable(ipipe, 1);
164 case ISS_PIPELINE_STREAM_STOPPED:
165 if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED)
167 if (omap4iss_module_sync_idle(&sd->entity, &ipipe->wait,
171 ipipe_enable(ipipe, 0);
172 omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
176 ipipe->state = enable;
180 static struct v4l2_mbus_framefmt *
181 __ipipe_get_format(struct iss_ipipe_device *ipipe,
182 struct v4l2_subdev_pad_config *cfg,
184 enum v4l2_subdev_format_whence which)
186 if (which == V4L2_SUBDEV_FORMAT_TRY)
187 return v4l2_subdev_get_try_format(&ipipe->subdev, cfg, pad);
189 return &ipipe->formats[pad];
193 * ipipe_try_format - Try video format on a pad
194 * @ipipe: ISS IPIPE device
195 * @cfg: V4L2 subdev pad config
200 ipipe_try_format(struct iss_ipipe_device *ipipe,
201 struct v4l2_subdev_pad_config *cfg,
203 struct v4l2_mbus_framefmt *fmt,
204 enum v4l2_subdev_format_whence which)
206 struct v4l2_mbus_framefmt *format;
207 unsigned int width = fmt->width;
208 unsigned int height = fmt->height;
213 for (i = 0; i < ARRAY_SIZE(ipipe_fmts); i++) {
214 if (fmt->code == ipipe_fmts[i])
218 /* If not found, use SGRBG10 as default */
219 if (i >= ARRAY_SIZE(ipipe_fmts))
220 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
222 /* Clamp the input size. */
223 fmt->width = clamp_t(u32, width, 1, 8192);
224 fmt->height = clamp_t(u32, height, 1, 8192);
225 fmt->colorspace = V4L2_COLORSPACE_SRGB;
228 case IPIPE_PAD_SOURCE_VP:
229 format = __ipipe_get_format(ipipe, cfg, IPIPE_PAD_SINK, which);
230 memcpy(fmt, format, sizeof(*fmt));
232 fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
233 fmt->width = clamp_t(u32, width, 32, fmt->width);
234 fmt->height = clamp_t(u32, height, 32, fmt->height);
235 fmt->colorspace = V4L2_COLORSPACE_JPEG;
239 fmt->field = V4L2_FIELD_NONE;
243 * ipipe_enum_mbus_code - Handle pixel format enumeration
244 * @sd : pointer to v4l2 subdev structure
245 * @cfg : V4L2 subdev pad config
246 * @code : pointer to v4l2_subdev_mbus_code_enum structure
247 * return -EINVAL or zero on success
249 static int ipipe_enum_mbus_code(struct v4l2_subdev *sd,
250 struct v4l2_subdev_pad_config *cfg,
251 struct v4l2_subdev_mbus_code_enum *code)
255 if (code->index >= ARRAY_SIZE(ipipe_fmts))
258 code->code = ipipe_fmts[code->index];
261 case IPIPE_PAD_SOURCE_VP:
262 /* FIXME: Forced format conversion inside IPIPE ? */
263 if (code->index != 0)
266 code->code = MEDIA_BUS_FMT_UYVY8_1X16;
276 static int ipipe_enum_frame_size(struct v4l2_subdev *sd,
277 struct v4l2_subdev_pad_config *cfg,
278 struct v4l2_subdev_frame_size_enum *fse)
280 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
281 struct v4l2_mbus_framefmt format;
286 format.code = fse->code;
289 ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
290 fse->min_width = format.width;
291 fse->min_height = format.height;
293 if (format.code != fse->code)
296 format.code = fse->code;
299 ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
300 fse->max_width = format.width;
301 fse->max_height = format.height;
307 * ipipe_get_format - Retrieve the video format on a pad
308 * @sd : ISP IPIPE V4L2 subdevice
309 * @cfg: V4L2 subdev pad config
312 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
313 * to the format type.
315 static int ipipe_get_format(struct v4l2_subdev *sd,
316 struct v4l2_subdev_pad_config *cfg,
317 struct v4l2_subdev_format *fmt)
319 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
320 struct v4l2_mbus_framefmt *format;
322 format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
326 fmt->format = *format;
331 * ipipe_set_format - Set the video format on a pad
332 * @sd : ISP IPIPE V4L2 subdevice
333 * @cfg: V4L2 subdev pad config
336 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
337 * to the format type.
339 static int ipipe_set_format(struct v4l2_subdev *sd,
340 struct v4l2_subdev_pad_config *cfg,
341 struct v4l2_subdev_format *fmt)
343 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
344 struct v4l2_mbus_framefmt *format;
346 format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
350 ipipe_try_format(ipipe, cfg, fmt->pad, &fmt->format, fmt->which);
351 *format = fmt->format;
353 /* Propagate the format from sink to source */
354 if (fmt->pad == IPIPE_PAD_SINK) {
355 format = __ipipe_get_format(ipipe, cfg, IPIPE_PAD_SOURCE_VP,
357 *format = fmt->format;
358 ipipe_try_format(ipipe, cfg, IPIPE_PAD_SOURCE_VP, format,
365 static int ipipe_link_validate(struct v4l2_subdev *sd, struct media_link *link,
366 struct v4l2_subdev_format *source_fmt,
367 struct v4l2_subdev_format *sink_fmt)
369 /* Check if the two ends match */
370 if (source_fmt->format.width != sink_fmt->format.width ||
371 source_fmt->format.height != sink_fmt->format.height)
374 if (source_fmt->format.code != sink_fmt->format.code)
381 * ipipe_init_formats - Initialize formats on all pads
382 * @sd: ISP IPIPE V4L2 subdevice
383 * @fh: V4L2 subdev file handle
385 * Initialize all pad formats with default values. If fh is not NULL, try
386 * formats are initialized on the file handle. Otherwise active formats are
387 * initialized on the device.
389 static int ipipe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
391 struct v4l2_subdev_format format;
393 memset(&format, 0, sizeof(format));
394 format.pad = IPIPE_PAD_SINK;
395 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
396 format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
397 format.format.width = 4096;
398 format.format.height = 4096;
399 ipipe_set_format(sd, fh ? fh->pad : NULL, &format);
404 /* V4L2 subdev video operations */
405 static const struct v4l2_subdev_video_ops ipipe_v4l2_video_ops = {
406 .s_stream = ipipe_set_stream,
409 /* V4L2 subdev pad operations */
410 static const struct v4l2_subdev_pad_ops ipipe_v4l2_pad_ops = {
411 .enum_mbus_code = ipipe_enum_mbus_code,
412 .enum_frame_size = ipipe_enum_frame_size,
413 .get_fmt = ipipe_get_format,
414 .set_fmt = ipipe_set_format,
415 .link_validate = ipipe_link_validate,
418 /* V4L2 subdev operations */
419 static const struct v4l2_subdev_ops ipipe_v4l2_ops = {
420 .video = &ipipe_v4l2_video_ops,
421 .pad = &ipipe_v4l2_pad_ops,
424 /* V4L2 subdev internal operations */
425 static const struct v4l2_subdev_internal_ops ipipe_v4l2_internal_ops = {
426 .open = ipipe_init_formats,
429 /* -----------------------------------------------------------------------------
430 * Media entity operations
434 * ipipe_link_setup - Setup IPIPE connections
435 * @entity: IPIPE media entity
436 * @local: Pad at the local end of the link
437 * @remote: Pad at the remote end of the link
440 * return -EINVAL or zero on success
442 static int ipipe_link_setup(struct media_entity *entity,
443 const struct media_pad *local,
444 const struct media_pad *remote, u32 flags)
446 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
447 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
448 struct iss_device *iss = to_iss_device(ipipe);
450 if (!is_media_entity_v4l2_subdev(remote->entity))
453 switch (local->index) {
455 /* Read from IPIPEIF. */
456 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
457 ipipe->input = IPIPE_INPUT_NONE;
461 if (ipipe->input != IPIPE_INPUT_NONE)
464 if (remote->entity == &iss->ipipeif.subdev.entity)
465 ipipe->input = IPIPE_INPUT_IPIPEIF;
469 case IPIPE_PAD_SOURCE_VP:
470 /* Send to RESIZER */
471 if (flags & MEDIA_LNK_FL_ENABLED) {
472 if (ipipe->output & ~IPIPE_OUTPUT_VP)
474 ipipe->output |= IPIPE_OUTPUT_VP;
476 ipipe->output &= ~IPIPE_OUTPUT_VP;
487 /* media operations */
488 static const struct media_entity_operations ipipe_media_ops = {
489 .link_setup = ipipe_link_setup,
490 .link_validate = v4l2_subdev_link_validate,
494 * ipipe_init_entities - Initialize V4L2 subdev and media entity
495 * @ipipe: ISS ISP IPIPE module
497 * Return 0 on success and a negative error code on failure.
499 static int ipipe_init_entities(struct iss_ipipe_device *ipipe)
501 struct v4l2_subdev *sd = &ipipe->subdev;
502 struct media_pad *pads = ipipe->pads;
503 struct media_entity *me = &sd->entity;
506 ipipe->input = IPIPE_INPUT_NONE;
508 v4l2_subdev_init(sd, &ipipe_v4l2_ops);
509 sd->internal_ops = &ipipe_v4l2_internal_ops;
510 strlcpy(sd->name, "OMAP4 ISS ISP IPIPE", sizeof(sd->name));
511 sd->grp_id = 1 << 16; /* group ID for iss subdevs */
512 v4l2_set_subdevdata(sd, ipipe);
513 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
515 pads[IPIPE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
516 pads[IPIPE_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
518 me->ops = &ipipe_media_ops;
519 ret = media_entity_pads_init(me, IPIPE_PADS_NUM, pads);
523 ipipe_init_formats(sd, NULL);
528 void omap4iss_ipipe_unregister_entities(struct iss_ipipe_device *ipipe)
530 v4l2_device_unregister_subdev(&ipipe->subdev);
533 int omap4iss_ipipe_register_entities(struct iss_ipipe_device *ipipe,
534 struct v4l2_device *vdev)
538 /* Register the subdev and video node. */
539 ret = v4l2_device_register_subdev(vdev, &ipipe->subdev);
546 omap4iss_ipipe_unregister_entities(ipipe);
550 /* -----------------------------------------------------------------------------
551 * ISP IPIPE initialisation and cleanup
555 * omap4iss_ipipe_init - IPIPE module initialization.
556 * @iss: Device pointer specific to the OMAP4 ISS.
558 * TODO: Get the initialisation values from platform data.
560 * Return 0 on success or a negative error code otherwise.
562 int omap4iss_ipipe_init(struct iss_device *iss)
564 struct iss_ipipe_device *ipipe = &iss->ipipe;
566 ipipe->state = ISS_PIPELINE_STREAM_STOPPED;
567 init_waitqueue_head(&ipipe->wait);
569 return ipipe_init_entities(ipipe);
573 * omap4iss_ipipe_cleanup - IPIPE module cleanup.
574 * @iss: Device pointer specific to the OMAP4 ISS.
576 void omap4iss_ipipe_cleanup(struct iss_device *iss)
578 struct iss_ipipe_device *ipipe = &iss->ipipe;
580 media_entity_cleanup(&ipipe->subdev.entity);