These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / media / omap4iss / iss_ipipe.c
1 /*
2  * TI OMAP4 ISS V4L2 Driver - ISP IPIPE module
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
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.
12  */
13
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>
19 #include <linux/mm.h>
20 #include <linux/sched.h>
21
22 #include "iss.h"
23 #include "iss_regs.h"
24 #include "iss_ipipe.h"
25
26 static struct v4l2_mbus_framefmt *
27 __ipipe_get_format(struct iss_ipipe_device *ipipe,
28                    struct v4l2_subdev_pad_config *cfg,
29                    unsigned int pad,
30                    enum v4l2_subdev_format_whence which);
31
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,
37 };
38
39 /*
40  * ipipe_print_status - Print current IPIPE Module register values.
41  * @ipipe: Pointer to ISS ISP IPIPE device.
42  *
43  * Also prints other debug information stored in the IPIPE module.
44  */
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))
48
49 static void ipipe_print_status(struct iss_ipipe_device *ipipe)
50 {
51         struct iss_device *iss = to_iss_device(ipipe);
52
53         dev_dbg(iss->dev, "-------------IPIPE Register dump-------------\n");
54
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);
65
66         dev_dbg(iss->dev, "-----------------------------------------------\n");
67 }
68
69 /*
70  * ipipe_enable - Enable/Disable IPIPE.
71  * @enable: enable flag
72  *
73  */
74 static void ipipe_enable(struct iss_ipipe_device *ipipe, u8 enable)
75 {
76         struct iss_device *iss = to_iss_device(ipipe);
77
78         iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_EN,
79                        IPIPE_SRC_EN_EN, enable ? IPIPE_SRC_EN_EN : 0);
80 }
81
82 /* -----------------------------------------------------------------------------
83  * Format- and pipeline-related configuration helpers
84  */
85
86 static void ipipe_configure(struct iss_ipipe_device *ipipe)
87 {
88         struct iss_device *iss = to_iss_device(ipipe);
89         struct v4l2_mbus_framefmt *format;
90
91         /* IPIPE_PAD_SINK */
92         format = &ipipe->formats[IPIPE_PAD_SINK];
93
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);
97
98         /* Enable YUV444 -> YUV422 conversion */
99         iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_YUV_PHS,
100                       IPIPE_YUV_PHS_LPF);
101
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);
108
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);
112
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);
117
118         /* IPIPE_PAD_SOURCE_VP */
119         format = &ipipe->formats[IPIPE_PAD_SOURCE_VP];
120         /* Do nothing? */
121 }
122
123 /* -----------------------------------------------------------------------------
124  * V4L2 subdev operations
125  */
126
127 /*
128  * ipipe_set_stream - Enable/Disable streaming on the IPIPE module
129  * @sd: ISP IPIPE V4L2 subdevice
130  * @enable: Enable/disable stream
131  */
132 static int ipipe_set_stream(struct v4l2_subdev *sd, int enable)
133 {
134         struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
135         struct iss_device *iss = to_iss_device(ipipe);
136         int ret = 0;
137
138         if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED) {
139                 if (enable == ISS_PIPELINE_STREAM_STOPPED)
140                         return 0;
141
142                 omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
143
144                 /* Enable clk_arm_g0 */
145                 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_MMR,
146                               IPIPE_GCK_MMR_REG);
147
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);
152         }
153
154         switch (enable) {
155         case ISS_PIPELINE_STREAM_CONTINUOUS:
156
157                 ipipe_configure(ipipe);
158                 ipipe_print_status(ipipe);
159
160                 atomic_set(&ipipe->stopping, 0);
161                 ipipe_enable(ipipe, 1);
162                 break;
163
164         case ISS_PIPELINE_STREAM_STOPPED:
165                 if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED)
166                         return 0;
167                 if (omap4iss_module_sync_idle(&sd->entity, &ipipe->wait,
168                                               &ipipe->stopping))
169                         ret = -ETIMEDOUT;
170
171                 ipipe_enable(ipipe, 0);
172                 omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
173                 break;
174         }
175
176         ipipe->state = enable;
177         return ret;
178 }
179
180 static struct v4l2_mbus_framefmt *
181 __ipipe_get_format(struct iss_ipipe_device *ipipe,
182                    struct v4l2_subdev_pad_config *cfg,
183                    unsigned int pad,
184                    enum v4l2_subdev_format_whence which)
185 {
186         if (which == V4L2_SUBDEV_FORMAT_TRY)
187                 return v4l2_subdev_get_try_format(&ipipe->subdev, cfg, pad);
188
189         return &ipipe->formats[pad];
190 }
191
192 /*
193  * ipipe_try_format - Try video format on a pad
194  * @ipipe: ISS IPIPE device
195  * @cfg: V4L2 subdev pad config
196  * @pad: Pad number
197  * @fmt: Format
198  */
199 static void
200 ipipe_try_format(struct iss_ipipe_device *ipipe,
201                  struct v4l2_subdev_pad_config *cfg,
202                  unsigned int pad,
203                  struct v4l2_mbus_framefmt *fmt,
204                  enum v4l2_subdev_format_whence which)
205 {
206         struct v4l2_mbus_framefmt *format;
207         unsigned int width = fmt->width;
208         unsigned int height = fmt->height;
209         unsigned int i;
210
211         switch (pad) {
212         case IPIPE_PAD_SINK:
213                 for (i = 0; i < ARRAY_SIZE(ipipe_fmts); i++) {
214                         if (fmt->code == ipipe_fmts[i])
215                                 break;
216                 }
217
218                 /* If not found, use SGRBG10 as default */
219                 if (i >= ARRAY_SIZE(ipipe_fmts))
220                         fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
221
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;
226                 break;
227
228         case IPIPE_PAD_SOURCE_VP:
229                 format = __ipipe_get_format(ipipe, cfg, IPIPE_PAD_SINK, which);
230                 memcpy(fmt, format, sizeof(*fmt));
231
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;
236                 break;
237         }
238
239         fmt->field = V4L2_FIELD_NONE;
240 }
241
242 /*
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
248  */
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)
252 {
253         switch (code->pad) {
254         case IPIPE_PAD_SINK:
255                 if (code->index >= ARRAY_SIZE(ipipe_fmts))
256                         return -EINVAL;
257
258                 code->code = ipipe_fmts[code->index];
259                 break;
260
261         case IPIPE_PAD_SOURCE_VP:
262                 /* FIXME: Forced format conversion inside IPIPE ? */
263                 if (code->index != 0)
264                         return -EINVAL;
265
266                 code->code = MEDIA_BUS_FMT_UYVY8_1X16;
267                 break;
268
269         default:
270                 return -EINVAL;
271         }
272
273         return 0;
274 }
275
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)
279 {
280         struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
281         struct v4l2_mbus_framefmt format;
282
283         if (fse->index != 0)
284                 return -EINVAL;
285
286         format.code = fse->code;
287         format.width = 1;
288         format.height = 1;
289         ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
290         fse->min_width = format.width;
291         fse->min_height = format.height;
292
293         if (format.code != fse->code)
294                 return -EINVAL;
295
296         format.code = fse->code;
297         format.width = -1;
298         format.height = -1;
299         ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
300         fse->max_width = format.width;
301         fse->max_height = format.height;
302
303         return 0;
304 }
305
306 /*
307  * ipipe_get_format - Retrieve the video format on a pad
308  * @sd : ISP IPIPE V4L2 subdevice
309  * @cfg: V4L2 subdev pad config
310  * @fmt: Format
311  *
312  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
313  * to the format type.
314  */
315 static int ipipe_get_format(struct v4l2_subdev *sd,
316                             struct v4l2_subdev_pad_config *cfg,
317                             struct v4l2_subdev_format *fmt)
318 {
319         struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
320         struct v4l2_mbus_framefmt *format;
321
322         format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
323         if (!format)
324                 return -EINVAL;
325
326         fmt->format = *format;
327         return 0;
328 }
329
330 /*
331  * ipipe_set_format - Set the video format on a pad
332  * @sd : ISP IPIPE V4L2 subdevice
333  * @cfg: V4L2 subdev pad config
334  * @fmt: Format
335  *
336  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
337  * to the format type.
338  */
339 static int ipipe_set_format(struct v4l2_subdev *sd,
340                             struct v4l2_subdev_pad_config *cfg,
341                             struct v4l2_subdev_format *fmt)
342 {
343         struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
344         struct v4l2_mbus_framefmt *format;
345
346         format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
347         if (!format)
348                 return -EINVAL;
349
350         ipipe_try_format(ipipe, cfg, fmt->pad, &fmt->format, fmt->which);
351         *format = fmt->format;
352
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,
356                                             fmt->which);
357                 *format = fmt->format;
358                 ipipe_try_format(ipipe, cfg, IPIPE_PAD_SOURCE_VP, format,
359                                  fmt->which);
360         }
361
362         return 0;
363 }
364
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)
368 {
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)
372                 return -EPIPE;
373
374         if (source_fmt->format.code != sink_fmt->format.code)
375                 return -EPIPE;
376
377         return 0;
378 }
379
380 /*
381  * ipipe_init_formats - Initialize formats on all pads
382  * @sd: ISP IPIPE V4L2 subdevice
383  * @fh: V4L2 subdev file handle
384  *
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.
388  */
389 static int ipipe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
390 {
391         struct v4l2_subdev_format format;
392
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);
400
401         return 0;
402 }
403
404 /* V4L2 subdev video operations */
405 static const struct v4l2_subdev_video_ops ipipe_v4l2_video_ops = {
406         .s_stream = ipipe_set_stream,
407 };
408
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,
416 };
417
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,
422 };
423
424 /* V4L2 subdev internal operations */
425 static const struct v4l2_subdev_internal_ops ipipe_v4l2_internal_ops = {
426         .open = ipipe_init_formats,
427 };
428
429 /* -----------------------------------------------------------------------------
430  * Media entity operations
431  */
432
433 /*
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
438  * @flags: Link flags
439  *
440  * return -EINVAL or zero on success
441  */
442 static int ipipe_link_setup(struct media_entity *entity,
443                             const struct media_pad *local,
444                             const struct media_pad *remote, u32 flags)
445 {
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);
449
450         switch (local->index | media_entity_type(remote->entity)) {
451         case IPIPE_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
452                 /* Read from IPIPEIF. */
453                 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
454                         ipipe->input = IPIPE_INPUT_NONE;
455                         break;
456                 }
457
458                 if (ipipe->input != IPIPE_INPUT_NONE)
459                         return -EBUSY;
460
461                 if (remote->entity == &iss->ipipeif.subdev.entity)
462                         ipipe->input = IPIPE_INPUT_IPIPEIF;
463
464                 break;
465
466         case IPIPE_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
467                 /* Send to RESIZER */
468                 if (flags & MEDIA_LNK_FL_ENABLED) {
469                         if (ipipe->output & ~IPIPE_OUTPUT_VP)
470                                 return -EBUSY;
471                         ipipe->output |= IPIPE_OUTPUT_VP;
472                 } else {
473                         ipipe->output &= ~IPIPE_OUTPUT_VP;
474                 }
475                 break;
476
477         default:
478                 return -EINVAL;
479         }
480
481         return 0;
482 }
483
484 /* media operations */
485 static const struct media_entity_operations ipipe_media_ops = {
486         .link_setup = ipipe_link_setup,
487         .link_validate = v4l2_subdev_link_validate,
488 };
489
490 /*
491  * ipipe_init_entities - Initialize V4L2 subdev and media entity
492  * @ipipe: ISS ISP IPIPE module
493  *
494  * Return 0 on success and a negative error code on failure.
495  */
496 static int ipipe_init_entities(struct iss_ipipe_device *ipipe)
497 {
498         struct v4l2_subdev *sd = &ipipe->subdev;
499         struct media_pad *pads = ipipe->pads;
500         struct media_entity *me = &sd->entity;
501         int ret;
502
503         ipipe->input = IPIPE_INPUT_NONE;
504
505         v4l2_subdev_init(sd, &ipipe_v4l2_ops);
506         sd->internal_ops = &ipipe_v4l2_internal_ops;
507         strlcpy(sd->name, "OMAP4 ISS ISP IPIPE", sizeof(sd->name));
508         sd->grp_id = 1 << 16;   /* group ID for iss subdevs */
509         v4l2_set_subdevdata(sd, ipipe);
510         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
511
512         pads[IPIPE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
513         pads[IPIPE_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
514
515         me->ops = &ipipe_media_ops;
516         ret = media_entity_init(me, IPIPE_PADS_NUM, pads, 0);
517         if (ret < 0)
518                 return ret;
519
520         ipipe_init_formats(sd, NULL);
521
522         return 0;
523 }
524
525 void omap4iss_ipipe_unregister_entities(struct iss_ipipe_device *ipipe)
526 {
527         v4l2_device_unregister_subdev(&ipipe->subdev);
528 }
529
530 int omap4iss_ipipe_register_entities(struct iss_ipipe_device *ipipe,
531                                      struct v4l2_device *vdev)
532 {
533         int ret;
534
535         /* Register the subdev and video node. */
536         ret = v4l2_device_register_subdev(vdev, &ipipe->subdev);
537         if (ret < 0)
538                 goto error;
539
540         return 0;
541
542 error:
543         omap4iss_ipipe_unregister_entities(ipipe);
544         return ret;
545 }
546
547 /* -----------------------------------------------------------------------------
548  * ISP IPIPE initialisation and cleanup
549  */
550
551 /*
552  * omap4iss_ipipe_init - IPIPE module initialization.
553  * @iss: Device pointer specific to the OMAP4 ISS.
554  *
555  * TODO: Get the initialisation values from platform data.
556  *
557  * Return 0 on success or a negative error code otherwise.
558  */
559 int omap4iss_ipipe_init(struct iss_device *iss)
560 {
561         struct iss_ipipe_device *ipipe = &iss->ipipe;
562
563         ipipe->state = ISS_PIPELINE_STREAM_STOPPED;
564         init_waitqueue_head(&ipipe->wait);
565
566         return ipipe_init_entities(ipipe);
567 }
568
569 /*
570  * omap4iss_ipipe_cleanup - IPIPE module cleanup.
571  * @iss: Device pointer specific to the OMAP4 ISS.
572  */
573 void omap4iss_ipipe_cleanup(struct iss_device *iss)
574 {
575         struct iss_ipipe_device *ipipe = &iss->ipipe;
576
577         media_entity_cleanup(&ipipe->subdev.entity);
578 }