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