1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
4 #include <linux/acpi.h>
5 #include <linux/device.h>
7 #include <linux/property.h>
8 #include <media/v4l2-fwnode.h>
10 #include "ipu-bridge.h"
13 * Extend this array with ACPI Hardware IDs of devices known to be working
14 * plus the number of link-frequencies expected by their drivers, along with
15 * the frequency values in hertz. This is somewhat opportunistic way of adding
16 * support for this for now in the hopes of a better source for the information
17 * (possibly some encoded value in the SSDB buffer that we're unaware of)
18 * becoming apparent in the future.
20 * Do not add an entry for a sensor that is not actually supported.
22 static const struct ipu_sensor_config ipu_supported_sensors[] = {
23 /* Omnivision OV5693 */
24 IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
25 /* Omnivision OV8865 */
26 IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
27 /* Omnivision OV7251 */
28 IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
29 /* Omnivision OV2680 */
30 IPU_SENSOR_CONFIG("OVTI2680", 0),
31 /* Omnivision ov8856 */
32 IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
33 /* Omnivision ov2740 */
34 IPU_SENSOR_CONFIG("INT3474", 1, 360000000),
36 IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
37 /* Omnivision ov13b10 */
38 IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
41 static const struct ipu_property_names prop_names = {
42 .clock_frequency = "clock-frequency",
43 .rotation = "rotation",
44 .orientation = "orientation",
45 .bus_type = "bus-type",
46 .data_lanes = "data-lanes",
47 .remote_endpoint = "remote-endpoint",
48 .link_frequencies = "link-frequencies",
51 static const char * const ipu_vcm_types[] = {
63 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
66 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
67 union acpi_object *obj;
71 status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
72 if (ACPI_FAILURE(status))
77 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
81 if (obj->type != ACPI_TYPE_BUFFER) {
82 dev_err(&adev->dev, "Not an ACPI buffer\n");
87 if (obj->buffer.length > size) {
88 dev_err(&adev->dev, "Given buffer is too small\n");
93 memcpy(data, obj->buffer.pointer, obj->buffer.length);
96 kfree(buffer.pointer);
100 static u32 ipu_bridge_parse_rotation(struct ipu_sensor *sensor)
102 switch (sensor->ssdb.degree) {
103 case IPU_SENSOR_ROTATION_NORMAL:
105 case IPU_SENSOR_ROTATION_INVERTED:
108 dev_warn(&sensor->adev->dev,
109 "Unknown rotation %d. Assume 0 degree rotation\n",
110 sensor->ssdb.degree);
115 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
117 enum v4l2_fwnode_orientation orientation;
118 struct acpi_pld_info *pld;
121 status = acpi_get_physical_device_location(adev->handle, &pld);
122 if (ACPI_FAILURE(status)) {
123 dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
124 return V4L2_FWNODE_ORIENTATION_EXTERNAL;
127 switch (pld->panel) {
128 case ACPI_PLD_PANEL_FRONT:
129 orientation = V4L2_FWNODE_ORIENTATION_FRONT;
131 case ACPI_PLD_PANEL_BACK:
132 orientation = V4L2_FWNODE_ORIENTATION_BACK;
134 case ACPI_PLD_PANEL_TOP:
135 case ACPI_PLD_PANEL_LEFT:
136 case ACPI_PLD_PANEL_RIGHT:
137 case ACPI_PLD_PANEL_UNKNOWN:
138 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
141 dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
142 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
150 static void ipu_bridge_create_fwnode_properties(
151 struct ipu_sensor *sensor,
152 struct ipu_bridge *bridge,
153 const struct ipu_sensor_config *cfg)
156 enum v4l2_fwnode_orientation orientation;
158 rotation = ipu_bridge_parse_rotation(sensor);
159 orientation = ipu_bridge_parse_orientation(sensor->adev);
161 sensor->prop_names = prop_names;
163 sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]);
164 sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
166 sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
167 sensor->prop_names.clock_frequency,
168 sensor->ssdb.mclkspeed);
169 sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
170 sensor->prop_names.rotation,
172 sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
173 sensor->prop_names.orientation,
175 if (sensor->ssdb.vcmtype) {
177 SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
178 sensor->dev_properties[3] =
179 PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
182 sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
183 sensor->prop_names.bus_type,
184 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
185 sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
186 sensor->prop_names.data_lanes,
189 sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
190 sensor->prop_names.remote_endpoint,
193 if (cfg->nr_link_freqs > 0)
194 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
195 sensor->prop_names.link_frequencies,
199 sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
200 sensor->prop_names.data_lanes,
203 sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
204 sensor->prop_names.remote_endpoint,
208 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
210 snprintf(sensor->node_names.remote_port,
211 sizeof(sensor->node_names.remote_port),
212 SWNODE_GRAPH_PORT_NAME_FMT, sensor->ssdb.link);
213 snprintf(sensor->node_names.port,
214 sizeof(sensor->node_names.port),
215 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
216 snprintf(sensor->node_names.endpoint,
217 sizeof(sensor->node_names.endpoint),
218 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
219 if (sensor->ssdb.vcmtype) {
220 /* append ssdb.link to distinguish nodes with same model VCM */
221 snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
222 "%s-%u", ipu_vcm_types[sensor->ssdb.vcmtype - 1],
227 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
229 struct software_node *nodes = sensor->swnodes;
231 sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
232 sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
233 sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
234 sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
235 sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
236 if (sensor->ssdb.vcmtype)
237 sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM];
240 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
241 struct ipu_sensor *sensor)
243 struct software_node *nodes = sensor->swnodes;
245 ipu_bridge_init_swnode_names(sensor);
247 nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
248 sensor->dev_properties);
249 nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
250 &nodes[SWNODE_SENSOR_HID]);
251 nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
252 sensor->node_names.endpoint,
253 &nodes[SWNODE_SENSOR_PORT],
254 sensor->ep_properties);
255 nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
256 &bridge->ipu_hid_node);
257 nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
258 sensor->node_names.endpoint,
259 &nodes[SWNODE_IPU_PORT],
260 sensor->ipu_properties);
261 nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
263 ipu_bridge_init_swnode_group(sensor);
266 static void ipu_bridge_instantiate_vcm_i2c_client(struct ipu_sensor *sensor)
268 struct i2c_board_info board_info = { };
271 if (!sensor->ssdb.vcmtype)
274 snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev));
275 board_info.dev_name = name;
276 strscpy(board_info.type, ipu_vcm_types[sensor->ssdb.vcmtype - 1],
277 ARRAY_SIZE(board_info.type));
278 board_info.swnode = &sensor->swnodes[SWNODE_VCM];
280 sensor->vcm_i2c_client =
281 i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev),
283 if (IS_ERR(sensor->vcm_i2c_client)) {
284 dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n",
285 PTR_ERR(sensor->vcm_i2c_client));
286 sensor->vcm_i2c_client = NULL;
290 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
292 struct ipu_sensor *sensor;
295 for (i = 0; i < bridge->n_sensors; i++) {
296 sensor = &bridge->sensors[i];
297 software_node_unregister_node_group(sensor->group);
298 acpi_dev_put(sensor->adev);
299 i2c_unregister_device(sensor->vcm_i2c_client);
303 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
304 struct ipu_bridge *bridge)
306 struct fwnode_handle *fwnode, *primary;
307 struct ipu_sensor *sensor;
308 struct acpi_device *adev;
311 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
312 if (!adev->status.enabled)
315 if (bridge->n_sensors >= IPU_MAX_PORTS) {
317 dev_err(bridge->dev, "Exceeded available IPU ports\n");
321 sensor = &bridge->sensors[bridge->n_sensors];
323 * Borrow our adev ref to the sensor for now, on success
324 * acpi_dev_get(adev) is done further below.
328 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB",
330 sizeof(sensor->ssdb));
334 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
335 cfg->hid, sensor->ssdb.link);
337 if (sensor->ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
338 dev_warn(&adev->dev, "Unknown VCM type %d\n",
339 sensor->ssdb.vcmtype);
340 sensor->ssdb.vcmtype = 0;
343 if (sensor->ssdb.lanes > IPU_MAX_LANES) {
345 "Number of lanes in SSDB is invalid\n");
350 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
351 ipu_bridge_create_connection_swnodes(bridge, sensor);
353 ret = software_node_register_node_group(sensor->group);
357 fwnode = software_node_fwnode(&sensor->swnodes[
361 goto err_free_swnodes;
364 sensor->adev = acpi_dev_get(adev);
366 primary = acpi_fwnode_handle(adev);
367 primary->secondary = fwnode;
369 ipu_bridge_instantiate_vcm_i2c_client(sensor);
371 dev_info(bridge->dev, "Found supported sensor %s\n",
372 acpi_dev_name(adev));
380 software_node_unregister_node_group(sensor->group);
386 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
391 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
392 const struct ipu_sensor_config *cfg =
393 &ipu_supported_sensors[i];
395 ret = ipu_bridge_connect_sensor(cfg, bridge);
397 goto err_unregister_sensors;
402 err_unregister_sensors:
403 ipu_bridge_unregister_sensors(bridge);
408 * The VCM cannot be probed until the PMIC is completely setup. We cannot rely
409 * on -EPROBE_DEFER for this, since the consumer<->supplier relations between
410 * the VCM and regulators/clks are not described in ACPI, instead they are
411 * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work
412 * for the clks/regulators the VCM i2c-clients must not be instantiated until
413 * the PMIC is fully setup.
415 * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the
416 * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does
419 static int ipu_bridge_sensors_are_ready(void)
421 struct acpi_device *adev;
425 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
426 const struct ipu_sensor_config *cfg =
427 &ipu_supported_sensors[i];
429 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
430 if (!adev->status.enabled)
433 if (!acpi_dev_ready_for_enumeration(adev))
441 int ipu_bridge_init(struct device *dev)
443 struct fwnode_handle *fwnode;
444 struct ipu_bridge *bridge;
448 if (!ipu_bridge_sensors_are_ready())
449 return -EPROBE_DEFER;
451 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
455 strscpy(bridge->ipu_node_name, IPU_HID,
456 sizeof(bridge->ipu_node_name));
457 bridge->ipu_hid_node.name = bridge->ipu_node_name;
460 ret = software_node_register(&bridge->ipu_hid_node);
462 dev_err(dev, "Failed to register the IPU HID node\n");
463 goto err_free_bridge;
467 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
468 * only need one, rather than one per sensor). We include it as a
469 * member of the struct ipu_bridge rather than a global variable so
470 * that it survives if the module is unloaded along with the rest of
473 for (i = 0; i < IPU_MAX_LANES; i++)
474 bridge->data_lanes[i] = i + 1;
476 ret = ipu_bridge_connect_sensors(bridge);
477 if (ret || bridge->n_sensors == 0)
478 goto err_unregister_ipu;
480 dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
482 fwnode = software_node_fwnode(&bridge->ipu_hid_node);
484 dev_err(dev, "Error getting fwnode from ipu software_node\n");
486 goto err_unregister_sensors;
489 set_secondary_fwnode(dev, fwnode);
493 err_unregister_sensors:
494 ipu_bridge_unregister_sensors(bridge);
496 software_node_unregister(&bridge->ipu_hid_node);
502 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
504 MODULE_LICENSE("GPL");
505 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");