1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
4 #include <linux/acpi.h>
5 #include <linux/cleanup.h>
6 #include <linux/device.h>
8 #include <linux/mei_cl_bus.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/property.h>
12 #include <linux/string.h>
13 #include <linux/workqueue.h>
15 #include <media/ipu-bridge.h>
16 #include <media/v4l2-fwnode.h>
18 #define ADEV_DEV(adev) ACPI_PTR(&((adev)->dev))
21 * 92335fcf-3203-4472-af93-7b4453ac29da
23 * Used to build MEI CSI device name to lookup MEI CSI device by
24 * device_find_child_by_name().
26 #define MEI_CSI_UUID \
27 UUID_LE(0x92335FCF, 0x3203, 0x4472, \
28 0xAF, 0x93, 0x7B, 0x44, 0x53, 0xAC, 0x29, 0xDA)
33 * Used to match IVSC device by ipu_bridge_match_ivsc_dev()
35 #define IVSC_DEV_NAME "intel_vsc"
38 * Extend this array with ACPI Hardware IDs of devices known to be working
39 * plus the number of link-frequencies expected by their drivers, along with
40 * the frequency values in hertz. This is somewhat opportunistic way of adding
41 * support for this for now in the hopes of a better source for the information
42 * (possibly some encoded value in the SSDB buffer that we're unaware of)
43 * becoming apparent in the future.
45 * Do not add an entry for a sensor that is not actually supported.
47 * Please keep the list sorted by ACPI HID.
49 static const struct ipu_sensor_config ipu_supported_sensors
[] = {
51 IPU_SENSOR_CONFIG("HIMX11B1", 1, 384000000),
53 IPU_SENSOR_CONFIG("HIMX2170", 1, 384000000),
55 IPU_SENSOR_CONFIG("HIMX2172", 1, 384000000),
56 /* GalaxyCore GC0310 */
57 IPU_SENSOR_CONFIG("INT0310", 0),
58 /* Omnivision OV5693 */
59 IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
60 /* Omnivision OV2740 */
61 IPU_SENSOR_CONFIG("INT3474", 1, 180000000),
62 /* Omnivision OV8865 */
63 IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
64 /* Omnivision OV7251 */
65 IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
67 IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
68 /* Omnivision OV01A10 / OV01A1S */
69 IPU_SENSOR_CONFIG("OVTI01A0", 1, 400000000),
70 IPU_SENSOR_CONFIG("OVTI01AS", 1, 400000000),
71 /* Omnivision OV02C10 */
72 IPU_SENSOR_CONFIG("OVTI02C1", 1, 400000000),
73 /* Omnivision OV02E10 */
74 IPU_SENSOR_CONFIG("OVTI02E1", 1, 360000000),
75 /* Omnivision OV08A10 */
76 IPU_SENSOR_CONFIG("OVTI08A1", 1, 500000000),
77 /* Omnivision OV08x40 */
78 IPU_SENSOR_CONFIG("OVTI08F4", 1, 400000000),
79 /* Omnivision OV13B10 */
80 IPU_SENSOR_CONFIG("OVTI13B1", 1, 560000000),
81 IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
82 /* Omnivision OV2680 */
83 IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000),
84 /* Omnivision OV8856 */
85 IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
88 static const struct ipu_property_names prop_names
= {
89 .clock_frequency
= "clock-frequency",
90 .rotation
= "rotation",
91 .orientation
= "orientation",
92 .bus_type
= "bus-type",
93 .data_lanes
= "data-lanes",
94 .remote_endpoint
= "remote-endpoint",
95 .link_frequencies
= "link-frequencies",
98 static const char * const ipu_vcm_types
[] = {
110 #if IS_ENABLED(CONFIG_ACPI)
112 * Used to figure out IVSC acpi device by ipu_bridge_get_ivsc_acpi_dev()
113 * instead of device and driver match to probe IVSC device.
115 static const struct acpi_device_id ivsc_acpi_ids
[] = {
122 static struct acpi_device
*ipu_bridge_get_ivsc_acpi_dev(struct acpi_device
*adev
)
126 for (i
= 0; i
< ARRAY_SIZE(ivsc_acpi_ids
); i
++) {
127 const struct acpi_device_id
*acpi_id
= &ivsc_acpi_ids
[i
];
128 struct acpi_device
*consumer
, *ivsc_adev
;
130 acpi_handle handle
= acpi_device_handle(adev
);
131 for_each_acpi_dev_match(ivsc_adev
, acpi_id
->id
, NULL
, -1)
132 /* camera sensor depends on IVSC in DSDT if exist */
133 for_each_acpi_consumer_dev(ivsc_adev
, consumer
)
134 if (consumer
->handle
== handle
) {
135 acpi_dev_put(consumer
);
143 static struct acpi_device
*ipu_bridge_get_ivsc_acpi_dev(struct acpi_device
*adev
)
149 static int ipu_bridge_match_ivsc_dev(struct device
*dev
, const void *adev
)
151 if (ACPI_COMPANION(dev
) != adev
)
154 if (!sysfs_streq(dev_name(dev
), IVSC_DEV_NAME
))
160 static struct device
*ipu_bridge_get_ivsc_csi_dev(struct acpi_device
*adev
)
162 struct device
*dev
, *csi_dev
;
163 uuid_le uuid
= MEI_CSI_UUID
;
166 /* IVSC device on platform bus */
167 dev
= bus_find_device(&platform_bus_type
, NULL
, adev
,
168 ipu_bridge_match_ivsc_dev
);
170 snprintf(name
, sizeof(name
), "%s-%pUl", dev_name(dev
), &uuid
);
172 csi_dev
= device_find_child_by_name(dev
, name
);
182 static int ipu_bridge_check_ivsc_dev(struct ipu_sensor
*sensor
,
183 struct acpi_device
*sensor_adev
)
185 struct acpi_device
*adev
;
186 struct device
*csi_dev
;
188 adev
= ipu_bridge_get_ivsc_acpi_dev(sensor_adev
);
190 csi_dev
= ipu_bridge_get_ivsc_csi_dev(adev
);
193 dev_err(ADEV_DEV(adev
), "Failed to find MEI CSI dev\n");
197 sensor
->csi_dev
= csi_dev
;
198 sensor
->ivsc_adev
= adev
;
204 static int ipu_bridge_read_acpi_buffer(struct acpi_device
*adev
, char *id
,
205 void *data
, u32 size
)
207 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
208 union acpi_object
*obj
;
212 status
= acpi_evaluate_object(ACPI_PTR(adev
->handle
),
214 if (ACPI_FAILURE(status
))
217 obj
= buffer
.pointer
;
219 dev_err(ADEV_DEV(adev
), "Couldn't locate ACPI buffer\n");
223 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
224 dev_err(ADEV_DEV(adev
), "Not an ACPI buffer\n");
229 if (obj
->buffer
.length
> size
) {
230 dev_err(ADEV_DEV(adev
), "Given buffer is too small\n");
235 memcpy(data
, obj
->buffer
.pointer
, obj
->buffer
.length
);
238 kfree(buffer
.pointer
);
242 static u32
ipu_bridge_parse_rotation(struct acpi_device
*adev
,
243 struct ipu_sensor_ssdb
*ssdb
)
245 switch (ssdb
->degree
) {
246 case IPU_SENSOR_ROTATION_NORMAL
:
248 case IPU_SENSOR_ROTATION_INVERTED
:
251 dev_warn(ADEV_DEV(adev
),
252 "Unknown rotation %d. Assume 0 degree rotation\n",
258 static enum v4l2_fwnode_orientation
ipu_bridge_parse_orientation(struct acpi_device
*adev
)
260 enum v4l2_fwnode_orientation orientation
;
261 struct acpi_pld_info
*pld
= NULL
;
262 acpi_status status
= AE_ERROR
;
264 #if IS_ENABLED(CONFIG_ACPI)
265 status
= acpi_get_physical_device_location(adev
->handle
, &pld
);
267 if (ACPI_FAILURE(status
)) {
268 dev_warn(ADEV_DEV(adev
), "_PLD call failed, using default orientation\n");
269 return V4L2_FWNODE_ORIENTATION_EXTERNAL
;
272 switch (pld
->panel
) {
273 case ACPI_PLD_PANEL_FRONT
:
274 orientation
= V4L2_FWNODE_ORIENTATION_FRONT
;
276 case ACPI_PLD_PANEL_BACK
:
277 orientation
= V4L2_FWNODE_ORIENTATION_BACK
;
279 case ACPI_PLD_PANEL_TOP
:
280 case ACPI_PLD_PANEL_LEFT
:
281 case ACPI_PLD_PANEL_RIGHT
:
282 case ACPI_PLD_PANEL_UNKNOWN
:
283 orientation
= V4L2_FWNODE_ORIENTATION_EXTERNAL
;
286 dev_warn(ADEV_DEV(adev
), "Unknown _PLD panel val %d\n",
288 orientation
= V4L2_FWNODE_ORIENTATION_EXTERNAL
;
296 int ipu_bridge_parse_ssdb(struct acpi_device
*adev
, struct ipu_sensor
*sensor
)
298 struct ipu_sensor_ssdb ssdb
= {};
301 ret
= ipu_bridge_read_acpi_buffer(adev
, "SSDB", &ssdb
, sizeof(ssdb
));
305 if (ssdb
.vcmtype
> ARRAY_SIZE(ipu_vcm_types
)) {
306 dev_warn(ADEV_DEV(adev
), "Unknown VCM type %d\n", ssdb
.vcmtype
);
310 if (ssdb
.lanes
> IPU_MAX_LANES
) {
311 dev_err(ADEV_DEV(adev
), "Number of lanes in SSDB is invalid\n");
315 sensor
->link
= ssdb
.link
;
316 sensor
->lanes
= ssdb
.lanes
;
317 sensor
->mclkspeed
= ssdb
.mclkspeed
;
318 sensor
->rotation
= ipu_bridge_parse_rotation(adev
, &ssdb
);
319 sensor
->orientation
= ipu_bridge_parse_orientation(adev
);
322 sensor
->vcm_type
= ipu_vcm_types
[ssdb
.vcmtype
- 1];
326 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb
, "INTEL_IPU_BRIDGE");
328 static void ipu_bridge_create_fwnode_properties(
329 struct ipu_sensor
*sensor
,
330 struct ipu_bridge
*bridge
,
331 const struct ipu_sensor_config
*cfg
)
333 struct ipu_property_names
*names
= &sensor
->prop_names
;
334 struct software_node
*nodes
= sensor
->swnodes
;
336 sensor
->prop_names
= prop_names
;
338 if (sensor
->csi_dev
) {
339 sensor
->local_ref
[0] =
340 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_IVSC_SENSOR_ENDPOINT
]);
341 sensor
->remote_ref
[0] =
342 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_IVSC_IPU_ENDPOINT
]);
343 sensor
->ivsc_sensor_ref
[0] =
344 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_SENSOR_ENDPOINT
]);
345 sensor
->ivsc_ipu_ref
[0] =
346 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_IPU_ENDPOINT
]);
348 sensor
->ivsc_sensor_ep_properties
[0] =
349 PROPERTY_ENTRY_U32(names
->bus_type
,
350 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY
);
351 sensor
->ivsc_sensor_ep_properties
[1] =
352 PROPERTY_ENTRY_U32_ARRAY_LEN(names
->data_lanes
,
355 sensor
->ivsc_sensor_ep_properties
[2] =
356 PROPERTY_ENTRY_REF_ARRAY(names
->remote_endpoint
,
357 sensor
->ivsc_sensor_ref
);
359 sensor
->ivsc_ipu_ep_properties
[0] =
360 PROPERTY_ENTRY_U32(names
->bus_type
,
361 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY
);
362 sensor
->ivsc_ipu_ep_properties
[1] =
363 PROPERTY_ENTRY_U32_ARRAY_LEN(names
->data_lanes
,
366 sensor
->ivsc_ipu_ep_properties
[2] =
367 PROPERTY_ENTRY_REF_ARRAY(names
->remote_endpoint
,
368 sensor
->ivsc_ipu_ref
);
370 sensor
->local_ref
[0] =
371 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_IPU_ENDPOINT
]);
372 sensor
->remote_ref
[0] =
373 SOFTWARE_NODE_REFERENCE(&nodes
[SWNODE_SENSOR_ENDPOINT
]);
376 sensor
->dev_properties
[0] = PROPERTY_ENTRY_U32(
377 sensor
->prop_names
.clock_frequency
,
379 sensor
->dev_properties
[1] = PROPERTY_ENTRY_U32(
380 sensor
->prop_names
.rotation
,
382 sensor
->dev_properties
[2] = PROPERTY_ENTRY_U32(
383 sensor
->prop_names
.orientation
,
384 sensor
->orientation
);
385 if (sensor
->vcm_type
) {
387 SOFTWARE_NODE_REFERENCE(&sensor
->swnodes
[SWNODE_VCM
]);
388 sensor
->dev_properties
[3] =
389 PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor
->vcm_ref
);
392 sensor
->ep_properties
[0] = PROPERTY_ENTRY_U32(
393 sensor
->prop_names
.bus_type
,
394 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY
);
395 sensor
->ep_properties
[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
396 sensor
->prop_names
.data_lanes
,
397 bridge
->data_lanes
, sensor
->lanes
);
398 sensor
->ep_properties
[2] = PROPERTY_ENTRY_REF_ARRAY(
399 sensor
->prop_names
.remote_endpoint
,
402 if (cfg
->nr_link_freqs
> 0)
403 sensor
->ep_properties
[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
404 sensor
->prop_names
.link_frequencies
,
408 sensor
->ipu_properties
[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
409 sensor
->prop_names
.data_lanes
,
410 bridge
->data_lanes
, sensor
->lanes
);
411 sensor
->ipu_properties
[1] = PROPERTY_ENTRY_REF_ARRAY(
412 sensor
->prop_names
.remote_endpoint
,
416 static void ipu_bridge_init_swnode_names(struct ipu_sensor
*sensor
)
418 snprintf(sensor
->node_names
.remote_port
,
419 sizeof(sensor
->node_names
.remote_port
),
420 SWNODE_GRAPH_PORT_NAME_FMT
, sensor
->link
);
421 snprintf(sensor
->node_names
.port
,
422 sizeof(sensor
->node_names
.port
),
423 SWNODE_GRAPH_PORT_NAME_FMT
, 0); /* Always port 0 */
424 snprintf(sensor
->node_names
.endpoint
,
425 sizeof(sensor
->node_names
.endpoint
),
426 SWNODE_GRAPH_ENDPOINT_NAME_FMT
, 0); /* And endpoint 0 */
427 if (sensor
->vcm_type
) {
428 /* append link to distinguish nodes with same model VCM */
429 snprintf(sensor
->node_names
.vcm
, sizeof(sensor
->node_names
.vcm
),
430 "%s-%u", sensor
->vcm_type
, sensor
->link
);
433 if (sensor
->csi_dev
) {
434 snprintf(sensor
->node_names
.ivsc_sensor_port
,
435 sizeof(sensor
->node_names
.ivsc_sensor_port
),
436 SWNODE_GRAPH_PORT_NAME_FMT
, 0);
437 snprintf(sensor
->node_names
.ivsc_ipu_port
,
438 sizeof(sensor
->node_names
.ivsc_ipu_port
),
439 SWNODE_GRAPH_PORT_NAME_FMT
, 1);
443 static void ipu_bridge_init_swnode_group(struct ipu_sensor
*sensor
)
445 struct software_node
*nodes
= sensor
->swnodes
;
447 sensor
->group
[SWNODE_SENSOR_HID
] = &nodes
[SWNODE_SENSOR_HID
];
448 sensor
->group
[SWNODE_SENSOR_PORT
] = &nodes
[SWNODE_SENSOR_PORT
];
449 sensor
->group
[SWNODE_SENSOR_ENDPOINT
] = &nodes
[SWNODE_SENSOR_ENDPOINT
];
450 sensor
->group
[SWNODE_IPU_PORT
] = &nodes
[SWNODE_IPU_PORT
];
451 sensor
->group
[SWNODE_IPU_ENDPOINT
] = &nodes
[SWNODE_IPU_ENDPOINT
];
452 if (sensor
->vcm_type
)
453 sensor
->group
[SWNODE_VCM
] = &nodes
[SWNODE_VCM
];
455 if (sensor
->csi_dev
) {
456 sensor
->group
[SWNODE_IVSC_HID
] =
457 &nodes
[SWNODE_IVSC_HID
];
458 sensor
->group
[SWNODE_IVSC_SENSOR_PORT
] =
459 &nodes
[SWNODE_IVSC_SENSOR_PORT
];
460 sensor
->group
[SWNODE_IVSC_SENSOR_ENDPOINT
] =
461 &nodes
[SWNODE_IVSC_SENSOR_ENDPOINT
];
462 sensor
->group
[SWNODE_IVSC_IPU_PORT
] =
463 &nodes
[SWNODE_IVSC_IPU_PORT
];
464 sensor
->group
[SWNODE_IVSC_IPU_ENDPOINT
] =
465 &nodes
[SWNODE_IVSC_IPU_ENDPOINT
];
467 if (sensor
->vcm_type
)
468 sensor
->group
[SWNODE_VCM
] = &nodes
[SWNODE_VCM
];
470 if (sensor
->vcm_type
)
471 sensor
->group
[SWNODE_IVSC_HID
] = &nodes
[SWNODE_VCM
];
475 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge
*bridge
,
476 struct ipu_sensor
*sensor
)
478 struct ipu_node_names
*names
= &sensor
->node_names
;
479 struct software_node
*nodes
= sensor
->swnodes
;
481 ipu_bridge_init_swnode_names(sensor
);
483 nodes
[SWNODE_SENSOR_HID
] = NODE_SENSOR(sensor
->name
,
484 sensor
->dev_properties
);
485 nodes
[SWNODE_SENSOR_PORT
] = NODE_PORT(sensor
->node_names
.port
,
486 &nodes
[SWNODE_SENSOR_HID
]);
487 nodes
[SWNODE_SENSOR_ENDPOINT
] = NODE_ENDPOINT(
488 sensor
->node_names
.endpoint
,
489 &nodes
[SWNODE_SENSOR_PORT
],
490 sensor
->ep_properties
);
491 nodes
[SWNODE_IPU_PORT
] = NODE_PORT(sensor
->node_names
.remote_port
,
492 &bridge
->ipu_hid_node
);
493 nodes
[SWNODE_IPU_ENDPOINT
] = NODE_ENDPOINT(
494 sensor
->node_names
.endpoint
,
495 &nodes
[SWNODE_IPU_PORT
],
496 sensor
->ipu_properties
);
498 if (sensor
->csi_dev
) {
499 const char *device_hid
= "";
501 #if IS_ENABLED(CONFIG_ACPI)
502 device_hid
= acpi_device_hid(sensor
->ivsc_adev
);
505 snprintf(sensor
->ivsc_name
, sizeof(sensor
->ivsc_name
), "%s-%u",
506 device_hid
, sensor
->link
);
508 nodes
[SWNODE_IVSC_HID
] = NODE_SENSOR(sensor
->ivsc_name
,
509 sensor
->ivsc_properties
);
510 nodes
[SWNODE_IVSC_SENSOR_PORT
] =
511 NODE_PORT(names
->ivsc_sensor_port
,
512 &nodes
[SWNODE_IVSC_HID
]);
513 nodes
[SWNODE_IVSC_SENSOR_ENDPOINT
] =
514 NODE_ENDPOINT(names
->endpoint
,
515 &nodes
[SWNODE_IVSC_SENSOR_PORT
],
516 sensor
->ivsc_sensor_ep_properties
);
517 nodes
[SWNODE_IVSC_IPU_PORT
] =
518 NODE_PORT(names
->ivsc_ipu_port
,
519 &nodes
[SWNODE_IVSC_HID
]);
520 nodes
[SWNODE_IVSC_IPU_ENDPOINT
] =
521 NODE_ENDPOINT(names
->endpoint
,
522 &nodes
[SWNODE_IVSC_IPU_PORT
],
523 sensor
->ivsc_ipu_ep_properties
);
526 nodes
[SWNODE_VCM
] = NODE_VCM(sensor
->node_names
.vcm
);
528 ipu_bridge_init_swnode_group(sensor
);
532 * The actual instantiation must be done from a workqueue to avoid
533 * a deadlock on taking list_lock from v4l2-async twice.
535 struct ipu_bridge_instantiate_vcm_work_data
{
536 struct work_struct work
;
537 struct device
*sensor
;
539 struct i2c_board_info board_info
;
542 static void ipu_bridge_instantiate_vcm_work(struct work_struct
*work
)
544 struct ipu_bridge_instantiate_vcm_work_data
*data
=
545 container_of(work
, struct ipu_bridge_instantiate_vcm_work_data
,
547 struct acpi_device
*adev
= ACPI_COMPANION(data
->sensor
);
548 struct i2c_client
*vcm_client
;
549 bool put_fwnode
= true;
553 * The client may get probed before the device_link gets added below
554 * make sure the sensor is powered-up during probe.
556 ret
= pm_runtime_get_sync(data
->sensor
);
558 dev_err(data
->sensor
, "Error %d runtime-resuming sensor, cannot instantiate VCM\n",
564 * Note the client is created only once and then kept around
565 * even after a rmmod, just like the software-nodes.
567 vcm_client
= i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(adev
),
568 1, &data
->board_info
);
569 if (IS_ERR(vcm_client
)) {
570 dev_err(data
->sensor
, "Error instantiating VCM client: %ld\n",
571 PTR_ERR(vcm_client
));
575 device_link_add(&vcm_client
->dev
, data
->sensor
, DL_FLAG_PM_RUNTIME
);
577 dev_info(data
->sensor
, "Instantiated %s VCM\n", data
->board_info
.type
);
578 put_fwnode
= false; /* Ownership has passed to the i2c-client */
581 pm_runtime_put(data
->sensor
);
582 put_device(data
->sensor
);
584 fwnode_handle_put(data
->board_info
.fwnode
);
588 int ipu_bridge_instantiate_vcm(struct device
*sensor
)
590 struct ipu_bridge_instantiate_vcm_work_data
*data
;
591 struct fwnode_handle
*vcm_fwnode
;
592 struct i2c_client
*vcm_client
;
593 struct acpi_device
*adev
;
596 adev
= ACPI_COMPANION(sensor
);
600 vcm_fwnode
= fwnode_find_reference(dev_fwnode(sensor
), "lens-focus", 0);
601 if (IS_ERR(vcm_fwnode
))
604 /* When reloading modules the client will already exist */
605 vcm_client
= i2c_find_device_by_fwnode(vcm_fwnode
);
607 fwnode_handle_put(vcm_fwnode
);
608 put_device(&vcm_client
->dev
);
612 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
614 fwnode_handle_put(vcm_fwnode
);
618 INIT_WORK(&data
->work
, ipu_bridge_instantiate_vcm_work
);
619 data
->sensor
= get_device(sensor
);
620 snprintf(data
->name
, sizeof(data
->name
), "%s-VCM",
621 acpi_dev_name(adev
));
622 data
->board_info
.dev_name
= data
->name
;
623 data
->board_info
.fwnode
= vcm_fwnode
;
624 snprintf(data
->board_info
.type
, sizeof(data
->board_info
.type
),
625 "%pfwP", vcm_fwnode
);
626 /* Strip "-<link>" postfix */
627 sep
= strchrnul(data
->board_info
.type
, '-');
630 queue_work(system_long_wq
, &data
->work
);
634 EXPORT_SYMBOL_NS_GPL(ipu_bridge_instantiate_vcm
, "INTEL_IPU_BRIDGE");
636 static int ipu_bridge_instantiate_ivsc(struct ipu_sensor
*sensor
)
638 struct fwnode_handle
*fwnode
;
640 if (!sensor
->csi_dev
)
643 fwnode
= software_node_fwnode(&sensor
->swnodes
[SWNODE_IVSC_HID
]);
647 set_secondary_fwnode(sensor
->csi_dev
, fwnode
);
652 static void ipu_bridge_unregister_sensors(struct ipu_bridge
*bridge
)
654 struct ipu_sensor
*sensor
;
657 for (i
= 0; i
< bridge
->n_sensors
; i
++) {
658 sensor
= &bridge
->sensors
[i
];
659 software_node_unregister_node_group(sensor
->group
);
660 acpi_dev_put(sensor
->adev
);
661 put_device(sensor
->csi_dev
);
662 acpi_dev_put(sensor
->ivsc_adev
);
666 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config
*cfg
,
667 struct ipu_bridge
*bridge
)
669 struct fwnode_handle
*fwnode
, *primary
;
670 struct ipu_sensor
*sensor
;
671 struct acpi_device
*adev
= NULL
;
674 #if IS_ENABLED(CONFIG_ACPI)
675 for_each_acpi_dev_match(adev
, cfg
->hid
, NULL
, -1) {
679 if (!ACPI_PTR(adev
->status
.enabled
))
682 if (bridge
->n_sensors
>= IPU_MAX_PORTS
) {
684 dev_err(bridge
->dev
, "Exceeded available IPU ports\n");
688 sensor
= &bridge
->sensors
[bridge
->n_sensors
];
690 ret
= bridge
->parse_sensor_fwnode(adev
, sensor
);
694 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s-%u",
695 cfg
->hid
, sensor
->link
);
697 ret
= ipu_bridge_check_ivsc_dev(sensor
, adev
);
701 ipu_bridge_create_fwnode_properties(sensor
, bridge
, cfg
);
702 ipu_bridge_create_connection_swnodes(bridge
, sensor
);
704 ret
= software_node_register_node_group(sensor
->group
);
708 fwnode
= software_node_fwnode(&sensor
->swnodes
[
712 goto err_free_swnodes
;
715 sensor
->adev
= ACPI_PTR(acpi_dev_get(adev
));
717 primary
= acpi_fwnode_handle(adev
);
718 primary
->secondary
= fwnode
;
720 ret
= ipu_bridge_instantiate_ivsc(sensor
);
722 goto err_free_swnodes
;
724 dev_info(bridge
->dev
, "Found supported sensor %s\n",
725 acpi_dev_name(adev
));
733 software_node_unregister_node_group(sensor
->group
);
735 put_device(sensor
->csi_dev
);
736 acpi_dev_put(sensor
->ivsc_adev
);
742 static int ipu_bridge_connect_sensors(struct ipu_bridge
*bridge
)
747 for (i
= 0; i
< ARRAY_SIZE(ipu_supported_sensors
); i
++) {
748 const struct ipu_sensor_config
*cfg
=
749 &ipu_supported_sensors
[i
];
751 ret
= ipu_bridge_connect_sensor(cfg
, bridge
);
753 goto err_unregister_sensors
;
758 err_unregister_sensors
:
759 ipu_bridge_unregister_sensors(bridge
);
763 static int ipu_bridge_ivsc_is_ready(void)
765 struct acpi_device
*sensor_adev
, *adev
;
766 struct device
*csi_dev
;
770 for (i
= 0; i
< ARRAY_SIZE(ipu_supported_sensors
); i
++) {
771 #if IS_ENABLED(CONFIG_ACPI)
772 const struct ipu_sensor_config
*cfg
=
773 &ipu_supported_sensors
[i
];
775 for_each_acpi_dev_match(sensor_adev
, cfg
->hid
, NULL
, -1) {
780 if (!ACPI_PTR(sensor_adev
->status
.enabled
))
783 adev
= ipu_bridge_get_ivsc_acpi_dev(sensor_adev
);
787 csi_dev
= ipu_bridge_get_ivsc_csi_dev(adev
);
799 static int ipu_bridge_check_fwnode_graph(struct fwnode_handle
*fwnode
)
801 struct fwnode_handle
*endpoint
;
803 if (IS_ERR_OR_NULL(fwnode
))
806 endpoint
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
808 fwnode_handle_put(endpoint
);
812 return ipu_bridge_check_fwnode_graph(fwnode
->secondary
);
815 static DEFINE_MUTEX(ipu_bridge_mutex
);
817 int ipu_bridge_init(struct device
*dev
,
818 ipu_parse_sensor_fwnode_t parse_sensor_fwnode
)
820 struct fwnode_handle
*fwnode
;
821 struct ipu_bridge
*bridge
;
825 guard(mutex
)(&ipu_bridge_mutex
);
827 if (!ipu_bridge_check_fwnode_graph(dev_fwnode(dev
)))
830 if (!ipu_bridge_ivsc_is_ready())
831 return -EPROBE_DEFER
;
833 bridge
= kzalloc(sizeof(*bridge
), GFP_KERNEL
);
837 strscpy(bridge
->ipu_node_name
, IPU_HID
,
838 sizeof(bridge
->ipu_node_name
));
839 bridge
->ipu_hid_node
.name
= bridge
->ipu_node_name
;
841 bridge
->parse_sensor_fwnode
= parse_sensor_fwnode
;
843 ret
= software_node_register(&bridge
->ipu_hid_node
);
845 dev_err(dev
, "Failed to register the IPU HID node\n");
846 goto err_free_bridge
;
850 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
851 * only need one, rather than one per sensor). We include it as a
852 * member of the struct ipu_bridge rather than a global variable so
853 * that it survives if the module is unloaded along with the rest of
856 for (i
= 0; i
< IPU_MAX_LANES
; i
++)
857 bridge
->data_lanes
[i
] = i
+ 1;
859 ret
= ipu_bridge_connect_sensors(bridge
);
860 if (ret
|| bridge
->n_sensors
== 0)
861 goto err_unregister_ipu
;
863 dev_info(dev
, "Connected %d cameras\n", bridge
->n_sensors
);
865 fwnode
= software_node_fwnode(&bridge
->ipu_hid_node
);
867 dev_err(dev
, "Error getting fwnode from ipu software_node\n");
869 goto err_unregister_sensors
;
872 set_secondary_fwnode(dev
, fwnode
);
876 err_unregister_sensors
:
877 ipu_bridge_unregister_sensors(bridge
);
879 software_node_unregister(&bridge
->ipu_hid_node
);
885 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init
, "INTEL_IPU_BRIDGE");
887 MODULE_LICENSE("GPL");
888 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");