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