xref: /linux/drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Code to build software firmware node graph for atomisp2 connected sensors
4  * from ACPI tables.
5  *
6  * Copyright (C) 2023 Hans de Goede <hdegoede@redhat.com>
7  *
8  * Based on drivers/media/pci/intel/ipu3/cio2-bridge.c written by:
9  * Dan Scally <djrscally@gmail.com>
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/dmi.h>
16 #include <linux/property.h>
17 
18 #include <media/ipu-bridge.h>
19 #include <media/v4l2-fwnode.h>
20 
21 #include "atomisp_cmd.h"
22 #include "atomisp_csi2.h"
23 #include "atomisp_internal.h"
24 
25 #define PMC_CLK_RATE_19_2MHZ			19200000
26 
27 /*
28  * 79234640-9e10-4fea-a5c1-b5aa8b19756f
29  * This _DSM GUID returns information about the GPIO lines mapped to a sensor.
30  * Function number 1 returns a count of the GPIO lines that are mapped.
31  * Subsequent functions return 32 bit ints encoding information about the GPIO.
32  */
33 static const guid_t intel_sensor_gpio_info_guid =
34 	GUID_INIT(0x79234640, 0x9e10, 0x4fea,
35 		  0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
36 
37 #define INTEL_GPIO_DSM_TYPE_SHIFT			0
38 #define INTEL_GPIO_DSM_TYPE_MASK			GENMASK(7, 0)
39 #define INTEL_GPIO_DSM_PIN_SHIFT			8
40 #define INTEL_GPIO_DSM_PIN_MASK				GENMASK(15, 8)
41 #define INTEL_GPIO_DSM_SENSOR_ON_VAL_SHIFT		24
42 #define INTEL_GPIO_DSM_SENSOR_ON_VAL_MASK		GENMASK(31, 24)
43 
44 #define INTEL_GPIO_DSM_TYPE(x) \
45 	(((x) & INTEL_GPIO_DSM_TYPE_MASK) >> INTEL_GPIO_DSM_TYPE_SHIFT)
46 #define INTEL_GPIO_DSM_PIN(x) \
47 	(((x) & INTEL_GPIO_DSM_PIN_MASK) >> INTEL_GPIO_DSM_PIN_SHIFT)
48 #define INTEL_GPIO_DSM_SENSOR_ON_VAL(x) \
49 	(((x) & INTEL_GPIO_DSM_SENSOR_ON_VAL_MASK) >> INTEL_GPIO_DSM_SENSOR_ON_VAL_SHIFT)
50 
51 /*
52  * 822ace8f-2814-4174-a56b-5f029fe079ee
53  * This _DSM GUID returns a string from the sensor device, which acts as a
54  * module identifier.
55  */
56 static const guid_t intel_sensor_module_guid =
57 	GUID_INIT(0x822ace8f, 0x2814, 0x4174,
58 		  0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
59 
60 /*
61  * dc2f6c4f-045b-4f1d-97b9-882a6860a4be
62  * This _DSM GUID returns a package with n*2 strings, with each set of 2 strings
63  * forming a key, value pair for settings like e.g. "CsiLanes" = "1".
64  */
65 static const guid_t atomisp_dsm_guid =
66 	GUID_INIT(0xdc2f6c4f, 0x045b, 0x4f1d,
67 		  0x97, 0xb9, 0x88, 0x2a, 0x68, 0x60, 0xa4, 0xbe);
68 
69 /*
70  * 75c9a639-5c8a-4a00-9f48-a9c3b5da789f
71  * This _DSM GUID returns a string giving the VCM type e.g. "AD5823".
72  */
73 static const guid_t vcm_dsm_guid =
74 	GUID_INIT(0x75c9a639, 0x5c8a, 0x4a00,
75 		  0x9f, 0x48, 0xa9, 0xc3, 0xb5, 0xda, 0x78, 0x9f);
76 
77 struct atomisp_sensor_config {
78 	int lanes;
79 	bool vcm;
80 };
81 
82 #define ATOMISP_SENSOR_CONFIG(_HID, _LANES, _VCM)			\
83 {									\
84 	.id = _HID,							\
85 	.driver_data = (long)&((const struct atomisp_sensor_config) {	\
86 		.lanes = _LANES,					\
87 		.vcm = _VCM,						\
88 	})								\
89 }
90 
91 /*
92  * gmin_cfg parsing code. This is a cleaned up version of the gmin_cfg parsing
93  * code from atomisp_gmin_platform.c.
94  * Once all sensors are moved to v4l2-async probing atomisp_gmin_platform.c can
95  * be removed and the duplication of this code goes away.
96  */
97 struct gmin_cfg_var {
98 	const char *acpi_dev_name;
99 	const char *key;
100 	const char *val;
101 };
102 
103 static struct gmin_cfg_var lenovo_ideapad_miix_310_vars[] = {
104 	/* _DSM contains the wrong CsiPort! */
105 	{ "OVTI2680:01", "CsiPort", "0" },
106 	{}
107 };
108 
109 static struct gmin_cfg_var xiaomi_mipad2_vars[] = {
110 	/* _DSM contains the wrong CsiPort for the front facing OV5693 sensor */
111 	{ "INT33BE:00", "CsiPort", "0" },
112 	/* _DSM contains the wrong CsiLanes for the back facing T4KA3 sensor */
113 	{ "XMCC0003:00", "CsiLanes", "4" },
114 	{}
115 };
116 
117 static const struct dmi_system_id gmin_cfg_dmi_overrides[] = {
118 	{
119 		/* Lenovo Ideapad Miix 310 */
120 		.matches = {
121 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
122 			DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10"),
123 		},
124 		.driver_data = lenovo_ideapad_miix_310_vars,
125 	},
126 	{
127 		/* Xiaomi Mipad2 */
128 		.matches = {
129 			DMI_MATCH(DMI_SYS_VENDOR, "Xiaomi Inc"),
130 			DMI_MATCH(DMI_PRODUCT_NAME, "Mipad2"),
131 		},
132 		.driver_data = xiaomi_mipad2_vars,
133 	},
134 	{}
135 };
136 
gmin_cfg_get_dsm(struct acpi_device * adev,const char * key)137 static char *gmin_cfg_get_dsm(struct acpi_device *adev, const char *key)
138 {
139 	union acpi_object *obj, *key_el, *val_el;
140 	char *val = NULL;
141 	int i;
142 
143 	obj = acpi_evaluate_dsm_typed(adev->handle, &atomisp_dsm_guid, 0, 0,
144 				      NULL, ACPI_TYPE_PACKAGE);
145 	if (!obj)
146 		return NULL;
147 
148 	for (i = 0; i < obj->package.count - 1; i += 2) {
149 		key_el = &obj->package.elements[i + 0];
150 		val_el = &obj->package.elements[i + 1];
151 
152 		if (key_el->type != ACPI_TYPE_STRING || val_el->type != ACPI_TYPE_STRING)
153 			break;
154 
155 		if (!strcmp(key_el->string.pointer, key)) {
156 			val = kstrdup(val_el->string.pointer, GFP_KERNEL);
157 			if (!val)
158 				break;
159 
160 			acpi_handle_info(adev->handle, "%s: Using DSM entry %s=%s\n",
161 					 dev_name(&adev->dev), key, val);
162 			break;
163 		}
164 	}
165 
166 	ACPI_FREE(obj);
167 	return val;
168 }
169 
gmin_cfg_get_dmi_override(struct acpi_device * adev,const char * key)170 static char *gmin_cfg_get_dmi_override(struct acpi_device *adev, const char *key)
171 {
172 	const struct dmi_system_id *id;
173 	struct gmin_cfg_var *gv;
174 
175 	id = dmi_first_match(gmin_cfg_dmi_overrides);
176 	if (!id)
177 		return NULL;
178 
179 	for (gv = id->driver_data; gv->acpi_dev_name; gv++) {
180 		if (strcmp(gv->acpi_dev_name, acpi_dev_name(adev)))
181 			continue;
182 
183 		if (strcmp(key, gv->key))
184 			continue;
185 
186 		acpi_handle_info(adev->handle, "%s: Using DMI entry %s=%s\n",
187 				 dev_name(&adev->dev), key, gv->val);
188 		return kstrdup(gv->val, GFP_KERNEL);
189 	}
190 
191 	return NULL;
192 }
193 
gmin_cfg_get(struct acpi_device * adev,const char * key)194 static char *gmin_cfg_get(struct acpi_device *adev, const char *key)
195 {
196 	char *val;
197 
198 	val = gmin_cfg_get_dmi_override(adev, key);
199 	if (val)
200 		return val;
201 
202 	return gmin_cfg_get_dsm(adev, key);
203 }
204 
gmin_cfg_get_int(struct acpi_device * adev,const char * key,int default_val)205 static int gmin_cfg_get_int(struct acpi_device *adev, const char *key, int default_val)
206 {
207 	char *str_val;
208 	long int_val;
209 	int ret;
210 
211 	str_val = gmin_cfg_get(adev, key);
212 	if (!str_val)
213 		goto out_use_default;
214 
215 	ret = kstrtoul(str_val, 0, &int_val);
216 	kfree(str_val);
217 	if (ret)
218 		goto out_use_default;
219 
220 	return int_val;
221 
222 out_use_default:
223 	acpi_handle_info(adev->handle, "%s: Using default %s=%d\n",
224 			 dev_name(&adev->dev), key, default_val);
225 	return default_val;
226 }
227 
atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(struct acpi_device * adev)228 static int atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(struct acpi_device *adev)
229 {
230 	/* ACPI_PATH_SEGMENT_LENGTH is guaranteed to be big enough for name + 0 term. */
231 	char name[ACPI_PATH_SEGMENT_LENGTH];
232 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
233 	struct acpi_buffer b_name = { sizeof(name), name };
234 	union acpi_object *package, *element;
235 	int i, ret = -ENOENT;
236 	acpi_handle rhandle;
237 	acpi_status status;
238 	u8 clock_num;
239 
240 	status = acpi_evaluate_object_typed(adev->handle, "_PR0", NULL, &buffer, ACPI_TYPE_PACKAGE);
241 	if (ACPI_FAILURE(status))
242 		return -ENOENT;
243 
244 	package = buffer.pointer;
245 	for (i = 0; i < package->package.count; i++) {
246 		element = &package->package.elements[i];
247 
248 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
249 			continue;
250 
251 		rhandle = element->reference.handle;
252 		if (!rhandle)
253 			continue;
254 
255 		acpi_get_name(rhandle, ACPI_SINGLE_NAME, &b_name);
256 
257 		if (str_has_prefix(name, "CLK") && !kstrtou8(&name[3], 10, &clock_num) &&
258 		    clock_num <= 4) {
259 			ret = clock_num;
260 			break;
261 		}
262 	}
263 
264 	ACPI_FREE(buffer.pointer);
265 
266 	if (ret < 0)
267 		acpi_handle_warn(adev->handle, "%s: Could not find PMC clk in _PR0\n",
268 				 dev_name(&adev->dev));
269 
270 	return ret;
271 }
272 
atomisp_csi2_set_pmc_clk_freq(struct acpi_device * adev,int clock_num)273 static int atomisp_csi2_set_pmc_clk_freq(struct acpi_device *adev, int clock_num)
274 {
275 	struct clk *clk;
276 	char name[14];
277 	int ret;
278 
279 	if (clock_num < 0)
280 		return 0;
281 
282 	snprintf(name, sizeof(name), "pmc_plt_clk_%d", clock_num);
283 
284 	clk = clk_get(NULL, name);
285 	if (IS_ERR(clk)) {
286 		ret = PTR_ERR(clk);
287 		acpi_handle_err(adev->handle, "%s: Error getting clk %s: %d\n",
288 				dev_name(&adev->dev), name, ret);
289 		return ret;
290 	}
291 
292 	/*
293 	 * The firmware might enable the clock at boot, to change
294 	 * the rate we must ensure the clock is disabled.
295 	 */
296 	ret = clk_prepare_enable(clk);
297 	if (!ret)
298 		clk_disable_unprepare(clk);
299 	if (!ret)
300 		ret = clk_set_rate(clk, PMC_CLK_RATE_19_2MHZ);
301 	if (ret)
302 		acpi_handle_err(adev->handle, "%s: Error setting clk-rate for %s: %d\n",
303 				dev_name(&adev->dev), name, ret);
304 
305 	clk_put(clk);
306 	return ret;
307 }
308 
atomisp_csi2_get_port(struct acpi_device * adev,int clock_num)309 static int atomisp_csi2_get_port(struct acpi_device *adev, int clock_num)
310 {
311 	int port;
312 
313 	/*
314 	 * Compare clock-number to the PMC-clock used for CsiPort 1
315 	 * in the CHT/BYT reference designs.
316 	 */
317 	if (IS_ISP2401)
318 		port = clock_num == 4 ? 1 : 0;
319 	else
320 		port = clock_num == 0 ? 1 : 0;
321 
322 	/* Intel DSM or DMI quirk overrides _PR0 CLK derived default */
323 	return gmin_cfg_get_int(adev, "CsiPort", port);
324 }
325 
326 /* Note this always returns 1 to continue looping so that res_count is accurate */
atomisp_csi2_handle_acpi_gpio_res(struct acpi_resource * ares,void * _data)327 static int atomisp_csi2_handle_acpi_gpio_res(struct acpi_resource *ares, void *_data)
328 {
329 	struct atomisp_csi2_acpi_gpio_parsing_data *data = _data;
330 	struct acpi_resource_gpio *agpio;
331 	const char *name;
332 	bool active_low;
333 	unsigned int i;
334 	u32 settings = 0;
335 	u16 pin;
336 
337 	if (!acpi_gpio_get_io_resource(ares, &agpio))
338 		return 1; /* Not a GPIO, continue the loop */
339 
340 	data->res_count++;
341 
342 	pin = agpio->pin_table[0];
343 	for (i = 0; i < data->settings_count; i++) {
344 		if (INTEL_GPIO_DSM_PIN(data->settings[i]) == pin) {
345 			settings = data->settings[i];
346 			break;
347 		}
348 	}
349 
350 	if (i == data->settings_count) {
351 		acpi_handle_warn(data->adev->handle,
352 				 "%s: Could not find DSM GPIO settings for pin %u\n",
353 				 dev_name(&data->adev->dev), pin);
354 		return 1;
355 	}
356 
357 	switch (INTEL_GPIO_DSM_TYPE(settings)) {
358 	case 0:
359 		name = "reset-gpios";
360 		break;
361 	case 1:
362 		name = "powerdown-gpios";
363 		break;
364 	default:
365 		acpi_handle_warn(data->adev->handle, "%s: Unknown GPIO type 0x%02lx for pin %u\n",
366 				 dev_name(&data->adev->dev),
367 				 INTEL_GPIO_DSM_TYPE(settings), pin);
368 		return 1;
369 	}
370 
371 	/*
372 	 * Both reset and power-down need to be logical false when the sensor
373 	 * is on (sensor should not be in reset and not be powered-down). So
374 	 * when the sensor-on-value (which is the physical pin value) is high,
375 	 * then the signal is active-low.
376 	 */
377 	active_low = INTEL_GPIO_DSM_SENSOR_ON_VAL(settings);
378 
379 	i = data->map_count;
380 	if (i == CSI2_MAX_ACPI_GPIOS)
381 		return 1;
382 
383 	/* res_count is already incremented */
384 	data->map->params[i].crs_entry_index = data->res_count - 1;
385 	data->map->params[i].active_low = active_low;
386 	data->map->mapping[i].name = name;
387 	data->map->mapping[i].data = &data->map->params[i];
388 	data->map->mapping[i].size = 1;
389 	data->map_count++;
390 
391 	acpi_handle_info(data->adev->handle, "%s: %s crs %d %s pin %u active-%s\n",
392 			 dev_name(&data->adev->dev), name,
393 			 data->res_count - 1, agpio->resource_source.string_ptr,
394 			 pin, active_low ? "low" : "high");
395 
396 	return 1;
397 }
398 
399 /*
400  * Helper function to create an ACPI GPIO lookup table for sensor reset and
401  * powerdown signals on Intel Bay Trail (BYT) and Cherry Trail (CHT) devices,
402  * including setting the correct polarity for the GPIO.
403  *
404  * This uses the "79234640-9e10-4fea-a5c1-b5aa8b19756f" DSM method directly
405  * on the sensor device's ACPI node. This is different from later Intel
406  * hardware which has a separate INT3472 acpi_device with this info.
407  *
408  * This function must be called before creating the sw-noded describing
409  * the fwnode graph endpoint. And sensor drivers used on these devices
410  * must return -EPROBE_DEFER when there is no endpoint description yet.
411  * Together this guarantees that the GPIO lookups are in place before
412  * the sensor driver tries to get GPIOs with gpiod_get().
413  *
414  * Note this code uses the same DSM GUID as the int3472_gpio_guid in
415  * the INT3472 discrete.c code and there is some overlap, but there are
416  * enough differences that it is difficult to share the code.
417  */
atomisp_csi2_add_gpio_mappings(struct acpi_device * adev)418 static int atomisp_csi2_add_gpio_mappings(struct acpi_device *adev)
419 {
420 	struct atomisp_csi2_acpi_gpio_parsing_data data = { };
421 	LIST_HEAD(resource_list);
422 	union acpi_object *obj;
423 	unsigned int i, j;
424 	int ret;
425 
426 	obj = acpi_evaluate_dsm_typed(adev->handle, &intel_sensor_module_guid,
427 				      0x00, 1, NULL, ACPI_TYPE_STRING);
428 	if (obj) {
429 		acpi_handle_info(adev->handle, "%s: Sensor module id: '%s'\n",
430 				 dev_name(&adev->dev), obj->string.pointer);
431 		ACPI_FREE(obj);
432 	}
433 
434 	/*
435 	 * First get the GPIO-settings count and then get count GPIO-settings
436 	 * values. Note the order of these may differ from the order in which
437 	 * the GPIOs are listed on the ACPI resources! So we first store them all
438 	 * and then enumerate the ACPI resources and match them up by pin number.
439 	 */
440 	obj = acpi_evaluate_dsm_typed(adev->handle,
441 				      &intel_sensor_gpio_info_guid, 0x00, 1,
442 				      NULL, ACPI_TYPE_INTEGER);
443 	if (!obj) {
444 		acpi_handle_err(adev->handle, "%s: No _DSM entry for GPIO pin count\n",
445 				dev_name(&adev->dev));
446 		return -EIO;
447 	}
448 
449 	data.settings_count = obj->integer.value;
450 	ACPI_FREE(obj);
451 
452 	if (data.settings_count > CSI2_MAX_ACPI_GPIOS) {
453 		acpi_handle_err(adev->handle, "%s: Too many GPIOs %u > %u\n",
454 				dev_name(&adev->dev), data.settings_count,
455 				CSI2_MAX_ACPI_GPIOS);
456 		return -EOVERFLOW;
457 	}
458 
459 	for (i = 0; i < data.settings_count; i++) {
460 		/*
461 		 * i + 2 because the index of this _DSM function is 1-based
462 		 * and the first function is just a count.
463 		 */
464 		obj = acpi_evaluate_dsm_typed(adev->handle,
465 					      &intel_sensor_gpio_info_guid,
466 					      0x00, i + 2,
467 					      NULL, ACPI_TYPE_INTEGER);
468 		if (!obj) {
469 			acpi_handle_err(adev->handle, "%s: No _DSM entry for pin %u\n",
470 					dev_name(&adev->dev), i);
471 			return -EIO;
472 		}
473 
474 		data.settings[i] = obj->integer.value;
475 		ACPI_FREE(obj);
476 	}
477 
478 	/* Since we match up by pin-number the pin-numbers must be unique */
479 	for (i = 0; i < data.settings_count; i++) {
480 		for (j = i + 1; j < data.settings_count; j++) {
481 			if (INTEL_GPIO_DSM_PIN(data.settings[i]) !=
482 			    INTEL_GPIO_DSM_PIN(data.settings[j]))
483 				continue;
484 
485 			acpi_handle_err(adev->handle, "%s: Duplicate pin number %lu\n",
486 					dev_name(&adev->dev),
487 					INTEL_GPIO_DSM_PIN(data.settings[i]));
488 			return -EIO;
489 		}
490 	}
491 
492 	data.map = kzalloc(sizeof(*data.map), GFP_KERNEL);
493 	if (!data.map)
494 		return -ENOMEM;
495 
496 	/* Now parse the ACPI resources and build the lookup table */
497 	data.adev = adev;
498 	ret = acpi_dev_get_resources(adev, &resource_list,
499 				     atomisp_csi2_handle_acpi_gpio_res, &data);
500 	if (ret < 0)
501 		return ret;
502 
503 	acpi_dev_free_resource_list(&resource_list);
504 
505 	if (data.map_count != data.settings_count ||
506 	    data.res_count != data.settings_count)
507 		acpi_handle_warn(adev->handle, "%s: ACPI GPIO resources vs DSM GPIO-info count mismatch (dsm: %d res: %d map %d\n",
508 				 dev_name(&adev->dev), data.settings_count,
509 				 data.res_count, data.map_count);
510 
511 	ret = acpi_dev_add_driver_gpios(adev, data.map->mapping);
512 	if (ret)
513 		acpi_handle_err(adev->handle, "%s: Error adding driver GPIOs: %d\n",
514 				dev_name(&adev->dev), ret);
515 
516 	return ret;
517 }
518 
atomisp_csi2_get_vcm_type(struct acpi_device * adev)519 static char *atomisp_csi2_get_vcm_type(struct acpi_device *adev)
520 {
521 	union acpi_object *obj;
522 	char *vcm_type;
523 
524 	obj = acpi_evaluate_dsm_typed(adev->handle, &vcm_dsm_guid, 0, 0,
525 				      NULL, ACPI_TYPE_STRING);
526 	if (!obj)
527 		return NULL;
528 
529 	vcm_type = kstrdup(obj->string.pointer, GFP_KERNEL);
530 	ACPI_FREE(obj);
531 
532 	if (!vcm_type)
533 		return NULL;
534 
535 	string_lower(vcm_type, vcm_type);
536 	return vcm_type;
537 }
538 
539 static const struct acpi_device_id atomisp_sensor_configs[] = {
540 	/*
541 	 * FIXME ov5693 modules have a VCM, but for unknown reasons
542 	 * the sensor fails to start streaming when instantiating
543 	 * an i2c-client for the VCM, so it is disabled for now.
544 	 */
545 	ATOMISP_SENSOR_CONFIG("INT33BE", 2, false),	/* OV5693 */
546 	{}
547 };
548 
atomisp_csi2_parse_sensor_fwnode(struct acpi_device * adev,struct ipu_sensor * sensor)549 static int atomisp_csi2_parse_sensor_fwnode(struct acpi_device *adev,
550 					    struct ipu_sensor *sensor)
551 {
552 	const struct acpi_device_id *id;
553 	int ret, clock_num;
554 	bool vcm = false;
555 	int lanes = 1;
556 
557 	id = acpi_match_acpi_device(atomisp_sensor_configs, adev);
558 	if (id) {
559 		struct atomisp_sensor_config *cfg =
560 			(struct atomisp_sensor_config *)id->driver_data;
561 
562 		lanes = cfg->lanes;
563 		vcm = cfg->vcm;
564 	}
565 
566 	/*
567 	 * ACPI takes care of turning the PMC clock on and off, but on BYT
568 	 * the clock defaults to 25 MHz instead of the expected 19.2 MHz.
569 	 * Get the PMC-clock number from ACPI PR0 method and set it to 19.2 MHz.
570 	 * The PMC-clock number is also used to determine the default CSI port.
571 	 */
572 	clock_num = atomisp_csi2_get_pmc_clk_nr_from_acpi_pr0(adev);
573 
574 	ret = atomisp_csi2_set_pmc_clk_freq(adev, clock_num);
575 	if (ret)
576 		return ret;
577 
578 	sensor->link = atomisp_csi2_get_port(adev, clock_num);
579 	if (sensor->link >= ATOMISP_CAMERA_NR_PORTS) {
580 		acpi_handle_err(adev->handle, "%s: Invalid port: %u\n",
581 				dev_name(&adev->dev), sensor->link);
582 		return -EINVAL;
583 	}
584 
585 	sensor->lanes = gmin_cfg_get_int(adev, "CsiLanes", lanes);
586 	if (sensor->lanes > IPU_MAX_LANES) {
587 		acpi_handle_err(adev->handle, "%s: Invalid lane-count: %d\n",
588 				dev_name(&adev->dev), sensor->lanes);
589 		return -EINVAL;
590 	}
591 
592 	ret = atomisp_csi2_add_gpio_mappings(adev);
593 	if (ret)
594 		return ret;
595 
596 	sensor->mclkspeed = PMC_CLK_RATE_19_2MHZ;
597 	sensor->rotation = 0;
598 	sensor->orientation = (sensor->link == 1) ?
599 		V4L2_FWNODE_ORIENTATION_BACK : V4L2_FWNODE_ORIENTATION_FRONT;
600 
601 	if (vcm)
602 		sensor->vcm_type = atomisp_csi2_get_vcm_type(adev);
603 
604 	return 0;
605 }
606 
atomisp_csi2_bridge_init(struct atomisp_device * isp)607 int atomisp_csi2_bridge_init(struct atomisp_device *isp)
608 {
609 	struct device *dev = isp->dev;
610 	struct fwnode_handle *fwnode;
611 
612 	/*
613 	 * This function is intended to run only once and then leave
614 	 * the created nodes attached even after a rmmod, therefore:
615 	 * 1. The bridge memory is leaked deliberately on success
616 	 * 2. If a secondary fwnode is already set exit early.
617 	 */
618 	fwnode = dev_fwnode(dev);
619 	if (fwnode && fwnode->secondary)
620 		return 0;
621 
622 	return ipu_bridge_init(dev, atomisp_csi2_parse_sensor_fwnode);
623 }
624 
625 /******* V4L2 sub-device asynchronous registration callbacks***********/
626 
627 struct sensor_async_subdev {
628 	struct v4l2_async_connection asd;
629 	int port;
630 };
631 
632 #define to_sensor_asd(a)	container_of(a, struct sensor_async_subdev, asd)
633 #define notifier_to_atomisp(n)	container_of(n, struct atomisp_device, notifier)
634 
635 /* .bound() notifier callback when a match is found */
atomisp_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_connection * asd)636 static int atomisp_notifier_bound(struct v4l2_async_notifier *notifier,
637 				  struct v4l2_subdev *sd,
638 				  struct v4l2_async_connection *asd)
639 {
640 	struct atomisp_device *isp = notifier_to_atomisp(notifier);
641 	struct sensor_async_subdev *s_asd = to_sensor_asd(asd);
642 	int ret;
643 
644 	if (s_asd->port >= ATOMISP_CAMERA_NR_PORTS) {
645 		dev_err(isp->dev, "port %d not supported\n", s_asd->port);
646 		return -EINVAL;
647 	}
648 
649 	if (isp->sensor_subdevs[s_asd->port]) {
650 		dev_err(isp->dev, "port %d already has a sensor attached\n", s_asd->port);
651 		return -EBUSY;
652 	}
653 
654 	ret = ipu_bridge_instantiate_vcm(sd->dev);
655 	if (ret)
656 		return ret;
657 
658 	isp->sensor_subdevs[s_asd->port] = sd;
659 	return 0;
660 }
661 
662 /* The .unbind callback */
atomisp_notifier_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_connection * asd)663 static void atomisp_notifier_unbind(struct v4l2_async_notifier *notifier,
664 				    struct v4l2_subdev *sd,
665 				    struct v4l2_async_connection *asd)
666 {
667 	struct atomisp_device *isp = notifier_to_atomisp(notifier);
668 	struct sensor_async_subdev *s_asd = to_sensor_asd(asd);
669 
670 	isp->sensor_subdevs[s_asd->port] = NULL;
671 }
672 
673 /* .complete() is called after all subdevices have been located */
atomisp_notifier_complete(struct v4l2_async_notifier * notifier)674 static int atomisp_notifier_complete(struct v4l2_async_notifier *notifier)
675 {
676 	struct atomisp_device *isp = notifier_to_atomisp(notifier);
677 
678 	return atomisp_register_device_nodes(isp);
679 }
680 
681 static const struct v4l2_async_notifier_operations atomisp_async_ops = {
682 	.bound = atomisp_notifier_bound,
683 	.unbind = atomisp_notifier_unbind,
684 	.complete = atomisp_notifier_complete,
685 };
686 
atomisp_csi2_bridge_parse_firmware(struct atomisp_device * isp)687 int atomisp_csi2_bridge_parse_firmware(struct atomisp_device *isp)
688 {
689 	int i, mipi_port, ret;
690 
691 	v4l2_async_nf_init(&isp->notifier, &isp->v4l2_dev);
692 	isp->notifier.ops = &atomisp_async_ops;
693 
694 	for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
695 		struct v4l2_fwnode_endpoint vep = {
696 			.bus_type = V4L2_MBUS_CSI2_DPHY,
697 		};
698 		struct sensor_async_subdev *s_asd;
699 		struct fwnode_handle *ep;
700 
701 		ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(isp->dev), i, 0,
702 						     FWNODE_GRAPH_ENDPOINT_NEXT);
703 		if (!ep)
704 			continue;
705 
706 		ret = v4l2_fwnode_endpoint_parse(ep, &vep);
707 		if (ret)
708 			goto err_parse;
709 
710 		if (vep.base.port >= ATOMISP_CAMERA_NR_PORTS) {
711 			dev_err(isp->dev, "port %d not supported\n", vep.base.port);
712 			ret = -EINVAL;
713 			goto err_parse;
714 		}
715 
716 		mipi_port = atomisp_port_to_mipi_port(isp, vep.base.port);
717 		isp->sensor_lanes[mipi_port] = vep.bus.mipi_csi2.num_data_lanes;
718 
719 		s_asd = v4l2_async_nf_add_fwnode_remote(&isp->notifier, ep,
720 							struct sensor_async_subdev);
721 		if (IS_ERR(s_asd)) {
722 			ret = PTR_ERR(s_asd);
723 			goto err_parse;
724 		}
725 
726 		s_asd->port = vep.base.port;
727 
728 		fwnode_handle_put(ep);
729 		continue;
730 
731 err_parse:
732 		fwnode_handle_put(ep);
733 		return ret;
734 	}
735 
736 	return 0;
737 }
738