summaryrefslogtreecommitdiff
path: root/drivers/media/platform/arm/mali-c55/mali-c55-isp.c
blob: 497f25fbdd13b0eb0e3e1b3da924dbbcd291176c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// SPDX-License-Identifier: GPL-2.0
/*
 * ARM Mali-C55 ISP Driver - Image signal processor
 *
 * Copyright (C) 2025 Ideas on Board Oy
 */

#include <linux/media/arm/mali-c55-config.h>

#include <linux/delay.h>
#include <linux/iopoll.h>
#include <linux/property.h>
#include <linux/string.h>

#include <uapi/linux/media/arm/mali-c55-config.h>

#include <media/media-entity.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <media/v4l2-mc.h>
#include <media/v4l2-subdev.h>

#include "mali-c55-common.h"
#include "mali-c55-registers.h"

static const struct mali_c55_isp_format_info mali_c55_isp_fmts[] = {
	{
		.code = MEDIA_BUS_FMT_SRGGB20_1X20,
		.shifted_code = MEDIA_BUS_FMT_SRGGB16_1X16,
		.order = MALI_C55_BAYER_ORDER_RGGB,
		.bypass = false,
	},
	{
		.code = MEDIA_BUS_FMT_SGRBG20_1X20,
		.shifted_code = MEDIA_BUS_FMT_SGRBG16_1X16,
		.order = MALI_C55_BAYER_ORDER_GRBG,
		.bypass = false,
	},
	{
		.code = MEDIA_BUS_FMT_SGBRG20_1X20,
		.shifted_code = MEDIA_BUS_FMT_SGBRG16_1X16,
		.order = MALI_C55_BAYER_ORDER_GBRG,
		.bypass = false,
	},
	{
		.code = MEDIA_BUS_FMT_SBGGR20_1X20,
		.shifted_code = MEDIA_BUS_FMT_SBGGR16_1X16,
		.order = MALI_C55_BAYER_ORDER_BGGR,
		.bypass = false,
	},
	{
		.code = MEDIA_BUS_FMT_RGB202020_1X60,
		.shifted_code = 0, /* Not relevant for this format */
		.order = 0, /* Not relevant for this format */
		.bypass = true,
	}
	/*
	 * TODO: Support MEDIA_BUS_FMT_YUV20_1X60 here. This is so that we can
	 * also support YUV input from a sensor passed-through to the output. At
	 * present we have no mechanism to test that though so it may have to
	 * wait a while...
	 */
};

const struct mali_c55_isp_format_info *
mali_c55_isp_get_mbus_config_by_index(u32 index)
{
	if (index < ARRAY_SIZE(mali_c55_isp_fmts))
		return &mali_c55_isp_fmts[index];

	return NULL;
}

const struct mali_c55_isp_format_info *
mali_c55_isp_get_mbus_config_by_code(u32 code)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(mali_c55_isp_fmts); i++) {
		if (mali_c55_isp_fmts[i].code == code)
			return &mali_c55_isp_fmts[i];
	}

	return NULL;
}

const struct mali_c55_isp_format_info *
mali_c55_isp_get_mbus_config_by_shifted_code(u32 code)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(mali_c55_isp_fmts); i++) {
		if (mali_c55_isp_fmts[i].shifted_code == code)
			return &mali_c55_isp_fmts[i];
	}

	return NULL;
}

static void mali_c55_isp_stop(struct mali_c55 *mali_c55)
{
	u32 val;

	mali_c55_write(mali_c55, MALI_C55_REG_INPUT_MODE_REQUEST,
		       MALI_C55_INPUT_SAFE_STOP);
	readl_poll_timeout(mali_c55->base + MALI_C55_REG_MODE_STATUS,
			   val, !val, 10 * USEC_PER_MSEC, 250 * USEC_PER_MSEC);
}

static int mali_c55_isp_start(struct mali_c55 *mali_c55,
			      const struct v4l2_subdev_state *state)
{
	struct mali_c55_context *ctx = mali_c55_get_active_context(mali_c55);
	const struct mali_c55_isp_format_info *cfg;
	const struct v4l2_mbus_framefmt *format;
	const struct v4l2_rect *crop;
	u32 val;
	int ret;

	mali_c55_update_bits(mali_c55, MALI_C55_REG_MCU_CONFIG,
			     MALI_C55_REG_MCU_CONFIG_WRITE_MASK,
			     MALI_C55_REG_MCU_CONFIG_WRITE_PING);

	/* Apply input windowing */
	crop = v4l2_subdev_state_get_crop(state, MALI_C55_ISP_PAD_SINK_VIDEO);
	format = v4l2_subdev_state_get_format(state,
					      MALI_C55_ISP_PAD_SINK_VIDEO);
	cfg = mali_c55_isp_get_mbus_config_by_code(format->code);

	mali_c55_write(mali_c55, MALI_C55_REG_HC_START,
		       MALI_C55_HC_START(crop->left));
	mali_c55_write(mali_c55, MALI_C55_REG_HC_SIZE,
		       MALI_C55_HC_SIZE(crop->width));
	mali_c55_write(mali_c55, MALI_C55_REG_VC_START_SIZE,
		       MALI_C55_VC_START(crop->top) |
		       MALI_C55_VC_SIZE(crop->height));
	mali_c55_ctx_update_bits(mali_c55, MALI_C55_REG_BASE_ADDR,
				 MALI_C55_REG_ACTIVE_WIDTH_MASK, format->width);
	mali_c55_ctx_update_bits(mali_c55, MALI_C55_REG_BASE_ADDR,
				 MALI_C55_REG_ACTIVE_HEIGHT_MASK,
				 format->height << 16);
	mali_c55_ctx_update_bits(mali_c55, MALI_C55_REG_BAYER_ORDER,
				 MALI_C55_BAYER_ORDER_MASK, cfg->order);
	mali_c55_ctx_update_bits(mali_c55, MALI_C55_REG_INPUT_WIDTH,
				 MALI_C55_INPUT_WIDTH_MASK,
				 MALI_C55_INPUT_WIDTH_20BIT);

	mali_c55_ctx_update_bits(mali_c55, MALI_C55_REG_ISP_RAW_BYPASS,
				 MALI_C55_ISP_RAW_BYPASS_BYPASS_MASK,
				 cfg->bypass ? MALI_C55_ISP_RAW_BYPASS_BYPASS_MASK :
					     0x00);

	mali_c55_params_write_config(mali_c55);
	ret = mali_c55_config_write(ctx, MALI_C55_CONFIG_PING, true);
	if (ret) {
		dev_err(mali_c55->dev, "failed to write ISP config\n");
		return ret;
	}

	mali_c55_write(mali_c55, MALI_C55_REG_INPUT_MODE_REQUEST,
		       MALI_C55_INPUT_SAFE_START);

	ret = readl_poll_timeout(mali_c55->base + MALI_C55_REG_MODE_STATUS, val,
				 val, 10 * USEC_PER_MSEC, 250 * USEC_PER_MSEC);
	if (ret) {
		mali_c55_isp_stop(mali_c55);
		dev_err(mali_c55->dev, "timeout starting ISP\n");
		return ret;
	}

	return 0;
}

static int mali_c55_isp_enum_mbus_code(struct v4l2_subdev *sd,
				       struct v4l2_subdev_state *state,
				       struct v4l2_subdev_mbus_code_enum *code)
{
	/*
	 * Only the internal RGB processed format is allowed on the regular
	 * processing source pad.
	 */
	if (code->pad == MALI_C55_ISP_PAD_SOURCE_VIDEO) {
		if (code->index)
			return -EINVAL;

		code->code = MEDIA_BUS_FMT_RGB121212_1X36;
		return 0;
	}

	/* On the sink and bypass pads all the supported formats are allowed. */
	if (code->index >= ARRAY_SIZE(mali_c55_isp_fmts))
		return -EINVAL;

	code->code = mali_c55_isp_fmts[code->index].code;

	return 0;
}

static int mali_c55_isp_enum_frame_size(struct v4l2_subdev *sd,
					struct v4l2_subdev_state *state,
					struct v4l2_subdev_frame_size_enum *fse)
{
	const struct mali_c55_isp_format_info *cfg;

	if (fse->index > 0)
		return -EINVAL;

	/*
	 * Only the internal RGB processed format is allowed on the regular
	 * processing source pad.
	 *
	 * On the sink and bypass pads all the supported formats are allowed.
	 */
	if (fse->pad == MALI_C55_ISP_PAD_SOURCE_VIDEO) {
		if (fse->code != MEDIA_BUS_FMT_RGB121212_1X36)
			return -EINVAL;
	} else {
		cfg = mali_c55_isp_get_mbus_config_by_code(fse->code);
		if (!cfg)
			return -EINVAL;
	}

	fse->min_width = MALI_C55_MIN_WIDTH;
	fse->min_height = MALI_C55_MIN_HEIGHT;
	fse->max_width = MALI_C55_MAX_WIDTH;
	fse->max_height = MALI_C55_MAX_HEIGHT;

	return 0;
}

static int mali_c55_isp_set_fmt(struct v4l2_subdev *sd,
				struct v4l2_subdev_state *state,
				struct v4l2_subdev_format *format)
{
	struct v4l2_mbus_framefmt *fmt = &format->format;
	struct v4l2_mbus_framefmt *src_fmt, *sink_fmt;
	const struct mali_c55_isp_format_info *cfg;
	struct v4l2_rect *crop;

	/*
	 * Disallow set_fmt on the source pads; format is fixed and the sizes
	 * are the result of applying the sink crop rectangle to the sink
	 * format.
	 */
	if (format->pad != MALI_C55_ISP_PAD_SINK_VIDEO)
		return v4l2_subdev_get_fmt(sd, state, format);

	sink_fmt = v4l2_subdev_state_get_format(state,
						MALI_C55_ISP_PAD_SINK_VIDEO);

	cfg = mali_c55_isp_get_mbus_config_by_code(fmt->code);
	sink_fmt->code = cfg ? fmt->code : MEDIA_BUS_FMT_SRGGB20_1X20;

	/*
	 * Clamp sizes in the accepted limits and clamp the crop rectangle in
	 * the new sizes.
	 */
	sink_fmt->width = clamp(fmt->width, MALI_C55_MIN_WIDTH,
				MALI_C55_MAX_WIDTH);
	sink_fmt->height = clamp(fmt->height, MALI_C55_MIN_HEIGHT,
				 MALI_C55_MAX_HEIGHT);

	*fmt = *sink_fmt;

	crop = v4l2_subdev_state_get_crop(state, MALI_C55_ISP_PAD_SINK_VIDEO);
	crop->left = 0;
	crop->top = 0;
	crop->width = sink_fmt->width;
	crop->height = sink_fmt->height;

	/*
	 * Propagate format to source pads. On the 'regular' output pad use
	 * the internal RGB processed format, while on the bypass pad simply
	 * replicate the ISP sink format. The sizes on both pads are the same as
	 * the ISP sink crop rectangle. The "field" and "colorspace" fields are
	 * set in .init_state() and fixed for both source pads, as is the "code"
	 * field for the processed data source pad.
	 */
	src_fmt = v4l2_subdev_state_get_format(state,
					       MALI_C55_ISP_PAD_SOURCE_VIDEO);
	src_fmt->width = crop->width;
	src_fmt->height = crop->height;

	src_fmt = v4l2_subdev_state_get_format(state,
					       MALI_C55_ISP_PAD_SOURCE_BYPASS);
	src_fmt->code = sink_fmt->code;
	src_fmt->width = crop->width;
	src_fmt->height = crop->height;

	return 0;
}

static int mali_c55_isp_get_selection(struct v4l2_subdev *sd,
				      struct v4l2_subdev_state *state,
				      struct v4l2_subdev_selection *sel)
{
	if (sel->pad != MALI_C55_ISP_PAD_SINK_VIDEO ||
	    sel->target != V4L2_SEL_TGT_CROP)
		return -EINVAL;

	sel->r = *v4l2_subdev_state_get_crop(state, MALI_C55_ISP_PAD_SINK_VIDEO);

	return 0;
}

static int mali_c55_isp_set_selection(struct v4l2_subdev *sd,
				      struct v4l2_subdev_state *state,
				      struct v4l2_subdev_selection *sel)
{
	struct v4l2_mbus_framefmt *src_fmt;
	const struct v4l2_mbus_framefmt *fmt;
	struct v4l2_rect *crop;

	if (sel->pad != MALI_C55_ISP_PAD_SINK_VIDEO ||
	    sel->target != V4L2_SEL_TGT_CROP)
		return -EINVAL;

	fmt = v4l2_subdev_state_get_format(state, MALI_C55_ISP_PAD_SINK_VIDEO);

	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, fmt->width);
	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, fmt->height);
	sel->r.width = clamp_t(unsigned int, sel->r.width, MALI_C55_MIN_WIDTH,
			       fmt->width - sel->r.left);
	sel->r.height = clamp_t(unsigned int, sel->r.height,
				MALI_C55_MIN_HEIGHT,
				fmt->height - sel->r.top);

	crop = v4l2_subdev_state_get_crop(state, MALI_C55_ISP_PAD_SINK_VIDEO);
	*crop = sel->r;

	/*
	 * Propagate the crop rectangle sizes to the source pad format. The crop
	 * isn't propagated to the bypass source pad, because the bypassed data
	 * cannot be cropped.
	 */
	src_fmt = v4l2_subdev_state_get_format(state,
					       MALI_C55_ISP_PAD_SOURCE_VIDEO);
	src_fmt->width = crop->width;
	src_fmt->height = crop->height;

	return 0;
}

static int mali_c55_isp_enable_streams(struct v4l2_subdev *sd,
				       struct v4l2_subdev_state *state, u32 pad,
				       u64 streams_mask)
{
	struct mali_c55_isp *isp = container_of(sd, struct mali_c55_isp, sd);
	struct mali_c55 *mali_c55 = isp->mali_c55;
	struct v4l2_subdev *src_sd;
	struct media_pad *sink_pad;
	int ret;

	/*
	 * We have two source pads, both of which have only a single stream. The
	 * core v4l2 code already validated those parameters so we can just get
	 * on with starting the ISP.
	 */

	sink_pad = &isp->pads[MALI_C55_ISP_PAD_SINK_VIDEO];
	isp->remote_src = media_pad_remote_pad_unique(sink_pad);
	src_sd = media_entity_to_v4l2_subdev(isp->remote_src->entity);

	isp->frame_sequence = 0;
	ret = mali_c55_isp_start(mali_c55, state);
	if (ret) {
		dev_err(mali_c55->dev, "Failed to start ISP\n");
		isp->remote_src = NULL;
		return ret;
	}

	/*
	 * We only support a single input stream, so we can just enable the 1st
	 * entry in the streams mask.
	 */
	ret = v4l2_subdev_enable_streams(src_sd, isp->remote_src->index, BIT(0));
	if (ret) {
		dev_err(mali_c55->dev, "Failed to start ISP source\n");
		mali_c55_isp_stop(mali_c55);
		return ret;
	}

	return 0;
}

static int mali_c55_isp_disable_streams(struct v4l2_subdev *sd,
					struct v4l2_subdev_state *state, u32 pad,
					u64 streams_mask)
{
	struct mali_c55_isp *isp = container_of(sd, struct mali_c55_isp, sd);
	struct mali_c55 *mali_c55 = isp->mali_c55;
	struct v4l2_subdev *src_sd;

	if (isp->remote_src) {
		src_sd = media_entity_to_v4l2_subdev(isp->remote_src->entity);
		v4l2_subdev_disable_streams(src_sd, isp->remote_src->index,
					    BIT(0));
	}
	isp->remote_src = NULL;

	mali_c55_isp_stop(mali_c55);

	return 0;
}

static const struct v4l2_subdev_pad_ops mali_c55_isp_pad_ops = {
	.enum_mbus_code		= mali_c55_isp_enum_mbus_code,
	.enum_frame_size	= mali_c55_isp_enum_frame_size,
	.get_fmt		= v4l2_subdev_get_fmt,
	.set_fmt		= mali_c55_isp_set_fmt,
	.get_selection		= mali_c55_isp_get_selection,
	.set_selection		= mali_c55_isp_set_selection,
	.link_validate		= v4l2_subdev_link_validate_default,
	.enable_streams		= mali_c55_isp_enable_streams,
	.disable_streams	= mali_c55_isp_disable_streams,
};

void mali_c55_isp_queue_event_sof(struct mali_c55 *mali_c55)
{
	struct v4l2_event event = {
		.type = V4L2_EVENT_FRAME_SYNC,
	};

	event.u.frame_sync.frame_sequence = mali_c55->isp.frame_sequence;
	v4l2_event_queue(mali_c55->isp.sd.devnode, &event);
}

static int
mali_c55_isp_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
			     struct v4l2_event_subscription *sub)
{
	switch (sub->type) {
	case V4L2_EVENT_FRAME_SYNC:
		return v4l2_event_subscribe(fh, sub, 0, NULL);
	case V4L2_EVENT_CTRL:
		return v4l2_ctrl_subscribe_event(fh, sub);
	default:
		return -EINVAL;
	}
}

static const struct v4l2_subdev_core_ops mali_c55_isp_core_ops = {
	.subscribe_event = mali_c55_isp_subscribe_event,
	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
};

static const struct v4l2_subdev_ops mali_c55_isp_ops = {
	.pad	= &mali_c55_isp_pad_ops,
	.core	= &mali_c55_isp_core_ops,
};

static int mali_c55_isp_init_state(struct v4l2_subdev *sd,
				   struct v4l2_subdev_state *state)
{
	struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
	struct v4l2_rect *in_crop;

	sink_fmt = v4l2_subdev_state_get_format(state,
						MALI_C55_ISP_PAD_SINK_VIDEO);
	src_fmt = v4l2_subdev_state_get_format(state,
					       MALI_C55_ISP_PAD_SOURCE_VIDEO);
	in_crop = v4l2_subdev_state_get_crop(state,
					     MALI_C55_ISP_PAD_SINK_VIDEO);

	sink_fmt->width = MALI_C55_DEFAULT_WIDTH;
	sink_fmt->height = MALI_C55_DEFAULT_HEIGHT;
	sink_fmt->field = V4L2_FIELD_NONE;
	sink_fmt->code = MEDIA_BUS_FMT_SRGGB20_1X20;
	sink_fmt->colorspace = V4L2_COLORSPACE_RAW;
	sink_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(sink_fmt->colorspace);
	sink_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(sink_fmt->colorspace);
	sink_fmt->quantization =
		V4L2_MAP_QUANTIZATION_DEFAULT(false, sink_fmt->colorspace,
					      sink_fmt->ycbcr_enc);

	*v4l2_subdev_state_get_format(state,
			      MALI_C55_ISP_PAD_SOURCE_BYPASS) = *sink_fmt;

	src_fmt->width = MALI_C55_DEFAULT_WIDTH;
	src_fmt->height = MALI_C55_DEFAULT_HEIGHT;
	src_fmt->field = V4L2_FIELD_NONE;
	src_fmt->code = MEDIA_BUS_FMT_RGB121212_1X36;
	src_fmt->colorspace = V4L2_COLORSPACE_SRGB;
	src_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(sink_fmt->colorspace);
	src_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(sink_fmt->colorspace);
	src_fmt->quantization =
		V4L2_MAP_QUANTIZATION_DEFAULT(false, sink_fmt->colorspace,
					      sink_fmt->ycbcr_enc);

	in_crop->top = 0;
	in_crop->left = 0;
	in_crop->width = MALI_C55_DEFAULT_WIDTH;
	in_crop->height = MALI_C55_DEFAULT_HEIGHT;

	src_fmt = v4l2_subdev_state_get_format(state,
					       MALI_C55_ISP_PAD_SOURCE_STATS);
	sink_fmt = v4l2_subdev_state_get_format(state,
						MALI_C55_ISP_PAD_SINK_PARAMS);

	src_fmt->width = 0;
	src_fmt->height = 0;
	src_fmt->field = V4L2_FIELD_NONE;
	src_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED;

	sink_fmt->width = 0;
	sink_fmt->height = 0;
	sink_fmt->field = V4L2_FIELD_NONE;
	sink_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED;

	return 0;
}

static const struct v4l2_subdev_internal_ops mali_c55_isp_internal_ops = {
	.init_state = mali_c55_isp_init_state,
};

static int mali_c55_subdev_link_validate(struct media_link *link)
{
	/*
	 * Skip validation for the parameters sink pad, as the source is not
	 * a subdevice.
	 */
	if (link->sink->index == MALI_C55_ISP_PAD_SINK_PARAMS)
		return 0;

	return v4l2_subdev_link_validate(link);
}

static const struct media_entity_operations mali_c55_isp_media_ops = {
	.link_validate		= mali_c55_subdev_link_validate,
};

static int mali_c55_isp_s_ctrl(struct v4l2_ctrl *ctrl)
{
	/*
	 * .s_ctrl() is a mandatory operation, but the driver has only a single
	 * read only control. If we got here, something went badly wrong.
	 */
	return -EINVAL;
}

static const struct v4l2_ctrl_ops mali_c55_isp_ctrl_ops = {
	.s_ctrl = mali_c55_isp_s_ctrl,
};

/* NOT const because the default needs to be filled in at runtime */
static struct v4l2_ctrl_config mali_c55_isp_v4l2_custom_ctrls[] = {
	{
		.ops = &mali_c55_isp_ctrl_ops,
		.id = V4L2_CID_MALI_C55_CAPABILITIES,
		.name = "Mali-C55 ISP Capabilities",
		.type = V4L2_CTRL_TYPE_BITMASK,
		.min = 0,
		.max = MALI_C55_GPS_PONG_FITTED |
		       MALI_C55_GPS_WDR_FITTED |
		       MALI_C55_GPS_COMPRESSION_FITTED |
		       MALI_C55_GPS_TEMPER_FITTED |
		       MALI_C55_GPS_SINTER_LITE_FITTED |
		       MALI_C55_GPS_SINTER_FITTED |
		       MALI_C55_GPS_IRIDIX_LTM_FITTED |
		       MALI_C55_GPS_IRIDIX_GTM_FITTED |
		       MALI_C55_GPS_CNR_FITTED |
		       MALI_C55_GPS_FRSCALER_FITTED |
		       MALI_C55_GPS_DS_PIPE_FITTED,
		.def = 0,
	},
};

static int mali_c55_isp_init_controls(struct mali_c55 *mali_c55)
{
	struct v4l2_ctrl_handler *handler = &mali_c55->isp.handler;
	struct v4l2_ctrl *capabilities;
	int ret;

	ret = v4l2_ctrl_handler_init(handler, 1);
	if (ret)
		return ret;

	mali_c55_isp_v4l2_custom_ctrls[0].def = mali_c55->capabilities;

	capabilities = v4l2_ctrl_new_custom(handler,
					    &mali_c55_isp_v4l2_custom_ctrls[0],
					    NULL);
	if (capabilities)
		capabilities->flags |= V4L2_CTRL_FLAG_READ_ONLY;

	if (handler->error) {
		dev_err(mali_c55->dev, "failed to register capabilities control\n");
		ret = handler->error;
		v4l2_ctrl_handler_free(handler);
		return ret;
	}

	mali_c55->isp.sd.ctrl_handler = handler;

	return 0;
}

int mali_c55_register_isp(struct mali_c55 *mali_c55)
{
	struct mali_c55_isp *isp = &mali_c55->isp;
	struct v4l2_subdev *sd = &isp->sd;
	int ret;

	isp->mali_c55 = mali_c55;

	v4l2_subdev_init(sd, &mali_c55_isp_ops);
	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
	sd->entity.ops = &mali_c55_isp_media_ops;
	sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_ISP;
	sd->internal_ops = &mali_c55_isp_internal_ops;
	strscpy(sd->name, MALI_C55_DRIVER_NAME " isp", sizeof(sd->name));

	isp->pads[MALI_C55_ISP_PAD_SINK_VIDEO].flags = MEDIA_PAD_FL_SINK |
						       MEDIA_PAD_FL_MUST_CONNECT;
	isp->pads[MALI_C55_ISP_PAD_SOURCE_VIDEO].flags = MEDIA_PAD_FL_SOURCE;
	isp->pads[MALI_C55_ISP_PAD_SOURCE_BYPASS].flags = MEDIA_PAD_FL_SOURCE;
	isp->pads[MALI_C55_ISP_PAD_SOURCE_STATS].flags = MEDIA_PAD_FL_SOURCE;
	isp->pads[MALI_C55_ISP_PAD_SINK_PARAMS].flags = MEDIA_PAD_FL_SINK;

	ret = media_entity_pads_init(&sd->entity, MALI_C55_ISP_NUM_PADS,
				     isp->pads);
	if (ret)
		return ret;

	ret = mali_c55_isp_init_controls(mali_c55);
	if (ret)
		goto err_cleanup_media_entity;

	ret = v4l2_subdev_init_finalize(sd);
	if (ret)
		goto err_free_ctrl_handler;

	ret = v4l2_device_register_subdev(&mali_c55->v4l2_dev, sd);
	if (ret)
		goto err_cleanup_subdev;

	mutex_init(&isp->capture_lock);

	return 0;

err_cleanup_subdev:
	v4l2_subdev_cleanup(sd);
err_free_ctrl_handler:
	v4l2_ctrl_handler_free(&isp->handler);
err_cleanup_media_entity:
	media_entity_cleanup(&sd->entity);
	isp->mali_c55 = NULL;

	return ret;
}

void mali_c55_unregister_isp(struct mali_c55 *mali_c55)
{
	struct mali_c55_isp *isp = &mali_c55->isp;

	if (!isp->mali_c55)
		return;

	mutex_destroy(&isp->capture_lock);
	v4l2_device_unregister_subdev(&isp->sd);
	v4l2_subdev_cleanup(&isp->sd);
	media_entity_cleanup(&isp->sd.entity);
}