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