xref: /linux/drivers/media/pci/intel/ipu-bridge.c (revision 007b61981aa970d314a6042cedcf7ef2cf34bf23)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3 
4 #include <linux/acpi.h>
5 #include <acpi/acpi_bus.h>
6 #include <linux/cleanup.h>
7 #include <linux/device.h>
8 #include <linux/dmi.h>
9 #include <linux/i2c.h>
10 #include <linux/mei_cl_bus.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/property.h>
14 #include <linux/string.h>
15 #include <linux/workqueue.h>
16 
17 #include <media/ipu-bridge.h>
18 #include <media/v4l2-fwnode.h>
19 
20 #define ADEV_DEV(adev) ACPI_PTR(&((adev)->dev))
21 
22 /*
23  * 92335fcf-3203-4472-af93-7b4453ac29da
24  *
25  * Used to build MEI CSI device name to lookup MEI CSI device by
26  * device_find_child_by_name().
27  */
28 #define MEI_CSI_UUID							\
29 	UUID_LE(0x92335FCF, 0x3203, 0x4472,				\
30 		0xAF, 0x93, 0x7B, 0x44, 0x53, 0xAC, 0x29, 0xDA)
31 
32 /*
33  * IVSC device name
34  *
35  * Used to match IVSC device by ipu_bridge_match_ivsc_dev()
36  */
37 #define IVSC_DEV_NAME "intel_vsc"
38 
39 /*
40  * Extend this array with ACPI Hardware IDs of devices known to be working
41  * plus the number of link-frequencies expected by their drivers, along with
42  * the frequency values in hertz. This is somewhat opportunistic way of adding
43  * support for this for now in the hopes of a better source for the information
44  * (possibly some encoded value in the SSDB buffer that we're unaware of)
45  * becoming apparent in the future.
46  *
47  * Do not add an entry for a sensor that is not actually supported.
48  *
49  * Please keep the list sorted by ACPI HID.
50  */
51 static const struct ipu_sensor_config ipu_supported_sensors[] = {
52 	/* Himax HM11B1 */
53 	IPU_SENSOR_CONFIG("HIMX11B1", 1, 384000000),
54 	/* Himax HM2170 */
55 	IPU_SENSOR_CONFIG("HIMX2170", 1, 384000000),
56 	/* Himax HM2172 */
57 	IPU_SENSOR_CONFIG("HIMX2172", 1, 384000000),
58 	/* GalaxyCore GC0310 */
59 	IPU_SENSOR_CONFIG("INT0310", 1, 55692000),
60 	/* Omnivision OV5693 */
61 	IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
62 	/* Onsemi MT9M114 */
63 	IPU_SENSOR_CONFIG("INT33F0", 1, 384000000),
64 	/* Omnivision OV2740 */
65 	IPU_SENSOR_CONFIG("INT3474", 1, 180000000),
66 	/* Omnivision OV5670 */
67 	IPU_SENSOR_CONFIG("INT3479", 1, 422400000),
68 	/* Omnivision OV8865 */
69 	IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
70 	/* Omnivision OV7251 */
71 	IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
72 	/* Hynix Hi-556 */
73 	IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
74 	/* Lontium lt6911uxe */
75 	IPU_SENSOR_CONFIG("INTC10C5", 0),
76 	/* Omnivision OV01A10 / OV01A1S */
77 	IPU_SENSOR_CONFIG("OVTI01A0", 1, 400000000),
78 	IPU_SENSOR_CONFIG("OVTI01AS", 1, 400000000),
79 	/* Omnivision OV02C10 */
80 	IPU_SENSOR_CONFIG("OVTI02C1", 1, 400000000),
81 	/* Omnivision OV02E10 */
82 	IPU_SENSOR_CONFIG("OVTI02E1", 1, 360000000),
83 	/* Omnivision ov05c10 */
84 	IPU_SENSOR_CONFIG("OVTI05C1", 1, 480000000),
85 	/* Omnivision OV08A10 */
86 	IPU_SENSOR_CONFIG("OVTI08A1", 1, 500000000),
87 	/* Omnivision OV08x40 */
88 	IPU_SENSOR_CONFIG("OVTI08F4", 3, 400000000, 749000000, 800000000),
89 	/* Omnivision OV13B10 */
90 	IPU_SENSOR_CONFIG("OVTI13B1", 1, 560000000),
91 	IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
92 	/* Omnivision OV2680 */
93 	IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000),
94 	/* Omnivision OV5675 */
95 	IPU_SENSOR_CONFIG("OVTI5675", 1, 450000000),
96 	/* Omnivision OV8856 */
97 	IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
98 	/* Sony IMX471 */
99 	IPU_SENSOR_CONFIG("SONY471A", 1, 200000000),
100 	/* Sony IMX471 (found on Lenovo X1 Carbon G14) */
101 	IPU_SENSOR_CONFIG("TBE20A0", 1, 200000000),
102 	/* Toshiba T4KA3 */
103 	IPU_SENSOR_CONFIG("XMCC0003", 1, 321468000),
104 };
105 
106 /*
107  * DMI matches for laptops which have their sensor mounted upside-down
108  * without reporting a rotation of 180° in neither the SSDB nor the _PLD.
109  */
110 static const struct dmi_system_id upside_down_sensor_dmi_ids[] = {
111 	{
112 		.matches = {
113 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9340"),
115 		},
116 		.driver_data = "OVTI02C1",
117 	},
118 	{
119 		.matches = {
120 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
121 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9350"),
122 		},
123 		.driver_data = "OVTI02C1",
124 	},
125 	{
126 		.matches = {
127 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 14 9440"),
129 		},
130 		.driver_data = "OVTI02C1",
131 	},
132 	{
133 		.matches = {
134 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
135 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 16 9640"),
136 		},
137 		.driver_data = "OVTI02C1",
138 	},
139 	{
140 		.matches = {
141 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
142 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 16 (Dell 16 Premium) DA16250"),
143 		},
144 		.driver_data = "OVTI02C1",
145 	},
146 	/*
147 	 * The first four characters of DMI_BOARD_NAME identify the Lenovo
148 	 * machine type/model. For example, a DMI_BOARD_NAME starting with
149 	 * "21Q6" indicates a ThinkPad X9-15.
150 	 *
151 	 * Reference: https://psref.lenovo.com/
152 	 */
153 	{
154 		/* Lenovo X9-14 */
155 		.matches = {
156 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
157 			DMI_MATCH(DMI_BOARD_NAME, "21QA"),
158 		},
159 		.driver_data = "SONY471A",
160 	},
161 	{
162 		/* Lenovo X9-14 */
163 		.matches = {
164 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
165 			DMI_MATCH(DMI_BOARD_NAME, "21QB"),
166 		},
167 		.driver_data = "SONY471A",
168 	},
169 	{
170 		/* Lenovo X9-15 */
171 		.matches = {
172 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
173 			DMI_MATCH(DMI_BOARD_NAME, "21Q6"),
174 		},
175 		.driver_data = "SONY471A",
176 	},
177 	{
178 		/* Lenovo X9-15 */
179 		.matches = {
180 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
181 			DMI_MATCH(DMI_BOARD_NAME, "21Q7"),
182 		},
183 		.driver_data = "SONY471A",
184 	},
185 	{} /* Terminating entry */
186 };
187 
188 static const struct ipu_property_names prop_names = {
189 	.clock_frequency = "clock-frequency",
190 	.rotation = "rotation",
191 	.orientation = "orientation",
192 	.bus_type = "bus-type",
193 	.data_lanes = "data-lanes",
194 	.remote_endpoint = "remote-endpoint",
195 	.link_frequencies = "link-frequencies",
196 };
197 
198 static const char * const ipu_vcm_types[] = {
199 	"ad5823",
200 	"dw9714",
201 	"ad5816",
202 	"dw9719",
203 	"dw9718",
204 	"dw9806b",
205 	"wv517s",
206 	"lc898122xa",
207 	"lc898212axb",
208 };
209 
210 /*
211  * Used to figure out IVSC acpi device by ipu_bridge_get_ivsc_acpi_dev()
212  * instead of device and driver match to probe IVSC device.
213  */
214 static const struct acpi_device_id ivsc_acpi_ids[] = {
215 	{ "INTC1059" },
216 	{ "INTC1095" },
217 	{ "INTC100A" },
218 	{ "INTC10CF" },
219 	{ "INTC10DE" }, /* LNL */
220 	{ "INTC10E0" }, /* ARL */
221 	{ "INTC10E1" }, /* PTL */
222 	{ "INTC10FA" }, /* NVL */
223 };
224 
225 static struct acpi_device *ipu_bridge_get_ivsc_acpi_dev(struct acpi_device *adev)
226 {
227 	unsigned int i;
228 
229 	for (i = 0; i < ARRAY_SIZE(ivsc_acpi_ids); i++) {
230 		const struct acpi_device_id *acpi_id = &ivsc_acpi_ids[i];
231 		struct acpi_device *consumer, *ivsc_adev;
232 		acpi_handle handle = acpi_device_handle(ACPI_PTR(adev));
233 
234 		for_each_acpi_dev_match(ivsc_adev, acpi_id->id, NULL, -1)
235 			/* camera sensor depends on IVSC in DSDT if exist */
236 			for_each_acpi_consumer_dev(ivsc_adev, consumer)
237 				if (ACPI_PTR(consumer->handle) == handle) {
238 					acpi_dev_put(consumer);
239 					return ivsc_adev;
240 				}
241 	}
242 
243 	return NULL;
244 }
245 
246 static int ipu_bridge_match_ivsc_dev(struct device *dev, const void *adev)
247 {
248 	if (ACPI_COMPANION(dev) != adev)
249 		return 0;
250 
251 	if (!sysfs_streq(dev_name(dev), IVSC_DEV_NAME))
252 		return 0;
253 
254 	return 1;
255 }
256 
257 static struct device *ipu_bridge_get_ivsc_csi_dev(struct acpi_device *adev)
258 {
259 	struct device *dev, *csi_dev;
260 	uuid_le uuid = MEI_CSI_UUID;
261 	char name[64];
262 
263 	/* IVSC device on platform bus */
264 	dev = bus_find_device(&platform_bus_type, NULL, adev,
265 			      ipu_bridge_match_ivsc_dev);
266 	if (dev) {
267 		snprintf(name, sizeof(name), "%s-%pUl", dev_name(dev), &uuid);
268 
269 		csi_dev = device_find_child_by_name(dev, name);
270 
271 		put_device(dev);
272 
273 		return csi_dev;
274 	}
275 
276 	/* Try to locate CVS device on the I2C bus */
277 	csi_dev = bus_find_device_by_acpi_dev(&i2c_bus_type, adev);
278 	if (csi_dev)
279 		return csi_dev;
280 
281 	/* Fallback to platform bus for CVS device */
282 	return bus_find_device_by_acpi_dev(&platform_bus_type, adev);
283 }
284 
285 static int ipu_bridge_check_ivsc_dev(struct ipu_sensor *sensor,
286 				     struct acpi_device *sensor_adev)
287 {
288 	struct acpi_device *adev;
289 	struct device *csi_dev;
290 
291 	adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev);
292 	if (adev) {
293 		csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
294 		if (!csi_dev) {
295 			acpi_dev_put(adev);
296 			dev_err(ADEV_DEV(adev), "Failed to find MEI or CVS CSI dev\n");
297 			return -ENODEV;
298 		}
299 
300 		sensor->csi_dev = csi_dev;
301 		sensor->ivsc_adev = adev;
302 	}
303 
304 	return 0;
305 }
306 
307 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
308 				       void *data, u32 size)
309 {
310 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
311 	union acpi_object *obj;
312 	acpi_status status;
313 	int ret = 0;
314 
315 	status = acpi_evaluate_object(ACPI_PTR(adev->handle),
316 				      id, NULL, &buffer);
317 	if (ACPI_FAILURE(status))
318 		return -ENODEV;
319 
320 	obj = buffer.pointer;
321 	if (!obj) {
322 		dev_err(ADEV_DEV(adev), "Couldn't locate ACPI buffer\n");
323 		return -ENODEV;
324 	}
325 
326 	if (obj->type != ACPI_TYPE_BUFFER) {
327 		dev_err(ADEV_DEV(adev), "Not an ACPI buffer\n");
328 		ret = -ENODEV;
329 		goto out_free_buff;
330 	}
331 
332 	if (obj->buffer.length > size) {
333 		dev_err(ADEV_DEV(adev), "Given buffer is too small\n");
334 		ret = -EINVAL;
335 		goto out_free_buff;
336 	}
337 
338 	memcpy(data, obj->buffer.pointer, obj->buffer.length);
339 
340 out_free_buff:
341 	kfree(buffer.pointer);
342 	return ret;
343 }
344 
345 static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,
346 				     struct ipu_sensor_ssdb *ssdb)
347 {
348 	const struct dmi_system_id *dmi_id;
349 
350 	dmi_id = dmi_first_match(upside_down_sensor_dmi_ids);
351 	if (dmi_id && acpi_dev_hid_match(adev, dmi_id->driver_data))
352 		return 180;
353 
354 	switch (ssdb->degree) {
355 	case IPU_SENSOR_ROTATION_NORMAL:
356 		return 0;
357 	case IPU_SENSOR_ROTATION_INVERTED:
358 		return 180;
359 	default:
360 		dev_warn(ADEV_DEV(adev),
361 			 "Unknown rotation %d. Assume 0 degree rotation\n",
362 			 ssdb->degree);
363 		return 0;
364 	}
365 }
366 
367 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
368 {
369 	enum v4l2_fwnode_orientation orientation;
370 	struct acpi_pld_info *pld = NULL;
371 
372 	if (!acpi_get_physical_device_location(ACPI_PTR(adev->handle), &pld)) {
373 		dev_warn(ADEV_DEV(adev), "_PLD call failed, using default orientation\n");
374 		return V4L2_FWNODE_ORIENTATION_EXTERNAL;
375 	}
376 
377 	switch (pld->panel) {
378 	case ACPI_PLD_PANEL_FRONT:
379 		orientation = V4L2_FWNODE_ORIENTATION_FRONT;
380 		break;
381 	case ACPI_PLD_PANEL_BACK:
382 		orientation = V4L2_FWNODE_ORIENTATION_BACK;
383 		break;
384 	case ACPI_PLD_PANEL_TOP:
385 	case ACPI_PLD_PANEL_LEFT:
386 	case ACPI_PLD_PANEL_RIGHT:
387 	case ACPI_PLD_PANEL_UNKNOWN:
388 		orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
389 		break;
390 	default:
391 		dev_warn(ADEV_DEV(adev), "Unknown _PLD panel val %d\n",
392 			 pld->panel);
393 		orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
394 		break;
395 	}
396 
397 	ACPI_FREE(pld);
398 	return orientation;
399 }
400 
401 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
402 {
403 	struct ipu_sensor_ssdb ssdb = {};
404 	int ret;
405 
406 	ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
407 	if (ret)
408 		return ret;
409 
410 	if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
411 		dev_warn(ADEV_DEV(adev), "Unknown VCM type %d\n", ssdb.vcmtype);
412 		ssdb.vcmtype = 0;
413 	}
414 
415 	if (ssdb.lanes > IPU_MAX_LANES) {
416 		dev_err(ADEV_DEV(adev), "Number of lanes in SSDB is invalid\n");
417 		return -EINVAL;
418 	}
419 
420 	sensor->link = ssdb.link;
421 	sensor->lanes = ssdb.lanes;
422 	sensor->mclkspeed = ssdb.mclkspeed;
423 	sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb);
424 	sensor->orientation = ipu_bridge_parse_orientation(adev);
425 
426 	if (ssdb.vcmtype)
427 		sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
428 
429 	return 0;
430 }
431 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, "INTEL_IPU_BRIDGE");
432 
433 static void ipu_bridge_create_fwnode_properties(
434 	struct ipu_sensor *sensor,
435 	struct ipu_bridge *bridge,
436 	const struct ipu_sensor_config *cfg)
437 {
438 	struct ipu_property_names *names = &sensor->prop_names;
439 	struct software_node *nodes = sensor->swnodes;
440 
441 	sensor->prop_names = prop_names;
442 
443 	if (sensor->csi_dev) {
444 		sensor->local_ref[0] =
445 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_SENSOR_ENDPOINT]);
446 		sensor->remote_ref[0] =
447 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_IPU_ENDPOINT]);
448 		sensor->ivsc_sensor_ref[0] =
449 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]);
450 		sensor->ivsc_ipu_ref[0] =
451 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]);
452 
453 		sensor->ivsc_sensor_ep_properties[0] =
454 			PROPERTY_ENTRY_U32(names->bus_type,
455 					   V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
456 		sensor->ivsc_sensor_ep_properties[1] =
457 			PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes,
458 						     bridge->data_lanes,
459 						     sensor->lanes);
460 		sensor->ivsc_sensor_ep_properties[2] =
461 			PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint,
462 						 sensor->ivsc_sensor_ref);
463 
464 		sensor->ivsc_ipu_ep_properties[0] =
465 			PROPERTY_ENTRY_U32(names->bus_type,
466 					   V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
467 		sensor->ivsc_ipu_ep_properties[1] =
468 			PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes,
469 						     bridge->data_lanes,
470 						     sensor->lanes);
471 		sensor->ivsc_ipu_ep_properties[2] =
472 			PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint,
473 						 sensor->ivsc_ipu_ref);
474 	} else {
475 		sensor->local_ref[0] =
476 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]);
477 		sensor->remote_ref[0] =
478 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]);
479 	}
480 
481 	sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
482 					sensor->prop_names.clock_frequency,
483 					sensor->mclkspeed);
484 	sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
485 					sensor->prop_names.rotation,
486 					sensor->rotation);
487 	sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
488 					sensor->prop_names.orientation,
489 					sensor->orientation);
490 	if (sensor->vcm_type) {
491 		sensor->vcm_ref[0] =
492 			SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
493 		sensor->dev_properties[3] =
494 			PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
495 	}
496 
497 	sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
498 					sensor->prop_names.bus_type,
499 					V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
500 	sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
501 					sensor->prop_names.data_lanes,
502 					bridge->data_lanes, sensor->lanes);
503 	sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
504 					sensor->prop_names.remote_endpoint,
505 					sensor->local_ref);
506 
507 	if (cfg->nr_link_freqs > 0)
508 		sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
509 			sensor->prop_names.link_frequencies,
510 			cfg->link_freqs,
511 			cfg->nr_link_freqs);
512 
513 	sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
514 					sensor->prop_names.data_lanes,
515 					bridge->data_lanes, sensor->lanes);
516 	sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
517 					sensor->prop_names.remote_endpoint,
518 					sensor->remote_ref);
519 }
520 
521 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
522 {
523 	snprintf(sensor->node_names.remote_port,
524 		 sizeof(sensor->node_names.remote_port),
525 		 SWNODE_GRAPH_PORT_NAME_FMT, sensor->link);
526 	snprintf(sensor->node_names.port,
527 		 sizeof(sensor->node_names.port),
528 		 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
529 	snprintf(sensor->node_names.endpoint,
530 		 sizeof(sensor->node_names.endpoint),
531 		 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
532 	if (sensor->vcm_type) {
533 		/* append link to distinguish nodes with same model VCM */
534 		snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
535 			 "%s-%u", sensor->vcm_type, sensor->link);
536 	}
537 
538 	if (sensor->csi_dev) {
539 		snprintf(sensor->node_names.ivsc_sensor_port,
540 			 sizeof(sensor->node_names.ivsc_sensor_port),
541 			 SWNODE_GRAPH_PORT_NAME_FMT, 0);
542 		snprintf(sensor->node_names.ivsc_ipu_port,
543 			 sizeof(sensor->node_names.ivsc_ipu_port),
544 			 SWNODE_GRAPH_PORT_NAME_FMT, 1);
545 	}
546 }
547 
548 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
549 {
550 	struct software_node *nodes = sensor->swnodes;
551 
552 	sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
553 	sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
554 	sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
555 	sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
556 	sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
557 	if (sensor->vcm_type)
558 		sensor->group[SWNODE_VCM] =  &nodes[SWNODE_VCM];
559 
560 	if (sensor->csi_dev) {
561 		sensor->group[SWNODE_IVSC_HID] =
562 					&nodes[SWNODE_IVSC_HID];
563 		sensor->group[SWNODE_IVSC_SENSOR_PORT] =
564 					&nodes[SWNODE_IVSC_SENSOR_PORT];
565 		sensor->group[SWNODE_IVSC_SENSOR_ENDPOINT] =
566 					&nodes[SWNODE_IVSC_SENSOR_ENDPOINT];
567 		sensor->group[SWNODE_IVSC_IPU_PORT] =
568 					&nodes[SWNODE_IVSC_IPU_PORT];
569 		sensor->group[SWNODE_IVSC_IPU_ENDPOINT] =
570 					&nodes[SWNODE_IVSC_IPU_ENDPOINT];
571 
572 		if (sensor->vcm_type)
573 			sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM];
574 	} else {
575 		if (sensor->vcm_type)
576 			sensor->group[SWNODE_IVSC_HID] = &nodes[SWNODE_VCM];
577 	}
578 }
579 
580 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
581 						 struct ipu_sensor *sensor)
582 {
583 	struct ipu_node_names *names = &sensor->node_names;
584 	struct software_node *nodes = sensor->swnodes;
585 
586 	ipu_bridge_init_swnode_names(sensor);
587 
588 	nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
589 					       sensor->dev_properties);
590 	nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
591 					      &nodes[SWNODE_SENSOR_HID]);
592 	nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
593 						sensor->node_names.endpoint,
594 						&nodes[SWNODE_SENSOR_PORT],
595 						sensor->ep_properties);
596 	nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
597 					   &bridge->ipu_hid_node);
598 	nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
599 						sensor->node_names.endpoint,
600 						&nodes[SWNODE_IPU_PORT],
601 						sensor->ipu_properties);
602 
603 	if (sensor->csi_dev) {
604 		const char *device_hid = "";
605 
606 		device_hid = acpi_device_hid(sensor->ivsc_adev);
607 
608 		snprintf(sensor->ivsc_name, sizeof(sensor->ivsc_name), "%s-%u",
609 			 device_hid, sensor->link);
610 
611 		nodes[SWNODE_IVSC_HID] = NODE_SENSOR(sensor->ivsc_name,
612 						     sensor->ivsc_properties);
613 		nodes[SWNODE_IVSC_SENSOR_PORT] =
614 				NODE_PORT(names->ivsc_sensor_port,
615 					  &nodes[SWNODE_IVSC_HID]);
616 		nodes[SWNODE_IVSC_SENSOR_ENDPOINT] =
617 				NODE_ENDPOINT(names->endpoint,
618 					      &nodes[SWNODE_IVSC_SENSOR_PORT],
619 					      sensor->ivsc_sensor_ep_properties);
620 		nodes[SWNODE_IVSC_IPU_PORT] =
621 				NODE_PORT(names->ivsc_ipu_port,
622 					  &nodes[SWNODE_IVSC_HID]);
623 		nodes[SWNODE_IVSC_IPU_ENDPOINT] =
624 				NODE_ENDPOINT(names->endpoint,
625 					      &nodes[SWNODE_IVSC_IPU_PORT],
626 					      sensor->ivsc_ipu_ep_properties);
627 	}
628 
629 	nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
630 
631 	ipu_bridge_init_swnode_group(sensor);
632 }
633 
634 /*
635  * The actual instantiation must be done from a workqueue to avoid
636  * a deadlock on taking list_lock from v4l2-async twice.
637  */
638 struct ipu_bridge_instantiate_vcm_work_data {
639 	struct work_struct work;
640 	struct device *sensor;
641 	char name[16];
642 	struct i2c_board_info board_info;
643 };
644 
645 static void ipu_bridge_instantiate_vcm_work(struct work_struct *work)
646 {
647 	struct ipu_bridge_instantiate_vcm_work_data *data =
648 		container_of(work, struct ipu_bridge_instantiate_vcm_work_data,
649 			     work);
650 	struct acpi_device *adev = ACPI_COMPANION(data->sensor);
651 	struct i2c_client *vcm_client;
652 	bool put_fwnode = true;
653 	int ret;
654 
655 	/*
656 	 * The client may get probed before the device_link gets added below
657 	 * make sure the sensor is powered-up during probe.
658 	 */
659 	ret = pm_runtime_get_sync(data->sensor);
660 	if (ret < 0) {
661 		dev_err(data->sensor, "Error %d runtime-resuming sensor, cannot instantiate VCM\n",
662 			ret);
663 		goto out_pm_put;
664 	}
665 
666 	/*
667 	 * Note the client is created only once and then kept around
668 	 * even after a rmmod, just like the software-nodes.
669 	 */
670 	vcm_client = i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(adev),
671 						   1, &data->board_info);
672 	if (IS_ERR(vcm_client)) {
673 		dev_err(data->sensor, "Error instantiating VCM client: %pe\n",
674 			vcm_client);
675 		goto out_pm_put;
676 	}
677 
678 	device_link_add(&vcm_client->dev, data->sensor, DL_FLAG_PM_RUNTIME);
679 
680 	dev_info(data->sensor, "Instantiated %s VCM\n", data->board_info.type);
681 	put_fwnode = false; /* Ownership has passed to the i2c-client */
682 
683 out_pm_put:
684 	pm_runtime_put(data->sensor);
685 	put_device(data->sensor);
686 	if (put_fwnode)
687 		fwnode_handle_put(data->board_info.fwnode);
688 	kfree(data);
689 }
690 
691 int ipu_bridge_instantiate_vcm(struct device *sensor)
692 {
693 	struct ipu_bridge_instantiate_vcm_work_data *data;
694 	struct fwnode_handle *vcm_fwnode;
695 	struct i2c_client *vcm_client;
696 	struct acpi_device *adev;
697 	char *sep;
698 
699 	adev = ACPI_COMPANION(sensor);
700 	if (!adev)
701 		return 0;
702 
703 	vcm_fwnode = fwnode_find_reference(dev_fwnode(sensor), "lens-focus", 0);
704 	if (IS_ERR(vcm_fwnode))
705 		return 0;
706 
707 	/* When reloading modules the client will already exist */
708 	vcm_client = i2c_find_device_by_fwnode(vcm_fwnode);
709 	if (vcm_client) {
710 		fwnode_handle_put(vcm_fwnode);
711 		put_device(&vcm_client->dev);
712 		return 0;
713 	}
714 
715 	data = kzalloc_obj(*data);
716 	if (!data) {
717 		fwnode_handle_put(vcm_fwnode);
718 		return -ENOMEM;
719 	}
720 
721 	INIT_WORK(&data->work, ipu_bridge_instantiate_vcm_work);
722 	data->sensor = get_device(sensor);
723 	snprintf(data->name, sizeof(data->name), "%s-VCM",
724 		 acpi_dev_name(adev));
725 	data->board_info.dev_name = data->name;
726 	data->board_info.fwnode = vcm_fwnode;
727 	snprintf(data->board_info.type, sizeof(data->board_info.type),
728 		 "%pfwP", vcm_fwnode);
729 	/* Strip "-<link>" postfix */
730 	sep = strchrnul(data->board_info.type, '-');
731 	*sep = 0;
732 
733 	queue_work(system_long_wq, &data->work);
734 
735 	return 0;
736 }
737 EXPORT_SYMBOL_NS_GPL(ipu_bridge_instantiate_vcm, "INTEL_IPU_BRIDGE");
738 
739 static int ipu_bridge_instantiate_ivsc(struct ipu_sensor *sensor)
740 {
741 	struct fwnode_handle *fwnode;
742 
743 	if (!sensor->csi_dev)
744 		return 0;
745 
746 	fwnode = software_node_fwnode(&sensor->swnodes[SWNODE_IVSC_HID]);
747 	if (!fwnode)
748 		return -ENODEV;
749 
750 	set_secondary_fwnode(sensor->csi_dev, fwnode);
751 
752 	return 0;
753 }
754 
755 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
756 {
757 	struct ipu_sensor *sensor;
758 	unsigned int i;
759 
760 	for (i = 0; i < bridge->n_sensors; i++) {
761 		sensor = &bridge->sensors[i];
762 		software_node_unregister_node_group(sensor->group);
763 		acpi_dev_put(sensor->adev);
764 		put_device(sensor->csi_dev);
765 		acpi_dev_put(sensor->ivsc_adev);
766 	}
767 }
768 
769 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
770 				     struct ipu_bridge *bridge)
771 {
772 	struct fwnode_handle *fwnode, *primary;
773 	struct ipu_sensor *sensor;
774 	struct acpi_device *adev = NULL;
775 	int ret;
776 
777 	for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
778 		if (!ACPI_PTR(adev->status.enabled))
779 			continue;
780 
781 		if (bridge->n_sensors >= IPU_MAX_PORTS) {
782 			acpi_dev_put(adev);
783 			dev_err(bridge->dev, "Exceeded available IPU ports\n");
784 			return -EINVAL;
785 		}
786 
787 		sensor = &bridge->sensors[bridge->n_sensors];
788 
789 		ret = bridge->parse_sensor_fwnode(adev, sensor);
790 		if (ret)
791 			goto err_put_adev;
792 
793 		snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
794 			 cfg->hid, sensor->link);
795 
796 		ret = ipu_bridge_check_ivsc_dev(sensor, adev);
797 		if (ret)
798 			goto err_put_adev;
799 
800 		ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
801 		ipu_bridge_create_connection_swnodes(bridge, sensor);
802 
803 		ret = software_node_register_node_group(sensor->group);
804 		if (ret)
805 			goto err_put_ivsc;
806 
807 		fwnode = software_node_fwnode(&sensor->swnodes[
808 						      SWNODE_SENSOR_HID]);
809 		if (!fwnode) {
810 			ret = -ENODEV;
811 			goto err_free_swnodes;
812 		}
813 
814 		sensor->adev = ACPI_PTR(acpi_dev_get(adev));
815 
816 		primary = acpi_fwnode_handle(adev);
817 		primary->secondary = fwnode;
818 
819 		ret = ipu_bridge_instantiate_ivsc(sensor);
820 		if (ret)
821 			goto err_free_swnodes;
822 
823 		dev_info(bridge->dev, "Found supported sensor %s\n",
824 			 acpi_dev_name(adev));
825 
826 		bridge->n_sensors++;
827 	}
828 
829 	return 0;
830 
831 err_free_swnodes:
832 	software_node_unregister_node_group(sensor->group);
833 err_put_ivsc:
834 	put_device(sensor->csi_dev);
835 	acpi_dev_put(sensor->ivsc_adev);
836 err_put_adev:
837 	acpi_dev_put(adev);
838 	return ret;
839 }
840 
841 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
842 {
843 	unsigned int i;
844 	int ret;
845 
846 	for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
847 		const struct ipu_sensor_config *cfg =
848 			&ipu_supported_sensors[i];
849 
850 		ret = ipu_bridge_connect_sensor(cfg, bridge);
851 		if (ret)
852 			goto err_unregister_sensors;
853 	}
854 
855 	return 0;
856 
857 err_unregister_sensors:
858 	ipu_bridge_unregister_sensors(bridge);
859 	return ret;
860 }
861 
862 static int ipu_bridge_ivsc_is_ready(void)
863 {
864 	struct acpi_device *sensor_adev, *adev;
865 	struct device *csi_dev;
866 	bool ready = true;
867 	unsigned int i;
868 
869 	for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
870 		const struct ipu_sensor_config *cfg =
871 			&ipu_supported_sensors[i];
872 
873 		for_each_acpi_dev_match(sensor_adev, cfg->hid, NULL, -1) {
874 			if (!ACPI_PTR(sensor_adev->status.enabled))
875 				continue;
876 
877 			adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev);
878 			if (!adev)
879 				continue;
880 
881 			csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
882 			if (!csi_dev)
883 				ready = false;
884 
885 			put_device(csi_dev);
886 			acpi_dev_put(adev);
887 		}
888 	}
889 
890 	return ready;
891 }
892 
893 static int ipu_bridge_check_fwnode_graph(struct fwnode_handle *fwnode)
894 {
895 	struct fwnode_handle *endpoint;
896 
897 	if (IS_ERR_OR_NULL(fwnode))
898 		return -EINVAL;
899 
900 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
901 	if (endpoint) {
902 		fwnode_handle_put(endpoint);
903 		return 0;
904 	}
905 
906 	return ipu_bridge_check_fwnode_graph(fwnode->secondary);
907 }
908 
909 static DEFINE_MUTEX(ipu_bridge_mutex);
910 
911 int ipu_bridge_init(struct device *dev,
912 		    ipu_parse_sensor_fwnode_t parse_sensor_fwnode)
913 {
914 	struct fwnode_handle *fwnode;
915 	struct ipu_bridge *bridge;
916 	unsigned int i;
917 	int ret;
918 
919 	guard(mutex)(&ipu_bridge_mutex);
920 
921 	if (!ipu_bridge_check_fwnode_graph(dev_fwnode(dev)))
922 		return 0;
923 
924 	if (!ipu_bridge_ivsc_is_ready())
925 		return dev_err_probe(dev, -EPROBE_DEFER,
926 				     "waiting for IVSC to become ready\n");
927 
928 	bridge = kzalloc_obj(*bridge);
929 	if (!bridge)
930 		return -ENOMEM;
931 
932 	strscpy(bridge->ipu_node_name, IPU_HID,
933 		sizeof(bridge->ipu_node_name));
934 	bridge->ipu_hid_node.name = bridge->ipu_node_name;
935 	bridge->dev = dev;
936 	bridge->parse_sensor_fwnode = parse_sensor_fwnode;
937 
938 	ret = software_node_register(&bridge->ipu_hid_node);
939 	if (ret < 0) {
940 		dev_err(dev, "Failed to register the IPU HID node\n");
941 		goto err_free_bridge;
942 	}
943 
944 	/*
945 	 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
946 	 * only need one, rather than one per sensor). We include it as a
947 	 * member of the struct ipu_bridge rather than a global variable so
948 	 * that it survives if the module is unloaded along with the rest of
949 	 * the struct.
950 	 */
951 	for (i = 0; i < IPU_MAX_LANES; i++)
952 		bridge->data_lanes[i] = i + 1;
953 
954 	ret = ipu_bridge_connect_sensors(bridge);
955 	if (ret || bridge->n_sensors == 0)
956 		goto err_unregister_ipu;
957 
958 	dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
959 
960 	fwnode = software_node_fwnode(&bridge->ipu_hid_node);
961 	if (!fwnode) {
962 		dev_err(dev, "Error getting fwnode from ipu software_node\n");
963 		ret = -ENODEV;
964 		goto err_unregister_sensors;
965 	}
966 
967 	set_secondary_fwnode(dev, fwnode);
968 
969 	return 0;
970 
971 err_unregister_sensors:
972 	ipu_bridge_unregister_sensors(bridge);
973 err_unregister_ipu:
974 	software_node_unregister(&bridge->ipu_hid_node);
975 err_free_bridge:
976 	kfree(bridge);
977 
978 	return ret;
979 }
980 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, "INTEL_IPU_BRIDGE");
981 
982 MODULE_LICENSE("GPL");
983 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");
984