| 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(new: &conn->entry, head: &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(list: &csi2->connections); |
| 120 | csi2->port_count = 1; |
| 121 | |
| 122 | if (ACPI_FAILURE(acpi_attach_data(handle, acpi_mipi_data_tag, csi2))) { |
| 123 | kfree(objp: csi2); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | list_add(new: &csi2->entry, head: 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(entry: &conn->entry); |
| 149 | kfree(objp: conn); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void acpi_mipi_del_crs_csi2(struct crs_csi2 *csi2) |
| 154 | { |
| 155 | list_del(entry: &csi2->entry); |
| 156 | acpi_detach_data(object: csi2->handle, handler: acpi_mipi_data_tag); |
| 157 | kfree(objp: csi2->swnodes); |
| 158 | csi_csr2_release_connections(list: &csi2->connections); |
| 159 | kfree(objp: 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(device: handle, METHOD_NAME__CRS, user_function: parse_csi2_resource, context: &crwd); |
| 182 | if (list_empty(head: &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, list: &acpi_mipi_crs_csi2_list); |
| 190 | if (!csi2) { |
| 191 | csi_csr2_release_connections(list: &crwd.connections); |
| 192 | return; /* Nothing really can be done about this. */ |
| 193 | } |
| 194 | |
| 195 | list_replace(old: &crwd.connections, new: &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 (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(handle: 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(swnodes: local_swnodes, |
| 313 | port_nr: 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(swnodes: remote_swnodes, |
| 318 | port_nr: 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(handle: 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(handle: conn->remote_handle, list: &aux_list); |
| 417 | } |
| 418 | } |
| 419 | list_splice(list: &aux_list, head: &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(fwnode: port_fwnode, propname: "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(fwnode: port_fwnode, |
| 468 | propname: "mipi-img-link-frequencies" , |
| 469 | val: port->link_frequencies, |
| 470 | nval: 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(fwnode: port_fwnode, propname: "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(fwnode: port_fwnode, propname: "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(fwnode: port_fwnode, |
| 518 | propname: "mipi-img-data-lanes" , |
| 519 | val, nval: 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(fwnode: port_fwnode, propname: "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(fwnode: port_fwnode, propname: "mipi-img-lane-polarities" , |
| 558 | val, nval: 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, index: 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(buf: port_name, size: sizeof(port_name), fmt: "%s%u" , |
| 588 | MIPI_IMG_PORT_PREFIX, port_nr) >= sizeof(port_name)) |
| 589 | return NULL; |
| 590 | |
| 591 | return fwnode_get_named_child_node(fwnode: adev_fwnode, childname: 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(fwnode: adev_fwnode, propname: "rotation" )) { |
| 625 | struct acpi_pld_info *pld; |
| 626 | |
| 627 | if (acpi_get_physical_device_location(handle, pld: &pld)) { |
| 628 | swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_ROTATION)] = |
| 629 | PROPERTY_ENTRY_U32("rotation" , |
| 630 | pld->rotation * 45U); |
| 631 | kfree(objp: pld); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (!fwnode_property_read_u32(fwnode: adev_fwnode, propname: "mipi-img-clock-frequency" , val: &val)) |
| 636 | swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_CLOCK_FREQUENCY)] = |
| 637 | PROPERTY_ENTRY_U32("clock-frequency" , val); |
| 638 | |
| 639 | if (!fwnode_property_read_u32(fwnode: adev_fwnode, propname: "mipi-img-led-max-current" , val: &val)) |
| 640 | swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_LED_MAX_MICROAMP)] = |
| 641 | PROPERTY_ENTRY_U32("led-max-microamp" , val); |
| 642 | |
| 643 | if (!fwnode_property_read_u32(fwnode: adev_fwnode, propname: "mipi-img-flash-max-current" , val: &val)) |
| 644 | swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_FLASH_MAX_MICROAMP)] = |
| 645 | PROPERTY_ENTRY_U32("flash-max-microamp" , val); |
| 646 | |
| 647 | if (!fwnode_property_read_u32(fwnode: adev_fwnode, propname: "mipi-img-flash-max-timeout-us" , val: &val)) |
| 648 | swnodes->dev_props[NEXT_PROPERTY(prop_index, DEV_FLASH_MAX_TIMEOUT_US)] = |
| 649 | PROPERTY_ENTRY_U32("flash-max-timeout-us" , val); |
| 650 | |
| 651 | status = acpi_get_name(object: handle, ACPI_FULL_PATHNAME, ret_path_ptr: &buffer); |
| 652 | if (ACPI_FAILURE(status)) { |
| 653 | acpi_handle_info(handle, "Unable to get the path name\n" ); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | swnodes->nodes[ACPI_DEVICE_SWNODE_ROOT] = |
| 658 | SOFTWARE_NODE(buffer.pointer, swnodes->dev_props, NULL); |
| 659 | |
| 660 | for (i = 0; i < swnodes->num_ports; i++) { |
| 661 | struct acpi_device_software_node_port *port = &swnodes->ports[i]; |
| 662 | struct fwnode_handle *port_fwnode; |
| 663 | |
| 664 | /* |
| 665 | * The MIPI DisCo for Imaging specification defines _DSD device |
| 666 | * properties for providing CSI-2 port parameters that can be |
| 667 | * accessed through the generic device properties framework. To |
| 668 | * access them, it is first necessary to find the data node |
| 669 | * representing the port under the given ACPI device object. |
| 670 | */ |
| 671 | port_fwnode = get_mipi_port_handle(adev_fwnode, port_nr: port->port_nr); |
| 672 | if (!port_fwnode) { |
| 673 | acpi_handle_info(handle, |
| 674 | "MIPI port name too long for port %u\n" , |
| 675 | port->port_nr); |
| 676 | continue; |
| 677 | } |
| 678 | |
| 679 | init_csi2_port(adev, swnodes, port, port_fwnode, port_index: i); |
| 680 | |
| 681 | fwnode_handle_put(fwnode: port_fwnode); |
| 682 | } |
| 683 | |
| 684 | ret = software_node_register_node_group(node_group: swnodes->nodeptrs); |
| 685 | if (ret < 0) { |
| 686 | acpi_handle_info(handle, |
| 687 | "Unable to register software nodes (%d)\n" , ret); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | adev->swnodes = swnodes; |
| 692 | adev_fwnode->secondary = software_node_fwnode(node: swnodes->nodes); |
| 693 | |
| 694 | /* |
| 695 | * Prevents the swnodes from this csi2 entry from being assigned again |
| 696 | * or freed prematurely. |
| 697 | */ |
| 698 | csi2->swnodes = NULL; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * acpi_mipi_init_crs_csi2_swnodes - Initialize _CRS CSI-2 software nodes |
| 703 | * |
| 704 | * Use MIPI DisCo for Imaging device properties to finalize the initialization |
| 705 | * of CSI-2 software nodes for all ACPI device objects that have been already |
| 706 | * enumerated. |
| 707 | */ |
| 708 | void acpi_mipi_init_crs_csi2_swnodes(void) |
| 709 | { |
| 710 | struct crs_csi2 *csi2, *csi2_tmp; |
| 711 | |
| 712 | list_for_each_entry_safe(csi2, csi2_tmp, &acpi_mipi_crs_csi2_list, entry) |
| 713 | init_crs_csi2_swnodes(csi2); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * acpi_mipi_crs_csi2_cleanup - Free _CRS CSI-2 temporary data |
| 718 | */ |
| 719 | void acpi_mipi_crs_csi2_cleanup(void) |
| 720 | { |
| 721 | struct crs_csi2 *csi2, *csi2_tmp; |
| 722 | |
| 723 | list_for_each_entry_safe(csi2, csi2_tmp, &acpi_mipi_crs_csi2_list, entry) |
| 724 | acpi_mipi_del_crs_csi2(csi2); |
| 725 | } |
| 726 | |
| 727 | #ifdef CONFIG_X86 |
| 728 | #include <asm/cpu_device_id.h> |
| 729 | #include <asm/intel-family.h> |
| 730 | |
| 731 | /* CPU matches for Dell generations with broken ACPI MIPI DISCO info */ |
| 732 | static const struct x86_cpu_id dell_broken_mipi_disco_cpu_gens[] = { |
| 733 | X86_MATCH_VFM(INTEL_TIGERLAKE, NULL), |
| 734 | X86_MATCH_VFM(INTEL_TIGERLAKE_L, NULL), |
| 735 | X86_MATCH_VFM(INTEL_ALDERLAKE, NULL), |
| 736 | X86_MATCH_VFM(INTEL_ALDERLAKE_L, NULL), |
| 737 | X86_MATCH_VFM(INTEL_RAPTORLAKE, NULL), |
| 738 | X86_MATCH_VFM(INTEL_RAPTORLAKE_P, NULL), |
| 739 | X86_MATCH_VFM(INTEL_RAPTORLAKE_S, NULL), |
| 740 | {} |
| 741 | }; |
| 742 | |
| 743 | static const char *strnext(const char *s1, const char *s2) |
| 744 | { |
| 745 | s1 = strstr(s1, s2); |
| 746 | |
| 747 | if (!s1) |
| 748 | return NULL; |
| 749 | |
| 750 | return s1 + strlen(s2); |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * acpi_graph_ignore_port - Tell whether a port node should be ignored |
| 755 | * @handle: The ACPI handle of the node (which may be a port node) |
| 756 | * |
| 757 | * Return: true if a port node should be ignored and the data to that should |
| 758 | * come from other sources instead (Windows ACPI definitions and |
| 759 | * ipu-bridge). This is currently used to ignore bad port nodes related to IPU6 |
| 760 | * ("IPU?") and camera sensor devices ("LNK?") in certain Dell systems with |
| 761 | * Intel VSC. |
| 762 | */ |
| 763 | bool acpi_graph_ignore_port(acpi_handle handle) |
| 764 | { |
| 765 | const char *path = NULL, *orig_path; |
| 766 | static bool dmi_tested, ignore_port; |
| 767 | |
| 768 | if (!dmi_tested) { |
| 769 | if (dmi_name_in_vendors(str: "Dell Inc." ) && |
| 770 | x86_match_cpu(match: dell_broken_mipi_disco_cpu_gens)) |
| 771 | ignore_port = true; |
| 772 | |
| 773 | dmi_tested = true; |
| 774 | } |
| 775 | |
| 776 | if (!ignore_port) |
| 777 | return false; |
| 778 | |
| 779 | /* Check if the device is either "IPU" or "LNK" (sensor). */ |
| 780 | orig_path = acpi_handle_path(handle); |
| 781 | if (!orig_path) |
| 782 | return false; |
| 783 | path = strnext(s1: orig_path, s2: "IPU" ); |
| 784 | if (!path) |
| 785 | path = strnext(s1: orig_path, s2: "LNK" ); |
| 786 | if (!path) |
| 787 | goto out_free; |
| 788 | |
| 789 | if (!(isdigit(c: path[0]) && path[1] == '.')) |
| 790 | goto out_free; |
| 791 | |
| 792 | /* Check if the node has a "PRT" prefix. */ |
| 793 | path = strnext(s1: path, s2: "PRT" ); |
| 794 | if (path && isdigit(c: path[0]) && !path[1]) { |
| 795 | acpi_handle_debug(handle, "ignoring data node\n" ); |
| 796 | |
| 797 | kfree(objp: orig_path); |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | out_free: |
| 802 | kfree(objp: orig_path); |
| 803 | return false; |
| 804 | } |
| 805 | #endif |
| 806 | |