1 // SPDX-License-Identifier: GPL-2.0 2 /* Author: Dan Scally <djrscally@gmail.com> */ 3 4 #include <linux/acpi.h> 5 #include <linux/device.h> 6 #include <linux/i2c.h> 7 #include <linux/pci.h> 8 #include <linux/property.h> 9 #include <media/v4l2-fwnode.h> 10 11 #include "ipu-bridge.h" 12 13 /* 14 * Extend this array with ACPI Hardware IDs of devices known to be working 15 * plus the number of link-frequencies expected by their drivers, along with 16 * the frequency values in hertz. This is somewhat opportunistic way of adding 17 * support for this for now in the hopes of a better source for the information 18 * (possibly some encoded value in the SSDB buffer that we're unaware of) 19 * becoming apparent in the future. 20 * 21 * Do not add an entry for a sensor that is not actually supported. 22 */ 23 static const struct ipu_sensor_config ipu_supported_sensors[] = { 24 /* Omnivision OV5693 */ 25 IPU_SENSOR_CONFIG("INT33BE", 1, 419200000), 26 /* Omnivision OV8865 */ 27 IPU_SENSOR_CONFIG("INT347A", 1, 360000000), 28 /* Omnivision OV7251 */ 29 IPU_SENSOR_CONFIG("INT347E", 1, 319200000), 30 /* Omnivision OV2680 */ 31 IPU_SENSOR_CONFIG("OVTI2680", 0), 32 /* Omnivision ov8856 */ 33 IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000), 34 /* Omnivision ov2740 */ 35 IPU_SENSOR_CONFIG("INT3474", 1, 360000000), 36 /* Hynix hi556 */ 37 IPU_SENSOR_CONFIG("INT3537", 1, 437000000), 38 /* Omnivision ov13b10 */ 39 IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000), 40 }; 41 42 static const struct ipu_property_names prop_names = { 43 .clock_frequency = "clock-frequency", 44 .rotation = "rotation", 45 .orientation = "orientation", 46 .bus_type = "bus-type", 47 .data_lanes = "data-lanes", 48 .remote_endpoint = "remote-endpoint", 49 .link_frequencies = "link-frequencies", 50 }; 51 52 static const char * const ipu_vcm_types[] = { 53 "ad5823", 54 "dw9714", 55 "ad5816", 56 "dw9719", 57 "dw9718", 58 "dw9806b", 59 "wv517s", 60 "lc898122xa", 61 "lc898212axb", 62 }; 63 64 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id, 65 void *data, u32 size) 66 { 67 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 68 union acpi_object *obj; 69 acpi_status status; 70 int ret = 0; 71 72 status = acpi_evaluate_object(adev->handle, id, NULL, &buffer); 73 if (ACPI_FAILURE(status)) 74 return -ENODEV; 75 76 obj = buffer.pointer; 77 if (!obj) { 78 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n"); 79 return -ENODEV; 80 } 81 82 if (obj->type != ACPI_TYPE_BUFFER) { 83 dev_err(&adev->dev, "Not an ACPI buffer\n"); 84 ret = -ENODEV; 85 goto out_free_buff; 86 } 87 88 if (obj->buffer.length > size) { 89 dev_err(&adev->dev, "Given buffer is too small\n"); 90 ret = -EINVAL; 91 goto out_free_buff; 92 } 93 94 memcpy(data, obj->buffer.pointer, obj->buffer.length); 95 96 out_free_buff: 97 kfree(buffer.pointer); 98 return ret; 99 } 100 101 static u32 ipu_bridge_parse_rotation(struct ipu_sensor *sensor) 102 { 103 switch (sensor->ssdb.degree) { 104 case IPU_SENSOR_ROTATION_NORMAL: 105 return 0; 106 case IPU_SENSOR_ROTATION_INVERTED: 107 return 180; 108 default: 109 dev_warn(&sensor->adev->dev, 110 "Unknown rotation %d. Assume 0 degree rotation\n", 111 sensor->ssdb.degree); 112 return 0; 113 } 114 } 115 116 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct ipu_sensor *sensor) 117 { 118 switch (sensor->pld->panel) { 119 case ACPI_PLD_PANEL_FRONT: 120 return V4L2_FWNODE_ORIENTATION_FRONT; 121 case ACPI_PLD_PANEL_BACK: 122 return V4L2_FWNODE_ORIENTATION_BACK; 123 case ACPI_PLD_PANEL_TOP: 124 case ACPI_PLD_PANEL_LEFT: 125 case ACPI_PLD_PANEL_RIGHT: 126 case ACPI_PLD_PANEL_UNKNOWN: 127 return V4L2_FWNODE_ORIENTATION_EXTERNAL; 128 default: 129 dev_warn(&sensor->adev->dev, "Unknown _PLD panel value %d\n", 130 sensor->pld->panel); 131 return V4L2_FWNODE_ORIENTATION_EXTERNAL; 132 } 133 } 134 135 static void ipu_bridge_create_fwnode_properties( 136 struct ipu_sensor *sensor, 137 struct ipu_bridge *bridge, 138 const struct ipu_sensor_config *cfg) 139 { 140 u32 rotation; 141 enum v4l2_fwnode_orientation orientation; 142 143 rotation = ipu_bridge_parse_rotation(sensor); 144 orientation = ipu_bridge_parse_orientation(sensor); 145 146 sensor->prop_names = prop_names; 147 148 sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]); 149 sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]); 150 151 sensor->dev_properties[0] = PROPERTY_ENTRY_U32( 152 sensor->prop_names.clock_frequency, 153 sensor->ssdb.mclkspeed); 154 sensor->dev_properties[1] = PROPERTY_ENTRY_U32( 155 sensor->prop_names.rotation, 156 rotation); 157 sensor->dev_properties[2] = PROPERTY_ENTRY_U32( 158 sensor->prop_names.orientation, 159 orientation); 160 if (sensor->ssdb.vcmtype) { 161 sensor->vcm_ref[0] = 162 SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]); 163 sensor->dev_properties[3] = 164 PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref); 165 } 166 167 sensor->ep_properties[0] = PROPERTY_ENTRY_U32( 168 sensor->prop_names.bus_type, 169 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY); 170 sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN( 171 sensor->prop_names.data_lanes, 172 bridge->data_lanes, 173 sensor->ssdb.lanes); 174 sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY( 175 sensor->prop_names.remote_endpoint, 176 sensor->local_ref); 177 178 if (cfg->nr_link_freqs > 0) 179 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN( 180 sensor->prop_names.link_frequencies, 181 cfg->link_freqs, 182 cfg->nr_link_freqs); 183 184 sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN( 185 sensor->prop_names.data_lanes, 186 bridge->data_lanes, 187 sensor->ssdb.lanes); 188 sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY( 189 sensor->prop_names.remote_endpoint, 190 sensor->remote_ref); 191 } 192 193 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor) 194 { 195 snprintf(sensor->node_names.remote_port, 196 sizeof(sensor->node_names.remote_port), 197 SWNODE_GRAPH_PORT_NAME_FMT, sensor->ssdb.link); 198 snprintf(sensor->node_names.port, 199 sizeof(sensor->node_names.port), 200 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */ 201 snprintf(sensor->node_names.endpoint, 202 sizeof(sensor->node_names.endpoint), 203 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */ 204 } 205 206 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor) 207 { 208 struct software_node *nodes = sensor->swnodes; 209 210 sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID]; 211 sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT]; 212 sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT]; 213 sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT]; 214 sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT]; 215 if (sensor->ssdb.vcmtype) 216 sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM]; 217 } 218 219 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge, 220 struct ipu_sensor *sensor) 221 { 222 struct software_node *nodes = sensor->swnodes; 223 char vcm_name[ACPI_ID_LEN + 4]; 224 225 ipu_bridge_init_swnode_names(sensor); 226 227 nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name, 228 sensor->dev_properties); 229 nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port, 230 &nodes[SWNODE_SENSOR_HID]); 231 nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT( 232 sensor->node_names.endpoint, 233 &nodes[SWNODE_SENSOR_PORT], 234 sensor->ep_properties); 235 nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port, 236 &bridge->ipu_hid_node); 237 nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT( 238 sensor->node_names.endpoint, 239 &nodes[SWNODE_IPU_PORT], 240 sensor->ipu_properties); 241 if (sensor->ssdb.vcmtype) { 242 /* append ssdb.link to distinguish VCM nodes with same HID */ 243 snprintf(vcm_name, sizeof(vcm_name), "%s-%u", 244 ipu_vcm_types[sensor->ssdb.vcmtype - 1], 245 sensor->ssdb.link); 246 nodes[SWNODE_VCM] = NODE_VCM(vcm_name); 247 } 248 249 ipu_bridge_init_swnode_group(sensor); 250 } 251 252 static void ipu_bridge_instantiate_vcm_i2c_client(struct ipu_sensor *sensor) 253 { 254 struct i2c_board_info board_info = { }; 255 char name[16]; 256 257 if (!sensor->ssdb.vcmtype) 258 return; 259 260 snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev)); 261 board_info.dev_name = name; 262 strscpy(board_info.type, ipu_vcm_types[sensor->ssdb.vcmtype - 1], 263 ARRAY_SIZE(board_info.type)); 264 board_info.swnode = &sensor->swnodes[SWNODE_VCM]; 265 266 sensor->vcm_i2c_client = 267 i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev), 268 1, &board_info); 269 if (IS_ERR(sensor->vcm_i2c_client)) { 270 dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n", 271 PTR_ERR(sensor->vcm_i2c_client)); 272 sensor->vcm_i2c_client = NULL; 273 } 274 } 275 276 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge) 277 { 278 struct ipu_sensor *sensor; 279 unsigned int i; 280 281 for (i = 0; i < bridge->n_sensors; i++) { 282 sensor = &bridge->sensors[i]; 283 software_node_unregister_node_group(sensor->group); 284 ACPI_FREE(sensor->pld); 285 acpi_dev_put(sensor->adev); 286 i2c_unregister_device(sensor->vcm_i2c_client); 287 } 288 } 289 290 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg, 291 struct ipu_bridge *bridge, 292 struct pci_dev *ipu) 293 { 294 struct fwnode_handle *fwnode, *primary; 295 struct ipu_sensor *sensor; 296 struct acpi_device *adev; 297 acpi_status status; 298 int ret; 299 300 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) { 301 if (!adev->status.enabled) 302 continue; 303 304 if (bridge->n_sensors >= IPU_MAX_PORTS) { 305 acpi_dev_put(adev); 306 dev_err(&ipu->dev, "Exceeded available IPU ports\n"); 307 return -EINVAL; 308 } 309 310 sensor = &bridge->sensors[bridge->n_sensors]; 311 312 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", 313 &sensor->ssdb, 314 sizeof(sensor->ssdb)); 315 if (ret) 316 goto err_put_adev; 317 318 snprintf(sensor->name, sizeof(sensor->name), "%s-%u", 319 cfg->hid, sensor->ssdb.link); 320 321 if (sensor->ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) { 322 dev_warn(&adev->dev, "Unknown VCM type %d\n", 323 sensor->ssdb.vcmtype); 324 sensor->ssdb.vcmtype = 0; 325 } 326 327 status = acpi_get_physical_device_location(adev->handle, &sensor->pld); 328 if (ACPI_FAILURE(status)) { 329 ret = -ENODEV; 330 goto err_put_adev; 331 } 332 333 if (sensor->ssdb.lanes > IPU_MAX_LANES) { 334 dev_err(&adev->dev, 335 "Number of lanes in SSDB is invalid\n"); 336 ret = -EINVAL; 337 goto err_free_pld; 338 } 339 340 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg); 341 ipu_bridge_create_connection_swnodes(bridge, sensor); 342 343 ret = software_node_register_node_group(sensor->group); 344 if (ret) 345 goto err_free_pld; 346 347 fwnode = software_node_fwnode(&sensor->swnodes[ 348 SWNODE_SENSOR_HID]); 349 if (!fwnode) { 350 ret = -ENODEV; 351 goto err_free_swnodes; 352 } 353 354 sensor->adev = acpi_dev_get(adev); 355 356 primary = acpi_fwnode_handle(adev); 357 primary->secondary = fwnode; 358 359 ipu_bridge_instantiate_vcm_i2c_client(sensor); 360 361 dev_info(&ipu->dev, "Found supported sensor %s\n", 362 acpi_dev_name(adev)); 363 364 bridge->n_sensors++; 365 } 366 367 return 0; 368 369 err_free_swnodes: 370 software_node_unregister_node_group(sensor->group); 371 err_free_pld: 372 ACPI_FREE(sensor->pld); 373 err_put_adev: 374 acpi_dev_put(adev); 375 return ret; 376 } 377 378 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge, 379 struct pci_dev *ipu) 380 { 381 unsigned int i; 382 int ret; 383 384 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) { 385 const struct ipu_sensor_config *cfg = 386 &ipu_supported_sensors[i]; 387 388 ret = ipu_bridge_connect_sensor(cfg, bridge, ipu); 389 if (ret) 390 goto err_unregister_sensors; 391 } 392 393 return 0; 394 395 err_unregister_sensors: 396 ipu_bridge_unregister_sensors(bridge); 397 return ret; 398 } 399 400 /* 401 * The VCM cannot be probed until the PMIC is completely setup. We cannot rely 402 * on -EPROBE_DEFER for this, since the consumer<->supplier relations between 403 * the VCM and regulators/clks are not described in ACPI, instead they are 404 * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work 405 * for the clks/regulators the VCM i2c-clients must not be instantiated until 406 * the PMIC is fully setup. 407 * 408 * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the 409 * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does 410 * for the sensors. 411 */ 412 static int ipu_bridge_sensors_are_ready(void) 413 { 414 struct acpi_device *adev; 415 bool ready = true; 416 unsigned int i; 417 418 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) { 419 const struct ipu_sensor_config *cfg = 420 &ipu_supported_sensors[i]; 421 422 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) { 423 if (!adev->status.enabled) 424 continue; 425 426 if (!acpi_dev_ready_for_enumeration(adev)) 427 ready = false; 428 } 429 } 430 431 return ready; 432 } 433 434 int ipu_bridge_init(struct pci_dev *ipu) 435 { 436 struct device *dev = &ipu->dev; 437 struct fwnode_handle *fwnode; 438 struct ipu_bridge *bridge; 439 unsigned int i; 440 int ret; 441 442 if (!ipu_bridge_sensors_are_ready()) 443 return -EPROBE_DEFER; 444 445 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); 446 if (!bridge) 447 return -ENOMEM; 448 449 strscpy(bridge->ipu_node_name, IPU_HID, 450 sizeof(bridge->ipu_node_name)); 451 bridge->ipu_hid_node.name = bridge->ipu_node_name; 452 453 ret = software_node_register(&bridge->ipu_hid_node); 454 if (ret < 0) { 455 dev_err(dev, "Failed to register the IPU HID node\n"); 456 goto err_free_bridge; 457 } 458 459 /* 460 * Map the lane arrangement, which is fixed for the IPU3 (meaning we 461 * only need one, rather than one per sensor). We include it as a 462 * member of the struct ipu_bridge rather than a global variable so 463 * that it survives if the module is unloaded along with the rest of 464 * the struct. 465 */ 466 for (i = 0; i < IPU_MAX_LANES; i++) 467 bridge->data_lanes[i] = i + 1; 468 469 ret = ipu_bridge_connect_sensors(bridge, ipu); 470 if (ret || bridge->n_sensors == 0) 471 goto err_unregister_ipu; 472 473 dev_info(dev, "Connected %d cameras\n", bridge->n_sensors); 474 475 fwnode = software_node_fwnode(&bridge->ipu_hid_node); 476 if (!fwnode) { 477 dev_err(dev, "Error getting fwnode from ipu software_node\n"); 478 ret = -ENODEV; 479 goto err_unregister_sensors; 480 } 481 482 set_secondary_fwnode(dev, fwnode); 483 484 return 0; 485 486 err_unregister_sensors: 487 ipu_bridge_unregister_sensors(bridge); 488 err_unregister_ipu: 489 software_node_unregister(&bridge->ipu_hid_node); 490 err_free_bridge: 491 kfree(bridge); 492 493 return ret; 494 } 495 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE); 496