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/mei_cl_bus.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/property.h>
11 #include <linux/string.h>
12 #include <linux/workqueue.h>
13 
14 #include <media/ipu-bridge.h>
15 #include <media/v4l2-fwnode.h>
16 
17 /*
18  * 92335fcf-3203-4472-af93-7b4453ac29da
19  *
20  * Used to build MEI CSI device name to lookup MEI CSI device by
21  * device_find_child_by_name().
22  */
23 #define MEI_CSI_UUID							\
24 	UUID_LE(0x92335FCF, 0x3203, 0x4472,				\
25 		0xAF, 0x93, 0x7B, 0x44, 0x53, 0xAC, 0x29, 0xDA)
26 
27 /*
28  * IVSC device name
29  *
30  * Used to match IVSC device by ipu_bridge_match_ivsc_dev()
31  */
32 #define IVSC_DEV_NAME "intel_vsc"
33 
34 /*
35  * Extend this array with ACPI Hardware IDs of devices known to be working
36  * plus the number of link-frequencies expected by their drivers, along with
37  * the frequency values in hertz. This is somewhat opportunistic way of adding
38  * support for this for now in the hopes of a better source for the information
39  * (possibly some encoded value in the SSDB buffer that we're unaware of)
40  * becoming apparent in the future.
41  *
42  * Do not add an entry for a sensor that is not actually supported.
43  */
44 static const struct ipu_sensor_config ipu_supported_sensors[] = {
45 	/* Omnivision OV5693 */
46 	IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
47 	/* Omnivision OV8865 */
48 	IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
49 	/* Omnivision OV7251 */
50 	IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
51 	/* Omnivision OV2680 */
52 	IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000),
53 	/* Omnivision ov8856 */
54 	IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
55 	/* Omnivision ov2740 */
56 	IPU_SENSOR_CONFIG("INT3474", 1, 360000000),
57 	/* Hynix hi556 */
58 	IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
59 	/* Omnivision ov13b10 */
60 	IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
61 	/* GalaxyCore GC0310 */
62 	IPU_SENSOR_CONFIG("INT0310", 0),
63 };
64 
65 static const struct ipu_property_names prop_names = {
66 	.clock_frequency = "clock-frequency",
67 	.rotation = "rotation",
68 	.orientation = "orientation",
69 	.bus_type = "bus-type",
70 	.data_lanes = "data-lanes",
71 	.remote_endpoint = "remote-endpoint",
72 	.link_frequencies = "link-frequencies",
73 };
74 
75 static const char * const ipu_vcm_types[] = {
76 	"ad5823",
77 	"dw9714",
78 	"ad5816",
79 	"dw9719",
80 	"dw9718",
81 	"dw9806b",
82 	"wv517s",
83 	"lc898122xa",
84 	"lc898212axb",
85 };
86 
87 /*
88  * Used to figure out IVSC acpi device by ipu_bridge_get_ivsc_acpi_dev()
89  * instead of device and driver match to probe IVSC device.
90  */
91 static const struct acpi_device_id ivsc_acpi_ids[] = {
92 	{ "INTC1059" },
93 	{ "INTC1095" },
94 	{ "INTC100A" },
95 	{ "INTC10CF" },
96 };
97 
98 static struct acpi_device *ipu_bridge_get_ivsc_acpi_dev(struct acpi_device *adev)
99 {
100 	acpi_handle handle = acpi_device_handle(adev);
101 	struct acpi_device *consumer, *ivsc_adev;
102 	unsigned int i;
103 
104 	for (i = 0; i < ARRAY_SIZE(ivsc_acpi_ids); i++) {
105 		const struct acpi_device_id *acpi_id = &ivsc_acpi_ids[i];
106 
107 		for_each_acpi_dev_match(ivsc_adev, acpi_id->id, NULL, -1)
108 			/* camera sensor depends on IVSC in DSDT if exist */
109 			for_each_acpi_consumer_dev(ivsc_adev, consumer)
110 				if (consumer->handle == handle)
111 					return ivsc_adev;
112 	}
113 
114 	return NULL;
115 }
116 
117 static int ipu_bridge_match_ivsc_dev(struct device *dev, const void *adev)
118 {
119 	if (ACPI_COMPANION(dev) != adev)
120 		return 0;
121 
122 	if (!sysfs_streq(dev_name(dev), IVSC_DEV_NAME))
123 		return 0;
124 
125 	return 1;
126 }
127 
128 static struct device *ipu_bridge_get_ivsc_csi_dev(struct acpi_device *adev)
129 {
130 	struct device *dev, *csi_dev;
131 	uuid_le uuid = MEI_CSI_UUID;
132 	char name[64];
133 
134 	/* IVSC device on platform bus */
135 	dev = bus_find_device(&platform_bus_type, NULL, adev,
136 			      ipu_bridge_match_ivsc_dev);
137 	if (dev) {
138 		snprintf(name, sizeof(name), "%s-%pUl", dev_name(dev), &uuid);
139 
140 		csi_dev = device_find_child_by_name(dev, name);
141 
142 		put_device(dev);
143 
144 		return csi_dev;
145 	}
146 
147 	return NULL;
148 }
149 
150 static int ipu_bridge_check_ivsc_dev(struct ipu_sensor *sensor,
151 				     struct acpi_device *sensor_adev)
152 {
153 	struct acpi_device *adev;
154 	struct device *csi_dev;
155 
156 	adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev);
157 	if (adev) {
158 		csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
159 		if (!csi_dev) {
160 			acpi_dev_put(adev);
161 			dev_err(&adev->dev, "Failed to find MEI CSI dev\n");
162 			return -ENODEV;
163 		}
164 
165 		sensor->csi_dev = csi_dev;
166 		sensor->ivsc_adev = adev;
167 	}
168 
169 	return 0;
170 }
171 
172 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
173 				       void *data, u32 size)
174 {
175 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
176 	union acpi_object *obj;
177 	acpi_status status;
178 	int ret = 0;
179 
180 	status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
181 	if (ACPI_FAILURE(status))
182 		return -ENODEV;
183 
184 	obj = buffer.pointer;
185 	if (!obj) {
186 		dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
187 		return -ENODEV;
188 	}
189 
190 	if (obj->type != ACPI_TYPE_BUFFER) {
191 		dev_err(&adev->dev, "Not an ACPI buffer\n");
192 		ret = -ENODEV;
193 		goto out_free_buff;
194 	}
195 
196 	if (obj->buffer.length > size) {
197 		dev_err(&adev->dev, "Given buffer is too small\n");
198 		ret = -EINVAL;
199 		goto out_free_buff;
200 	}
201 
202 	memcpy(data, obj->buffer.pointer, obj->buffer.length);
203 
204 out_free_buff:
205 	kfree(buffer.pointer);
206 	return ret;
207 }
208 
209 static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,
210 				     struct ipu_sensor_ssdb *ssdb)
211 {
212 	switch (ssdb->degree) {
213 	case IPU_SENSOR_ROTATION_NORMAL:
214 		return 0;
215 	case IPU_SENSOR_ROTATION_INVERTED:
216 		return 180;
217 	default:
218 		dev_warn(&adev->dev,
219 			 "Unknown rotation %d. Assume 0 degree rotation\n",
220 			 ssdb->degree);
221 		return 0;
222 	}
223 }
224 
225 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
226 {
227 	enum v4l2_fwnode_orientation orientation;
228 	struct acpi_pld_info *pld;
229 	acpi_status status;
230 
231 	status = acpi_get_physical_device_location(adev->handle, &pld);
232 	if (ACPI_FAILURE(status)) {
233 		dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
234 		return V4L2_FWNODE_ORIENTATION_EXTERNAL;
235 	}
236 
237 	switch (pld->panel) {
238 	case ACPI_PLD_PANEL_FRONT:
239 		orientation = V4L2_FWNODE_ORIENTATION_FRONT;
240 		break;
241 	case ACPI_PLD_PANEL_BACK:
242 		orientation = V4L2_FWNODE_ORIENTATION_BACK;
243 		break;
244 	case ACPI_PLD_PANEL_TOP:
245 	case ACPI_PLD_PANEL_LEFT:
246 	case ACPI_PLD_PANEL_RIGHT:
247 	case ACPI_PLD_PANEL_UNKNOWN:
248 		orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
249 		break;
250 	default:
251 		dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
252 		orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
253 		break;
254 	}
255 
256 	ACPI_FREE(pld);
257 	return orientation;
258 }
259 
260 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
261 {
262 	struct ipu_sensor_ssdb ssdb = {};
263 	int ret;
264 
265 	ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
266 	if (ret)
267 		return ret;
268 
269 	if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
270 		dev_warn(&adev->dev, "Unknown VCM type %d\n", ssdb.vcmtype);
271 		ssdb.vcmtype = 0;
272 	}
273 
274 	if (ssdb.lanes > IPU_MAX_LANES) {
275 		dev_err(&adev->dev, "Number of lanes in SSDB is invalid\n");
276 		return -EINVAL;
277 	}
278 
279 	sensor->link = ssdb.link;
280 	sensor->lanes = ssdb.lanes;
281 	sensor->mclkspeed = ssdb.mclkspeed;
282 	sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb);
283 	sensor->orientation = ipu_bridge_parse_orientation(adev);
284 
285 	if (ssdb.vcmtype)
286 		sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
287 
288 	return 0;
289 }
290 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, INTEL_IPU_BRIDGE);
291 
292 static void ipu_bridge_create_fwnode_properties(
293 	struct ipu_sensor *sensor,
294 	struct ipu_bridge *bridge,
295 	const struct ipu_sensor_config *cfg)
296 {
297 	struct ipu_property_names *names = &sensor->prop_names;
298 	struct software_node *nodes = sensor->swnodes;
299 
300 	sensor->prop_names = prop_names;
301 
302 	if (sensor->csi_dev) {
303 		sensor->local_ref[0] =
304 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_SENSOR_ENDPOINT]);
305 		sensor->remote_ref[0] =
306 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_IPU_ENDPOINT]);
307 		sensor->ivsc_sensor_ref[0] =
308 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]);
309 		sensor->ivsc_ipu_ref[0] =
310 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]);
311 
312 		sensor->ivsc_sensor_ep_properties[0] =
313 			PROPERTY_ENTRY_U32(names->bus_type,
314 					   V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
315 		sensor->ivsc_sensor_ep_properties[1] =
316 			PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes,
317 						     bridge->data_lanes,
318 						     sensor->lanes);
319 		sensor->ivsc_sensor_ep_properties[2] =
320 			PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint,
321 						 sensor->ivsc_sensor_ref);
322 
323 		sensor->ivsc_ipu_ep_properties[0] =
324 			PROPERTY_ENTRY_U32(names->bus_type,
325 					   V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
326 		sensor->ivsc_ipu_ep_properties[1] =
327 			PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes,
328 						     bridge->data_lanes,
329 						     sensor->lanes);
330 		sensor->ivsc_ipu_ep_properties[2] =
331 			PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint,
332 						 sensor->ivsc_ipu_ref);
333 	} else {
334 		sensor->local_ref[0] =
335 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]);
336 		sensor->remote_ref[0] =
337 			SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]);
338 	}
339 
340 	sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
341 					sensor->prop_names.clock_frequency,
342 					sensor->mclkspeed);
343 	sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
344 					sensor->prop_names.rotation,
345 					sensor->rotation);
346 	sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
347 					sensor->prop_names.orientation,
348 					sensor->orientation);
349 	if (sensor->vcm_type) {
350 		sensor->vcm_ref[0] =
351 			SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
352 		sensor->dev_properties[3] =
353 			PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
354 	}
355 
356 	sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
357 					sensor->prop_names.bus_type,
358 					V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
359 	sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
360 					sensor->prop_names.data_lanes,
361 					bridge->data_lanes, sensor->lanes);
362 	sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
363 					sensor->prop_names.remote_endpoint,
364 					sensor->local_ref);
365 
366 	if (cfg->nr_link_freqs > 0)
367 		sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
368 			sensor->prop_names.link_frequencies,
369 			cfg->link_freqs,
370 			cfg->nr_link_freqs);
371 
372 	sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
373 					sensor->prop_names.data_lanes,
374 					bridge->data_lanes, sensor->lanes);
375 	sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
376 					sensor->prop_names.remote_endpoint,
377 					sensor->remote_ref);
378 }
379 
380 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
381 {
382 	snprintf(sensor->node_names.remote_port,
383 		 sizeof(sensor->node_names.remote_port),
384 		 SWNODE_GRAPH_PORT_NAME_FMT, sensor->link);
385 	snprintf(sensor->node_names.port,
386 		 sizeof(sensor->node_names.port),
387 		 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
388 	snprintf(sensor->node_names.endpoint,
389 		 sizeof(sensor->node_names.endpoint),
390 		 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
391 	if (sensor->vcm_type) {
392 		/* append link to distinguish nodes with same model VCM */
393 		snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
394 			 "%s-%u", sensor->vcm_type, sensor->link);
395 	}
396 
397 	if (sensor->csi_dev) {
398 		snprintf(sensor->node_names.ivsc_sensor_port,
399 			 sizeof(sensor->node_names.ivsc_sensor_port),
400 			 SWNODE_GRAPH_PORT_NAME_FMT, 0);
401 		snprintf(sensor->node_names.ivsc_ipu_port,
402 			 sizeof(sensor->node_names.ivsc_ipu_port),
403 			 SWNODE_GRAPH_PORT_NAME_FMT, 1);
404 	}
405 }
406 
407 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
408 {
409 	struct software_node *nodes = sensor->swnodes;
410 
411 	sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
412 	sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
413 	sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
414 	sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
415 	sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
416 	if (sensor->vcm_type)
417 		sensor->group[SWNODE_VCM] =  &nodes[SWNODE_VCM];
418 
419 	if (sensor->csi_dev) {
420 		sensor->group[SWNODE_IVSC_HID] =
421 					&nodes[SWNODE_IVSC_HID];
422 		sensor->group[SWNODE_IVSC_SENSOR_PORT] =
423 					&nodes[SWNODE_IVSC_SENSOR_PORT];
424 		sensor->group[SWNODE_IVSC_SENSOR_ENDPOINT] =
425 					&nodes[SWNODE_IVSC_SENSOR_ENDPOINT];
426 		sensor->group[SWNODE_IVSC_IPU_PORT] =
427 					&nodes[SWNODE_IVSC_IPU_PORT];
428 		sensor->group[SWNODE_IVSC_IPU_ENDPOINT] =
429 					&nodes[SWNODE_IVSC_IPU_ENDPOINT];
430 
431 		if (sensor->vcm_type)
432 			sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM];
433 	} else {
434 		if (sensor->vcm_type)
435 			sensor->group[SWNODE_IVSC_HID] = &nodes[SWNODE_VCM];
436 	}
437 }
438 
439 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
440 						 struct ipu_sensor *sensor)
441 {
442 	struct ipu_node_names *names = &sensor->node_names;
443 	struct software_node *nodes = sensor->swnodes;
444 
445 	ipu_bridge_init_swnode_names(sensor);
446 
447 	nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
448 					       sensor->dev_properties);
449 	nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
450 					      &nodes[SWNODE_SENSOR_HID]);
451 	nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
452 						sensor->node_names.endpoint,
453 						&nodes[SWNODE_SENSOR_PORT],
454 						sensor->ep_properties);
455 	nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
456 					   &bridge->ipu_hid_node);
457 	nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
458 						sensor->node_names.endpoint,
459 						&nodes[SWNODE_IPU_PORT],
460 						sensor->ipu_properties);
461 
462 	if (sensor->csi_dev) {
463 		snprintf(sensor->ivsc_name, sizeof(sensor->ivsc_name), "%s-%u",
464 			 acpi_device_hid(sensor->ivsc_adev), sensor->link);
465 
466 		nodes[SWNODE_IVSC_HID] = NODE_SENSOR(sensor->ivsc_name,
467 						     sensor->ivsc_properties);
468 		nodes[SWNODE_IVSC_SENSOR_PORT] =
469 				NODE_PORT(names->ivsc_sensor_port,
470 					  &nodes[SWNODE_IVSC_HID]);
471 		nodes[SWNODE_IVSC_SENSOR_ENDPOINT] =
472 				NODE_ENDPOINT(names->endpoint,
473 					      &nodes[SWNODE_IVSC_SENSOR_PORT],
474 					      sensor->ivsc_sensor_ep_properties);
475 		nodes[SWNODE_IVSC_IPU_PORT] =
476 				NODE_PORT(names->ivsc_ipu_port,
477 					  &nodes[SWNODE_IVSC_HID]);
478 		nodes[SWNODE_IVSC_IPU_ENDPOINT] =
479 				NODE_ENDPOINT(names->endpoint,
480 					      &nodes[SWNODE_IVSC_IPU_PORT],
481 					      sensor->ivsc_ipu_ep_properties);
482 	}
483 
484 	nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
485 
486 	ipu_bridge_init_swnode_group(sensor);
487 }
488 
489 /*
490  * The actual instantiation must be done from a workqueue to avoid
491  * a deadlock on taking list_lock from v4l2-async twice.
492  */
493 struct ipu_bridge_instantiate_vcm_work_data {
494 	struct work_struct work;
495 	struct device *sensor;
496 	char name[16];
497 	struct i2c_board_info board_info;
498 };
499 
500 static void ipu_bridge_instantiate_vcm_work(struct work_struct *work)
501 {
502 	struct ipu_bridge_instantiate_vcm_work_data *data =
503 		container_of(work, struct ipu_bridge_instantiate_vcm_work_data,
504 			     work);
505 	struct acpi_device *adev = ACPI_COMPANION(data->sensor);
506 	struct i2c_client *vcm_client;
507 	bool put_fwnode = true;
508 	int ret;
509 
510 	/*
511 	 * The client may get probed before the device_link gets added below
512 	 * make sure the sensor is powered-up during probe.
513 	 */
514 	ret = pm_runtime_get_sync(data->sensor);
515 	if (ret < 0) {
516 		dev_err(data->sensor, "Error %d runtime-resuming sensor, cannot instantiate VCM\n",
517 			ret);
518 		goto out_pm_put;
519 	}
520 
521 	/*
522 	 * Note the client is created only once and then kept around
523 	 * even after a rmmod, just like the software-nodes.
524 	 */
525 	vcm_client = i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(adev),
526 						   1, &data->board_info);
527 	if (IS_ERR(vcm_client)) {
528 		dev_err(data->sensor, "Error instantiating VCM client: %ld\n",
529 			PTR_ERR(vcm_client));
530 		goto out_pm_put;
531 	}
532 
533 	device_link_add(&vcm_client->dev, data->sensor, DL_FLAG_PM_RUNTIME);
534 
535 	dev_info(data->sensor, "Instantiated %s VCM\n", data->board_info.type);
536 	put_fwnode = false; /* Ownership has passed to the i2c-client */
537 
538 out_pm_put:
539 	pm_runtime_put(data->sensor);
540 	put_device(data->sensor);
541 	if (put_fwnode)
542 		fwnode_handle_put(data->board_info.fwnode);
543 	kfree(data);
544 }
545 
546 int ipu_bridge_instantiate_vcm(struct device *sensor)
547 {
548 	struct ipu_bridge_instantiate_vcm_work_data *data;
549 	struct fwnode_handle *vcm_fwnode;
550 	struct i2c_client *vcm_client;
551 	struct acpi_device *adev;
552 	char *sep;
553 
554 	adev = ACPI_COMPANION(sensor);
555 	if (!adev)
556 		return 0;
557 
558 	vcm_fwnode = fwnode_find_reference(dev_fwnode(sensor), "lens-focus", 0);
559 	if (IS_ERR(vcm_fwnode))
560 		return 0;
561 
562 	/* When reloading modules the client will already exist */
563 	vcm_client = i2c_find_device_by_fwnode(vcm_fwnode);
564 	if (vcm_client) {
565 		fwnode_handle_put(vcm_fwnode);
566 		put_device(&vcm_client->dev);
567 		return 0;
568 	}
569 
570 	data = kzalloc(sizeof(*data), GFP_KERNEL);
571 	if (!data) {
572 		fwnode_handle_put(vcm_fwnode);
573 		return -ENOMEM;
574 	}
575 
576 	INIT_WORK(&data->work, ipu_bridge_instantiate_vcm_work);
577 	data->sensor = get_device(sensor);
578 	snprintf(data->name, sizeof(data->name), "%s-VCM",
579 		 acpi_dev_name(adev));
580 	data->board_info.dev_name = data->name;
581 	data->board_info.fwnode = vcm_fwnode;
582 	snprintf(data->board_info.type, sizeof(data->board_info.type),
583 		 "%pfwP", vcm_fwnode);
584 	/* Strip "-<link>" postfix */
585 	sep = strchrnul(data->board_info.type, '-');
586 	*sep = 0;
587 
588 	queue_work(system_long_wq, &data->work);
589 
590 	return 0;
591 }
592 EXPORT_SYMBOL_NS_GPL(ipu_bridge_instantiate_vcm, INTEL_IPU_BRIDGE);
593 
594 static int ipu_bridge_instantiate_ivsc(struct ipu_sensor *sensor)
595 {
596 	struct fwnode_handle *fwnode;
597 
598 	if (!sensor->csi_dev)
599 		return 0;
600 
601 	fwnode = software_node_fwnode(&sensor->swnodes[SWNODE_IVSC_HID]);
602 	if (!fwnode)
603 		return -ENODEV;
604 
605 	set_secondary_fwnode(sensor->csi_dev, fwnode);
606 
607 	return 0;
608 }
609 
610 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
611 {
612 	struct ipu_sensor *sensor;
613 	unsigned int i;
614 
615 	for (i = 0; i < bridge->n_sensors; i++) {
616 		sensor = &bridge->sensors[i];
617 		software_node_unregister_node_group(sensor->group);
618 		acpi_dev_put(sensor->adev);
619 		put_device(sensor->csi_dev);
620 		acpi_dev_put(sensor->ivsc_adev);
621 	}
622 }
623 
624 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
625 				     struct ipu_bridge *bridge)
626 {
627 	struct fwnode_handle *fwnode, *primary;
628 	struct ipu_sensor *sensor;
629 	struct acpi_device *adev;
630 	int ret;
631 
632 	for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
633 		if (!adev->status.enabled)
634 			continue;
635 
636 		if (bridge->n_sensors >= IPU_MAX_PORTS) {
637 			acpi_dev_put(adev);
638 			dev_err(bridge->dev, "Exceeded available IPU ports\n");
639 			return -EINVAL;
640 		}
641 
642 		sensor = &bridge->sensors[bridge->n_sensors];
643 
644 		ret = bridge->parse_sensor_fwnode(adev, sensor);
645 		if (ret)
646 			goto err_put_adev;
647 
648 		snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
649 			 cfg->hid, sensor->link);
650 
651 		ret = ipu_bridge_check_ivsc_dev(sensor, adev);
652 		if (ret)
653 			goto err_put_adev;
654 
655 		ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
656 		ipu_bridge_create_connection_swnodes(bridge, sensor);
657 
658 		ret = software_node_register_node_group(sensor->group);
659 		if (ret)
660 			goto err_put_ivsc;
661 
662 		fwnode = software_node_fwnode(&sensor->swnodes[
663 						      SWNODE_SENSOR_HID]);
664 		if (!fwnode) {
665 			ret = -ENODEV;
666 			goto err_free_swnodes;
667 		}
668 
669 		sensor->adev = acpi_dev_get(adev);
670 
671 		primary = acpi_fwnode_handle(adev);
672 		primary->secondary = fwnode;
673 
674 		ret = ipu_bridge_instantiate_ivsc(sensor);
675 		if (ret)
676 			goto err_free_swnodes;
677 
678 		dev_info(bridge->dev, "Found supported sensor %s\n",
679 			 acpi_dev_name(adev));
680 
681 		bridge->n_sensors++;
682 	}
683 
684 	return 0;
685 
686 err_free_swnodes:
687 	software_node_unregister_node_group(sensor->group);
688 err_put_ivsc:
689 	put_device(sensor->csi_dev);
690 	acpi_dev_put(sensor->ivsc_adev);
691 err_put_adev:
692 	acpi_dev_put(adev);
693 	return ret;
694 }
695 
696 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
697 {
698 	unsigned int i;
699 	int ret;
700 
701 	for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
702 		const struct ipu_sensor_config *cfg =
703 			&ipu_supported_sensors[i];
704 
705 		ret = ipu_bridge_connect_sensor(cfg, bridge);
706 		if (ret)
707 			goto err_unregister_sensors;
708 	}
709 
710 	return 0;
711 
712 err_unregister_sensors:
713 	ipu_bridge_unregister_sensors(bridge);
714 	return ret;
715 }
716 
717 static int ipu_bridge_ivsc_is_ready(void)
718 {
719 	struct acpi_device *sensor_adev, *adev;
720 	struct device *csi_dev;
721 	bool ready = true;
722 	unsigned int i;
723 
724 	for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
725 		const struct ipu_sensor_config *cfg =
726 			&ipu_supported_sensors[i];
727 
728 		for_each_acpi_dev_match(sensor_adev, cfg->hid, NULL, -1) {
729 			if (!sensor_adev->status.enabled)
730 				continue;
731 
732 			adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev);
733 			if (!adev)
734 				continue;
735 
736 			csi_dev = ipu_bridge_get_ivsc_csi_dev(adev);
737 			if (!csi_dev)
738 				ready = false;
739 
740 			put_device(csi_dev);
741 			acpi_dev_put(adev);
742 		}
743 	}
744 
745 	return ready;
746 }
747 
748 int ipu_bridge_init(struct device *dev,
749 		    ipu_parse_sensor_fwnode_t parse_sensor_fwnode)
750 {
751 	struct fwnode_handle *fwnode;
752 	struct ipu_bridge *bridge;
753 	unsigned int i;
754 	int ret;
755 
756 	if (!ipu_bridge_ivsc_is_ready())
757 		return -EPROBE_DEFER;
758 
759 	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
760 	if (!bridge)
761 		return -ENOMEM;
762 
763 	strscpy(bridge->ipu_node_name, IPU_HID,
764 		sizeof(bridge->ipu_node_name));
765 	bridge->ipu_hid_node.name = bridge->ipu_node_name;
766 	bridge->dev = dev;
767 	bridge->parse_sensor_fwnode = parse_sensor_fwnode;
768 
769 	ret = software_node_register(&bridge->ipu_hid_node);
770 	if (ret < 0) {
771 		dev_err(dev, "Failed to register the IPU HID node\n");
772 		goto err_free_bridge;
773 	}
774 
775 	/*
776 	 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
777 	 * only need one, rather than one per sensor). We include it as a
778 	 * member of the struct ipu_bridge rather than a global variable so
779 	 * that it survives if the module is unloaded along with the rest of
780 	 * the struct.
781 	 */
782 	for (i = 0; i < IPU_MAX_LANES; i++)
783 		bridge->data_lanes[i] = i + 1;
784 
785 	ret = ipu_bridge_connect_sensors(bridge);
786 	if (ret || bridge->n_sensors == 0)
787 		goto err_unregister_ipu;
788 
789 	dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
790 
791 	fwnode = software_node_fwnode(&bridge->ipu_hid_node);
792 	if (!fwnode) {
793 		dev_err(dev, "Error getting fwnode from ipu software_node\n");
794 		ret = -ENODEV;
795 		goto err_unregister_sensors;
796 	}
797 
798 	set_secondary_fwnode(dev, fwnode);
799 
800 	return 0;
801 
802 err_unregister_sensors:
803 	ipu_bridge_unregister_sensors(bridge);
804 err_unregister_ipu:
805 	software_node_unregister(&bridge->ipu_hid_node);
806 err_free_bridge:
807 	kfree(bridge);
808 
809 	return ret;
810 }
811 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
812 
813 MODULE_LICENSE("GPL");
814 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");
815