media: ipu-bridge: Move ipu-bridge.h to include/media/
[platform/kernel/linux-starfive.git] / drivers / media / pci / intel / ipu-bridge.c
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/property.h>
8
9 #include <media/ipu-bridge.h>
10 #include <media/v4l2-fwnode.h>
11
12 /*
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.
19  *
20  * Do not add an entry for a sensor that is not actually supported.
21  */
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),
35         /* Hynix hi556 */
36         IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
37         /* Omnivision ov13b10 */
38         IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
39 };
40
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",
49 };
50
51 static const char * const ipu_vcm_types[] = {
52         "ad5823",
53         "dw9714",
54         "ad5816",
55         "dw9719",
56         "dw9718",
57         "dw9806b",
58         "wv517s",
59         "lc898122xa",
60         "lc898212axb",
61 };
62
63 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
64                                        void *data, u32 size)
65 {
66         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
67         union acpi_object *obj;
68         acpi_status status;
69         int ret = 0;
70
71         status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
72         if (ACPI_FAILURE(status))
73                 return -ENODEV;
74
75         obj = buffer.pointer;
76         if (!obj) {
77                 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
78                 return -ENODEV;
79         }
80
81         if (obj->type != ACPI_TYPE_BUFFER) {
82                 dev_err(&adev->dev, "Not an ACPI buffer\n");
83                 ret = -ENODEV;
84                 goto out_free_buff;
85         }
86
87         if (obj->buffer.length > size) {
88                 dev_err(&adev->dev, "Given buffer is too small\n");
89                 ret = -EINVAL;
90                 goto out_free_buff;
91         }
92
93         memcpy(data, obj->buffer.pointer, obj->buffer.length);
94
95 out_free_buff:
96         kfree(buffer.pointer);
97         return ret;
98 }
99
100 static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,
101                                      struct ipu_sensor_ssdb *ssdb)
102 {
103         switch (ssdb->degree) {
104         case IPU_SENSOR_ROTATION_NORMAL:
105                 return 0;
106         case IPU_SENSOR_ROTATION_INVERTED:
107                 return 180;
108         default:
109                 dev_warn(&adev->dev,
110                          "Unknown rotation %d. Assume 0 degree rotation\n",
111                          ssdb->degree);
112                 return 0;
113         }
114 }
115
116 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
117 {
118         enum v4l2_fwnode_orientation orientation;
119         struct acpi_pld_info *pld;
120         acpi_status status;
121
122         status = acpi_get_physical_device_location(adev->handle, &pld);
123         if (ACPI_FAILURE(status)) {
124                 dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
125                 return V4L2_FWNODE_ORIENTATION_EXTERNAL;
126         }
127
128         switch (pld->panel) {
129         case ACPI_PLD_PANEL_FRONT:
130                 orientation = V4L2_FWNODE_ORIENTATION_FRONT;
131                 break;
132         case ACPI_PLD_PANEL_BACK:
133                 orientation = V4L2_FWNODE_ORIENTATION_BACK;
134                 break;
135         case ACPI_PLD_PANEL_TOP:
136         case ACPI_PLD_PANEL_LEFT:
137         case ACPI_PLD_PANEL_RIGHT:
138         case ACPI_PLD_PANEL_UNKNOWN:
139                 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
140                 break;
141         default:
142                 dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
143                 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
144                 break;
145         }
146
147         ACPI_FREE(pld);
148         return orientation;
149 }
150
151 int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor)
152 {
153         struct ipu_sensor_ssdb ssdb = {};
154         int ret;
155
156         ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb));
157         if (ret)
158                 return ret;
159
160         if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
161                 dev_warn(&adev->dev, "Unknown VCM type %d\n", ssdb.vcmtype);
162                 ssdb.vcmtype = 0;
163         }
164
165         if (ssdb.lanes > IPU_MAX_LANES) {
166                 dev_err(&adev->dev, "Number of lanes in SSDB is invalid\n");
167                 return -EINVAL;
168         }
169
170         sensor->link = ssdb.link;
171         sensor->lanes = ssdb.lanes;
172         sensor->mclkspeed = ssdb.mclkspeed;
173         sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb);
174         sensor->orientation = ipu_bridge_parse_orientation(adev);
175
176         if (ssdb.vcmtype)
177                 sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1];
178
179         return 0;
180 }
181 EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, INTEL_IPU_BRIDGE);
182
183 static void ipu_bridge_create_fwnode_properties(
184         struct ipu_sensor *sensor,
185         struct ipu_bridge *bridge,
186         const struct ipu_sensor_config *cfg)
187 {
188         sensor->prop_names = prop_names;
189
190         sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]);
191         sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
192
193         sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
194                                         sensor->prop_names.clock_frequency,
195                                         sensor->mclkspeed);
196         sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
197                                         sensor->prop_names.rotation,
198                                         sensor->rotation);
199         sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
200                                         sensor->prop_names.orientation,
201                                         sensor->orientation);
202         if (sensor->vcm_type) {
203                 sensor->vcm_ref[0] =
204                         SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
205                 sensor->dev_properties[3] =
206                         PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
207         }
208
209         sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
210                                         sensor->prop_names.bus_type,
211                                         V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
212         sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
213                                         sensor->prop_names.data_lanes,
214                                         bridge->data_lanes, sensor->lanes);
215         sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
216                                         sensor->prop_names.remote_endpoint,
217                                         sensor->local_ref);
218
219         if (cfg->nr_link_freqs > 0)
220                 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
221                         sensor->prop_names.link_frequencies,
222                         cfg->link_freqs,
223                         cfg->nr_link_freqs);
224
225         sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
226                                         sensor->prop_names.data_lanes,
227                                         bridge->data_lanes, sensor->lanes);
228         sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
229                                         sensor->prop_names.remote_endpoint,
230                                         sensor->remote_ref);
231 }
232
233 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
234 {
235         snprintf(sensor->node_names.remote_port,
236                  sizeof(sensor->node_names.remote_port),
237                  SWNODE_GRAPH_PORT_NAME_FMT, sensor->link);
238         snprintf(sensor->node_names.port,
239                  sizeof(sensor->node_names.port),
240                  SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
241         snprintf(sensor->node_names.endpoint,
242                  sizeof(sensor->node_names.endpoint),
243                  SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
244         if (sensor->vcm_type) {
245                 /* append link to distinguish nodes with same model VCM */
246                 snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
247                          "%s-%u", sensor->vcm_type, sensor->link);
248         }
249 }
250
251 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
252 {
253         struct software_node *nodes = sensor->swnodes;
254
255         sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
256         sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
257         sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
258         sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
259         sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
260         if (sensor->vcm_type)
261                 sensor->group[SWNODE_VCM] =  &nodes[SWNODE_VCM];
262 }
263
264 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
265                                                  struct ipu_sensor *sensor)
266 {
267         struct software_node *nodes = sensor->swnodes;
268
269         ipu_bridge_init_swnode_names(sensor);
270
271         nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
272                                                sensor->dev_properties);
273         nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
274                                               &nodes[SWNODE_SENSOR_HID]);
275         nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
276                                                 sensor->node_names.endpoint,
277                                                 &nodes[SWNODE_SENSOR_PORT],
278                                                 sensor->ep_properties);
279         nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
280                                            &bridge->ipu_hid_node);
281         nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
282                                                 sensor->node_names.endpoint,
283                                                 &nodes[SWNODE_IPU_PORT],
284                                                 sensor->ipu_properties);
285         nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
286
287         ipu_bridge_init_swnode_group(sensor);
288 }
289
290 static void ipu_bridge_instantiate_vcm_i2c_client(struct ipu_sensor *sensor)
291 {
292         struct i2c_board_info board_info = { };
293         char name[16];
294
295         if (!sensor->vcm_type)
296                 return;
297
298         snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev));
299         board_info.dev_name = name;
300         strscpy(board_info.type, sensor->vcm_type, ARRAY_SIZE(board_info.type));
301         board_info.swnode = &sensor->swnodes[SWNODE_VCM];
302
303         sensor->vcm_i2c_client =
304                 i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev),
305                                               1, &board_info);
306         if (IS_ERR(sensor->vcm_i2c_client)) {
307                 dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n",
308                          PTR_ERR(sensor->vcm_i2c_client));
309                 sensor->vcm_i2c_client = NULL;
310         }
311 }
312
313 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
314 {
315         struct ipu_sensor *sensor;
316         unsigned int i;
317
318         for (i = 0; i < bridge->n_sensors; i++) {
319                 sensor = &bridge->sensors[i];
320                 software_node_unregister_node_group(sensor->group);
321                 acpi_dev_put(sensor->adev);
322                 i2c_unregister_device(sensor->vcm_i2c_client);
323         }
324 }
325
326 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
327                                      struct ipu_bridge *bridge)
328 {
329         struct fwnode_handle *fwnode, *primary;
330         struct ipu_sensor *sensor;
331         struct acpi_device *adev;
332         int ret;
333
334         for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
335                 if (!adev->status.enabled)
336                         continue;
337
338                 if (bridge->n_sensors >= IPU_MAX_PORTS) {
339                         acpi_dev_put(adev);
340                         dev_err(bridge->dev, "Exceeded available IPU ports\n");
341                         return -EINVAL;
342                 }
343
344                 sensor = &bridge->sensors[bridge->n_sensors];
345
346                 ret = bridge->parse_sensor_fwnode(adev, sensor);
347                 if (ret)
348                         goto err_put_adev;
349
350                 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
351                          cfg->hid, sensor->link);
352
353                 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
354                 ipu_bridge_create_connection_swnodes(bridge, sensor);
355
356                 ret = software_node_register_node_group(sensor->group);
357                 if (ret)
358                         goto err_put_adev;
359
360                 fwnode = software_node_fwnode(&sensor->swnodes[
361                                                       SWNODE_SENSOR_HID]);
362                 if (!fwnode) {
363                         ret = -ENODEV;
364                         goto err_free_swnodes;
365                 }
366
367                 sensor->adev = acpi_dev_get(adev);
368
369                 primary = acpi_fwnode_handle(adev);
370                 primary->secondary = fwnode;
371
372                 ipu_bridge_instantiate_vcm_i2c_client(sensor);
373
374                 dev_info(bridge->dev, "Found supported sensor %s\n",
375                          acpi_dev_name(adev));
376
377                 bridge->n_sensors++;
378         }
379
380         return 0;
381
382 err_free_swnodes:
383         software_node_unregister_node_group(sensor->group);
384 err_put_adev:
385         acpi_dev_put(adev);
386         return ret;
387 }
388
389 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
390 {
391         unsigned int i;
392         int ret;
393
394         for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
395                 const struct ipu_sensor_config *cfg =
396                         &ipu_supported_sensors[i];
397
398                 ret = ipu_bridge_connect_sensor(cfg, bridge);
399                 if (ret)
400                         goto err_unregister_sensors;
401         }
402
403         return 0;
404
405 err_unregister_sensors:
406         ipu_bridge_unregister_sensors(bridge);
407         return ret;
408 }
409
410 /*
411  * The VCM cannot be probed until the PMIC is completely setup. We cannot rely
412  * on -EPROBE_DEFER for this, since the consumer<->supplier relations between
413  * the VCM and regulators/clks are not described in ACPI, instead they are
414  * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work
415  * for the clks/regulators the VCM i2c-clients must not be instantiated until
416  * the PMIC is fully setup.
417  *
418  * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the
419  * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does
420  * for the sensors.
421  */
422 static int ipu_bridge_sensors_are_ready(void)
423 {
424         struct acpi_device *adev;
425         bool ready = true;
426         unsigned int i;
427
428         for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
429                 const struct ipu_sensor_config *cfg =
430                         &ipu_supported_sensors[i];
431
432                 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
433                         if (!adev->status.enabled)
434                                 continue;
435
436                         if (!acpi_dev_ready_for_enumeration(adev))
437                                 ready = false;
438                 }
439         }
440
441         return ready;
442 }
443
444 int ipu_bridge_init(struct device *dev,
445                     ipu_parse_sensor_fwnode_t parse_sensor_fwnode)
446 {
447         struct fwnode_handle *fwnode;
448         struct ipu_bridge *bridge;
449         unsigned int i;
450         int ret;
451
452         if (!ipu_bridge_sensors_are_ready())
453                 return -EPROBE_DEFER;
454
455         bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
456         if (!bridge)
457                 return -ENOMEM;
458
459         strscpy(bridge->ipu_node_name, IPU_HID,
460                 sizeof(bridge->ipu_node_name));
461         bridge->ipu_hid_node.name = bridge->ipu_node_name;
462         bridge->dev = dev;
463         bridge->parse_sensor_fwnode = parse_sensor_fwnode;
464
465         ret = software_node_register(&bridge->ipu_hid_node);
466         if (ret < 0) {
467                 dev_err(dev, "Failed to register the IPU HID node\n");
468                 goto err_free_bridge;
469         }
470
471         /*
472          * Map the lane arrangement, which is fixed for the IPU3 (meaning we
473          * only need one, rather than one per sensor). We include it as a
474          * member of the struct ipu_bridge rather than a global variable so
475          * that it survives if the module is unloaded along with the rest of
476          * the struct.
477          */
478         for (i = 0; i < IPU_MAX_LANES; i++)
479                 bridge->data_lanes[i] = i + 1;
480
481         ret = ipu_bridge_connect_sensors(bridge);
482         if (ret || bridge->n_sensors == 0)
483                 goto err_unregister_ipu;
484
485         dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
486
487         fwnode = software_node_fwnode(&bridge->ipu_hid_node);
488         if (!fwnode) {
489                 dev_err(dev, "Error getting fwnode from ipu software_node\n");
490                 ret = -ENODEV;
491                 goto err_unregister_sensors;
492         }
493
494         set_secondary_fwnode(dev, fwnode);
495
496         return 0;
497
498 err_unregister_sensors:
499         ipu_bridge_unregister_sensors(bridge);
500 err_unregister_ipu:
501         software_node_unregister(&bridge->ipu_hid_node);
502 err_free_bridge:
503         kfree(bridge);
504
505         return ret;
506 }
507 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
508
509 MODULE_LICENSE("GPL");
510 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");