xref: /linux/drivers/media/pci/intel/ivsc/mei_csi.c (revision d5859510d35d8e7d63fed5169f1775317f40fb03)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2023 Intel Corporation. All rights reserved.
4  * Intel Visual Sensing Controller CSI Linux driver
5  */
6 
7 /*
8  * To set ownership of CSI-2 link and to configure CSI-2 link, there
9  * are specific commands, which are sent via MEI protocol. The send
10  * command function uses "completion" as a synchronization mechanism.
11  * The response for command is received via a mei callback which wakes
12  * up the caller. There can be only one outstanding command at a time.
13  */
14 
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/math64.h>
19 #include <linux/mei_cl_bus.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/pci.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/units.h>
26 #include <linux/uuid.h>
27 #include <linux/workqueue.h>
28 
29 #include <media/ipu-bridge.h>
30 #include <media/ipu6-pci-table.h>
31 #include <media/v4l2-async.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-fwnode.h>
34 #include <media/v4l2-subdev.h>
35 
36 #define MEI_CSI_ENTITY_NAME "Intel IVSC CSI"
37 
38 #define MEI_CSI_LINK_FREQ_400MHZ 400000000ULL
39 
40 /* the 5s used here is based on experiment */
41 #define CSI_CMD_TIMEOUT (5 * HZ)
42 /* to setup CSI-2 link an extra delay needed and determined experimentally */
43 #define CSI_FW_READY_DELAY_MS 100
44 /* link frequency unit is 100kHz */
45 #define CSI_LINK_FREQ(x) ((u32)(div_u64(x, 100 * HZ_PER_KHZ)))
46 
47 /*
48  * identify the command id supported by firmware
49  * IPC, as well as the privacy notification id
50  * used when processing privacy event.
51  */
52 enum csi_cmd_id {
53 	/* used to set csi ownership */
54 	CSI_SET_OWNER = 0,
55 
56 	/* used to configure CSI-2 link */
57 	CSI_SET_CONF = 2,
58 
59 	/* privacy notification id used when privacy state changes */
60 	CSI_PRIVACY_NOTIF = 6,
61 };
62 
63 /* CSI-2 link ownership definition */
64 enum csi_link_owner {
65 	CSI_LINK_IVSC,
66 	CSI_LINK_HOST,
67 };
68 
69 /* privacy status definition */
70 enum ivsc_privacy_status {
71 	CSI_PRIVACY_OFF,
72 	CSI_PRIVACY_ON,
73 	CSI_PRIVACY_MAX,
74 };
75 
76 enum csi_pads {
77 	CSI_PAD_SINK,
78 	CSI_PAD_SOURCE,
79 	CSI_NUM_PADS
80 };
81 
82 /* configuration of the CSI-2 link between host and IVSC */
83 struct csi_link_cfg {
84 	/* number of data lanes used on the CSI-2 link */
85 	u32 nr_of_lanes;
86 
87 	/* frequency of the CSI-2 link */
88 	u32 link_freq;
89 
90 	/* for future use */
91 	u32 rsvd[2];
92 } __packed;
93 
94 /* CSI command structure */
95 struct csi_cmd {
96 	u32 cmd_id;
97 	union _cmd_param {
98 		u32 param;
99 		struct csi_link_cfg conf;
100 	} param;
101 } __packed;
102 
103 /* CSI notification structure */
104 struct csi_notif {
105 	u32 cmd_id;
106 	int status;
107 	union _resp_cont {
108 		u32 cont;
109 		struct csi_link_cfg conf;
110 	} cont;
111 } __packed;
112 
113 struct mei_csi {
114 	struct mei_cl_device *cldev;
115 
116 	/* command response */
117 	struct csi_notif cmd_response;
118 	/* used to wait for command response from firmware */
119 	struct completion cmd_completion;
120 	/* protect command download */
121 	struct mutex lock;
122 
123 	struct v4l2_subdev subdev;
124 	struct v4l2_subdev *remote;
125 	struct v4l2_async_notifier notifier;
126 	struct v4l2_ctrl_handler ctrl_handler;
127 	struct v4l2_ctrl *freq_ctrl;
128 	struct v4l2_ctrl *privacy_ctrl;
129 	unsigned int remote_pad;
130 	/* start streaming or not */
131 	int streaming;
132 
133 	struct media_pad pads[CSI_NUM_PADS];
134 
135 	/* number of data lanes used on the CSI-2 link */
136 	u32 nr_of_lanes;
137 	/* frequency of the CSI-2 link */
138 	u64 link_freq;
139 
140 	/* privacy status */
141 	enum ivsc_privacy_status status;
142 };
143 
144 static const struct v4l2_mbus_framefmt mei_csi_format_mbus_default = {
145 	.width = 1,
146 	.height = 1,
147 	.code = MEDIA_BUS_FMT_Y8_1X8,
148 	.field = V4L2_FIELD_NONE,
149 };
150 
151 static s64 link_freq_menu_items[] = {
152 	MEI_CSI_LINK_FREQ_400MHZ
153 };
154 
155 static inline struct mei_csi *notifier_to_csi(struct v4l2_async_notifier *n)
156 {
157 	return container_of(n, struct mei_csi, notifier);
158 }
159 
160 static inline struct mei_csi *sd_to_csi(struct v4l2_subdev *sd)
161 {
162 	return container_of(sd, struct mei_csi, subdev);
163 }
164 
165 static inline struct mei_csi *ctrl_to_csi(struct v4l2_ctrl *ctrl)
166 {
167 	return container_of(ctrl->handler, struct mei_csi, ctrl_handler);
168 }
169 
170 /* send a command to firmware and mutex must be held by caller */
171 static int mei_csi_send(struct mei_csi *csi, u8 *buf, size_t len)
172 {
173 	struct csi_cmd *cmd = (struct csi_cmd *)buf;
174 	int ret;
175 
176 	reinit_completion(&csi->cmd_completion);
177 
178 	ret = mei_cldev_send(csi->cldev, buf, len);
179 	if (ret < 0)
180 		goto out;
181 
182 	ret = wait_for_completion_killable_timeout(&csi->cmd_completion,
183 						   CSI_CMD_TIMEOUT);
184 	if (ret < 0) {
185 		goto out;
186 	} else if (!ret) {
187 		ret = -ETIMEDOUT;
188 		goto out;
189 	}
190 
191 	/* command response status */
192 	ret = csi->cmd_response.status;
193 	if (ret) {
194 		ret = -EINVAL;
195 		goto out;
196 	}
197 
198 	if (csi->cmd_response.cmd_id != cmd->cmd_id)
199 		ret = -EINVAL;
200 
201 out:
202 	return ret;
203 }
204 
205 /* set CSI-2 link ownership */
206 static int csi_set_link_owner(struct mei_csi *csi, enum csi_link_owner owner)
207 {
208 	struct csi_cmd cmd = { 0 };
209 	size_t cmd_size;
210 	int ret;
211 
212 	cmd.cmd_id = CSI_SET_OWNER;
213 	cmd.param.param = owner;
214 	cmd_size = sizeof(cmd.cmd_id) + sizeof(cmd.param.param);
215 
216 	mutex_lock(&csi->lock);
217 
218 	ret = mei_csi_send(csi, (u8 *)&cmd, cmd_size);
219 
220 	mutex_unlock(&csi->lock);
221 
222 	return ret;
223 }
224 
225 /* configure CSI-2 link between host and IVSC */
226 static int csi_set_link_cfg(struct mei_csi *csi)
227 {
228 	struct csi_cmd cmd = { 0 };
229 	size_t cmd_size;
230 	int ret;
231 
232 	cmd.cmd_id = CSI_SET_CONF;
233 	cmd.param.conf.nr_of_lanes = csi->nr_of_lanes;
234 	cmd.param.conf.link_freq = CSI_LINK_FREQ(csi->link_freq);
235 	cmd_size = sizeof(cmd.cmd_id) + sizeof(cmd.param.conf);
236 
237 	mutex_lock(&csi->lock);
238 
239 	ret = mei_csi_send(csi, (u8 *)&cmd, cmd_size);
240 	/*
241 	 * wait configuration ready if download success. placing
242 	 * delay under mutex is to make sure current command flow
243 	 * completed before starting a possible new one.
244 	 */
245 	if (!ret)
246 		msleep(CSI_FW_READY_DELAY_MS);
247 
248 	mutex_unlock(&csi->lock);
249 
250 	return ret;
251 }
252 
253 /* callback for receive */
254 static void mei_csi_rx(struct mei_cl_device *cldev)
255 {
256 	struct mei_csi *csi = mei_cldev_get_drvdata(cldev);
257 	struct csi_notif notif = { 0 };
258 	int ret;
259 
260 	ret = mei_cldev_recv(cldev, (u8 *)&notif, sizeof(notif));
261 	if (ret < 0) {
262 		dev_err(&cldev->dev, "recv error: %d\n", ret);
263 		return;
264 	}
265 
266 	switch (notif.cmd_id) {
267 	case CSI_PRIVACY_NOTIF:
268 		if (notif.cont.cont < CSI_PRIVACY_MAX) {
269 			csi->status = notif.cont.cont;
270 			v4l2_ctrl_s_ctrl(csi->privacy_ctrl, csi->status);
271 		}
272 		break;
273 	case CSI_SET_OWNER:
274 	case CSI_SET_CONF:
275 		memcpy(&csi->cmd_response, &notif, ret);
276 
277 		complete(&csi->cmd_completion);
278 		break;
279 	default:
280 		break;
281 	}
282 }
283 
284 static int mei_csi_set_stream(struct v4l2_subdev *sd, int enable)
285 {
286 	struct mei_csi *csi = sd_to_csi(sd);
287 	s64 freq;
288 	int ret;
289 
290 	if (enable && csi->streaming == 0) {
291 		freq = v4l2_get_link_freq(csi->remote->ctrl_handler, 0, 0);
292 		if (freq < 0) {
293 			dev_err(&csi->cldev->dev,
294 				"error %lld, invalid link_freq\n", freq);
295 			ret = freq;
296 			goto err;
297 		}
298 		csi->link_freq = freq;
299 
300 		/* switch CSI-2 link to host */
301 		ret = csi_set_link_owner(csi, CSI_LINK_HOST);
302 		if (ret < 0)
303 			goto err;
304 
305 		/* configure CSI-2 link */
306 		ret = csi_set_link_cfg(csi);
307 		if (ret < 0)
308 			goto err_switch;
309 
310 		ret = v4l2_subdev_call(csi->remote, video, s_stream, 1);
311 		if (ret)
312 			goto err_switch;
313 	} else if (!enable && csi->streaming == 1) {
314 		v4l2_subdev_call(csi->remote, video, s_stream, 0);
315 
316 		/* switch CSI-2 link to IVSC */
317 		ret = csi_set_link_owner(csi, CSI_LINK_IVSC);
318 		if (ret < 0)
319 			dev_warn(&csi->cldev->dev,
320 				 "failed to switch CSI2 link: %d\n", ret);
321 	}
322 
323 	csi->streaming = enable;
324 
325 	return 0;
326 
327 err_switch:
328 	csi_set_link_owner(csi, CSI_LINK_IVSC);
329 
330 err:
331 	return ret;
332 }
333 
334 static int mei_csi_init_state(struct v4l2_subdev *sd,
335 			      struct v4l2_subdev_state *sd_state)
336 {
337 	struct v4l2_mbus_framefmt *mbusformat;
338 	unsigned int i;
339 
340 	for (i = 0; i < sd->entity.num_pads; i++) {
341 		mbusformat = v4l2_subdev_state_get_format(sd_state, i);
342 		*mbusformat = mei_csi_format_mbus_default;
343 	}
344 
345 	return 0;
346 }
347 
348 static int mei_csi_set_fmt(struct v4l2_subdev *sd,
349 			   struct v4l2_subdev_state *sd_state,
350 			   struct v4l2_subdev_format *format)
351 {
352 	struct v4l2_mbus_framefmt *source_fmt;
353 	struct v4l2_mbus_framefmt *sink_fmt;
354 
355 	sink_fmt = v4l2_subdev_state_get_format(sd_state, CSI_PAD_SINK);
356 	source_fmt = v4l2_subdev_state_get_format(sd_state, CSI_PAD_SOURCE);
357 
358 	if (format->pad) {
359 		*source_fmt = *sink_fmt;
360 
361 		return 0;
362 	}
363 
364 	v4l_bound_align_image(&format->format.width, 1, 65536, 0,
365 			      &format->format.height, 1, 65536, 0, 0);
366 
367 	switch (format->format.code) {
368 	case MEDIA_BUS_FMT_RGB444_1X12:
369 	case MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE:
370 	case MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE:
371 	case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE:
372 	case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
373 	case MEDIA_BUS_FMT_RGB565_1X16:
374 	case MEDIA_BUS_FMT_BGR565_2X8_BE:
375 	case MEDIA_BUS_FMT_BGR565_2X8_LE:
376 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
377 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
378 	case MEDIA_BUS_FMT_RGB666_1X18:
379 	case MEDIA_BUS_FMT_RBG888_1X24:
380 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
381 	case MEDIA_BUS_FMT_BGR888_1X24:
382 	case MEDIA_BUS_FMT_GBR888_1X24:
383 	case MEDIA_BUS_FMT_RGB888_1X24:
384 	case MEDIA_BUS_FMT_RGB888_2X12_BE:
385 	case MEDIA_BUS_FMT_RGB888_2X12_LE:
386 	case MEDIA_BUS_FMT_ARGB8888_1X32:
387 	case MEDIA_BUS_FMT_RGB888_1X32_PADHI:
388 	case MEDIA_BUS_FMT_RGB101010_1X30:
389 	case MEDIA_BUS_FMT_RGB121212_1X36:
390 	case MEDIA_BUS_FMT_RGB161616_1X48:
391 	case MEDIA_BUS_FMT_Y8_1X8:
392 	case MEDIA_BUS_FMT_UV8_1X8:
393 	case MEDIA_BUS_FMT_UYVY8_1_5X8:
394 	case MEDIA_BUS_FMT_VYUY8_1_5X8:
395 	case MEDIA_BUS_FMT_YUYV8_1_5X8:
396 	case MEDIA_BUS_FMT_YVYU8_1_5X8:
397 	case MEDIA_BUS_FMT_UYVY8_2X8:
398 	case MEDIA_BUS_FMT_VYUY8_2X8:
399 	case MEDIA_BUS_FMT_YUYV8_2X8:
400 	case MEDIA_BUS_FMT_YVYU8_2X8:
401 	case MEDIA_BUS_FMT_Y10_1X10:
402 	case MEDIA_BUS_FMT_UYVY10_2X10:
403 	case MEDIA_BUS_FMT_VYUY10_2X10:
404 	case MEDIA_BUS_FMT_YUYV10_2X10:
405 	case MEDIA_BUS_FMT_YVYU10_2X10:
406 	case MEDIA_BUS_FMT_Y12_1X12:
407 	case MEDIA_BUS_FMT_UYVY12_2X12:
408 	case MEDIA_BUS_FMT_VYUY12_2X12:
409 	case MEDIA_BUS_FMT_YUYV12_2X12:
410 	case MEDIA_BUS_FMT_YVYU12_2X12:
411 	case MEDIA_BUS_FMT_UYVY8_1X16:
412 	case MEDIA_BUS_FMT_VYUY8_1X16:
413 	case MEDIA_BUS_FMT_YUYV8_1X16:
414 	case MEDIA_BUS_FMT_YVYU8_1X16:
415 	case MEDIA_BUS_FMT_YDYUYDYV8_1X16:
416 	case MEDIA_BUS_FMT_UYVY10_1X20:
417 	case MEDIA_BUS_FMT_VYUY10_1X20:
418 	case MEDIA_BUS_FMT_YUYV10_1X20:
419 	case MEDIA_BUS_FMT_YVYU10_1X20:
420 	case MEDIA_BUS_FMT_VUY8_1X24:
421 	case MEDIA_BUS_FMT_YUV8_1X24:
422 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
423 	case MEDIA_BUS_FMT_UYVY12_1X24:
424 	case MEDIA_BUS_FMT_VYUY12_1X24:
425 	case MEDIA_BUS_FMT_YUYV12_1X24:
426 	case MEDIA_BUS_FMT_YVYU12_1X24:
427 	case MEDIA_BUS_FMT_YUV10_1X30:
428 	case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
429 	case MEDIA_BUS_FMT_AYUV8_1X32:
430 	case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
431 	case MEDIA_BUS_FMT_YUV12_1X36:
432 	case MEDIA_BUS_FMT_YUV16_1X48:
433 	case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
434 	case MEDIA_BUS_FMT_JPEG_1X8:
435 	case MEDIA_BUS_FMT_AHSV8888_1X32:
436 	case MEDIA_BUS_FMT_SBGGR8_1X8:
437 	case MEDIA_BUS_FMT_SGBRG8_1X8:
438 	case MEDIA_BUS_FMT_SGRBG8_1X8:
439 	case MEDIA_BUS_FMT_SRGGB8_1X8:
440 	case MEDIA_BUS_FMT_SBGGR10_1X10:
441 	case MEDIA_BUS_FMT_SGBRG10_1X10:
442 	case MEDIA_BUS_FMT_SGRBG10_1X10:
443 	case MEDIA_BUS_FMT_SRGGB10_1X10:
444 	case MEDIA_BUS_FMT_SBGGR12_1X12:
445 	case MEDIA_BUS_FMT_SGBRG12_1X12:
446 	case MEDIA_BUS_FMT_SGRBG12_1X12:
447 	case MEDIA_BUS_FMT_SRGGB12_1X12:
448 	case MEDIA_BUS_FMT_SBGGR14_1X14:
449 	case MEDIA_BUS_FMT_SGBRG14_1X14:
450 	case MEDIA_BUS_FMT_SGRBG14_1X14:
451 	case MEDIA_BUS_FMT_SRGGB14_1X14:
452 	case MEDIA_BUS_FMT_SBGGR16_1X16:
453 	case MEDIA_BUS_FMT_SGBRG16_1X16:
454 	case MEDIA_BUS_FMT_SGRBG16_1X16:
455 	case MEDIA_BUS_FMT_SRGGB16_1X16:
456 		break;
457 	default:
458 		format->format.code = MEDIA_BUS_FMT_Y8_1X8;
459 		break;
460 	}
461 
462 	if (format->format.field == V4L2_FIELD_ANY)
463 		format->format.field = V4L2_FIELD_NONE;
464 
465 	*sink_fmt = format->format;
466 	*source_fmt = *sink_fmt;
467 
468 	return 0;
469 }
470 
471 static int mei_csi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
472 {
473 	struct mei_csi *csi = ctrl_to_csi(ctrl);
474 	s64 freq;
475 
476 	if (ctrl->id == V4L2_CID_LINK_FREQ) {
477 		if (!csi->remote)
478 			return -EINVAL;
479 
480 		freq = v4l2_get_link_freq(csi->remote->ctrl_handler, 0, 0);
481 		if (freq < 0) {
482 			dev_err(&csi->cldev->dev,
483 				"error %lld, invalid link_freq\n", freq);
484 			return -EINVAL;
485 		}
486 
487 		link_freq_menu_items[0] = freq;
488 		ctrl->val = 0;
489 
490 		return 0;
491 	}
492 
493 	return -EINVAL;
494 }
495 
496 static const struct v4l2_ctrl_ops mei_csi_ctrl_ops = {
497 	.g_volatile_ctrl = mei_csi_g_volatile_ctrl,
498 };
499 
500 static const struct v4l2_subdev_video_ops mei_csi_video_ops = {
501 	.s_stream = mei_csi_set_stream,
502 };
503 
504 static const struct v4l2_subdev_pad_ops mei_csi_pad_ops = {
505 	.get_fmt = v4l2_subdev_get_fmt,
506 	.set_fmt = mei_csi_set_fmt,
507 };
508 
509 static const struct v4l2_subdev_ops mei_csi_subdev_ops = {
510 	.video = &mei_csi_video_ops,
511 	.pad = &mei_csi_pad_ops,
512 };
513 
514 static const struct v4l2_subdev_internal_ops mei_csi_internal_ops = {
515 	.init_state = mei_csi_init_state,
516 };
517 
518 static const struct media_entity_operations mei_csi_entity_ops = {
519 	.link_validate = v4l2_subdev_link_validate,
520 };
521 
522 static int mei_csi_notify_bound(struct v4l2_async_notifier *notifier,
523 				struct v4l2_subdev *subdev,
524 				struct v4l2_async_connection *asd)
525 {
526 	struct mei_csi *csi = notifier_to_csi(notifier);
527 	int pad;
528 
529 	pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode,
530 					  MEDIA_PAD_FL_SOURCE);
531 	if (pad < 0)
532 		return pad;
533 
534 	csi->remote = subdev;
535 	csi->remote_pad = pad;
536 
537 	return media_create_pad_link(&subdev->entity, pad,
538 				     &csi->subdev.entity, CSI_PAD_SINK,
539 				     MEDIA_LNK_FL_ENABLED |
540 				     MEDIA_LNK_FL_IMMUTABLE);
541 }
542 
543 static void mei_csi_notify_unbind(struct v4l2_async_notifier *notifier,
544 				  struct v4l2_subdev *subdev,
545 				  struct v4l2_async_connection *asd)
546 {
547 	struct mei_csi *csi = notifier_to_csi(notifier);
548 
549 	csi->remote = NULL;
550 }
551 
552 static const struct v4l2_async_notifier_operations mei_csi_notify_ops = {
553 	.bound = mei_csi_notify_bound,
554 	.unbind = mei_csi_notify_unbind,
555 };
556 
557 static int mei_csi_init_controls(struct mei_csi *csi)
558 {
559 	u32 max;
560 	int ret;
561 
562 	ret = v4l2_ctrl_handler_init(&csi->ctrl_handler, 2);
563 	if (ret)
564 		return ret;
565 
566 	csi->ctrl_handler.lock = &csi->lock;
567 
568 	max = ARRAY_SIZE(link_freq_menu_items) - 1;
569 	csi->freq_ctrl = v4l2_ctrl_new_int_menu(&csi->ctrl_handler,
570 						&mei_csi_ctrl_ops,
571 						V4L2_CID_LINK_FREQ,
572 						max,
573 						0,
574 						link_freq_menu_items);
575 	if (csi->freq_ctrl)
576 		csi->freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY |
577 					 V4L2_CTRL_FLAG_VOLATILE;
578 
579 	csi->privacy_ctrl = v4l2_ctrl_new_std(&csi->ctrl_handler, NULL,
580 					      V4L2_CID_PRIVACY, 0, 1, 1, 0);
581 	if (csi->privacy_ctrl)
582 		csi->privacy_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
583 
584 	if (csi->ctrl_handler.error)
585 		return csi->ctrl_handler.error;
586 
587 	csi->subdev.ctrl_handler = &csi->ctrl_handler;
588 
589 	return 0;
590 }
591 
592 static int mei_csi_parse_firmware(struct mei_csi *csi)
593 {
594 	struct v4l2_fwnode_endpoint v4l2_ep = {
595 		.bus_type = V4L2_MBUS_CSI2_DPHY,
596 	};
597 	struct device *dev = &csi->cldev->dev;
598 	struct v4l2_async_connection *asd;
599 	struct fwnode_handle *sink_ep, *source_ep;
600 	int ret;
601 
602 	sink_ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0, 0);
603 	if (!sink_ep) {
604 		dev_err(dev, "can't obtain sink endpoint\n");
605 		return -EINVAL;
606 	}
607 
608 	v4l2_async_subdev_nf_init(&csi->notifier, &csi->subdev);
609 	csi->notifier.ops = &mei_csi_notify_ops;
610 
611 	ret = v4l2_fwnode_endpoint_parse(sink_ep, &v4l2_ep);
612 	if (ret) {
613 		dev_err(dev, "could not parse v4l2 sink endpoint\n");
614 		goto out_nf_cleanup;
615 	}
616 
617 	csi->nr_of_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
618 
619 	source_ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 1, 0, 0);
620 	if (!source_ep) {
621 		ret = -ENOTCONN;
622 		dev_err(dev, "can't obtain source endpoint\n");
623 		goto out_nf_cleanup;
624 	}
625 
626 	ret = v4l2_fwnode_endpoint_parse(source_ep, &v4l2_ep);
627 	fwnode_handle_put(source_ep);
628 	if (ret) {
629 		dev_err(dev, "could not parse v4l2 source endpoint\n");
630 		goto out_nf_cleanup;
631 	}
632 
633 	if (csi->nr_of_lanes != v4l2_ep.bus.mipi_csi2.num_data_lanes) {
634 		ret = -EINVAL;
635 		dev_err(dev,
636 			"the number of lanes does not match (%u vs. %u)\n",
637 			csi->nr_of_lanes, v4l2_ep.bus.mipi_csi2.num_data_lanes);
638 		goto out_nf_cleanup;
639 	}
640 
641 	asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, sink_ep,
642 					      struct v4l2_async_connection);
643 	if (IS_ERR(asd)) {
644 		ret = PTR_ERR(asd);
645 		goto out_nf_cleanup;
646 	}
647 
648 	ret = v4l2_async_nf_register(&csi->notifier);
649 	if (ret)
650 		goto out_nf_cleanup;
651 
652 	fwnode_handle_put(sink_ep);
653 
654 	return 0;
655 
656 out_nf_cleanup:
657 	v4l2_async_nf_cleanup(&csi->notifier);
658 	fwnode_handle_put(sink_ep);
659 
660 	return ret;
661 }
662 
663 static int mei_csi_probe(struct mei_cl_device *cldev,
664 			 const struct mei_cl_device_id *id)
665 {
666 	struct device *dev = &cldev->dev;
667 	struct pci_dev *ipu;
668 	struct mei_csi *csi;
669 	unsigned int i;
670 	int ret;
671 
672 	for (i = 0, ipu = NULL; !ipu && ipu6_pci_tbl[i].vendor; i++)
673 		ipu = pci_get_device(ipu6_pci_tbl[i].vendor,
674 				     ipu6_pci_tbl[i].device, NULL);
675 
676 	if (!ipu)
677 		return -ENODEV;
678 
679 	ret = ipu_bridge_init(&ipu->dev, ipu_bridge_parse_ssdb);
680 	put_device(&ipu->dev);
681 	if (ret < 0)
682 		return ret;
683 	if (!dev_fwnode(dev)) {
684 		dev_err(dev, "mei-csi probed without device fwnode!\n");
685 		return -ENXIO;
686 	}
687 
688 	csi = devm_kzalloc(dev, sizeof(struct mei_csi), GFP_KERNEL);
689 	if (!csi)
690 		return -ENOMEM;
691 
692 	csi->cldev = cldev;
693 	mutex_init(&csi->lock);
694 	init_completion(&csi->cmd_completion);
695 
696 	mei_cldev_set_drvdata(cldev, csi);
697 
698 	ret = mei_cldev_enable(cldev);
699 	if (ret < 0) {
700 		dev_err(dev, "mei_cldev_enable failed: %d\n", ret);
701 		goto destroy_mutex;
702 	}
703 
704 	ret = mei_cldev_register_rx_cb(cldev, mei_csi_rx);
705 	if (ret) {
706 		dev_err(dev, "event cb registration failed: %d\n", ret);
707 		goto err_disable;
708 	}
709 
710 	ret = mei_csi_parse_firmware(csi);
711 	if (ret)
712 		goto err_disable;
713 
714 	csi->subdev.dev = &cldev->dev;
715 	csi->subdev.state_lock = &csi->lock;
716 	v4l2_subdev_init(&csi->subdev, &mei_csi_subdev_ops);
717 	csi->subdev.internal_ops = &mei_csi_internal_ops;
718 	v4l2_set_subdevdata(&csi->subdev, csi);
719 	csi->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE |
720 			    V4L2_SUBDEV_FL_HAS_EVENTS;
721 	csi->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
722 	csi->subdev.entity.ops = &mei_csi_entity_ops;
723 
724 	snprintf(csi->subdev.name, sizeof(csi->subdev.name),
725 		 MEI_CSI_ENTITY_NAME);
726 
727 	ret = mei_csi_init_controls(csi);
728 	if (ret)
729 		goto err_ctrl_handler;
730 
731 	csi->pads[CSI_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
732 	csi->pads[CSI_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
733 	ret = media_entity_pads_init(&csi->subdev.entity, CSI_NUM_PADS,
734 				     csi->pads);
735 	if (ret)
736 		goto err_ctrl_handler;
737 
738 	ret = v4l2_subdev_init_finalize(&csi->subdev);
739 	if (ret < 0)
740 		goto err_entity;
741 
742 	ret = v4l2_async_register_subdev(&csi->subdev);
743 	if (ret < 0)
744 		goto err_subdev;
745 
746 	pm_runtime_enable(&cldev->dev);
747 
748 	return 0;
749 
750 err_subdev:
751 	v4l2_subdev_cleanup(&csi->subdev);
752 
753 err_entity:
754 	media_entity_cleanup(&csi->subdev.entity);
755 
756 err_ctrl_handler:
757 	v4l2_ctrl_handler_free(&csi->ctrl_handler);
758 	v4l2_async_nf_unregister(&csi->notifier);
759 	v4l2_async_nf_cleanup(&csi->notifier);
760 
761 err_disable:
762 	mei_cldev_disable(cldev);
763 
764 destroy_mutex:
765 	mutex_destroy(&csi->lock);
766 
767 	return ret;
768 }
769 
770 static void mei_csi_remove(struct mei_cl_device *cldev)
771 {
772 	struct mei_csi *csi = mei_cldev_get_drvdata(cldev);
773 
774 	v4l2_async_nf_unregister(&csi->notifier);
775 	v4l2_async_nf_cleanup(&csi->notifier);
776 	v4l2_ctrl_handler_free(&csi->ctrl_handler);
777 	v4l2_async_unregister_subdev(&csi->subdev);
778 	v4l2_subdev_cleanup(&csi->subdev);
779 	media_entity_cleanup(&csi->subdev.entity);
780 
781 	pm_runtime_disable(&cldev->dev);
782 
783 	mutex_destroy(&csi->lock);
784 }
785 
786 #define MEI_CSI_UUID UUID_LE(0x92335FCF, 0x3203, 0x4472, \
787 			     0xAF, 0x93, 0x7b, 0x44, 0x53, 0xAC, 0x29, 0xDA)
788 
789 static const struct mei_cl_device_id mei_csi_tbl[] = {
790 	{ .uuid = MEI_CSI_UUID, .version = MEI_CL_VERSION_ANY },
791 	{ /* sentinel */ }
792 };
793 MODULE_DEVICE_TABLE(mei, mei_csi_tbl);
794 
795 static struct mei_cl_driver mei_csi_driver = {
796 	.id_table = mei_csi_tbl,
797 	.name = KBUILD_MODNAME,
798 
799 	.probe = mei_csi_probe,
800 	.remove = mei_csi_remove,
801 };
802 
803 module_mei_cl_driver(mei_csi_driver);
804 
805 MODULE_IMPORT_NS(INTEL_IPU_BRIDGE);
806 MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
807 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
808 MODULE_DESCRIPTION("Device driver for IVSC CSI");
809 MODULE_LICENSE("GPL");
810