module: Convert symbol namespace to string literal
[linux.git] / drivers / media / pci / intel / ipu-bridge.c
blob1bf249f446a988769e7976ccdb0e179beadac30a
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>
7 #include <linux/i2c.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)
31 * IVSC device name
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[] = {
50 /* Himax HM11B1 */
51 IPU_SENSOR_CONFIG("HIMX11B1", 1, 384000000),
52 /* Himax HM2170 */
53 IPU_SENSOR_CONFIG("HIMX2170", 1, 384000000),
54 /* Himax HM2172 */
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),
66 /* Hynix Hi-556 */
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[] = {
99 "ad5823",
100 "dw9714",
101 "ad5816",
102 "dw9719",
103 "dw9718",
104 "dw9806b",
105 "wv517s",
106 "lc898122xa",
107 "lc898212axb",
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[] = {
116 { "INTC1059" },
117 { "INTC1095" },
118 { "INTC100A" },
119 { "INTC10CF" },
122 static struct acpi_device *ipu_bridge_get_ivsc_acpi_dev(struct acpi_device *adev)
124 unsigned int i;
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);
136 return ivsc_adev;
140 return NULL;
142 #else
143 static struct acpi_device *ipu_bridge_get_ivsc_acpi_dev(struct acpi_device *adev)
145 return NULL;
147 #endif
149 static int ipu_bridge_match_ivsc_dev(struct device *dev, const void *adev)
151 if (ACPI_COMPANION(dev) != adev)
152 return 0;
154 if (!sysfs_streq(dev_name(dev), IVSC_DEV_NAME))
155 return 0;
157 return 1;
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;
164 char name[64];
166 /* IVSC device on platform bus */
167 dev = bus_find_device(&platform_bus_type, NULL, adev,
168 ipu_bridge_match_ivsc_dev);
169 if (dev) {
170 snprintf(name, sizeof(name), "%s-%pUl", dev_name(dev), &uuid);
172 csi_dev = device_find_child_by_name(dev, name);
174 put_device(dev);
176 return csi_dev;
179 return NULL;
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);
189 if (adev) {
190 csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
191 if (!csi_dev) {
192 acpi_dev_put(adev);
193 dev_err(ADEV_DEV(adev), "Failed to find MEI CSI dev\n");
194 return -ENODEV;
197 sensor->csi_dev = csi_dev;
198 sensor->ivsc_adev = adev;
201 return 0;
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;
209 acpi_status status;
210 int ret = 0;
212 status = acpi_evaluate_object(ACPI_PTR(adev->handle),
213 id, NULL, &buffer);
214 if (ACPI_FAILURE(status))
215 return -ENODEV;
217 obj = buffer.pointer;
218 if (!obj) {
219 dev_err(ADEV_DEV(adev), "Couldn't locate ACPI buffer\n");
220 return -ENODEV;
223 if (obj->type != ACPI_TYPE_BUFFER) {
224 dev_err(ADEV_DEV(adev), "Not an ACPI buffer\n");
225 ret = -ENODEV;
226 goto out_free_buff;
229 if (obj->buffer.length > size) {
230 dev_err(ADEV_DEV(adev), "Given buffer is too small\n");
231 ret = -EINVAL;
232 goto out_free_buff;
235 memcpy(data, obj->buffer.pointer, obj->buffer.length);
237 out_free_buff:
238 kfree(buffer.pointer);
239 return ret;
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:
247 return 0;
248 case IPU_SENSOR_ROTATION_INVERTED:
249 return 180;
250 default:
251 dev_warn(ADEV_DEV(adev),
252 "Unknown rotation %d. Assume 0 degree rotation\n",
253 ssdb->degree);
254 return 0;
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);
266 #endif
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;
275 break;
276 case ACPI_PLD_PANEL_BACK:
277 orientation = V4L2_FWNODE_ORIENTATION_BACK;
278 break;
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;
284 break;
285 default:
286 dev_warn(ADEV_DEV(adev), "Unknown _PLD panel val %d\n",
287 pld->panel);
288 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
289 break;
292 ACPI_FREE(pld);
293 return orientation;
296 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
298 struct ipu_sensor_ssdb ssdb = {};
299 int ret;
301 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
302 if (ret)
303 return ret;
305 if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
306 dev_warn(ADEV_DEV(adev), "Unknown VCM type %d\n", ssdb.vcmtype);
307 ssdb.vcmtype = 0;
310 if (ssdb.lanes > IPU_MAX_LANES) {
311 dev_err(ADEV_DEV(adev), "Number of lanes in SSDB is invalid\n");
312 return -EINVAL;
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);
321 if (ssdb.vcmtype)
322 sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
324 return 0;
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,
353 bridge->data_lanes,
354 sensor->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,
364 bridge->data_lanes,
365 sensor->lanes);
366 sensor->ivsc_ipu_ep_properties[2] =
367 PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint,
368 sensor->ivsc_ipu_ref);
369 } else {
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,
378 sensor->mclkspeed);
379 sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
380 sensor->prop_names.rotation,
381 sensor->rotation);
382 sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
383 sensor->prop_names.orientation,
384 sensor->orientation);
385 if (sensor->vcm_type) {
386 sensor->vcm_ref[0] =
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,
400 sensor->local_ref);
402 if (cfg->nr_link_freqs > 0)
403 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
404 sensor->prop_names.link_frequencies,
405 cfg->link_freqs,
406 cfg->nr_link_freqs);
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,
413 sensor->remote_ref);
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];
469 } else {
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);
503 #endif
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;
538 char name[16];
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,
546 work);
547 struct acpi_device *adev = ACPI_COMPANION(data->sensor);
548 struct i2c_client *vcm_client;
549 bool put_fwnode = true;
550 int ret;
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);
557 if (ret < 0) {
558 dev_err(data->sensor, "Error %d runtime-resuming sensor, cannot instantiate VCM\n",
559 ret);
560 goto out_pm_put;
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));
572 goto out_pm_put;
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 */
580 out_pm_put:
581 pm_runtime_put(data->sensor);
582 put_device(data->sensor);
583 if (put_fwnode)
584 fwnode_handle_put(data->board_info.fwnode);
585 kfree(data);
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;
594 char *sep;
596 adev = ACPI_COMPANION(sensor);
597 if (!adev)
598 return 0;
600 vcm_fwnode = fwnode_find_reference(dev_fwnode(sensor), "lens-focus", 0);
601 if (IS_ERR(vcm_fwnode))
602 return 0;
604 /* When reloading modules the client will already exist */
605 vcm_client = i2c_find_device_by_fwnode(vcm_fwnode);
606 if (vcm_client) {
607 fwnode_handle_put(vcm_fwnode);
608 put_device(&vcm_client->dev);
609 return 0;
612 data = kzalloc(sizeof(*data), GFP_KERNEL);
613 if (!data) {
614 fwnode_handle_put(vcm_fwnode);
615 return -ENOMEM;
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, '-');
628 *sep = 0;
630 queue_work(system_long_wq, &data->work);
632 return 0;
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)
641 return 0;
643 fwnode = software_node_fwnode(&sensor->swnodes[SWNODE_IVSC_HID]);
644 if (!fwnode)
645 return -ENODEV;
647 set_secondary_fwnode(sensor->csi_dev, fwnode);
649 return 0;
652 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
654 struct ipu_sensor *sensor;
655 unsigned int i;
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;
672 int ret;
674 #if IS_ENABLED(CONFIG_ACPI)
675 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
676 #else
677 while (true) {
678 #endif
679 if (!ACPI_PTR(adev->status.enabled))
680 continue;
682 if (bridge->n_sensors >= IPU_MAX_PORTS) {
683 acpi_dev_put(adev);
684 dev_err(bridge->dev, "Exceeded available IPU ports\n");
685 return -EINVAL;
688 sensor = &bridge->sensors[bridge->n_sensors];
690 ret = bridge->parse_sensor_fwnode(adev, sensor);
691 if (ret)
692 goto err_put_adev;
694 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
695 cfg->hid, sensor->link);
697 ret = ipu_bridge_check_ivsc_dev(sensor, adev);
698 if (ret)
699 goto err_put_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);
705 if (ret)
706 goto err_put_ivsc;
708 fwnode = software_node_fwnode(&sensor->swnodes[
709 SWNODE_SENSOR_HID]);
710 if (!fwnode) {
711 ret = -ENODEV;
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);
721 if (ret)
722 goto err_free_swnodes;
724 dev_info(bridge->dev, "Found supported sensor %s\n",
725 acpi_dev_name(adev));
727 bridge->n_sensors++;
730 return 0;
732 err_free_swnodes:
733 software_node_unregister_node_group(sensor->group);
734 err_put_ivsc:
735 put_device(sensor->csi_dev);
736 acpi_dev_put(sensor->ivsc_adev);
737 err_put_adev:
738 acpi_dev_put(adev);
739 return ret;
742 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
744 unsigned int i;
745 int ret;
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);
752 if (ret)
753 goto err_unregister_sensors;
756 return 0;
758 err_unregister_sensors:
759 ipu_bridge_unregister_sensors(bridge);
760 return ret;
763 static int ipu_bridge_ivsc_is_ready(void)
765 struct acpi_device *sensor_adev, *adev;
766 struct device *csi_dev;
767 bool ready = true;
768 unsigned int i;
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) {
776 #else
777 while (true) {
778 sensor_adev = NULL;
779 #endif
780 if (!ACPI_PTR(sensor_adev->status.enabled))
781 continue;
783 adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev);
784 if (!adev)
785 continue;
787 csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
788 if (!csi_dev)
789 ready = false;
791 put_device(csi_dev);
792 acpi_dev_put(adev);
796 return ready;
799 static int ipu_bridge_check_fwnode_graph(struct fwnode_handle *fwnode)
801 struct fwnode_handle *endpoint;
803 if (IS_ERR_OR_NULL(fwnode))
804 return -EINVAL;
806 endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
807 if (endpoint) {
808 fwnode_handle_put(endpoint);
809 return 0;
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;
822 unsigned int i;
823 int ret;
825 guard(mutex)(&ipu_bridge_mutex);
827 if (!ipu_bridge_check_fwnode_graph(dev_fwnode(dev)))
828 return 0;
830 if (!ipu_bridge_ivsc_is_ready())
831 return -EPROBE_DEFER;
833 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
834 if (!bridge)
835 return -ENOMEM;
837 strscpy(bridge->ipu_node_name, IPU_HID,
838 sizeof(bridge->ipu_node_name));
839 bridge->ipu_hid_node.name = bridge->ipu_node_name;
840 bridge->dev = dev;
841 bridge->parse_sensor_fwnode = parse_sensor_fwnode;
843 ret = software_node_register(&bridge->ipu_hid_node);
844 if (ret < 0) {
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
854 * the struct.
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);
866 if (!fwnode) {
867 dev_err(dev, "Error getting fwnode from ipu software_node\n");
868 ret = -ENODEV;
869 goto err_unregister_sensors;
872 set_secondary_fwnode(dev, fwnode);
874 return 0;
876 err_unregister_sensors:
877 ipu_bridge_unregister_sensors(bridge);
878 err_unregister_ipu:
879 software_node_unregister(&bridge->ipu_hid_node);
880 err_free_bridge:
881 kfree(bridge);
883 return ret;
885 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, "INTEL_IPU_BRIDGE");
887 MODULE_LICENSE("GPL");
888 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");