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>
9 #include <media/ipu-bridge.h>
10 #include <media/v4l2-fwnode.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),
39 /* GalaxyCore GC0310 */
40 IPU_SENSOR_CONFIG("INT0310", 0),
43 static const struct ipu_property_names prop_names = {
44 .clock_frequency = "clock-frequency",
45 .rotation = "rotation",
46 .orientation = "orientation",
47 .bus_type = "bus-type",
48 .data_lanes = "data-lanes",
49 .remote_endpoint = "remote-endpoint",
50 .link_frequencies = "link-frequencies",
53 static const char * const ipu_vcm_types[] = {
65 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
68 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
69 union acpi_object *obj;
73 status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
74 if (ACPI_FAILURE(status))
79 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
83 if (obj->type != ACPI_TYPE_BUFFER) {
84 dev_err(&adev->dev, "Not an ACPI buffer\n");
89 if (obj->buffer.length > size) {
90 dev_err(&adev->dev, "Given buffer is too small\n");
95 memcpy(data, obj->buffer.pointer, obj->buffer.length);
98 kfree(buffer.pointer);
102 static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,
103 struct ipu_sensor_ssdb *ssdb)
105 switch (ssdb->degree) {
106 case IPU_SENSOR_ROTATION_NORMAL:
108 case IPU_SENSOR_ROTATION_INVERTED:
112 "Unknown rotation %d. Assume 0 degree rotation\n",
118 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
120 enum v4l2_fwnode_orientation orientation;
121 struct acpi_pld_info *pld;
124 status = acpi_get_physical_device_location(adev->handle, &pld);
125 if (ACPI_FAILURE(status)) {
126 dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
127 return V4L2_FWNODE_ORIENTATION_EXTERNAL;
130 switch (pld->panel) {
131 case ACPI_PLD_PANEL_FRONT:
132 orientation = V4L2_FWNODE_ORIENTATION_FRONT;
134 case ACPI_PLD_PANEL_BACK:
135 orientation = V4L2_FWNODE_ORIENTATION_BACK;
137 case ACPI_PLD_PANEL_TOP:
138 case ACPI_PLD_PANEL_LEFT:
139 case ACPI_PLD_PANEL_RIGHT:
140 case ACPI_PLD_PANEL_UNKNOWN:
141 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
144 dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
145 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
153 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
155 struct ipu_sensor_ssdb ssdb = {};
158 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
162 if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
163 dev_warn(&adev->dev, "Unknown VCM type %d\n", ssdb.vcmtype);
167 if (ssdb.lanes > IPU_MAX_LANES) {
168 dev_err(&adev->dev, "Number of lanes in SSDB is invalid\n");
172 sensor->link = ssdb.link;
173 sensor->lanes = ssdb.lanes;
174 sensor->mclkspeed = ssdb.mclkspeed;
175 sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb);
176 sensor->orientation = ipu_bridge_parse_orientation(adev);
179 sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
183 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, INTEL_IPU_BRIDGE);
185 static void ipu_bridge_create_fwnode_properties(
186 struct ipu_sensor *sensor,
187 struct ipu_bridge *bridge,
188 const struct ipu_sensor_config *cfg)
190 sensor->prop_names = prop_names;
192 sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]);
193 sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
195 sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
196 sensor->prop_names.clock_frequency,
198 sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
199 sensor->prop_names.rotation,
201 sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
202 sensor->prop_names.orientation,
203 sensor->orientation);
204 if (sensor->vcm_type) {
206 SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
207 sensor->dev_properties[3] =
208 PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
211 sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
212 sensor->prop_names.bus_type,
213 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
214 sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
215 sensor->prop_names.data_lanes,
216 bridge->data_lanes, sensor->lanes);
217 sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
218 sensor->prop_names.remote_endpoint,
221 if (cfg->nr_link_freqs > 0)
222 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
223 sensor->prop_names.link_frequencies,
227 sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
228 sensor->prop_names.data_lanes,
229 bridge->data_lanes, sensor->lanes);
230 sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
231 sensor->prop_names.remote_endpoint,
235 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
237 snprintf(sensor->node_names.remote_port,
238 sizeof(sensor->node_names.remote_port),
239 SWNODE_GRAPH_PORT_NAME_FMT, sensor->link);
240 snprintf(sensor->node_names.port,
241 sizeof(sensor->node_names.port),
242 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
243 snprintf(sensor->node_names.endpoint,
244 sizeof(sensor->node_names.endpoint),
245 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
246 if (sensor->vcm_type) {
247 /* append link to distinguish nodes with same model VCM */
248 snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
249 "%s-%u", sensor->vcm_type, sensor->link);
253 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
255 struct software_node *nodes = sensor->swnodes;
257 sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
258 sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
259 sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
260 sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
261 sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
262 if (sensor->vcm_type)
263 sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM];
266 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
267 struct ipu_sensor *sensor)
269 struct software_node *nodes = sensor->swnodes;
271 ipu_bridge_init_swnode_names(sensor);
273 nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
274 sensor->dev_properties);
275 nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
276 &nodes[SWNODE_SENSOR_HID]);
277 nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
278 sensor->node_names.endpoint,
279 &nodes[SWNODE_SENSOR_PORT],
280 sensor->ep_properties);
281 nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
282 &bridge->ipu_hid_node);
283 nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
284 sensor->node_names.endpoint,
285 &nodes[SWNODE_IPU_PORT],
286 sensor->ipu_properties);
287 nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
289 ipu_bridge_init_swnode_group(sensor);
292 static void ipu_bridge_instantiate_vcm_i2c_client(struct ipu_sensor *sensor)
294 struct i2c_board_info board_info = { };
297 if (!sensor->vcm_type)
300 snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev));
301 board_info.dev_name = name;
302 strscpy(board_info.type, sensor->vcm_type, ARRAY_SIZE(board_info.type));
303 board_info.swnode = &sensor->swnodes[SWNODE_VCM];
305 sensor->vcm_i2c_client =
306 i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev),
308 if (IS_ERR(sensor->vcm_i2c_client)) {
309 dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n",
310 PTR_ERR(sensor->vcm_i2c_client));
311 sensor->vcm_i2c_client = NULL;
315 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
317 struct ipu_sensor *sensor;
320 for (i = 0; i < bridge->n_sensors; i++) {
321 sensor = &bridge->sensors[i];
322 software_node_unregister_node_group(sensor->group);
323 acpi_dev_put(sensor->adev);
324 i2c_unregister_device(sensor->vcm_i2c_client);
328 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
329 struct ipu_bridge *bridge)
331 struct fwnode_handle *fwnode, *primary;
332 struct ipu_sensor *sensor;
333 struct acpi_device *adev;
336 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
337 if (!adev->status.enabled)
340 if (bridge->n_sensors >= IPU_MAX_PORTS) {
342 dev_err(bridge->dev, "Exceeded available IPU ports\n");
346 sensor = &bridge->sensors[bridge->n_sensors];
348 ret = bridge->parse_sensor_fwnode(adev, sensor);
352 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
353 cfg->hid, sensor->link);
355 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
356 ipu_bridge_create_connection_swnodes(bridge, sensor);
358 ret = software_node_register_node_group(sensor->group);
362 fwnode = software_node_fwnode(&sensor->swnodes[
366 goto err_free_swnodes;
369 sensor->adev = acpi_dev_get(adev);
371 primary = acpi_fwnode_handle(adev);
372 primary->secondary = fwnode;
374 ipu_bridge_instantiate_vcm_i2c_client(sensor);
376 dev_info(bridge->dev, "Found supported sensor %s\n",
377 acpi_dev_name(adev));
385 software_node_unregister_node_group(sensor->group);
391 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
396 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
397 const struct ipu_sensor_config *cfg =
398 &ipu_supported_sensors[i];
400 ret = ipu_bridge_connect_sensor(cfg, bridge);
402 goto err_unregister_sensors;
407 err_unregister_sensors:
408 ipu_bridge_unregister_sensors(bridge);
413 * The VCM cannot be probed until the PMIC is completely setup. We cannot rely
414 * on -EPROBE_DEFER for this, since the consumer<->supplier relations between
415 * the VCM and regulators/clks are not described in ACPI, instead they are
416 * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work
417 * for the clks/regulators the VCM i2c-clients must not be instantiated until
418 * the PMIC is fully setup.
420 * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the
421 * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does
424 static int ipu_bridge_sensors_are_ready(void)
426 struct acpi_device *adev;
430 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
431 const struct ipu_sensor_config *cfg =
432 &ipu_supported_sensors[i];
434 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
435 if (!adev->status.enabled)
438 if (!acpi_dev_ready_for_enumeration(adev))
446 int ipu_bridge_init(struct device *dev,
447 ipu_parse_sensor_fwnode_t parse_sensor_fwnode)
449 struct fwnode_handle *fwnode;
450 struct ipu_bridge *bridge;
454 if (!ipu_bridge_sensors_are_ready())
455 return -EPROBE_DEFER;
457 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
461 strscpy(bridge->ipu_node_name, IPU_HID,
462 sizeof(bridge->ipu_node_name));
463 bridge->ipu_hid_node.name = bridge->ipu_node_name;
465 bridge->parse_sensor_fwnode = parse_sensor_fwnode;
467 ret = software_node_register(&bridge->ipu_hid_node);
469 dev_err(dev, "Failed to register the IPU HID node\n");
470 goto err_free_bridge;
474 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
475 * only need one, rather than one per sensor). We include it as a
476 * member of the struct ipu_bridge rather than a global variable so
477 * that it survives if the module is unloaded along with the rest of
480 for (i = 0; i < IPU_MAX_LANES; i++)
481 bridge->data_lanes[i] = i + 1;
483 ret = ipu_bridge_connect_sensors(bridge);
484 if (ret || bridge->n_sensors == 0)
485 goto err_unregister_ipu;
487 dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
489 fwnode = software_node_fwnode(&bridge->ipu_hid_node);
491 dev_err(dev, "Error getting fwnode from ipu software_node\n");
493 goto err_unregister_sensors;
496 set_secondary_fwnode(dev, fwnode);
500 err_unregister_sensors:
501 ipu_bridge_unregister_sensors(bridge);
503 software_node_unregister(&bridge->ipu_hid_node);
509 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
511 MODULE_LICENSE("GPL");
512 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");