xref: /linux/drivers/acpi/mipi-disco-img.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MIPI DisCo for Imaging support.
4  *
5  * Copyright (C) 2023 Intel Corporation
6  *
7  * Support MIPI DisCo for Imaging by parsing ACPI _CRS CSI-2 records defined in
8  * Section 6.4.3.8.2.4 "Camera Serial Interface (CSI-2) Connection Resource
9  * Descriptor" of ACPI 6.5 and using device properties defined by the MIPI DisCo
10  * for Imaging specification.
11  *
12  * The implementation looks for the information in the ACPI namespace (CSI-2
13  * resource descriptors in _CRS) and constructs software nodes compatible with
14  * Documentation/firmware-guide/acpi/dsd/graph.rst to represent the CSI-2
15  * connection graph.  The software nodes are then populated with the data
16  * extracted from the _CRS CSI-2 resource descriptors and the MIPI DisCo
17  * for Imaging device properties present in _DSD for the ACPI device objects
18  * with CSI-2 connections.
19  */
20 
21 #include <linux/acpi.h>
22 #include <linux/dmi.h>
23 #include <linux/limits.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/overflow.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 
31 #include <media/v4l2-fwnode.h>
32 
33 #include "internal.h"
34 
35 static LIST_HEAD(acpi_mipi_crs_csi2_list);
36 
37 static void acpi_mipi_data_tag(acpi_handle handle, void *context)
38 {
39 }
40 
41 /* Connection data extracted from one _CRS CSI-2 resource descriptor. */
42 struct crs_csi2_connection {
43 	struct list_head entry;
44 	struct acpi_resource_csi2_serialbus csi2_data;
45 	acpi_handle remote_handle;
46 	char remote_name[];
47 };
48 
49 /* Data extracted from _CRS CSI-2 resource descriptors for one device. */
50 struct crs_csi2 {
51 	struct list_head entry;
52 	acpi_handle handle;
53 	struct acpi_device_software_nodes *swnodes;
54 	struct list_head connections;
55 	u32 port_count;
56 };
57 
58 struct csi2_resources_walk_data {
59 	acpi_handle handle;
60 	struct list_head connections;
61 };
62 
63 static acpi_status parse_csi2_resource(struct acpi_resource *res, void *context)
64 {
65 	struct csi2_resources_walk_data *crwd = context;
66 	struct acpi_resource_csi2_serialbus *csi2_res;
67 	struct acpi_resource_source *csi2_res_src;
68 	u16 csi2_res_src_length;
69 	struct crs_csi2_connection *conn;
70 	acpi_handle remote_handle;
71 
72 	if (res->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
73 		return AE_OK;
74 
75 	csi2_res = &res->data.csi2_serial_bus;
76 
77 	if (csi2_res->type != ACPI_RESOURCE_SERIAL_TYPE_CSI2)
78 		return AE_OK;
79 
80 	csi2_res_src = &csi2_res->resource_source;
81 	if (ACPI_FAILURE(acpi_get_handle(NULL, csi2_res_src->string_ptr,
82 					 &remote_handle))) {
83 		acpi_handle_debug(crwd->handle,
84 				  "unable to find resource source\n");
85 		return AE_OK;
86 	}
87 	csi2_res_src_length = csi2_res_src->string_length;
88 	if (!csi2_res_src_length) {
89 		acpi_handle_debug(crwd->handle,
90 				  "invalid resource source string length\n");
91 		return AE_OK;
92 	}
93 
94 	conn = kmalloc(struct_size(conn, remote_name, csi2_res_src_length + 1),
95 		       GFP_KERNEL);
96 	if (!conn)
97 		return AE_OK;
98 
99 	conn->csi2_data = *csi2_res;
100 	strscpy(conn->remote_name, csi2_res_src->string_ptr, csi2_res_src_length);
101 	conn->csi2_data.resource_source.string_ptr = conn->remote_name;
102 	conn->remote_handle = remote_handle;
103 
104 	list_add(&conn->entry, &crwd->connections);
105 
106 	return AE_OK;
107 }
108 
109 static struct crs_csi2 *acpi_mipi_add_crs_csi2(acpi_handle handle,
110 					       struct list_head *list)
111 {
112 	struct crs_csi2 *csi2;
113 
114 	csi2 = kzalloc(sizeof(*csi2), GFP_KERNEL);
115 	if (!csi2)
116 		return NULL;
117 
118 	csi2->handle = handle;
119 	INIT_LIST_HEAD(&csi2->connections);
120 	csi2->port_count = 1;
121 
122 	if (ACPI_FAILURE(acpi_attach_data(handle, acpi_mipi_data_tag, csi2))) {
123 		kfree(csi2);
124 		return NULL;
125 	}
126 
127 	list_add(&csi2->entry, list);
128 
129 	return csi2;
130 }
131 
132 static struct crs_csi2 *acpi_mipi_get_crs_csi2(acpi_handle handle)
133 {
134 	struct crs_csi2 *csi2;
135 
136 	if (ACPI_FAILURE(acpi_get_data_full(handle, acpi_mipi_data_tag,
137 					    (void **)&csi2, NULL)))
138 		return NULL;
139 
140 	return csi2;
141 }
142 
143 static void csi_csr2_release_connections(struct list_head *list)
144 {
145 	struct crs_csi2_connection *conn, *conn_tmp;
146 
147 	list_for_each_entry_safe(conn, conn_tmp, list, entry) {
148 		list_del(&conn->entry);
149 		kfree(conn);
150 	}
151 }
152 
153 static void acpi_mipi_del_crs_csi2(struct crs_csi2 *csi2)
154 {
155 	list_del(&csi2->entry);
156 	acpi_detach_data(csi2->handle, acpi_mipi_data_tag);
157 	kfree(csi2->swnodes);
158 	csi_csr2_release_connections(&csi2->connections);
159 	kfree(csi2);
160 }
161 
162 /**
163  * acpi_mipi_check_crs_csi2 - Look for CSI-2 resources in _CRS
164  * @handle: Device object handle to evaluate _CRS for.
165  *
166  * Find all CSI-2 resource descriptors in the given device's _CRS
167  * and collect them into a list.
168  */
169 void acpi_mipi_check_crs_csi2(acpi_handle handle)
170 {
171 	struct csi2_resources_walk_data crwd = {
172 		.handle = handle,
173 		.connections = LIST_HEAD_INIT(crwd.connections),
174 	};
175 	struct crs_csi2 *csi2;
176 
177 	/*
178 	 * Avoid allocating _CRS CSI-2 objects for devices without any CSI-2
179 	 * resource descriptions in _CRS to reduce overhead.
180 	 */
181 	acpi_walk_resources(handle, METHOD_NAME__CRS, parse_csi2_resource, &crwd);
182 	if (list_empty(&crwd.connections))
183 		return;
184 
185 	/*
186 	 * Create a _CRS CSI-2 entry to store the extracted connection
187 	 * information and add it to the global list.
188 	 */
189 	csi2 = acpi_mipi_add_crs_csi2(handle, &acpi_mipi_crs_csi2_list);
190 	if (!csi2) {
191 		csi_csr2_release_connections(&crwd.connections);
192 		return; /* Nothing really can be done about this. */
193 	}
194 
195 	list_replace(&crwd.connections, &csi2->connections);
196 }
197 
198 #define NO_CSI2_PORT (UINT_MAX - 1)
199 
200 static void alloc_crs_csi2_swnodes(struct crs_csi2 *csi2)
201 {
202 	size_t port_count = csi2->port_count;
203 	struct acpi_device_software_nodes *swnodes;
204 	size_t alloc_size;
205 	unsigned int i;
206 
207 	/*
208 	 * Allocate memory for ports, node pointers (number of nodes +
209 	 * 1 (guardian), nodes (root + number of ports * 2 (because for
210 	 * every port there is an endpoint)).
211 	 */
212 	if (check_mul_overflow(sizeof(*swnodes->ports) +
213 			       sizeof(*swnodes->nodes) * 2 +
214 			       sizeof(*swnodes->nodeptrs) * 2,
215 			       port_count, &alloc_size) ||
216 	    check_add_overflow(sizeof(*swnodes) +
217 			       sizeof(*swnodes->nodes) +
218 			       sizeof(*swnodes->nodeptrs) * 2,
219 			       alloc_size, &alloc_size)) {
220 		acpi_handle_info(csi2->handle,
221 				 "too many _CRS CSI-2 resource handles (%zu)",
222 				 port_count);
223 		return;
224 	}
225 
226 	swnodes = kmalloc(alloc_size, GFP_KERNEL);
227 	if (!swnodes)
228 		return;
229 
230 	swnodes->ports = (struct acpi_device_software_node_port *)(swnodes + 1);
231 	swnodes->nodes = (struct software_node *)(swnodes->ports + port_count);
232 	swnodes->nodeptrs = (const struct software_node **)(swnodes->nodes + 1 +
233 				2 * port_count);
234 	swnodes->num_ports = port_count;
235 
236 	for (i = 0; i < 2 * port_count + 1; i++)
237 		swnodes->nodeptrs[i] = &swnodes->nodes[i];
238 
239 	swnodes->nodeptrs[i] = NULL;
240 
241 	for (i = 0; i < port_count; i++)
242 		swnodes->ports[i].port_nr = NO_CSI2_PORT;
243 
244 	csi2->swnodes = swnodes;
245 }
246 
247 #define ACPI_CRS_CSI2_PHY_TYPE_C	0
248 #define ACPI_CRS_CSI2_PHY_TYPE_D	1
249 
250 static unsigned int next_csi2_port_index(struct acpi_device_software_nodes *swnodes,
251 					 unsigned int port_nr)
252 {
253 	unsigned int i;
254 
255 	for (i = 0; i < swnodes->num_ports; i++) {
256 		struct acpi_device_software_node_port *port = &swnodes->ports[i];
257 
258 		if (port->port_nr == port_nr)
259 			return i;
260 
261 		if (port->port_nr == NO_CSI2_PORT) {
262 			port->port_nr = port_nr;
263 			return i;
264 		}
265 	}
266 
267 	return NO_CSI2_PORT;
268 }
269 
270 /* Print graph port name into a buffer, return non-zero on failure. */
271 #define GRAPH_PORT_NAME(var, num)					    \
272 	(snprintf((var), sizeof(var), SWNODE_GRAPH_PORT_NAME_FMT, (num)) >= \
273 	 sizeof(var))
274 
275 static void extract_crs_csi2_conn_info(acpi_handle local_handle,
276 				       struct acpi_device_software_nodes *local_swnodes,
277 				       struct crs_csi2_connection *conn)
278 {
279 	struct crs_csi2 *remote_csi2 = acpi_mipi_get_crs_csi2(conn->remote_handle);
280 	struct acpi_device_software_nodes *remote_swnodes;
281 	struct acpi_device_software_node_port *local_port, *remote_port;
282 	struct software_node *local_node, *remote_node;
283 	unsigned int local_index, remote_index;
284 	unsigned int bus_type;
285 
286 	/*
287 	 * If the previous steps have failed to make room for a _CRS CSI-2
288 	 * representation for the remote end of the given connection, skip it.
289 	 */
290 	if (!remote_csi2)
291 		return;
292 
293 	remote_swnodes = remote_csi2->swnodes;
294 	if (!remote_swnodes)
295 		return;
296 
297 	switch (conn->csi2_data.phy_type) {
298 	case ACPI_CRS_CSI2_PHY_TYPE_C:
299 		bus_type = V4L2_FWNODE_BUS_TYPE_CSI2_CPHY;
300 		break;
301 
302 	case ACPI_CRS_CSI2_PHY_TYPE_D:
303 		bus_type = V4L2_FWNODE_BUS_TYPE_CSI2_DPHY;
304 		break;
305 
306 	default:
307 		acpi_handle_info(local_handle, "unknown CSI-2 PHY type %u\n",
308 				 conn->csi2_data.phy_type);
309 		return;
310 	}
311 
312 	local_index = next_csi2_port_index(local_swnodes,
313 					   conn->csi2_data.local_port_instance);
314 	if (WARN_ON_ONCE(local_index >= local_swnodes->num_ports))
315 		return;
316 
317 	remote_index = next_csi2_port_index(remote_swnodes,
318 					    conn->csi2_data.resource_source.index);
319 	if (WARN_ON_ONCE(remote_index >= remote_swnodes->num_ports))
320 		return;
321 
322 	local_port = &local_swnodes->ports[local_index];
323 	local_node = &local_swnodes->nodes[ACPI_DEVICE_SWNODE_EP(local_index)];
324 	local_port->crs_csi2_local = true;
325 
326 	remote_port = &remote_swnodes->ports[remote_index];
327 	remote_node = &remote_swnodes->nodes[ACPI_DEVICE_SWNODE_EP(remote_index)];
328 
329 	local_port->remote_ep[0] = SOFTWARE_NODE_REFERENCE(remote_node);
330 	remote_port->remote_ep[0] = SOFTWARE_NODE_REFERENCE(local_node);
331 
332 	local_port->ep_props[ACPI_DEVICE_SWNODE_EP_REMOTE_EP] =
333 			PROPERTY_ENTRY_REF_ARRAY("remote-endpoint",
334 						 local_port->remote_ep);
335 
336 	local_port->ep_props[ACPI_DEVICE_SWNODE_EP_BUS_TYPE] =
337 			PROPERTY_ENTRY_U32("bus-type", bus_type);
338 
339 	local_port->ep_props[ACPI_DEVICE_SWNODE_EP_REG] =
340 			PROPERTY_ENTRY_U32("reg", 0);
341 
342 	local_port->port_props[ACPI_DEVICE_SWNODE_PORT_REG] =
343 			PROPERTY_ENTRY_U32("reg", conn->csi2_data.local_port_instance);
344 
345 	if (GRAPH_PORT_NAME(local_port->port_name,
346 			    conn->csi2_data.local_port_instance))
347 		acpi_handle_info(local_handle, "local port %u name too long",
348 				 conn->csi2_data.local_port_instance);
349 
350 	remote_port->ep_props[ACPI_DEVICE_SWNODE_EP_REMOTE_EP] =
351 			PROPERTY_ENTRY_REF_ARRAY("remote-endpoint",
352 						 remote_port->remote_ep);
353 
354 	remote_port->ep_props[ACPI_DEVICE_SWNODE_EP_BUS_TYPE] =
355 			PROPERTY_ENTRY_U32("bus-type", bus_type);
356 
357 	remote_port->ep_props[ACPI_DEVICE_SWNODE_EP_REG] =
358 			PROPERTY_ENTRY_U32("reg", 0);
359 
360 	remote_port->port_props[ACPI_DEVICE_SWNODE_PORT_REG] =
361 			PROPERTY_ENTRY_U32("reg", conn->csi2_data.resource_source.index);
362 
363 	if (GRAPH_PORT_NAME(remote_port->port_name,
364 			    conn->csi2_data.resource_source.index))
365 		acpi_handle_info(local_handle, "remote port %u name too long",
366 				 conn->csi2_data.resource_source.index);
367 }
368 
369 static void prepare_crs_csi2_swnodes(struct crs_csi2 *csi2)
370 {
371 	struct acpi_device_software_nodes *local_swnodes = csi2->swnodes;
372 	acpi_handle local_handle = csi2->handle;
373 	struct crs_csi2_connection *conn;
374 
375 	/* Bail out if the allocation of swnodes has failed. */
376 	if (!local_swnodes)
377 		return;
378 
379 	list_for_each_entry(conn, &csi2->connections, entry)
380 		extract_crs_csi2_conn_info(local_handle, local_swnodes, conn);
381 }
382 
383 /**
384  * acpi_mipi_scan_crs_csi2 - Create ACPI _CRS CSI-2 software nodes
385  *
386  * Note that this function must be called before any struct acpi_device objects
387  * are bound to any ACPI drivers or scan handlers, so it cannot assume the
388  * existence of struct acpi_device objects for every device present in the ACPI
389  * namespace.
390  *
391  * acpi_scan_lock in scan.c must be held when calling this function.
392  */
393 void acpi_mipi_scan_crs_csi2(void)
394 {
395 	struct crs_csi2 *csi2;
396 	LIST_HEAD(aux_list);
397 
398 	/* Count references to each ACPI handle in the CSI-2 connection graph. */
399 	list_for_each_entry(csi2, &acpi_mipi_crs_csi2_list, entry) {
400 		struct crs_csi2_connection *conn;
401 
402 		list_for_each_entry(conn, &csi2->connections, entry) {
403 			struct crs_csi2 *remote_csi2;
404 
405 			csi2->port_count++;
406 
407 			remote_csi2 = acpi_mipi_get_crs_csi2(conn->remote_handle);
408 			if (remote_csi2) {
409 				remote_csi2->port_count++;
410 				continue;
411 			}
412 			/*
413 			 * The remote endpoint has no _CRS CSI-2 list entry yet,
414 			 * so create one for it and add it to the list.
415 			 */
416 			acpi_mipi_add_crs_csi2(conn->remote_handle, &aux_list);
417 		}
418 	}
419 	list_splice(&aux_list, &acpi_mipi_crs_csi2_list);
420 
421 	/*
422 	 * Allocate software nodes for representing the CSI-2 information.
423 	 *
424 	 * This needs to be done for all of the list entries in one go, because
425 	 * they may point to each other without restrictions and the next step
426 	 * relies on the availability of swnodes memory for each list entry.
427 	 */
428 	list_for_each_entry(csi2, &acpi_mipi_crs_csi2_list, entry)
429 		alloc_crs_csi2_swnodes(csi2);
430 
431 	/*
432 	 * Set up software node properties using data from _CRS CSI-2 resource
433 	 * descriptors.
434 	 */
435 	list_for_each_entry(csi2, &acpi_mipi_crs_csi2_list, entry)
436 		prepare_crs_csi2_swnodes(csi2);
437 }
438 
439 /*
440  * Get the index of the next property in the property array, with a given
441  * maximum value.
442  */
443 #define NEXT_PROPERTY(index, max)			\
444 	(WARN_ON((index) > ACPI_DEVICE_SWNODE_##max) ?	\
445 	 ACPI_DEVICE_SWNODE_##max : (index)++)
446 
447 static void init_csi2_port_local(struct acpi_device *adev,
448 				 struct acpi_device_software_node_port *port,
449 				 struct fwnode_handle *port_fwnode,
450 				 unsigned int index)
451 {
452 	acpi_handle handle = acpi_device_handle(adev);
453 	unsigned int num_link_freqs;
454 	int ret;
455 
456 	ret = fwnode_property_count_u64(port_fwnode, "mipi-img-link-frequencies");
457 	if (ret <= 0)
458 		return;
459 
460 	num_link_freqs = ret;
461 	if (num_link_freqs > ACPI_DEVICE_CSI2_DATA_LANES) {
462 		acpi_handle_info(handle, "Too many link frequencies: %u\n",
463 				 num_link_freqs);
464 		num_link_freqs = ACPI_DEVICE_CSI2_DATA_LANES;
465 	}
466 
467 	ret = fwnode_property_read_u64_array(port_fwnode,
468 					     "mipi-img-link-frequencies",
469 					     port->link_frequencies,
470 					     num_link_freqs);
471 	if (ret) {
472 		acpi_handle_info(handle, "Unable to get link frequencies (%d)\n",
473 				 ret);
474 		return;
475 	}
476 
477 	port->ep_props[NEXT_PROPERTY(index, EP_LINK_FREQUENCIES)] =
478 				PROPERTY_ENTRY_U64_ARRAY_LEN("link-frequencies",
479 							     port->link_frequencies,
480 							     num_link_freqs);
481 }
482 
483 static void init_csi2_port(struct acpi_device *adev,
484 			   struct acpi_device_software_nodes *swnodes,
485 			   struct acpi_device_software_node_port *port,
486 			   struct fwnode_handle *port_fwnode,
487 			   unsigned int port_index)
488 {
489 	unsigned int ep_prop_index = ACPI_DEVICE_SWNODE_EP_CLOCK_LANES;
490 	acpi_handle handle = acpi_device_handle(adev);
491 	u8 val[ACPI_DEVICE_CSI2_DATA_LANES];
492 	int num_lanes = 0;
493 	int ret;
494 
495 	if (GRAPH_PORT_NAME(port->port_name, port->port_nr))
496 		return;
497 
498 	swnodes->nodes[ACPI_DEVICE_SWNODE_PORT(port_index)] =
499 			SOFTWARE_NODE(port->port_name, port->port_props,
500 				      &swnodes->nodes[ACPI_DEVICE_SWNODE_ROOT]);
501 
502 	ret = fwnode_property_read_u8(port_fwnode, "mipi-img-clock-lane", val);
503 	if (!ret)
504 		port->ep_props[NEXT_PROPERTY(ep_prop_index, EP_CLOCK_LANES)] =
505 			PROPERTY_ENTRY_U32("clock-lanes", val[0]);
506 
507 	ret = fwnode_property_count_u8(port_fwnode, "mipi-img-data-lanes");
508 	if (ret > 0) {
509 		num_lanes = ret;
510 
511 		if (num_lanes > ACPI_DEVICE_CSI2_DATA_LANES) {
512 			acpi_handle_info(handle, "Too many data lanes: %u\n",
513 					 num_lanes);
514 			num_lanes = ACPI_DEVICE_CSI2_DATA_LANES;
515 		}
516 
517 		ret = fwnode_property_read_u8_array(port_fwnode,
518 						    "mipi-img-data-lanes",
519 						    val, num_lanes);
520 		if (!ret) {
521 			unsigned int i;
522 
523 			for (i = 0; i < num_lanes; i++)
524 				port->data_lanes[i] = val[i];
525 
526 			port->ep_props[NEXT_PROPERTY(ep_prop_index, EP_DATA_LANES)] =
527 				PROPERTY_ENTRY_U32_ARRAY_LEN("data-lanes",
528 							     port->data_lanes,
529 							     num_lanes);
530 		}
531 	}
532 
533 	ret = fwnode_property_count_u8(port_fwnode, "mipi-img-lane-polarities");
534 	if (ret < 0) {
535 		acpi_handle_debug(handle, "Lane polarity bytes missing\n");
536 	} else if (ret * BITS_PER_TYPE(u8) < num_lanes + 1) {
537 		acpi_handle_info(handle, "Too few lane polarity bits (%zu vs. %d)\n",
538 				 ret * BITS_PER_TYPE(u8), num_lanes + 1);
539 	} else {
540 		unsigned long mask = 0;
541 		int byte_count = ret;
542 		unsigned int i;
543 
544 		/*
545 		 * The total number of lanes is ACPI_DEVICE_CSI2_DATA_LANES + 1
546 		 * (data lanes + clock lane).  It is not expected to ever be
547 		 * greater than the number of bits in an unsigned long
548 		 * variable, but ensure that this is the case.
549 		 */
550 		BUILD_BUG_ON(BITS_PER_TYPE(unsigned long) <= ACPI_DEVICE_CSI2_DATA_LANES);
551 
552 		if (byte_count > sizeof(mask)) {
553 			acpi_handle_info(handle, "Too many lane polarities: %d\n",
554 					 byte_count);
555 			byte_count = sizeof(mask);
556 		}
557 		fwnode_property_read_u8_array(port_fwnode, "mipi-img-lane-polarities",
558 					      val, byte_count);
559 
560 		for (i = 0; i < byte_count; i++)
561 			mask |= (unsigned long)val[i] << BITS_PER_TYPE(u8) * i;
562 
563 		for (i = 0; i <= num_lanes; i++)
564 			port->lane_polarities[i] = test_bit(i, &mask);
565 
566 		port->ep_props[NEXT_PROPERTY(ep_prop_index, EP_LANE_POLARITIES)] =
567 				PROPERTY_ENTRY_U32_ARRAY_LEN("lane-polarities",
568 							     port->lane_polarities,
569 							     num_lanes + 1);
570 	}
571 
572 	swnodes->nodes[ACPI_DEVICE_SWNODE_EP(port_index)] =
573 		SOFTWARE_NODE("endpoint@0", swnodes->ports[port_index].ep_props,
574 			      &swnodes->nodes[ACPI_DEVICE_SWNODE_PORT(port_index)]);
575 
576 	if (port->crs_csi2_local)
577 		init_csi2_port_local(adev, port, port_fwnode, ep_prop_index);
578 }
579 
580 #define MIPI_IMG_PORT_PREFIX "mipi-img-port-"
581 
582 static struct fwnode_handle *get_mipi_port_handle(struct fwnode_handle *adev_fwnode,
583 						  unsigned int port_nr)
584 {
585 	char port_name[sizeof(MIPI_IMG_PORT_PREFIX) + 2];
586 
587 	if (snprintf(port_name, sizeof(port_name), "%s%u",
588 		     MIPI_IMG_PORT_PREFIX, port_nr) >= sizeof(port_name))
589 		return NULL;
590 
591 	return fwnode_get_named_child_node(adev_fwnode, port_name);
592 }
593 
594 static void init_crs_csi2_swnodes(struct crs_csi2 *csi2)
595 {
596 	struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER };
597 	struct acpi_device_software_nodes *swnodes = csi2->swnodes;
598 	acpi_handle handle = csi2->handle;
599 	unsigned int prop_index = 0;
600 	struct fwnode_handle *adev_fwnode;
601 	struct acpi_device *adev;
602 	acpi_status status;
603 	unsigned int i;
604 	u32 val;
605 	int ret;
606 
607 	/*
608 	 * Bail out if the swnodes are not available (either they have not been
609 	 * allocated or they have been assigned to the device already).
610 	 */
611 	if (!swnodes)
612 		return;
613 
614 	adev = acpi_fetch_acpi_dev(handle);
615 	if (!adev)
616 		return;
617 
618 	adev_fwnode = acpi_fwnode_handle(adev);
619 
620 	/*
621 	 * If the "rotation" property is not present, but _PLD is there,
622 	 * evaluate it to get the "rotation" value.
623 	 */
624 	if (!fwnode_property_present(adev_fwnode, "rotation")) {
625 		struct acpi_pld_info *pld;
626 
627 		status = acpi_get_physical_device_location(handle, &pld);
628 		if (ACPI_SUCCESS(status)) {
629 			swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_ROTATION)] =
630 					PROPERTY_ENTRY_U32("rotation",
631 							   pld->rotation * 45U);
632 			kfree(pld);
633 		}
634 	}
635 
636 	if (!fwnode_property_read_u32(adev_fwnode, "mipi-img-clock-frequency", &val))
637 		swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_CLOCK_FREQUENCY)] =
638 			PROPERTY_ENTRY_U32("clock-frequency", val);
639 
640 	if (!fwnode_property_read_u32(adev_fwnode, "mipi-img-led-max-current", &val))
641 		swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_LED_MAX_MICROAMP)] =
642 			PROPERTY_ENTRY_U32("led-max-microamp", val);
643 
644 	if (!fwnode_property_read_u32(adev_fwnode, "mipi-img-flash-max-current", &val))
645 		swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_FLASH_MAX_MICROAMP)] =
646 			PROPERTY_ENTRY_U32("flash-max-microamp", val);
647 
648 	if (!fwnode_property_read_u32(adev_fwnode, "mipi-img-flash-max-timeout-us", &val))
649 		swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_FLASH_MAX_TIMEOUT_US)] =
650 			PROPERTY_ENTRY_U32("flash-max-timeout-us", val);
651 
652 	status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
653 	if (ACPI_FAILURE(status)) {
654 		acpi_handle_info(handle, "Unable to get the path name\n");
655 		return;
656 	}
657 
658 	swnodes->nodes[ACPI_DEVICE_SWNODE_ROOT] =
659 			SOFTWARE_NODE(buffer.pointer, swnodes->dev_props, NULL);
660 
661 	for (i = 0; i < swnodes->num_ports; i++) {
662 		struct acpi_device_software_node_port *port = &swnodes->ports[i];
663 		struct fwnode_handle *port_fwnode;
664 
665 		/*
666 		 * The MIPI DisCo for Imaging specification defines _DSD device
667 		 * properties for providing CSI-2 port parameters that can be
668 		 * accessed through the generic device properties framework.  To
669 		 * access them, it is first necessary to find the data node
670 		 * representing the port under the given ACPI device object.
671 		 */
672 		port_fwnode = get_mipi_port_handle(adev_fwnode, port->port_nr);
673 		if (!port_fwnode) {
674 			acpi_handle_info(handle,
675 					 "MIPI port name too long for port %u\n",
676 					 port->port_nr);
677 			continue;
678 		}
679 
680 		init_csi2_port(adev, swnodes, port, port_fwnode, i);
681 
682 		fwnode_handle_put(port_fwnode);
683 	}
684 
685 	ret = software_node_register_node_group(swnodes->nodeptrs);
686 	if (ret < 0) {
687 		acpi_handle_info(handle,
688 				 "Unable to register software nodes (%d)\n", ret);
689 		return;
690 	}
691 
692 	adev->swnodes = swnodes;
693 	adev_fwnode->secondary = software_node_fwnode(swnodes->nodes);
694 
695 	/*
696 	 * Prevents the swnodes from this csi2 entry from being assigned again
697 	 * or freed prematurely.
698 	 */
699 	csi2->swnodes = NULL;
700 }
701 
702 /**
703  * acpi_mipi_init_crs_csi2_swnodes - Initialize _CRS CSI-2 software nodes
704  *
705  * Use MIPI DisCo for Imaging device properties to finalize the initialization
706  * of CSI-2 software nodes for all ACPI device objects that have been already
707  * enumerated.
708  */
709 void acpi_mipi_init_crs_csi2_swnodes(void)
710 {
711 	struct crs_csi2 *csi2, *csi2_tmp;
712 
713 	list_for_each_entry_safe(csi2, csi2_tmp, &acpi_mipi_crs_csi2_list, entry)
714 		init_crs_csi2_swnodes(csi2);
715 }
716 
717 /**
718  * acpi_mipi_crs_csi2_cleanup - Free _CRS CSI-2 temporary data
719  */
720 void acpi_mipi_crs_csi2_cleanup(void)
721 {
722 	struct crs_csi2 *csi2, *csi2_tmp;
723 
724 	list_for_each_entry_safe(csi2, csi2_tmp, &acpi_mipi_crs_csi2_list, entry)
725 		acpi_mipi_del_crs_csi2(csi2);
726 }
727 
728 #ifdef CONFIG_X86
729 #include <asm/cpu_device_id.h>
730 #include <asm/intel-family.h>
731 
732 /* CPU matches for Dell generations with broken ACPI MIPI DISCO info */
733 static const struct x86_cpu_id dell_broken_mipi_disco_cpu_gens[] = {
734 	X86_MATCH_VFM(INTEL_TIGERLAKE, NULL),
735 	X86_MATCH_VFM(INTEL_TIGERLAKE_L, NULL),
736 	X86_MATCH_VFM(INTEL_ALDERLAKE, NULL),
737 	X86_MATCH_VFM(INTEL_ALDERLAKE_L, NULL),
738 	X86_MATCH_VFM(INTEL_RAPTORLAKE, NULL),
739 	X86_MATCH_VFM(INTEL_RAPTORLAKE_P, NULL),
740 	X86_MATCH_VFM(INTEL_RAPTORLAKE_S, NULL),
741 	{}
742 };
743 
744 static const char *strnext(const char *s1, const char *s2)
745 {
746 	s1 = strstr(s1, s2);
747 
748 	if (!s1)
749 		return NULL;
750 
751 	return s1 + strlen(s2);
752 }
753 
754 /**
755  * acpi_graph_ignore_port - Tell whether a port node should be ignored
756  * @handle: The ACPI handle of the node (which may be a port node)
757  *
758  * Return: true if a port node should be ignored and the data to that should
759  * come from other sources instead (Windows ACPI definitions and
760  * ipu-bridge). This is currently used to ignore bad port nodes related to IPU6
761  * ("IPU?") and camera sensor devices ("LNK?") in certain Dell systems with
762  * Intel VSC.
763  */
764 bool acpi_graph_ignore_port(acpi_handle handle)
765 {
766 	const char *path = NULL, *orig_path;
767 	static bool dmi_tested, ignore_port;
768 
769 	if (!dmi_tested) {
770 		if (dmi_name_in_vendors("Dell Inc.") &&
771 		    x86_match_cpu(dell_broken_mipi_disco_cpu_gens))
772 			ignore_port = true;
773 
774 		dmi_tested = true;
775 	}
776 
777 	if (!ignore_port)
778 		return false;
779 
780 	/* Check if the device is either "IPU" or "LNK" (sensor). */
781 	orig_path = acpi_handle_path(handle);
782 	if (!orig_path)
783 		return false;
784 	path = strnext(orig_path, "IPU");
785 	if (!path)
786 		path = strnext(orig_path, "LNK");
787 	if (!path)
788 		goto out_free;
789 
790 	if (!(isdigit(path[0]) && path[1] == '.'))
791 		goto out_free;
792 
793 	/* Check if the node has a "PRT" prefix. */
794 	path = strnext(path, "PRT");
795 	if (path && isdigit(path[0]) && !path[1]) {
796 		acpi_handle_debug(handle, "ignoring data node\n");
797 
798 		kfree(orig_path);
799 		return true;
800 	}
801 
802 out_free:
803 	kfree(orig_path);
804 	return false;
805 }
806 #endif
807