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/pm_runtime.h>
8 #include <linux/property.h>
9 #include <linux/string.h>
10 #include <linux/workqueue.h>
12 #include <media/ipu-bridge.h>
13 #include <media/v4l2-fwnode.h>
16 * Extend this array with ACPI Hardware IDs of devices known to be working
17 * plus the number of link-frequencies expected by their drivers, along with
18 * the frequency values in hertz. This is somewhat opportunistic way of adding
19 * support for this for now in the hopes of a better source for the information
20 * (possibly some encoded value in the SSDB buffer that we're unaware of)
21 * becoming apparent in the future.
23 * Do not add an entry for a sensor that is not actually supported.
25 static const struct ipu_sensor_config ipu_supported_sensors[] = {
26 /* Omnivision OV5693 */
27 IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
28 /* Omnivision OV8865 */
29 IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
30 /* Omnivision OV7251 */
31 IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
32 /* Omnivision OV2680 */
33 IPU_SENSOR_CONFIG("OVTI2680", 0),
34 /* Omnivision ov8856 */
35 IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
36 /* Omnivision ov2740 */
37 IPU_SENSOR_CONFIG("INT3474", 1, 360000000),
39 IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
40 /* Omnivision ov13b10 */
41 IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
42 /* GalaxyCore GC0310 */
43 IPU_SENSOR_CONFIG("INT0310", 0),
46 static const struct ipu_property_names prop_names = {
47 .clock_frequency = "clock-frequency",
48 .rotation = "rotation",
49 .orientation = "orientation",
50 .bus_type = "bus-type",
51 .data_lanes = "data-lanes",
52 .remote_endpoint = "remote-endpoint",
53 .link_frequencies = "link-frequencies",
56 static const char * const ipu_vcm_types[] = {
68 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
71 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
72 union acpi_object *obj;
76 status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
77 if (ACPI_FAILURE(status))
82 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
86 if (obj->type != ACPI_TYPE_BUFFER) {
87 dev_err(&adev->dev, "Not an ACPI buffer\n");
92 if (obj->buffer.length > size) {
93 dev_err(&adev->dev, "Given buffer is too small\n");
98 memcpy(data, obj->buffer.pointer, obj->buffer.length);
101 kfree(buffer.pointer);
105 static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,
106 struct ipu_sensor_ssdb *ssdb)
108 switch (ssdb->degree) {
109 case IPU_SENSOR_ROTATION_NORMAL:
111 case IPU_SENSOR_ROTATION_INVERTED:
115 "Unknown rotation %d. Assume 0 degree rotation\n",
121 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
123 enum v4l2_fwnode_orientation orientation;
124 struct acpi_pld_info *pld;
127 status = acpi_get_physical_device_location(adev->handle, &pld);
128 if (ACPI_FAILURE(status)) {
129 dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
130 return V4L2_FWNODE_ORIENTATION_EXTERNAL;
133 switch (pld->panel) {
134 case ACPI_PLD_PANEL_FRONT:
135 orientation = V4L2_FWNODE_ORIENTATION_FRONT;
137 case ACPI_PLD_PANEL_BACK:
138 orientation = V4L2_FWNODE_ORIENTATION_BACK;
140 case ACPI_PLD_PANEL_TOP:
141 case ACPI_PLD_PANEL_LEFT:
142 case ACPI_PLD_PANEL_RIGHT:
143 case ACPI_PLD_PANEL_UNKNOWN:
144 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
147 dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
148 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
156 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
158 struct ipu_sensor_ssdb ssdb = {};
161 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
165 if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
166 dev_warn(&adev->dev, "Unknown VCM type %d\n", ssdb.vcmtype);
170 if (ssdb.lanes > IPU_MAX_LANES) {
171 dev_err(&adev->dev, "Number of lanes in SSDB is invalid\n");
175 sensor->link = ssdb.link;
176 sensor->lanes = ssdb.lanes;
177 sensor->mclkspeed = ssdb.mclkspeed;
178 sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb);
179 sensor->orientation = ipu_bridge_parse_orientation(adev);
182 sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
186 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, INTEL_IPU_BRIDGE);
188 static void ipu_bridge_create_fwnode_properties(
189 struct ipu_sensor *sensor,
190 struct ipu_bridge *bridge,
191 const struct ipu_sensor_config *cfg)
193 sensor->prop_names = prop_names;
195 sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]);
196 sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
198 sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
199 sensor->prop_names.clock_frequency,
201 sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
202 sensor->prop_names.rotation,
204 sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
205 sensor->prop_names.orientation,
206 sensor->orientation);
207 if (sensor->vcm_type) {
209 SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
210 sensor->dev_properties[3] =
211 PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
214 sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
215 sensor->prop_names.bus_type,
216 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
217 sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
218 sensor->prop_names.data_lanes,
219 bridge->data_lanes, sensor->lanes);
220 sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
221 sensor->prop_names.remote_endpoint,
224 if (cfg->nr_link_freqs > 0)
225 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
226 sensor->prop_names.link_frequencies,
230 sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
231 sensor->prop_names.data_lanes,
232 bridge->data_lanes, sensor->lanes);
233 sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
234 sensor->prop_names.remote_endpoint,
238 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
240 snprintf(sensor->node_names.remote_port,
241 sizeof(sensor->node_names.remote_port),
242 SWNODE_GRAPH_PORT_NAME_FMT, sensor->link);
243 snprintf(sensor->node_names.port,
244 sizeof(sensor->node_names.port),
245 SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
246 snprintf(sensor->node_names.endpoint,
247 sizeof(sensor->node_names.endpoint),
248 SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
249 if (sensor->vcm_type) {
250 /* append link to distinguish nodes with same model VCM */
251 snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
252 "%s-%u", sensor->vcm_type, sensor->link);
256 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
258 struct software_node *nodes = sensor->swnodes;
260 sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
261 sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
262 sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
263 sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
264 sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
265 if (sensor->vcm_type)
266 sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM];
269 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
270 struct ipu_sensor *sensor)
272 struct software_node *nodes = sensor->swnodes;
274 ipu_bridge_init_swnode_names(sensor);
276 nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
277 sensor->dev_properties);
278 nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
279 &nodes[SWNODE_SENSOR_HID]);
280 nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
281 sensor->node_names.endpoint,
282 &nodes[SWNODE_SENSOR_PORT],
283 sensor->ep_properties);
284 nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
285 &bridge->ipu_hid_node);
286 nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
287 sensor->node_names.endpoint,
288 &nodes[SWNODE_IPU_PORT],
289 sensor->ipu_properties);
290 nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
292 ipu_bridge_init_swnode_group(sensor);
296 * The actual instantiation must be done from a workqueue to avoid
297 * a deadlock on taking list_lock from v4l2-async twice.
299 struct ipu_bridge_instantiate_vcm_work_data {
300 struct work_struct work;
301 struct device *sensor;
303 struct i2c_board_info board_info;
306 static void ipu_bridge_instantiate_vcm_work(struct work_struct *work)
308 struct ipu_bridge_instantiate_vcm_work_data *data =
309 container_of(work, struct ipu_bridge_instantiate_vcm_work_data,
311 struct acpi_device *adev = ACPI_COMPANION(data->sensor);
312 struct i2c_client *vcm_client;
313 bool put_fwnode = true;
317 * The client may get probed before the device_link gets added below
318 * make sure the sensor is powered-up during probe.
320 ret = pm_runtime_get_sync(data->sensor);
322 dev_err(data->sensor, "Error %d runtime-resuming sensor, cannot instantiate VCM\n",
328 * Note the client is created only once and then kept around
329 * even after a rmmod, just like the software-nodes.
331 vcm_client = i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(adev),
332 1, &data->board_info);
333 if (IS_ERR(vcm_client)) {
334 dev_err(data->sensor, "Error instantiating VCM client: %ld\n",
335 PTR_ERR(vcm_client));
339 device_link_add(&vcm_client->dev, data->sensor, DL_FLAG_PM_RUNTIME);
341 dev_info(data->sensor, "Instantiated %s VCM\n", data->board_info.type);
342 put_fwnode = false; /* Ownership has passed to the i2c-client */
345 pm_runtime_put(data->sensor);
346 put_device(data->sensor);
348 fwnode_handle_put(data->board_info.fwnode);
352 int ipu_bridge_instantiate_vcm(struct device *sensor)
354 struct ipu_bridge_instantiate_vcm_work_data *data;
355 struct fwnode_handle *vcm_fwnode;
356 struct i2c_client *vcm_client;
357 struct acpi_device *adev;
360 adev = ACPI_COMPANION(sensor);
364 vcm_fwnode = fwnode_find_reference(dev_fwnode(sensor), "lens-focus", 0);
365 if (IS_ERR(vcm_fwnode))
368 /* When reloading modules the client will already exist */
369 vcm_client = i2c_find_device_by_fwnode(vcm_fwnode);
371 fwnode_handle_put(vcm_fwnode);
372 put_device(&vcm_client->dev);
376 data = kzalloc(sizeof(*data), GFP_KERNEL);
378 fwnode_handle_put(vcm_fwnode);
382 INIT_WORK(&data->work, ipu_bridge_instantiate_vcm_work);
383 data->sensor = get_device(sensor);
384 snprintf(data->name, sizeof(data->name), "%s-VCM",
385 acpi_dev_name(adev));
386 data->board_info.dev_name = data->name;
387 data->board_info.fwnode = vcm_fwnode;
388 snprintf(data->board_info.type, sizeof(data->board_info.type),
389 "%pfwP", vcm_fwnode);
390 /* Strip "-<link>" postfix */
391 sep = strchrnul(data->board_info.type, '-');
394 queue_work(system_long_wq, &data->work);
398 EXPORT_SYMBOL_NS_GPL(ipu_bridge_instantiate_vcm, INTEL_IPU_BRIDGE);
400 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
402 struct ipu_sensor *sensor;
405 for (i = 0; i < bridge->n_sensors; i++) {
406 sensor = &bridge->sensors[i];
407 software_node_unregister_node_group(sensor->group);
408 acpi_dev_put(sensor->adev);
412 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
413 struct ipu_bridge *bridge)
415 struct fwnode_handle *fwnode, *primary;
416 struct ipu_sensor *sensor;
417 struct acpi_device *adev;
420 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
421 if (!adev->status.enabled)
424 if (bridge->n_sensors >= IPU_MAX_PORTS) {
426 dev_err(bridge->dev, "Exceeded available IPU ports\n");
430 sensor = &bridge->sensors[bridge->n_sensors];
432 ret = bridge->parse_sensor_fwnode(adev, sensor);
436 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
437 cfg->hid, sensor->link);
439 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
440 ipu_bridge_create_connection_swnodes(bridge, sensor);
442 ret = software_node_register_node_group(sensor->group);
446 fwnode = software_node_fwnode(&sensor->swnodes[
450 goto err_free_swnodes;
453 sensor->adev = acpi_dev_get(adev);
455 primary = acpi_fwnode_handle(adev);
456 primary->secondary = fwnode;
458 dev_info(bridge->dev, "Found supported sensor %s\n",
459 acpi_dev_name(adev));
467 software_node_unregister_node_group(sensor->group);
473 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
478 for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
479 const struct ipu_sensor_config *cfg =
480 &ipu_supported_sensors[i];
482 ret = ipu_bridge_connect_sensor(cfg, bridge);
484 goto err_unregister_sensors;
489 err_unregister_sensors:
490 ipu_bridge_unregister_sensors(bridge);
494 int ipu_bridge_init(struct device *dev,
495 ipu_parse_sensor_fwnode_t parse_sensor_fwnode)
497 struct fwnode_handle *fwnode;
498 struct ipu_bridge *bridge;
502 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
506 strscpy(bridge->ipu_node_name, IPU_HID,
507 sizeof(bridge->ipu_node_name));
508 bridge->ipu_hid_node.name = bridge->ipu_node_name;
510 bridge->parse_sensor_fwnode = parse_sensor_fwnode;
512 ret = software_node_register(&bridge->ipu_hid_node);
514 dev_err(dev, "Failed to register the IPU HID node\n");
515 goto err_free_bridge;
519 * Map the lane arrangement, which is fixed for the IPU3 (meaning we
520 * only need one, rather than one per sensor). We include it as a
521 * member of the struct ipu_bridge rather than a global variable so
522 * that it survives if the module is unloaded along with the rest of
525 for (i = 0; i < IPU_MAX_LANES; i++)
526 bridge->data_lanes[i] = i + 1;
528 ret = ipu_bridge_connect_sensors(bridge);
529 if (ret || bridge->n_sensors == 0)
530 goto err_unregister_ipu;
532 dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
534 fwnode = software_node_fwnode(&bridge->ipu_hid_node);
536 dev_err(dev, "Error getting fwnode from ipu software_node\n");
538 goto err_unregister_sensors;
541 set_secondary_fwnode(dev, fwnode);
545 err_unregister_sensors:
546 ipu_bridge_unregister_sensors(bridge);
548 software_node_unregister(&bridge->ipu_hid_node);
554 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
556 MODULE_LICENSE("GPL");
557 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");