1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/uaccess.h>
12#include <linux/delay.h>
13#include <linux/device.h>
14#include <linux/dma-mapping.h>
15#include <linux/mm.h>
16#include <linux/sched.h>
17
18#include "iss.h"
19#include "iss_regs.h"
20#include "iss_ipipe.h"
21
22static struct v4l2_mbus_framefmt *
23__ipipe_get_format(struct iss_ipipe_device *ipipe,
24 struct v4l2_subdev_state *sd_state,
25 unsigned int pad,
26 enum v4l2_subdev_format_whence which);
27
28static const unsigned int ipipe_fmts[] = {
29 MEDIA_BUS_FMT_SGRBG10_1X10,
30 MEDIA_BUS_FMT_SRGGB10_1X10,
31 MEDIA_BUS_FMT_SBGGR10_1X10,
32 MEDIA_BUS_FMT_SGBRG10_1X10,
33};
34
35
36
37
38
39
40
41#define IPIPE_PRINT_REGISTER(iss, name)\
42 dev_dbg(iss->dev, "###IPIPE " #name "=0x%08x\n", \
43 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_##name))
44
45static void ipipe_print_status(struct iss_ipipe_device *ipipe)
46{
47 struct iss_device *iss = to_iss_device(ipipe);
48
49 dev_dbg(iss->dev, "-------------IPIPE Register dump-------------\n");
50
51 IPIPE_PRINT_REGISTER(iss, SRC_EN);
52 IPIPE_PRINT_REGISTER(iss, SRC_MODE);
53 IPIPE_PRINT_REGISTER(iss, SRC_FMT);
54 IPIPE_PRINT_REGISTER(iss, SRC_COL);
55 IPIPE_PRINT_REGISTER(iss, SRC_VPS);
56 IPIPE_PRINT_REGISTER(iss, SRC_VSZ);
57 IPIPE_PRINT_REGISTER(iss, SRC_HPS);
58 IPIPE_PRINT_REGISTER(iss, SRC_HSZ);
59 IPIPE_PRINT_REGISTER(iss, GCK_MMR);
60 IPIPE_PRINT_REGISTER(iss, YUV_PHS);
61
62 dev_dbg(iss->dev, "-----------------------------------------------\n");
63}
64
65
66
67
68
69
70static void ipipe_enable(struct iss_ipipe_device *ipipe, u8 enable)
71{
72 struct iss_device *iss = to_iss_device(ipipe);
73
74 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_EN,
75 IPIPE_SRC_EN_EN, enable ? IPIPE_SRC_EN_EN : 0);
76}
77
78
79
80
81
82static void ipipe_configure(struct iss_ipipe_device *ipipe)
83{
84 struct iss_device *iss = to_iss_device(ipipe);
85 struct v4l2_mbus_framefmt *format;
86
87
88 format = &ipipe->formats[IPIPE_PAD_SINK];
89
90
91 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_FMT,
92 IPIPE_SRC_FMT_RAW2YUV);
93
94
95 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_YUV_PHS,
96 IPIPE_YUV_PHS_LPF);
97
98 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VPS, 0);
99 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HPS, 0);
100 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VSZ,
101 (format->height - 2) & IPIPE_SRC_VSZ_MASK);
102 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HSZ,
103 (format->width - 1) & IPIPE_SRC_HSZ_MASK);
104
105
106 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_MODE,
107 IPIPE_SRC_MODE_WRT | IPIPE_SRC_MODE_OST);
108
109
110 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_COL,
111 IPIPE_SRC_COL_EE_B | IPIPE_SRC_COL_EO_GB |
112 IPIPE_SRC_COL_OE_GR | IPIPE_SRC_COL_OO_R);
113
114
115 format = &ipipe->formats[IPIPE_PAD_SOURCE_VP];
116
117}
118
119
120
121
122
123
124
125
126
127
128static int ipipe_set_stream(struct v4l2_subdev *sd, int enable)
129{
130 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
131 struct iss_device *iss = to_iss_device(ipipe);
132 int ret = 0;
133
134 if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED) {
135 if (enable == ISS_PIPELINE_STREAM_STOPPED)
136 return 0;
137
138 omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
139
140
141 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_MMR,
142 IPIPE_GCK_MMR_REG);
143
144
145 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_PIX,
146 IPIPE_GCK_PIX_G3 | IPIPE_GCK_PIX_G2 |
147 IPIPE_GCK_PIX_G1 | IPIPE_GCK_PIX_G0);
148 }
149
150 switch (enable) {
151 case ISS_PIPELINE_STREAM_CONTINUOUS:
152
153 ipipe_configure(ipipe);
154 ipipe_print_status(ipipe);
155
156 atomic_set(&ipipe->stopping, 0);
157 ipipe_enable(ipipe, 1);
158 break;
159
160 case ISS_PIPELINE_STREAM_STOPPED:
161 if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED)
162 return 0;
163 if (omap4iss_module_sync_idle(&sd->entity, &ipipe->wait,
164 &ipipe->stopping))
165 ret = -ETIMEDOUT;
166
167 ipipe_enable(ipipe, 0);
168 omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
169 break;
170 }
171
172 ipipe->state = enable;
173 return ret;
174}
175
176static struct v4l2_mbus_framefmt *
177__ipipe_get_format(struct iss_ipipe_device *ipipe,
178 struct v4l2_subdev_state *sd_state,
179 unsigned int pad,
180 enum v4l2_subdev_format_whence which)
181{
182 if (which == V4L2_SUBDEV_FORMAT_TRY)
183 return v4l2_subdev_get_try_format(&ipipe->subdev, sd_state,
184 pad);
185
186 return &ipipe->formats[pad];
187}
188
189
190
191
192
193
194
195
196static void
197ipipe_try_format(struct iss_ipipe_device *ipipe,
198 struct v4l2_subdev_state *sd_state,
199 unsigned int pad,
200 struct v4l2_mbus_framefmt *fmt,
201 enum v4l2_subdev_format_whence which)
202{
203 struct v4l2_mbus_framefmt *format;
204 unsigned int width = fmt->width;
205 unsigned int height = fmt->height;
206 unsigned int i;
207
208 switch (pad) {
209 case IPIPE_PAD_SINK:
210 for (i = 0; i < ARRAY_SIZE(ipipe_fmts); i++) {
211 if (fmt->code == ipipe_fmts[i])
212 break;
213 }
214
215
216 if (i >= ARRAY_SIZE(ipipe_fmts))
217 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
218
219
220 fmt->width = clamp_t(u32, width, 1, 8192);
221 fmt->height = clamp_t(u32, height, 1, 8192);
222 fmt->colorspace = V4L2_COLORSPACE_SRGB;
223 break;
224
225 case IPIPE_PAD_SOURCE_VP:
226 format = __ipipe_get_format(ipipe, sd_state, IPIPE_PAD_SINK,
227 which);
228 memcpy(fmt, format, sizeof(*fmt));
229
230 fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
231 fmt->width = clamp_t(u32, width, 32, fmt->width);
232 fmt->height = clamp_t(u32, height, 32, fmt->height);
233 fmt->colorspace = V4L2_COLORSPACE_JPEG;
234 break;
235 }
236
237 fmt->field = V4L2_FIELD_NONE;
238}
239
240
241
242
243
244
245
246
247static int ipipe_enum_mbus_code(struct v4l2_subdev *sd,
248 struct v4l2_subdev_state *sd_state,
249 struct v4l2_subdev_mbus_code_enum *code)
250{
251 switch (code->pad) {
252 case IPIPE_PAD_SINK:
253 if (code->index >= ARRAY_SIZE(ipipe_fmts))
254 return -EINVAL;
255
256 code->code = ipipe_fmts[code->index];
257 break;
258
259 case IPIPE_PAD_SOURCE_VP:
260
261 if (code->index != 0)
262 return -EINVAL;
263
264 code->code = MEDIA_BUS_FMT_UYVY8_1X16;
265 break;
266
267 default:
268 return -EINVAL;
269 }
270
271 return 0;
272}
273
274static int ipipe_enum_frame_size(struct v4l2_subdev *sd,
275 struct v4l2_subdev_state *sd_state,
276 struct v4l2_subdev_frame_size_enum *fse)
277{
278 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
279 struct v4l2_mbus_framefmt format;
280
281 if (fse->index != 0)
282 return -EINVAL;
283
284 format.code = fse->code;
285 format.width = 1;
286 format.height = 1;
287 ipipe_try_format(ipipe, sd_state, fse->pad, &format, fse->which);
288 fse->min_width = format.width;
289 fse->min_height = format.height;
290
291 if (format.code != fse->code)
292 return -EINVAL;
293
294 format.code = fse->code;
295 format.width = -1;
296 format.height = -1;
297 ipipe_try_format(ipipe, sd_state, fse->pad, &format, fse->which);
298 fse->max_width = format.width;
299 fse->max_height = format.height;
300
301 return 0;
302}
303
304
305
306
307
308
309
310
311
312
313static int ipipe_get_format(struct v4l2_subdev *sd,
314 struct v4l2_subdev_state *sd_state,
315 struct v4l2_subdev_format *fmt)
316{
317 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
318 struct v4l2_mbus_framefmt *format;
319
320 format = __ipipe_get_format(ipipe, sd_state, fmt->pad, fmt->which);
321 if (!format)
322 return -EINVAL;
323
324 fmt->format = *format;
325 return 0;
326}
327
328
329
330
331
332
333
334
335
336
337static int ipipe_set_format(struct v4l2_subdev *sd,
338 struct v4l2_subdev_state *sd_state,
339 struct v4l2_subdev_format *fmt)
340{
341 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
342 struct v4l2_mbus_framefmt *format;
343
344 format = __ipipe_get_format(ipipe, sd_state, fmt->pad, fmt->which);
345 if (!format)
346 return -EINVAL;
347
348 ipipe_try_format(ipipe, sd_state, fmt->pad, &fmt->format, fmt->which);
349 *format = fmt->format;
350
351
352 if (fmt->pad == IPIPE_PAD_SINK) {
353 format = __ipipe_get_format(ipipe, sd_state,
354 IPIPE_PAD_SOURCE_VP,
355 fmt->which);
356 *format = fmt->format;
357 ipipe_try_format(ipipe, sd_state, IPIPE_PAD_SOURCE_VP, format,
358 fmt->which);
359 }
360
361 return 0;
362}
363
364static int ipipe_link_validate(struct v4l2_subdev *sd, struct media_link *link,
365 struct v4l2_subdev_format *source_fmt,
366 struct v4l2_subdev_format *sink_fmt)
367{
368
369 if (source_fmt->format.width != sink_fmt->format.width ||
370 source_fmt->format.height != sink_fmt->format.height)
371 return -EPIPE;
372
373 if (source_fmt->format.code != sink_fmt->format.code)
374 return -EPIPE;
375
376 return 0;
377}
378
379
380
381
382
383
384
385
386
387
388static int ipipe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
389{
390 struct v4l2_subdev_format format;
391
392 memset(&format, 0, sizeof(format));
393 format.pad = IPIPE_PAD_SINK;
394 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
395 format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
396 format.format.width = 4096;
397 format.format.height = 4096;
398 ipipe_set_format(sd, fh ? fh->state : NULL, &format);
399
400 return 0;
401}
402
403
404static const struct v4l2_subdev_video_ops ipipe_v4l2_video_ops = {
405 .s_stream = ipipe_set_stream,
406};
407
408
409static const struct v4l2_subdev_pad_ops ipipe_v4l2_pad_ops = {
410 .enum_mbus_code = ipipe_enum_mbus_code,
411 .enum_frame_size = ipipe_enum_frame_size,
412 .get_fmt = ipipe_get_format,
413 .set_fmt = ipipe_set_format,
414 .link_validate = ipipe_link_validate,
415};
416
417
418static const struct v4l2_subdev_ops ipipe_v4l2_ops = {
419 .video = &ipipe_v4l2_video_ops,
420 .pad = &ipipe_v4l2_pad_ops,
421};
422
423
424static const struct v4l2_subdev_internal_ops ipipe_v4l2_internal_ops = {
425 .open = ipipe_init_formats,
426};
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441static int ipipe_link_setup(struct media_entity *entity,
442 const struct media_pad *local,
443 const struct media_pad *remote, u32 flags)
444{
445 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
446 struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
447 struct iss_device *iss = to_iss_device(ipipe);
448
449 if (!is_media_entity_v4l2_subdev(remote->entity))
450 return -EINVAL;
451
452 switch (local->index) {
453 case IPIPE_PAD_SINK:
454
455 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
456 ipipe->input = IPIPE_INPUT_NONE;
457 break;
458 }
459
460 if (ipipe->input != IPIPE_INPUT_NONE)
461 return -EBUSY;
462
463 if (remote->entity == &iss->ipipeif.subdev.entity)
464 ipipe->input = IPIPE_INPUT_IPIPEIF;
465
466 break;
467
468 case IPIPE_PAD_SOURCE_VP:
469
470 if (flags & MEDIA_LNK_FL_ENABLED) {
471 if (ipipe->output & ~IPIPE_OUTPUT_VP)
472 return -EBUSY;
473 ipipe->output |= IPIPE_OUTPUT_VP;
474 } else {
475 ipipe->output &= ~IPIPE_OUTPUT_VP;
476 }
477 break;
478
479 default:
480 return -EINVAL;
481 }
482
483 return 0;
484}
485
486
487static const struct media_entity_operations ipipe_media_ops = {
488 .link_setup = ipipe_link_setup,
489 .link_validate = v4l2_subdev_link_validate,
490};
491
492
493
494
495
496
497
498static int ipipe_init_entities(struct iss_ipipe_device *ipipe)
499{
500 struct v4l2_subdev *sd = &ipipe->subdev;
501 struct media_pad *pads = ipipe->pads;
502 struct media_entity *me = &sd->entity;
503 int ret;
504
505 ipipe->input = IPIPE_INPUT_NONE;
506
507 v4l2_subdev_init(sd, &ipipe_v4l2_ops);
508 sd->internal_ops = &ipipe_v4l2_internal_ops;
509 strscpy(sd->name, "OMAP4 ISS ISP IPIPE", sizeof(sd->name));
510 sd->grp_id = BIT(16);
511 v4l2_set_subdevdata(sd, ipipe);
512 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
513
514 pads[IPIPE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
515 pads[IPIPE_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
516
517 me->ops = &ipipe_media_ops;
518 ret = media_entity_pads_init(me, IPIPE_PADS_NUM, pads);
519 if (ret < 0)
520 return ret;
521
522 ipipe_init_formats(sd, NULL);
523
524 return 0;
525}
526
527void omap4iss_ipipe_unregister_entities(struct iss_ipipe_device *ipipe)
528{
529 v4l2_device_unregister_subdev(&ipipe->subdev);
530}
531
532int omap4iss_ipipe_register_entities(struct iss_ipipe_device *ipipe,
533 struct v4l2_device *vdev)
534{
535 int ret;
536
537
538 ret = v4l2_device_register_subdev(vdev, &ipipe->subdev);
539 if (ret < 0)
540 goto error;
541
542 return 0;
543
544error:
545 omap4iss_ipipe_unregister_entities(ipipe);
546 return ret;
547}
548
549
550
551
552
553
554
555
556
557
558
559
560
561int omap4iss_ipipe_init(struct iss_device *iss)
562{
563 struct iss_ipipe_device *ipipe = &iss->ipipe;
564
565 ipipe->state = ISS_PIPELINE_STREAM_STOPPED;
566 init_waitqueue_head(&ipipe->wait);
567
568 return ipipe_init_entities(ipipe);
569}
570
571
572
573
574
575void omap4iss_ipipe_cleanup(struct iss_device *iss)
576{
577 struct iss_ipipe_device *ipipe = &iss->ipipe;
578
579 media_entity_cleanup(&ipipe->subdev.entity);
580}
581